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> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 53 | #include "md.h" |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 54 | #include "raid5.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 55 | #include "bitmap.h" |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* |
| 58 | * Stripe cache |
| 59 | */ |
| 60 | |
| 61 | #define NR_STRIPES 256 |
| 62 | #define STRIPE_SIZE PAGE_SIZE |
| 63 | #define STRIPE_SHIFT (PAGE_SHIFT - 9) |
| 64 | #define STRIPE_SECTORS (STRIPE_SIZE>>9) |
| 65 | #define IO_THRESHOLD 1 |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 66 | #define BYPASS_THRESHOLD 1 |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 67 | #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #define HASH_MASK (NR_HASH - 1) |
| 69 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 70 | #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] | 71 | |
| 72 | /* bio's attached to a stripe+device for I/O are linked together in bi_sector |
| 73 | * order without overlap. There may be several bio's per stripe+device, and |
| 74 | * a bio could span several devices. |
| 75 | * When walking this list for a particular stripe+device, we must never proceed |
| 76 | * beyond a bio that extends past this device, as the next bio might no longer |
| 77 | * be valid. |
| 78 | * This macro is used to determine the 'next' bio in the list, given the sector |
| 79 | * of the current stripe+device |
| 80 | */ |
| 81 | #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL) |
| 82 | /* |
| 83 | * The following can be used to debug the driver |
| 84 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | #define RAID5_PARANOIA 1 |
| 86 | #if RAID5_PARANOIA && defined(CONFIG_SMP) |
| 87 | # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock) |
| 88 | #else |
| 89 | # define CHECK_DEVLOCK() |
| 90 | #endif |
| 91 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 92 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | #define inline |
| 94 | #define __inline__ |
| 95 | #endif |
| 96 | |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 97 | #define printk_rl(args...) ((void) (printk_ratelimit() && printk(args))) |
| 98 | |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 99 | /* |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 100 | * We maintain a biased count of active stripes in the bottom 16 bits of |
| 101 | * 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] | 102 | */ |
| 103 | static inline int raid5_bi_phys_segments(struct bio *bio) |
| 104 | { |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 105 | return bio->bi_phys_segments & 0xffff; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static inline int raid5_bi_hw_segments(struct bio *bio) |
| 109 | { |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 110 | return (bio->bi_phys_segments >> 16) & 0xffff; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static inline int raid5_dec_bi_phys_segments(struct bio *bio) |
| 114 | { |
| 115 | --bio->bi_phys_segments; |
| 116 | return raid5_bi_phys_segments(bio); |
| 117 | } |
| 118 | |
| 119 | static inline int raid5_dec_bi_hw_segments(struct bio *bio) |
| 120 | { |
| 121 | unsigned short val = raid5_bi_hw_segments(bio); |
| 122 | |
| 123 | --val; |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 124 | bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 125 | return val; |
| 126 | } |
| 127 | |
| 128 | static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt) |
| 129 | { |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 130 | bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 131 | } |
| 132 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 133 | /* Find first data disk in a raid6 stripe */ |
| 134 | static inline int raid6_d0(struct stripe_head *sh) |
| 135 | { |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 136 | if (sh->ddf_layout) |
| 137 | /* ddf always start from first device */ |
| 138 | return 0; |
| 139 | /* md starts just after Q block */ |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 140 | if (sh->qd_idx == sh->disks - 1) |
| 141 | return 0; |
| 142 | else |
| 143 | return sh->qd_idx + 1; |
| 144 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 145 | static inline int raid6_next_disk(int disk, int raid_disks) |
| 146 | { |
| 147 | disk++; |
| 148 | return (disk < raid_disks) ? disk : 0; |
| 149 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 150 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 151 | /* When walking through the disks in a raid5, starting at raid6_d0, |
| 152 | * We need to map each disk to a 'slot', where the data disks are slot |
| 153 | * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk |
| 154 | * is raid_disks-1. This help does that mapping. |
| 155 | */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 156 | static int raid6_idx_to_slot(int idx, struct stripe_head *sh, |
| 157 | int *count, int syndrome_disks) |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 158 | { |
| 159 | int slot; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 160 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 161 | if (idx == sh->pd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 162 | return syndrome_disks; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 163 | if (idx == sh->qd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 164 | return syndrome_disks + 1; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 165 | slot = (*count)++; |
| 166 | return slot; |
| 167 | } |
| 168 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 169 | static void return_io(struct bio *return_bi) |
| 170 | { |
| 171 | struct bio *bi = return_bi; |
| 172 | while (bi) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 173 | |
| 174 | return_bi = bi->bi_next; |
| 175 | bi->bi_next = NULL; |
| 176 | bi->bi_size = 0; |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 177 | bio_endio(bi, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 178 | bi = return_bi; |
| 179 | } |
| 180 | } |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | static void print_raid5_conf (raid5_conf_t *conf); |
| 183 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 184 | static int stripe_operations_active(struct stripe_head *sh) |
| 185 | { |
| 186 | return sh->check_state || sh->reconstruct_state || |
| 187 | test_bit(STRIPE_BIOFILL_RUN, &sh->state) || |
| 188 | test_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 189 | } |
| 190 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 191 | static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | if (atomic_dec_and_test(&sh->count)) { |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 194 | BUG_ON(!list_empty(&sh->lru)); |
| 195 | BUG_ON(atomic_read(&conf->active_stripes)==0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
NeilBrown | 7c785b7 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 197 | if (test_bit(STRIPE_DELAYED, &sh->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | list_add_tail(&sh->lru, &conf->delayed_list); |
NeilBrown | 7c785b7 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 199 | blk_plug_device(conf->mddev->queue); |
| 200 | } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 201 | sh->bm_seq - conf->seq_write > 0) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 202 | list_add_tail(&sh->lru, &conf->bitmap_list); |
NeilBrown | 7c785b7 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 203 | blk_plug_device(conf->mddev->queue); |
| 204 | } else { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 205 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | list_add_tail(&sh->lru, &conf->handle_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | md_wakeup_thread(conf->mddev->thread); |
| 209 | } else { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 210 | BUG_ON(stripe_operations_active(sh)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 212 | atomic_dec(&conf->preread_active_stripes); |
| 213 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) |
| 214 | md_wakeup_thread(conf->mddev->thread); |
| 215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | atomic_dec(&conf->active_stripes); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 217 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
| 218 | list_add_tail(&sh->lru, &conf->inactive_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | wake_up(&conf->wait_for_stripe); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 220 | if (conf->retry_read_aligned) |
| 221 | md_wakeup_thread(conf->mddev->thread); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 222 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | } |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | static void release_stripe(struct stripe_head *sh) |
| 228 | { |
| 229 | raid5_conf_t *conf = sh->raid_conf; |
| 230 | unsigned long flags; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | spin_lock_irqsave(&conf->device_lock, flags); |
| 233 | __release_stripe(conf, sh); |
| 234 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 235 | } |
| 236 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 237 | static inline void remove_hash(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 239 | pr_debug("remove_hash(), stripe %llu\n", |
| 240 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 242 | hlist_del_init(&sh->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 245 | 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] | 246 | { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 247 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 249 | pr_debug("insert_hash(), stripe %llu\n", |
| 250 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | CHECK_DEVLOCK(); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 253 | hlist_add_head(&sh->hash, hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | |
| 257 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
| 258 | static struct stripe_head *get_free_stripe(raid5_conf_t *conf) |
| 259 | { |
| 260 | struct stripe_head *sh = NULL; |
| 261 | struct list_head *first; |
| 262 | |
| 263 | CHECK_DEVLOCK(); |
| 264 | if (list_empty(&conf->inactive_list)) |
| 265 | goto out; |
| 266 | first = conf->inactive_list.next; |
| 267 | sh = list_entry(first, struct stripe_head, lru); |
| 268 | list_del_init(first); |
| 269 | remove_hash(sh); |
| 270 | atomic_inc(&conf->active_stripes); |
| 271 | out: |
| 272 | return sh; |
| 273 | } |
| 274 | |
| 275 | static void shrink_buffers(struct stripe_head *sh, int num) |
| 276 | { |
| 277 | struct page *p; |
| 278 | int i; |
| 279 | |
| 280 | for (i=0; i<num ; i++) { |
| 281 | p = sh->dev[i].page; |
| 282 | if (!p) |
| 283 | continue; |
| 284 | sh->dev[i].page = NULL; |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 285 | put_page(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | static int grow_buffers(struct stripe_head *sh, int num) |
| 290 | { |
| 291 | int i; |
| 292 | |
| 293 | for (i=0; i<num; i++) { |
| 294 | struct page *page; |
| 295 | |
| 296 | if (!(page = alloc_page(GFP_KERNEL))) { |
| 297 | return 1; |
| 298 | } |
| 299 | sh->dev[i].page = page; |
| 300 | } |
| 301 | return 0; |
| 302 | } |
| 303 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 304 | static void raid5_build_block(struct stripe_head *sh, int i, int previous); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 305 | static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous, |
| 306 | struct stripe_head *sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 308 | 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] | 309 | { |
| 310 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 311 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 313 | BUG_ON(atomic_read(&sh->count) != 0); |
| 314 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 315 | BUG_ON(stripe_operations_active(sh)); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | CHECK_DEVLOCK(); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 318 | pr_debug("init_stripe called, stripe %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | (unsigned long long)sh->sector); |
| 320 | |
| 321 | remove_hash(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 322 | |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 323 | sh->generation = conf->generation - previous; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 324 | sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | sh->sector = sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 326 | stripe_set_idx(sector, conf, previous, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | sh->state = 0; |
| 328 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 329 | |
| 330 | for (i = sh->disks; i--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | struct r5dev *dev = &sh->dev[i]; |
| 332 | |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 333 | if (dev->toread || dev->read || dev->towrite || dev->written || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | test_bit(R5_LOCKED, &dev->flags)) { |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 335 | printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | (unsigned long long)sh->sector, i, dev->toread, |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 337 | dev->read, dev->towrite, dev->written, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | test_bit(R5_LOCKED, &dev->flags)); |
| 339 | BUG(); |
| 340 | } |
| 341 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 342 | raid5_build_block(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | insert_hash(conf, sh); |
| 345 | } |
| 346 | |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 347 | static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, |
| 348 | short generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | { |
| 350 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 351 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
| 353 | CHECK_DEVLOCK(); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 354 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 355 | hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash) |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 356 | if (sh->sector == sector && sh->generation == generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | return sh; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 358 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | static void unplug_slaves(mddev_t *mddev); |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 363 | static void raid5_unplug_device(struct request_queue *q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 365 | static struct stripe_head * |
| 366 | get_active_stripe(raid5_conf_t *conf, sector_t sector, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 367 | int previous, int noblock, int noquiesce) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
| 369 | struct stripe_head *sh; |
| 370 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 371 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | spin_lock_irq(&conf->device_lock); |
| 374 | |
| 375 | do { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 376 | wait_event_lock_irq(conf->wait_for_stripe, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 377 | conf->quiesce == 0 || noquiesce, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 378 | conf->device_lock, /* nothing */); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 379 | sh = __find_stripe(conf, sector, conf->generation - previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | if (!sh) { |
| 381 | if (!conf->inactive_blocked) |
| 382 | sh = get_free_stripe(conf); |
| 383 | if (noblock && sh == NULL) |
| 384 | break; |
| 385 | if (!sh) { |
| 386 | conf->inactive_blocked = 1; |
| 387 | wait_event_lock_irq(conf->wait_for_stripe, |
| 388 | !list_empty(&conf->inactive_list) && |
NeilBrown | 5036805 | 2005-12-12 02:39:17 -0800 | [diff] [blame] | 389 | (atomic_read(&conf->active_stripes) |
| 390 | < (conf->max_nr_stripes *3/4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | || !conf->inactive_blocked), |
| 392 | conf->device_lock, |
NeilBrown | f437078 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 393 | raid5_unplug_device(conf->mddev->queue) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | ); |
| 395 | conf->inactive_blocked = 0; |
| 396 | } else |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 397 | init_stripe(sh, sector, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } else { |
| 399 | if (atomic_read(&sh->count)) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 400 | BUG_ON(!list_empty(&sh->lru) |
| 401 | && !test_bit(STRIPE_EXPANDING, &sh->state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } else { |
| 403 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 404 | atomic_inc(&conf->active_stripes); |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 405 | if (list_empty(&sh->lru) && |
| 406 | !test_bit(STRIPE_EXPANDING, &sh->state)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 407 | BUG(); |
| 408 | list_del_init(&sh->lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | } while (sh == NULL); |
| 412 | |
| 413 | if (sh) |
| 414 | atomic_inc(&sh->count); |
| 415 | |
| 416 | spin_unlock_irq(&conf->device_lock); |
| 417 | return sh; |
| 418 | } |
| 419 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 420 | static void |
| 421 | raid5_end_read_request(struct bio *bi, int error); |
| 422 | static void |
| 423 | raid5_end_write_request(struct bio *bi, int error); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 424 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 425 | 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] | 426 | { |
| 427 | raid5_conf_t *conf = sh->raid_conf; |
| 428 | int i, disks = sh->disks; |
| 429 | |
| 430 | might_sleep(); |
| 431 | |
| 432 | for (i = disks; i--; ) { |
| 433 | int rw; |
| 434 | struct bio *bi; |
| 435 | mdk_rdev_t *rdev; |
| 436 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) |
| 437 | rw = WRITE; |
| 438 | else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
| 439 | rw = READ; |
| 440 | else |
| 441 | continue; |
| 442 | |
| 443 | bi = &sh->dev[i].req; |
| 444 | |
| 445 | bi->bi_rw = rw; |
| 446 | if (rw == WRITE) |
| 447 | bi->bi_end_io = raid5_end_write_request; |
| 448 | else |
| 449 | bi->bi_end_io = raid5_end_read_request; |
| 450 | |
| 451 | rcu_read_lock(); |
| 452 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 453 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 454 | rdev = NULL; |
| 455 | if (rdev) |
| 456 | atomic_inc(&rdev->nr_pending); |
| 457 | rcu_read_unlock(); |
| 458 | |
| 459 | if (rdev) { |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 460 | if (s->syncing || s->expanding || s->expanded) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 461 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 462 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 463 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 464 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 465 | bi->bi_bdev = rdev->bdev; |
| 466 | pr_debug("%s: for %llu schedule op %ld on disc %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 467 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 468 | bi->bi_rw, i); |
| 469 | atomic_inc(&sh->count); |
| 470 | bi->bi_sector = sh->sector + rdev->data_offset; |
| 471 | bi->bi_flags = 1 << BIO_UPTODATE; |
| 472 | bi->bi_vcnt = 1; |
| 473 | bi->bi_max_vecs = 1; |
| 474 | bi->bi_idx = 0; |
| 475 | bi->bi_io_vec = &sh->dev[i].vec; |
| 476 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 477 | bi->bi_io_vec[0].bv_offset = 0; |
| 478 | bi->bi_size = STRIPE_SIZE; |
| 479 | bi->bi_next = NULL; |
| 480 | if (rw == WRITE && |
| 481 | test_bit(R5_ReWrite, &sh->dev[i].flags)) |
| 482 | atomic_add(STRIPE_SECTORS, |
| 483 | &rdev->corrected_errors); |
| 484 | generic_make_request(bi); |
| 485 | } else { |
| 486 | if (rw == WRITE) |
| 487 | set_bit(STRIPE_DEGRADED, &sh->state); |
| 488 | pr_debug("skip op %ld on disc %d for sector %llu\n", |
| 489 | bi->bi_rw, i, (unsigned long long)sh->sector); |
| 490 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 491 | set_bit(STRIPE_HANDLE, &sh->state); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | static struct dma_async_tx_descriptor * |
| 497 | async_copy_data(int frombio, struct bio *bio, struct page *page, |
| 498 | sector_t sector, struct dma_async_tx_descriptor *tx) |
| 499 | { |
| 500 | struct bio_vec *bvl; |
| 501 | struct page *bio_page; |
| 502 | int i; |
| 503 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 504 | struct async_submit_ctl submit; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 505 | enum async_tx_flags flags = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 506 | |
| 507 | if (bio->bi_sector >= sector) |
| 508 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 509 | else |
| 510 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 511 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 512 | if (frombio) |
| 513 | flags |= ASYNC_TX_FENCE; |
| 514 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); |
| 515 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 516 | bio_for_each_segment(bvl, bio, i) { |
| 517 | int len = bio_iovec_idx(bio, i)->bv_len; |
| 518 | int clen; |
| 519 | int b_offset = 0; |
| 520 | |
| 521 | if (page_offset < 0) { |
| 522 | b_offset = -page_offset; |
| 523 | page_offset += b_offset; |
| 524 | len -= b_offset; |
| 525 | } |
| 526 | |
| 527 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 528 | clen = STRIPE_SIZE - page_offset; |
| 529 | else |
| 530 | clen = len; |
| 531 | |
| 532 | if (clen > 0) { |
| 533 | b_offset += bio_iovec_idx(bio, i)->bv_offset; |
| 534 | bio_page = bio_iovec_idx(bio, i)->bv_page; |
| 535 | if (frombio) |
| 536 | tx = async_memcpy(page, bio_page, page_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 537 | b_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 538 | else |
| 539 | tx = async_memcpy(bio_page, page, b_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 540 | page_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 541 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 542 | /* chain the operations */ |
| 543 | submit.depend_tx = tx; |
| 544 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 545 | if (clen < len) /* hit end of page */ |
| 546 | break; |
| 547 | page_offset += len; |
| 548 | } |
| 549 | |
| 550 | return tx; |
| 551 | } |
| 552 | |
| 553 | static void ops_complete_biofill(void *stripe_head_ref) |
| 554 | { |
| 555 | struct stripe_head *sh = stripe_head_ref; |
| 556 | struct bio *return_bi = NULL; |
| 557 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 558 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 559 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 560 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 561 | (unsigned long long)sh->sector); |
| 562 | |
| 563 | /* clear completed biofills */ |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 564 | spin_lock_irq(&conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 565 | for (i = sh->disks; i--; ) { |
| 566 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 567 | |
| 568 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 569 | /* and check if we need to reply to a read request, |
| 570 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 571 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 572 | */ |
| 573 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 574 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 575 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 576 | BUG_ON(!dev->read); |
| 577 | rbi = dev->read; |
| 578 | dev->read = NULL; |
| 579 | while (rbi && rbi->bi_sector < |
| 580 | dev->sector + STRIPE_SECTORS) { |
| 581 | rbi2 = r5_next_bio(rbi, dev->sector); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 582 | if (!raid5_dec_bi_phys_segments(rbi)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 583 | rbi->bi_next = return_bi; |
| 584 | return_bi = rbi; |
| 585 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 586 | rbi = rbi2; |
| 587 | } |
| 588 | } |
| 589 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 590 | spin_unlock_irq(&conf->device_lock); |
| 591 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 592 | |
| 593 | return_io(return_bi); |
| 594 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 595 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 596 | release_stripe(sh); |
| 597 | } |
| 598 | |
| 599 | static void ops_run_biofill(struct stripe_head *sh) |
| 600 | { |
| 601 | struct dma_async_tx_descriptor *tx = NULL; |
| 602 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 603 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 604 | int i; |
| 605 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 606 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 607 | (unsigned long long)sh->sector); |
| 608 | |
| 609 | for (i = sh->disks; i--; ) { |
| 610 | struct r5dev *dev = &sh->dev[i]; |
| 611 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 612 | struct bio *rbi; |
| 613 | spin_lock_irq(&conf->device_lock); |
| 614 | dev->read = rbi = dev->toread; |
| 615 | dev->toread = NULL; |
| 616 | spin_unlock_irq(&conf->device_lock); |
| 617 | while (rbi && rbi->bi_sector < |
| 618 | dev->sector + STRIPE_SECTORS) { |
| 619 | tx = async_copy_data(0, rbi, dev->page, |
| 620 | dev->sector, tx); |
| 621 | rbi = r5_next_bio(rbi, dev->sector); |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 627 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 628 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 631 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 632 | { |
| 633 | struct r5dev *tgt; |
| 634 | |
| 635 | if (target < 0) |
| 636 | return; |
| 637 | |
| 638 | tgt = &sh->dev[target]; |
| 639 | set_bit(R5_UPTODATE, &tgt->flags); |
| 640 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 641 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 642 | } |
| 643 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 644 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 645 | { |
| 646 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 647 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 648 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 649 | (unsigned long long)sh->sector); |
| 650 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 651 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 652 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 653 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 654 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 655 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 656 | if (sh->check_state == check_state_compute_run) |
| 657 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 658 | set_bit(STRIPE_HANDLE, &sh->state); |
| 659 | release_stripe(sh); |
| 660 | } |
| 661 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 662 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 663 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
| 664 | struct raid5_percpu *percpu) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 665 | { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 666 | return percpu->scribble + sizeof(struct page *) * (sh->disks + 2); |
| 667 | } |
| 668 | |
| 669 | static struct dma_async_tx_descriptor * |
| 670 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 671 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 672 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 673 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 674 | int target = sh->ops.target; |
| 675 | struct r5dev *tgt = &sh->dev[target]; |
| 676 | struct page *xor_dest = tgt->page; |
| 677 | int count = 0; |
| 678 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 679 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 680 | int i; |
| 681 | |
| 682 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 683 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 684 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 685 | |
| 686 | for (i = disks; i--; ) |
| 687 | if (i != target) |
| 688 | xor_srcs[count++] = sh->dev[i].page; |
| 689 | |
| 690 | atomic_inc(&sh->count); |
| 691 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 692 | 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] | 693 | ops_complete_compute, sh, to_addr_conv(sh, percpu)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 694 | if (unlikely(count == 1)) |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 695 | 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] | 696 | else |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 697 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 698 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 699 | return tx; |
| 700 | } |
| 701 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 702 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 703 | * @srcs - (struct page *) array of size sh->disks |
| 704 | * @sh - stripe_head to parse |
| 705 | * |
| 706 | * Populates srcs in proper layout order for the stripe and returns the |
| 707 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 708 | * destination buffer is recorded in srcs[count] and the Q destination |
| 709 | * is recorded in srcs[count+1]]. |
| 710 | */ |
| 711 | static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh) |
| 712 | { |
| 713 | int disks = sh->disks; |
| 714 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 715 | int d0_idx = raid6_d0(sh); |
| 716 | int count; |
| 717 | int i; |
| 718 | |
| 719 | for (i = 0; i < disks; i++) |
| 720 | srcs[i] = (void *)raid6_empty_zero_page; |
| 721 | |
| 722 | count = 0; |
| 723 | i = d0_idx; |
| 724 | do { |
| 725 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 726 | |
| 727 | srcs[slot] = sh->dev[i].page; |
| 728 | i = raid6_next_disk(i, disks); |
| 729 | } while (i != d0_idx); |
| 730 | BUG_ON(count != syndrome_disks); |
| 731 | |
| 732 | return count; |
| 733 | } |
| 734 | |
| 735 | static struct dma_async_tx_descriptor * |
| 736 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 737 | { |
| 738 | int disks = sh->disks; |
| 739 | struct page **blocks = percpu->scribble; |
| 740 | int target; |
| 741 | int qd_idx = sh->qd_idx; |
| 742 | struct dma_async_tx_descriptor *tx; |
| 743 | struct async_submit_ctl submit; |
| 744 | struct r5dev *tgt; |
| 745 | struct page *dest; |
| 746 | int i; |
| 747 | int count; |
| 748 | |
| 749 | if (sh->ops.target < 0) |
| 750 | target = sh->ops.target2; |
| 751 | else if (sh->ops.target2 < 0) |
| 752 | target = sh->ops.target; |
| 753 | else |
| 754 | /* we should only have one valid target */ |
| 755 | BUG(); |
| 756 | BUG_ON(target < 0); |
| 757 | pr_debug("%s: stripe %llu block: %d\n", |
| 758 | __func__, (unsigned long long)sh->sector, target); |
| 759 | |
| 760 | tgt = &sh->dev[target]; |
| 761 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 762 | dest = tgt->page; |
| 763 | |
| 764 | atomic_inc(&sh->count); |
| 765 | |
| 766 | if (target == qd_idx) { |
| 767 | count = set_syndrome_sources(blocks, sh); |
| 768 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 769 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 770 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 771 | ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 772 | to_addr_conv(sh, percpu)); |
| 773 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 774 | } else { |
| 775 | /* Compute any data- or p-drive using XOR */ |
| 776 | count = 0; |
| 777 | for (i = disks; i-- ; ) { |
| 778 | if (i == target || i == qd_idx) |
| 779 | continue; |
| 780 | blocks[count++] = sh->dev[i].page; |
| 781 | } |
| 782 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 783 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 784 | NULL, ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 785 | to_addr_conv(sh, percpu)); |
| 786 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); |
| 787 | } |
| 788 | |
| 789 | return tx; |
| 790 | } |
| 791 | |
| 792 | static struct dma_async_tx_descriptor * |
| 793 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 794 | { |
| 795 | int i, count, disks = sh->disks; |
| 796 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 797 | int d0_idx = raid6_d0(sh); |
| 798 | int faila = -1, failb = -1; |
| 799 | int target = sh->ops.target; |
| 800 | int target2 = sh->ops.target2; |
| 801 | struct r5dev *tgt = &sh->dev[target]; |
| 802 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 803 | struct dma_async_tx_descriptor *tx; |
| 804 | struct page **blocks = percpu->scribble; |
| 805 | struct async_submit_ctl submit; |
| 806 | |
| 807 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 808 | __func__, (unsigned long long)sh->sector, target, target2); |
| 809 | BUG_ON(target < 0 || target2 < 0); |
| 810 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 811 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 812 | |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 813 | /* we need to open-code set_syndrome_sources to handle the |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 814 | * slot number conversion for 'faila' and 'failb' |
| 815 | */ |
| 816 | for (i = 0; i < disks ; i++) |
| 817 | blocks[i] = (void *)raid6_empty_zero_page; |
| 818 | count = 0; |
| 819 | i = d0_idx; |
| 820 | do { |
| 821 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 822 | |
| 823 | blocks[slot] = sh->dev[i].page; |
| 824 | |
| 825 | if (i == target) |
| 826 | faila = slot; |
| 827 | if (i == target2) |
| 828 | failb = slot; |
| 829 | i = raid6_next_disk(i, disks); |
| 830 | } while (i != d0_idx); |
| 831 | BUG_ON(count != syndrome_disks); |
| 832 | |
| 833 | BUG_ON(faila == failb); |
| 834 | if (failb < faila) |
| 835 | swap(faila, failb); |
| 836 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 837 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 838 | |
| 839 | atomic_inc(&sh->count); |
| 840 | |
| 841 | if (failb == syndrome_disks+1) { |
| 842 | /* Q disk is one of the missing disks */ |
| 843 | if (faila == syndrome_disks) { |
| 844 | /* Missing P+Q, just recompute */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 845 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 846 | ops_complete_compute, sh, |
| 847 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 848 | return async_gen_syndrome(blocks, 0, count+2, |
| 849 | STRIPE_SIZE, &submit); |
| 850 | } else { |
| 851 | struct page *dest; |
| 852 | int data_target; |
| 853 | int qd_idx = sh->qd_idx; |
| 854 | |
| 855 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 856 | if (target == qd_idx) |
| 857 | data_target = target2; |
| 858 | else |
| 859 | data_target = target; |
| 860 | |
| 861 | count = 0; |
| 862 | for (i = disks; i-- ; ) { |
| 863 | if (i == data_target || i == qd_idx) |
| 864 | continue; |
| 865 | blocks[count++] = sh->dev[i].page; |
| 866 | } |
| 867 | dest = sh->dev[data_target].page; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 868 | init_async_submit(&submit, |
| 869 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 870 | NULL, NULL, NULL, |
| 871 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 872 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
| 873 | &submit); |
| 874 | |
| 875 | count = set_syndrome_sources(blocks, sh); |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 876 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
| 877 | ops_complete_compute, sh, |
| 878 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 879 | return async_gen_syndrome(blocks, 0, count+2, |
| 880 | STRIPE_SIZE, &submit); |
| 881 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 882 | } else { |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 883 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 884 | ops_complete_compute, sh, |
| 885 | to_addr_conv(sh, percpu)); |
| 886 | if (failb == syndrome_disks) { |
| 887 | /* We're missing D+P. */ |
| 888 | return async_raid6_datap_recov(syndrome_disks+2, |
| 889 | STRIPE_SIZE, faila, |
| 890 | blocks, &submit); |
| 891 | } else { |
| 892 | /* We're missing D+D. */ |
| 893 | return async_raid6_2data_recov(syndrome_disks+2, |
| 894 | STRIPE_SIZE, faila, failb, |
| 895 | blocks, &submit); |
| 896 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
| 900 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 901 | static void ops_complete_prexor(void *stripe_head_ref) |
| 902 | { |
| 903 | struct stripe_head *sh = stripe_head_ref; |
| 904 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 905 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 906 | (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | static struct dma_async_tx_descriptor * |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 910 | ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 911 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 912 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 913 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 914 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 915 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 916 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 917 | |
| 918 | /* existing parity data subtracted */ |
| 919 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 920 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 921 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 922 | (unsigned long long)sh->sector); |
| 923 | |
| 924 | for (i = disks; i--; ) { |
| 925 | struct r5dev *dev = &sh->dev[i]; |
| 926 | /* Only process blocks that are known to be uptodate */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 927 | if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 928 | xor_srcs[count++] = dev->page; |
| 929 | } |
| 930 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 931 | 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] | 932 | ops_complete_prexor, sh, to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 933 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 934 | |
| 935 | return tx; |
| 936 | } |
| 937 | |
| 938 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 939 | 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] | 940 | { |
| 941 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 942 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 943 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 944 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 945 | (unsigned long long)sh->sector); |
| 946 | |
| 947 | for (i = disks; i--; ) { |
| 948 | struct r5dev *dev = &sh->dev[i]; |
| 949 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 950 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 951 | if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 952 | struct bio *wbi; |
| 953 | |
| 954 | spin_lock(&sh->lock); |
| 955 | chosen = dev->towrite; |
| 956 | dev->towrite = NULL; |
| 957 | BUG_ON(dev->written); |
| 958 | wbi = dev->written = chosen; |
| 959 | spin_unlock(&sh->lock); |
| 960 | |
| 961 | while (wbi && wbi->bi_sector < |
| 962 | dev->sector + STRIPE_SECTORS) { |
| 963 | tx = async_copy_data(1, wbi, dev->page, |
| 964 | dev->sector, tx); |
| 965 | wbi = r5_next_bio(wbi, dev->sector); |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | return tx; |
| 971 | } |
| 972 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 973 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 974 | { |
| 975 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 976 | int disks = sh->disks; |
| 977 | int pd_idx = sh->pd_idx; |
| 978 | int qd_idx = sh->qd_idx; |
| 979 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 980 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 981 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 982 | (unsigned long long)sh->sector); |
| 983 | |
| 984 | for (i = disks; i--; ) { |
| 985 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 986 | |
| 987 | if (dev->written || i == pd_idx || i == qd_idx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 988 | set_bit(R5_UPTODATE, &dev->flags); |
| 989 | } |
| 990 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 991 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 992 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 993 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 994 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 995 | else { |
| 996 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 997 | sh->reconstruct_state = reconstruct_state_result; |
| 998 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 999 | |
| 1000 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1001 | release_stripe(sh); |
| 1002 | } |
| 1003 | |
| 1004 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1005 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1006 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1007 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1008 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1009 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1010 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1011 | int count = 0, pd_idx = sh->pd_idx, i; |
| 1012 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1013 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1014 | unsigned long flags; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1015 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1016 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1017 | (unsigned long long)sh->sector); |
| 1018 | |
| 1019 | /* check if prexor is active which means only process blocks |
| 1020 | * that are part of a read-modify-write (written) |
| 1021 | */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1022 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 1023 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1024 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1025 | for (i = disks; i--; ) { |
| 1026 | struct r5dev *dev = &sh->dev[i]; |
| 1027 | if (dev->written) |
| 1028 | xor_srcs[count++] = dev->page; |
| 1029 | } |
| 1030 | } else { |
| 1031 | xor_dest = sh->dev[pd_idx].page; |
| 1032 | for (i = disks; i--; ) { |
| 1033 | struct r5dev *dev = &sh->dev[i]; |
| 1034 | if (i != pd_idx) |
| 1035 | xor_srcs[count++] = dev->page; |
| 1036 | } |
| 1037 | } |
| 1038 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1039 | /* 1/ if we prexor'd then the dest is reused as a source |
| 1040 | * 2/ if we did not prexor then we are redoing the parity |
| 1041 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 1042 | * for the synchronous xor case |
| 1043 | */ |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 1044 | flags = ASYNC_TX_ACK | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1045 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
| 1046 | |
| 1047 | atomic_inc(&sh->count); |
| 1048 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1049 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1050 | to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1051 | if (unlikely(count == 1)) |
| 1052 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
| 1053 | else |
| 1054 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1057 | static void |
| 1058 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1059 | struct dma_async_tx_descriptor *tx) |
| 1060 | { |
| 1061 | struct async_submit_ctl submit; |
| 1062 | struct page **blocks = percpu->scribble; |
| 1063 | int count; |
| 1064 | |
| 1065 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 1066 | |
| 1067 | count = set_syndrome_sources(blocks, sh); |
| 1068 | |
| 1069 | atomic_inc(&sh->count); |
| 1070 | |
| 1071 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct, |
| 1072 | sh, to_addr_conv(sh, percpu)); |
| 1073 | async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | static void ops_complete_check(void *stripe_head_ref) |
| 1077 | { |
| 1078 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1079 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1080 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1081 | (unsigned long long)sh->sector); |
| 1082 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1083 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1084 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1085 | release_stripe(sh); |
| 1086 | } |
| 1087 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1088 | 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] | 1089 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1090 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1091 | int pd_idx = sh->pd_idx; |
| 1092 | int qd_idx = sh->qd_idx; |
| 1093 | struct page *xor_dest; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1094 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1095 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1096 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1097 | int count; |
| 1098 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1099 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1100 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1101 | (unsigned long long)sh->sector); |
| 1102 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1103 | count = 0; |
| 1104 | xor_dest = sh->dev[pd_idx].page; |
| 1105 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1106 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1107 | if (i == pd_idx || i == qd_idx) |
| 1108 | continue; |
| 1109 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1112 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
| 1113 | to_addr_conv(sh, percpu)); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 1114 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1115 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1116 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1117 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1118 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 1119 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1122 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 1123 | { |
| 1124 | struct page **srcs = percpu->scribble; |
| 1125 | struct async_submit_ctl submit; |
| 1126 | int count; |
| 1127 | |
| 1128 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 1129 | (unsigned long long)sh->sector, checkp); |
| 1130 | |
| 1131 | count = set_syndrome_sources(srcs, sh); |
| 1132 | if (!checkp) |
| 1133 | srcs[count] = NULL; |
| 1134 | |
| 1135 | atomic_inc(&sh->count); |
| 1136 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
| 1137 | sh, to_addr_conv(sh, percpu)); |
| 1138 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, |
| 1139 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); |
| 1140 | } |
| 1141 | |
| 1142 | 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] | 1143 | { |
| 1144 | int overlap_clear = 0, i, disks = sh->disks; |
| 1145 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1146 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1147 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1148 | struct raid5_percpu *percpu; |
| 1149 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1150 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1151 | cpu = get_cpu(); |
| 1152 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1153 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1154 | ops_run_biofill(sh); |
| 1155 | overlap_clear++; |
| 1156 | } |
| 1157 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1158 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1159 | if (level < 6) |
| 1160 | tx = ops_run_compute5(sh, percpu); |
| 1161 | else { |
| 1162 | if (sh->ops.target2 < 0 || sh->ops.target < 0) |
| 1163 | tx = ops_run_compute6_1(sh, percpu); |
| 1164 | else |
| 1165 | tx = ops_run_compute6_2(sh, percpu); |
| 1166 | } |
| 1167 | /* terminate the chain if reconstruct is not set to be run */ |
| 1168 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1169 | async_tx_ack(tx); |
| 1170 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1171 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1172 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1173 | tx = ops_run_prexor(sh, percpu, tx); |
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_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1176 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1177 | overlap_clear++; |
| 1178 | } |
| 1179 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1180 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
| 1181 | if (level < 6) |
| 1182 | ops_run_reconstruct5(sh, percpu, tx); |
| 1183 | else |
| 1184 | ops_run_reconstruct6(sh, percpu, tx); |
| 1185 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1186 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1187 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
| 1188 | if (sh->check_state == check_state_run) |
| 1189 | ops_run_check_p(sh, percpu); |
| 1190 | else if (sh->check_state == check_state_run_q) |
| 1191 | ops_run_check_pq(sh, percpu, 0); |
| 1192 | else if (sh->check_state == check_state_run_pq) |
| 1193 | ops_run_check_pq(sh, percpu, 1); |
| 1194 | else |
| 1195 | BUG(); |
| 1196 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1197 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1198 | if (overlap_clear) |
| 1199 | for (i = disks; i--; ) { |
| 1200 | struct r5dev *dev = &sh->dev[i]; |
| 1201 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 1202 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 1203 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1204 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1207 | static int grow_one_stripe(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | { |
| 1209 | struct stripe_head *sh; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1210 | sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL); |
| 1211 | if (!sh) |
| 1212 | return 0; |
| 1213 | memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev)); |
| 1214 | sh->raid_conf = conf; |
| 1215 | spin_lock_init(&sh->lock); |
| 1216 | |
| 1217 | if (grow_buffers(sh, conf->raid_disks)) { |
| 1218 | shrink_buffers(sh, conf->raid_disks); |
| 1219 | kmem_cache_free(conf->slab_cache, sh); |
| 1220 | return 0; |
| 1221 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1222 | sh->disks = conf->raid_disks; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1223 | /* we just created an active stripe so... */ |
| 1224 | atomic_set(&sh->count, 1); |
| 1225 | atomic_inc(&conf->active_stripes); |
| 1226 | INIT_LIST_HEAD(&sh->lru); |
| 1227 | release_stripe(sh); |
| 1228 | return 1; |
| 1229 | } |
| 1230 | |
| 1231 | static int grow_stripes(raid5_conf_t *conf, int num) |
| 1232 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1233 | struct kmem_cache *sc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | int devs = conf->raid_disks; |
| 1235 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 1236 | sprintf(conf->cache_name[0], |
| 1237 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 1238 | sprintf(conf->cache_name[1], |
| 1239 | "raid%d-%s-alt", conf->level, mdname(conf->mddev)); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1240 | conf->active_name = 0; |
| 1241 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1243 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | if (!sc) |
| 1245 | return 1; |
| 1246 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1247 | conf->pool_size = devs; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1248 | while (num--) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1249 | if (!grow_one_stripe(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | return 0; |
| 1252 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 1253 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1254 | /** |
| 1255 | * scribble_len - return the required size of the scribble region |
| 1256 | * @num - total number of disks in the array |
| 1257 | * |
| 1258 | * The size must be enough to contain: |
| 1259 | * 1/ a struct page pointer for each device in the array +2 |
| 1260 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 1261 | * (dma_map_page()) or page (page_address()) address. |
| 1262 | * |
| 1263 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 1264 | * calculate over all devices (not just the data blocks), using zeros in place |
| 1265 | * of the P and Q blocks. |
| 1266 | */ |
| 1267 | static size_t scribble_len(int num) |
| 1268 | { |
| 1269 | size_t len; |
| 1270 | |
| 1271 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); |
| 1272 | |
| 1273 | return len; |
| 1274 | } |
| 1275 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1276 | static int resize_stripes(raid5_conf_t *conf, int newsize) |
| 1277 | { |
| 1278 | /* Make all the stripes able to hold 'newsize' devices. |
| 1279 | * New slots in each stripe get 'page' set to a new page. |
| 1280 | * |
| 1281 | * This happens in stages: |
| 1282 | * 1/ create a new kmem_cache and allocate the required number of |
| 1283 | * stripe_heads. |
| 1284 | * 2/ gather all the old stripe_heads and tranfer the pages across |
| 1285 | * to the new stripe_heads. This will have the side effect of |
| 1286 | * freezing the array as once all stripe_heads have been collected, |
| 1287 | * no IO will be possible. Old stripe heads are freed once their |
| 1288 | * pages have been transferred over, and the old kmem_cache is |
| 1289 | * freed when all stripes are done. |
| 1290 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 1291 | * we simple return a failre status - no need to clean anything up. |
| 1292 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 1293 | * If this fails, we don't bother trying the shrink the |
| 1294 | * stripe_heads down again, we just leave them as they are. |
| 1295 | * As each stripe_head is processed the new one is released into |
| 1296 | * active service. |
| 1297 | * |
| 1298 | * Once step2 is started, we cannot afford to wait for a write, |
| 1299 | * so we use GFP_NOIO allocations. |
| 1300 | */ |
| 1301 | struct stripe_head *osh, *nsh; |
| 1302 | LIST_HEAD(newstripes); |
| 1303 | struct disk_info *ndisks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1304 | unsigned long cpu; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1305 | int err; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1306 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1307 | int i; |
| 1308 | |
| 1309 | if (newsize <= conf->pool_size) |
| 1310 | return 0; /* never bother to shrink */ |
| 1311 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1312 | err = md_allow_write(conf->mddev); |
| 1313 | if (err) |
| 1314 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 1315 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1316 | /* Step 1 */ |
| 1317 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 1318 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1319 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1320 | if (!sc) |
| 1321 | return -ENOMEM; |
| 1322 | |
| 1323 | for (i = conf->max_nr_stripes; i; i--) { |
| 1324 | nsh = kmem_cache_alloc(sc, GFP_KERNEL); |
| 1325 | if (!nsh) |
| 1326 | break; |
| 1327 | |
| 1328 | memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev)); |
| 1329 | |
| 1330 | nsh->raid_conf = conf; |
| 1331 | spin_lock_init(&nsh->lock); |
| 1332 | |
| 1333 | list_add(&nsh->lru, &newstripes); |
| 1334 | } |
| 1335 | if (i) { |
| 1336 | /* didn't get enough, give up */ |
| 1337 | while (!list_empty(&newstripes)) { |
| 1338 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1339 | list_del(&nsh->lru); |
| 1340 | kmem_cache_free(sc, nsh); |
| 1341 | } |
| 1342 | kmem_cache_destroy(sc); |
| 1343 | return -ENOMEM; |
| 1344 | } |
| 1345 | /* Step 2 - Must use GFP_NOIO now. |
| 1346 | * OK, we have enough stripes, start collecting inactive |
| 1347 | * stripes and copying them over |
| 1348 | */ |
| 1349 | list_for_each_entry(nsh, &newstripes, lru) { |
| 1350 | spin_lock_irq(&conf->device_lock); |
| 1351 | wait_event_lock_irq(conf->wait_for_stripe, |
| 1352 | !list_empty(&conf->inactive_list), |
| 1353 | conf->device_lock, |
NeilBrown | b3b46be | 2006-03-27 01:18:16 -0800 | [diff] [blame] | 1354 | unplug_slaves(conf->mddev) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1355 | ); |
| 1356 | osh = get_free_stripe(conf); |
| 1357 | spin_unlock_irq(&conf->device_lock); |
| 1358 | atomic_set(&nsh->count, 1); |
| 1359 | for(i=0; i<conf->pool_size; i++) |
| 1360 | nsh->dev[i].page = osh->dev[i].page; |
| 1361 | for( ; i<newsize; i++) |
| 1362 | nsh->dev[i].page = NULL; |
| 1363 | kmem_cache_free(conf->slab_cache, osh); |
| 1364 | } |
| 1365 | kmem_cache_destroy(conf->slab_cache); |
| 1366 | |
| 1367 | /* Step 3. |
| 1368 | * At this point, we are holding all the stripes so the array |
| 1369 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1370 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1371 | */ |
| 1372 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 1373 | if (ndisks) { |
| 1374 | for (i=0; i<conf->raid_disks; i++) |
| 1375 | ndisks[i] = conf->disks[i]; |
| 1376 | kfree(conf->disks); |
| 1377 | conf->disks = ndisks; |
| 1378 | } else |
| 1379 | err = -ENOMEM; |
| 1380 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1381 | get_online_cpus(); |
| 1382 | conf->scribble_len = scribble_len(newsize); |
| 1383 | for_each_present_cpu(cpu) { |
| 1384 | struct raid5_percpu *percpu; |
| 1385 | void *scribble; |
| 1386 | |
| 1387 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 1388 | scribble = kmalloc(conf->scribble_len, GFP_NOIO); |
| 1389 | |
| 1390 | if (scribble) { |
| 1391 | kfree(percpu->scribble); |
| 1392 | percpu->scribble = scribble; |
| 1393 | } else { |
| 1394 | err = -ENOMEM; |
| 1395 | break; |
| 1396 | } |
| 1397 | } |
| 1398 | put_online_cpus(); |
| 1399 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1400 | /* Step 4, return new stripes to service */ |
| 1401 | while(!list_empty(&newstripes)) { |
| 1402 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1403 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1404 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1405 | for (i=conf->raid_disks; i < newsize; i++) |
| 1406 | if (nsh->dev[i].page == NULL) { |
| 1407 | struct page *p = alloc_page(GFP_NOIO); |
| 1408 | nsh->dev[i].page = p; |
| 1409 | if (!p) |
| 1410 | err = -ENOMEM; |
| 1411 | } |
| 1412 | release_stripe(nsh); |
| 1413 | } |
| 1414 | /* critical section pass, GFP_NOIO no longer needed */ |
| 1415 | |
| 1416 | conf->slab_cache = sc; |
| 1417 | conf->active_name = 1-conf->active_name; |
| 1418 | conf->pool_size = newsize; |
| 1419 | return err; |
| 1420 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1422 | static int drop_one_stripe(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | { |
| 1424 | struct stripe_head *sh; |
| 1425 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1426 | spin_lock_irq(&conf->device_lock); |
| 1427 | sh = get_free_stripe(conf); |
| 1428 | spin_unlock_irq(&conf->device_lock); |
| 1429 | if (!sh) |
| 1430 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1431 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1432 | shrink_buffers(sh, conf->pool_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1433 | kmem_cache_free(conf->slab_cache, sh); |
| 1434 | atomic_dec(&conf->active_stripes); |
| 1435 | return 1; |
| 1436 | } |
| 1437 | |
| 1438 | static void shrink_stripes(raid5_conf_t *conf) |
| 1439 | { |
| 1440 | while (drop_one_stripe(conf)) |
| 1441 | ; |
| 1442 | |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1443 | if (conf->slab_cache) |
| 1444 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | conf->slab_cache = NULL; |
| 1446 | } |
| 1447 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1448 | static void raid5_end_read_request(struct bio * bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1450 | struct stripe_head *sh = bi->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1452 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1454 | char b[BDEVNAME_SIZE]; |
| 1455 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | |
| 1458 | for (i=0 ; i<disks; i++) |
| 1459 | if (bi == &sh->dev[i].req) |
| 1460 | break; |
| 1461 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1462 | pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
| 1463 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | uptodate); |
| 1465 | if (i == disks) { |
| 1466 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1467 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1472 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1473 | rdev = conf->disks[i].rdev; |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1474 | printk_rl(KERN_INFO "raid5:%s: read error corrected" |
| 1475 | " (%lu sectors at %llu on %s)\n", |
| 1476 | mdname(conf->mddev), STRIPE_SECTORS, |
| 1477 | (unsigned long long)(sh->sector |
| 1478 | + rdev->data_offset), |
| 1479 | bdevname(rdev->bdev, b)); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1480 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1481 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
| 1482 | } |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1483 | if (atomic_read(&conf->disks[i].rdev->read_errors)) |
| 1484 | atomic_set(&conf->disks[i].rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | } else { |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1486 | const char *bdn = bdevname(conf->disks[i].rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1487 | int retry = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1488 | rdev = conf->disks[i].rdev; |
| 1489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1491 | atomic_inc(&rdev->read_errors); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1492 | if (conf->mddev->degraded) |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1493 | printk_rl(KERN_WARNING |
| 1494 | "raid5:%s: read error not correctable " |
| 1495 | "(sector %llu on %s).\n", |
| 1496 | mdname(conf->mddev), |
| 1497 | (unsigned long long)(sh->sector |
| 1498 | + rdev->data_offset), |
| 1499 | bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1500 | else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1501 | /* Oh, no!!! */ |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1502 | printk_rl(KERN_WARNING |
| 1503 | "raid5:%s: read error NOT corrected!! " |
| 1504 | "(sector %llu on %s).\n", |
| 1505 | mdname(conf->mddev), |
| 1506 | (unsigned long long)(sh->sector |
| 1507 | + rdev->data_offset), |
| 1508 | bdn); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1509 | else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1510 | > conf->max_nr_stripes) |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1511 | printk(KERN_WARNING |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1512 | "raid5:%s: Too many read errors, failing device %s.\n", |
| 1513 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1514 | else |
| 1515 | retry = 1; |
| 1516 | if (retry) |
| 1517 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 1518 | else { |
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); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1521 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1522 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | } |
| 1524 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1526 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1527 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | } |
| 1529 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1530 | static void raid5_end_write_request(struct bio *bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1532 | struct stripe_head *sh = bi->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1534 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
| 1536 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | for (i=0 ; i<disks; i++) |
| 1538 | if (bi == &sh->dev[i].req) |
| 1539 | break; |
| 1540 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1541 | pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 1543 | uptodate); |
| 1544 | if (i == disks) { |
| 1545 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1546 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | } |
| 1548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | if (!uptodate) |
| 1550 | md_error(conf->mddev, conf->disks[i].rdev); |
| 1551 | |
| 1552 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
| 1553 | |
| 1554 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1555 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1556 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1560 | 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] | 1561 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1562 | 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] | 1563 | { |
| 1564 | struct r5dev *dev = &sh->dev[i]; |
| 1565 | |
| 1566 | bio_init(&dev->req); |
| 1567 | dev->req.bi_io_vec = &dev->vec; |
| 1568 | dev->req.bi_vcnt++; |
| 1569 | dev->req.bi_max_vecs++; |
| 1570 | dev->vec.bv_page = dev->page; |
| 1571 | dev->vec.bv_len = STRIPE_SIZE; |
| 1572 | dev->vec.bv_offset = 0; |
| 1573 | |
| 1574 | dev->req.bi_sector = sh->sector; |
| 1575 | dev->req.bi_private = sh; |
| 1576 | |
| 1577 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1578 | dev->sector = compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | static void error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1582 | { |
| 1583 | char b[BDEVNAME_SIZE]; |
| 1584 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1585 | pr_debug("raid5: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1587 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1588 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1589 | if (test_and_clear_bit(In_sync, &rdev->flags)) { |
| 1590 | unsigned long flags; |
| 1591 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | mddev->degraded++; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1593 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | /* |
| 1595 | * if recovery was running, make sure it aborts. |
| 1596 | */ |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 1597 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1599 | set_bit(Faulty, &rdev->flags); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1600 | printk(KERN_ALERT |
| 1601 | "raid5: Disk failure on %s, disabling device.\n" |
| 1602 | "raid5: Operation continuing on %d devices.\n", |
| 1603 | bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1605 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | |
| 1607 | /* |
| 1608 | * Input: a 'big' sector number, |
| 1609 | * Output: index of the data and parity disk, and the sector # in them. |
| 1610 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1611 | 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] | 1612 | int previous, int *dd_idx, |
| 1613 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | { |
| 1615 | long stripe; |
| 1616 | unsigned long chunk_number; |
| 1617 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1618 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1619 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1621 | int algorithm = previous ? conf->prev_algo |
| 1622 | : conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 1623 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 1624 | : conf->chunk_sectors; |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1625 | int raid_disks = previous ? conf->previous_raid_disks |
| 1626 | : conf->raid_disks; |
| 1627 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | |
| 1629 | /* First compute the information on this sector */ |
| 1630 | |
| 1631 | /* |
| 1632 | * Compute the chunk number and the sector offset inside the chunk |
| 1633 | */ |
| 1634 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 1635 | chunk_number = r_sector; |
| 1636 | BUG_ON(r_sector != chunk_number); |
| 1637 | |
| 1638 | /* |
| 1639 | * Compute the stripe number |
| 1640 | */ |
| 1641 | stripe = chunk_number / data_disks; |
| 1642 | |
| 1643 | /* |
| 1644 | * Compute the data disk and parity disk indexes inside the stripe |
| 1645 | */ |
| 1646 | *dd_idx = chunk_number % data_disks; |
| 1647 | |
| 1648 | /* |
| 1649 | * Select the parity disk based on the user selected algorithm. |
| 1650 | */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1651 | pd_idx = qd_idx = ~0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1652 | switch(conf->level) { |
| 1653 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1654 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1655 | break; |
| 1656 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1657 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1659 | pd_idx = data_disks - stripe % raid_disks; |
| 1660 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | (*dd_idx)++; |
| 1662 | break; |
| 1663 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1664 | pd_idx = stripe % raid_disks; |
| 1665 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | (*dd_idx)++; |
| 1667 | break; |
| 1668 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1669 | pd_idx = data_disks - stripe % raid_disks; |
| 1670 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | break; |
| 1672 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1673 | pd_idx = stripe % raid_disks; |
| 1674 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1676 | case ALGORITHM_PARITY_0: |
| 1677 | pd_idx = 0; |
| 1678 | (*dd_idx)++; |
| 1679 | break; |
| 1680 | case ALGORITHM_PARITY_N: |
| 1681 | pd_idx = data_disks; |
| 1682 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | default: |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1684 | printk(KERN_ERR "raid5: unsupported algorithm %d\n", |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1685 | algorithm); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1686 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1687 | } |
| 1688 | break; |
| 1689 | case 6: |
| 1690 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1691 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1692 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1693 | pd_idx = raid_disks - 1 - (stripe % raid_disks); |
| 1694 | qd_idx = pd_idx + 1; |
| 1695 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1696 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1697 | qd_idx = 0; |
| 1698 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1699 | (*dd_idx) += 2; /* D D P Q D */ |
| 1700 | break; |
| 1701 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1702 | pd_idx = stripe % raid_disks; |
| 1703 | qd_idx = pd_idx + 1; |
| 1704 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1705 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1706 | qd_idx = 0; |
| 1707 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1708 | (*dd_idx) += 2; /* D D P Q D */ |
| 1709 | break; |
| 1710 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1711 | pd_idx = raid_disks - 1 - (stripe % raid_disks); |
| 1712 | qd_idx = (pd_idx + 1) % raid_disks; |
| 1713 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1714 | break; |
| 1715 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1716 | pd_idx = stripe % raid_disks; |
| 1717 | qd_idx = (pd_idx + 1) % raid_disks; |
| 1718 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1719 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1720 | |
| 1721 | case ALGORITHM_PARITY_0: |
| 1722 | pd_idx = 0; |
| 1723 | qd_idx = 1; |
| 1724 | (*dd_idx) += 2; |
| 1725 | break; |
| 1726 | case ALGORITHM_PARITY_N: |
| 1727 | pd_idx = data_disks; |
| 1728 | qd_idx = data_disks + 1; |
| 1729 | break; |
| 1730 | |
| 1731 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 1732 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 1733 | * of blocks for computing Q is different. |
| 1734 | */ |
| 1735 | pd_idx = stripe % raid_disks; |
| 1736 | qd_idx = pd_idx + 1; |
| 1737 | if (pd_idx == raid_disks-1) { |
| 1738 | (*dd_idx)++; /* Q D D D P */ |
| 1739 | qd_idx = 0; |
| 1740 | } else if (*dd_idx >= pd_idx) |
| 1741 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1742 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1743 | break; |
| 1744 | |
| 1745 | case ALGORITHM_ROTATING_N_RESTART: |
| 1746 | /* Same a left_asymmetric, by first stripe is |
| 1747 | * D D D P Q rather than |
| 1748 | * Q D D D P |
| 1749 | */ |
| 1750 | pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks); |
| 1751 | qd_idx = pd_idx + 1; |
| 1752 | if (pd_idx == raid_disks-1) { |
| 1753 | (*dd_idx)++; /* Q D D D P */ |
| 1754 | qd_idx = 0; |
| 1755 | } else if (*dd_idx >= pd_idx) |
| 1756 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1757 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1758 | break; |
| 1759 | |
| 1760 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 1761 | /* Same as left_symmetric but Q is before P */ |
| 1762 | pd_idx = raid_disks - 1 - (stripe % raid_disks); |
| 1763 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 1764 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1765 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1766 | break; |
| 1767 | |
| 1768 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 1769 | /* RAID5 left_asymmetric, with Q on last device */ |
| 1770 | pd_idx = data_disks - stripe % (raid_disks-1); |
| 1771 | if (*dd_idx >= pd_idx) |
| 1772 | (*dd_idx)++; |
| 1773 | qd_idx = raid_disks - 1; |
| 1774 | break; |
| 1775 | |
| 1776 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 1777 | pd_idx = stripe % (raid_disks-1); |
| 1778 | if (*dd_idx >= pd_idx) |
| 1779 | (*dd_idx)++; |
| 1780 | qd_idx = raid_disks - 1; |
| 1781 | break; |
| 1782 | |
| 1783 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 1784 | pd_idx = data_disks - stripe % (raid_disks-1); |
| 1785 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 1786 | qd_idx = raid_disks - 1; |
| 1787 | break; |
| 1788 | |
| 1789 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 1790 | pd_idx = stripe % (raid_disks-1); |
| 1791 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 1792 | qd_idx = raid_disks - 1; |
| 1793 | break; |
| 1794 | |
| 1795 | case ALGORITHM_PARITY_0_6: |
| 1796 | pd_idx = 0; |
| 1797 | (*dd_idx)++; |
| 1798 | qd_idx = raid_disks - 1; |
| 1799 | break; |
| 1800 | |
| 1801 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1802 | default: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1803 | printk(KERN_CRIT "raid6: unsupported algorithm %d\n", |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1804 | algorithm); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1805 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1806 | } |
| 1807 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1810 | if (sh) { |
| 1811 | sh->pd_idx = pd_idx; |
| 1812 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1813 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1814 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | /* |
| 1816 | * Finally, compute the new sector number |
| 1817 | */ |
| 1818 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 1819 | return new_sector; |
| 1820 | } |
| 1821 | |
| 1822 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1823 | 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] | 1824 | { |
| 1825 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 1826 | int raid_disks = sh->disks; |
| 1827 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | sector_t new_sector = sh->sector, check; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 1829 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 1830 | : conf->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1831 | int algorithm = previous ? conf->prev_algo |
| 1832 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | sector_t stripe; |
| 1834 | int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1835 | int chunk_number, dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1837 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1839 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 1841 | stripe = new_sector; |
| 1842 | BUG_ON(new_sector != stripe); |
| 1843 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1844 | if (i == sh->pd_idx) |
| 1845 | return 0; |
| 1846 | switch(conf->level) { |
| 1847 | case 4: break; |
| 1848 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1849 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 1851 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 1852 | if (i > sh->pd_idx) |
| 1853 | i--; |
| 1854 | break; |
| 1855 | case ALGORITHM_LEFT_SYMMETRIC: |
| 1856 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 1857 | if (i < sh->pd_idx) |
| 1858 | i += raid_disks; |
| 1859 | i -= (sh->pd_idx + 1); |
| 1860 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1861 | case ALGORITHM_PARITY_0: |
| 1862 | i -= 1; |
| 1863 | break; |
| 1864 | case ALGORITHM_PARITY_N: |
| 1865 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | default: |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1867 | printk(KERN_ERR "raid5: unsupported algorithm %d\n", |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1868 | algorithm); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1869 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1870 | } |
| 1871 | break; |
| 1872 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1873 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1874 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1875 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1876 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 1877 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1878 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 1879 | case ALGORITHM_ROTATING_N_RESTART: |
| 1880 | if (sh->pd_idx == raid_disks-1) |
| 1881 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1882 | else if (i > sh->pd_idx) |
| 1883 | i -= 2; /* D D P Q D */ |
| 1884 | break; |
| 1885 | case ALGORITHM_LEFT_SYMMETRIC: |
| 1886 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 1887 | if (sh->pd_idx == raid_disks-1) |
| 1888 | i--; /* Q D D D P */ |
| 1889 | else { |
| 1890 | /* D D P Q D */ |
| 1891 | if (i < sh->pd_idx) |
| 1892 | i += raid_disks; |
| 1893 | i -= (sh->pd_idx + 2); |
| 1894 | } |
| 1895 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1896 | case ALGORITHM_PARITY_0: |
| 1897 | i -= 2; |
| 1898 | break; |
| 1899 | case ALGORITHM_PARITY_N: |
| 1900 | break; |
| 1901 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 1902 | if (sh->pd_idx == 0) |
| 1903 | i--; /* P D D D Q */ |
| 1904 | else if (i > sh->pd_idx) |
| 1905 | i -= 2; /* D D Q P D */ |
| 1906 | break; |
| 1907 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 1908 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 1909 | if (i > sh->pd_idx) |
| 1910 | i--; |
| 1911 | break; |
| 1912 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 1913 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 1914 | if (i < sh->pd_idx) |
| 1915 | i += data_disks + 1; |
| 1916 | i -= (sh->pd_idx + 1); |
| 1917 | break; |
| 1918 | case ALGORITHM_PARITY_0_6: |
| 1919 | i -= 1; |
| 1920 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1921 | default: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1922 | printk(KERN_CRIT "raid6: unsupported algorithm %d\n", |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1923 | algorithm); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1924 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1925 | } |
| 1926 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | chunk_number = stripe * data_disks + i; |
| 1930 | r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset; |
| 1931 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1932 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1933 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1934 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 1935 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1936 | printk(KERN_ERR "compute_blocknr: map not correct\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | return 0; |
| 1938 | } |
| 1939 | return r_sector; |
| 1940 | } |
| 1941 | |
| 1942 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1943 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 1944 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1945 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1946 | { |
| 1947 | int i, pd_idx = sh->pd_idx, disks = sh->disks; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 1948 | raid5_conf_t *conf = sh->raid_conf; |
| 1949 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1950 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1951 | if (rcw) { |
| 1952 | /* if we are not expanding this is a proper write request, and |
| 1953 | * there will be bios with new data to be drained into the |
| 1954 | * stripe cache |
| 1955 | */ |
| 1956 | if (!expand) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1957 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 1958 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 1959 | } else |
| 1960 | sh->reconstruct_state = reconstruct_state_run; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1961 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1962 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1963 | |
| 1964 | for (i = disks; i--; ) { |
| 1965 | struct r5dev *dev = &sh->dev[i]; |
| 1966 | |
| 1967 | if (dev->towrite) { |
| 1968 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1969 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1970 | if (!expand) |
| 1971 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1972 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1973 | } |
| 1974 | } |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 1975 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 1976 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 1977 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1978 | } else { |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 1979 | BUG_ON(level == 6); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1980 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 1981 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
| 1982 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1983 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1984 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 1985 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1986 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1987 | |
| 1988 | for (i = disks; i--; ) { |
| 1989 | struct r5dev *dev = &sh->dev[i]; |
| 1990 | if (i == pd_idx) |
| 1991 | continue; |
| 1992 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1993 | if (dev->towrite && |
| 1994 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1995 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 1996 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1997 | set_bit(R5_LOCKED, &dev->flags); |
| 1998 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1999 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2000 | } |
| 2001 | } |
| 2002 | } |
| 2003 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2004 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2005 | * are in flight |
| 2006 | */ |
| 2007 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 2008 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2009 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2010 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2011 | if (level == 6) { |
| 2012 | int qd_idx = sh->qd_idx; |
| 2013 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 2014 | |
| 2015 | set_bit(R5_LOCKED, &dev->flags); |
| 2016 | clear_bit(R5_UPTODATE, &dev->flags); |
| 2017 | s->locked++; |
| 2018 | } |
| 2019 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2020 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2021 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2022 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2023 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | /* |
| 2026 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2027 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | * The bi_next chain must be in order. |
| 2029 | */ |
| 2030 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) |
| 2031 | { |
| 2032 | struct bio **bip; |
| 2033 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2034 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2036 | pr_debug("adding bh b#%llu to stripe s#%llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | (unsigned long long)bi->bi_sector, |
| 2038 | (unsigned long long)sh->sector); |
| 2039 | |
| 2040 | |
| 2041 | spin_lock(&sh->lock); |
| 2042 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2043 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | bip = &sh->dev[dd_idx].towrite; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2045 | if (*bip == NULL && sh->dev[dd_idx].written == NULL) |
| 2046 | firstwrite = 1; |
| 2047 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | bip = &sh->dev[dd_idx].toread; |
| 2049 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { |
| 2050 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) |
| 2051 | goto overlap; |
| 2052 | bip = & (*bip)->bi_next; |
| 2053 | } |
| 2054 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) |
| 2055 | goto overlap; |
| 2056 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2057 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | if (*bip) |
| 2059 | bi->bi_next = *bip; |
| 2060 | *bip = bi; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2061 | bi->bi_phys_segments++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | spin_unlock_irq(&conf->device_lock); |
| 2063 | spin_unlock(&sh->lock); |
| 2064 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2065 | 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] | 2066 | (unsigned long long)bi->bi_sector, |
| 2067 | (unsigned long long)sh->sector, dd_idx); |
| 2068 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2069 | if (conf->mddev->bitmap && firstwrite) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2070 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 2071 | STRIPE_SECTORS, 0); |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 2072 | sh->bm_seq = conf->seq_flush+1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2073 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 2074 | } |
| 2075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | if (forwrite) { |
| 2077 | /* check if page is covered */ |
| 2078 | sector_t sector = sh->dev[dd_idx].sector; |
| 2079 | for (bi=sh->dev[dd_idx].towrite; |
| 2080 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
| 2081 | bi && bi->bi_sector <= sector; |
| 2082 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
| 2083 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) |
| 2084 | sector = bi->bi_sector + (bi->bi_size>>9); |
| 2085 | } |
| 2086 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
| 2087 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); |
| 2088 | } |
| 2089 | return 1; |
| 2090 | |
| 2091 | overlap: |
| 2092 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
| 2093 | spin_unlock_irq(&conf->device_lock); |
| 2094 | spin_unlock(&sh->lock); |
| 2095 | return 0; |
| 2096 | } |
| 2097 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2098 | static void end_reshape(raid5_conf_t *conf); |
| 2099 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2100 | static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous, |
| 2101 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2102 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2103 | int sectors_per_chunk = |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2104 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2105 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2106 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2107 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2108 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2109 | raid5_compute_sector(conf, |
| 2110 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2111 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2112 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2113 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2114 | } |
| 2115 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2116 | static void |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2117 | handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2118 | struct stripe_head_state *s, int disks, |
| 2119 | struct bio **return_bi) |
| 2120 | { |
| 2121 | int i; |
| 2122 | for (i = disks; i--; ) { |
| 2123 | struct bio *bi; |
| 2124 | int bitmap_end = 0; |
| 2125 | |
| 2126 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
| 2127 | mdk_rdev_t *rdev; |
| 2128 | rcu_read_lock(); |
| 2129 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 2130 | if (rdev && test_bit(In_sync, &rdev->flags)) |
| 2131 | /* multiple read failures in one stripe */ |
| 2132 | md_error(conf->mddev, rdev); |
| 2133 | rcu_read_unlock(); |
| 2134 | } |
| 2135 | spin_lock_irq(&conf->device_lock); |
| 2136 | /* fail all writes first */ |
| 2137 | bi = sh->dev[i].towrite; |
| 2138 | sh->dev[i].towrite = NULL; |
| 2139 | if (bi) { |
| 2140 | s->to_write--; |
| 2141 | bitmap_end = 1; |
| 2142 | } |
| 2143 | |
| 2144 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2145 | wake_up(&conf->wait_for_overlap); |
| 2146 | |
| 2147 | while (bi && bi->bi_sector < |
| 2148 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2149 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 2150 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2151 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2152 | md_write_end(conf->mddev); |
| 2153 | bi->bi_next = *return_bi; |
| 2154 | *return_bi = bi; |
| 2155 | } |
| 2156 | bi = nextbi; |
| 2157 | } |
| 2158 | /* and fail all 'written' */ |
| 2159 | bi = sh->dev[i].written; |
| 2160 | sh->dev[i].written = NULL; |
| 2161 | if (bi) bitmap_end = 1; |
| 2162 | while (bi && bi->bi_sector < |
| 2163 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2164 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 2165 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2166 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2167 | md_write_end(conf->mddev); |
| 2168 | bi->bi_next = *return_bi; |
| 2169 | *return_bi = bi; |
| 2170 | } |
| 2171 | bi = bi2; |
| 2172 | } |
| 2173 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2174 | /* fail any reads if this device is non-operational and |
| 2175 | * the data has not reached the cache yet. |
| 2176 | */ |
| 2177 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
| 2178 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 2179 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2180 | bi = sh->dev[i].toread; |
| 2181 | sh->dev[i].toread = NULL; |
| 2182 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2183 | wake_up(&conf->wait_for_overlap); |
| 2184 | if (bi) s->to_read--; |
| 2185 | while (bi && bi->bi_sector < |
| 2186 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2187 | struct bio *nextbi = |
| 2188 | r5_next_bio(bi, sh->dev[i].sector); |
| 2189 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2190 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2191 | bi->bi_next = *return_bi; |
| 2192 | *return_bi = bi; |
| 2193 | } |
| 2194 | bi = nextbi; |
| 2195 | } |
| 2196 | } |
| 2197 | spin_unlock_irq(&conf->device_lock); |
| 2198 | if (bitmap_end) |
| 2199 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2200 | STRIPE_SECTORS, 0, 0); |
| 2201 | } |
| 2202 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2203 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2204 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2205 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2206 | } |
| 2207 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2208 | /* fetch_block5 - checks the given member device to see if its data needs |
| 2209 | * to be read or computed to satisfy a request. |
| 2210 | * |
| 2211 | * Returns 1 when no more member devices need to be checked, otherwise returns |
| 2212 | * 0 to tell the loop in handle_stripe_fill5 to continue |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2213 | */ |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2214 | static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s, |
| 2215 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2216 | { |
| 2217 | struct r5dev *dev = &sh->dev[disk_idx]; |
| 2218 | struct r5dev *failed_dev = &sh->dev[s->failed_num]; |
| 2219 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2220 | /* is the data in this block needed, and can we get it? */ |
| 2221 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2222 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2223 | (dev->toread || |
| 2224 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2225 | s->syncing || s->expanding || |
| 2226 | (s->failed && |
| 2227 | (failed_dev->toread || |
| 2228 | (failed_dev->towrite && |
| 2229 | !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) { |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2230 | /* We would like to get this block, possibly by computing it, |
| 2231 | * otherwise read it if the backing disk is insync |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2232 | */ |
| 2233 | if ((s->uptodate == disks - 1) && |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2234 | (s->failed && disk_idx == s->failed_num)) { |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2235 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2236 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2237 | set_bit(R5_Wantcompute, &dev->flags); |
| 2238 | sh->ops.target = disk_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2239 | sh->ops.target2 = -1; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2240 | s->req_compute = 1; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2241 | /* Careful: from this point on 'uptodate' is in the eye |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2242 | * of raid_run_ops which services 'compute' operations |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2243 | * before writes. R5_Wantcompute flags a block that will |
| 2244 | * be R5_UPTODATE by the time it is needed for a |
| 2245 | * subsequent operation. |
| 2246 | */ |
| 2247 | s->uptodate++; |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2248 | return 1; /* uptodate + compute == disks */ |
Dan Williams | 7a1fc53 | 2008-07-10 04:54:57 -0700 | [diff] [blame] | 2249 | } else if (test_bit(R5_Insync, &dev->flags)) { |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2250 | set_bit(R5_LOCKED, &dev->flags); |
| 2251 | set_bit(R5_Wantread, &dev->flags); |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2252 | s->locked++; |
| 2253 | pr_debug("Reading block %d (sync=%d)\n", disk_idx, |
| 2254 | s->syncing); |
| 2255 | } |
| 2256 | } |
| 2257 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2258 | return 0; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2259 | } |
| 2260 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2261 | /** |
| 2262 | * handle_stripe_fill5 - read or compute data to satisfy pending requests. |
| 2263 | */ |
| 2264 | static void handle_stripe_fill5(struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2265 | struct stripe_head_state *s, int disks) |
| 2266 | { |
| 2267 | int i; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2268 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2269 | /* look for blocks to read/compute, skip this if a compute |
| 2270 | * is already in flight, or if the stripe contents are in the |
| 2271 | * midst of changing due to a write |
| 2272 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2273 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2274 | !sh->reconstruct_state) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2275 | for (i = disks; i--; ) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2276 | if (fetch_block5(sh, s, i, disks)) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2277 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2278 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2279 | } |
| 2280 | |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2281 | /* fetch_block6 - checks the given member device to see if its data needs |
| 2282 | * to be read or computed to satisfy a request. |
| 2283 | * |
| 2284 | * Returns 1 when no more member devices need to be checked, otherwise returns |
| 2285 | * 0 to tell the loop in handle_stripe_fill6 to continue |
| 2286 | */ |
| 2287 | static int fetch_block6(struct stripe_head *sh, struct stripe_head_state *s, |
| 2288 | struct r6_state *r6s, int disk_idx, int disks) |
| 2289 | { |
| 2290 | struct r5dev *dev = &sh->dev[disk_idx]; |
| 2291 | struct r5dev *fdev[2] = { &sh->dev[r6s->failed_num[0]], |
| 2292 | &sh->dev[r6s->failed_num[1]] }; |
| 2293 | |
| 2294 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 2295 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2296 | (dev->toread || |
| 2297 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2298 | s->syncing || s->expanding || |
| 2299 | (s->failed >= 1 && |
| 2300 | (fdev[0]->toread || s->to_write)) || |
| 2301 | (s->failed >= 2 && |
| 2302 | (fdev[1]->toread || s->to_write)))) { |
| 2303 | /* we would like to get this block, possibly by computing it, |
| 2304 | * otherwise read it if the backing disk is insync |
| 2305 | */ |
| 2306 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 2307 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
| 2308 | if ((s->uptodate == disks - 1) && |
| 2309 | (s->failed && (disk_idx == r6s->failed_num[0] || |
| 2310 | disk_idx == r6s->failed_num[1]))) { |
| 2311 | /* have disk failed, and we're requested to fetch it; |
| 2312 | * do compute it |
| 2313 | */ |
| 2314 | pr_debug("Computing stripe %llu block %d\n", |
| 2315 | (unsigned long long)sh->sector, disk_idx); |
| 2316 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2317 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2318 | set_bit(R5_Wantcompute, &dev->flags); |
| 2319 | sh->ops.target = disk_idx; |
| 2320 | sh->ops.target2 = -1; /* no 2nd target */ |
| 2321 | s->req_compute = 1; |
| 2322 | s->uptodate++; |
| 2323 | return 1; |
| 2324 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 2325 | /* Computing 2-failure is *very* expensive; only |
| 2326 | * do it if failed >= 2 |
| 2327 | */ |
| 2328 | int other; |
| 2329 | for (other = disks; other--; ) { |
| 2330 | if (other == disk_idx) |
| 2331 | continue; |
| 2332 | if (!test_bit(R5_UPTODATE, |
| 2333 | &sh->dev[other].flags)) |
| 2334 | break; |
| 2335 | } |
| 2336 | BUG_ON(other < 0); |
| 2337 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 2338 | (unsigned long long)sh->sector, |
| 2339 | disk_idx, other); |
| 2340 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2341 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2342 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 2343 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 2344 | sh->ops.target = disk_idx; |
| 2345 | sh->ops.target2 = other; |
| 2346 | s->uptodate += 2; |
| 2347 | s->req_compute = 1; |
| 2348 | return 1; |
| 2349 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 2350 | set_bit(R5_LOCKED, &dev->flags); |
| 2351 | set_bit(R5_Wantread, &dev->flags); |
| 2352 | s->locked++; |
| 2353 | pr_debug("Reading block %d (sync=%d)\n", |
| 2354 | disk_idx, s->syncing); |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | return 0; |
| 2359 | } |
| 2360 | |
| 2361 | /** |
| 2362 | * handle_stripe_fill6 - read or compute data to satisfy pending requests. |
| 2363 | */ |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2364 | static void handle_stripe_fill6(struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2365 | struct stripe_head_state *s, struct r6_state *r6s, |
| 2366 | int disks) |
| 2367 | { |
| 2368 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2369 | |
| 2370 | /* look for blocks to read/compute, skip this if a compute |
| 2371 | * is already in flight, or if the stripe contents are in the |
| 2372 | * midst of changing due to a write |
| 2373 | */ |
| 2374 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
| 2375 | !sh->reconstruct_state) |
| 2376 | for (i = disks; i--; ) |
| 2377 | if (fetch_block6(sh, s, r6s, i, disks)) |
| 2378 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2379 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2380 | } |
| 2381 | |
| 2382 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2383 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2384 | * any written block on an uptodate or failed drive can be returned. |
| 2385 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 2386 | * never LOCKED, so we don't need to test 'failed' directly. |
| 2387 | */ |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2388 | static void handle_stripe_clean_event(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2389 | struct stripe_head *sh, int disks, struct bio **return_bi) |
| 2390 | { |
| 2391 | int i; |
| 2392 | struct r5dev *dev; |
| 2393 | |
| 2394 | for (i = disks; i--; ) |
| 2395 | if (sh->dev[i].written) { |
| 2396 | dev = &sh->dev[i]; |
| 2397 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 2398 | test_bit(R5_UPTODATE, &dev->flags)) { |
| 2399 | /* We can return any write requests */ |
| 2400 | struct bio *wbi, *wbi2; |
| 2401 | int bitmap_end = 0; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2402 | pr_debug("Return write for disc %d\n", i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2403 | spin_lock_irq(&conf->device_lock); |
| 2404 | wbi = dev->written; |
| 2405 | dev->written = NULL; |
| 2406 | while (wbi && wbi->bi_sector < |
| 2407 | dev->sector + STRIPE_SECTORS) { |
| 2408 | wbi2 = r5_next_bio(wbi, dev->sector); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2409 | if (!raid5_dec_bi_phys_segments(wbi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2410 | md_write_end(conf->mddev); |
| 2411 | wbi->bi_next = *return_bi; |
| 2412 | *return_bi = wbi; |
| 2413 | } |
| 2414 | wbi = wbi2; |
| 2415 | } |
| 2416 | if (dev->towrite == NULL) |
| 2417 | bitmap_end = 1; |
| 2418 | spin_unlock_irq(&conf->device_lock); |
| 2419 | if (bitmap_end) |
| 2420 | bitmap_endwrite(conf->mddev->bitmap, |
| 2421 | sh->sector, |
| 2422 | STRIPE_SECTORS, |
| 2423 | !test_bit(STRIPE_DEGRADED, &sh->state), |
| 2424 | 0); |
| 2425 | } |
| 2426 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2427 | |
| 2428 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2429 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2430 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2431 | } |
| 2432 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2433 | static void handle_stripe_dirtying5(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2434 | struct stripe_head *sh, struct stripe_head_state *s, int disks) |
| 2435 | { |
| 2436 | int rmw = 0, rcw = 0, i; |
| 2437 | for (i = disks; i--; ) { |
| 2438 | /* would I have to read this buffer for read_modify_write */ |
| 2439 | struct r5dev *dev = &sh->dev[i]; |
| 2440 | if ((dev->towrite || i == sh->pd_idx) && |
| 2441 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2442 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2443 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2444 | if (test_bit(R5_Insync, &dev->flags)) |
| 2445 | rmw++; |
| 2446 | else |
| 2447 | rmw += 2*disks; /* cannot read it */ |
| 2448 | } |
| 2449 | /* Would I have to read this buffer for reconstruct_write */ |
| 2450 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 2451 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2452 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2453 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2454 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2455 | else |
| 2456 | rcw += 2*disks; |
| 2457 | } |
| 2458 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2459 | pr_debug("for sector %llu, rmw=%d rcw=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2460 | (unsigned long long)sh->sector, rmw, rcw); |
| 2461 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2462 | if (rmw < rcw && rmw > 0) |
| 2463 | /* prefer read-modify-write, but need to get some data */ |
| 2464 | for (i = disks; i--; ) { |
| 2465 | struct r5dev *dev = &sh->dev[i]; |
| 2466 | if ((dev->towrite || i == sh->pd_idx) && |
| 2467 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2468 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2469 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2470 | test_bit(R5_Insync, &dev->flags)) { |
| 2471 | if ( |
| 2472 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2473 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2474 | "%d for r-m-w\n", i); |
| 2475 | set_bit(R5_LOCKED, &dev->flags); |
| 2476 | set_bit(R5_Wantread, &dev->flags); |
| 2477 | s->locked++; |
| 2478 | } else { |
| 2479 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2480 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2481 | } |
| 2482 | } |
| 2483 | } |
| 2484 | if (rcw <= rmw && rcw > 0) |
| 2485 | /* want reconstruct write, but need to get some data */ |
| 2486 | for (i = disks; i--; ) { |
| 2487 | struct r5dev *dev = &sh->dev[i]; |
| 2488 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 2489 | i != sh->pd_idx && |
| 2490 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2491 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2492 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2493 | test_bit(R5_Insync, &dev->flags)) { |
| 2494 | if ( |
| 2495 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2496 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2497 | "%d for Reconstruct\n", i); |
| 2498 | set_bit(R5_LOCKED, &dev->flags); |
| 2499 | set_bit(R5_Wantread, &dev->flags); |
| 2500 | s->locked++; |
| 2501 | } else { |
| 2502 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2503 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2504 | } |
| 2505 | } |
| 2506 | } |
| 2507 | /* now if nothing is locked, and if we have enough data, |
| 2508 | * we can start a write request |
| 2509 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2510 | /* since handle_stripe can be called at any time we need to handle the |
| 2511 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2512 | * subsequent call wants to start a write request. raid_run_ops only |
| 2513 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2514 | * simultaneously. If this is not the case then new writes need to be |
| 2515 | * held off until the compute completes. |
| 2516 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2517 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2518 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
| 2519 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2520 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2521 | } |
| 2522 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2523 | static void handle_stripe_dirtying6(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2524 | struct stripe_head *sh, struct stripe_head_state *s, |
| 2525 | struct r6_state *r6s, int disks) |
| 2526 | { |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2527 | int rcw = 0, pd_idx = sh->pd_idx, i; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2528 | int qd_idx = sh->qd_idx; |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2529 | |
| 2530 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2531 | for (i = disks; i--; ) { |
| 2532 | struct r5dev *dev = &sh->dev[i]; |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2533 | /* check if we haven't enough data */ |
| 2534 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 2535 | i != pd_idx && i != qd_idx && |
| 2536 | !test_bit(R5_LOCKED, &dev->flags) && |
| 2537 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2538 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2539 | rcw++; |
| 2540 | if (!test_bit(R5_Insync, &dev->flags)) |
| 2541 | continue; /* it's a failed drive */ |
| 2542 | |
| 2543 | if ( |
| 2544 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 2545 | pr_debug("Read_old stripe %llu " |
| 2546 | "block %d for Reconstruct\n", |
| 2547 | (unsigned long long)sh->sector, i); |
| 2548 | set_bit(R5_LOCKED, &dev->flags); |
| 2549 | set_bit(R5_Wantread, &dev->flags); |
| 2550 | s->locked++; |
| 2551 | } else { |
| 2552 | pr_debug("Request delayed stripe %llu " |
| 2553 | "block %d for Reconstruct\n", |
| 2554 | (unsigned long long)sh->sector, i); |
| 2555 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2556 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2557 | } |
| 2558 | } |
| 2559 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2560 | /* now if nothing is locked, and if we have enough data, we can start a |
| 2561 | * write request |
| 2562 | */ |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2563 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2564 | s->locked == 0 && rcw == 0 && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2565 | !test_bit(STRIPE_BIT_DELAY, &sh->state)) { |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2566 | schedule_reconstruction(sh, s, 1, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh, |
| 2571 | struct stripe_head_state *s, int disks) |
| 2572 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2573 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2574 | |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2575 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2576 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2577 | switch (sh->check_state) { |
| 2578 | case check_state_idle: |
| 2579 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2580 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2581 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2582 | sh->check_state = check_state_run; |
| 2583 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2584 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2585 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2586 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2587 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2588 | dev = &sh->dev[s->failed_num]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2589 | /* fall through */ |
| 2590 | case check_state_compute_result: |
| 2591 | sh->check_state = check_state_idle; |
| 2592 | if (!dev) |
| 2593 | dev = &sh->dev[sh->pd_idx]; |
| 2594 | |
| 2595 | /* check that a write has not made the stripe insync */ |
| 2596 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2597 | break; |
| 2598 | |
| 2599 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2600 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 2601 | BUG_ON(s->uptodate != disks); |
| 2602 | |
| 2603 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2604 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2605 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2606 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2607 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2608 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2609 | break; |
| 2610 | case check_state_run: |
| 2611 | break; /* we will be called again upon completion */ |
| 2612 | case check_state_check_result: |
| 2613 | sh->check_state = check_state_idle; |
| 2614 | |
| 2615 | /* if a failure occurred during the check operation, leave |
| 2616 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 2617 | */ |
| 2618 | if (s->failed) |
| 2619 | break; |
| 2620 | |
| 2621 | /* handle a successful check operation, if parity is correct |
| 2622 | * we are done. Otherwise update the mismatch count and repair |
| 2623 | * parity if !MD_RECOVERY_CHECK |
| 2624 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 2625 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2626 | /* parity is correct (on disc, |
| 2627 | * not in buffer any more) |
| 2628 | */ |
| 2629 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2630 | else { |
| 2631 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2632 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2633 | /* don't try to repair!! */ |
| 2634 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2635 | else { |
| 2636 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2637 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2638 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2639 | set_bit(R5_Wantcompute, |
| 2640 | &sh->dev[sh->pd_idx].flags); |
| 2641 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2642 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2643 | s->uptodate++; |
| 2644 | } |
| 2645 | } |
| 2646 | break; |
| 2647 | case check_state_compute_run: |
| 2648 | break; |
| 2649 | default: |
| 2650 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 2651 | __func__, sh->check_state, |
| 2652 | (unsigned long long) sh->sector); |
| 2653 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | |
| 2658 | 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] | 2659 | struct stripe_head_state *s, |
| 2660 | struct r6_state *r6s, int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2661 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2662 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2663 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2664 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2665 | |
| 2666 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2667 | |
| 2668 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2669 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2670 | /* Want to check and possibly repair P and Q. |
| 2671 | * However there could be one 'failed' device, in which |
| 2672 | * case we can only check one of them, possibly using the |
| 2673 | * other to generate missing data |
| 2674 | */ |
| 2675 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2676 | switch (sh->check_state) { |
| 2677 | case check_state_idle: |
| 2678 | /* start a new check operation if there are < 2 failures */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2679 | if (s->failed == r6s->q_failed) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2680 | /* The only possible failed device holds Q, so it |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2681 | * makes sense to check P (If anything else were failed, |
| 2682 | * we would have used P to recreate it). |
| 2683 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2684 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2685 | } |
| 2686 | if (!r6s->q_failed && s->failed < 2) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2687 | /* Q is not failed, and we didn't use it to generate |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2688 | * anything, so it makes sense to check it |
| 2689 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2690 | if (sh->check_state == check_state_run) |
| 2691 | sh->check_state = check_state_run_pq; |
| 2692 | else |
| 2693 | sh->check_state = check_state_run_q; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2694 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2695 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2696 | /* discard potentially stale zero_sum_result */ |
| 2697 | sh->ops.zero_sum_result = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2698 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2699 | if (sh->check_state == check_state_run) { |
| 2700 | /* async_xor_zero_sum destroys the contents of P */ |
| 2701 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 2702 | s->uptodate--; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2703 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2704 | if (sh->check_state >= check_state_run && |
| 2705 | sh->check_state <= check_state_run_pq) { |
| 2706 | /* async_syndrome_zero_sum preserves P and Q, so |
| 2707 | * no need to mark them !uptodate here |
| 2708 | */ |
| 2709 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 2710 | break; |
| 2711 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2712 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2713 | /* we have 2-disk failure */ |
| 2714 | BUG_ON(s->failed != 2); |
| 2715 | /* fall through */ |
| 2716 | case check_state_compute_result: |
| 2717 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2718 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2719 | /* check that a write has not made the stripe insync */ |
| 2720 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2721 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2722 | |
| 2723 | /* now write out any block on a failed drive, |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2724 | * or P or Q if they were recomputed |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2725 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2726 | 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] | 2727 | if (s->failed == 2) { |
| 2728 | dev = &sh->dev[r6s->failed_num[1]]; |
| 2729 | s->locked++; |
| 2730 | set_bit(R5_LOCKED, &dev->flags); |
| 2731 | set_bit(R5_Wantwrite, &dev->flags); |
| 2732 | } |
| 2733 | if (s->failed >= 1) { |
| 2734 | dev = &sh->dev[r6s->failed_num[0]]; |
| 2735 | s->locked++; |
| 2736 | set_bit(R5_LOCKED, &dev->flags); |
| 2737 | set_bit(R5_Wantwrite, &dev->flags); |
| 2738 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2739 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2740 | dev = &sh->dev[pd_idx]; |
| 2741 | s->locked++; |
| 2742 | set_bit(R5_LOCKED, &dev->flags); |
| 2743 | set_bit(R5_Wantwrite, &dev->flags); |
| 2744 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2745 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2746 | dev = &sh->dev[qd_idx]; |
| 2747 | s->locked++; |
| 2748 | set_bit(R5_LOCKED, &dev->flags); |
| 2749 | set_bit(R5_Wantwrite, &dev->flags); |
| 2750 | } |
| 2751 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 2752 | |
| 2753 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 2754 | break; |
| 2755 | case check_state_run: |
| 2756 | case check_state_run_q: |
| 2757 | case check_state_run_pq: |
| 2758 | break; /* we will be called again upon completion */ |
| 2759 | case check_state_check_result: |
| 2760 | sh->check_state = check_state_idle; |
| 2761 | |
| 2762 | /* handle a successful check operation, if parity is correct |
| 2763 | * we are done. Otherwise update the mismatch count and repair |
| 2764 | * parity if !MD_RECOVERY_CHECK |
| 2765 | */ |
| 2766 | if (sh->ops.zero_sum_result == 0) { |
| 2767 | /* both parities are correct */ |
| 2768 | if (!s->failed) |
| 2769 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2770 | else { |
| 2771 | /* in contrast to the raid5 case we can validate |
| 2772 | * parity, but still have a failure to write |
| 2773 | * back |
| 2774 | */ |
| 2775 | sh->check_state = check_state_compute_result; |
| 2776 | /* Returning at this point means that we may go |
| 2777 | * off and bring p and/or q uptodate again so |
| 2778 | * we make sure to check zero_sum_result again |
| 2779 | * to verify if p or q need writeback |
| 2780 | */ |
| 2781 | } |
| 2782 | } else { |
| 2783 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2784 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2785 | /* don't try to repair!! */ |
| 2786 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2787 | else { |
| 2788 | int *target = &sh->ops.target; |
| 2789 | |
| 2790 | sh->ops.target = -1; |
| 2791 | sh->ops.target2 = -1; |
| 2792 | sh->check_state = check_state_compute_run; |
| 2793 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2794 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2795 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 2796 | set_bit(R5_Wantcompute, |
| 2797 | &sh->dev[pd_idx].flags); |
| 2798 | *target = pd_idx; |
| 2799 | target = &sh->ops.target2; |
| 2800 | s->uptodate++; |
| 2801 | } |
| 2802 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 2803 | set_bit(R5_Wantcompute, |
| 2804 | &sh->dev[qd_idx].flags); |
| 2805 | *target = qd_idx; |
| 2806 | s->uptodate++; |
| 2807 | } |
| 2808 | } |
| 2809 | } |
| 2810 | break; |
| 2811 | case check_state_compute_run: |
| 2812 | break; |
| 2813 | default: |
| 2814 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 2815 | __func__, sh->check_state, |
| 2816 | (unsigned long long) sh->sector); |
| 2817 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2818 | } |
| 2819 | } |
| 2820 | |
| 2821 | static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh, |
| 2822 | struct r6_state *r6s) |
| 2823 | { |
| 2824 | int i; |
| 2825 | |
| 2826 | /* We have read all the blocks in this stripe and now we need to |
| 2827 | * copy some of them into a target stripe for expand. |
| 2828 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2829 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2830 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 2831 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2832 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2833 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2834 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2835 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2836 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2837 | sector_t bn = compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2838 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 2839 | &dd_idx, NULL); |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 2840 | sh2 = get_active_stripe(conf, s, 0, 1, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2841 | if (sh2 == NULL) |
| 2842 | /* so far only the early blocks of this stripe |
| 2843 | * have been requested. When later blocks |
| 2844 | * get requested, we will try again |
| 2845 | */ |
| 2846 | continue; |
| 2847 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 2848 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 2849 | /* must have already done this block */ |
| 2850 | release_stripe(sh2); |
| 2851 | continue; |
| 2852 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2853 | |
| 2854 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2855 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2856 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 2857 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2858 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2859 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2860 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 2861 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 2862 | for (j = 0; j < conf->raid_disks; j++) |
| 2863 | if (j != sh2->pd_idx && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2864 | (!r6s || j != sh2->qd_idx) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2865 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 2866 | break; |
| 2867 | if (j == conf->raid_disks) { |
| 2868 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 2869 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 2870 | } |
| 2871 | release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2872 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2873 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 2874 | /* done submitting copies, wait for them to complete */ |
| 2875 | if (tx) { |
| 2876 | async_tx_ack(tx); |
| 2877 | dma_wait_for_async_tx(tx); |
| 2878 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2879 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2880 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2881 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2882 | /* |
| 2883 | * handle_stripe - do things to a stripe. |
| 2884 | * |
| 2885 | * We lock the stripe and then examine the state of various bits |
| 2886 | * to see what needs to be done. |
| 2887 | * Possible results: |
| 2888 | * return some read request which now have data |
| 2889 | * return some write requests which are safely on disc |
| 2890 | * schedule a read on some buffers |
| 2891 | * schedule a write of some buffers |
| 2892 | * return confirmation of parity correctness |
| 2893 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | * buffers are taken off read_list or write_list, and bh_cache buffers |
| 2895 | * get BH_Lock set before the stripe lock is released. |
| 2896 | * |
| 2897 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2898 | |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 2899 | static bool handle_stripe5(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2900 | { |
| 2901 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2902 | int disks = sh->disks, i; |
| 2903 | struct bio *return_bi = NULL; |
| 2904 | struct stripe_head_state s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2905 | struct r5dev *dev; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2906 | mdk_rdev_t *blocked_rdev = NULL; |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 2907 | int prexor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2908 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2909 | memset(&s, 0, sizeof(s)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2910 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d " |
| 2911 | "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state, |
| 2912 | atomic_read(&sh->count), sh->pd_idx, sh->check_state, |
| 2913 | sh->reconstruct_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2914 | |
| 2915 | spin_lock(&sh->lock); |
| 2916 | clear_bit(STRIPE_HANDLE, &sh->state); |
| 2917 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 2918 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2919 | s.syncing = test_bit(STRIPE_SYNCING, &sh->state); |
| 2920 | s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 2921 | s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | /* Now to look around and see what can be done */ |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 2924 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2925 | for (i=disks; i--; ) { |
| 2926 | mdk_rdev_t *rdev; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 2927 | |
| 2928 | dev = &sh->dev[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | clear_bit(R5_Insync, &dev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2931 | pr_debug("check %d: state 0x%lx toread %p read %p write %p " |
| 2932 | "written %p\n", i, dev->flags, dev->toread, dev->read, |
| 2933 | dev->towrite, dev->written); |
| 2934 | |
| 2935 | /* maybe we can request a biofill operation |
| 2936 | * |
| 2937 | * new wantfill requests are only permitted while |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2938 | * ops_complete_biofill is guaranteed to be inactive |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2939 | */ |
| 2940 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2941 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2942 | set_bit(R5_Wantfill, &dev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | |
| 2944 | /* now count some things */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2945 | if (test_bit(R5_LOCKED, &dev->flags)) s.locked++; |
| 2946 | if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2947 | if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2949 | if (test_bit(R5_Wantfill, &dev->flags)) |
| 2950 | s.to_fill++; |
| 2951 | else if (dev->toread) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2952 | s.to_read++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | if (dev->towrite) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2954 | s.to_write++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2955 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2956 | s.non_overwrite++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2957 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2958 | if (dev->written) |
| 2959 | s.written++; |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 2960 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2961 | if (blocked_rdev == NULL && |
| 2962 | rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2963 | blocked_rdev = rdev; |
| 2964 | atomic_inc(&rdev->nr_pending); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2965 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2966 | if (!rdev || !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 2967 | /* The ReadError flag will just be confusing now */ |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2968 | clear_bit(R5_ReadError, &dev->flags); |
| 2969 | clear_bit(R5_ReWrite, &dev->flags); |
| 2970 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2971 | if (!rdev || !test_bit(In_sync, &rdev->flags) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2972 | || test_bit(R5_ReadError, &dev->flags)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2973 | s.failed++; |
| 2974 | s.failed_num = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | } else |
| 2976 | set_bit(R5_Insync, &dev->flags); |
| 2977 | } |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 2978 | rcu_read_unlock(); |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2979 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2980 | if (unlikely(blocked_rdev)) { |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2981 | if (s.syncing || s.expanding || s.expanded || |
| 2982 | s.to_write || s.written) { |
| 2983 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2984 | goto unlock; |
| 2985 | } |
| 2986 | /* There is nothing for the blocked_rdev to block */ |
| 2987 | rdev_dec_pending(blocked_rdev, conf->mddev); |
| 2988 | blocked_rdev = NULL; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2989 | } |
| 2990 | |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2991 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 2992 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 2993 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 2994 | } |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2995 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2996 | pr_debug("locked=%d uptodate=%d to_read=%d" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2997 | " to_write=%d failed=%d failed_num=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2998 | s.locked, s.uptodate, s.to_read, s.to_write, |
| 2999 | s.failed, s.failed_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3000 | /* check if the array has lost two devices and, if so, some requests might |
| 3001 | * need to be failed |
| 3002 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3003 | if (s.failed > 1 && s.to_read+s.to_write+s.written) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3004 | handle_failed_stripe(conf, sh, &s, disks, &return_bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3005 | if (s.failed > 1 && s.syncing) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); |
| 3007 | clear_bit(STRIPE_SYNCING, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3008 | s.syncing = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3009 | } |
| 3010 | |
| 3011 | /* might be able to return some write requests if the parity block |
| 3012 | * is safe, or on a failed drive |
| 3013 | */ |
| 3014 | dev = &sh->dev[sh->pd_idx]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3015 | if ( s.written && |
| 3016 | ((test_bit(R5_Insync, &dev->flags) && |
| 3017 | !test_bit(R5_LOCKED, &dev->flags) && |
| 3018 | test_bit(R5_UPTODATE, &dev->flags)) || |
| 3019 | (s.failed == 1 && s.failed_num == sh->pd_idx))) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3020 | handle_stripe_clean_event(conf, sh, disks, &return_bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3021 | |
| 3022 | /* Now we might consider reading some blocks, either to check/generate |
| 3023 | * parity, or to satisfy requests |
| 3024 | * or to load a block that is being partially written. |
| 3025 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3026 | if (s.to_read || s.non_overwrite || |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3027 | (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3028 | handle_stripe_fill5(sh, &s, disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3029 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3030 | /* Now we check to see if any write operations have recently |
| 3031 | * completed |
| 3032 | */ |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 3033 | prexor = 0; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 3034 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 3035 | prexor = 1; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 3036 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 3037 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3038 | sh->reconstruct_state = reconstruct_state_idle; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3039 | |
| 3040 | /* All the 'written' buffers and the parity block are ready to |
| 3041 | * be written back to disk |
| 3042 | */ |
| 3043 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags)); |
| 3044 | for (i = disks; i--; ) { |
| 3045 | dev = &sh->dev[i]; |
| 3046 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 3047 | (i == sh->pd_idx || dev->written)) { |
| 3048 | pr_debug("Writing block %d\n", i); |
| 3049 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 3050 | if (prexor) |
| 3051 | continue; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3052 | if (!test_bit(R5_Insync, &dev->flags) || |
| 3053 | (i == sh->pd_idx && s.failed == 0)) |
| 3054 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3055 | } |
| 3056 | } |
| 3057 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 3058 | atomic_dec(&conf->preread_active_stripes); |
| 3059 | if (atomic_read(&conf->preread_active_stripes) < |
| 3060 | IO_THRESHOLD) |
| 3061 | md_wakeup_thread(conf->mddev->thread); |
| 3062 | } |
| 3063 | } |
| 3064 | |
| 3065 | /* Now to consider new write requests and what else, if anything |
| 3066 | * should be read. We do not handle new writes when: |
| 3067 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 3068 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 3069 | * block. |
| 3070 | */ |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3071 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3072 | handle_stripe_dirtying5(conf, sh, &s, disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | |
| 3074 | /* 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] | 3075 | * Any reads will already have been scheduled, so we just see if enough |
| 3076 | * data is available. The parity check is held off while parity |
| 3077 | * dependent operations are in flight. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | */ |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3079 | if (sh->check_state || |
| 3080 | (s.syncing && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3081 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3082 | !test_bit(STRIPE_INSYNC, &sh->state))) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3083 | handle_parity_checks5(conf, sh, &s, disks); |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3084 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3085 | if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); |
| 3087 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 3088 | } |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3089 | |
| 3090 | /* If the failed drive is just a ReadError, then we might need to progress |
| 3091 | * the repair/check process |
| 3092 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3093 | if (s.failed == 1 && !conf->mddev->ro && |
| 3094 | test_bit(R5_ReadError, &sh->dev[s.failed_num].flags) |
| 3095 | && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags) |
| 3096 | && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3097 | ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3098 | dev = &sh->dev[s.failed_num]; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3099 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3100 | set_bit(R5_Wantwrite, &dev->flags); |
| 3101 | set_bit(R5_ReWrite, &dev->flags); |
| 3102 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3103 | s.locked++; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3104 | } else { |
| 3105 | /* let's read it back */ |
| 3106 | set_bit(R5_Wantread, &dev->flags); |
| 3107 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3108 | s.locked++; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3109 | } |
| 3110 | } |
| 3111 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3112 | /* Finish reconstruct operations initiated by the expansion process */ |
| 3113 | if (sh->reconstruct_state == reconstruct_state_result) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3114 | struct stripe_head *sh2 |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3115 | = get_active_stripe(conf, sh->sector, 1, 1, 1); |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3116 | if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) { |
| 3117 | /* sh cannot be written until sh2 has been read. |
| 3118 | * so arrange for sh to be delayed a little |
| 3119 | */ |
| 3120 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3121 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3122 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3123 | &sh2->state)) |
| 3124 | atomic_inc(&conf->preread_active_stripes); |
| 3125 | release_stripe(sh2); |
| 3126 | goto unlock; |
| 3127 | } |
| 3128 | if (sh2) |
| 3129 | release_stripe(sh2); |
| 3130 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3131 | sh->reconstruct_state = reconstruct_state_idle; |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3132 | clear_bit(STRIPE_EXPANDING, &sh->state); |
Dan Williams | 2339788 | 2008-07-23 20:05:34 -0700 | [diff] [blame] | 3133 | for (i = conf->raid_disks; i--; ) { |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3134 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
Dan Williams | 2339788 | 2008-07-23 20:05:34 -0700 | [diff] [blame] | 3135 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
Neil Brown | efe3114 | 2008-06-28 08:31:14 +1000 | [diff] [blame] | 3136 | s.locked++; |
Dan Williams | 2339788 | 2008-07-23 20:05:34 -0700 | [diff] [blame] | 3137 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3141 | !sh->reconstruct_state) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3142 | /* Need to write out all blocks after computing parity */ |
| 3143 | sh->disks = conf->raid_disks; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3144 | stripe_set_idx(sh->sector, conf, 0, sh); |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3145 | schedule_reconstruction(sh, &s, 1, 1); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3146 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3147 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3148 | atomic_dec(&conf->reshape_stripes); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3149 | wake_up(&conf->wait_for_overlap); |
| 3150 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3151 | } |
| 3152 | |
Dan Williams | 0f94e87 | 2008-01-08 15:32:53 -0800 | [diff] [blame] | 3153 | if (s.expanding && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3154 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3155 | handle_stripe_expansion(conf, sh, NULL); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3156 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3157 | unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3158 | spin_unlock(&sh->lock); |
| 3159 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3160 | /* wait for this device to become unblocked */ |
| 3161 | if (unlikely(blocked_rdev)) |
| 3162 | md_wait_for_blocked_rdev(blocked_rdev, conf->mddev); |
| 3163 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3164 | if (s.ops_request) |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 3165 | raid_run_ops(sh, s.ops_request); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3166 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 3167 | ops_run_io(sh, &s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3168 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3169 | return_io(return_bi); |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 3170 | |
| 3171 | return blocked_rdev == NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | } |
| 3173 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3174 | static bool handle_stripe6(struct stripe_head *sh) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3175 | { |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3176 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3177 | int disks = sh->disks; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3178 | struct bio *return_bi = NULL; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3179 | int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3180 | struct stripe_head_state s; |
| 3181 | struct r6_state r6s; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3182 | struct r5dev *dev, *pdev, *qdev; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3183 | mdk_rdev_t *blocked_rdev = NULL; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3184 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3185 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3186 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3187 | (unsigned long long)sh->sector, sh->state, |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3188 | atomic_read(&sh->count), pd_idx, qd_idx, |
| 3189 | sh->check_state, sh->reconstruct_state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3190 | memset(&s, 0, sizeof(s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3191 | |
| 3192 | spin_lock(&sh->lock); |
| 3193 | clear_bit(STRIPE_HANDLE, &sh->state); |
| 3194 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3195 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3196 | s.syncing = test_bit(STRIPE_SYNCING, &sh->state); |
| 3197 | s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3198 | s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3199 | /* Now to look around and see what can be done */ |
| 3200 | |
| 3201 | rcu_read_lock(); |
| 3202 | for (i=disks; i--; ) { |
| 3203 | mdk_rdev_t *rdev; |
| 3204 | dev = &sh->dev[i]; |
| 3205 | clear_bit(R5_Insync, &dev->flags); |
| 3206 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3207 | 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] | 3208 | i, dev->flags, dev->toread, dev->towrite, dev->written); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3209 | /* maybe we can reply to a read |
| 3210 | * |
| 3211 | * new wantfill requests are only permitted while |
| 3212 | * ops_complete_biofill is guaranteed to be inactive |
| 3213 | */ |
| 3214 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
| 3215 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
| 3216 | set_bit(R5_Wantfill, &dev->flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3217 | |
| 3218 | /* now count some things */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3219 | if (test_bit(R5_LOCKED, &dev->flags)) s.locked++; |
| 3220 | if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++; |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3221 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
| 3222 | s.compute++; |
| 3223 | BUG_ON(s.compute > 2); |
| 3224 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3225 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3226 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 3227 | s.to_fill++; |
| 3228 | } else if (dev->toread) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3229 | s.to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3230 | if (dev->towrite) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3231 | s.to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3232 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3233 | s.non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3234 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3235 | if (dev->written) |
| 3236 | s.written++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3237 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3238 | if (blocked_rdev == NULL && |
| 3239 | rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3240 | blocked_rdev = rdev; |
| 3241 | atomic_inc(&rdev->nr_pending); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3242 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3243 | if (!rdev || !test_bit(In_sync, &rdev->flags)) { |
| 3244 | /* The ReadError flag will just be confusing now */ |
| 3245 | clear_bit(R5_ReadError, &dev->flags); |
| 3246 | clear_bit(R5_ReWrite, &dev->flags); |
| 3247 | } |
| 3248 | if (!rdev || !test_bit(In_sync, &rdev->flags) |
| 3249 | || test_bit(R5_ReadError, &dev->flags)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3250 | if (s.failed < 2) |
| 3251 | r6s.failed_num[s.failed] = i; |
| 3252 | s.failed++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3253 | } else |
| 3254 | set_bit(R5_Insync, &dev->flags); |
| 3255 | } |
| 3256 | rcu_read_unlock(); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3257 | |
| 3258 | if (unlikely(blocked_rdev)) { |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3259 | if (s.syncing || s.expanding || s.expanded || |
| 3260 | s.to_write || s.written) { |
| 3261 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3262 | goto unlock; |
| 3263 | } |
| 3264 | /* There is nothing for the blocked_rdev to block */ |
| 3265 | rdev_dec_pending(blocked_rdev, conf->mddev); |
| 3266 | blocked_rdev = NULL; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3267 | } |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3268 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3269 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 3270 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 3271 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 3272 | } |
| 3273 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3274 | pr_debug("locked=%d uptodate=%d to_read=%d" |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3275 | " to_write=%d failed=%d failed_num=%d,%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3276 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 3277 | r6s.failed_num[0], r6s.failed_num[1]); |
| 3278 | /* check if the array has lost >2 devices and, if so, some requests |
| 3279 | * might need to be failed |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3280 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3281 | if (s.failed > 2 && s.to_read+s.to_write+s.written) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3282 | handle_failed_stripe(conf, sh, &s, disks, &return_bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3283 | if (s.failed > 2 && s.syncing) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3284 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); |
| 3285 | clear_bit(STRIPE_SYNCING, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3286 | s.syncing = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3287 | } |
| 3288 | |
| 3289 | /* |
| 3290 | * might be able to return some write requests if the parity blocks |
| 3291 | * are safe, or on a failed drive |
| 3292 | */ |
| 3293 | pdev = &sh->dev[pd_idx]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3294 | r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx) |
| 3295 | || (s.failed >= 2 && r6s.failed_num[1] == pd_idx); |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3296 | qdev = &sh->dev[qd_idx]; |
| 3297 | r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx) |
| 3298 | || (s.failed >= 2 && r6s.failed_num[1] == qd_idx); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3299 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3300 | if ( s.written && |
| 3301 | ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3302 | && !test_bit(R5_LOCKED, &pdev->flags) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3303 | && test_bit(R5_UPTODATE, &pdev->flags)))) && |
| 3304 | ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3305 | && !test_bit(R5_LOCKED, &qdev->flags) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3306 | && test_bit(R5_UPTODATE, &qdev->flags))))) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3307 | handle_stripe_clean_event(conf, sh, disks, &return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3308 | |
| 3309 | /* Now we might consider reading some blocks, either to check/generate |
| 3310 | * parity, or to satisfy requests |
| 3311 | * or to load a block that is being partially written. |
| 3312 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3313 | if (s.to_read || s.non_overwrite || (s.to_write && s.failed) || |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3314 | (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3315 | handle_stripe_fill6(sh, &s, &r6s, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3316 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3317 | /* Now we check to see if any write operations have recently |
| 3318 | * completed |
| 3319 | */ |
| 3320 | if (sh->reconstruct_state == reconstruct_state_drain_result) { |
| 3321 | int qd_idx = sh->qd_idx; |
| 3322 | |
| 3323 | sh->reconstruct_state = reconstruct_state_idle; |
| 3324 | /* All the 'written' buffers and the parity blocks are ready to |
| 3325 | * be written back to disk |
| 3326 | */ |
| 3327 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags)); |
| 3328 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags)); |
| 3329 | for (i = disks; i--; ) { |
| 3330 | dev = &sh->dev[i]; |
| 3331 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 3332 | (i == sh->pd_idx || i == qd_idx || |
| 3333 | dev->written)) { |
| 3334 | pr_debug("Writing block %d\n", i); |
| 3335 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 3336 | set_bit(R5_Wantwrite, &dev->flags); |
| 3337 | if (!test_bit(R5_Insync, &dev->flags) || |
| 3338 | ((i == sh->pd_idx || i == qd_idx) && |
| 3339 | s.failed == 0)) |
| 3340 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3341 | } |
| 3342 | } |
| 3343 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 3344 | atomic_dec(&conf->preread_active_stripes); |
| 3345 | if (atomic_read(&conf->preread_active_stripes) < |
| 3346 | IO_THRESHOLD) |
| 3347 | md_wakeup_thread(conf->mddev->thread); |
| 3348 | } |
| 3349 | } |
| 3350 | |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3351 | /* Now to consider new write requests and what else, if anything |
| 3352 | * should be read. We do not handle new writes when: |
| 3353 | * 1/ A 'write' operation (copy+gen_syndrome) is already in flight. |
| 3354 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 3355 | * block. |
| 3356 | */ |
| 3357 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3358 | handle_stripe_dirtying6(conf, sh, &s, &r6s, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3359 | |
| 3360 | /* 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] | 3361 | * 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] | 3362 | * data is available. The parity check is held off while parity |
| 3363 | * dependent operations are in flight. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3364 | */ |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3365 | if (sh->check_state || |
| 3366 | (s.syncing && s.locked == 0 && |
| 3367 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
| 3368 | !test_bit(STRIPE_INSYNC, &sh->state))) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3369 | handle_parity_checks6(conf, sh, &s, &r6s, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3370 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3371 | if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3372 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); |
| 3373 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 3374 | } |
| 3375 | |
| 3376 | /* If the failed drives are just a ReadError, then we might need |
| 3377 | * to progress the repair/check process |
| 3378 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3379 | if (s.failed <= 2 && !conf->mddev->ro) |
| 3380 | for (i = 0; i < s.failed; i++) { |
| 3381 | dev = &sh->dev[r6s.failed_num[i]]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3382 | if (test_bit(R5_ReadError, &dev->flags) |
| 3383 | && !test_bit(R5_LOCKED, &dev->flags) |
| 3384 | && test_bit(R5_UPTODATE, &dev->flags) |
| 3385 | ) { |
| 3386 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3387 | set_bit(R5_Wantwrite, &dev->flags); |
| 3388 | set_bit(R5_ReWrite, &dev->flags); |
| 3389 | set_bit(R5_LOCKED, &dev->flags); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3390 | s.locked++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3391 | } else { |
| 3392 | /* let's read it back */ |
| 3393 | set_bit(R5_Wantread, &dev->flags); |
| 3394 | set_bit(R5_LOCKED, &dev->flags); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3395 | s.locked++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3396 | } |
| 3397 | } |
| 3398 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3399 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3400 | /* Finish reconstruct operations initiated by the expansion process */ |
| 3401 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 3402 | sh->reconstruct_state = reconstruct_state_idle; |
| 3403 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 3404 | for (i = conf->raid_disks; i--; ) { |
| 3405 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 3406 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3407 | s.locked++; |
| 3408 | } |
| 3409 | } |
| 3410 | |
| 3411 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
| 3412 | !sh->reconstruct_state) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3413 | struct stripe_head *sh2 |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3414 | = get_active_stripe(conf, sh->sector, 1, 1, 1); |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3415 | if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) { |
| 3416 | /* sh cannot be written until sh2 has been read. |
| 3417 | * so arrange for sh to be delayed a little |
| 3418 | */ |
| 3419 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3420 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3421 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3422 | &sh2->state)) |
| 3423 | atomic_inc(&conf->preread_active_stripes); |
| 3424 | release_stripe(sh2); |
| 3425 | goto unlock; |
| 3426 | } |
| 3427 | if (sh2) |
| 3428 | release_stripe(sh2); |
| 3429 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3430 | /* Need to write out all blocks after computing P&Q */ |
| 3431 | sh->disks = conf->raid_disks; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3432 | stripe_set_idx(sh->sector, conf, 0, sh); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3433 | schedule_reconstruction(sh, &s, 1, 1); |
| 3434 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3435 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3436 | atomic_dec(&conf->reshape_stripes); |
| 3437 | wake_up(&conf->wait_for_overlap); |
| 3438 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3439 | } |
| 3440 | |
Dan Williams | 0f94e87 | 2008-01-08 15:32:53 -0800 | [diff] [blame] | 3441 | if (s.expanding && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3442 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3443 | handle_stripe_expansion(conf, sh, &r6s); |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3444 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3445 | unlock: |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3446 | spin_unlock(&sh->lock); |
| 3447 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3448 | /* wait for this device to become unblocked */ |
| 3449 | if (unlikely(blocked_rdev)) |
| 3450 | md_wait_for_blocked_rdev(blocked_rdev, conf->mddev); |
| 3451 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3452 | if (s.ops_request) |
| 3453 | raid_run_ops(sh, s.ops_request); |
| 3454 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 3455 | ops_run_io(sh, &s); |
| 3456 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3457 | return_io(return_bi); |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 3458 | |
| 3459 | return blocked_rdev == NULL; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3460 | } |
| 3461 | |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 3462 | /* returns true if the stripe was handled */ |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3463 | static bool handle_stripe(struct stripe_head *sh) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3464 | { |
| 3465 | if (sh->raid_conf->level == 6) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3466 | return handle_stripe6(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3467 | else |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 3468 | return handle_stripe5(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3469 | } |
| 3470 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 3471 | static void raid5_activate_delayed(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | { |
| 3473 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 3474 | while (!list_empty(&conf->delayed_list)) { |
| 3475 | struct list_head *l = conf->delayed_list.next; |
| 3476 | struct stripe_head *sh; |
| 3477 | sh = list_entry(l, struct stripe_head, lru); |
| 3478 | list_del_init(l); |
| 3479 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3480 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3481 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3482 | list_add_tail(&sh->lru, &conf->hold_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3483 | } |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 3484 | } else |
| 3485 | blk_plug_device(conf->mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | } |
| 3487 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 3488 | static void activate_bit_delay(raid5_conf_t *conf) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3489 | { |
| 3490 | /* device_lock is held */ |
| 3491 | struct list_head head; |
| 3492 | list_add(&head, &conf->bitmap_list); |
| 3493 | list_del_init(&conf->bitmap_list); |
| 3494 | while (!list_empty(&head)) { |
| 3495 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
| 3496 | list_del_init(&sh->lru); |
| 3497 | atomic_inc(&sh->count); |
| 3498 | __release_stripe(conf, sh); |
| 3499 | } |
| 3500 | } |
| 3501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3502 | static void unplug_slaves(mddev_t *mddev) |
| 3503 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3504 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3505 | int i; |
| 3506 | |
| 3507 | rcu_read_lock(); |
NeilBrown | f001a70 | 2009-06-09 14:30:31 +1000 | [diff] [blame] | 3508 | for (i = 0; i < conf->raid_disks; i++) { |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 3509 | mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3510 | if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3511 | struct request_queue *r_queue = bdev_get_queue(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3512 | |
| 3513 | atomic_inc(&rdev->nr_pending); |
| 3514 | rcu_read_unlock(); |
| 3515 | |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 3516 | blk_unplug(r_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3517 | |
| 3518 | rdev_dec_pending(rdev, mddev); |
| 3519 | rcu_read_lock(); |
| 3520 | } |
| 3521 | } |
| 3522 | rcu_read_unlock(); |
| 3523 | } |
| 3524 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3525 | static void raid5_unplug_device(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3526 | { |
| 3527 | mddev_t *mddev = q->queuedata; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3528 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3529 | unsigned long flags; |
| 3530 | |
| 3531 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3532 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3533 | if (blk_remove_plug(q)) { |
| 3534 | conf->seq_flush++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3535 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3536 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | md_wakeup_thread(mddev->thread); |
| 3538 | |
| 3539 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3540 | |
| 3541 | unplug_slaves(mddev); |
| 3542 | } |
| 3543 | |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3544 | static int raid5_congested(void *data, int bits) |
| 3545 | { |
| 3546 | mddev_t *mddev = data; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3547 | raid5_conf_t *conf = mddev->private; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3548 | |
| 3549 | /* No difference between reads and writes. Just check |
| 3550 | * how busy the stripe_cache is |
| 3551 | */ |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 3552 | |
| 3553 | if (mddev_congested(mddev, bits)) |
| 3554 | return 1; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3555 | if (conf->inactive_blocked) |
| 3556 | return 1; |
| 3557 | if (conf->quiesce) |
| 3558 | return 1; |
| 3559 | if (list_empty_careful(&conf->inactive_list)) |
| 3560 | return 1; |
| 3561 | |
| 3562 | return 0; |
| 3563 | } |
| 3564 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3565 | /* We want read requests to align with chunks where possible, |
| 3566 | * but write requests don't need to. |
| 3567 | */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3568 | static int raid5_mergeable_bvec(struct request_queue *q, |
| 3569 | struct bvec_merge_data *bvm, |
| 3570 | struct bio_vec *biovec) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3571 | { |
| 3572 | mddev_t *mddev = q->queuedata; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3573 | 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] | 3574 | int max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3575 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3576 | unsigned int bio_sectors = bvm->bi_size >> 9; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3577 | |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3578 | if ((bvm->bi_rw & 1) == WRITE) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3579 | return biovec->bv_len; /* always allow writes to be mergeable */ |
| 3580 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3581 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3582 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3583 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 3584 | if (max < 0) max = 0; |
| 3585 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 3586 | return biovec->bv_len; |
| 3587 | else |
| 3588 | return max; |
| 3589 | } |
| 3590 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3591 | |
| 3592 | static int in_chunk_boundary(mddev_t *mddev, struct bio *bio) |
| 3593 | { |
| 3594 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3595 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3596 | unsigned int bio_sectors = bio->bi_size >> 9; |
| 3597 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3598 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3599 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3600 | return chunk_sectors >= |
| 3601 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 3602 | } |
| 3603 | |
| 3604 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3605 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 3606 | * later sampled by raid5d. |
| 3607 | */ |
| 3608 | static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf) |
| 3609 | { |
| 3610 | unsigned long flags; |
| 3611 | |
| 3612 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3613 | |
| 3614 | bi->bi_next = conf->retry_read_aligned_list; |
| 3615 | conf->retry_read_aligned_list = bi; |
| 3616 | |
| 3617 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3618 | md_wakeup_thread(conf->mddev->thread); |
| 3619 | } |
| 3620 | |
| 3621 | |
| 3622 | static struct bio *remove_bio_from_retry(raid5_conf_t *conf) |
| 3623 | { |
| 3624 | struct bio *bi; |
| 3625 | |
| 3626 | bi = conf->retry_read_aligned; |
| 3627 | if (bi) { |
| 3628 | conf->retry_read_aligned = NULL; |
| 3629 | return bi; |
| 3630 | } |
| 3631 | bi = conf->retry_read_aligned_list; |
| 3632 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3633 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3634 | bi->bi_next = NULL; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3635 | /* |
| 3636 | * this sets the active strip count to 1 and the processed |
| 3637 | * strip count to zero (upper 8 bits) |
| 3638 | */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3639 | bi->bi_phys_segments = 1; /* biased count of active stripes */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3640 | } |
| 3641 | |
| 3642 | return bi; |
| 3643 | } |
| 3644 | |
| 3645 | |
| 3646 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3647 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 3648 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 3649 | * first). |
| 3650 | * If the read failed.. |
| 3651 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3652 | static void raid5_align_endio(struct bio *bi, int error) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3653 | { |
| 3654 | struct bio* raid_bi = bi->bi_private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3655 | mddev_t *mddev; |
| 3656 | raid5_conf_t *conf; |
| 3657 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
| 3658 | mdk_rdev_t *rdev; |
| 3659 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3660 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3661 | |
| 3662 | mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3663 | conf = mddev->private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3664 | rdev = (void*)raid_bi->bi_next; |
| 3665 | raid_bi->bi_next = NULL; |
| 3666 | |
| 3667 | rdev_dec_pending(rdev, conf->mddev); |
| 3668 | |
| 3669 | if (!error && uptodate) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3670 | bio_endio(raid_bi, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3671 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 3672 | wake_up(&conf->wait_for_stripe); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3673 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3674 | } |
| 3675 | |
| 3676 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3677 | 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] | 3678 | |
| 3679 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3680 | } |
| 3681 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3682 | static int bio_fits_rdev(struct bio *bi) |
| 3683 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3684 | struct request_queue *q = bdev_get_queue(bi->bi_bdev); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3685 | |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 3686 | if ((bi->bi_size>>9) > queue_max_sectors(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3687 | return 0; |
| 3688 | blk_recount_segments(q, bi); |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 3689 | if (bi->bi_phys_segments > queue_max_phys_segments(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3690 | return 0; |
| 3691 | |
| 3692 | if (q->merge_bvec_fn) |
| 3693 | /* it's too hard to apply the merge_bvec_fn at this stage, |
| 3694 | * just just give up |
| 3695 | */ |
| 3696 | return 0; |
| 3697 | |
| 3698 | return 1; |
| 3699 | } |
| 3700 | |
| 3701 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3702 | static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3703 | { |
| 3704 | mddev_t *mddev = q->queuedata; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3705 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3706 | unsigned int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3707 | struct bio* align_bi; |
| 3708 | mdk_rdev_t *rdev; |
| 3709 | |
| 3710 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3711 | pr_debug("chunk_aligned_read : non aligned\n"); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3712 | return 0; |
| 3713 | } |
| 3714 | /* |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3715 | * use bio_clone to make a copy of the bio |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3716 | */ |
| 3717 | align_bi = bio_clone(raid_bio, GFP_NOIO); |
| 3718 | if (!align_bi) |
| 3719 | return 0; |
| 3720 | /* |
| 3721 | * set bi_end_io to a new function, and set bi_private to the |
| 3722 | * original bio. |
| 3723 | */ |
| 3724 | align_bi->bi_end_io = raid5_align_endio; |
| 3725 | align_bi->bi_private = raid_bio; |
| 3726 | /* |
| 3727 | * compute position |
| 3728 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3729 | align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector, |
| 3730 | 0, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3731 | &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3732 | |
| 3733 | rcu_read_lock(); |
| 3734 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 3735 | if (rdev && test_bit(In_sync, &rdev->flags)) { |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3736 | atomic_inc(&rdev->nr_pending); |
| 3737 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3738 | raid_bio->bi_next = (void*)rdev; |
| 3739 | align_bi->bi_bdev = rdev->bdev; |
| 3740 | align_bi->bi_flags &= ~(1 << BIO_SEG_VALID); |
| 3741 | align_bi->bi_sector += rdev->data_offset; |
| 3742 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3743 | if (!bio_fits_rdev(align_bi)) { |
| 3744 | /* too big in some way */ |
| 3745 | bio_put(align_bi); |
| 3746 | rdev_dec_pending(rdev, mddev); |
| 3747 | return 0; |
| 3748 | } |
| 3749 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3750 | spin_lock_irq(&conf->device_lock); |
| 3751 | wait_event_lock_irq(conf->wait_for_stripe, |
| 3752 | conf->quiesce == 0, |
| 3753 | conf->device_lock, /* nothing */); |
| 3754 | atomic_inc(&conf->active_aligned_reads); |
| 3755 | spin_unlock_irq(&conf->device_lock); |
| 3756 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3757 | generic_make_request(align_bi); |
| 3758 | return 1; |
| 3759 | } else { |
| 3760 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3761 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3762 | return 0; |
| 3763 | } |
| 3764 | } |
| 3765 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3766 | /* __get_priority_stripe - get the next stripe to process |
| 3767 | * |
| 3768 | * Full stripe writes are allowed to pass preread active stripes up until |
| 3769 | * the bypass_threshold is exceeded. In general the bypass_count |
| 3770 | * increments when the handle_list is handled before the hold_list; however, it |
| 3771 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 3772 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 3773 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 3774 | * handle_list. |
| 3775 | */ |
| 3776 | static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf) |
| 3777 | { |
| 3778 | struct stripe_head *sh; |
| 3779 | |
| 3780 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 3781 | __func__, |
| 3782 | list_empty(&conf->handle_list) ? "empty" : "busy", |
| 3783 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 3784 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 3785 | |
| 3786 | if (!list_empty(&conf->handle_list)) { |
| 3787 | sh = list_entry(conf->handle_list.next, typeof(*sh), lru); |
| 3788 | |
| 3789 | if (list_empty(&conf->hold_list)) |
| 3790 | conf->bypass_count = 0; |
| 3791 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 3792 | if (conf->hold_list.next == conf->last_hold) |
| 3793 | conf->bypass_count++; |
| 3794 | else { |
| 3795 | conf->last_hold = conf->hold_list.next; |
| 3796 | conf->bypass_count -= conf->bypass_threshold; |
| 3797 | if (conf->bypass_count < 0) |
| 3798 | conf->bypass_count = 0; |
| 3799 | } |
| 3800 | } |
| 3801 | } else if (!list_empty(&conf->hold_list) && |
| 3802 | ((conf->bypass_threshold && |
| 3803 | conf->bypass_count > conf->bypass_threshold) || |
| 3804 | atomic_read(&conf->pending_full_writes) == 0)) { |
| 3805 | sh = list_entry(conf->hold_list.next, |
| 3806 | typeof(*sh), lru); |
| 3807 | conf->bypass_count -= conf->bypass_threshold; |
| 3808 | if (conf->bypass_count < 0) |
| 3809 | conf->bypass_count = 0; |
| 3810 | } else |
| 3811 | return NULL; |
| 3812 | |
| 3813 | list_del_init(&sh->lru); |
| 3814 | atomic_inc(&sh->count); |
| 3815 | BUG_ON(atomic_read(&sh->count) != 1); |
| 3816 | return sh; |
| 3817 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3818 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3819 | static int make_request(struct request_queue *q, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3820 | { |
| 3821 | mddev_t *mddev = q->queuedata; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 3822 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3823 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3824 | sector_t new_sector; |
| 3825 | sector_t logical_sector, last_sector; |
| 3826 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 3827 | const int rw = bio_data_dir(bi); |
Tejun Heo | c995905 | 2008-08-25 19:47:21 +0900 | [diff] [blame] | 3828 | int cpu, remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3829 | |
Jens Axboe | 1f98a13 | 2009-09-11 14:32:04 +0200 | [diff] [blame] | 3830 | if (unlikely(bio_rw_flagged(bi, BIO_RW_BARRIER))) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3831 | bio_endio(bi, -EOPNOTSUPP); |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 3832 | return 0; |
| 3833 | } |
| 3834 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 3835 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3836 | |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 3837 | cpu = part_stat_lock(); |
| 3838 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 3839 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], |
| 3840 | bio_sectors(bi)); |
| 3841 | part_stat_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3842 | |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 3843 | if (rw == READ && |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 3844 | mddev->reshape_position == MaxSector && |
| 3845 | chunk_aligned_read(q,bi)) |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3846 | return 0; |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 3847 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3848 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 3849 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 3850 | bi->bi_next = NULL; |
| 3851 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3853 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
| 3854 | DEFINE_WAIT(w); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3855 | int disks, data_disks; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3856 | int previous; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3857 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3858 | retry: |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3859 | previous = 0; |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3860 | disks = conf->raid_disks; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3861 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3862 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3863 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 3864 | * 64bit on a 32bit platform, and so it might be |
| 3865 | * possible to see a half-updated value |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3866 | * Ofcourse reshape_progress could change after |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 3867 | * the lock is dropped, so once we get a reference |
| 3868 | * to the stripe that we think it is, we will have |
| 3869 | * to check again. |
| 3870 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3871 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3872 | if (mddev->delta_disks < 0 |
| 3873 | ? logical_sector < conf->reshape_progress |
| 3874 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3875 | disks = conf->previous_raid_disks; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3876 | previous = 1; |
| 3877 | } else { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3878 | if (mddev->delta_disks < 0 |
| 3879 | ? logical_sector < conf->reshape_safe |
| 3880 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3881 | spin_unlock_irq(&conf->device_lock); |
| 3882 | schedule(); |
| 3883 | goto retry; |
| 3884 | } |
| 3885 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3886 | spin_unlock_irq(&conf->device_lock); |
| 3887 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3888 | data_disks = disks - conf->max_degraded; |
| 3889 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3890 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 3891 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3892 | &dd_idx, NULL); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3893 | pr_debug("raid5: make_request, sector %llu logical %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3894 | (unsigned long long)new_sector, |
| 3895 | (unsigned long long)logical_sector); |
| 3896 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3897 | sh = get_active_stripe(conf, new_sector, previous, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3898 | (bi->bi_rw&RWA_MASK), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3899 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3900 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3901 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 3902 | * stripe, so we must do the range check again. |
| 3903 | * Expansion could still move past after this |
| 3904 | * test, but as we are holding a reference to |
| 3905 | * 'sh', we know that if that happens, |
| 3906 | * STRIPE_EXPANDING will get set and the expansion |
| 3907 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3908 | */ |
| 3909 | int must_retry = 0; |
| 3910 | spin_lock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3911 | if (mddev->delta_disks < 0 |
| 3912 | ? logical_sector >= conf->reshape_progress |
| 3913 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3914 | /* mismatch, need to try again */ |
| 3915 | must_retry = 1; |
| 3916 | spin_unlock_irq(&conf->device_lock); |
| 3917 | if (must_retry) { |
| 3918 | release_stripe(sh); |
Dan Williams | 7a3ab90 | 2009-06-16 16:00:33 -0700 | [diff] [blame] | 3919 | schedule(); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3920 | goto retry; |
| 3921 | } |
| 3922 | } |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 3923 | |
NeilBrown | a5c308d | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 3924 | if (bio_data_dir(bi) == WRITE && |
| 3925 | logical_sector >= mddev->suspend_lo && |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3926 | logical_sector < mddev->suspend_hi) { |
| 3927 | release_stripe(sh); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 3928 | /* As the suspend_* range is controlled by |
| 3929 | * userspace, we want an interruptible |
| 3930 | * wait. |
| 3931 | */ |
| 3932 | flush_signals(current); |
| 3933 | prepare_to_wait(&conf->wait_for_overlap, |
| 3934 | &w, TASK_INTERRUPTIBLE); |
| 3935 | if (logical_sector >= mddev->suspend_lo && |
| 3936 | logical_sector < mddev->suspend_hi) |
| 3937 | schedule(); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3938 | goto retry; |
| 3939 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3940 | |
| 3941 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
| 3942 | !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) { |
| 3943 | /* Stripe is busy expanding or |
| 3944 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3945 | * and wait a while |
| 3946 | */ |
| 3947 | raid5_unplug_device(mddev->queue); |
| 3948 | release_stripe(sh); |
| 3949 | schedule(); |
| 3950 | goto retry; |
| 3951 | } |
| 3952 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 3953 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3954 | clear_bit(STRIPE_DELAYED, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3955 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3956 | } else { |
| 3957 | /* cannot get stripe for read-ahead, just give-up */ |
| 3958 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 3959 | finish_wait(&conf->wait_for_overlap, &w); |
| 3960 | break; |
| 3961 | } |
| 3962 | |
| 3963 | } |
| 3964 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3965 | remaining = raid5_dec_bi_phys_segments(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 3966 | spin_unlock_irq(&conf->device_lock); |
| 3967 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3968 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3969 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3970 | md_write_end(mddev); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3971 | |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 3972 | bio_endio(bi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3973 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3974 | return 0; |
| 3975 | } |
| 3976 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3977 | static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks); |
| 3978 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3979 | 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] | 3980 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3981 | /* reshaping is quite different to recovery/resync so it is |
| 3982 | * handled quite separately ... here. |
| 3983 | * |
| 3984 | * On each call to sync_request, we gather one chunk worth of |
| 3985 | * destination stripes and flag them as expanding. |
| 3986 | * Then we find all the source stripes and request reads. |
| 3987 | * As the reads complete, handle_stripe will copy the data |
| 3988 | * into the destination stripe and release that stripe. |
| 3989 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3990 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 3991 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3992 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3993 | int raid_disks = conf->previous_raid_disks; |
| 3994 | int data_disks = raid_disks - conf->max_degraded; |
| 3995 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3996 | int i; |
| 3997 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 3998 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 3999 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4000 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4001 | struct list_head stripes; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4002 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4003 | if (sector_nr == 0) { |
| 4004 | /* If restarting in the middle, skip the initial sectors */ |
| 4005 | if (mddev->delta_disks < 0 && |
| 4006 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 4007 | sector_nr = raid5_size(mddev, 0, 0) |
| 4008 | - conf->reshape_progress; |
NeilBrown | a639755 | 2009-08-13 10:13:00 +1000 | [diff] [blame] | 4009 | } else if (mddev->delta_disks >= 0 && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4010 | conf->reshape_progress > 0) |
| 4011 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4012 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4013 | if (sector_nr) { |
| 4014 | *skipped = 1; |
| 4015 | return sector_nr; |
| 4016 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4017 | } |
| 4018 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4019 | /* We need to process a full chunk at a time. |
| 4020 | * If old and new chunk sizes differ, we need to process the |
| 4021 | * largest of these |
| 4022 | */ |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4023 | if (mddev->new_chunk_sectors > mddev->chunk_sectors) |
| 4024 | reshape_sectors = mddev->new_chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4025 | else |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4026 | reshape_sectors = mddev->chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4027 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4028 | /* we update the metadata when there is more than 3Meg |
| 4029 | * in the block range (that is rather arbitrary, should |
| 4030 | * probably be time based) or when the data about to be |
| 4031 | * copied would over-write the source of the data at |
| 4032 | * the front of the range. |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4033 | * i.e. one new_stripe along from reshape_progress new_maps |
| 4034 | * to after where reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4035 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4036 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4037 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4038 | readpos = conf->reshape_progress; |
| 4039 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4040 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4041 | sector_div(safepos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4042 | if (mddev->delta_disks < 0) { |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4043 | writepos -= min_t(sector_t, reshape_sectors, writepos); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4044 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4045 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4046 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4047 | writepos += reshape_sectors; |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4048 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
| 4049 | safepos -= min_t(sector_t, reshape_sectors, safepos); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4050 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4051 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4052 | /* 'writepos' is the most advanced device address we might write. |
| 4053 | * 'readpos' is the least advanced device address we might read. |
| 4054 | * 'safepos' is the least address recorded in the metadata as having |
| 4055 | * been reshaped. |
| 4056 | * If 'readpos' is behind 'writepos', then there is no way that we can |
| 4057 | * ensure safety in the face of a crash - that must be done by userspace |
| 4058 | * making a backup of the data. So in that case there is no particular |
| 4059 | * rush to update metadata. |
| 4060 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 4061 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 4062 | * we can be safe in the event of a crash. |
| 4063 | * So we insist on updating metadata if safepos is behind writepos and |
| 4064 | * readpos is beyond writepos. |
| 4065 | * In any case, update the metadata every 10 seconds. |
| 4066 | * Maybe that number should be configurable, but I'm not sure it is |
| 4067 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 4068 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4069 | if ((mddev->delta_disks < 0 |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4070 | ? (safepos > writepos && readpos < writepos) |
| 4071 | : (safepos < writepos && readpos > writepos)) || |
| 4072 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4073 | /* Cannot proceed until we've updated the superblock... */ |
| 4074 | wait_event(conf->wait_for_overlap, |
| 4075 | atomic_read(&conf->reshape_stripes)==0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4076 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4077 | mddev->curr_resync_completed = mddev->curr_resync; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4078 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4079 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4080 | md_wakeup_thread(mddev->thread); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4081 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4082 | kthread_should_stop()); |
| 4083 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4084 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4085 | spin_unlock_irq(&conf->device_lock); |
| 4086 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4087 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4088 | } |
| 4089 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4090 | if (mddev->delta_disks < 0) { |
| 4091 | BUG_ON(conf->reshape_progress == 0); |
| 4092 | stripe_addr = writepos; |
| 4093 | BUG_ON((mddev->dev_sectors & |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4094 | ~((sector_t)reshape_sectors - 1)) |
| 4095 | - reshape_sectors - stripe_addr |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4096 | != sector_nr); |
| 4097 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4098 | BUG_ON(writepos != sector_nr + reshape_sectors); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4099 | stripe_addr = sector_nr; |
| 4100 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4101 | INIT_LIST_HEAD(&stripes); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4102 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4103 | int j; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4104 | int skipped_disk = 0; |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4105 | sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4106 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 4107 | atomic_inc(&conf->reshape_stripes); |
| 4108 | /* If any of this stripe is beyond the end of the old |
| 4109 | * array, then we need to zero those blocks |
| 4110 | */ |
| 4111 | for (j=sh->disks; j--;) { |
| 4112 | sector_t s; |
| 4113 | if (j == sh->pd_idx) |
| 4114 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4115 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4116 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4117 | continue; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4118 | s = compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4119 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4120 | skipped_disk = 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4121 | continue; |
| 4122 | } |
| 4123 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 4124 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 4125 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 4126 | } |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4127 | if (!skipped_disk) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4128 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 4129 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4130 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4131 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4132 | } |
| 4133 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4134 | if (mddev->delta_disks < 0) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4135 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4136 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4137 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4138 | spin_unlock_irq(&conf->device_lock); |
| 4139 | /* Ok, those stripe are ready. We can start scheduling |
| 4140 | * reads on the source stripes. |
| 4141 | * The source stripes are determined by mapping the first and last |
| 4142 | * block on the destination stripes. |
| 4143 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4144 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4145 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4146 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4147 | last_sector = |
NeilBrown | 0e6e027 | 2009-06-09 16:32:22 +1000 | [diff] [blame] | 4148 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4149 | * new_data_disks - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4150 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4151 | if (last_sector >= mddev->dev_sectors) |
| 4152 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4153 | while (first_sector <= last_sector) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4154 | sh = get_active_stripe(conf, first_sector, 1, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4155 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 4156 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4157 | release_stripe(sh); |
| 4158 | first_sector += STRIPE_SECTORS; |
| 4159 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4160 | /* Now that the sources are clearly marked, we can release |
| 4161 | * the destination stripes |
| 4162 | */ |
| 4163 | while (!list_empty(&stripes)) { |
| 4164 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 4165 | list_del_init(&sh->lru); |
| 4166 | release_stripe(sh); |
| 4167 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4168 | /* If this takes us to the resync_max point where we have to pause, |
| 4169 | * then we need to write out the superblock. |
| 4170 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4171 | sector_nr += reshape_sectors; |
NeilBrown | c03f6a1 | 2009-04-17 11:06:30 +1000 | [diff] [blame] | 4172 | if ((sector_nr - mddev->curr_resync_completed) * 2 |
| 4173 | >= mddev->resync_max - mddev->curr_resync_completed) { |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4174 | /* Cannot proceed until we've updated the superblock... */ |
| 4175 | wait_event(conf->wait_for_overlap, |
| 4176 | atomic_read(&conf->reshape_stripes) == 0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4177 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 48606a9 | 2009-06-18 09:14:12 +1000 | [diff] [blame] | 4178 | mddev->curr_resync_completed = mddev->curr_resync + reshape_sectors; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4179 | conf->reshape_checkpoint = jiffies; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4180 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 4181 | md_wakeup_thread(mddev->thread); |
| 4182 | wait_event(mddev->sb_wait, |
| 4183 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) |
| 4184 | || kthread_should_stop()); |
| 4185 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4186 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4187 | spin_unlock_irq(&conf->device_lock); |
| 4188 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4189 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4190 | } |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4191 | return reshape_sectors; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4192 | } |
| 4193 | |
| 4194 | /* FIXME go_faster isn't used */ |
| 4195 | static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster) |
| 4196 | { |
| 4197 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 4198 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4199 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4200 | int sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4201 | int still_degraded = 0; |
| 4202 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4203 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4204 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4205 | /* just being told to finish up .. nothing much to do */ |
| 4206 | unplug_slaves(mddev); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 4207 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4208 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 4209 | end_reshape(conf); |
| 4210 | return 0; |
| 4211 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4212 | |
| 4213 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 4214 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 4215 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4216 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4217 | conf->fullsync = 0; |
| 4218 | bitmap_close_sync(mddev->bitmap); |
| 4219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4220 | return 0; |
| 4221 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4222 | |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4223 | /* Allow raid5_quiesce to complete */ |
| 4224 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); |
| 4225 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4226 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4227 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4228 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4229 | /* No need to check resync_max as we never do more than one |
| 4230 | * stripe, and as resync_max will always be on a chunk boundary, |
| 4231 | * if the check in md_do_sync didn't fire, there is no chance |
| 4232 | * of overstepping resync_max here |
| 4233 | */ |
| 4234 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4235 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | * to resync, then assert that we are finished, because there is |
| 4237 | * nothing we can do. |
| 4238 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 4239 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4240 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4241 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 4242 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | return rv; |
| 4244 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4245 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
NeilBrown | 3855ad9 | 2005-11-08 21:39:38 -0800 | [diff] [blame] | 4246 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4247 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
| 4248 | /* we can skip this block, and probably more */ |
| 4249 | sync_blocks /= STRIPE_SECTORS; |
| 4250 | *skipped = 1; |
| 4251 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 4252 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4253 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 4254 | |
| 4255 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 4256 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4257 | sh = get_active_stripe(conf, sector_nr, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 | if (sh == NULL) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4259 | sh = get_active_stripe(conf, sector_nr, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4261 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 4263 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4264 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4265 | /* Need to check if array will still be degraded after recovery/resync |
| 4266 | * We don't need to check the 'failed' flag as when that gets set, |
| 4267 | * recovery aborts. |
| 4268 | */ |
NeilBrown | f001a70 | 2009-06-09 14:30:31 +1000 | [diff] [blame] | 4269 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4270 | if (conf->disks[i].rdev == NULL) |
| 4271 | still_degraded = 1; |
| 4272 | |
| 4273 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 4274 | |
| 4275 | spin_lock(&sh->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | set_bit(STRIPE_SYNCING, &sh->state); |
| 4277 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 4278 | spin_unlock(&sh->lock); |
| 4279 | |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 4280 | /* wait for any blocked device to be handled */ |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4281 | while (unlikely(!handle_stripe(sh))) |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 4282 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | release_stripe(sh); |
| 4284 | |
| 4285 | return STRIPE_SECTORS; |
| 4286 | } |
| 4287 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4288 | static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio) |
| 4289 | { |
| 4290 | /* We may not be able to submit a whole bio at once as there |
| 4291 | * may not be enough stripe_heads available. |
| 4292 | * We cannot pre-allocate enough stripe_heads as we may need |
| 4293 | * more than exist in the cache (if we allow ever large chunks). |
| 4294 | * So we do one stripe head at a time and record in |
| 4295 | * ->bi_hw_segments how many have been done. |
| 4296 | * |
| 4297 | * We *know* that this entire raid_bio is in one chunk, so |
| 4298 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 4299 | */ |
| 4300 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4301 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4302 | sector_t sector, logical_sector, last_sector; |
| 4303 | int scnt = 0; |
| 4304 | int remaining; |
| 4305 | int handled = 0; |
| 4306 | |
| 4307 | logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4308 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4309 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4310 | last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9); |
| 4311 | |
| 4312 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4313 | logical_sector += STRIPE_SECTORS, |
| 4314 | sector += STRIPE_SECTORS, |
| 4315 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4316 | |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4317 | if (scnt < raid5_bi_hw_segments(raid_bio)) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4318 | /* already done this stripe */ |
| 4319 | continue; |
| 4320 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4321 | sh = get_active_stripe(conf, sector, 0, 1, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4322 | |
| 4323 | if (!sh) { |
| 4324 | /* failed to get a stripe - must wait */ |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4325 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4326 | conf->retry_read_aligned = raid_bio; |
| 4327 | return handled; |
| 4328 | } |
| 4329 | |
| 4330 | set_bit(R5_ReadError, &sh->dev[dd_idx].flags); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4331 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { |
| 4332 | release_stripe(sh); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4333 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4334 | conf->retry_read_aligned = raid_bio; |
| 4335 | return handled; |
| 4336 | } |
| 4337 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4338 | handle_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4339 | release_stripe(sh); |
| 4340 | handled++; |
| 4341 | } |
| 4342 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4343 | remaining = raid5_dec_bi_phys_segments(raid_bio); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4344 | spin_unlock_irq(&conf->device_lock); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4345 | if (remaining == 0) |
| 4346 | bio_endio(raid_bio, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4347 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 4348 | wake_up(&conf->wait_for_stripe); |
| 4349 | return handled; |
| 4350 | } |
| 4351 | |
Dan Williams | 07a3b41 | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 4352 | #ifdef CONFIG_MULTICORE_RAID456 |
| 4353 | static void __process_stripe(void *param, async_cookie_t cookie) |
| 4354 | { |
| 4355 | struct stripe_head *sh = param; |
| 4356 | |
| 4357 | handle_stripe(sh); |
| 4358 | release_stripe(sh); |
| 4359 | } |
| 4360 | |
| 4361 | static void process_stripe(struct stripe_head *sh, struct list_head *domain) |
| 4362 | { |
| 4363 | async_schedule_domain(__process_stripe, sh, domain); |
| 4364 | } |
| 4365 | |
| 4366 | static void synchronize_stripe_processing(struct list_head *domain) |
| 4367 | { |
| 4368 | async_synchronize_full_domain(domain); |
| 4369 | } |
| 4370 | #else |
| 4371 | static void process_stripe(struct stripe_head *sh, struct list_head *domain) |
| 4372 | { |
| 4373 | handle_stripe(sh); |
| 4374 | release_stripe(sh); |
| 4375 | cond_resched(); |
| 4376 | } |
| 4377 | |
| 4378 | static void synchronize_stripe_processing(struct list_head *domain) |
| 4379 | { |
| 4380 | } |
| 4381 | #endif |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4382 | |
| 4383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | /* |
| 4385 | * This is our raid5 kernel thread. |
| 4386 | * |
| 4387 | * We scan the hash table for stripes which can be handled now. |
| 4388 | * During the scan, completed stripes are saved for us by the interrupt |
| 4389 | * handler, so that they will not have to wait for our next wakeup. |
| 4390 | */ |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 4391 | static void raid5d(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | { |
| 4393 | struct stripe_head *sh; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4394 | raid5_conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4395 | int handled; |
Dan Williams | 07a3b41 | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 4396 | LIST_HEAD(raid_domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4398 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4399 | |
| 4400 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4401 | |
| 4402 | handled = 0; |
| 4403 | spin_lock_irq(&conf->device_lock); |
| 4404 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4405 | struct bio *bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4406 | |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 4407 | if (conf->seq_flush != conf->seq_write) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4408 | int seq = conf->seq_flush; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4409 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4410 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4411 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4412 | conf->seq_write = seq; |
| 4413 | activate_bit_delay(conf); |
| 4414 | } |
| 4415 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4416 | while ((bio = remove_bio_from_retry(conf))) { |
| 4417 | int ok; |
| 4418 | spin_unlock_irq(&conf->device_lock); |
| 4419 | ok = retry_aligned_read(conf, bio); |
| 4420 | spin_lock_irq(&conf->device_lock); |
| 4421 | if (!ok) |
| 4422 | break; |
| 4423 | handled++; |
| 4424 | } |
| 4425 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4426 | sh = __get_priority_stripe(conf); |
| 4427 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4428 | if (!sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4429 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | spin_unlock_irq(&conf->device_lock); |
| 4431 | |
| 4432 | handled++; |
Dan Williams | 07a3b41 | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 4433 | process_stripe(sh, &raid_domain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | |
| 4435 | spin_lock_irq(&conf->device_lock); |
| 4436 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4437 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | |
| 4439 | spin_unlock_irq(&conf->device_lock); |
| 4440 | |
Dan Williams | 07a3b41 | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 4441 | synchronize_stripe_processing(&raid_domain); |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4442 | async_tx_issue_pending_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 | unplug_slaves(mddev); |
| 4444 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4445 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4446 | } |
| 4447 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4448 | static ssize_t |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4449 | raid5_show_stripe_cache_size(mddev_t *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4450 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4451 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4452 | if (conf) |
| 4453 | return sprintf(page, "%d\n", conf->max_nr_stripes); |
| 4454 | else |
| 4455 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4456 | } |
| 4457 | |
| 4458 | static ssize_t |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4459 | 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] | 4460 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4461 | raid5_conf_t *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4462 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4463 | int err; |
| 4464 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4465 | if (len >= PAGE_SIZE) |
| 4466 | return -EINVAL; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4467 | if (!conf) |
| 4468 | return -ENODEV; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4469 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4470 | if (strict_strtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4471 | return -EINVAL; |
| 4472 | if (new <= 16 || new > 32768) |
| 4473 | return -EINVAL; |
| 4474 | while (new < conf->max_nr_stripes) { |
| 4475 | if (drop_one_stripe(conf)) |
| 4476 | conf->max_nr_stripes--; |
| 4477 | else |
| 4478 | break; |
| 4479 | } |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4480 | err = md_allow_write(mddev); |
| 4481 | if (err) |
| 4482 | return err; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4483 | while (new > conf->max_nr_stripes) { |
| 4484 | if (grow_one_stripe(conf)) |
| 4485 | conf->max_nr_stripes++; |
| 4486 | else break; |
| 4487 | } |
| 4488 | return len; |
| 4489 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4490 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4491 | static struct md_sysfs_entry |
| 4492 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 4493 | raid5_show_stripe_cache_size, |
| 4494 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4495 | |
| 4496 | static ssize_t |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4497 | raid5_show_preread_threshold(mddev_t *mddev, char *page) |
| 4498 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4499 | raid5_conf_t *conf = mddev->private; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4500 | if (conf) |
| 4501 | return sprintf(page, "%d\n", conf->bypass_threshold); |
| 4502 | else |
| 4503 | return 0; |
| 4504 | } |
| 4505 | |
| 4506 | static ssize_t |
| 4507 | raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len) |
| 4508 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4509 | raid5_conf_t *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4510 | unsigned long new; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4511 | if (len >= PAGE_SIZE) |
| 4512 | return -EINVAL; |
| 4513 | if (!conf) |
| 4514 | return -ENODEV; |
| 4515 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4516 | if (strict_strtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4517 | return -EINVAL; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4518 | if (new > conf->max_nr_stripes) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4519 | return -EINVAL; |
| 4520 | conf->bypass_threshold = new; |
| 4521 | return len; |
| 4522 | } |
| 4523 | |
| 4524 | static struct md_sysfs_entry |
| 4525 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 4526 | S_IRUGO | S_IWUSR, |
| 4527 | raid5_show_preread_threshold, |
| 4528 | raid5_store_preread_threshold); |
| 4529 | |
| 4530 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4531 | stripe_cache_active_show(mddev_t *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4532 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4533 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4534 | if (conf) |
| 4535 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 4536 | else |
| 4537 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4538 | } |
| 4539 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4540 | static struct md_sysfs_entry |
| 4541 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4542 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4543 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4544 | &raid5_stripecache_size.attr, |
| 4545 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4546 | &raid5_preread_bypass_threshold.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4547 | NULL, |
| 4548 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4549 | static struct attribute_group raid5_attrs_group = { |
| 4550 | .name = NULL, |
| 4551 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4552 | }; |
| 4553 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4554 | static sector_t |
| 4555 | raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks) |
| 4556 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 4557 | raid5_conf_t *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4558 | |
| 4559 | if (!sectors) |
| 4560 | sectors = mddev->dev_sectors; |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 4561 | if (!raid_disks) { |
| 4562 | /* size is defined by the smallest of previous and new size */ |
| 4563 | if (conf->raid_disks < conf->previous_raid_disks) |
| 4564 | raid_disks = conf->raid_disks; |
| 4565 | else |
| 4566 | raid_disks = conf->previous_raid_disks; |
| 4567 | } |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4568 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4569 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4570 | sectors &= ~((sector_t)mddev->new_chunk_sectors - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4571 | return sectors * (raid_disks - conf->max_degraded); |
| 4572 | } |
| 4573 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4574 | static void raid5_free_percpu(raid5_conf_t *conf) |
| 4575 | { |
| 4576 | struct raid5_percpu *percpu; |
| 4577 | unsigned long cpu; |
| 4578 | |
| 4579 | if (!conf->percpu) |
| 4580 | return; |
| 4581 | |
| 4582 | get_online_cpus(); |
| 4583 | for_each_possible_cpu(cpu) { |
| 4584 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4585 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4586 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4587 | } |
| 4588 | #ifdef CONFIG_HOTPLUG_CPU |
| 4589 | unregister_cpu_notifier(&conf->cpu_notify); |
| 4590 | #endif |
| 4591 | put_online_cpus(); |
| 4592 | |
| 4593 | free_percpu(conf->percpu); |
| 4594 | } |
| 4595 | |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4596 | static void free_conf(raid5_conf_t *conf) |
| 4597 | { |
| 4598 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4599 | raid5_free_percpu(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4600 | kfree(conf->disks); |
| 4601 | kfree(conf->stripe_hashtbl); |
| 4602 | kfree(conf); |
| 4603 | } |
| 4604 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4605 | #ifdef CONFIG_HOTPLUG_CPU |
| 4606 | static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action, |
| 4607 | void *hcpu) |
| 4608 | { |
| 4609 | raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify); |
| 4610 | long cpu = (long)hcpu; |
| 4611 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4612 | |
| 4613 | switch (action) { |
| 4614 | case CPU_UP_PREPARE: |
| 4615 | case CPU_UP_PREPARE_FROZEN: |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4616 | if (conf->level == 6 && !percpu->spare_page) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4617 | percpu->spare_page = alloc_page(GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4618 | if (!percpu->scribble) |
| 4619 | percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
| 4620 | |
| 4621 | if (!percpu->scribble || |
| 4622 | (conf->level == 6 && !percpu->spare_page)) { |
| 4623 | safe_put_page(percpu->spare_page); |
| 4624 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4625 | pr_err("%s: failed memory allocation for cpu%ld\n", |
| 4626 | __func__, cpu); |
| 4627 | return NOTIFY_BAD; |
| 4628 | } |
| 4629 | break; |
| 4630 | case CPU_DEAD: |
| 4631 | case CPU_DEAD_FROZEN: |
| 4632 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4633 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4634 | percpu->spare_page = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4635 | percpu->scribble = NULL; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4636 | break; |
| 4637 | default: |
| 4638 | break; |
| 4639 | } |
| 4640 | return NOTIFY_OK; |
| 4641 | } |
| 4642 | #endif |
| 4643 | |
| 4644 | static int raid5_alloc_percpu(raid5_conf_t *conf) |
| 4645 | { |
| 4646 | unsigned long cpu; |
| 4647 | struct page *spare_page; |
| 4648 | struct raid5_percpu *allcpus; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4649 | void *scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4650 | int err; |
| 4651 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4652 | allcpus = alloc_percpu(struct raid5_percpu); |
| 4653 | if (!allcpus) |
| 4654 | return -ENOMEM; |
| 4655 | conf->percpu = allcpus; |
| 4656 | |
| 4657 | get_online_cpus(); |
| 4658 | err = 0; |
| 4659 | for_each_present_cpu(cpu) { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4660 | if (conf->level == 6) { |
| 4661 | spare_page = alloc_page(GFP_KERNEL); |
| 4662 | if (!spare_page) { |
| 4663 | err = -ENOMEM; |
| 4664 | break; |
| 4665 | } |
| 4666 | per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page; |
| 4667 | } |
| 4668 | scribble = kmalloc(scribble_len(conf->raid_disks), GFP_KERNEL); |
| 4669 | if (!scribble) { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4670 | err = -ENOMEM; |
| 4671 | break; |
| 4672 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4673 | per_cpu_ptr(conf->percpu, cpu)->scribble = scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4674 | } |
| 4675 | #ifdef CONFIG_HOTPLUG_CPU |
| 4676 | conf->cpu_notify.notifier_call = raid456_cpu_notify; |
| 4677 | conf->cpu_notify.priority = 0; |
| 4678 | if (err == 0) |
| 4679 | err = register_cpu_notifier(&conf->cpu_notify); |
| 4680 | #endif |
| 4681 | put_online_cpus(); |
| 4682 | |
| 4683 | return err; |
| 4684 | } |
| 4685 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4686 | static raid5_conf_t *setup_conf(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4687 | { |
| 4688 | raid5_conf_t *conf; |
| 4689 | int raid_disk, memory; |
| 4690 | mdk_rdev_t *rdev; |
| 4691 | struct disk_info *disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4692 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4693 | if (mddev->new_level != 5 |
| 4694 | && mddev->new_level != 4 |
| 4695 | && mddev->new_level != 6) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4696 | printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4697 | mdname(mddev), mddev->new_level); |
| 4698 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4699 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4700 | if ((mddev->new_level == 5 |
| 4701 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 4702 | (mddev->new_level == 6 |
| 4703 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4704 | printk(KERN_ERR "raid5: %s: layout %d not supported\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4705 | mdname(mddev), mddev->new_layout); |
| 4706 | return ERR_PTR(-EIO); |
| 4707 | } |
| 4708 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
| 4709 | printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n", |
| 4710 | mdname(mddev), mddev->raid_disks); |
| 4711 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4712 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4713 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4714 | if (!mddev->new_chunk_sectors || |
| 4715 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || |
| 4716 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4717 | printk(KERN_ERR "raid5: invalid chunk size %d for %s\n", |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4718 | mddev->new_chunk_sectors << 9, mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4719 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4720 | } |
| 4721 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4722 | conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL); |
| 4723 | if (conf == NULL) |
| 4724 | goto abort; |
| 4725 | |
| 4726 | conf->raid_disks = mddev->raid_disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4727 | conf->scribble_len = scribble_len(conf->raid_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4728 | if (mddev->reshape_position == MaxSector) |
| 4729 | conf->previous_raid_disks = mddev->raid_disks; |
| 4730 | else |
| 4731 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
| 4732 | |
| 4733 | conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info), |
| 4734 | GFP_KERNEL); |
| 4735 | if (!conf->disks) |
| 4736 | goto abort; |
| 4737 | |
| 4738 | conf->mddev = mddev; |
| 4739 | |
| 4740 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 4741 | goto abort; |
| 4742 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4743 | conf->level = mddev->new_level; |
| 4744 | if (raid5_alloc_percpu(conf) != 0) |
| 4745 | goto abort; |
| 4746 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4747 | spin_lock_init(&conf->device_lock); |
| 4748 | init_waitqueue_head(&conf->wait_for_stripe); |
| 4749 | init_waitqueue_head(&conf->wait_for_overlap); |
| 4750 | INIT_LIST_HEAD(&conf->handle_list); |
| 4751 | INIT_LIST_HEAD(&conf->hold_list); |
| 4752 | INIT_LIST_HEAD(&conf->delayed_list); |
| 4753 | INIT_LIST_HEAD(&conf->bitmap_list); |
| 4754 | INIT_LIST_HEAD(&conf->inactive_list); |
| 4755 | atomic_set(&conf->active_stripes, 0); |
| 4756 | atomic_set(&conf->preread_active_stripes, 0); |
| 4757 | atomic_set(&conf->active_aligned_reads, 0); |
| 4758 | conf->bypass_threshold = BYPASS_THRESHOLD; |
| 4759 | |
| 4760 | pr_debug("raid5: run(%s) called.\n", mdname(mddev)); |
| 4761 | |
| 4762 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 4763 | raid_disk = rdev->raid_disk; |
| 4764 | if (raid_disk >= conf->raid_disks |
| 4765 | || raid_disk < 0) |
| 4766 | continue; |
| 4767 | disk = conf->disks + raid_disk; |
| 4768 | |
| 4769 | disk->rdev = rdev; |
| 4770 | |
| 4771 | if (test_bit(In_sync, &rdev->flags)) { |
| 4772 | char b[BDEVNAME_SIZE]; |
| 4773 | printk(KERN_INFO "raid5: device %s operational as raid" |
| 4774 | " disk %d\n", bdevname(rdev->bdev,b), |
| 4775 | raid_disk); |
| 4776 | } else |
| 4777 | /* Cannot rely on bitmap to complete recovery */ |
| 4778 | conf->fullsync = 1; |
| 4779 | } |
| 4780 | |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4781 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4782 | conf->level = mddev->new_level; |
| 4783 | if (conf->level == 6) |
| 4784 | conf->max_degraded = 2; |
| 4785 | else |
| 4786 | conf->max_degraded = 1; |
| 4787 | conf->algorithm = mddev->new_layout; |
| 4788 | conf->max_nr_stripes = NR_STRIPES; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4789 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4790 | if (conf->reshape_progress != MaxSector) { |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4791 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4792 | conf->prev_algo = mddev->layout; |
| 4793 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4794 | |
| 4795 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
| 4796 | conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
| 4797 | if (grow_stripes(conf, conf->max_nr_stripes)) { |
| 4798 | printk(KERN_ERR |
| 4799 | "raid5: couldn't allocate %dkB for buffers\n", memory); |
| 4800 | goto abort; |
| 4801 | } else |
| 4802 | printk(KERN_INFO "raid5: allocated %dkB for %s\n", |
| 4803 | memory, mdname(mddev)); |
| 4804 | |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 4805 | conf->thread = md_register_thread(raid5d, mddev, NULL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4806 | if (!conf->thread) { |
| 4807 | printk(KERN_ERR |
| 4808 | "raid5: couldn't allocate thread for %s\n", |
| 4809 | mdname(mddev)); |
| 4810 | goto abort; |
| 4811 | } |
| 4812 | |
| 4813 | return conf; |
| 4814 | |
| 4815 | abort: |
| 4816 | if (conf) { |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4817 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4818 | return ERR_PTR(-EIO); |
| 4819 | } else |
| 4820 | return ERR_PTR(-ENOMEM); |
| 4821 | } |
| 4822 | |
| 4823 | static int run(mddev_t *mddev) |
| 4824 | { |
| 4825 | raid5_conf_t *conf; |
Martin K. Petersen | 8f6c2e4 | 2009-07-01 11:13:45 +1000 | [diff] [blame] | 4826 | int working_disks = 0, chunk_size; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4827 | mdk_rdev_t *rdev; |
| 4828 | |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 4829 | if (mddev->recovery_cp != MaxSector) |
| 4830 | printk(KERN_NOTICE "raid5: %s is not clean" |
| 4831 | " -- starting background reconstruction\n", |
| 4832 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4833 | if (mddev->reshape_position != MaxSector) { |
| 4834 | /* Check that we can continue the reshape. |
| 4835 | * Currently only disks can change, it must |
| 4836 | * increase, and we must be past the point where |
| 4837 | * a stripe over-writes itself |
| 4838 | */ |
| 4839 | sector_t here_new, here_old; |
| 4840 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 4841 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4842 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 4843 | if (mddev->new_level != mddev->level) { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4844 | printk(KERN_ERR "raid5: %s: unsupported reshape " |
| 4845 | "required - aborting.\n", |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4846 | mdname(mddev)); |
| 4847 | return -EINVAL; |
| 4848 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4849 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 4850 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4851 | * further up in new geometry must map after here in old |
| 4852 | * geometry. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4853 | */ |
| 4854 | here_new = mddev->reshape_position; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4855 | if (sector_div(here_new, mddev->new_chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4856 | (mddev->raid_disks - max_degraded))) { |
| 4857 | printk(KERN_ERR "raid5: reshape_position not " |
| 4858 | "on a stripe boundary\n"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4859 | return -EINVAL; |
| 4860 | } |
| 4861 | /* here_new is the stripe we will write to */ |
| 4862 | here_old = mddev->reshape_position; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4863 | sector_div(here_old, mddev->chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4864 | (old_disks-max_degraded)); |
| 4865 | /* here_old is the first stripe that we might need to read |
| 4866 | * from */ |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 4867 | if (mddev->delta_disks == 0) { |
| 4868 | /* We cannot be sure it is safe to start an in-place |
| 4869 | * reshape. It is only safe if user-space if monitoring |
| 4870 | * and taking constant backups. |
| 4871 | * mdadm always starts a situation like this in |
| 4872 | * readonly mode so it can take control before |
| 4873 | * allowing any writes. So just check for that. |
| 4874 | */ |
| 4875 | if ((here_new * mddev->new_chunk_sectors != |
| 4876 | here_old * mddev->chunk_sectors) || |
| 4877 | mddev->ro == 0) { |
| 4878 | printk(KERN_ERR "raid5: in-place reshape must be started" |
| 4879 | " in read-only mode - aborting\n"); |
| 4880 | return -EINVAL; |
| 4881 | } |
| 4882 | } else if (mddev->delta_disks < 0 |
| 4883 | ? (here_new * mddev->new_chunk_sectors <= |
| 4884 | here_old * mddev->chunk_sectors) |
| 4885 | : (here_new * mddev->new_chunk_sectors >= |
| 4886 | here_old * mddev->chunk_sectors)) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4887 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4888 | printk(KERN_ERR "raid5: reshape_position too early for " |
| 4889 | "auto-recovery - aborting.\n"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4890 | return -EINVAL; |
| 4891 | } |
| 4892 | printk(KERN_INFO "raid5: reshape will continue\n"); |
| 4893 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4894 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4895 | BUG_ON(mddev->level != mddev->new_level); |
| 4896 | BUG_ON(mddev->layout != mddev->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4897 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4898 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4899 | } |
| 4900 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4901 | if (mddev->private == NULL) |
| 4902 | conf = setup_conf(mddev); |
| 4903 | else |
| 4904 | conf = mddev->private; |
| 4905 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4906 | if (IS_ERR(conf)) |
| 4907 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 4908 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4909 | mddev->thread = conf->thread; |
| 4910 | conf->thread = NULL; |
| 4911 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4913 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4914 | * 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] | 4915 | */ |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4916 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4917 | if (rdev->raid_disk >= 0 && |
| 4918 | test_bit(In_sync, &rdev->flags)) |
| 4919 | working_disks++; |
| 4920 | |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 4921 | mddev->degraded = conf->raid_disks - working_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4922 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4923 | if (mddev->degraded > conf->max_degraded) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4924 | printk(KERN_ERR "raid5: not enough operational devices for %s" |
| 4925 | " (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 4926 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4927 | goto abort; |
| 4928 | } |
| 4929 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4930 | /* device size must be a multiple of chunk size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4931 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4932 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4933 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4934 | if (mddev->degraded > 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4935 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4936 | if (mddev->ok_start_degraded) |
| 4937 | printk(KERN_WARNING |
| 4938 | "raid5: starting dirty degraded array: %s" |
| 4939 | "- data corruption possible.\n", |
| 4940 | mdname(mddev)); |
| 4941 | else { |
| 4942 | printk(KERN_ERR |
| 4943 | "raid5: cannot start dirty degraded array for %s\n", |
| 4944 | mdname(mddev)); |
| 4945 | goto abort; |
| 4946 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | } |
| 4948 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | if (mddev->degraded == 0) |
| 4950 | printk("raid5: raid level %d set %s active with %d out of %d" |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4951 | " devices, algorithm %d\n", conf->level, mdname(mddev), |
| 4952 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 4953 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4954 | else |
| 4955 | printk(KERN_ALERT "raid5: raid level %d set %s active with %d" |
| 4956 | " out of %d devices, algorithm %d\n", conf->level, |
| 4957 | mdname(mddev), mddev->raid_disks - mddev->degraded, |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4958 | mddev->raid_disks, mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4959 | |
| 4960 | print_raid5_conf(conf); |
| 4961 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4962 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4963 | printk("...ok start reshape thread\n"); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4964 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4965 | atomic_set(&conf->reshape_stripes, 0); |
| 4966 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 4967 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 4968 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 4969 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 4970 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 4971 | "reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4972 | } |
| 4973 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4974 | /* read-ahead size must cover two whole stripes, which is |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4975 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | */ |
| 4977 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4978 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 4979 | int stripe = data_disks * |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4980 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4981 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 4982 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 4983 | } |
| 4984 | |
| 4985 | /* Ok, everything is just fine now */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4986 | if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
| 4987 | printk(KERN_WARNING |
| 4988 | "raid5: failed to create sysfs attributes for %s\n", |
| 4989 | mdname(mddev)); |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 4990 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4991 | mddev->queue->queue_lock = &conf->device_lock; |
| 4992 | |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 4993 | mddev->queue->unplug_fn = raid5_unplug_device; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4994 | mddev->queue->backing_dev_info.congested_data = mddev; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4995 | mddev->queue->backing_dev_info.congested_fn = raid5_congested; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4996 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 4997 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 4998 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 4999 | blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); |
Martin K. Petersen | 8f6c2e4 | 2009-07-01 11:13:45 +1000 | [diff] [blame] | 5000 | chunk_size = mddev->chunk_sectors << 9; |
| 5001 | blk_queue_io_min(mddev->queue, chunk_size); |
| 5002 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 5003 | (conf->raid_disks - conf->max_degraded)); |
| 5004 | |
| 5005 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 5006 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 5007 | rdev->data_offset << 9); |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 5008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5009 | return 0; |
| 5010 | abort: |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5011 | md_unregister_thread(mddev->thread); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5012 | mddev->thread = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | if (conf) { |
| 5014 | print_raid5_conf(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5015 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5016 | } |
| 5017 | mddev->private = NULL; |
| 5018 | printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev)); |
| 5019 | return -EIO; |
| 5020 | } |
| 5021 | |
| 5022 | |
| 5023 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5024 | static int stop(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | { |
| 5026 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 5027 | |
| 5028 | md_unregister_thread(mddev->thread); |
| 5029 | mddev->thread = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5030 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 5032 | sysfs_remove_group(&mddev->kobj, &raid5_attrs_group); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5033 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | mddev->private = NULL; |
| 5035 | return 0; |
| 5036 | } |
| 5037 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5038 | #ifdef DEBUG |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5039 | static void print_sh(struct seq_file *seq, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5040 | { |
| 5041 | int i; |
| 5042 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5043 | seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n", |
| 5044 | (unsigned long long)sh->sector, sh->pd_idx, sh->state); |
| 5045 | seq_printf(seq, "sh %llu, count %d.\n", |
| 5046 | (unsigned long long)sh->sector, atomic_read(&sh->count)); |
| 5047 | seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5048 | for (i = 0; i < sh->disks; i++) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5049 | seq_printf(seq, "(cache%d: %p %ld) ", |
| 5050 | i, sh->dev[i].page, sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5051 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5052 | seq_printf(seq, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5053 | } |
| 5054 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5055 | static void printall(struct seq_file *seq, raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5056 | { |
| 5057 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 5058 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5059 | int i; |
| 5060 | |
| 5061 | spin_lock_irq(&conf->device_lock); |
| 5062 | for (i = 0; i < NR_HASH; i++) { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 5063 | hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5064 | if (sh->raid_conf != conf) |
| 5065 | continue; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5066 | print_sh(seq, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | } |
| 5068 | } |
| 5069 | spin_unlock_irq(&conf->device_lock); |
| 5070 | } |
| 5071 | #endif |
| 5072 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5073 | static void status(struct seq_file *seq, mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5074 | { |
| 5075 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 5076 | int i; |
| 5077 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5078 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
| 5079 | mddev->chunk_sectors / 2, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5080 | 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] | 5081 | for (i = 0; i < conf->raid_disks; i++) |
| 5082 | seq_printf (seq, "%s", |
| 5083 | conf->disks[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5084 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | seq_printf (seq, "]"); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5086 | #ifdef DEBUG |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5087 | seq_printf (seq, "\n"); |
| 5088 | printall(seq, conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5089 | #endif |
| 5090 | } |
| 5091 | |
| 5092 | static void print_raid5_conf (raid5_conf_t *conf) |
| 5093 | { |
| 5094 | int i; |
| 5095 | struct disk_info *tmp; |
| 5096 | |
| 5097 | printk("RAID5 conf printout:\n"); |
| 5098 | if (!conf) { |
| 5099 | printk("(conf==NULL)\n"); |
| 5100 | return; |
| 5101 | } |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5102 | printk(" --- rd:%d wd:%d\n", conf->raid_disks, |
| 5103 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | |
| 5105 | for (i = 0; i < conf->raid_disks; i++) { |
| 5106 | char b[BDEVNAME_SIZE]; |
| 5107 | tmp = conf->disks + i; |
| 5108 | if (tmp->rdev) |
| 5109 | printk(" disk %d, o:%d, dev:%s\n", |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5110 | i, !test_bit(Faulty, &tmp->rdev->flags), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5111 | bdevname(tmp->rdev->bdev,b)); |
| 5112 | } |
| 5113 | } |
| 5114 | |
| 5115 | static int raid5_spare_active(mddev_t *mddev) |
| 5116 | { |
| 5117 | int i; |
| 5118 | raid5_conf_t *conf = mddev->private; |
| 5119 | struct disk_info *tmp; |
| 5120 | |
| 5121 | for (i = 0; i < conf->raid_disks; i++) { |
| 5122 | tmp = conf->disks + i; |
| 5123 | if (tmp->rdev |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5124 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5125 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
| 5126 | unsigned long flags; |
| 5127 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5128 | mddev->degraded--; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5129 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | } |
| 5131 | } |
| 5132 | print_raid5_conf(conf); |
| 5133 | return 0; |
| 5134 | } |
| 5135 | |
| 5136 | static int raid5_remove_disk(mddev_t *mddev, int number) |
| 5137 | { |
| 5138 | raid5_conf_t *conf = mddev->private; |
| 5139 | int err = 0; |
| 5140 | mdk_rdev_t *rdev; |
| 5141 | struct disk_info *p = conf->disks + number; |
| 5142 | |
| 5143 | print_raid5_conf(conf); |
| 5144 | rdev = p->rdev; |
| 5145 | if (rdev) { |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5146 | if (number >= conf->raid_disks && |
| 5147 | conf->reshape_progress == MaxSector) |
| 5148 | clear_bit(In_sync, &rdev->flags); |
| 5149 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5150 | if (test_bit(In_sync, &rdev->flags) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5151 | atomic_read(&rdev->nr_pending)) { |
| 5152 | err = -EBUSY; |
| 5153 | goto abort; |
| 5154 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5155 | /* Only remove non-faulty devices if recovery |
| 5156 | * isn't possible. |
| 5157 | */ |
| 5158 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5159 | mddev->degraded <= conf->max_degraded && |
| 5160 | number < conf->raid_disks) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5161 | err = -EBUSY; |
| 5162 | goto abort; |
| 5163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | p->rdev = NULL; |
Paul E. McKenney | fbd568a3e | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 5165 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | if (atomic_read(&rdev->nr_pending)) { |
| 5167 | /* lost the race, try later */ |
| 5168 | err = -EBUSY; |
| 5169 | p->rdev = rdev; |
| 5170 | } |
| 5171 | } |
| 5172 | abort: |
| 5173 | |
| 5174 | print_raid5_conf(conf); |
| 5175 | return err; |
| 5176 | } |
| 5177 | |
| 5178 | static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5179 | { |
| 5180 | raid5_conf_t *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5181 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5182 | int disk; |
| 5183 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5184 | int first = 0; |
| 5185 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5187 | if (mddev->degraded > conf->max_degraded) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5188 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5189 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5190 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5191 | if (rdev->raid_disk >= 0) |
| 5192 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5193 | |
| 5194 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5195 | * find the disk ... but prefer rdev->saved_raid_disk |
| 5196 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5197 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5198 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5199 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5200 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
| 5201 | disk = rdev->saved_raid_disk; |
| 5202 | else |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5203 | disk = first; |
| 5204 | for ( ; disk <= last ; disk++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5205 | if ((p=conf->disks + disk)->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5206 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5207 | rdev->raid_disk = disk; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5208 | err = 0; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5209 | if (rdev->saved_raid_disk != disk) |
| 5210 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 5211 | rcu_assign_pointer(p->rdev, rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5212 | break; |
| 5213 | } |
| 5214 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5215 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | } |
| 5217 | |
| 5218 | static int raid5_resize(mddev_t *mddev, sector_t sectors) |
| 5219 | { |
| 5220 | /* no resync is happening, and there is enough space |
| 5221 | * on all devices, so we can resize. |
| 5222 | * We need to make sure resync covers any new space. |
| 5223 | * If the array is shrinking we should possibly wait until |
| 5224 | * any io in the removed space completes, but it hardly seems |
| 5225 | * worth it. |
| 5226 | */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5227 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5228 | md_set_array_sectors(mddev, raid5_size(mddev, sectors, |
| 5229 | mddev->raid_disks)); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5230 | if (mddev->array_sectors > |
| 5231 | raid5_size(mddev, sectors, mddev->raid_disks)) |
| 5232 | return -EINVAL; |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5233 | set_capacity(mddev->gendisk, mddev->array_sectors); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5234 | mddev->changed = 1; |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5235 | revalidate_disk(mddev->gendisk); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5236 | if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) { |
| 5237 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5238 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5239 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5240 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 5241 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | return 0; |
| 5243 | } |
| 5244 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5245 | static int check_stripe_cache(mddev_t *mddev) |
| 5246 | { |
| 5247 | /* Can only proceed if there are plenty of stripe_heads. |
| 5248 | * We need a minimum of one full stripe,, and for sensible progress |
| 5249 | * it is best to have about 4 times that. |
| 5250 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 5251 | * allow for chunk sizes up to 256K, which is probably OK. |
| 5252 | * If the chunk size is greater, user-space should request more |
| 5253 | * stripe_heads first. |
| 5254 | */ |
| 5255 | raid5_conf_t *conf = mddev->private; |
| 5256 | if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5257 | > conf->max_nr_stripes || |
| 5258 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5259 | > conf->max_nr_stripes) { |
| 5260 | printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n", |
| 5261 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
| 5262 | / STRIPE_SIZE)*4); |
| 5263 | return 0; |
| 5264 | } |
| 5265 | return 1; |
| 5266 | } |
| 5267 | |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5268 | static int check_reshape(mddev_t *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5269 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5270 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5271 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5272 | if (mddev->delta_disks == 0 && |
| 5273 | mddev->new_layout == mddev->layout && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5274 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5275 | return 0; /* nothing to do */ |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5276 | if (mddev->bitmap) |
| 5277 | /* Cannot grow a bitmap yet */ |
| 5278 | return -EBUSY; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5279 | if (mddev->degraded > conf->max_degraded) |
| 5280 | return -EINVAL; |
| 5281 | if (mddev->delta_disks < 0) { |
| 5282 | /* We might be able to shrink, but the devices must |
| 5283 | * be made bigger first. |
| 5284 | * For raid6, 4 is the minimum size. |
| 5285 | * Otherwise 2 is the minimum |
| 5286 | */ |
| 5287 | int min = 2; |
| 5288 | if (mddev->level == 6) |
| 5289 | min = 4; |
| 5290 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 5291 | return -EINVAL; |
| 5292 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5293 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5294 | if (!check_stripe_cache(mddev)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5295 | return -ENOSPC; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5296 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5297 | return resize_stripes(conf, conf->raid_disks + mddev->delta_disks); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5298 | } |
| 5299 | |
| 5300 | static int raid5_start_reshape(mddev_t *mddev) |
| 5301 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5302 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5303 | mdk_rdev_t *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5304 | int spares = 0; |
| 5305 | int added_devices = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5306 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5307 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5308 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5309 | return -EBUSY; |
| 5310 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5311 | if (!check_stripe_cache(mddev)) |
| 5312 | return -ENOSPC; |
| 5313 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5314 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5315 | if (rdev->raid_disk < 0 && |
| 5316 | !test_bit(Faulty, &rdev->flags)) |
| 5317 | spares++; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5318 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5319 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5320 | /* Not enough devices even to make a degraded array |
| 5321 | * of that size |
| 5322 | */ |
| 5323 | return -EINVAL; |
| 5324 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5325 | /* Refuse to reduce size of the array. Any reductions in |
| 5326 | * array size must be through explicit setting of array_size |
| 5327 | * attribute. |
| 5328 | */ |
| 5329 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 5330 | < mddev->array_sectors) { |
| 5331 | printk(KERN_ERR "md: %s: array size must be reduced " |
| 5332 | "before number of disks\n", mdname(mddev)); |
| 5333 | return -EINVAL; |
| 5334 | } |
| 5335 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5336 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5337 | spin_lock_irq(&conf->device_lock); |
| 5338 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5339 | conf->raid_disks += mddev->delta_disks; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5340 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 5341 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5342 | conf->prev_algo = conf->algorithm; |
| 5343 | conf->algorithm = mddev->new_layout; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5344 | if (mddev->delta_disks < 0) |
| 5345 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 5346 | else |
| 5347 | conf->reshape_progress = 0; |
| 5348 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 5349 | conf->generation++; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5350 | spin_unlock_irq(&conf->device_lock); |
| 5351 | |
| 5352 | /* Add some new drives, as many as will fit. |
| 5353 | * We know there are enough to make the newly sized array work. |
| 5354 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5355 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5356 | if (rdev->raid_disk < 0 && |
| 5357 | !test_bit(Faulty, &rdev->flags)) { |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5358 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5359 | char nm[20]; |
| 5360 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5361 | added_devices++; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5362 | rdev->recovery_offset = 0; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5363 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5364 | if (sysfs_create_link(&mddev->kobj, |
| 5365 | &rdev->kobj, nm)) |
| 5366 | printk(KERN_WARNING |
| 5367 | "raid5: failed to create " |
| 5368 | " link %s for %s\n", |
| 5369 | nm, mdname(mddev)); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5370 | } else |
| 5371 | break; |
| 5372 | } |
| 5373 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5374 | if (mddev->delta_disks > 0) { |
| 5375 | spin_lock_irqsave(&conf->device_lock, flags); |
| 5376 | mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) |
| 5377 | - added_devices; |
| 5378 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 5379 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5380 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | e516402 | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 5381 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5382 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5383 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5384 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5385 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5386 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5387 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5388 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5389 | "reshape"); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5390 | if (!mddev->sync_thread) { |
| 5391 | mddev->recovery = 0; |
| 5392 | spin_lock_irq(&conf->device_lock); |
| 5393 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5394 | conf->reshape_progress = MaxSector; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5395 | spin_unlock_irq(&conf->device_lock); |
| 5396 | return -EAGAIN; |
| 5397 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5398 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5399 | md_wakeup_thread(mddev->sync_thread); |
| 5400 | md_new_event(mddev); |
| 5401 | return 0; |
| 5402 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5403 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5404 | /* This is called from the reshape thread and should make any |
| 5405 | * changes needed in 'conf' |
| 5406 | */ |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5407 | static void end_reshape(raid5_conf_t *conf) |
| 5408 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5409 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5410 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5411 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5412 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5413 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5414 | conf->reshape_progress = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5415 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5416 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5417 | |
| 5418 | /* read-ahead size must cover two whole stripes, which is |
| 5419 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 5420 | */ |
| 5421 | { |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5422 | int data_disks = conf->raid_disks - conf->max_degraded; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5423 | int stripe = data_disks * ((conf->chunk_sectors << 9) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5424 | / PAGE_SIZE); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5425 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5426 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 5427 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5428 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5429 | } |
| 5430 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5431 | /* This is called from the raid5d thread with mddev_lock held. |
| 5432 | * It makes config changes to the device. |
| 5433 | */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5434 | static void raid5_finish_reshape(mddev_t *mddev) |
| 5435 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5436 | raid5_conf_t *conf = mddev->private; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5437 | |
| 5438 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5439 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5440 | if (mddev->delta_disks > 0) { |
| 5441 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 5442 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5443 | mddev->changed = 1; |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5444 | revalidate_disk(mddev->gendisk); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5445 | } else { |
| 5446 | int d; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5447 | mddev->degraded = conf->raid_disks; |
| 5448 | for (d = 0; d < conf->raid_disks ; d++) |
| 5449 | if (conf->disks[d].rdev && |
| 5450 | test_bit(In_sync, |
| 5451 | &conf->disks[d].rdev->flags)) |
| 5452 | mddev->degraded--; |
| 5453 | for (d = conf->raid_disks ; |
| 5454 | d < conf->raid_disks - mddev->delta_disks; |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 5455 | d++) { |
| 5456 | mdk_rdev_t *rdev = conf->disks[d].rdev; |
| 5457 | if (rdev && raid5_remove_disk(mddev, d) == 0) { |
| 5458 | char nm[20]; |
| 5459 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 5460 | sysfs_remove_link(&mddev->kobj, nm); |
| 5461 | rdev->raid_disk = -1; |
| 5462 | } |
| 5463 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5464 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5465 | mddev->layout = conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5466 | mddev->chunk_sectors = conf->chunk_sectors; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5467 | mddev->reshape_position = MaxSector; |
| 5468 | mddev->delta_disks = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5469 | } |
| 5470 | } |
| 5471 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5472 | static void raid5_quiesce(mddev_t *mddev, int state) |
| 5473 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5474 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5475 | |
| 5476 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5477 | case 2: /* resume for a suspend */ |
| 5478 | wake_up(&conf->wait_for_overlap); |
| 5479 | break; |
| 5480 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5481 | case 1: /* stop all writes */ |
| 5482 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5483 | /* '2' tells resync/reshape to pause so that all |
| 5484 | * active stripes can drain |
| 5485 | */ |
| 5486 | conf->quiesce = 2; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5487 | wait_event_lock_irq(conf->wait_for_stripe, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5488 | atomic_read(&conf->active_stripes) == 0 && |
| 5489 | atomic_read(&conf->active_aligned_reads) == 0, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5490 | conf->device_lock, /* nothing */); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5491 | conf->quiesce = 1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5492 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5493 | /* allow reshape to continue */ |
| 5494 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5495 | break; |
| 5496 | |
| 5497 | case 0: /* re-enable writes */ |
| 5498 | spin_lock_irq(&conf->device_lock); |
| 5499 | conf->quiesce = 0; |
| 5500 | wake_up(&conf->wait_for_stripe); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5501 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5502 | spin_unlock_irq(&conf->device_lock); |
| 5503 | break; |
| 5504 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5505 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5506 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5507 | |
| 5508 | static void *raid5_takeover_raid1(mddev_t *mddev) |
| 5509 | { |
| 5510 | int chunksect; |
| 5511 | |
| 5512 | if (mddev->raid_disks != 2 || |
| 5513 | mddev->degraded > 1) |
| 5514 | return ERR_PTR(-EINVAL); |
| 5515 | |
| 5516 | /* Should check if there are write-behind devices? */ |
| 5517 | |
| 5518 | chunksect = 64*2; /* 64K by default */ |
| 5519 | |
| 5520 | /* The array must be an exact multiple of chunksize */ |
| 5521 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 5522 | chunksect >>= 1; |
| 5523 | |
| 5524 | if ((chunksect<<9) < STRIPE_SIZE) |
| 5525 | /* array size does not allow a suitable chunk size */ |
| 5526 | return ERR_PTR(-EINVAL); |
| 5527 | |
| 5528 | mddev->new_level = 5; |
| 5529 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5530 | mddev->new_chunk_sectors = chunksect; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5531 | |
| 5532 | return setup_conf(mddev); |
| 5533 | } |
| 5534 | |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 5535 | static void *raid5_takeover_raid6(mddev_t *mddev) |
| 5536 | { |
| 5537 | int new_layout; |
| 5538 | |
| 5539 | switch (mddev->layout) { |
| 5540 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 5541 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 5542 | break; |
| 5543 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 5544 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 5545 | break; |
| 5546 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 5547 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 5548 | break; |
| 5549 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 5550 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 5551 | break; |
| 5552 | case ALGORITHM_PARITY_0_6: |
| 5553 | new_layout = ALGORITHM_PARITY_0; |
| 5554 | break; |
| 5555 | case ALGORITHM_PARITY_N: |
| 5556 | new_layout = ALGORITHM_PARITY_N; |
| 5557 | break; |
| 5558 | default: |
| 5559 | return ERR_PTR(-EINVAL); |
| 5560 | } |
| 5561 | mddev->new_level = 5; |
| 5562 | mddev->new_layout = new_layout; |
| 5563 | mddev->delta_disks = -1; |
| 5564 | mddev->raid_disks -= 1; |
| 5565 | return setup_conf(mddev); |
| 5566 | } |
| 5567 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5568 | |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5569 | static int raid5_check_reshape(mddev_t *mddev) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5570 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5571 | /* For a 2-drive array, the layout and chunk size can be changed |
| 5572 | * immediately as not restriping is needed. |
| 5573 | * For larger arrays we record the new value - after validation |
| 5574 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5575 | */ |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 5576 | raid5_conf_t *conf = mddev->private; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5577 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5578 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5579 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5580 | return -EINVAL; |
| 5581 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 5582 | if (!is_power_of_2(new_chunk)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5583 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5584 | if (new_chunk < (PAGE_SIZE>>9)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5585 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5586 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5587 | /* not factor of array size */ |
| 5588 | return -EINVAL; |
| 5589 | } |
| 5590 | |
| 5591 | /* They look valid */ |
| 5592 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5593 | if (mddev->raid_disks == 2) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5594 | /* can make the change immediately */ |
| 5595 | if (mddev->new_layout >= 0) { |
| 5596 | conf->algorithm = mddev->new_layout; |
| 5597 | mddev->layout = mddev->new_layout; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5598 | } |
| 5599 | if (new_chunk > 0) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5600 | conf->chunk_sectors = new_chunk ; |
| 5601 | mddev->chunk_sectors = new_chunk; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5602 | } |
| 5603 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 5604 | md_wakeup_thread(mddev->thread); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5605 | } |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5606 | return check_reshape(mddev); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5607 | } |
| 5608 | |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5609 | static int raid6_check_reshape(mddev_t *mddev) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5610 | { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5611 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5612 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5613 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5614 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5615 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 5616 | if (!is_power_of_2(new_chunk)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5617 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5618 | if (new_chunk < (PAGE_SIZE >> 9)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5619 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5620 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5621 | /* not factor of array size */ |
| 5622 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5623 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5624 | |
| 5625 | /* They look valid */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5626 | return check_reshape(mddev); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5627 | } |
| 5628 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5629 | static void *raid5_takeover(mddev_t *mddev) |
| 5630 | { |
| 5631 | /* raid5 can take over: |
| 5632 | * raid0 - if all devices are the same - make it a raid4 layout |
| 5633 | * raid1 - if there are two drives. We need to know the chunk size |
| 5634 | * raid4 - trivial - just use a raid4 layout. |
| 5635 | * raid6 - Providing it is a *_6 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5636 | */ |
| 5637 | |
| 5638 | if (mddev->level == 1) |
| 5639 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 5640 | if (mddev->level == 4) { |
| 5641 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 5642 | mddev->new_level = 5; |
| 5643 | return setup_conf(mddev); |
| 5644 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 5645 | if (mddev->level == 6) |
| 5646 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5647 | |
| 5648 | return ERR_PTR(-EINVAL); |
| 5649 | } |
| 5650 | |
| 5651 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5652 | static struct mdk_personality raid5_personality; |
| 5653 | |
| 5654 | static void *raid6_takeover(mddev_t *mddev) |
| 5655 | { |
| 5656 | /* Currently can only take over a raid5. We map the |
| 5657 | * personality to an equivalent raid6 personality |
| 5658 | * with the Q block at the end. |
| 5659 | */ |
| 5660 | int new_layout; |
| 5661 | |
| 5662 | if (mddev->pers != &raid5_personality) |
| 5663 | return ERR_PTR(-EINVAL); |
| 5664 | if (mddev->degraded > 1) |
| 5665 | return ERR_PTR(-EINVAL); |
| 5666 | if (mddev->raid_disks > 253) |
| 5667 | return ERR_PTR(-EINVAL); |
| 5668 | if (mddev->raid_disks < 3) |
| 5669 | return ERR_PTR(-EINVAL); |
| 5670 | |
| 5671 | switch (mddev->layout) { |
| 5672 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 5673 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 5674 | break; |
| 5675 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 5676 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 5677 | break; |
| 5678 | case ALGORITHM_LEFT_SYMMETRIC: |
| 5679 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 5680 | break; |
| 5681 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 5682 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 5683 | break; |
| 5684 | case ALGORITHM_PARITY_0: |
| 5685 | new_layout = ALGORITHM_PARITY_0_6; |
| 5686 | break; |
| 5687 | case ALGORITHM_PARITY_N: |
| 5688 | new_layout = ALGORITHM_PARITY_N; |
| 5689 | break; |
| 5690 | default: |
| 5691 | return ERR_PTR(-EINVAL); |
| 5692 | } |
| 5693 | mddev->new_level = 6; |
| 5694 | mddev->new_layout = new_layout; |
| 5695 | mddev->delta_disks = 1; |
| 5696 | mddev->raid_disks += 1; |
| 5697 | return setup_conf(mddev); |
| 5698 | } |
| 5699 | |
| 5700 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5701 | static struct mdk_personality raid6_personality = |
| 5702 | { |
| 5703 | .name = "raid6", |
| 5704 | .level = 6, |
| 5705 | .owner = THIS_MODULE, |
| 5706 | .make_request = make_request, |
| 5707 | .run = run, |
| 5708 | .stop = stop, |
| 5709 | .status = status, |
| 5710 | .error_handler = error, |
| 5711 | .hot_add_disk = raid5_add_disk, |
| 5712 | .hot_remove_disk= raid5_remove_disk, |
| 5713 | .spare_active = raid5_spare_active, |
| 5714 | .sync_request = sync_request, |
| 5715 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5716 | .size = raid5_size, |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5717 | .check_reshape = raid6_check_reshape, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5718 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5719 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5720 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5721 | .takeover = raid6_takeover, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5722 | }; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5723 | static struct mdk_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | { |
| 5725 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5726 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5727 | .owner = THIS_MODULE, |
| 5728 | .make_request = make_request, |
| 5729 | .run = run, |
| 5730 | .stop = stop, |
| 5731 | .status = status, |
| 5732 | .error_handler = error, |
| 5733 | .hot_add_disk = raid5_add_disk, |
| 5734 | .hot_remove_disk= raid5_remove_disk, |
| 5735 | .spare_active = raid5_spare_active, |
| 5736 | .sync_request = sync_request, |
| 5737 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5738 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5739 | .check_reshape = raid5_check_reshape, |
| 5740 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5741 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5742 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5743 | .takeover = raid5_takeover, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5744 | }; |
| 5745 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5746 | static struct mdk_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5747 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5748 | .name = "raid4", |
| 5749 | .level = 4, |
| 5750 | .owner = THIS_MODULE, |
| 5751 | .make_request = make_request, |
| 5752 | .run = run, |
| 5753 | .stop = stop, |
| 5754 | .status = status, |
| 5755 | .error_handler = error, |
| 5756 | .hot_add_disk = raid5_add_disk, |
| 5757 | .hot_remove_disk= raid5_remove_disk, |
| 5758 | .spare_active = raid5_spare_active, |
| 5759 | .sync_request = sync_request, |
| 5760 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5761 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 5762 | .check_reshape = raid5_check_reshape, |
| 5763 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5764 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5765 | .quiesce = raid5_quiesce, |
| 5766 | }; |
| 5767 | |
| 5768 | static int __init raid5_init(void) |
| 5769 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5770 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5771 | register_md_personality(&raid5_personality); |
| 5772 | register_md_personality(&raid4_personality); |
| 5773 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | } |
| 5775 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5776 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5777 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5778 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5779 | unregister_md_personality(&raid5_personality); |
| 5780 | unregister_md_personality(&raid4_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5781 | } |
| 5782 | |
| 5783 | module_init(raid5_init); |
| 5784 | module_exit(raid5_exit); |
| 5785 | MODULE_LICENSE("GPL"); |
| 5786 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5787 | MODULE_ALIAS("md-raid5"); |
| 5788 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5789 | MODULE_ALIAS("md-level-5"); |
| 5790 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5791 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 5792 | MODULE_ALIAS("md-raid6"); |
| 5793 | MODULE_ALIAS("md-level-6"); |
| 5794 | |
| 5795 | /* This used to be two separate modules, they were: */ |
| 5796 | MODULE_ALIAS("raid5"); |
| 5797 | MODULE_ALIAS("raid6"); |