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> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 50 | #include <linux/seq_file.h> |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 51 | #include <linux/cpu.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 52 | #include "md.h" |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 53 | #include "raid5.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 54 | #include "bitmap.h" |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | /* |
| 57 | * Stripe cache |
| 58 | */ |
| 59 | |
| 60 | #define NR_STRIPES 256 |
| 61 | #define STRIPE_SIZE PAGE_SIZE |
| 62 | #define STRIPE_SHIFT (PAGE_SHIFT - 9) |
| 63 | #define STRIPE_SECTORS (STRIPE_SIZE>>9) |
| 64 | #define IO_THRESHOLD 1 |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 65 | #define BYPASS_THRESHOLD 1 |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 66 | #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #define HASH_MASK (NR_HASH - 1) |
| 68 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 69 | #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] | 70 | |
| 71 | /* bio's attached to a stripe+device for I/O are linked together in bi_sector |
| 72 | * order without overlap. There may be several bio's per stripe+device, and |
| 73 | * a bio could span several devices. |
| 74 | * When walking this list for a particular stripe+device, we must never proceed |
| 75 | * beyond a bio that extends past this device, as the next bio might no longer |
| 76 | * be valid. |
| 77 | * This macro is used to determine the 'next' bio in the list, given the sector |
| 78 | * of the current stripe+device |
| 79 | */ |
| 80 | #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL) |
| 81 | /* |
| 82 | * The following can be used to debug the driver |
| 83 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | #define RAID5_PARANOIA 1 |
| 85 | #if RAID5_PARANOIA && defined(CONFIG_SMP) |
| 86 | # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock) |
| 87 | #else |
| 88 | # define CHECK_DEVLOCK() |
| 89 | #endif |
| 90 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 91 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | #define inline |
| 93 | #define __inline__ |
| 94 | #endif |
| 95 | |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 96 | #define printk_rl(args...) ((void) (printk_ratelimit() && printk(args))) |
| 97 | |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 98 | /* |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 99 | * We maintain a biased count of active stripes in the bottom 16 bits of |
| 100 | * bi_phys_segments, and a count of processed stripes in the upper 16 bits |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 101 | */ |
| 102 | static inline int raid5_bi_phys_segments(struct bio *bio) |
| 103 | { |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 104 | return bio->bi_phys_segments & 0xffff; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static inline int raid5_bi_hw_segments(struct bio *bio) |
| 108 | { |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 109 | return (bio->bi_phys_segments >> 16) & 0xffff; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static inline int raid5_dec_bi_phys_segments(struct bio *bio) |
| 113 | { |
| 114 | --bio->bi_phys_segments; |
| 115 | return raid5_bi_phys_segments(bio); |
| 116 | } |
| 117 | |
| 118 | static inline int raid5_dec_bi_hw_segments(struct bio *bio) |
| 119 | { |
| 120 | unsigned short val = raid5_bi_hw_segments(bio); |
| 121 | |
| 122 | --val; |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 123 | bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 124 | return val; |
| 125 | } |
| 126 | |
| 127 | static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt) |
| 128 | { |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 129 | bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 130 | } |
| 131 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 132 | /* Find first data disk in a raid6 stripe */ |
| 133 | static inline int raid6_d0(struct stripe_head *sh) |
| 134 | { |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 135 | if (sh->ddf_layout) |
| 136 | /* ddf always start from first device */ |
| 137 | return 0; |
| 138 | /* md starts just after Q block */ |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 139 | if (sh->qd_idx == sh->disks - 1) |
| 140 | return 0; |
| 141 | else |
| 142 | return sh->qd_idx + 1; |
| 143 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 144 | static inline int raid6_next_disk(int disk, int raid_disks) |
| 145 | { |
| 146 | disk++; |
| 147 | return (disk < raid_disks) ? disk : 0; |
| 148 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 149 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 150 | /* When walking through the disks in a raid5, starting at raid6_d0, |
| 151 | * We need to map each disk to a 'slot', where the data disks are slot |
| 152 | * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk |
| 153 | * is raid_disks-1. This help does that mapping. |
| 154 | */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 155 | static int raid6_idx_to_slot(int idx, struct stripe_head *sh, |
| 156 | int *count, int syndrome_disks) |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 157 | { |
| 158 | int slot; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 159 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 160 | if (idx == sh->pd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 161 | return syndrome_disks; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 162 | if (idx == sh->qd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 163 | return syndrome_disks + 1; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 164 | slot = (*count)++; |
| 165 | return slot; |
| 166 | } |
| 167 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 168 | static void return_io(struct bio *return_bi) |
| 169 | { |
| 170 | struct bio *bi = return_bi; |
| 171 | while (bi) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 172 | |
| 173 | return_bi = bi->bi_next; |
| 174 | bi->bi_next = NULL; |
| 175 | bi->bi_size = 0; |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 176 | bio_endio(bi, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 177 | bi = return_bi; |
| 178 | } |
| 179 | } |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | static void print_raid5_conf (raid5_conf_t *conf); |
| 182 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 183 | static int stripe_operations_active(struct stripe_head *sh) |
| 184 | { |
| 185 | return sh->check_state || sh->reconstruct_state || |
| 186 | test_bit(STRIPE_BIOFILL_RUN, &sh->state) || |
| 187 | test_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 188 | } |
| 189 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 190 | static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
| 192 | if (atomic_dec_and_test(&sh->count)) { |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 193 | BUG_ON(!list_empty(&sh->lru)); |
| 194 | BUG_ON(atomic_read(&conf->active_stripes)==0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
NeilBrown | 7c785b7 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 196 | if (test_bit(STRIPE_DELAYED, &sh->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | list_add_tail(&sh->lru, &conf->delayed_list); |
NeilBrown | 7c785b7 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 198 | blk_plug_device(conf->mddev->queue); |
| 199 | } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 200 | sh->bm_seq - conf->seq_write > 0) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 201 | list_add_tail(&sh->lru, &conf->bitmap_list); |
NeilBrown | 7c785b7 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 202 | blk_plug_device(conf->mddev->queue); |
| 203 | } else { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 204 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | list_add_tail(&sh->lru, &conf->handle_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 206 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | md_wakeup_thread(conf->mddev->thread); |
| 208 | } else { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 209 | BUG_ON(stripe_operations_active(sh)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 211 | atomic_dec(&conf->preread_active_stripes); |
| 212 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) |
| 213 | md_wakeup_thread(conf->mddev->thread); |
| 214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | atomic_dec(&conf->active_stripes); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 216 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
| 217 | list_add_tail(&sh->lru, &conf->inactive_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | wake_up(&conf->wait_for_stripe); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 219 | if (conf->retry_read_aligned) |
| 220 | md_wakeup_thread(conf->mddev->thread); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | } |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | static void release_stripe(struct stripe_head *sh) |
| 227 | { |
| 228 | raid5_conf_t *conf = sh->raid_conf; |
| 229 | unsigned long flags; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | spin_lock_irqsave(&conf->device_lock, flags); |
| 232 | __release_stripe(conf, sh); |
| 233 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 234 | } |
| 235 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 236 | static inline void remove_hash(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 238 | pr_debug("remove_hash(), stripe %llu\n", |
| 239 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 241 | hlist_del_init(&sh->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 244 | 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] | 245 | { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 246 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 248 | pr_debug("insert_hash(), stripe %llu\n", |
| 249 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | CHECK_DEVLOCK(); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 252 | hlist_add_head(&sh->hash, hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | |
| 256 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
| 257 | static struct stripe_head *get_free_stripe(raid5_conf_t *conf) |
| 258 | { |
| 259 | struct stripe_head *sh = NULL; |
| 260 | struct list_head *first; |
| 261 | |
| 262 | CHECK_DEVLOCK(); |
| 263 | if (list_empty(&conf->inactive_list)) |
| 264 | goto out; |
| 265 | first = conf->inactive_list.next; |
| 266 | sh = list_entry(first, struct stripe_head, lru); |
| 267 | list_del_init(first); |
| 268 | remove_hash(sh); |
| 269 | atomic_inc(&conf->active_stripes); |
| 270 | out: |
| 271 | return sh; |
| 272 | } |
| 273 | |
| 274 | static void shrink_buffers(struct stripe_head *sh, int num) |
| 275 | { |
| 276 | struct page *p; |
| 277 | int i; |
| 278 | |
| 279 | for (i=0; i<num ; i++) { |
| 280 | p = sh->dev[i].page; |
| 281 | if (!p) |
| 282 | continue; |
| 283 | sh->dev[i].page = NULL; |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 284 | put_page(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | static int grow_buffers(struct stripe_head *sh, int num) |
| 289 | { |
| 290 | int i; |
| 291 | |
| 292 | for (i=0; i<num; i++) { |
| 293 | struct page *page; |
| 294 | |
| 295 | if (!(page = alloc_page(GFP_KERNEL))) { |
| 296 | return 1; |
| 297 | } |
| 298 | sh->dev[i].page = page; |
| 299 | } |
| 300 | return 0; |
| 301 | } |
| 302 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 303 | static void raid5_build_block(struct stripe_head *sh, int i, int previous); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 304 | static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous, |
| 305 | struct stripe_head *sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 307 | static void init_stripe(struct stripe_head *sh, sector_t sector, int previous) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { |
| 309 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 310 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 312 | BUG_ON(atomic_read(&sh->count) != 0); |
| 313 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 314 | BUG_ON(stripe_operations_active(sh)); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | CHECK_DEVLOCK(); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 317 | pr_debug("init_stripe called, stripe %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | (unsigned long long)sh->sector); |
| 319 | |
| 320 | remove_hash(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 321 | |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 322 | sh->generation = conf->generation - previous; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 323 | sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | sh->sector = sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 325 | stripe_set_idx(sector, conf, previous, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | sh->state = 0; |
| 327 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 328 | |
| 329 | for (i = sh->disks; i--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | struct r5dev *dev = &sh->dev[i]; |
| 331 | |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 332 | if (dev->toread || dev->read || dev->towrite || dev->written || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | test_bit(R5_LOCKED, &dev->flags)) { |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 334 | 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] | 335 | (unsigned long long)sh->sector, i, dev->toread, |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 336 | dev->read, dev->towrite, dev->written, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | test_bit(R5_LOCKED, &dev->flags)); |
| 338 | BUG(); |
| 339 | } |
| 340 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 341 | raid5_build_block(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
| 343 | insert_hash(conf, sh); |
| 344 | } |
| 345 | |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 346 | static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, |
| 347 | short generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { |
| 349 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 350 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
| 352 | CHECK_DEVLOCK(); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 353 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 354 | hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash) |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 355 | if (sh->sector == sector && sh->generation == generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | return sh; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 357 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | return NULL; |
| 359 | } |
| 360 | |
| 361 | static void unplug_slaves(mddev_t *mddev); |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 362 | static void raid5_unplug_device(struct request_queue *q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 364 | static struct stripe_head * |
| 365 | get_active_stripe(raid5_conf_t *conf, sector_t sector, |
| 366 | int previous, int noblock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { |
| 368 | struct stripe_head *sh; |
| 369 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 370 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
| 372 | spin_lock_irq(&conf->device_lock); |
| 373 | |
| 374 | do { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 375 | wait_event_lock_irq(conf->wait_for_stripe, |
| 376 | conf->quiesce == 0, |
| 377 | conf->device_lock, /* nothing */); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 378 | sh = __find_stripe(conf, sector, conf->generation - previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | if (!sh) { |
| 380 | if (!conf->inactive_blocked) |
| 381 | sh = get_free_stripe(conf); |
| 382 | if (noblock && sh == NULL) |
| 383 | break; |
| 384 | if (!sh) { |
| 385 | conf->inactive_blocked = 1; |
| 386 | wait_event_lock_irq(conf->wait_for_stripe, |
| 387 | !list_empty(&conf->inactive_list) && |
NeilBrown | 5036805 | 2005-12-12 02:39:17 -0800 | [diff] [blame] | 388 | (atomic_read(&conf->active_stripes) |
| 389 | < (conf->max_nr_stripes *3/4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | || !conf->inactive_blocked), |
| 391 | conf->device_lock, |
NeilBrown | f437078 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 392 | raid5_unplug_device(conf->mddev->queue) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | ); |
| 394 | conf->inactive_blocked = 0; |
| 395 | } else |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 396 | init_stripe(sh, sector, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } else { |
| 398 | if (atomic_read(&sh->count)) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 399 | BUG_ON(!list_empty(&sh->lru) |
| 400 | && !test_bit(STRIPE_EXPANDING, &sh->state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } else { |
| 402 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 403 | atomic_inc(&conf->active_stripes); |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 404 | if (list_empty(&sh->lru) && |
| 405 | !test_bit(STRIPE_EXPANDING, &sh->state)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 406 | BUG(); |
| 407 | list_del_init(&sh->lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | } while (sh == NULL); |
| 411 | |
| 412 | if (sh) |
| 413 | atomic_inc(&sh->count); |
| 414 | |
| 415 | spin_unlock_irq(&conf->device_lock); |
| 416 | return sh; |
| 417 | } |
| 418 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 419 | static void |
| 420 | raid5_end_read_request(struct bio *bi, int error); |
| 421 | static void |
| 422 | raid5_end_write_request(struct bio *bi, int error); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 423 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 424 | 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] | 425 | { |
| 426 | raid5_conf_t *conf = sh->raid_conf; |
| 427 | int i, disks = sh->disks; |
| 428 | |
| 429 | might_sleep(); |
| 430 | |
| 431 | for (i = disks; i--; ) { |
| 432 | int rw; |
| 433 | struct bio *bi; |
| 434 | mdk_rdev_t *rdev; |
| 435 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) |
| 436 | rw = WRITE; |
| 437 | else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
| 438 | rw = READ; |
| 439 | else |
| 440 | continue; |
| 441 | |
| 442 | bi = &sh->dev[i].req; |
| 443 | |
| 444 | bi->bi_rw = rw; |
| 445 | if (rw == WRITE) |
| 446 | bi->bi_end_io = raid5_end_write_request; |
| 447 | else |
| 448 | bi->bi_end_io = raid5_end_read_request; |
| 449 | |
| 450 | rcu_read_lock(); |
| 451 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 452 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 453 | rdev = NULL; |
| 454 | if (rdev) |
| 455 | atomic_inc(&rdev->nr_pending); |
| 456 | rcu_read_unlock(); |
| 457 | |
| 458 | if (rdev) { |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 459 | if (s->syncing || s->expanding || s->expanded) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 460 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 461 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 462 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 463 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 464 | bi->bi_bdev = rdev->bdev; |
| 465 | pr_debug("%s: for %llu schedule op %ld on disc %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 466 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 467 | bi->bi_rw, i); |
| 468 | atomic_inc(&sh->count); |
| 469 | bi->bi_sector = sh->sector + rdev->data_offset; |
| 470 | bi->bi_flags = 1 << BIO_UPTODATE; |
| 471 | bi->bi_vcnt = 1; |
| 472 | bi->bi_max_vecs = 1; |
| 473 | bi->bi_idx = 0; |
| 474 | bi->bi_io_vec = &sh->dev[i].vec; |
| 475 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 476 | bi->bi_io_vec[0].bv_offset = 0; |
| 477 | bi->bi_size = STRIPE_SIZE; |
| 478 | bi->bi_next = NULL; |
| 479 | if (rw == WRITE && |
| 480 | test_bit(R5_ReWrite, &sh->dev[i].flags)) |
| 481 | atomic_add(STRIPE_SECTORS, |
| 482 | &rdev->corrected_errors); |
| 483 | generic_make_request(bi); |
| 484 | } else { |
| 485 | if (rw == WRITE) |
| 486 | set_bit(STRIPE_DEGRADED, &sh->state); |
| 487 | pr_debug("skip op %ld on disc %d for sector %llu\n", |
| 488 | bi->bi_rw, i, (unsigned long long)sh->sector); |
| 489 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 490 | set_bit(STRIPE_HANDLE, &sh->state); |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | static struct dma_async_tx_descriptor * |
| 496 | async_copy_data(int frombio, struct bio *bio, struct page *page, |
| 497 | sector_t sector, struct dma_async_tx_descriptor *tx) |
| 498 | { |
| 499 | struct bio_vec *bvl; |
| 500 | struct page *bio_page; |
| 501 | int i; |
| 502 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 503 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 504 | |
| 505 | if (bio->bi_sector >= sector) |
| 506 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 507 | else |
| 508 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 509 | |
| 510 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 511 | bio_for_each_segment(bvl, bio, i) { |
| 512 | int len = bio_iovec_idx(bio, i)->bv_len; |
| 513 | int clen; |
| 514 | int b_offset = 0; |
| 515 | |
| 516 | if (page_offset < 0) { |
| 517 | b_offset = -page_offset; |
| 518 | page_offset += b_offset; |
| 519 | len -= b_offset; |
| 520 | } |
| 521 | |
| 522 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 523 | clen = STRIPE_SIZE - page_offset; |
| 524 | else |
| 525 | clen = len; |
| 526 | |
| 527 | if (clen > 0) { |
| 528 | b_offset += bio_iovec_idx(bio, i)->bv_offset; |
| 529 | bio_page = bio_iovec_idx(bio, i)->bv_page; |
| 530 | if (frombio) |
| 531 | tx = async_memcpy(page, bio_page, page_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 532 | b_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 533 | else |
| 534 | tx = async_memcpy(bio_page, page, b_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 535 | page_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 536 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 537 | /* chain the operations */ |
| 538 | submit.depend_tx = tx; |
| 539 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 540 | if (clen < len) /* hit end of page */ |
| 541 | break; |
| 542 | page_offset += len; |
| 543 | } |
| 544 | |
| 545 | return tx; |
| 546 | } |
| 547 | |
| 548 | static void ops_complete_biofill(void *stripe_head_ref) |
| 549 | { |
| 550 | struct stripe_head *sh = stripe_head_ref; |
| 551 | struct bio *return_bi = NULL; |
| 552 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 553 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 554 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 555 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 556 | (unsigned long long)sh->sector); |
| 557 | |
| 558 | /* clear completed biofills */ |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 559 | spin_lock_irq(&conf->device_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 560 | for (i = sh->disks; i--; ) { |
| 561 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 562 | |
| 563 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 564 | /* and check if we need to reply to a read request, |
| 565 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 566 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 567 | */ |
| 568 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 569 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 570 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 571 | BUG_ON(!dev->read); |
| 572 | rbi = dev->read; |
| 573 | dev->read = NULL; |
| 574 | while (rbi && rbi->bi_sector < |
| 575 | dev->sector + STRIPE_SECTORS) { |
| 576 | rbi2 = r5_next_bio(rbi, dev->sector); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 577 | if (!raid5_dec_bi_phys_segments(rbi)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 578 | rbi->bi_next = return_bi; |
| 579 | return_bi = rbi; |
| 580 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 581 | rbi = rbi2; |
| 582 | } |
| 583 | } |
| 584 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 585 | spin_unlock_irq(&conf->device_lock); |
| 586 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 587 | |
| 588 | return_io(return_bi); |
| 589 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 590 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 591 | release_stripe(sh); |
| 592 | } |
| 593 | |
| 594 | static void ops_run_biofill(struct stripe_head *sh) |
| 595 | { |
| 596 | struct dma_async_tx_descriptor *tx = NULL; |
| 597 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 598 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 599 | int i; |
| 600 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 601 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 602 | (unsigned long long)sh->sector); |
| 603 | |
| 604 | for (i = sh->disks; i--; ) { |
| 605 | struct r5dev *dev = &sh->dev[i]; |
| 606 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 607 | struct bio *rbi; |
| 608 | spin_lock_irq(&conf->device_lock); |
| 609 | dev->read = rbi = dev->toread; |
| 610 | dev->toread = NULL; |
| 611 | spin_unlock_irq(&conf->device_lock); |
| 612 | while (rbi && rbi->bi_sector < |
| 613 | dev->sector + STRIPE_SECTORS) { |
| 614 | tx = async_copy_data(0, rbi, dev->page, |
| 615 | dev->sector, tx); |
| 616 | rbi = r5_next_bio(rbi, dev->sector); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 622 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 623 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 626 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 627 | { |
| 628 | struct r5dev *tgt; |
| 629 | |
| 630 | if (target < 0) |
| 631 | return; |
| 632 | |
| 633 | tgt = &sh->dev[target]; |
| 634 | set_bit(R5_UPTODATE, &tgt->flags); |
| 635 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 636 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 637 | } |
| 638 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 639 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 640 | { |
| 641 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 642 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 643 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 644 | (unsigned long long)sh->sector); |
| 645 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 646 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 647 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 648 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 649 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 650 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 651 | if (sh->check_state == check_state_compute_run) |
| 652 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 653 | set_bit(STRIPE_HANDLE, &sh->state); |
| 654 | release_stripe(sh); |
| 655 | } |
| 656 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 657 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 658 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
| 659 | struct raid5_percpu *percpu) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 660 | { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 661 | return percpu->scribble + sizeof(struct page *) * (sh->disks + 2); |
| 662 | } |
| 663 | |
| 664 | static struct dma_async_tx_descriptor * |
| 665 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 666 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 667 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 668 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 669 | int target = sh->ops.target; |
| 670 | struct r5dev *tgt = &sh->dev[target]; |
| 671 | struct page *xor_dest = tgt->page; |
| 672 | int count = 0; |
| 673 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 674 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 675 | int i; |
| 676 | |
| 677 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 678 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 679 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 680 | |
| 681 | for (i = disks; i--; ) |
| 682 | if (i != target) |
| 683 | xor_srcs[count++] = sh->dev[i].page; |
| 684 | |
| 685 | atomic_inc(&sh->count); |
| 686 | |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 687 | init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 688 | ops_complete_compute, sh, to_addr_conv(sh, percpu)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 689 | if (unlikely(count == 1)) |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 690 | 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] | 691 | else |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 692 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 693 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 694 | return tx; |
| 695 | } |
| 696 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 697 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 698 | * @srcs - (struct page *) array of size sh->disks |
| 699 | * @sh - stripe_head to parse |
| 700 | * |
| 701 | * Populates srcs in proper layout order for the stripe and returns the |
| 702 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 703 | * destination buffer is recorded in srcs[count] and the Q destination |
| 704 | * is recorded in srcs[count+1]]. |
| 705 | */ |
| 706 | static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh) |
| 707 | { |
| 708 | int disks = sh->disks; |
| 709 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 710 | int d0_idx = raid6_d0(sh); |
| 711 | int count; |
| 712 | int i; |
| 713 | |
| 714 | for (i = 0; i < disks; i++) |
| 715 | srcs[i] = (void *)raid6_empty_zero_page; |
| 716 | |
| 717 | count = 0; |
| 718 | i = d0_idx; |
| 719 | do { |
| 720 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 721 | |
| 722 | srcs[slot] = sh->dev[i].page; |
| 723 | i = raid6_next_disk(i, disks); |
| 724 | } while (i != d0_idx); |
| 725 | BUG_ON(count != syndrome_disks); |
| 726 | |
| 727 | return count; |
| 728 | } |
| 729 | |
| 730 | static struct dma_async_tx_descriptor * |
| 731 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 732 | { |
| 733 | int disks = sh->disks; |
| 734 | struct page **blocks = percpu->scribble; |
| 735 | int target; |
| 736 | int qd_idx = sh->qd_idx; |
| 737 | struct dma_async_tx_descriptor *tx; |
| 738 | struct async_submit_ctl submit; |
| 739 | struct r5dev *tgt; |
| 740 | struct page *dest; |
| 741 | int i; |
| 742 | int count; |
| 743 | |
| 744 | if (sh->ops.target < 0) |
| 745 | target = sh->ops.target2; |
| 746 | else if (sh->ops.target2 < 0) |
| 747 | target = sh->ops.target; |
| 748 | else |
| 749 | /* we should only have one valid target */ |
| 750 | BUG(); |
| 751 | BUG_ON(target < 0); |
| 752 | pr_debug("%s: stripe %llu block: %d\n", |
| 753 | __func__, (unsigned long long)sh->sector, target); |
| 754 | |
| 755 | tgt = &sh->dev[target]; |
| 756 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 757 | dest = tgt->page; |
| 758 | |
| 759 | atomic_inc(&sh->count); |
| 760 | |
| 761 | if (target == qd_idx) { |
| 762 | count = set_syndrome_sources(blocks, sh); |
| 763 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 764 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
| 765 | init_async_submit(&submit, 0, NULL, ops_complete_compute, sh, |
| 766 | to_addr_conv(sh, percpu)); |
| 767 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 768 | } else { |
| 769 | /* Compute any data- or p-drive using XOR */ |
| 770 | count = 0; |
| 771 | for (i = disks; i-- ; ) { |
| 772 | if (i == target || i == qd_idx) |
| 773 | continue; |
| 774 | blocks[count++] = sh->dev[i].page; |
| 775 | } |
| 776 | |
| 777 | init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL, |
| 778 | ops_complete_compute, sh, |
| 779 | to_addr_conv(sh, percpu)); |
| 780 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); |
| 781 | } |
| 782 | |
| 783 | return tx; |
| 784 | } |
| 785 | |
| 786 | static struct dma_async_tx_descriptor * |
| 787 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 788 | { |
| 789 | int i, count, disks = sh->disks; |
| 790 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 791 | int d0_idx = raid6_d0(sh); |
| 792 | int faila = -1, failb = -1; |
| 793 | int target = sh->ops.target; |
| 794 | int target2 = sh->ops.target2; |
| 795 | struct r5dev *tgt = &sh->dev[target]; |
| 796 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 797 | struct dma_async_tx_descriptor *tx; |
| 798 | struct page **blocks = percpu->scribble; |
| 799 | struct async_submit_ctl submit; |
| 800 | |
| 801 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 802 | __func__, (unsigned long long)sh->sector, target, target2); |
| 803 | BUG_ON(target < 0 || target2 < 0); |
| 804 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 805 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 806 | |
| 807 | /* we need to open-code set_syndrome_sources to handle to the |
| 808 | * slot number conversion for 'faila' and 'failb' |
| 809 | */ |
| 810 | for (i = 0; i < disks ; i++) |
| 811 | blocks[i] = (void *)raid6_empty_zero_page; |
| 812 | count = 0; |
| 813 | i = d0_idx; |
| 814 | do { |
| 815 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 816 | |
| 817 | blocks[slot] = sh->dev[i].page; |
| 818 | |
| 819 | if (i == target) |
| 820 | faila = slot; |
| 821 | if (i == target2) |
| 822 | failb = slot; |
| 823 | i = raid6_next_disk(i, disks); |
| 824 | } while (i != d0_idx); |
| 825 | BUG_ON(count != syndrome_disks); |
| 826 | |
| 827 | BUG_ON(faila == failb); |
| 828 | if (failb < faila) |
| 829 | swap(faila, failb); |
| 830 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 831 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 832 | |
| 833 | atomic_inc(&sh->count); |
| 834 | |
| 835 | if (failb == syndrome_disks+1) { |
| 836 | /* Q disk is one of the missing disks */ |
| 837 | if (faila == syndrome_disks) { |
| 838 | /* Missing P+Q, just recompute */ |
| 839 | init_async_submit(&submit, 0, NULL, ops_complete_compute, |
| 840 | sh, to_addr_conv(sh, percpu)); |
| 841 | return async_gen_syndrome(blocks, 0, count+2, |
| 842 | STRIPE_SIZE, &submit); |
| 843 | } else { |
| 844 | struct page *dest; |
| 845 | int data_target; |
| 846 | int qd_idx = sh->qd_idx; |
| 847 | |
| 848 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 849 | if (target == qd_idx) |
| 850 | data_target = target2; |
| 851 | else |
| 852 | data_target = target; |
| 853 | |
| 854 | count = 0; |
| 855 | for (i = disks; i-- ; ) { |
| 856 | if (i == data_target || i == qd_idx) |
| 857 | continue; |
| 858 | blocks[count++] = sh->dev[i].page; |
| 859 | } |
| 860 | dest = sh->dev[data_target].page; |
| 861 | init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL, |
| 862 | NULL, NULL, to_addr_conv(sh, percpu)); |
| 863 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
| 864 | &submit); |
| 865 | |
| 866 | count = set_syndrome_sources(blocks, sh); |
| 867 | init_async_submit(&submit, 0, tx, ops_complete_compute, |
| 868 | sh, to_addr_conv(sh, percpu)); |
| 869 | return async_gen_syndrome(blocks, 0, count+2, |
| 870 | STRIPE_SIZE, &submit); |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | init_async_submit(&submit, 0, NULL, ops_complete_compute, sh, |
| 875 | to_addr_conv(sh, percpu)); |
| 876 | if (failb == syndrome_disks) { |
| 877 | /* We're missing D+P. */ |
| 878 | return async_raid6_datap_recov(syndrome_disks+2, STRIPE_SIZE, |
| 879 | faila, blocks, &submit); |
| 880 | } else { |
| 881 | /* We're missing D+D. */ |
| 882 | return async_raid6_2data_recov(syndrome_disks+2, STRIPE_SIZE, |
| 883 | faila, failb, blocks, &submit); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 888 | static void ops_complete_prexor(void *stripe_head_ref) |
| 889 | { |
| 890 | struct stripe_head *sh = stripe_head_ref; |
| 891 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 892 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 893 | (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | static struct dma_async_tx_descriptor * |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 897 | ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 898 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 899 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 900 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 901 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 902 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 903 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 904 | |
| 905 | /* existing parity data subtracted */ |
| 906 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 907 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 908 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 909 | (unsigned long long)sh->sector); |
| 910 | |
| 911 | for (i = disks; i--; ) { |
| 912 | struct r5dev *dev = &sh->dev[i]; |
| 913 | /* Only process blocks that are known to be uptodate */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 914 | if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 915 | xor_srcs[count++] = dev->page; |
| 916 | } |
| 917 | |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 918 | init_async_submit(&submit, ASYNC_TX_XOR_DROP_DST, tx, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 919 | ops_complete_prexor, sh, to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 920 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 921 | |
| 922 | return tx; |
| 923 | } |
| 924 | |
| 925 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 926 | 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] | 927 | { |
| 928 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 929 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 930 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 931 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 932 | (unsigned long long)sh->sector); |
| 933 | |
| 934 | for (i = disks; i--; ) { |
| 935 | struct r5dev *dev = &sh->dev[i]; |
| 936 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 937 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 938 | if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 939 | struct bio *wbi; |
| 940 | |
| 941 | spin_lock(&sh->lock); |
| 942 | chosen = dev->towrite; |
| 943 | dev->towrite = NULL; |
| 944 | BUG_ON(dev->written); |
| 945 | wbi = dev->written = chosen; |
| 946 | spin_unlock(&sh->lock); |
| 947 | |
| 948 | while (wbi && wbi->bi_sector < |
| 949 | dev->sector + STRIPE_SECTORS) { |
| 950 | tx = async_copy_data(1, wbi, dev->page, |
| 951 | dev->sector, tx); |
| 952 | wbi = r5_next_bio(wbi, dev->sector); |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | return tx; |
| 958 | } |
| 959 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 960 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 961 | { |
| 962 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 963 | int disks = sh->disks; |
| 964 | int pd_idx = sh->pd_idx; |
| 965 | int qd_idx = sh->qd_idx; |
| 966 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 967 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 968 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 969 | (unsigned long long)sh->sector); |
| 970 | |
| 971 | for (i = disks; i--; ) { |
| 972 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 973 | |
| 974 | if (dev->written || i == pd_idx || i == qd_idx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 975 | set_bit(R5_UPTODATE, &dev->flags); |
| 976 | } |
| 977 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 978 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 979 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 980 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 981 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 982 | else { |
| 983 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 984 | sh->reconstruct_state = reconstruct_state_result; |
| 985 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 986 | |
| 987 | set_bit(STRIPE_HANDLE, &sh->state); |
| 988 | release_stripe(sh); |
| 989 | } |
| 990 | |
| 991 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 992 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 993 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 994 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 995 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 996 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 997 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 998 | int count = 0, pd_idx = sh->pd_idx, i; |
| 999 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1000 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1001 | unsigned long flags; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1002 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1003 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1004 | (unsigned long long)sh->sector); |
| 1005 | |
| 1006 | /* check if prexor is active which means only process blocks |
| 1007 | * that are part of a read-modify-write (written) |
| 1008 | */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1009 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 1010 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1011 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1012 | for (i = disks; i--; ) { |
| 1013 | struct r5dev *dev = &sh->dev[i]; |
| 1014 | if (dev->written) |
| 1015 | xor_srcs[count++] = dev->page; |
| 1016 | } |
| 1017 | } else { |
| 1018 | xor_dest = sh->dev[pd_idx].page; |
| 1019 | for (i = disks; i--; ) { |
| 1020 | struct r5dev *dev = &sh->dev[i]; |
| 1021 | if (i != pd_idx) |
| 1022 | xor_srcs[count++] = dev->page; |
| 1023 | } |
| 1024 | } |
| 1025 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1026 | /* 1/ if we prexor'd then the dest is reused as a source |
| 1027 | * 2/ if we did not prexor then we are redoing the parity |
| 1028 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 1029 | * for the synchronous xor case |
| 1030 | */ |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 1031 | flags = ASYNC_TX_ACK | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1032 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
| 1033 | |
| 1034 | atomic_inc(&sh->count); |
| 1035 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1036 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1037 | to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1038 | if (unlikely(count == 1)) |
| 1039 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
| 1040 | else |
| 1041 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1044 | static void |
| 1045 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1046 | struct dma_async_tx_descriptor *tx) |
| 1047 | { |
| 1048 | struct async_submit_ctl submit; |
| 1049 | struct page **blocks = percpu->scribble; |
| 1050 | int count; |
| 1051 | |
| 1052 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 1053 | |
| 1054 | count = set_syndrome_sources(blocks, sh); |
| 1055 | |
| 1056 | atomic_inc(&sh->count); |
| 1057 | |
| 1058 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct, |
| 1059 | sh, to_addr_conv(sh, percpu)); |
| 1060 | async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 1061 | } |
| 1062 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1063 | static void ops_complete_check(void *stripe_head_ref) |
| 1064 | { |
| 1065 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1066 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1067 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1068 | (unsigned long long)sh->sector); |
| 1069 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1070 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1071 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1072 | release_stripe(sh); |
| 1073 | } |
| 1074 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1075 | 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] | 1076 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1077 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1078 | int pd_idx = sh->pd_idx; |
| 1079 | int qd_idx = sh->qd_idx; |
| 1080 | struct page *xor_dest; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1081 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1082 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1083 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1084 | int count; |
| 1085 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1086 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1087 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1088 | (unsigned long long)sh->sector); |
| 1089 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1090 | count = 0; |
| 1091 | xor_dest = sh->dev[pd_idx].page; |
| 1092 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1093 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1094 | if (i == pd_idx || i == qd_idx) |
| 1095 | continue; |
| 1096 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1099 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
| 1100 | to_addr_conv(sh, percpu)); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 1101 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1102 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1103 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1104 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1105 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 1106 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1109 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 1110 | { |
| 1111 | struct page **srcs = percpu->scribble; |
| 1112 | struct async_submit_ctl submit; |
| 1113 | int count; |
| 1114 | |
| 1115 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 1116 | (unsigned long long)sh->sector, checkp); |
| 1117 | |
| 1118 | count = set_syndrome_sources(srcs, sh); |
| 1119 | if (!checkp) |
| 1120 | srcs[count] = NULL; |
| 1121 | |
| 1122 | atomic_inc(&sh->count); |
| 1123 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
| 1124 | sh, to_addr_conv(sh, percpu)); |
| 1125 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, |
| 1126 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); |
| 1127 | } |
| 1128 | |
| 1129 | 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] | 1130 | { |
| 1131 | int overlap_clear = 0, i, disks = sh->disks; |
| 1132 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1133 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1134 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1135 | struct raid5_percpu *percpu; |
| 1136 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1137 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1138 | cpu = get_cpu(); |
| 1139 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1140 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1141 | ops_run_biofill(sh); |
| 1142 | overlap_clear++; |
| 1143 | } |
| 1144 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1145 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1146 | if (level < 6) |
| 1147 | tx = ops_run_compute5(sh, percpu); |
| 1148 | else { |
| 1149 | if (sh->ops.target2 < 0 || sh->ops.target < 0) |
| 1150 | tx = ops_run_compute6_1(sh, percpu); |
| 1151 | else |
| 1152 | tx = ops_run_compute6_2(sh, percpu); |
| 1153 | } |
| 1154 | /* terminate the chain if reconstruct is not set to be run */ |
| 1155 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1156 | async_tx_ack(tx); |
| 1157 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1158 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1159 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1160 | tx = ops_run_prexor(sh, percpu, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1161 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1162 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1163 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1164 | overlap_clear++; |
| 1165 | } |
| 1166 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1167 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
| 1168 | if (level < 6) |
| 1169 | ops_run_reconstruct5(sh, percpu, tx); |
| 1170 | else |
| 1171 | ops_run_reconstruct6(sh, percpu, tx); |
| 1172 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1173 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1174 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
| 1175 | if (sh->check_state == check_state_run) |
| 1176 | ops_run_check_p(sh, percpu); |
| 1177 | else if (sh->check_state == check_state_run_q) |
| 1178 | ops_run_check_pq(sh, percpu, 0); |
| 1179 | else if (sh->check_state == check_state_run_pq) |
| 1180 | ops_run_check_pq(sh, percpu, 1); |
| 1181 | else |
| 1182 | BUG(); |
| 1183 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1184 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1185 | if (overlap_clear) |
| 1186 | for (i = disks; i--; ) { |
| 1187 | struct r5dev *dev = &sh->dev[i]; |
| 1188 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 1189 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 1190 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1191 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1194 | static int grow_one_stripe(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | { |
| 1196 | struct stripe_head *sh; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1197 | sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL); |
| 1198 | if (!sh) |
| 1199 | return 0; |
| 1200 | memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev)); |
| 1201 | sh->raid_conf = conf; |
| 1202 | spin_lock_init(&sh->lock); |
| 1203 | |
| 1204 | if (grow_buffers(sh, conf->raid_disks)) { |
| 1205 | shrink_buffers(sh, conf->raid_disks); |
| 1206 | kmem_cache_free(conf->slab_cache, sh); |
| 1207 | return 0; |
| 1208 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1209 | sh->disks = conf->raid_disks; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1210 | /* we just created an active stripe so... */ |
| 1211 | atomic_set(&sh->count, 1); |
| 1212 | atomic_inc(&conf->active_stripes); |
| 1213 | INIT_LIST_HEAD(&sh->lru); |
| 1214 | release_stripe(sh); |
| 1215 | return 1; |
| 1216 | } |
| 1217 | |
| 1218 | static int grow_stripes(raid5_conf_t *conf, int num) |
| 1219 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1220 | struct kmem_cache *sc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | int devs = conf->raid_disks; |
| 1222 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 1223 | sprintf(conf->cache_name[0], |
| 1224 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 1225 | sprintf(conf->cache_name[1], |
| 1226 | "raid%d-%s-alt", conf->level, mdname(conf->mddev)); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1227 | conf->active_name = 0; |
| 1228 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1230 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | if (!sc) |
| 1232 | return 1; |
| 1233 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1234 | conf->pool_size = devs; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1235 | while (num--) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1236 | if (!grow_one_stripe(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | return 0; |
| 1239 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 1240 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1241 | /** |
| 1242 | * scribble_len - return the required size of the scribble region |
| 1243 | * @num - total number of disks in the array |
| 1244 | * |
| 1245 | * The size must be enough to contain: |
| 1246 | * 1/ a struct page pointer for each device in the array +2 |
| 1247 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 1248 | * (dma_map_page()) or page (page_address()) address. |
| 1249 | * |
| 1250 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 1251 | * calculate over all devices (not just the data blocks), using zeros in place |
| 1252 | * of the P and Q blocks. |
| 1253 | */ |
| 1254 | static size_t scribble_len(int num) |
| 1255 | { |
| 1256 | size_t len; |
| 1257 | |
| 1258 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); |
| 1259 | |
| 1260 | return len; |
| 1261 | } |
| 1262 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1263 | static int resize_stripes(raid5_conf_t *conf, int newsize) |
| 1264 | { |
| 1265 | /* Make all the stripes able to hold 'newsize' devices. |
| 1266 | * New slots in each stripe get 'page' set to a new page. |
| 1267 | * |
| 1268 | * This happens in stages: |
| 1269 | * 1/ create a new kmem_cache and allocate the required number of |
| 1270 | * stripe_heads. |
| 1271 | * 2/ gather all the old stripe_heads and tranfer the pages across |
| 1272 | * to the new stripe_heads. This will have the side effect of |
| 1273 | * freezing the array as once all stripe_heads have been collected, |
| 1274 | * no IO will be possible. Old stripe heads are freed once their |
| 1275 | * pages have been transferred over, and the old kmem_cache is |
| 1276 | * freed when all stripes are done. |
| 1277 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 1278 | * we simple return a failre status - no need to clean anything up. |
| 1279 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 1280 | * If this fails, we don't bother trying the shrink the |
| 1281 | * stripe_heads down again, we just leave them as they are. |
| 1282 | * As each stripe_head is processed the new one is released into |
| 1283 | * active service. |
| 1284 | * |
| 1285 | * Once step2 is started, we cannot afford to wait for a write, |
| 1286 | * so we use GFP_NOIO allocations. |
| 1287 | */ |
| 1288 | struct stripe_head *osh, *nsh; |
| 1289 | LIST_HEAD(newstripes); |
| 1290 | struct disk_info *ndisks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1291 | unsigned long cpu; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1292 | int err; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1293 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1294 | int i; |
| 1295 | |
| 1296 | if (newsize <= conf->pool_size) |
| 1297 | return 0; /* never bother to shrink */ |
| 1298 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1299 | err = md_allow_write(conf->mddev); |
| 1300 | if (err) |
| 1301 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 1302 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1303 | /* Step 1 */ |
| 1304 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 1305 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1306 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1307 | if (!sc) |
| 1308 | return -ENOMEM; |
| 1309 | |
| 1310 | for (i = conf->max_nr_stripes; i; i--) { |
| 1311 | nsh = kmem_cache_alloc(sc, GFP_KERNEL); |
| 1312 | if (!nsh) |
| 1313 | break; |
| 1314 | |
| 1315 | memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev)); |
| 1316 | |
| 1317 | nsh->raid_conf = conf; |
| 1318 | spin_lock_init(&nsh->lock); |
| 1319 | |
| 1320 | list_add(&nsh->lru, &newstripes); |
| 1321 | } |
| 1322 | if (i) { |
| 1323 | /* didn't get enough, give up */ |
| 1324 | while (!list_empty(&newstripes)) { |
| 1325 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1326 | list_del(&nsh->lru); |
| 1327 | kmem_cache_free(sc, nsh); |
| 1328 | } |
| 1329 | kmem_cache_destroy(sc); |
| 1330 | return -ENOMEM; |
| 1331 | } |
| 1332 | /* Step 2 - Must use GFP_NOIO now. |
| 1333 | * OK, we have enough stripes, start collecting inactive |
| 1334 | * stripes and copying them over |
| 1335 | */ |
| 1336 | list_for_each_entry(nsh, &newstripes, lru) { |
| 1337 | spin_lock_irq(&conf->device_lock); |
| 1338 | wait_event_lock_irq(conf->wait_for_stripe, |
| 1339 | !list_empty(&conf->inactive_list), |
| 1340 | conf->device_lock, |
NeilBrown | b3b46be | 2006-03-27 01:18:16 -0800 | [diff] [blame] | 1341 | unplug_slaves(conf->mddev) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1342 | ); |
| 1343 | osh = get_free_stripe(conf); |
| 1344 | spin_unlock_irq(&conf->device_lock); |
| 1345 | atomic_set(&nsh->count, 1); |
| 1346 | for(i=0; i<conf->pool_size; i++) |
| 1347 | nsh->dev[i].page = osh->dev[i].page; |
| 1348 | for( ; i<newsize; i++) |
| 1349 | nsh->dev[i].page = NULL; |
| 1350 | kmem_cache_free(conf->slab_cache, osh); |
| 1351 | } |
| 1352 | kmem_cache_destroy(conf->slab_cache); |
| 1353 | |
| 1354 | /* Step 3. |
| 1355 | * At this point, we are holding all the stripes so the array |
| 1356 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1357 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1358 | */ |
| 1359 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 1360 | if (ndisks) { |
| 1361 | for (i=0; i<conf->raid_disks; i++) |
| 1362 | ndisks[i] = conf->disks[i]; |
| 1363 | kfree(conf->disks); |
| 1364 | conf->disks = ndisks; |
| 1365 | } else |
| 1366 | err = -ENOMEM; |
| 1367 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1368 | get_online_cpus(); |
| 1369 | conf->scribble_len = scribble_len(newsize); |
| 1370 | for_each_present_cpu(cpu) { |
| 1371 | struct raid5_percpu *percpu; |
| 1372 | void *scribble; |
| 1373 | |
| 1374 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 1375 | scribble = kmalloc(conf->scribble_len, GFP_NOIO); |
| 1376 | |
| 1377 | if (scribble) { |
| 1378 | kfree(percpu->scribble); |
| 1379 | percpu->scribble = scribble; |
| 1380 | } else { |
| 1381 | err = -ENOMEM; |
| 1382 | break; |
| 1383 | } |
| 1384 | } |
| 1385 | put_online_cpus(); |
| 1386 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1387 | /* Step 4, return new stripes to service */ |
| 1388 | while(!list_empty(&newstripes)) { |
| 1389 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1390 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1391 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1392 | for (i=conf->raid_disks; i < newsize; i++) |
| 1393 | if (nsh->dev[i].page == NULL) { |
| 1394 | struct page *p = alloc_page(GFP_NOIO); |
| 1395 | nsh->dev[i].page = p; |
| 1396 | if (!p) |
| 1397 | err = -ENOMEM; |
| 1398 | } |
| 1399 | release_stripe(nsh); |
| 1400 | } |
| 1401 | /* critical section pass, GFP_NOIO no longer needed */ |
| 1402 | |
| 1403 | conf->slab_cache = sc; |
| 1404 | conf->active_name = 1-conf->active_name; |
| 1405 | conf->pool_size = newsize; |
| 1406 | return err; |
| 1407 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1409 | static int drop_one_stripe(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | { |
| 1411 | struct stripe_head *sh; |
| 1412 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1413 | spin_lock_irq(&conf->device_lock); |
| 1414 | sh = get_free_stripe(conf); |
| 1415 | spin_unlock_irq(&conf->device_lock); |
| 1416 | if (!sh) |
| 1417 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1418 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1419 | shrink_buffers(sh, conf->pool_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1420 | kmem_cache_free(conf->slab_cache, sh); |
| 1421 | atomic_dec(&conf->active_stripes); |
| 1422 | return 1; |
| 1423 | } |
| 1424 | |
| 1425 | static void shrink_stripes(raid5_conf_t *conf) |
| 1426 | { |
| 1427 | while (drop_one_stripe(conf)) |
| 1428 | ; |
| 1429 | |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1430 | if (conf->slab_cache) |
| 1431 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | conf->slab_cache = NULL; |
| 1433 | } |
| 1434 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1435 | static void raid5_end_read_request(struct bio * bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1437 | struct stripe_head *sh = bi->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1439 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1441 | char b[BDEVNAME_SIZE]; |
| 1442 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | |
| 1445 | for (i=0 ; i<disks; i++) |
| 1446 | if (bi == &sh->dev[i].req) |
| 1447 | break; |
| 1448 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1449 | pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
| 1450 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | uptodate); |
| 1452 | if (i == disks) { |
| 1453 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1454 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1459 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1460 | rdev = conf->disks[i].rdev; |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1461 | printk_rl(KERN_INFO "raid5:%s: read error corrected" |
| 1462 | " (%lu sectors at %llu on %s)\n", |
| 1463 | mdname(conf->mddev), STRIPE_SECTORS, |
| 1464 | (unsigned long long)(sh->sector |
| 1465 | + rdev->data_offset), |
| 1466 | bdevname(rdev->bdev, b)); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1467 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1468 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
| 1469 | } |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1470 | if (atomic_read(&conf->disks[i].rdev->read_errors)) |
| 1471 | atomic_set(&conf->disks[i].rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | } else { |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1473 | const char *bdn = bdevname(conf->disks[i].rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1474 | int retry = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1475 | rdev = conf->disks[i].rdev; |
| 1476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1478 | atomic_inc(&rdev->read_errors); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1479 | if (conf->mddev->degraded) |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1480 | printk_rl(KERN_WARNING |
| 1481 | "raid5:%s: read error not correctable " |
| 1482 | "(sector %llu on %s).\n", |
| 1483 | mdname(conf->mddev), |
| 1484 | (unsigned long long)(sh->sector |
| 1485 | + rdev->data_offset), |
| 1486 | bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1487 | else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1488 | /* Oh, no!!! */ |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1489 | printk_rl(KERN_WARNING |
| 1490 | "raid5:%s: read error NOT corrected!! " |
| 1491 | "(sector %llu on %s).\n", |
| 1492 | mdname(conf->mddev), |
| 1493 | (unsigned long long)(sh->sector |
| 1494 | + rdev->data_offset), |
| 1495 | bdn); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1496 | else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1497 | > conf->max_nr_stripes) |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1498 | printk(KERN_WARNING |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1499 | "raid5:%s: Too many read errors, failing device %s.\n", |
| 1500 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1501 | else |
| 1502 | retry = 1; |
| 1503 | if (retry) |
| 1504 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 1505 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1506 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1507 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1508 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1509 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | } |
| 1511 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1513 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1514 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1517 | static void raid5_end_write_request(struct bio *bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1519 | struct stripe_head *sh = bi->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1521 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
| 1523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | for (i=0 ; i<disks; i++) |
| 1525 | if (bi == &sh->dev[i].req) |
| 1526 | break; |
| 1527 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1528 | pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 1530 | uptodate); |
| 1531 | if (i == disks) { |
| 1532 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1533 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | if (!uptodate) |
| 1537 | md_error(conf->mddev, conf->disks[i].rdev); |
| 1538 | |
| 1539 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
| 1540 | |
| 1541 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1542 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1543 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1547 | 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] | 1548 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1549 | 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] | 1550 | { |
| 1551 | struct r5dev *dev = &sh->dev[i]; |
| 1552 | |
| 1553 | bio_init(&dev->req); |
| 1554 | dev->req.bi_io_vec = &dev->vec; |
| 1555 | dev->req.bi_vcnt++; |
| 1556 | dev->req.bi_max_vecs++; |
| 1557 | dev->vec.bv_page = dev->page; |
| 1558 | dev->vec.bv_len = STRIPE_SIZE; |
| 1559 | dev->vec.bv_offset = 0; |
| 1560 | |
| 1561 | dev->req.bi_sector = sh->sector; |
| 1562 | dev->req.bi_private = sh; |
| 1563 | |
| 1564 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1565 | dev->sector = compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | static void error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1569 | { |
| 1570 | char b[BDEVNAME_SIZE]; |
| 1571 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1572 | pr_debug("raid5: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1574 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1575 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1576 | if (test_and_clear_bit(In_sync, &rdev->flags)) { |
| 1577 | unsigned long flags; |
| 1578 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | mddev->degraded++; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1580 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | /* |
| 1582 | * if recovery was running, make sure it aborts. |
| 1583 | */ |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 1584 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1586 | set_bit(Faulty, &rdev->flags); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1587 | printk(KERN_ALERT |
| 1588 | "raid5: Disk failure on %s, disabling device.\n" |
| 1589 | "raid5: Operation continuing on %d devices.\n", |
| 1590 | bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1592 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | |
| 1594 | /* |
| 1595 | * Input: a 'big' sector number, |
| 1596 | * Output: index of the data and parity disk, and the sector # in them. |
| 1597 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1598 | 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] | 1599 | int previous, int *dd_idx, |
| 1600 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | { |
| 1602 | long stripe; |
| 1603 | unsigned long chunk_number; |
| 1604 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1605 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1606 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1608 | int algorithm = previous ? conf->prev_algo |
| 1609 | : conf->algorithm; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1610 | int sectors_per_chunk = previous ? (conf->prev_chunk >> 9) |
| 1611 | : (conf->chunk_size >> 9); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1612 | int raid_disks = previous ? conf->previous_raid_disks |
| 1613 | : conf->raid_disks; |
| 1614 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | |
| 1616 | /* First compute the information on this sector */ |
| 1617 | |
| 1618 | /* |
| 1619 | * Compute the chunk number and the sector offset inside the chunk |
| 1620 | */ |
| 1621 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 1622 | chunk_number = r_sector; |
| 1623 | BUG_ON(r_sector != chunk_number); |
| 1624 | |
| 1625 | /* |
| 1626 | * Compute the stripe number |
| 1627 | */ |
| 1628 | stripe = chunk_number / data_disks; |
| 1629 | |
| 1630 | /* |
| 1631 | * Compute the data disk and parity disk indexes inside the stripe |
| 1632 | */ |
| 1633 | *dd_idx = chunk_number % data_disks; |
| 1634 | |
| 1635 | /* |
| 1636 | * Select the parity disk based on the user selected algorithm. |
| 1637 | */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1638 | pd_idx = qd_idx = ~0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1639 | switch(conf->level) { |
| 1640 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1641 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1642 | break; |
| 1643 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1644 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1646 | pd_idx = data_disks - stripe % raid_disks; |
| 1647 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | (*dd_idx)++; |
| 1649 | break; |
| 1650 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1651 | pd_idx = stripe % raid_disks; |
| 1652 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | (*dd_idx)++; |
| 1654 | break; |
| 1655 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1656 | pd_idx = data_disks - stripe % raid_disks; |
| 1657 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | break; |
| 1659 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1660 | pd_idx = stripe % raid_disks; |
| 1661 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1663 | case ALGORITHM_PARITY_0: |
| 1664 | pd_idx = 0; |
| 1665 | (*dd_idx)++; |
| 1666 | break; |
| 1667 | case ALGORITHM_PARITY_N: |
| 1668 | pd_idx = data_disks; |
| 1669 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | default: |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1671 | printk(KERN_ERR "raid5: unsupported algorithm %d\n", |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1672 | algorithm); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1673 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1674 | } |
| 1675 | break; |
| 1676 | case 6: |
| 1677 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1678 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1679 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1680 | pd_idx = raid_disks - 1 - (stripe % raid_disks); |
| 1681 | qd_idx = pd_idx + 1; |
| 1682 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1683 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1684 | qd_idx = 0; |
| 1685 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1686 | (*dd_idx) += 2; /* D D P Q D */ |
| 1687 | break; |
| 1688 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1689 | pd_idx = stripe % raid_disks; |
| 1690 | qd_idx = pd_idx + 1; |
| 1691 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1692 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1693 | qd_idx = 0; |
| 1694 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1695 | (*dd_idx) += 2; /* D D P Q D */ |
| 1696 | break; |
| 1697 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1698 | pd_idx = raid_disks - 1 - (stripe % raid_disks); |
| 1699 | qd_idx = (pd_idx + 1) % raid_disks; |
| 1700 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1701 | break; |
| 1702 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1703 | pd_idx = stripe % raid_disks; |
| 1704 | qd_idx = (pd_idx + 1) % raid_disks; |
| 1705 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1706 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1707 | |
| 1708 | case ALGORITHM_PARITY_0: |
| 1709 | pd_idx = 0; |
| 1710 | qd_idx = 1; |
| 1711 | (*dd_idx) += 2; |
| 1712 | break; |
| 1713 | case ALGORITHM_PARITY_N: |
| 1714 | pd_idx = data_disks; |
| 1715 | qd_idx = data_disks + 1; |
| 1716 | break; |
| 1717 | |
| 1718 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 1719 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 1720 | * of blocks for computing Q is different. |
| 1721 | */ |
| 1722 | pd_idx = stripe % raid_disks; |
| 1723 | qd_idx = pd_idx + 1; |
| 1724 | if (pd_idx == raid_disks-1) { |
| 1725 | (*dd_idx)++; /* Q D D D P */ |
| 1726 | qd_idx = 0; |
| 1727 | } else if (*dd_idx >= pd_idx) |
| 1728 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1729 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1730 | break; |
| 1731 | |
| 1732 | case ALGORITHM_ROTATING_N_RESTART: |
| 1733 | /* Same a left_asymmetric, by first stripe is |
| 1734 | * D D D P Q rather than |
| 1735 | * Q D D D P |
| 1736 | */ |
| 1737 | pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks); |
| 1738 | qd_idx = pd_idx + 1; |
| 1739 | if (pd_idx == raid_disks-1) { |
| 1740 | (*dd_idx)++; /* Q D D D P */ |
| 1741 | qd_idx = 0; |
| 1742 | } else if (*dd_idx >= pd_idx) |
| 1743 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1744 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1745 | break; |
| 1746 | |
| 1747 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 1748 | /* Same as left_symmetric but Q is before P */ |
| 1749 | pd_idx = raid_disks - 1 - (stripe % raid_disks); |
| 1750 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 1751 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1752 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1753 | break; |
| 1754 | |
| 1755 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 1756 | /* RAID5 left_asymmetric, with Q on last device */ |
| 1757 | pd_idx = data_disks - stripe % (raid_disks-1); |
| 1758 | if (*dd_idx >= pd_idx) |
| 1759 | (*dd_idx)++; |
| 1760 | qd_idx = raid_disks - 1; |
| 1761 | break; |
| 1762 | |
| 1763 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 1764 | pd_idx = stripe % (raid_disks-1); |
| 1765 | if (*dd_idx >= pd_idx) |
| 1766 | (*dd_idx)++; |
| 1767 | qd_idx = raid_disks - 1; |
| 1768 | break; |
| 1769 | |
| 1770 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 1771 | pd_idx = data_disks - stripe % (raid_disks-1); |
| 1772 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 1773 | qd_idx = raid_disks - 1; |
| 1774 | break; |
| 1775 | |
| 1776 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 1777 | pd_idx = stripe % (raid_disks-1); |
| 1778 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 1779 | qd_idx = raid_disks - 1; |
| 1780 | break; |
| 1781 | |
| 1782 | case ALGORITHM_PARITY_0_6: |
| 1783 | pd_idx = 0; |
| 1784 | (*dd_idx)++; |
| 1785 | qd_idx = raid_disks - 1; |
| 1786 | break; |
| 1787 | |
| 1788 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1789 | default: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1790 | printk(KERN_CRIT "raid6: unsupported algorithm %d\n", |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1791 | algorithm); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1792 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1793 | } |
| 1794 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | } |
| 1796 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1797 | if (sh) { |
| 1798 | sh->pd_idx = pd_idx; |
| 1799 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1800 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1801 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | /* |
| 1803 | * Finally, compute the new sector number |
| 1804 | */ |
| 1805 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 1806 | return new_sector; |
| 1807 | } |
| 1808 | |
| 1809 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1810 | 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] | 1811 | { |
| 1812 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 1813 | int raid_disks = sh->disks; |
| 1814 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | sector_t new_sector = sh->sector, check; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1816 | int sectors_per_chunk = previous ? (conf->prev_chunk >> 9) |
| 1817 | : (conf->chunk_size >> 9); |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1818 | int algorithm = previous ? conf->prev_algo |
| 1819 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | sector_t stripe; |
| 1821 | int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1822 | int chunk_number, dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1824 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 1828 | stripe = new_sector; |
| 1829 | BUG_ON(new_sector != stripe); |
| 1830 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1831 | if (i == sh->pd_idx) |
| 1832 | return 0; |
| 1833 | switch(conf->level) { |
| 1834 | case 4: break; |
| 1835 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1836 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 1838 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 1839 | if (i > sh->pd_idx) |
| 1840 | i--; |
| 1841 | break; |
| 1842 | case ALGORITHM_LEFT_SYMMETRIC: |
| 1843 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 1844 | if (i < sh->pd_idx) |
| 1845 | i += raid_disks; |
| 1846 | i -= (sh->pd_idx + 1); |
| 1847 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1848 | case ALGORITHM_PARITY_0: |
| 1849 | i -= 1; |
| 1850 | break; |
| 1851 | case ALGORITHM_PARITY_N: |
| 1852 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | default: |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1854 | printk(KERN_ERR "raid5: unsupported algorithm %d\n", |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1855 | algorithm); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1856 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1857 | } |
| 1858 | break; |
| 1859 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1860 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1861 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1862 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1863 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 1864 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1865 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 1866 | case ALGORITHM_ROTATING_N_RESTART: |
| 1867 | if (sh->pd_idx == raid_disks-1) |
| 1868 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1869 | else if (i > sh->pd_idx) |
| 1870 | i -= 2; /* D D P Q D */ |
| 1871 | break; |
| 1872 | case ALGORITHM_LEFT_SYMMETRIC: |
| 1873 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 1874 | if (sh->pd_idx == raid_disks-1) |
| 1875 | i--; /* Q D D D P */ |
| 1876 | else { |
| 1877 | /* D D P Q D */ |
| 1878 | if (i < sh->pd_idx) |
| 1879 | i += raid_disks; |
| 1880 | i -= (sh->pd_idx + 2); |
| 1881 | } |
| 1882 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1883 | case ALGORITHM_PARITY_0: |
| 1884 | i -= 2; |
| 1885 | break; |
| 1886 | case ALGORITHM_PARITY_N: |
| 1887 | break; |
| 1888 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 1889 | if (sh->pd_idx == 0) |
| 1890 | i--; /* P D D D Q */ |
| 1891 | else if (i > sh->pd_idx) |
| 1892 | i -= 2; /* D D Q P D */ |
| 1893 | break; |
| 1894 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 1895 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 1896 | if (i > sh->pd_idx) |
| 1897 | i--; |
| 1898 | break; |
| 1899 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 1900 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 1901 | if (i < sh->pd_idx) |
| 1902 | i += data_disks + 1; |
| 1903 | i -= (sh->pd_idx + 1); |
| 1904 | break; |
| 1905 | case ALGORITHM_PARITY_0_6: |
| 1906 | i -= 1; |
| 1907 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1908 | default: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1909 | printk(KERN_CRIT "raid6: unsupported algorithm %d\n", |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1910 | algorithm); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1911 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1912 | } |
| 1913 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | chunk_number = stripe * data_disks + i; |
| 1917 | r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset; |
| 1918 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1919 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1920 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1921 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 1922 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1923 | printk(KERN_ERR "compute_blocknr: map not correct\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | return 0; |
| 1925 | } |
| 1926 | return r_sector; |
| 1927 | } |
| 1928 | |
| 1929 | |
| 1930 | |
| 1931 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1932 | * Copy data between a page in the stripe cache, and one or more bion |
| 1933 | * The page could align with the middle of the bio, or there could be |
| 1934 | * several bion, each with several bio_vecs, which cover part of the page |
| 1935 | * Multiple bion are linked together on bi_next. There may be extras |
| 1936 | * at the end of this list. We ignore them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | */ |
| 1938 | static void copy_data(int frombio, struct bio *bio, |
| 1939 | struct page *page, |
| 1940 | sector_t sector) |
| 1941 | { |
| 1942 | char *pa = page_address(page); |
| 1943 | struct bio_vec *bvl; |
| 1944 | int i; |
| 1945 | int page_offset; |
| 1946 | |
| 1947 | if (bio->bi_sector >= sector) |
| 1948 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 1949 | else |
| 1950 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
| 1951 | bio_for_each_segment(bvl, bio, i) { |
| 1952 | int len = bio_iovec_idx(bio,i)->bv_len; |
| 1953 | int clen; |
| 1954 | int b_offset = 0; |
| 1955 | |
| 1956 | if (page_offset < 0) { |
| 1957 | b_offset = -page_offset; |
| 1958 | page_offset += b_offset; |
| 1959 | len -= b_offset; |
| 1960 | } |
| 1961 | |
| 1962 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 1963 | clen = STRIPE_SIZE - page_offset; |
| 1964 | else clen = len; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1965 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | if (clen > 0) { |
| 1967 | char *ba = __bio_kmap_atomic(bio, i, KM_USER0); |
| 1968 | if (frombio) |
| 1969 | memcpy(pa+page_offset, ba+b_offset, clen); |
| 1970 | else |
| 1971 | memcpy(ba+b_offset, pa+page_offset, clen); |
| 1972 | __bio_kunmap_atomic(ba, KM_USER0); |
| 1973 | } |
| 1974 | if (clen < len) /* hit end of page */ |
| 1975 | break; |
| 1976 | page_offset += len; |
| 1977 | } |
| 1978 | } |
| 1979 | |
Dan Williams | 9bc89cd | 2007-01-02 11:10:44 -0700 | [diff] [blame] | 1980 | #define check_xor() do { \ |
| 1981 | if (count == MAX_XOR_BLOCKS) { \ |
| 1982 | xor_blocks(count, STRIPE_SIZE, dest, ptr);\ |
| 1983 | count = 0; \ |
| 1984 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | } while(0) |
| 1986 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1987 | static void compute_parity6(struct stripe_head *sh, int method) |
| 1988 | { |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1989 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1990 | int i, pd_idx, qd_idx, d0_idx, disks = sh->disks, count; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1991 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1992 | struct bio *chosen; |
| 1993 | /**** FIX THIS: This could be very bad if disks is close to 256 ****/ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1994 | void *ptrs[syndrome_disks+2]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1995 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1996 | pd_idx = sh->pd_idx; |
| 1997 | qd_idx = sh->qd_idx; |
| 1998 | d0_idx = raid6_d0(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1999 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2000 | pr_debug("compute_parity, stripe %llu, method %d\n", |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2001 | (unsigned long long)sh->sector, method); |
| 2002 | |
| 2003 | switch(method) { |
| 2004 | case READ_MODIFY_WRITE: |
| 2005 | BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */ |
| 2006 | case RECONSTRUCT_WRITE: |
| 2007 | for (i= disks; i-- ;) |
| 2008 | if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) { |
| 2009 | chosen = sh->dev[i].towrite; |
| 2010 | sh->dev[i].towrite = NULL; |
| 2011 | |
| 2012 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2013 | wake_up(&conf->wait_for_overlap); |
| 2014 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 2015 | BUG_ON(sh->dev[i].written); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2016 | sh->dev[i].written = chosen; |
| 2017 | } |
| 2018 | break; |
| 2019 | case CHECK_PARITY: |
| 2020 | BUG(); /* Not implemented yet */ |
| 2021 | } |
| 2022 | |
| 2023 | for (i = disks; i--;) |
| 2024 | if (sh->dev[i].written) { |
| 2025 | sector_t sector = sh->dev[i].sector; |
| 2026 | struct bio *wbi = sh->dev[i].written; |
| 2027 | while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) { |
| 2028 | copy_data(1, wbi, sh->dev[i].page, sector); |
| 2029 | wbi = r5_next_bio(wbi, sector); |
| 2030 | } |
| 2031 | |
| 2032 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 2033 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
| 2034 | } |
| 2035 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2036 | /* Note that unlike RAID-5, the ordering of the disks matters greatly.*/ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2037 | |
| 2038 | for (i = 0; i < disks; i++) |
| 2039 | ptrs[i] = (void *)raid6_empty_zero_page; |
| 2040 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2041 | count = 0; |
| 2042 | i = d0_idx; |
| 2043 | do { |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2044 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 2045 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2046 | ptrs[slot] = page_address(sh->dev[i].page); |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2047 | if (slot < syndrome_disks && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2048 | !test_bit(R5_UPTODATE, &sh->dev[i].flags)) { |
| 2049 | printk(KERN_ERR "block %d/%d not uptodate " |
| 2050 | "on parity calc\n", i, count); |
| 2051 | BUG(); |
| 2052 | } |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2053 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2054 | i = raid6_next_disk(i, disks); |
| 2055 | } while (i != d0_idx); |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2056 | BUG_ON(count != syndrome_disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2057 | |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2058 | raid6_call.gen_syndrome(syndrome_disks+2, STRIPE_SIZE, ptrs); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2059 | |
| 2060 | switch(method) { |
| 2061 | case RECONSTRUCT_WRITE: |
| 2062 | set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 2063 | set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags); |
| 2064 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 2065 | set_bit(R5_LOCKED, &sh->dev[qd_idx].flags); |
| 2066 | break; |
| 2067 | case UPDATE_PARITY: |
| 2068 | set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 2069 | set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags); |
| 2070 | break; |
| 2071 | } |
| 2072 | } |
| 2073 | |
| 2074 | |
| 2075 | /* Compute one missing block */ |
| 2076 | static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero) |
| 2077 | { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 2078 | int i, count, disks = sh->disks; |
Dan Williams | 9bc89cd | 2007-01-02 11:10:44 -0700 | [diff] [blame] | 2079 | void *ptr[MAX_XOR_BLOCKS], *dest, *p; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2080 | int qd_idx = sh->qd_idx; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2081 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2082 | pr_debug("compute_block_1, stripe %llu, idx %d\n", |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2083 | (unsigned long long)sh->sector, dd_idx); |
| 2084 | |
| 2085 | if ( dd_idx == qd_idx ) { |
| 2086 | /* We're actually computing the Q drive */ |
| 2087 | compute_parity6(sh, UPDATE_PARITY); |
| 2088 | } else { |
Dan Williams | 9bc89cd | 2007-01-02 11:10:44 -0700 | [diff] [blame] | 2089 | dest = page_address(sh->dev[dd_idx].page); |
| 2090 | if (!nozero) memset(dest, 0, STRIPE_SIZE); |
| 2091 | count = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2092 | for (i = disks ; i--; ) { |
| 2093 | if (i == dd_idx || i == qd_idx) |
| 2094 | continue; |
| 2095 | p = page_address(sh->dev[i].page); |
| 2096 | if (test_bit(R5_UPTODATE, &sh->dev[i].flags)) |
| 2097 | ptr[count++] = p; |
| 2098 | else |
| 2099 | printk("compute_block() %d, stripe %llu, %d" |
| 2100 | " not present\n", dd_idx, |
| 2101 | (unsigned long long)sh->sector, i); |
| 2102 | |
| 2103 | check_xor(); |
| 2104 | } |
Dan Williams | 9bc89cd | 2007-01-02 11:10:44 -0700 | [diff] [blame] | 2105 | if (count) |
| 2106 | xor_blocks(count, STRIPE_SIZE, dest, ptr); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2107 | if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags); |
| 2108 | else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags); |
| 2109 | } |
| 2110 | } |
| 2111 | |
| 2112 | /* Compute two missing blocks */ |
| 2113 | static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2) |
| 2114 | { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 2115 | int i, count, disks = sh->disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2116 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2117 | int d0_idx = raid6_d0(sh); |
| 2118 | int faila = -1, failb = -1; |
| 2119 | /**** FIX THIS: This could be very bad if disks is close to 256 ****/ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2120 | void *ptrs[syndrome_disks+2]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2121 | |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2122 | for (i = 0; i < disks ; i++) |
| 2123 | ptrs[i] = (void *)raid6_empty_zero_page; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2124 | count = 0; |
| 2125 | i = d0_idx; |
| 2126 | do { |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2127 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 2128 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2129 | ptrs[slot] = page_address(sh->dev[i].page); |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2130 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2131 | if (i == dd_idx1) |
| 2132 | faila = slot; |
| 2133 | if (i == dd_idx2) |
| 2134 | failb = slot; |
| 2135 | i = raid6_next_disk(i, disks); |
| 2136 | } while (i != d0_idx); |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2137 | BUG_ON(count != syndrome_disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2138 | |
| 2139 | BUG_ON(faila == failb); |
| 2140 | if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; } |
| 2141 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2142 | pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n", |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2143 | (unsigned long long)sh->sector, dd_idx1, dd_idx2, |
| 2144 | faila, failb); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2145 | |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2146 | if (failb == syndrome_disks+1) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2147 | /* Q disk is one of the missing disks */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2148 | if (faila == syndrome_disks) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2149 | /* Missing P+Q, just recompute */ |
| 2150 | compute_parity6(sh, UPDATE_PARITY); |
| 2151 | return; |
| 2152 | } else { |
| 2153 | /* We're missing D+Q; recompute D from P */ |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2154 | compute_block_1(sh, ((dd_idx1 == sh->qd_idx) ? |
| 2155 | dd_idx2 : dd_idx1), |
| 2156 | 0); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2157 | compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */ |
| 2158 | return; |
| 2159 | } |
| 2160 | } |
| 2161 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2162 | /* We're missing D+P or D+D; */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2163 | if (failb == syndrome_disks) { |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2164 | /* We're missing D+P. */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2165 | raid6_datap_recov(syndrome_disks+2, STRIPE_SIZE, faila, ptrs); |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2166 | } else { |
| 2167 | /* We're missing D+D. */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2168 | raid6_2data_recov(syndrome_disks+2, STRIPE_SIZE, faila, failb, |
| 2169 | ptrs); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2170 | } |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2171 | |
| 2172 | /* Both the above update both missing blocks */ |
| 2173 | set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags); |
| 2174 | set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2177 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2178 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2179 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2180 | { |
| 2181 | int i, pd_idx = sh->pd_idx, disks = sh->disks; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2182 | raid5_conf_t *conf = sh->raid_conf; |
| 2183 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2184 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2185 | if (rcw) { |
| 2186 | /* if we are not expanding this is a proper write request, and |
| 2187 | * there will be bios with new data to be drained into the |
| 2188 | * stripe cache |
| 2189 | */ |
| 2190 | if (!expand) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2191 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 2192 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 2193 | } else |
| 2194 | sh->reconstruct_state = reconstruct_state_run; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2195 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2196 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2197 | |
| 2198 | for (i = disks; i--; ) { |
| 2199 | struct r5dev *dev = &sh->dev[i]; |
| 2200 | |
| 2201 | if (dev->towrite) { |
| 2202 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2203 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2204 | if (!expand) |
| 2205 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2206 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2207 | } |
| 2208 | } |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2209 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2210 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2211 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2212 | } else { |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2213 | BUG_ON(level == 6); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2214 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 2215 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
| 2216 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2217 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2218 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 2219 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2220 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2221 | |
| 2222 | for (i = disks; i--; ) { |
| 2223 | struct r5dev *dev = &sh->dev[i]; |
| 2224 | if (i == pd_idx) |
| 2225 | continue; |
| 2226 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2227 | if (dev->towrite && |
| 2228 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2229 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2230 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2231 | set_bit(R5_LOCKED, &dev->flags); |
| 2232 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2233 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2234 | } |
| 2235 | } |
| 2236 | } |
| 2237 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2238 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2239 | * are in flight |
| 2240 | */ |
| 2241 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 2242 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2243 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2244 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2245 | if (level == 6) { |
| 2246 | int qd_idx = sh->qd_idx; |
| 2247 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 2248 | |
| 2249 | set_bit(R5_LOCKED, &dev->flags); |
| 2250 | clear_bit(R5_UPTODATE, &dev->flags); |
| 2251 | s->locked++; |
| 2252 | } |
| 2253 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2254 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2255 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2256 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2257 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | /* |
| 2260 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2261 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | * The bi_next chain must be in order. |
| 2263 | */ |
| 2264 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) |
| 2265 | { |
| 2266 | struct bio **bip; |
| 2267 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2268 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2270 | pr_debug("adding bh b#%llu to stripe s#%llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | (unsigned long long)bi->bi_sector, |
| 2272 | (unsigned long long)sh->sector); |
| 2273 | |
| 2274 | |
| 2275 | spin_lock(&sh->lock); |
| 2276 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2277 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | bip = &sh->dev[dd_idx].towrite; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2279 | if (*bip == NULL && sh->dev[dd_idx].written == NULL) |
| 2280 | firstwrite = 1; |
| 2281 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | bip = &sh->dev[dd_idx].toread; |
| 2283 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { |
| 2284 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) |
| 2285 | goto overlap; |
| 2286 | bip = & (*bip)->bi_next; |
| 2287 | } |
| 2288 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) |
| 2289 | goto overlap; |
| 2290 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2291 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | if (*bip) |
| 2293 | bi->bi_next = *bip; |
| 2294 | *bip = bi; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2295 | bi->bi_phys_segments++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | spin_unlock_irq(&conf->device_lock); |
| 2297 | spin_unlock(&sh->lock); |
| 2298 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2299 | 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] | 2300 | (unsigned long long)bi->bi_sector, |
| 2301 | (unsigned long long)sh->sector, dd_idx); |
| 2302 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2303 | if (conf->mddev->bitmap && firstwrite) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2304 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 2305 | STRIPE_SECTORS, 0); |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 2306 | sh->bm_seq = conf->seq_flush+1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2307 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 2308 | } |
| 2309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | if (forwrite) { |
| 2311 | /* check if page is covered */ |
| 2312 | sector_t sector = sh->dev[dd_idx].sector; |
| 2313 | for (bi=sh->dev[dd_idx].towrite; |
| 2314 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
| 2315 | bi && bi->bi_sector <= sector; |
| 2316 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
| 2317 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) |
| 2318 | sector = bi->bi_sector + (bi->bi_size>>9); |
| 2319 | } |
| 2320 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
| 2321 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); |
| 2322 | } |
| 2323 | return 1; |
| 2324 | |
| 2325 | overlap: |
| 2326 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
| 2327 | spin_unlock_irq(&conf->device_lock); |
| 2328 | spin_unlock(&sh->lock); |
| 2329 | return 0; |
| 2330 | } |
| 2331 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2332 | static void end_reshape(raid5_conf_t *conf); |
| 2333 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2334 | static int page_is_zero(struct page *p) |
| 2335 | { |
| 2336 | char *a = page_address(p); |
| 2337 | return ((*(u32*)a) == 0 && |
| 2338 | memcmp(a, a+4, STRIPE_SIZE-4)==0); |
| 2339 | } |
| 2340 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2341 | static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous, |
| 2342 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2343 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2344 | int sectors_per_chunk = |
| 2345 | previous ? (conf->prev_chunk >> 9) |
| 2346 | : (conf->chunk_size >> 9); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2347 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2348 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2349 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2350 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2351 | raid5_compute_sector(conf, |
| 2352 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2353 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2354 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2355 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2356 | } |
| 2357 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2358 | static void |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2359 | handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2360 | struct stripe_head_state *s, int disks, |
| 2361 | struct bio **return_bi) |
| 2362 | { |
| 2363 | int i; |
| 2364 | for (i = disks; i--; ) { |
| 2365 | struct bio *bi; |
| 2366 | int bitmap_end = 0; |
| 2367 | |
| 2368 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
| 2369 | mdk_rdev_t *rdev; |
| 2370 | rcu_read_lock(); |
| 2371 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 2372 | if (rdev && test_bit(In_sync, &rdev->flags)) |
| 2373 | /* multiple read failures in one stripe */ |
| 2374 | md_error(conf->mddev, rdev); |
| 2375 | rcu_read_unlock(); |
| 2376 | } |
| 2377 | spin_lock_irq(&conf->device_lock); |
| 2378 | /* fail all writes first */ |
| 2379 | bi = sh->dev[i].towrite; |
| 2380 | sh->dev[i].towrite = NULL; |
| 2381 | if (bi) { |
| 2382 | s->to_write--; |
| 2383 | bitmap_end = 1; |
| 2384 | } |
| 2385 | |
| 2386 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2387 | wake_up(&conf->wait_for_overlap); |
| 2388 | |
| 2389 | while (bi && bi->bi_sector < |
| 2390 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2391 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 2392 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2393 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2394 | md_write_end(conf->mddev); |
| 2395 | bi->bi_next = *return_bi; |
| 2396 | *return_bi = bi; |
| 2397 | } |
| 2398 | bi = nextbi; |
| 2399 | } |
| 2400 | /* and fail all 'written' */ |
| 2401 | bi = sh->dev[i].written; |
| 2402 | sh->dev[i].written = NULL; |
| 2403 | if (bi) bitmap_end = 1; |
| 2404 | while (bi && bi->bi_sector < |
| 2405 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2406 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 2407 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2408 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2409 | md_write_end(conf->mddev); |
| 2410 | bi->bi_next = *return_bi; |
| 2411 | *return_bi = bi; |
| 2412 | } |
| 2413 | bi = bi2; |
| 2414 | } |
| 2415 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2416 | /* fail any reads if this device is non-operational and |
| 2417 | * the data has not reached the cache yet. |
| 2418 | */ |
| 2419 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
| 2420 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 2421 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2422 | bi = sh->dev[i].toread; |
| 2423 | sh->dev[i].toread = NULL; |
| 2424 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2425 | wake_up(&conf->wait_for_overlap); |
| 2426 | if (bi) s->to_read--; |
| 2427 | while (bi && bi->bi_sector < |
| 2428 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2429 | struct bio *nextbi = |
| 2430 | r5_next_bio(bi, sh->dev[i].sector); |
| 2431 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2432 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2433 | bi->bi_next = *return_bi; |
| 2434 | *return_bi = bi; |
| 2435 | } |
| 2436 | bi = nextbi; |
| 2437 | } |
| 2438 | } |
| 2439 | spin_unlock_irq(&conf->device_lock); |
| 2440 | if (bitmap_end) |
| 2441 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2442 | STRIPE_SECTORS, 0, 0); |
| 2443 | } |
| 2444 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2445 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2446 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2447 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2448 | } |
| 2449 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2450 | /* fetch_block5 - checks the given member device to see if its data needs |
| 2451 | * to be read or computed to satisfy a request. |
| 2452 | * |
| 2453 | * Returns 1 when no more member devices need to be checked, otherwise returns |
| 2454 | * 0 to tell the loop in handle_stripe_fill5 to continue |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2455 | */ |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2456 | static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s, |
| 2457 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2458 | { |
| 2459 | struct r5dev *dev = &sh->dev[disk_idx]; |
| 2460 | struct r5dev *failed_dev = &sh->dev[s->failed_num]; |
| 2461 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2462 | /* is the data in this block needed, and can we get it? */ |
| 2463 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2464 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2465 | (dev->toread || |
| 2466 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2467 | s->syncing || s->expanding || |
| 2468 | (s->failed && |
| 2469 | (failed_dev->toread || |
| 2470 | (failed_dev->towrite && |
| 2471 | !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) { |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2472 | /* We would like to get this block, possibly by computing it, |
| 2473 | * otherwise read it if the backing disk is insync |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2474 | */ |
| 2475 | if ((s->uptodate == disks - 1) && |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2476 | (s->failed && disk_idx == s->failed_num)) { |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2477 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2478 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2479 | set_bit(R5_Wantcompute, &dev->flags); |
| 2480 | sh->ops.target = disk_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2481 | sh->ops.target2 = -1; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2482 | s->req_compute = 1; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2483 | /* Careful: from this point on 'uptodate' is in the eye |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2484 | * of raid_run_ops which services 'compute' operations |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2485 | * before writes. R5_Wantcompute flags a block that will |
| 2486 | * be R5_UPTODATE by the time it is needed for a |
| 2487 | * subsequent operation. |
| 2488 | */ |
| 2489 | s->uptodate++; |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2490 | return 1; /* uptodate + compute == disks */ |
Dan Williams | 7a1fc53 | 2008-07-10 04:54:57 -0700 | [diff] [blame] | 2491 | } else if (test_bit(R5_Insync, &dev->flags)) { |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2492 | set_bit(R5_LOCKED, &dev->flags); |
| 2493 | set_bit(R5_Wantread, &dev->flags); |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2494 | s->locked++; |
| 2495 | pr_debug("Reading block %d (sync=%d)\n", disk_idx, |
| 2496 | s->syncing); |
| 2497 | } |
| 2498 | } |
| 2499 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2500 | return 0; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2501 | } |
| 2502 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2503 | /** |
| 2504 | * handle_stripe_fill5 - read or compute data to satisfy pending requests. |
| 2505 | */ |
| 2506 | static void handle_stripe_fill5(struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2507 | struct stripe_head_state *s, int disks) |
| 2508 | { |
| 2509 | int i; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2510 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2511 | /* look for blocks to read/compute, skip this if a compute |
| 2512 | * is already in flight, or if the stripe contents are in the |
| 2513 | * midst of changing due to a write |
| 2514 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2515 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2516 | !sh->reconstruct_state) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2517 | for (i = disks; i--; ) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2518 | if (fetch_block5(sh, s, i, disks)) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2519 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2520 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2521 | } |
| 2522 | |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2523 | /* fetch_block6 - checks the given member device to see if its data needs |
| 2524 | * to be read or computed to satisfy a request. |
| 2525 | * |
| 2526 | * Returns 1 when no more member devices need to be checked, otherwise returns |
| 2527 | * 0 to tell the loop in handle_stripe_fill6 to continue |
| 2528 | */ |
| 2529 | static int fetch_block6(struct stripe_head *sh, struct stripe_head_state *s, |
| 2530 | struct r6_state *r6s, int disk_idx, int disks) |
| 2531 | { |
| 2532 | struct r5dev *dev = &sh->dev[disk_idx]; |
| 2533 | struct r5dev *fdev[2] = { &sh->dev[r6s->failed_num[0]], |
| 2534 | &sh->dev[r6s->failed_num[1]] }; |
| 2535 | |
| 2536 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 2537 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2538 | (dev->toread || |
| 2539 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2540 | s->syncing || s->expanding || |
| 2541 | (s->failed >= 1 && |
| 2542 | (fdev[0]->toread || s->to_write)) || |
| 2543 | (s->failed >= 2 && |
| 2544 | (fdev[1]->toread || s->to_write)))) { |
| 2545 | /* we would like to get this block, possibly by computing it, |
| 2546 | * otherwise read it if the backing disk is insync |
| 2547 | */ |
| 2548 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 2549 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
| 2550 | if ((s->uptodate == disks - 1) && |
| 2551 | (s->failed && (disk_idx == r6s->failed_num[0] || |
| 2552 | disk_idx == r6s->failed_num[1]))) { |
| 2553 | /* have disk failed, and we're requested to fetch it; |
| 2554 | * do compute it |
| 2555 | */ |
| 2556 | pr_debug("Computing stripe %llu block %d\n", |
| 2557 | (unsigned long long)sh->sector, disk_idx); |
| 2558 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2559 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2560 | set_bit(R5_Wantcompute, &dev->flags); |
| 2561 | sh->ops.target = disk_idx; |
| 2562 | sh->ops.target2 = -1; /* no 2nd target */ |
| 2563 | s->req_compute = 1; |
| 2564 | s->uptodate++; |
| 2565 | return 1; |
| 2566 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 2567 | /* Computing 2-failure is *very* expensive; only |
| 2568 | * do it if failed >= 2 |
| 2569 | */ |
| 2570 | int other; |
| 2571 | for (other = disks; other--; ) { |
| 2572 | if (other == disk_idx) |
| 2573 | continue; |
| 2574 | if (!test_bit(R5_UPTODATE, |
| 2575 | &sh->dev[other].flags)) |
| 2576 | break; |
| 2577 | } |
| 2578 | BUG_ON(other < 0); |
| 2579 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 2580 | (unsigned long long)sh->sector, |
| 2581 | disk_idx, other); |
| 2582 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2583 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2584 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 2585 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 2586 | sh->ops.target = disk_idx; |
| 2587 | sh->ops.target2 = other; |
| 2588 | s->uptodate += 2; |
| 2589 | s->req_compute = 1; |
| 2590 | return 1; |
| 2591 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 2592 | set_bit(R5_LOCKED, &dev->flags); |
| 2593 | set_bit(R5_Wantread, &dev->flags); |
| 2594 | s->locked++; |
| 2595 | pr_debug("Reading block %d (sync=%d)\n", |
| 2596 | disk_idx, s->syncing); |
| 2597 | } |
| 2598 | } |
| 2599 | |
| 2600 | return 0; |
| 2601 | } |
| 2602 | |
| 2603 | /** |
| 2604 | * handle_stripe_fill6 - read or compute data to satisfy pending requests. |
| 2605 | */ |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2606 | static void handle_stripe_fill6(struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2607 | struct stripe_head_state *s, struct r6_state *r6s, |
| 2608 | int disks) |
| 2609 | { |
| 2610 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2611 | |
| 2612 | /* look for blocks to read/compute, skip this if a compute |
| 2613 | * is already in flight, or if the stripe contents are in the |
| 2614 | * midst of changing due to a write |
| 2615 | */ |
| 2616 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
| 2617 | !sh->reconstruct_state) |
| 2618 | for (i = disks; i--; ) |
| 2619 | if (fetch_block6(sh, s, r6s, i, disks)) |
| 2620 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2621 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2622 | } |
| 2623 | |
| 2624 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2625 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2626 | * any written block on an uptodate or failed drive can be returned. |
| 2627 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 2628 | * never LOCKED, so we don't need to test 'failed' directly. |
| 2629 | */ |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2630 | static void handle_stripe_clean_event(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2631 | struct stripe_head *sh, int disks, struct bio **return_bi) |
| 2632 | { |
| 2633 | int i; |
| 2634 | struct r5dev *dev; |
| 2635 | |
| 2636 | for (i = disks; i--; ) |
| 2637 | if (sh->dev[i].written) { |
| 2638 | dev = &sh->dev[i]; |
| 2639 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 2640 | test_bit(R5_UPTODATE, &dev->flags)) { |
| 2641 | /* We can return any write requests */ |
| 2642 | struct bio *wbi, *wbi2; |
| 2643 | int bitmap_end = 0; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2644 | pr_debug("Return write for disc %d\n", i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2645 | spin_lock_irq(&conf->device_lock); |
| 2646 | wbi = dev->written; |
| 2647 | dev->written = NULL; |
| 2648 | while (wbi && wbi->bi_sector < |
| 2649 | dev->sector + STRIPE_SECTORS) { |
| 2650 | wbi2 = r5_next_bio(wbi, dev->sector); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2651 | if (!raid5_dec_bi_phys_segments(wbi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2652 | md_write_end(conf->mddev); |
| 2653 | wbi->bi_next = *return_bi; |
| 2654 | *return_bi = wbi; |
| 2655 | } |
| 2656 | wbi = wbi2; |
| 2657 | } |
| 2658 | if (dev->towrite == NULL) |
| 2659 | bitmap_end = 1; |
| 2660 | spin_unlock_irq(&conf->device_lock); |
| 2661 | if (bitmap_end) |
| 2662 | bitmap_endwrite(conf->mddev->bitmap, |
| 2663 | sh->sector, |
| 2664 | STRIPE_SECTORS, |
| 2665 | !test_bit(STRIPE_DEGRADED, &sh->state), |
| 2666 | 0); |
| 2667 | } |
| 2668 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2669 | |
| 2670 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2671 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2672 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2673 | } |
| 2674 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2675 | static void handle_stripe_dirtying5(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2676 | struct stripe_head *sh, struct stripe_head_state *s, int disks) |
| 2677 | { |
| 2678 | int rmw = 0, rcw = 0, i; |
| 2679 | for (i = disks; i--; ) { |
| 2680 | /* would I have to read this buffer for read_modify_write */ |
| 2681 | struct r5dev *dev = &sh->dev[i]; |
| 2682 | if ((dev->towrite || i == sh->pd_idx) && |
| 2683 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2684 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2685 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2686 | if (test_bit(R5_Insync, &dev->flags)) |
| 2687 | rmw++; |
| 2688 | else |
| 2689 | rmw += 2*disks; /* cannot read it */ |
| 2690 | } |
| 2691 | /* Would I have to read this buffer for reconstruct_write */ |
| 2692 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 2693 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2694 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2695 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2696 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2697 | else |
| 2698 | rcw += 2*disks; |
| 2699 | } |
| 2700 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2701 | pr_debug("for sector %llu, rmw=%d rcw=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2702 | (unsigned long long)sh->sector, rmw, rcw); |
| 2703 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2704 | if (rmw < rcw && rmw > 0) |
| 2705 | /* prefer read-modify-write, but need to get some data */ |
| 2706 | for (i = disks; i--; ) { |
| 2707 | struct r5dev *dev = &sh->dev[i]; |
| 2708 | if ((dev->towrite || i == sh->pd_idx) && |
| 2709 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2710 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2711 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2712 | test_bit(R5_Insync, &dev->flags)) { |
| 2713 | if ( |
| 2714 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2715 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2716 | "%d for r-m-w\n", i); |
| 2717 | set_bit(R5_LOCKED, &dev->flags); |
| 2718 | set_bit(R5_Wantread, &dev->flags); |
| 2719 | s->locked++; |
| 2720 | } else { |
| 2721 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2722 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2723 | } |
| 2724 | } |
| 2725 | } |
| 2726 | if (rcw <= rmw && rcw > 0) |
| 2727 | /* want reconstruct write, but need to get some data */ |
| 2728 | for (i = disks; i--; ) { |
| 2729 | struct r5dev *dev = &sh->dev[i]; |
| 2730 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 2731 | i != sh->pd_idx && |
| 2732 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2733 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2734 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2735 | test_bit(R5_Insync, &dev->flags)) { |
| 2736 | if ( |
| 2737 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2738 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2739 | "%d for Reconstruct\n", i); |
| 2740 | set_bit(R5_LOCKED, &dev->flags); |
| 2741 | set_bit(R5_Wantread, &dev->flags); |
| 2742 | s->locked++; |
| 2743 | } else { |
| 2744 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2745 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2746 | } |
| 2747 | } |
| 2748 | } |
| 2749 | /* now if nothing is locked, and if we have enough data, |
| 2750 | * we can start a write request |
| 2751 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2752 | /* since handle_stripe can be called at any time we need to handle the |
| 2753 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2754 | * subsequent call wants to start a write request. raid_run_ops only |
| 2755 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2756 | * simultaneously. If this is not the case then new writes need to be |
| 2757 | * held off until the compute completes. |
| 2758 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2759 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2760 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
| 2761 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2762 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2763 | } |
| 2764 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2765 | static void handle_stripe_dirtying6(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2766 | struct stripe_head *sh, struct stripe_head_state *s, |
| 2767 | struct r6_state *r6s, int disks) |
| 2768 | { |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2769 | int rcw = 0, pd_idx = sh->pd_idx, i; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2770 | int qd_idx = sh->qd_idx; |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2771 | |
| 2772 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2773 | for (i = disks; i--; ) { |
| 2774 | struct r5dev *dev = &sh->dev[i]; |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2775 | /* check if we haven't enough data */ |
| 2776 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 2777 | i != pd_idx && i != qd_idx && |
| 2778 | !test_bit(R5_LOCKED, &dev->flags) && |
| 2779 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2780 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2781 | rcw++; |
| 2782 | if (!test_bit(R5_Insync, &dev->flags)) |
| 2783 | continue; /* it's a failed drive */ |
| 2784 | |
| 2785 | if ( |
| 2786 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 2787 | pr_debug("Read_old stripe %llu " |
| 2788 | "block %d for Reconstruct\n", |
| 2789 | (unsigned long long)sh->sector, i); |
| 2790 | set_bit(R5_LOCKED, &dev->flags); |
| 2791 | set_bit(R5_Wantread, &dev->flags); |
| 2792 | s->locked++; |
| 2793 | } else { |
| 2794 | pr_debug("Request delayed stripe %llu " |
| 2795 | "block %d for Reconstruct\n", |
| 2796 | (unsigned long long)sh->sector, i); |
| 2797 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2798 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2799 | } |
| 2800 | } |
| 2801 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2802 | /* now if nothing is locked, and if we have enough data, we can start a |
| 2803 | * write request |
| 2804 | */ |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2805 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2806 | s->locked == 0 && rcw == 0 && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2807 | !test_bit(STRIPE_BIT_DELAY, &sh->state)) { |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2808 | schedule_reconstruction(sh, s, 1, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2809 | } |
| 2810 | } |
| 2811 | |
| 2812 | static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh, |
| 2813 | struct stripe_head_state *s, int disks) |
| 2814 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2815 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2816 | |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2817 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2818 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2819 | switch (sh->check_state) { |
| 2820 | case check_state_idle: |
| 2821 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2822 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2823 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2824 | sh->check_state = check_state_run; |
| 2825 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2826 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2827 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2828 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2829 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2830 | dev = &sh->dev[s->failed_num]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2831 | /* fall through */ |
| 2832 | case check_state_compute_result: |
| 2833 | sh->check_state = check_state_idle; |
| 2834 | if (!dev) |
| 2835 | dev = &sh->dev[sh->pd_idx]; |
| 2836 | |
| 2837 | /* check that a write has not made the stripe insync */ |
| 2838 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2839 | break; |
| 2840 | |
| 2841 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2842 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 2843 | BUG_ON(s->uptodate != disks); |
| 2844 | |
| 2845 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2846 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2847 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2848 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2849 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2850 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2851 | break; |
| 2852 | case check_state_run: |
| 2853 | break; /* we will be called again upon completion */ |
| 2854 | case check_state_check_result: |
| 2855 | sh->check_state = check_state_idle; |
| 2856 | |
| 2857 | /* if a failure occurred during the check operation, leave |
| 2858 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 2859 | */ |
| 2860 | if (s->failed) |
| 2861 | break; |
| 2862 | |
| 2863 | /* handle a successful check operation, if parity is correct |
| 2864 | * we are done. Otherwise update the mismatch count and repair |
| 2865 | * parity if !MD_RECOVERY_CHECK |
| 2866 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 2867 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2868 | /* parity is correct (on disc, |
| 2869 | * not in buffer any more) |
| 2870 | */ |
| 2871 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2872 | else { |
| 2873 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2874 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2875 | /* don't try to repair!! */ |
| 2876 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2877 | else { |
| 2878 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2879 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2880 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2881 | set_bit(R5_Wantcompute, |
| 2882 | &sh->dev[sh->pd_idx].flags); |
| 2883 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2884 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2885 | s->uptodate++; |
| 2886 | } |
| 2887 | } |
| 2888 | break; |
| 2889 | case check_state_compute_run: |
| 2890 | break; |
| 2891 | default: |
| 2892 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 2893 | __func__, sh->check_state, |
| 2894 | (unsigned long long) sh->sector); |
| 2895 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2896 | } |
| 2897 | } |
| 2898 | |
| 2899 | |
| 2900 | 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] | 2901 | struct stripe_head_state *s, |
| 2902 | struct r6_state *r6s, int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2903 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2904 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2905 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame^] | 2906 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2907 | |
| 2908 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2909 | |
| 2910 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame^] | 2911 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2912 | /* Want to check and possibly repair P and Q. |
| 2913 | * However there could be one 'failed' device, in which |
| 2914 | * case we can only check one of them, possibly using the |
| 2915 | * other to generate missing data |
| 2916 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame^] | 2917 | |
| 2918 | switch (sh->check_state) { |
| 2919 | case check_state_idle: |
| 2920 | /* start a new check operation if there are < 2 failures */ |
| 2921 | if (s->failed == r6s->q_failed) { |
| 2922 | /* The only possible failed device holds Q, so it |
| 2923 | * makes sense to check P (If anything else were failed, |
| 2924 | * we would have used P to recreate it). |
| 2925 | */ |
| 2926 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2927 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame^] | 2928 | if (!r6s->q_failed && s->failed < 2) { |
| 2929 | /* Q is not failed, and we didn't use it to generate |
| 2930 | * anything, so it makes sense to check it |
| 2931 | */ |
| 2932 | if (sh->check_state == check_state_run) |
| 2933 | sh->check_state = check_state_run_pq; |
| 2934 | else |
| 2935 | sh->check_state = check_state_run_q; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2936 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2937 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame^] | 2938 | /* discard potentially stale zero_sum_result */ |
| 2939 | sh->ops.zero_sum_result = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2940 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame^] | 2941 | if (sh->check_state == check_state_run) { |
| 2942 | /* async_xor_zero_sum destroys the contents of P */ |
| 2943 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 2944 | s->uptodate--; |
| 2945 | } |
| 2946 | if (sh->check_state >= check_state_run && |
| 2947 | sh->check_state <= check_state_run_pq) { |
| 2948 | /* async_syndrome_zero_sum preserves P and Q, so |
| 2949 | * no need to mark them !uptodate here |
| 2950 | */ |
| 2951 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 2952 | break; |
| 2953 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2954 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame^] | 2955 | /* we have 2-disk failure */ |
| 2956 | BUG_ON(s->failed != 2); |
| 2957 | /* fall through */ |
| 2958 | case check_state_compute_result: |
| 2959 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2960 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame^] | 2961 | /* check that a write has not made the stripe insync */ |
| 2962 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2963 | break; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2964 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame^] | 2965 | /* now write out any block on a failed drive, |
| 2966 | * or P or Q if they were recomputed |
| 2967 | */ |
| 2968 | BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */ |
| 2969 | if (s->failed == 2) { |
| 2970 | dev = &sh->dev[r6s->failed_num[1]]; |
| 2971 | s->locked++; |
| 2972 | set_bit(R5_LOCKED, &dev->flags); |
| 2973 | set_bit(R5_Wantwrite, &dev->flags); |
| 2974 | } |
| 2975 | if (s->failed >= 1) { |
| 2976 | dev = &sh->dev[r6s->failed_num[0]]; |
| 2977 | s->locked++; |
| 2978 | set_bit(R5_LOCKED, &dev->flags); |
| 2979 | set_bit(R5_Wantwrite, &dev->flags); |
| 2980 | } |
| 2981 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 2982 | dev = &sh->dev[pd_idx]; |
| 2983 | s->locked++; |
| 2984 | set_bit(R5_LOCKED, &dev->flags); |
| 2985 | set_bit(R5_Wantwrite, &dev->flags); |
| 2986 | } |
| 2987 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 2988 | dev = &sh->dev[qd_idx]; |
| 2989 | s->locked++; |
| 2990 | set_bit(R5_LOCKED, &dev->flags); |
| 2991 | set_bit(R5_Wantwrite, &dev->flags); |
| 2992 | } |
| 2993 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 2994 | |
| 2995 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2996 | break; |
| 2997 | case check_state_run: |
| 2998 | case check_state_run_q: |
| 2999 | case check_state_run_pq: |
| 3000 | break; /* we will be called again upon completion */ |
| 3001 | case check_state_check_result: |
| 3002 | sh->check_state = check_state_idle; |
| 3003 | |
| 3004 | /* handle a successful check operation, if parity is correct |
| 3005 | * we are done. Otherwise update the mismatch count and repair |
| 3006 | * parity if !MD_RECOVERY_CHECK |
| 3007 | */ |
| 3008 | if (sh->ops.zero_sum_result == 0) { |
| 3009 | /* both parities are correct */ |
| 3010 | if (!s->failed) |
| 3011 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3012 | else { |
| 3013 | /* in contrast to the raid5 case we can validate |
| 3014 | * parity, but still have a failure to write |
| 3015 | * back |
| 3016 | */ |
| 3017 | sh->check_state = check_state_compute_result; |
| 3018 | /* Returning at this point means that we may go |
| 3019 | * off and bring p and/or q uptodate again so |
| 3020 | * we make sure to check zero_sum_result again |
| 3021 | * to verify if p or q need writeback |
| 3022 | */ |
| 3023 | } |
| 3024 | } else { |
| 3025 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 3026 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 3027 | /* don't try to repair!! */ |
| 3028 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3029 | else { |
| 3030 | int *target = &sh->ops.target; |
| 3031 | |
| 3032 | sh->ops.target = -1; |
| 3033 | sh->ops.target2 = -1; |
| 3034 | sh->check_state = check_state_compute_run; |
| 3035 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3036 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3037 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 3038 | set_bit(R5_Wantcompute, |
| 3039 | &sh->dev[pd_idx].flags); |
| 3040 | *target = pd_idx; |
| 3041 | target = &sh->ops.target2; |
| 3042 | s->uptodate++; |
| 3043 | } |
| 3044 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 3045 | set_bit(R5_Wantcompute, |
| 3046 | &sh->dev[qd_idx].flags); |
| 3047 | *target = qd_idx; |
| 3048 | s->uptodate++; |
| 3049 | } |
| 3050 | } |
| 3051 | } |
| 3052 | break; |
| 3053 | case check_state_compute_run: |
| 3054 | break; |
| 3055 | default: |
| 3056 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 3057 | __func__, sh->check_state, |
| 3058 | (unsigned long long) sh->sector); |
| 3059 | BUG(); |
| 3060 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3061 | } |
| 3062 | |
| 3063 | static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh, |
| 3064 | struct r6_state *r6s) |
| 3065 | { |
| 3066 | int i; |
| 3067 | |
| 3068 | /* We have read all the blocks in this stripe and now we need to |
| 3069 | * copy some of them into a target stripe for expand. |
| 3070 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3071 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3072 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3073 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3074 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3075 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3076 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3077 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3078 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3079 | sector_t bn = compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3080 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 3081 | &dd_idx, NULL); |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3082 | sh2 = get_active_stripe(conf, s, 0, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3083 | if (sh2 == NULL) |
| 3084 | /* so far only the early blocks of this stripe |
| 3085 | * have been requested. When later blocks |
| 3086 | * get requested, we will try again |
| 3087 | */ |
| 3088 | continue; |
| 3089 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 3090 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 3091 | /* must have already done this block */ |
| 3092 | release_stripe(sh2); |
| 3093 | continue; |
| 3094 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3095 | |
| 3096 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3097 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3098 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 3099 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3100 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3101 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3102 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 3103 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 3104 | for (j = 0; j < conf->raid_disks; j++) |
| 3105 | if (j != sh2->pd_idx && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3106 | (!r6s || j != sh2->qd_idx) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3107 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 3108 | break; |
| 3109 | if (j == conf->raid_disks) { |
| 3110 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 3111 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 3112 | } |
| 3113 | release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3114 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3115 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 3116 | /* done submitting copies, wait for them to complete */ |
| 3117 | if (tx) { |
| 3118 | async_tx_ack(tx); |
| 3119 | dma_wait_for_async_tx(tx); |
| 3120 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3121 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3122 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3124 | /* |
| 3125 | * handle_stripe - do things to a stripe. |
| 3126 | * |
| 3127 | * We lock the stripe and then examine the state of various bits |
| 3128 | * to see what needs to be done. |
| 3129 | * Possible results: |
| 3130 | * return some read request which now have data |
| 3131 | * return some write requests which are safely on disc |
| 3132 | * schedule a read on some buffers |
| 3133 | * schedule a write of some buffers |
| 3134 | * return confirmation of parity correctness |
| 3135 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3136 | * buffers are taken off read_list or write_list, and bh_cache buffers |
| 3137 | * get BH_Lock set before the stripe lock is released. |
| 3138 | * |
| 3139 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3140 | |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 3141 | static bool handle_stripe5(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | { |
| 3143 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3144 | int disks = sh->disks, i; |
| 3145 | struct bio *return_bi = NULL; |
| 3146 | struct stripe_head_state s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | struct r5dev *dev; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3148 | mdk_rdev_t *blocked_rdev = NULL; |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 3149 | int prexor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3151 | memset(&s, 0, sizeof(s)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3152 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d " |
| 3153 | "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state, |
| 3154 | atomic_read(&sh->count), sh->pd_idx, sh->check_state, |
| 3155 | sh->reconstruct_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | |
| 3157 | spin_lock(&sh->lock); |
| 3158 | clear_bit(STRIPE_HANDLE, &sh->state); |
| 3159 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3160 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3161 | s.syncing = test_bit(STRIPE_SYNCING, &sh->state); |
| 3162 | s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3163 | s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 3164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3165 | /* Now to look around and see what can be done */ |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 3166 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | for (i=disks; i--; ) { |
| 3168 | mdk_rdev_t *rdev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3169 | struct r5dev *dev = &sh->dev[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3170 | clear_bit(R5_Insync, &dev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3172 | pr_debug("check %d: state 0x%lx toread %p read %p write %p " |
| 3173 | "written %p\n", i, dev->flags, dev->toread, dev->read, |
| 3174 | dev->towrite, dev->written); |
| 3175 | |
| 3176 | /* maybe we can request a biofill operation |
| 3177 | * |
| 3178 | * new wantfill requests are only permitted while |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 3179 | * ops_complete_biofill is guaranteed to be inactive |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3180 | */ |
| 3181 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 3182 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3183 | set_bit(R5_Wantfill, &dev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3184 | |
| 3185 | /* now count some things */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3186 | if (test_bit(R5_LOCKED, &dev->flags)) s.locked++; |
| 3187 | if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3188 | if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3189 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3190 | if (test_bit(R5_Wantfill, &dev->flags)) |
| 3191 | s.to_fill++; |
| 3192 | else if (dev->toread) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3193 | s.to_read++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3194 | if (dev->towrite) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3195 | s.to_write++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3196 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3197 | s.non_overwrite++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3199 | if (dev->written) |
| 3200 | s.written++; |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 3201 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3202 | if (blocked_rdev == NULL && |
| 3203 | rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3204 | blocked_rdev = rdev; |
| 3205 | atomic_inc(&rdev->nr_pending); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3206 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3207 | if (!rdev || !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 3208 | /* The ReadError flag will just be confusing now */ |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3209 | clear_bit(R5_ReadError, &dev->flags); |
| 3210 | clear_bit(R5_ReWrite, &dev->flags); |
| 3211 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3212 | if (!rdev || !test_bit(In_sync, &rdev->flags) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3213 | || test_bit(R5_ReadError, &dev->flags)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3214 | s.failed++; |
| 3215 | s.failed_num = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3216 | } else |
| 3217 | set_bit(R5_Insync, &dev->flags); |
| 3218 | } |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 3219 | rcu_read_unlock(); |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3220 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3221 | if (unlikely(blocked_rdev)) { |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3222 | if (s.syncing || s.expanding || s.expanded || |
| 3223 | s.to_write || s.written) { |
| 3224 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3225 | goto unlock; |
| 3226 | } |
| 3227 | /* There is nothing for the blocked_rdev to block */ |
| 3228 | rdev_dec_pending(blocked_rdev, conf->mddev); |
| 3229 | blocked_rdev = NULL; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3230 | } |
| 3231 | |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 3232 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 3233 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 3234 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 3235 | } |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3236 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3237 | pr_debug("locked=%d uptodate=%d to_read=%d" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3238 | " to_write=%d failed=%d failed_num=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3239 | s.locked, s.uptodate, s.to_read, s.to_write, |
| 3240 | s.failed, s.failed_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3241 | /* check if the array has lost two devices and, if so, some requests might |
| 3242 | * need to be failed |
| 3243 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3244 | if (s.failed > 1 && s.to_read+s.to_write+s.written) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3245 | handle_failed_stripe(conf, sh, &s, disks, &return_bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3246 | if (s.failed > 1 && s.syncing) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3247 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); |
| 3248 | clear_bit(STRIPE_SYNCING, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3249 | s.syncing = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3250 | } |
| 3251 | |
| 3252 | /* might be able to return some write requests if the parity block |
| 3253 | * is safe, or on a failed drive |
| 3254 | */ |
| 3255 | dev = &sh->dev[sh->pd_idx]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3256 | if ( s.written && |
| 3257 | ((test_bit(R5_Insync, &dev->flags) && |
| 3258 | !test_bit(R5_LOCKED, &dev->flags) && |
| 3259 | test_bit(R5_UPTODATE, &dev->flags)) || |
| 3260 | (s.failed == 1 && s.failed_num == sh->pd_idx))) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3261 | handle_stripe_clean_event(conf, sh, disks, &return_bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | |
| 3263 | /* Now we might consider reading some blocks, either to check/generate |
| 3264 | * parity, or to satisfy requests |
| 3265 | * or to load a block that is being partially written. |
| 3266 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3267 | if (s.to_read || s.non_overwrite || |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3268 | (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3269 | handle_stripe_fill5(sh, &s, disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3271 | /* Now we check to see if any write operations have recently |
| 3272 | * completed |
| 3273 | */ |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 3274 | prexor = 0; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 3275 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 3276 | prexor = 1; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 3277 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 3278 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3279 | sh->reconstruct_state = reconstruct_state_idle; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3280 | |
| 3281 | /* All the 'written' buffers and the parity block are ready to |
| 3282 | * be written back to disk |
| 3283 | */ |
| 3284 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags)); |
| 3285 | for (i = disks; i--; ) { |
| 3286 | dev = &sh->dev[i]; |
| 3287 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 3288 | (i == sh->pd_idx || dev->written)) { |
| 3289 | pr_debug("Writing block %d\n", i); |
| 3290 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 3291 | if (prexor) |
| 3292 | continue; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3293 | if (!test_bit(R5_Insync, &dev->flags) || |
| 3294 | (i == sh->pd_idx && s.failed == 0)) |
| 3295 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3296 | } |
| 3297 | } |
| 3298 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 3299 | atomic_dec(&conf->preread_active_stripes); |
| 3300 | if (atomic_read(&conf->preread_active_stripes) < |
| 3301 | IO_THRESHOLD) |
| 3302 | md_wakeup_thread(conf->mddev->thread); |
| 3303 | } |
| 3304 | } |
| 3305 | |
| 3306 | /* Now to consider new write requests and what else, if anything |
| 3307 | * should be read. We do not handle new writes when: |
| 3308 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 3309 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 3310 | * block. |
| 3311 | */ |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3312 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3313 | handle_stripe_dirtying5(conf, sh, &s, disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3314 | |
| 3315 | /* 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] | 3316 | * Any reads will already have been scheduled, so we just see if enough |
| 3317 | * data is available. The parity check is held off while parity |
| 3318 | * dependent operations are in flight. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 | */ |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3320 | if (sh->check_state || |
| 3321 | (s.syncing && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3322 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3323 | !test_bit(STRIPE_INSYNC, &sh->state))) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3324 | handle_parity_checks5(conf, sh, &s, disks); |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3325 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3326 | if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3327 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); |
| 3328 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 3329 | } |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3330 | |
| 3331 | /* If the failed drive is just a ReadError, then we might need to progress |
| 3332 | * the repair/check process |
| 3333 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3334 | if (s.failed == 1 && !conf->mddev->ro && |
| 3335 | test_bit(R5_ReadError, &sh->dev[s.failed_num].flags) |
| 3336 | && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags) |
| 3337 | && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3338 | ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3339 | dev = &sh->dev[s.failed_num]; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3340 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3341 | set_bit(R5_Wantwrite, &dev->flags); |
| 3342 | set_bit(R5_ReWrite, &dev->flags); |
| 3343 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3344 | s.locked++; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3345 | } else { |
| 3346 | /* let's read it back */ |
| 3347 | set_bit(R5_Wantread, &dev->flags); |
| 3348 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3349 | s.locked++; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3350 | } |
| 3351 | } |
| 3352 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3353 | /* Finish reconstruct operations initiated by the expansion process */ |
| 3354 | if (sh->reconstruct_state == reconstruct_state_result) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3355 | struct stripe_head *sh2 |
| 3356 | = get_active_stripe(conf, sh->sector, 1, 1); |
| 3357 | if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) { |
| 3358 | /* sh cannot be written until sh2 has been read. |
| 3359 | * so arrange for sh to be delayed a little |
| 3360 | */ |
| 3361 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3362 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3363 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3364 | &sh2->state)) |
| 3365 | atomic_inc(&conf->preread_active_stripes); |
| 3366 | release_stripe(sh2); |
| 3367 | goto unlock; |
| 3368 | } |
| 3369 | if (sh2) |
| 3370 | release_stripe(sh2); |
| 3371 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3372 | sh->reconstruct_state = reconstruct_state_idle; |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3373 | clear_bit(STRIPE_EXPANDING, &sh->state); |
Dan Williams | 2339788 | 2008-07-23 20:05:34 -0700 | [diff] [blame] | 3374 | for (i = conf->raid_disks; i--; ) { |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3375 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
Dan Williams | 2339788 | 2008-07-23 20:05:34 -0700 | [diff] [blame] | 3376 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
Neil Brown | efe3114 | 2008-06-28 08:31:14 +1000 | [diff] [blame] | 3377 | s.locked++; |
Dan Williams | 2339788 | 2008-07-23 20:05:34 -0700 | [diff] [blame] | 3378 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3382 | !sh->reconstruct_state) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3383 | /* Need to write out all blocks after computing parity */ |
| 3384 | sh->disks = conf->raid_disks; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3385 | stripe_set_idx(sh->sector, conf, 0, sh); |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3386 | schedule_reconstruction(sh, &s, 1, 1); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3387 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3388 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3389 | atomic_dec(&conf->reshape_stripes); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3390 | wake_up(&conf->wait_for_overlap); |
| 3391 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3392 | } |
| 3393 | |
Dan Williams | 0f94e87 | 2008-01-08 15:32:53 -0800 | [diff] [blame] | 3394 | if (s.expanding && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3395 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3396 | handle_stripe_expansion(conf, sh, NULL); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3397 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3398 | unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3399 | spin_unlock(&sh->lock); |
| 3400 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3401 | /* wait for this device to become unblocked */ |
| 3402 | if (unlikely(blocked_rdev)) |
| 3403 | md_wait_for_blocked_rdev(blocked_rdev, conf->mddev); |
| 3404 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3405 | if (s.ops_request) |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 3406 | raid_run_ops(sh, s.ops_request); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3407 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 3408 | ops_run_io(sh, &s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3410 | return_io(return_bi); |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 3411 | |
| 3412 | return blocked_rdev == NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | } |
| 3414 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3415 | static bool handle_stripe6(struct stripe_head *sh) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3416 | { |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3417 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3418 | int disks = sh->disks; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3419 | struct bio *return_bi = NULL; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3420 | int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3421 | struct stripe_head_state s; |
| 3422 | struct r6_state r6s; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3423 | struct r5dev *dev, *pdev, *qdev; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3424 | mdk_rdev_t *blocked_rdev = NULL; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3425 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3426 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3427 | "pd_idx=%d, qd_idx=%d\n", |
| 3428 | (unsigned long long)sh->sector, sh->state, |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3429 | atomic_read(&sh->count), pd_idx, qd_idx); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3430 | memset(&s, 0, sizeof(s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3431 | |
| 3432 | spin_lock(&sh->lock); |
| 3433 | clear_bit(STRIPE_HANDLE, &sh->state); |
| 3434 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3435 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3436 | s.syncing = test_bit(STRIPE_SYNCING, &sh->state); |
| 3437 | s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3438 | s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3439 | /* Now to look around and see what can be done */ |
| 3440 | |
| 3441 | rcu_read_lock(); |
| 3442 | for (i=disks; i--; ) { |
| 3443 | mdk_rdev_t *rdev; |
| 3444 | dev = &sh->dev[i]; |
| 3445 | clear_bit(R5_Insync, &dev->flags); |
| 3446 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3447 | 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] | 3448 | i, dev->flags, dev->toread, dev->towrite, dev->written); |
| 3449 | /* maybe we can reply to a read */ |
| 3450 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) { |
| 3451 | struct bio *rbi, *rbi2; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3452 | pr_debug("Return read for disc %d\n", i); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3453 | spin_lock_irq(&conf->device_lock); |
| 3454 | rbi = dev->toread; |
| 3455 | dev->toread = NULL; |
| 3456 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 3457 | wake_up(&conf->wait_for_overlap); |
| 3458 | spin_unlock_irq(&conf->device_lock); |
| 3459 | while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) { |
| 3460 | copy_data(0, rbi, dev->page, dev->sector); |
| 3461 | rbi2 = r5_next_bio(rbi, dev->sector); |
| 3462 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3463 | if (!raid5_dec_bi_phys_segments(rbi)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3464 | rbi->bi_next = return_bi; |
| 3465 | return_bi = rbi; |
| 3466 | } |
| 3467 | spin_unlock_irq(&conf->device_lock); |
| 3468 | rbi = rbi2; |
| 3469 | } |
| 3470 | } |
| 3471 | |
| 3472 | /* now count some things */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3473 | if (test_bit(R5_LOCKED, &dev->flags)) s.locked++; |
| 3474 | if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3475 | |
| 3476 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3477 | if (dev->toread) |
| 3478 | s.to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3479 | if (dev->towrite) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3480 | s.to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3481 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3482 | s.non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3483 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3484 | if (dev->written) |
| 3485 | s.written++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3486 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3487 | if (blocked_rdev == NULL && |
| 3488 | rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3489 | blocked_rdev = rdev; |
| 3490 | atomic_inc(&rdev->nr_pending); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3491 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3492 | if (!rdev || !test_bit(In_sync, &rdev->flags)) { |
| 3493 | /* The ReadError flag will just be confusing now */ |
| 3494 | clear_bit(R5_ReadError, &dev->flags); |
| 3495 | clear_bit(R5_ReWrite, &dev->flags); |
| 3496 | } |
| 3497 | if (!rdev || !test_bit(In_sync, &rdev->flags) |
| 3498 | || test_bit(R5_ReadError, &dev->flags)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3499 | if (s.failed < 2) |
| 3500 | r6s.failed_num[s.failed] = i; |
| 3501 | s.failed++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3502 | } else |
| 3503 | set_bit(R5_Insync, &dev->flags); |
| 3504 | } |
| 3505 | rcu_read_unlock(); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3506 | |
| 3507 | if (unlikely(blocked_rdev)) { |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3508 | if (s.syncing || s.expanding || s.expanded || |
| 3509 | s.to_write || s.written) { |
| 3510 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3511 | goto unlock; |
| 3512 | } |
| 3513 | /* There is nothing for the blocked_rdev to block */ |
| 3514 | rdev_dec_pending(blocked_rdev, conf->mddev); |
| 3515 | blocked_rdev = NULL; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3516 | } |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3517 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3518 | pr_debug("locked=%d uptodate=%d to_read=%d" |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3519 | " to_write=%d failed=%d failed_num=%d,%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3520 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 3521 | r6s.failed_num[0], r6s.failed_num[1]); |
| 3522 | /* check if the array has lost >2 devices and, if so, some requests |
| 3523 | * might need to be failed |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3524 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3525 | if (s.failed > 2 && s.to_read+s.to_write+s.written) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3526 | handle_failed_stripe(conf, sh, &s, disks, &return_bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3527 | if (s.failed > 2 && s.syncing) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3528 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); |
| 3529 | clear_bit(STRIPE_SYNCING, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3530 | s.syncing = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3531 | } |
| 3532 | |
| 3533 | /* |
| 3534 | * might be able to return some write requests if the parity blocks |
| 3535 | * are safe, or on a failed drive |
| 3536 | */ |
| 3537 | pdev = &sh->dev[pd_idx]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3538 | r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx) |
| 3539 | || (s.failed >= 2 && r6s.failed_num[1] == pd_idx); |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3540 | qdev = &sh->dev[qd_idx]; |
| 3541 | r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx) |
| 3542 | || (s.failed >= 2 && r6s.failed_num[1] == qd_idx); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3543 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3544 | if ( s.written && |
| 3545 | ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3546 | && !test_bit(R5_LOCKED, &pdev->flags) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3547 | && test_bit(R5_UPTODATE, &pdev->flags)))) && |
| 3548 | ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3549 | && !test_bit(R5_LOCKED, &qdev->flags) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3550 | && test_bit(R5_UPTODATE, &qdev->flags))))) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3551 | handle_stripe_clean_event(conf, sh, disks, &return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3552 | |
| 3553 | /* Now we might consider reading some blocks, either to check/generate |
| 3554 | * parity, or to satisfy requests |
| 3555 | * or to load a block that is being partially written. |
| 3556 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3557 | if (s.to_read || s.non_overwrite || (s.to_write && s.failed) || |
| 3558 | (s.syncing && (s.uptodate < disks)) || s.expanding) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3559 | handle_stripe_fill6(sh, &s, &r6s, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3560 | |
Yuri Tikhonov | a9b39a7 | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3561 | /* Now to consider new write requests and what else, if anything |
| 3562 | * should be read. We do not handle new writes when: |
| 3563 | * 1/ A 'write' operation (copy+gen_syndrome) is already in flight. |
| 3564 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 3565 | * block. |
| 3566 | */ |
| 3567 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3568 | handle_stripe_dirtying6(conf, sh, &s, &r6s, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3569 | |
| 3570 | /* 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] | 3571 | * Any reads will already have been scheduled, so we just see if enough |
| 3572 | * data is available |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3573 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3574 | if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3575 | handle_parity_checks6(conf, sh, &s, &r6s, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3576 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3577 | if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3578 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); |
| 3579 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 3580 | } |
| 3581 | |
| 3582 | /* If the failed drives are just a ReadError, then we might need |
| 3583 | * to progress the repair/check process |
| 3584 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3585 | if (s.failed <= 2 && !conf->mddev->ro) |
| 3586 | for (i = 0; i < s.failed; i++) { |
| 3587 | dev = &sh->dev[r6s.failed_num[i]]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3588 | if (test_bit(R5_ReadError, &dev->flags) |
| 3589 | && !test_bit(R5_LOCKED, &dev->flags) |
| 3590 | && test_bit(R5_UPTODATE, &dev->flags) |
| 3591 | ) { |
| 3592 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3593 | set_bit(R5_Wantwrite, &dev->flags); |
| 3594 | set_bit(R5_ReWrite, &dev->flags); |
| 3595 | set_bit(R5_LOCKED, &dev->flags); |
| 3596 | } else { |
| 3597 | /* let's read it back */ |
| 3598 | set_bit(R5_Wantread, &dev->flags); |
| 3599 | set_bit(R5_LOCKED, &dev->flags); |
| 3600 | } |
| 3601 | } |
| 3602 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3603 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3604 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3605 | struct stripe_head *sh2 |
| 3606 | = get_active_stripe(conf, sh->sector, 1, 1); |
| 3607 | if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) { |
| 3608 | /* sh cannot be written until sh2 has been read. |
| 3609 | * so arrange for sh to be delayed a little |
| 3610 | */ |
| 3611 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3612 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3613 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3614 | &sh2->state)) |
| 3615 | atomic_inc(&conf->preread_active_stripes); |
| 3616 | release_stripe(sh2); |
| 3617 | goto unlock; |
| 3618 | } |
| 3619 | if (sh2) |
| 3620 | release_stripe(sh2); |
| 3621 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3622 | /* Need to write out all blocks after computing P&Q */ |
| 3623 | sh->disks = conf->raid_disks; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3624 | stripe_set_idx(sh->sector, conf, 0, sh); |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3625 | compute_parity6(sh, RECONSTRUCT_WRITE); |
| 3626 | for (i = conf->raid_disks ; i-- ; ) { |
| 3627 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3628 | s.locked++; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3629 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 3630 | } |
| 3631 | clear_bit(STRIPE_EXPANDING, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3632 | } else if (s.expanded) { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3633 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3634 | atomic_dec(&conf->reshape_stripes); |
| 3635 | wake_up(&conf->wait_for_overlap); |
| 3636 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3637 | } |
| 3638 | |
Dan Williams | 0f94e87 | 2008-01-08 15:32:53 -0800 | [diff] [blame] | 3639 | if (s.expanding && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3640 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3641 | handle_stripe_expansion(conf, sh, &r6s); |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3642 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3643 | unlock: |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3644 | spin_unlock(&sh->lock); |
| 3645 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3646 | /* wait for this device to become unblocked */ |
| 3647 | if (unlikely(blocked_rdev)) |
| 3648 | md_wait_for_blocked_rdev(blocked_rdev, conf->mddev); |
| 3649 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 3650 | ops_run_io(sh, &s); |
| 3651 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3652 | return_io(return_bi); |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 3653 | |
| 3654 | return blocked_rdev == NULL; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3655 | } |
| 3656 | |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 3657 | /* returns true if the stripe was handled */ |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3658 | static bool handle_stripe(struct stripe_head *sh) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3659 | { |
| 3660 | if (sh->raid_conf->level == 6) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3661 | return handle_stripe6(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3662 | else |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 3663 | return handle_stripe5(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3664 | } |
| 3665 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 3666 | static void raid5_activate_delayed(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3667 | { |
| 3668 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 3669 | while (!list_empty(&conf->delayed_list)) { |
| 3670 | struct list_head *l = conf->delayed_list.next; |
| 3671 | struct stripe_head *sh; |
| 3672 | sh = list_entry(l, struct stripe_head, lru); |
| 3673 | list_del_init(l); |
| 3674 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3675 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3676 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3677 | list_add_tail(&sh->lru, &conf->hold_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3678 | } |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 3679 | } else |
| 3680 | blk_plug_device(conf->mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3681 | } |
| 3682 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 3683 | static void activate_bit_delay(raid5_conf_t *conf) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3684 | { |
| 3685 | /* device_lock is held */ |
| 3686 | struct list_head head; |
| 3687 | list_add(&head, &conf->bitmap_list); |
| 3688 | list_del_init(&conf->bitmap_list); |
| 3689 | while (!list_empty(&head)) { |
| 3690 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
| 3691 | list_del_init(&sh->lru); |
| 3692 | atomic_inc(&sh->count); |
| 3693 | __release_stripe(conf, sh); |
| 3694 | } |
| 3695 | } |
| 3696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3697 | static void unplug_slaves(mddev_t *mddev) |
| 3698 | { |
| 3699 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 3700 | int i; |
| 3701 | |
| 3702 | rcu_read_lock(); |
| 3703 | for (i=0; i<mddev->raid_disks; i++) { |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 3704 | mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3705 | if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3706 | struct request_queue *r_queue = bdev_get_queue(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3707 | |
| 3708 | atomic_inc(&rdev->nr_pending); |
| 3709 | rcu_read_unlock(); |
| 3710 | |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 3711 | blk_unplug(r_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3712 | |
| 3713 | rdev_dec_pending(rdev, mddev); |
| 3714 | rcu_read_lock(); |
| 3715 | } |
| 3716 | } |
| 3717 | rcu_read_unlock(); |
| 3718 | } |
| 3719 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3720 | static void raid5_unplug_device(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3721 | { |
| 3722 | mddev_t *mddev = q->queuedata; |
| 3723 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 3724 | unsigned long flags; |
| 3725 | |
| 3726 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3727 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3728 | if (blk_remove_plug(q)) { |
| 3729 | conf->seq_flush++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3730 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3731 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3732 | md_wakeup_thread(mddev->thread); |
| 3733 | |
| 3734 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3735 | |
| 3736 | unplug_slaves(mddev); |
| 3737 | } |
| 3738 | |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3739 | static int raid5_congested(void *data, int bits) |
| 3740 | { |
| 3741 | mddev_t *mddev = data; |
| 3742 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 3743 | |
| 3744 | /* No difference between reads and writes. Just check |
| 3745 | * how busy the stripe_cache is |
| 3746 | */ |
| 3747 | if (conf->inactive_blocked) |
| 3748 | return 1; |
| 3749 | if (conf->quiesce) |
| 3750 | return 1; |
| 3751 | if (list_empty_careful(&conf->inactive_list)) |
| 3752 | return 1; |
| 3753 | |
| 3754 | return 0; |
| 3755 | } |
| 3756 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3757 | /* We want read requests to align with chunks where possible, |
| 3758 | * but write requests don't need to. |
| 3759 | */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3760 | static int raid5_mergeable_bvec(struct request_queue *q, |
| 3761 | struct bvec_merge_data *bvm, |
| 3762 | struct bio_vec *biovec) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3763 | { |
| 3764 | mddev_t *mddev = q->queuedata; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3765 | 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] | 3766 | int max; |
| 3767 | unsigned int chunk_sectors = mddev->chunk_size >> 9; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3768 | unsigned int bio_sectors = bvm->bi_size >> 9; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3769 | |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3770 | if ((bvm->bi_rw & 1) == WRITE) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3771 | return biovec->bv_len; /* always allow writes to be mergeable */ |
| 3772 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3773 | if (mddev->new_chunk < mddev->chunk_size) |
| 3774 | chunk_sectors = mddev->new_chunk >> 9; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3775 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 3776 | if (max < 0) max = 0; |
| 3777 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 3778 | return biovec->bv_len; |
| 3779 | else |
| 3780 | return max; |
| 3781 | } |
| 3782 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3783 | |
| 3784 | static int in_chunk_boundary(mddev_t *mddev, struct bio *bio) |
| 3785 | { |
| 3786 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); |
| 3787 | unsigned int chunk_sectors = mddev->chunk_size >> 9; |
| 3788 | unsigned int bio_sectors = bio->bi_size >> 9; |
| 3789 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3790 | if (mddev->new_chunk < mddev->chunk_size) |
| 3791 | chunk_sectors = mddev->new_chunk >> 9; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3792 | return chunk_sectors >= |
| 3793 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 3794 | } |
| 3795 | |
| 3796 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3797 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 3798 | * later sampled by raid5d. |
| 3799 | */ |
| 3800 | static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf) |
| 3801 | { |
| 3802 | unsigned long flags; |
| 3803 | |
| 3804 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3805 | |
| 3806 | bi->bi_next = conf->retry_read_aligned_list; |
| 3807 | conf->retry_read_aligned_list = bi; |
| 3808 | |
| 3809 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3810 | md_wakeup_thread(conf->mddev->thread); |
| 3811 | } |
| 3812 | |
| 3813 | |
| 3814 | static struct bio *remove_bio_from_retry(raid5_conf_t *conf) |
| 3815 | { |
| 3816 | struct bio *bi; |
| 3817 | |
| 3818 | bi = conf->retry_read_aligned; |
| 3819 | if (bi) { |
| 3820 | conf->retry_read_aligned = NULL; |
| 3821 | return bi; |
| 3822 | } |
| 3823 | bi = conf->retry_read_aligned_list; |
| 3824 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3825 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3826 | bi->bi_next = NULL; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3827 | /* |
| 3828 | * this sets the active strip count to 1 and the processed |
| 3829 | * strip count to zero (upper 8 bits) |
| 3830 | */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3831 | bi->bi_phys_segments = 1; /* biased count of active stripes */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | return bi; |
| 3835 | } |
| 3836 | |
| 3837 | |
| 3838 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3839 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 3840 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 3841 | * first). |
| 3842 | * If the read failed.. |
| 3843 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3844 | static void raid5_align_endio(struct bio *bi, int error) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3845 | { |
| 3846 | struct bio* raid_bi = bi->bi_private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3847 | mddev_t *mddev; |
| 3848 | raid5_conf_t *conf; |
| 3849 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
| 3850 | mdk_rdev_t *rdev; |
| 3851 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3852 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3853 | |
| 3854 | mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata; |
| 3855 | conf = mddev_to_conf(mddev); |
| 3856 | rdev = (void*)raid_bi->bi_next; |
| 3857 | raid_bi->bi_next = NULL; |
| 3858 | |
| 3859 | rdev_dec_pending(rdev, conf->mddev); |
| 3860 | |
| 3861 | if (!error && uptodate) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3862 | bio_endio(raid_bi, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3863 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 3864 | wake_up(&conf->wait_for_stripe); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3865 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3866 | } |
| 3867 | |
| 3868 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3869 | 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] | 3870 | |
| 3871 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3872 | } |
| 3873 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3874 | static int bio_fits_rdev(struct bio *bi) |
| 3875 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3876 | struct request_queue *q = bdev_get_queue(bi->bi_bdev); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3877 | |
| 3878 | if ((bi->bi_size>>9) > q->max_sectors) |
| 3879 | return 0; |
| 3880 | blk_recount_segments(q, bi); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3881 | if (bi->bi_phys_segments > q->max_phys_segments) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3882 | return 0; |
| 3883 | |
| 3884 | if (q->merge_bvec_fn) |
| 3885 | /* it's too hard to apply the merge_bvec_fn at this stage, |
| 3886 | * just just give up |
| 3887 | */ |
| 3888 | return 0; |
| 3889 | |
| 3890 | return 1; |
| 3891 | } |
| 3892 | |
| 3893 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3894 | 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] | 3895 | { |
| 3896 | mddev_t *mddev = q->queuedata; |
| 3897 | raid5_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3898 | unsigned int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3899 | struct bio* align_bi; |
| 3900 | mdk_rdev_t *rdev; |
| 3901 | |
| 3902 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3903 | pr_debug("chunk_aligned_read : non aligned\n"); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3904 | return 0; |
| 3905 | } |
| 3906 | /* |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3907 | * use bio_clone to make a copy of the bio |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3908 | */ |
| 3909 | align_bi = bio_clone(raid_bio, GFP_NOIO); |
| 3910 | if (!align_bi) |
| 3911 | return 0; |
| 3912 | /* |
| 3913 | * set bi_end_io to a new function, and set bi_private to the |
| 3914 | * original bio. |
| 3915 | */ |
| 3916 | align_bi->bi_end_io = raid5_align_endio; |
| 3917 | align_bi->bi_private = raid_bio; |
| 3918 | /* |
| 3919 | * compute position |
| 3920 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3921 | align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector, |
| 3922 | 0, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3923 | &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3924 | |
| 3925 | rcu_read_lock(); |
| 3926 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 3927 | if (rdev && test_bit(In_sync, &rdev->flags)) { |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3928 | atomic_inc(&rdev->nr_pending); |
| 3929 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3930 | raid_bio->bi_next = (void*)rdev; |
| 3931 | align_bi->bi_bdev = rdev->bdev; |
| 3932 | align_bi->bi_flags &= ~(1 << BIO_SEG_VALID); |
| 3933 | align_bi->bi_sector += rdev->data_offset; |
| 3934 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3935 | if (!bio_fits_rdev(align_bi)) { |
| 3936 | /* too big in some way */ |
| 3937 | bio_put(align_bi); |
| 3938 | rdev_dec_pending(rdev, mddev); |
| 3939 | return 0; |
| 3940 | } |
| 3941 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3942 | spin_lock_irq(&conf->device_lock); |
| 3943 | wait_event_lock_irq(conf->wait_for_stripe, |
| 3944 | conf->quiesce == 0, |
| 3945 | conf->device_lock, /* nothing */); |
| 3946 | atomic_inc(&conf->active_aligned_reads); |
| 3947 | spin_unlock_irq(&conf->device_lock); |
| 3948 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3949 | generic_make_request(align_bi); |
| 3950 | return 1; |
| 3951 | } else { |
| 3952 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3953 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3954 | return 0; |
| 3955 | } |
| 3956 | } |
| 3957 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3958 | /* __get_priority_stripe - get the next stripe to process |
| 3959 | * |
| 3960 | * Full stripe writes are allowed to pass preread active stripes up until |
| 3961 | * the bypass_threshold is exceeded. In general the bypass_count |
| 3962 | * increments when the handle_list is handled before the hold_list; however, it |
| 3963 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 3964 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 3965 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 3966 | * handle_list. |
| 3967 | */ |
| 3968 | static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf) |
| 3969 | { |
| 3970 | struct stripe_head *sh; |
| 3971 | |
| 3972 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 3973 | __func__, |
| 3974 | list_empty(&conf->handle_list) ? "empty" : "busy", |
| 3975 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 3976 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 3977 | |
| 3978 | if (!list_empty(&conf->handle_list)) { |
| 3979 | sh = list_entry(conf->handle_list.next, typeof(*sh), lru); |
| 3980 | |
| 3981 | if (list_empty(&conf->hold_list)) |
| 3982 | conf->bypass_count = 0; |
| 3983 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 3984 | if (conf->hold_list.next == conf->last_hold) |
| 3985 | conf->bypass_count++; |
| 3986 | else { |
| 3987 | conf->last_hold = conf->hold_list.next; |
| 3988 | conf->bypass_count -= conf->bypass_threshold; |
| 3989 | if (conf->bypass_count < 0) |
| 3990 | conf->bypass_count = 0; |
| 3991 | } |
| 3992 | } |
| 3993 | } else if (!list_empty(&conf->hold_list) && |
| 3994 | ((conf->bypass_threshold && |
| 3995 | conf->bypass_count > conf->bypass_threshold) || |
| 3996 | atomic_read(&conf->pending_full_writes) == 0)) { |
| 3997 | sh = list_entry(conf->hold_list.next, |
| 3998 | typeof(*sh), lru); |
| 3999 | conf->bypass_count -= conf->bypass_threshold; |
| 4000 | if (conf->bypass_count < 0) |
| 4001 | conf->bypass_count = 0; |
| 4002 | } else |
| 4003 | return NULL; |
| 4004 | |
| 4005 | list_del_init(&sh->lru); |
| 4006 | atomic_inc(&sh->count); |
| 4007 | BUG_ON(atomic_read(&sh->count) != 1); |
| 4008 | return sh; |
| 4009 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4010 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 4011 | static int make_request(struct request_queue *q, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | { |
| 4013 | mddev_t *mddev = q->queuedata; |
| 4014 | raid5_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4015 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4016 | sector_t new_sector; |
| 4017 | sector_t logical_sector, last_sector; |
| 4018 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 4019 | const int rw = bio_data_dir(bi); |
Tejun Heo | c995905 | 2008-08-25 19:47:21 +0900 | [diff] [blame] | 4020 | int cpu, remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4021 | |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 4022 | if (unlikely(bio_barrier(bi))) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 4023 | bio_endio(bi, -EOPNOTSUPP); |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 4024 | return 0; |
| 4025 | } |
| 4026 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 4027 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4028 | |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 4029 | cpu = part_stat_lock(); |
| 4030 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 4031 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], |
| 4032 | bio_sectors(bi)); |
| 4033 | part_stat_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4034 | |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 4035 | if (rw == READ && |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 4036 | mddev->reshape_position == MaxSector && |
| 4037 | chunk_aligned_read(q,bi)) |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4038 | return 0; |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 4039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4040 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 4041 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 4042 | bi->bi_next = NULL; |
| 4043 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4044 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4045 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
| 4046 | DEFINE_WAIT(w); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4047 | int disks, data_disks; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4048 | int previous; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4049 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4050 | retry: |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4051 | previous = 0; |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4052 | disks = conf->raid_disks; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4053 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4054 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4055 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4056 | * 64bit on a 32bit platform, and so it might be |
| 4057 | * possible to see a half-updated value |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4058 | * Ofcourse reshape_progress could change after |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4059 | * the lock is dropped, so once we get a reference |
| 4060 | * to the stripe that we think it is, we will have |
| 4061 | * to check again. |
| 4062 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4063 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4064 | if (mddev->delta_disks < 0 |
| 4065 | ? logical_sector < conf->reshape_progress |
| 4066 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4067 | disks = conf->previous_raid_disks; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4068 | previous = 1; |
| 4069 | } else { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4070 | if (mddev->delta_disks < 0 |
| 4071 | ? logical_sector < conf->reshape_safe |
| 4072 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4073 | spin_unlock_irq(&conf->device_lock); |
| 4074 | schedule(); |
| 4075 | goto retry; |
| 4076 | } |
| 4077 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4078 | spin_unlock_irq(&conf->device_lock); |
| 4079 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4080 | data_disks = disks - conf->max_degraded; |
| 4081 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4082 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 4083 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4084 | &dd_idx, NULL); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4085 | pr_debug("raid5: make_request, sector %llu logical %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4086 | (unsigned long long)new_sector, |
| 4087 | (unsigned long long)logical_sector); |
| 4088 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4089 | sh = get_active_stripe(conf, new_sector, previous, |
| 4090 | (bi->bi_rw&RWA_MASK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4091 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4092 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4093 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4094 | * stripe, so we must do the range check again. |
| 4095 | * Expansion could still move past after this |
| 4096 | * test, but as we are holding a reference to |
| 4097 | * 'sh', we know that if that happens, |
| 4098 | * STRIPE_EXPANDING will get set and the expansion |
| 4099 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4100 | */ |
| 4101 | int must_retry = 0; |
| 4102 | spin_lock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4103 | if (mddev->delta_disks < 0 |
| 4104 | ? logical_sector >= conf->reshape_progress |
| 4105 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4106 | /* mismatch, need to try again */ |
| 4107 | must_retry = 1; |
| 4108 | spin_unlock_irq(&conf->device_lock); |
| 4109 | if (must_retry) { |
| 4110 | release_stripe(sh); |
| 4111 | goto retry; |
| 4112 | } |
| 4113 | } |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4114 | /* FIXME what if we get a false positive because these |
| 4115 | * are being updated. |
| 4116 | */ |
| 4117 | if (logical_sector >= mddev->suspend_lo && |
| 4118 | logical_sector < mddev->suspend_hi) { |
| 4119 | release_stripe(sh); |
| 4120 | schedule(); |
| 4121 | goto retry; |
| 4122 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4123 | |
| 4124 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
| 4125 | !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) { |
| 4126 | /* Stripe is busy expanding or |
| 4127 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4128 | * and wait a while |
| 4129 | */ |
| 4130 | raid5_unplug_device(mddev->queue); |
| 4131 | release_stripe(sh); |
| 4132 | schedule(); |
| 4133 | goto retry; |
| 4134 | } |
| 4135 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 4136 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4137 | clear_bit(STRIPE_DELAYED, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4138 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4139 | } else { |
| 4140 | /* cannot get stripe for read-ahead, just give-up */ |
| 4141 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 4142 | finish_wait(&conf->wait_for_overlap, &w); |
| 4143 | break; |
| 4144 | } |
| 4145 | |
| 4146 | } |
| 4147 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4148 | remaining = raid5_dec_bi_phys_segments(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 4149 | spin_unlock_irq(&conf->device_lock); |
| 4150 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4152 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | md_write_end(mddev); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 4154 | |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4155 | bio_endio(bi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | return 0; |
| 4158 | } |
| 4159 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4160 | static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks); |
| 4161 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4162 | 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] | 4163 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4164 | /* reshaping is quite different to recovery/resync so it is |
| 4165 | * handled quite separately ... here. |
| 4166 | * |
| 4167 | * On each call to sync_request, we gather one chunk worth of |
| 4168 | * destination stripes and flag them as expanding. |
| 4169 | * Then we find all the source stripes and request reads. |
| 4170 | * As the reads complete, handle_stripe will copy the data |
| 4171 | * into the destination stripe and release that stripe. |
| 4172 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4173 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 4174 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4175 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4176 | int raid_disks = conf->previous_raid_disks; |
| 4177 | int data_disks = raid_disks - conf->max_degraded; |
| 4178 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4179 | int i; |
| 4180 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4181 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4182 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4183 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4184 | struct list_head stripes; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4185 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4186 | if (sector_nr == 0) { |
| 4187 | /* If restarting in the middle, skip the initial sectors */ |
| 4188 | if (mddev->delta_disks < 0 && |
| 4189 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 4190 | sector_nr = raid5_size(mddev, 0, 0) |
| 4191 | - conf->reshape_progress; |
| 4192 | } else if (mddev->delta_disks > 0 && |
| 4193 | conf->reshape_progress > 0) |
| 4194 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4195 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4196 | if (sector_nr) { |
| 4197 | *skipped = 1; |
| 4198 | return sector_nr; |
| 4199 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4200 | } |
| 4201 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4202 | /* We need to process a full chunk at a time. |
| 4203 | * If old and new chunk sizes differ, we need to process the |
| 4204 | * largest of these |
| 4205 | */ |
| 4206 | if (mddev->new_chunk > mddev->chunk_size) |
| 4207 | reshape_sectors = mddev->new_chunk / 512; |
| 4208 | else |
| 4209 | reshape_sectors = mddev->chunk_size / 512; |
| 4210 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4211 | /* we update the metadata when there is more than 3Meg |
| 4212 | * in the block range (that is rather arbitrary, should |
| 4213 | * probably be time based) or when the data about to be |
| 4214 | * copied would over-write the source of the data at |
| 4215 | * the front of the range. |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4216 | * i.e. one new_stripe along from reshape_progress new_maps |
| 4217 | * to after where reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4218 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4219 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4220 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4221 | readpos = conf->reshape_progress; |
| 4222 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4223 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4224 | sector_div(safepos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4225 | if (mddev->delta_disks < 0) { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4226 | writepos -= reshape_sectors; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4227 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4228 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4229 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4230 | writepos += reshape_sectors; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4231 | readpos -= reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4232 | safepos -= reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4233 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4234 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4235 | /* 'writepos' is the most advanced device address we might write. |
| 4236 | * 'readpos' is the least advanced device address we might read. |
| 4237 | * 'safepos' is the least address recorded in the metadata as having |
| 4238 | * been reshaped. |
| 4239 | * If 'readpos' is behind 'writepos', then there is no way that we can |
| 4240 | * ensure safety in the face of a crash - that must be done by userspace |
| 4241 | * making a backup of the data. So in that case there is no particular |
| 4242 | * rush to update metadata. |
| 4243 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 4244 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 4245 | * we can be safe in the event of a crash. |
| 4246 | * So we insist on updating metadata if safepos is behind writepos and |
| 4247 | * readpos is beyond writepos. |
| 4248 | * In any case, update the metadata every 10 seconds. |
| 4249 | * Maybe that number should be configurable, but I'm not sure it is |
| 4250 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 4251 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4252 | if ((mddev->delta_disks < 0 |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4253 | ? (safepos > writepos && readpos < writepos) |
| 4254 | : (safepos < writepos && readpos > writepos)) || |
| 4255 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4256 | /* Cannot proceed until we've updated the superblock... */ |
| 4257 | wait_event(conf->wait_for_overlap, |
| 4258 | atomic_read(&conf->reshape_stripes)==0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4259 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4260 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4261 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4262 | md_wakeup_thread(mddev->thread); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4263 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4264 | kthread_should_stop()); |
| 4265 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4266 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4267 | spin_unlock_irq(&conf->device_lock); |
| 4268 | wake_up(&conf->wait_for_overlap); |
| 4269 | } |
| 4270 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4271 | if (mddev->delta_disks < 0) { |
| 4272 | BUG_ON(conf->reshape_progress == 0); |
| 4273 | stripe_addr = writepos; |
| 4274 | BUG_ON((mddev->dev_sectors & |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4275 | ~((sector_t)reshape_sectors - 1)) |
| 4276 | - reshape_sectors - stripe_addr |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4277 | != sector_nr); |
| 4278 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4279 | BUG_ON(writepos != sector_nr + reshape_sectors); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4280 | stripe_addr = sector_nr; |
| 4281 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4282 | INIT_LIST_HEAD(&stripes); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4283 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4284 | int j; |
| 4285 | int skipped = 0; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4286 | sh = get_active_stripe(conf, stripe_addr+i, 0, 0); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4287 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 4288 | atomic_inc(&conf->reshape_stripes); |
| 4289 | /* If any of this stripe is beyond the end of the old |
| 4290 | * array, then we need to zero those blocks |
| 4291 | */ |
| 4292 | for (j=sh->disks; j--;) { |
| 4293 | sector_t s; |
| 4294 | if (j == sh->pd_idx) |
| 4295 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4296 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4297 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4298 | continue; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4299 | s = compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4300 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4301 | skipped = 1; |
| 4302 | continue; |
| 4303 | } |
| 4304 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 4305 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 4306 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 4307 | } |
| 4308 | if (!skipped) { |
| 4309 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 4310 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4311 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4312 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4313 | } |
| 4314 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4315 | if (mddev->delta_disks < 0) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4316 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4317 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4318 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4319 | spin_unlock_irq(&conf->device_lock); |
| 4320 | /* Ok, those stripe are ready. We can start scheduling |
| 4321 | * reads on the source stripes. |
| 4322 | * The source stripes are determined by mapping the first and last |
| 4323 | * block on the destination stripes. |
| 4324 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4325 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4326 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4327 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4328 | last_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4329 | raid5_compute_sector(conf, ((stripe_addr+conf->chunk_size/512) |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4330 | *(new_data_disks) - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4331 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4332 | if (last_sector >= mddev->dev_sectors) |
| 4333 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4334 | while (first_sector <= last_sector) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4335 | sh = get_active_stripe(conf, first_sector, 1, 0); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4336 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 4337 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4338 | release_stripe(sh); |
| 4339 | first_sector += STRIPE_SECTORS; |
| 4340 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4341 | /* Now that the sources are clearly marked, we can release |
| 4342 | * the destination stripes |
| 4343 | */ |
| 4344 | while (!list_empty(&stripes)) { |
| 4345 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 4346 | list_del_init(&sh->lru); |
| 4347 | release_stripe(sh); |
| 4348 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4349 | /* If this takes us to the resync_max point where we have to pause, |
| 4350 | * then we need to write out the superblock. |
| 4351 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4352 | sector_nr += reshape_sectors; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4353 | if (sector_nr >= mddev->resync_max) { |
| 4354 | /* Cannot proceed until we've updated the superblock... */ |
| 4355 | wait_event(conf->wait_for_overlap, |
| 4356 | atomic_read(&conf->reshape_stripes) == 0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4357 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4358 | conf->reshape_checkpoint = jiffies; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4359 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 4360 | md_wakeup_thread(mddev->thread); |
| 4361 | wait_event(mddev->sb_wait, |
| 4362 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) |
| 4363 | || kthread_should_stop()); |
| 4364 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4365 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4366 | spin_unlock_irq(&conf->device_lock); |
| 4367 | wake_up(&conf->wait_for_overlap); |
| 4368 | } |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4369 | return reshape_sectors; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | /* FIXME go_faster isn't used */ |
| 4373 | static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster) |
| 4374 | { |
| 4375 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 4376 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4377 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4378 | int sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4379 | int still_degraded = 0; |
| 4380 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4381 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4382 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | /* just being told to finish up .. nothing much to do */ |
| 4384 | unplug_slaves(mddev); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 4385 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4386 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 4387 | end_reshape(conf); |
| 4388 | return 0; |
| 4389 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4390 | |
| 4391 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 4392 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 4393 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4394 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4395 | conf->fullsync = 0; |
| 4396 | bitmap_close_sync(mddev->bitmap); |
| 4397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | return 0; |
| 4399 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4400 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4401 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4402 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4403 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4404 | /* No need to check resync_max as we never do more than one |
| 4405 | * stripe, and as resync_max will always be on a chunk boundary, |
| 4406 | * if the check in md_do_sync didn't fire, there is no chance |
| 4407 | * of overstepping resync_max here |
| 4408 | */ |
| 4409 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4410 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | * to resync, then assert that we are finished, because there is |
| 4412 | * nothing we can do. |
| 4413 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 4414 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4415 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4416 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 4417 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | return rv; |
| 4419 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4420 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
NeilBrown | 3855ad9 | 2005-11-08 21:39:38 -0800 | [diff] [blame] | 4421 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4422 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
| 4423 | /* we can skip this block, and probably more */ |
| 4424 | sync_blocks /= STRIPE_SECTORS; |
| 4425 | *skipped = 1; |
| 4426 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 4427 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4428 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 4429 | |
| 4430 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 4431 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4432 | sh = get_active_stripe(conf, sector_nr, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4433 | if (sh == NULL) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4434 | sh = get_active_stripe(conf, sector_nr, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4436 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4437 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 4438 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4439 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4440 | /* Need to check if array will still be degraded after recovery/resync |
| 4441 | * We don't need to check the 'failed' flag as when that gets set, |
| 4442 | * recovery aborts. |
| 4443 | */ |
| 4444 | for (i=0; i<mddev->raid_disks; i++) |
| 4445 | if (conf->disks[i].rdev == NULL) |
| 4446 | still_degraded = 1; |
| 4447 | |
| 4448 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 4449 | |
| 4450 | spin_lock(&sh->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4451 | set_bit(STRIPE_SYNCING, &sh->state); |
| 4452 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 4453 | spin_unlock(&sh->lock); |
| 4454 | |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 4455 | /* wait for any blocked device to be handled */ |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4456 | while (unlikely(!handle_stripe(sh))) |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 4457 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | release_stripe(sh); |
| 4459 | |
| 4460 | return STRIPE_SECTORS; |
| 4461 | } |
| 4462 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4463 | static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio) |
| 4464 | { |
| 4465 | /* We may not be able to submit a whole bio at once as there |
| 4466 | * may not be enough stripe_heads available. |
| 4467 | * We cannot pre-allocate enough stripe_heads as we may need |
| 4468 | * more than exist in the cache (if we allow ever large chunks). |
| 4469 | * So we do one stripe head at a time and record in |
| 4470 | * ->bi_hw_segments how many have been done. |
| 4471 | * |
| 4472 | * We *know* that this entire raid_bio is in one chunk, so |
| 4473 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 4474 | */ |
| 4475 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4476 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4477 | sector_t sector, logical_sector, last_sector; |
| 4478 | int scnt = 0; |
| 4479 | int remaining; |
| 4480 | int handled = 0; |
| 4481 | |
| 4482 | logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4483 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4484 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4485 | last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9); |
| 4486 | |
| 4487 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4488 | logical_sector += STRIPE_SECTORS, |
| 4489 | sector += STRIPE_SECTORS, |
| 4490 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4491 | |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4492 | if (scnt < raid5_bi_hw_segments(raid_bio)) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4493 | /* already done this stripe */ |
| 4494 | continue; |
| 4495 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4496 | sh = get_active_stripe(conf, sector, 0, 1); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4497 | |
| 4498 | if (!sh) { |
| 4499 | /* failed to get a stripe - must wait */ |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4500 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4501 | conf->retry_read_aligned = raid_bio; |
| 4502 | return handled; |
| 4503 | } |
| 4504 | |
| 4505 | set_bit(R5_ReadError, &sh->dev[dd_idx].flags); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4506 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { |
| 4507 | release_stripe(sh); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4508 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4509 | conf->retry_read_aligned = raid_bio; |
| 4510 | return handled; |
| 4511 | } |
| 4512 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4513 | handle_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4514 | release_stripe(sh); |
| 4515 | handled++; |
| 4516 | } |
| 4517 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4518 | remaining = raid5_dec_bi_phys_segments(raid_bio); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4519 | spin_unlock_irq(&conf->device_lock); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4520 | if (remaining == 0) |
| 4521 | bio_endio(raid_bio, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4522 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 4523 | wake_up(&conf->wait_for_stripe); |
| 4524 | return handled; |
| 4525 | } |
| 4526 | |
| 4527 | |
| 4528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4529 | /* |
| 4530 | * This is our raid5 kernel thread. |
| 4531 | * |
| 4532 | * We scan the hash table for stripes which can be handled now. |
| 4533 | * During the scan, completed stripes are saved for us by the interrupt |
| 4534 | * handler, so that they will not have to wait for our next wakeup. |
| 4535 | */ |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 4536 | static void raid5d(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4537 | { |
| 4538 | struct stripe_head *sh; |
| 4539 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 4540 | int handled; |
| 4541 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4542 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4543 | |
| 4544 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4545 | |
| 4546 | handled = 0; |
| 4547 | spin_lock_irq(&conf->device_lock); |
| 4548 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4549 | struct bio *bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4550 | |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 4551 | if (conf->seq_flush != conf->seq_write) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4552 | int seq = conf->seq_flush; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4553 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4554 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4555 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4556 | conf->seq_write = seq; |
| 4557 | activate_bit_delay(conf); |
| 4558 | } |
| 4559 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4560 | while ((bio = remove_bio_from_retry(conf))) { |
| 4561 | int ok; |
| 4562 | spin_unlock_irq(&conf->device_lock); |
| 4563 | ok = retry_aligned_read(conf, bio); |
| 4564 | spin_lock_irq(&conf->device_lock); |
| 4565 | if (!ok) |
| 4566 | break; |
| 4567 | handled++; |
| 4568 | } |
| 4569 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4570 | sh = __get_priority_stripe(conf); |
| 4571 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4572 | if (!sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4573 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4574 | spin_unlock_irq(&conf->device_lock); |
| 4575 | |
| 4576 | handled++; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4577 | handle_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | release_stripe(sh); |
| 4579 | |
| 4580 | spin_lock_irq(&conf->device_lock); |
| 4581 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4582 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4583 | |
| 4584 | spin_unlock_irq(&conf->device_lock); |
| 4585 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4586 | async_tx_issue_pending_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4587 | unplug_slaves(mddev); |
| 4588 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4589 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | } |
| 4591 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4592 | static ssize_t |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4593 | raid5_show_stripe_cache_size(mddev_t *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4594 | { |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4595 | raid5_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4596 | if (conf) |
| 4597 | return sprintf(page, "%d\n", conf->max_nr_stripes); |
| 4598 | else |
| 4599 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4600 | } |
| 4601 | |
| 4602 | static ssize_t |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4603 | 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] | 4604 | { |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4605 | raid5_conf_t *conf = mddev_to_conf(mddev); |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4606 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4607 | int err; |
| 4608 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4609 | if (len >= PAGE_SIZE) |
| 4610 | return -EINVAL; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4611 | if (!conf) |
| 4612 | return -ENODEV; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4613 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4614 | if (strict_strtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4615 | return -EINVAL; |
| 4616 | if (new <= 16 || new > 32768) |
| 4617 | return -EINVAL; |
| 4618 | while (new < conf->max_nr_stripes) { |
| 4619 | if (drop_one_stripe(conf)) |
| 4620 | conf->max_nr_stripes--; |
| 4621 | else |
| 4622 | break; |
| 4623 | } |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4624 | err = md_allow_write(mddev); |
| 4625 | if (err) |
| 4626 | return err; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4627 | while (new > conf->max_nr_stripes) { |
| 4628 | if (grow_one_stripe(conf)) |
| 4629 | conf->max_nr_stripes++; |
| 4630 | else break; |
| 4631 | } |
| 4632 | return len; |
| 4633 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4634 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4635 | static struct md_sysfs_entry |
| 4636 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 4637 | raid5_show_stripe_cache_size, |
| 4638 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4639 | |
| 4640 | static ssize_t |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4641 | raid5_show_preread_threshold(mddev_t *mddev, char *page) |
| 4642 | { |
| 4643 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 4644 | if (conf) |
| 4645 | return sprintf(page, "%d\n", conf->bypass_threshold); |
| 4646 | else |
| 4647 | return 0; |
| 4648 | } |
| 4649 | |
| 4650 | static ssize_t |
| 4651 | raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len) |
| 4652 | { |
| 4653 | raid5_conf_t *conf = mddev_to_conf(mddev); |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4654 | unsigned long new; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4655 | if (len >= PAGE_SIZE) |
| 4656 | return -EINVAL; |
| 4657 | if (!conf) |
| 4658 | return -ENODEV; |
| 4659 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4660 | if (strict_strtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4661 | return -EINVAL; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4662 | if (new > conf->max_nr_stripes) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4663 | return -EINVAL; |
| 4664 | conf->bypass_threshold = new; |
| 4665 | return len; |
| 4666 | } |
| 4667 | |
| 4668 | static struct md_sysfs_entry |
| 4669 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 4670 | S_IRUGO | S_IWUSR, |
| 4671 | raid5_show_preread_threshold, |
| 4672 | raid5_store_preread_threshold); |
| 4673 | |
| 4674 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4675 | stripe_cache_active_show(mddev_t *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4676 | { |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4677 | raid5_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4678 | if (conf) |
| 4679 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 4680 | else |
| 4681 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4682 | } |
| 4683 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4684 | static struct md_sysfs_entry |
| 4685 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4686 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4687 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4688 | &raid5_stripecache_size.attr, |
| 4689 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4690 | &raid5_preread_bypass_threshold.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4691 | NULL, |
| 4692 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4693 | static struct attribute_group raid5_attrs_group = { |
| 4694 | .name = NULL, |
| 4695 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4696 | }; |
| 4697 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4698 | static sector_t |
| 4699 | raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks) |
| 4700 | { |
| 4701 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 4702 | |
| 4703 | if (!sectors) |
| 4704 | sectors = mddev->dev_sectors; |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 4705 | if (!raid_disks) { |
| 4706 | /* size is defined by the smallest of previous and new size */ |
| 4707 | if (conf->raid_disks < conf->previous_raid_disks) |
| 4708 | raid_disks = conf->raid_disks; |
| 4709 | else |
| 4710 | raid_disks = conf->previous_raid_disks; |
| 4711 | } |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4712 | |
| 4713 | sectors &= ~((sector_t)mddev->chunk_size/512 - 1); |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4714 | sectors &= ~((sector_t)mddev->new_chunk/512 - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4715 | return sectors * (raid_disks - conf->max_degraded); |
| 4716 | } |
| 4717 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4718 | static void raid5_free_percpu(raid5_conf_t *conf) |
| 4719 | { |
| 4720 | struct raid5_percpu *percpu; |
| 4721 | unsigned long cpu; |
| 4722 | |
| 4723 | if (!conf->percpu) |
| 4724 | return; |
| 4725 | |
| 4726 | get_online_cpus(); |
| 4727 | for_each_possible_cpu(cpu) { |
| 4728 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4729 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4730 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4731 | } |
| 4732 | #ifdef CONFIG_HOTPLUG_CPU |
| 4733 | unregister_cpu_notifier(&conf->cpu_notify); |
| 4734 | #endif |
| 4735 | put_online_cpus(); |
| 4736 | |
| 4737 | free_percpu(conf->percpu); |
| 4738 | } |
| 4739 | |
Dan Williams | a11034b | 2009-07-14 11:48:16 -0700 | [diff] [blame] | 4740 | static void free_conf(raid5_conf_t *conf) |
| 4741 | { |
| 4742 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4743 | raid5_free_percpu(conf); |
Dan Williams | a11034b | 2009-07-14 11:48:16 -0700 | [diff] [blame] | 4744 | kfree(conf->disks); |
| 4745 | kfree(conf->stripe_hashtbl); |
| 4746 | kfree(conf); |
| 4747 | } |
| 4748 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4749 | #ifdef CONFIG_HOTPLUG_CPU |
| 4750 | static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action, |
| 4751 | void *hcpu) |
| 4752 | { |
| 4753 | raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify); |
| 4754 | long cpu = (long)hcpu; |
| 4755 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4756 | |
| 4757 | switch (action) { |
| 4758 | case CPU_UP_PREPARE: |
| 4759 | case CPU_UP_PREPARE_FROZEN: |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4760 | if (conf->level == 6 && !percpu->spare_page) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4761 | percpu->spare_page = alloc_page(GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4762 | if (!percpu->scribble) |
| 4763 | percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
| 4764 | |
| 4765 | if (!percpu->scribble || |
| 4766 | (conf->level == 6 && !percpu->spare_page)) { |
| 4767 | safe_put_page(percpu->spare_page); |
| 4768 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4769 | pr_err("%s: failed memory allocation for cpu%ld\n", |
| 4770 | __func__, cpu); |
| 4771 | return NOTIFY_BAD; |
| 4772 | } |
| 4773 | break; |
| 4774 | case CPU_DEAD: |
| 4775 | case CPU_DEAD_FROZEN: |
| 4776 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4777 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4778 | percpu->spare_page = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4779 | percpu->scribble = NULL; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4780 | break; |
| 4781 | default: |
| 4782 | break; |
| 4783 | } |
| 4784 | return NOTIFY_OK; |
| 4785 | } |
| 4786 | #endif |
| 4787 | |
| 4788 | static int raid5_alloc_percpu(raid5_conf_t *conf) |
| 4789 | { |
| 4790 | unsigned long cpu; |
| 4791 | struct page *spare_page; |
| 4792 | struct raid5_percpu *allcpus; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4793 | void *scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4794 | int err; |
| 4795 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4796 | allcpus = alloc_percpu(struct raid5_percpu); |
| 4797 | if (!allcpus) |
| 4798 | return -ENOMEM; |
| 4799 | conf->percpu = allcpus; |
| 4800 | |
| 4801 | get_online_cpus(); |
| 4802 | err = 0; |
| 4803 | for_each_present_cpu(cpu) { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4804 | if (conf->level == 6) { |
| 4805 | spare_page = alloc_page(GFP_KERNEL); |
| 4806 | if (!spare_page) { |
| 4807 | err = -ENOMEM; |
| 4808 | break; |
| 4809 | } |
| 4810 | per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page; |
| 4811 | } |
| 4812 | scribble = kmalloc(scribble_len(conf->raid_disks), GFP_KERNEL); |
| 4813 | if (!scribble) { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4814 | err = -ENOMEM; |
| 4815 | break; |
| 4816 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4817 | per_cpu_ptr(conf->percpu, cpu)->scribble = scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4818 | } |
| 4819 | #ifdef CONFIG_HOTPLUG_CPU |
| 4820 | conf->cpu_notify.notifier_call = raid456_cpu_notify; |
| 4821 | conf->cpu_notify.priority = 0; |
| 4822 | if (err == 0) |
| 4823 | err = register_cpu_notifier(&conf->cpu_notify); |
| 4824 | #endif |
| 4825 | put_online_cpus(); |
| 4826 | |
| 4827 | return err; |
| 4828 | } |
| 4829 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4830 | static raid5_conf_t *setup_conf(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4831 | { |
| 4832 | raid5_conf_t *conf; |
| 4833 | int raid_disk, memory; |
| 4834 | mdk_rdev_t *rdev; |
| 4835 | struct disk_info *disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4836 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4837 | if (mddev->new_level != 5 |
| 4838 | && mddev->new_level != 4 |
| 4839 | && mddev->new_level != 6) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4840 | 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] | 4841 | mdname(mddev), mddev->new_level); |
| 4842 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4843 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4844 | if ((mddev->new_level == 5 |
| 4845 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 4846 | (mddev->new_level == 6 |
| 4847 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4848 | printk(KERN_ERR "raid5: %s: layout %d not supported\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4849 | mdname(mddev), mddev->new_layout); |
| 4850 | return ERR_PTR(-EIO); |
| 4851 | } |
| 4852 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
| 4853 | printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n", |
| 4854 | mdname(mddev), mddev->raid_disks); |
| 4855 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4856 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4857 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4858 | if (!mddev->new_chunk || mddev->new_chunk % PAGE_SIZE) { |
| 4859 | printk(KERN_ERR "raid5: invalid chunk size %d for %s\n", |
| 4860 | mddev->new_chunk, mdname(mddev)); |
| 4861 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4862 | } |
| 4863 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4864 | conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL); |
| 4865 | if (conf == NULL) |
| 4866 | goto abort; |
| 4867 | |
| 4868 | conf->raid_disks = mddev->raid_disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4869 | conf->scribble_len = scribble_len(conf->raid_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4870 | if (mddev->reshape_position == MaxSector) |
| 4871 | conf->previous_raid_disks = mddev->raid_disks; |
| 4872 | else |
| 4873 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
| 4874 | |
| 4875 | conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info), |
| 4876 | GFP_KERNEL); |
| 4877 | if (!conf->disks) |
| 4878 | goto abort; |
| 4879 | |
| 4880 | conf->mddev = mddev; |
| 4881 | |
| 4882 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 4883 | goto abort; |
| 4884 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4885 | conf->level = mddev->new_level; |
| 4886 | if (raid5_alloc_percpu(conf) != 0) |
| 4887 | goto abort; |
| 4888 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4889 | spin_lock_init(&conf->device_lock); |
| 4890 | init_waitqueue_head(&conf->wait_for_stripe); |
| 4891 | init_waitqueue_head(&conf->wait_for_overlap); |
| 4892 | INIT_LIST_HEAD(&conf->handle_list); |
| 4893 | INIT_LIST_HEAD(&conf->hold_list); |
| 4894 | INIT_LIST_HEAD(&conf->delayed_list); |
| 4895 | INIT_LIST_HEAD(&conf->bitmap_list); |
| 4896 | INIT_LIST_HEAD(&conf->inactive_list); |
| 4897 | atomic_set(&conf->active_stripes, 0); |
| 4898 | atomic_set(&conf->preread_active_stripes, 0); |
| 4899 | atomic_set(&conf->active_aligned_reads, 0); |
| 4900 | conf->bypass_threshold = BYPASS_THRESHOLD; |
| 4901 | |
| 4902 | pr_debug("raid5: run(%s) called.\n", mdname(mddev)); |
| 4903 | |
| 4904 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 4905 | raid_disk = rdev->raid_disk; |
| 4906 | if (raid_disk >= conf->raid_disks |
| 4907 | || raid_disk < 0) |
| 4908 | continue; |
| 4909 | disk = conf->disks + raid_disk; |
| 4910 | |
| 4911 | disk->rdev = rdev; |
| 4912 | |
| 4913 | if (test_bit(In_sync, &rdev->flags)) { |
| 4914 | char b[BDEVNAME_SIZE]; |
| 4915 | printk(KERN_INFO "raid5: device %s operational as raid" |
| 4916 | " disk %d\n", bdevname(rdev->bdev,b), |
| 4917 | raid_disk); |
| 4918 | } else |
| 4919 | /* Cannot rely on bitmap to complete recovery */ |
| 4920 | conf->fullsync = 1; |
| 4921 | } |
| 4922 | |
| 4923 | conf->chunk_size = mddev->new_chunk; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4924 | if (conf->level == 6) |
| 4925 | conf->max_degraded = 2; |
| 4926 | else |
| 4927 | conf->max_degraded = 1; |
| 4928 | conf->algorithm = mddev->new_layout; |
| 4929 | conf->max_nr_stripes = NR_STRIPES; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4930 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4931 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4932 | conf->prev_chunk = mddev->chunk_size; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4933 | conf->prev_algo = mddev->layout; |
| 4934 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4935 | |
| 4936 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
| 4937 | conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
| 4938 | if (grow_stripes(conf, conf->max_nr_stripes)) { |
| 4939 | printk(KERN_ERR |
| 4940 | "raid5: couldn't allocate %dkB for buffers\n", memory); |
| 4941 | goto abort; |
| 4942 | } else |
| 4943 | printk(KERN_INFO "raid5: allocated %dkB for %s\n", |
| 4944 | memory, mdname(mddev)); |
| 4945 | |
| 4946 | conf->thread = md_register_thread(raid5d, mddev, "%s_raid5"); |
| 4947 | if (!conf->thread) { |
| 4948 | printk(KERN_ERR |
| 4949 | "raid5: couldn't allocate thread for %s\n", |
| 4950 | mdname(mddev)); |
| 4951 | goto abort; |
| 4952 | } |
| 4953 | |
| 4954 | return conf; |
| 4955 | |
| 4956 | abort: |
| 4957 | if (conf) { |
Dan Williams | a11034b | 2009-07-14 11:48:16 -0700 | [diff] [blame] | 4958 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4959 | return ERR_PTR(-EIO); |
| 4960 | } else |
| 4961 | return ERR_PTR(-ENOMEM); |
| 4962 | } |
| 4963 | |
| 4964 | static int run(mddev_t *mddev) |
| 4965 | { |
| 4966 | raid5_conf_t *conf; |
| 4967 | int working_disks = 0; |
| 4968 | mdk_rdev_t *rdev; |
| 4969 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4970 | if (mddev->reshape_position != MaxSector) { |
| 4971 | /* Check that we can continue the reshape. |
| 4972 | * Currently only disks can change, it must |
| 4973 | * increase, and we must be past the point where |
| 4974 | * a stripe over-writes itself |
| 4975 | */ |
| 4976 | sector_t here_new, here_old; |
| 4977 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 4978 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4979 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 4980 | if (mddev->new_level != mddev->level) { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4981 | printk(KERN_ERR "raid5: %s: unsupported reshape " |
| 4982 | "required - aborting.\n", |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4983 | mdname(mddev)); |
| 4984 | return -EINVAL; |
| 4985 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4986 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 4987 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4988 | * further up in new geometry must map after here in old |
| 4989 | * geometry. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4990 | */ |
| 4991 | here_new = mddev->reshape_position; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4992 | if (sector_div(here_new, (mddev->new_chunk>>9)* |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4993 | (mddev->raid_disks - max_degraded))) { |
| 4994 | printk(KERN_ERR "raid5: reshape_position not " |
| 4995 | "on a stripe boundary\n"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4996 | return -EINVAL; |
| 4997 | } |
| 4998 | /* here_new is the stripe we will write to */ |
| 4999 | here_old = mddev->reshape_position; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5000 | sector_div(here_old, (mddev->chunk_size>>9)* |
| 5001 | (old_disks-max_degraded)); |
| 5002 | /* here_old is the first stripe that we might need to read |
| 5003 | * from */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5004 | if (here_new >= here_old) { |
| 5005 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5006 | printk(KERN_ERR "raid5: reshape_position too early for " |
| 5007 | "auto-recovery - aborting.\n"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5008 | return -EINVAL; |
| 5009 | } |
| 5010 | printk(KERN_INFO "raid5: reshape will continue\n"); |
| 5011 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5012 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5013 | BUG_ON(mddev->level != mddev->new_level); |
| 5014 | BUG_ON(mddev->layout != mddev->new_layout); |
| 5015 | BUG_ON(mddev->chunk_size != mddev->new_chunk); |
| 5016 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5017 | } |
| 5018 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5019 | if (mddev->private == NULL) |
| 5020 | conf = setup_conf(mddev); |
| 5021 | else |
| 5022 | conf = mddev->private; |
| 5023 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5024 | if (IS_ERR(conf)) |
| 5025 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5026 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5027 | mddev->thread = conf->thread; |
| 5028 | conf->thread = NULL; |
| 5029 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5032 | * 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] | 5033 | */ |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5034 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 5035 | if (rdev->raid_disk >= 0 && |
| 5036 | test_bit(In_sync, &rdev->flags)) |
| 5037 | working_disks++; |
| 5038 | |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5039 | mddev->degraded = conf->raid_disks - working_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5040 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5041 | if (mddev->degraded > conf->max_degraded) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5042 | printk(KERN_ERR "raid5: not enough operational devices for %s" |
| 5043 | " (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5044 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5045 | goto abort; |
| 5046 | } |
| 5047 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5048 | /* device size must be a multiple of chunk size */ |
| 5049 | mddev->dev_sectors &= ~(mddev->chunk_size / 512 - 1); |
| 5050 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5051 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5052 | if (mddev->degraded > 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5053 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5054 | if (mddev->ok_start_degraded) |
| 5055 | printk(KERN_WARNING |
| 5056 | "raid5: starting dirty degraded array: %s" |
| 5057 | "- data corruption possible.\n", |
| 5058 | mdname(mddev)); |
| 5059 | else { |
| 5060 | printk(KERN_ERR |
| 5061 | "raid5: cannot start dirty degraded array for %s\n", |
| 5062 | mdname(mddev)); |
| 5063 | goto abort; |
| 5064 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5065 | } |
| 5066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | if (mddev->degraded == 0) |
| 5068 | printk("raid5: raid level %d set %s active with %d out of %d" |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5069 | " devices, algorithm %d\n", conf->level, mdname(mddev), |
| 5070 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 5071 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5072 | else |
| 5073 | printk(KERN_ALERT "raid5: raid level %d set %s active with %d" |
| 5074 | " out of %d devices, algorithm %d\n", conf->level, |
| 5075 | mdname(mddev), mddev->raid_disks - mddev->degraded, |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5076 | mddev->raid_disks, mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5077 | |
| 5078 | print_raid5_conf(conf); |
| 5079 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5080 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5081 | printk("...ok start reshape thread\n"); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5082 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5083 | atomic_set(&conf->reshape_stripes, 0); |
| 5084 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5085 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5086 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5087 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5088 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
| 5089 | "%s_reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5090 | } |
| 5091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5092 | /* read-ahead size must cover two whole stripes, which is |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5093 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5094 | */ |
| 5095 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5096 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 5097 | int stripe = data_disks * |
NeilBrown | 8932c2e | 2006-06-26 00:27:36 -0700 | [diff] [blame] | 5098 | (mddev->chunk_size / PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5099 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5100 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 5101 | } |
| 5102 | |
| 5103 | /* Ok, everything is just fine now */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5104 | if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
| 5105 | printk(KERN_WARNING |
| 5106 | "raid5: failed to create sysfs attributes for %s\n", |
| 5107 | mdname(mddev)); |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 5108 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5109 | mddev->queue->queue_lock = &conf->device_lock; |
| 5110 | |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 5111 | mddev->queue->unplug_fn = raid5_unplug_device; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 5112 | mddev->queue->backing_dev_info.congested_data = mddev; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5113 | mddev->queue->backing_dev_info.congested_fn = raid5_congested; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 5114 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5115 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 5116 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 5117 | blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); |
| 5118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5119 | return 0; |
| 5120 | abort: |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5121 | md_unregister_thread(mddev->thread); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5122 | mddev->thread = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5123 | if (conf) { |
| 5124 | print_raid5_conf(conf); |
Dan Williams | a11034b | 2009-07-14 11:48:16 -0700 | [diff] [blame] | 5125 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5126 | } |
| 5127 | mddev->private = NULL; |
| 5128 | printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev)); |
| 5129 | return -EIO; |
| 5130 | } |
| 5131 | |
| 5132 | |
| 5133 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5134 | static int stop(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5135 | { |
| 5136 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 5137 | |
| 5138 | md_unregister_thread(mddev->thread); |
| 5139 | mddev->thread = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5140 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 5142 | sysfs_remove_group(&mddev->kobj, &raid5_attrs_group); |
Dan Williams | a11034b | 2009-07-14 11:48:16 -0700 | [diff] [blame] | 5143 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | mddev->private = NULL; |
| 5145 | return 0; |
| 5146 | } |
| 5147 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5148 | #ifdef DEBUG |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5149 | static void print_sh(struct seq_file *seq, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5150 | { |
| 5151 | int i; |
| 5152 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5153 | seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n", |
| 5154 | (unsigned long long)sh->sector, sh->pd_idx, sh->state); |
| 5155 | seq_printf(seq, "sh %llu, count %d.\n", |
| 5156 | (unsigned long long)sh->sector, atomic_read(&sh->count)); |
| 5157 | seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5158 | for (i = 0; i < sh->disks; i++) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5159 | seq_printf(seq, "(cache%d: %p %ld) ", |
| 5160 | i, sh->dev[i].page, sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5162 | seq_printf(seq, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5163 | } |
| 5164 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5165 | static void printall(struct seq_file *seq, raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | { |
| 5167 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 5168 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5169 | int i; |
| 5170 | |
| 5171 | spin_lock_irq(&conf->device_lock); |
| 5172 | for (i = 0; i < NR_HASH; i++) { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 5173 | hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5174 | if (sh->raid_conf != conf) |
| 5175 | continue; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5176 | print_sh(seq, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5177 | } |
| 5178 | } |
| 5179 | spin_unlock_irq(&conf->device_lock); |
| 5180 | } |
| 5181 | #endif |
| 5182 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5183 | static void status(struct seq_file *seq, mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | { |
| 5185 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 5186 | int i; |
| 5187 | |
| 5188 | seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5189 | 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] | 5190 | for (i = 0; i < conf->raid_disks; i++) |
| 5191 | seq_printf (seq, "%s", |
| 5192 | conf->disks[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5193 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5194 | seq_printf (seq, "]"); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5195 | #ifdef DEBUG |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5196 | seq_printf (seq, "\n"); |
| 5197 | printall(seq, conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5198 | #endif |
| 5199 | } |
| 5200 | |
| 5201 | static void print_raid5_conf (raid5_conf_t *conf) |
| 5202 | { |
| 5203 | int i; |
| 5204 | struct disk_info *tmp; |
| 5205 | |
| 5206 | printk("RAID5 conf printout:\n"); |
| 5207 | if (!conf) { |
| 5208 | printk("(conf==NULL)\n"); |
| 5209 | return; |
| 5210 | } |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5211 | printk(" --- rd:%d wd:%d\n", conf->raid_disks, |
| 5212 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | |
| 5214 | for (i = 0; i < conf->raid_disks; i++) { |
| 5215 | char b[BDEVNAME_SIZE]; |
| 5216 | tmp = conf->disks + i; |
| 5217 | if (tmp->rdev) |
| 5218 | printk(" disk %d, o:%d, dev:%s\n", |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5219 | i, !test_bit(Faulty, &tmp->rdev->flags), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | bdevname(tmp->rdev->bdev,b)); |
| 5221 | } |
| 5222 | } |
| 5223 | |
| 5224 | static int raid5_spare_active(mddev_t *mddev) |
| 5225 | { |
| 5226 | int i; |
| 5227 | raid5_conf_t *conf = mddev->private; |
| 5228 | struct disk_info *tmp; |
| 5229 | |
| 5230 | for (i = 0; i < conf->raid_disks; i++) { |
| 5231 | tmp = conf->disks + i; |
| 5232 | if (tmp->rdev |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5233 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5234 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
| 5235 | unsigned long flags; |
| 5236 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | mddev->degraded--; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5238 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5239 | } |
| 5240 | } |
| 5241 | print_raid5_conf(conf); |
| 5242 | return 0; |
| 5243 | } |
| 5244 | |
| 5245 | static int raid5_remove_disk(mddev_t *mddev, int number) |
| 5246 | { |
| 5247 | raid5_conf_t *conf = mddev->private; |
| 5248 | int err = 0; |
| 5249 | mdk_rdev_t *rdev; |
| 5250 | struct disk_info *p = conf->disks + number; |
| 5251 | |
| 5252 | print_raid5_conf(conf); |
| 5253 | rdev = p->rdev; |
| 5254 | if (rdev) { |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5255 | if (number >= conf->raid_disks && |
| 5256 | conf->reshape_progress == MaxSector) |
| 5257 | clear_bit(In_sync, &rdev->flags); |
| 5258 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5259 | if (test_bit(In_sync, &rdev->flags) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5260 | atomic_read(&rdev->nr_pending)) { |
| 5261 | err = -EBUSY; |
| 5262 | goto abort; |
| 5263 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5264 | /* Only remove non-faulty devices if recovery |
| 5265 | * isn't possible. |
| 5266 | */ |
| 5267 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5268 | mddev->degraded <= conf->max_degraded && |
| 5269 | number < conf->raid_disks) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5270 | err = -EBUSY; |
| 5271 | goto abort; |
| 5272 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | p->rdev = NULL; |
Paul E. McKenney | fbd568a3e | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 5274 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5275 | if (atomic_read(&rdev->nr_pending)) { |
| 5276 | /* lost the race, try later */ |
| 5277 | err = -EBUSY; |
| 5278 | p->rdev = rdev; |
| 5279 | } |
| 5280 | } |
| 5281 | abort: |
| 5282 | |
| 5283 | print_raid5_conf(conf); |
| 5284 | return err; |
| 5285 | } |
| 5286 | |
| 5287 | static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5288 | { |
| 5289 | raid5_conf_t *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5290 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5291 | int disk; |
| 5292 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5293 | int first = 0; |
| 5294 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5295 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5296 | if (mddev->degraded > conf->max_degraded) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5298 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5299 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5300 | if (rdev->raid_disk >= 0) |
| 5301 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | |
| 5303 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5304 | * find the disk ... but prefer rdev->saved_raid_disk |
| 5305 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5306 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5307 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5308 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5309 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
| 5310 | disk = rdev->saved_raid_disk; |
| 5311 | else |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5312 | disk = first; |
| 5313 | for ( ; disk <= last ; disk++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5314 | if ((p=conf->disks + disk)->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5315 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | rdev->raid_disk = disk; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5317 | err = 0; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5318 | if (rdev->saved_raid_disk != disk) |
| 5319 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 5320 | rcu_assign_pointer(p->rdev, rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5321 | break; |
| 5322 | } |
| 5323 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5324 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | } |
| 5326 | |
| 5327 | static int raid5_resize(mddev_t *mddev, sector_t sectors) |
| 5328 | { |
| 5329 | /* no resync is happening, and there is enough space |
| 5330 | * on all devices, so we can resize. |
| 5331 | * We need to make sure resync covers any new space. |
| 5332 | * If the array is shrinking we should possibly wait until |
| 5333 | * any io in the removed space completes, but it hardly seems |
| 5334 | * worth it. |
| 5335 | */ |
| 5336 | sectors &= ~((sector_t)mddev->chunk_size/512 - 1); |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5337 | md_set_array_sectors(mddev, raid5_size(mddev, sectors, |
| 5338 | mddev->raid_disks)); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5339 | if (mddev->array_sectors > |
| 5340 | raid5_size(mddev, sectors, mddev->raid_disks)) |
| 5341 | return -EINVAL; |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5342 | set_capacity(mddev->gendisk, mddev->array_sectors); |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5343 | mddev->changed = 1; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5344 | if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) { |
| 5345 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5347 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5348 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 5349 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5350 | return 0; |
| 5351 | } |
| 5352 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5353 | static int raid5_check_reshape(mddev_t *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5354 | { |
| 5355 | raid5_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5356 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5357 | if (mddev->delta_disks == 0 && |
| 5358 | mddev->new_layout == mddev->layout && |
| 5359 | mddev->new_chunk == mddev->chunk_size) |
| 5360 | return -EINVAL; /* nothing to do */ |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5361 | if (mddev->bitmap) |
| 5362 | /* Cannot grow a bitmap yet */ |
| 5363 | return -EBUSY; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5364 | if (mddev->degraded > conf->max_degraded) |
| 5365 | return -EINVAL; |
| 5366 | if (mddev->delta_disks < 0) { |
| 5367 | /* We might be able to shrink, but the devices must |
| 5368 | * be made bigger first. |
| 5369 | * For raid6, 4 is the minimum size. |
| 5370 | * Otherwise 2 is the minimum |
| 5371 | */ |
| 5372 | int min = 2; |
| 5373 | if (mddev->level == 6) |
| 5374 | min = 4; |
| 5375 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 5376 | return -EINVAL; |
| 5377 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5378 | |
| 5379 | /* Can only proceed if there are plenty of stripe_heads. |
| 5380 | * We need a minimum of one full stripe,, and for sensible progress |
| 5381 | * it is best to have about 4 times that. |
| 5382 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 5383 | * allow for chunk sizes up to 256K, which is probably OK. |
| 5384 | * If the chunk size is greater, user-space should request more |
| 5385 | * stripe_heads first. |
| 5386 | */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5387 | if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes || |
| 5388 | (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5389 | printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n", |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 5390 | (max(mddev->chunk_size, mddev->new_chunk) |
| 5391 | / STRIPE_SIZE)*4); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5392 | return -ENOSPC; |
| 5393 | } |
| 5394 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5395 | return resize_stripes(conf, conf->raid_disks + mddev->delta_disks); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5396 | } |
| 5397 | |
| 5398 | static int raid5_start_reshape(mddev_t *mddev) |
| 5399 | { |
| 5400 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 5401 | mdk_rdev_t *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5402 | int spares = 0; |
| 5403 | int added_devices = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5404 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5405 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5406 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5407 | return -EBUSY; |
| 5408 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5409 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5410 | if (rdev->raid_disk < 0 && |
| 5411 | !test_bit(Faulty, &rdev->flags)) |
| 5412 | spares++; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5413 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5414 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5415 | /* Not enough devices even to make a degraded array |
| 5416 | * of that size |
| 5417 | */ |
| 5418 | return -EINVAL; |
| 5419 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5420 | /* Refuse to reduce size of the array. Any reductions in |
| 5421 | * array size must be through explicit setting of array_size |
| 5422 | * attribute. |
| 5423 | */ |
| 5424 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 5425 | < mddev->array_sectors) { |
| 5426 | printk(KERN_ERR "md: %s: array size must be reduced " |
| 5427 | "before number of disks\n", mdname(mddev)); |
| 5428 | return -EINVAL; |
| 5429 | } |
| 5430 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5431 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5432 | spin_lock_irq(&conf->device_lock); |
| 5433 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5434 | conf->raid_disks += mddev->delta_disks; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5435 | conf->prev_chunk = conf->chunk_size; |
| 5436 | conf->chunk_size = mddev->new_chunk; |
| 5437 | conf->prev_algo = conf->algorithm; |
| 5438 | conf->algorithm = mddev->new_layout; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5439 | if (mddev->delta_disks < 0) |
| 5440 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 5441 | else |
| 5442 | conf->reshape_progress = 0; |
| 5443 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 5444 | conf->generation++; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5445 | spin_unlock_irq(&conf->device_lock); |
| 5446 | |
| 5447 | /* Add some new drives, as many as will fit. |
| 5448 | * We know there are enough to make the newly sized array work. |
| 5449 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5450 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5451 | if (rdev->raid_disk < 0 && |
| 5452 | !test_bit(Faulty, &rdev->flags)) { |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5453 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5454 | char nm[20]; |
| 5455 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5456 | added_devices++; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5457 | rdev->recovery_offset = 0; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5458 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5459 | if (sysfs_create_link(&mddev->kobj, |
| 5460 | &rdev->kobj, nm)) |
| 5461 | printk(KERN_WARNING |
| 5462 | "raid5: failed to create " |
| 5463 | " link %s for %s\n", |
| 5464 | nm, mdname(mddev)); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5465 | } else |
| 5466 | break; |
| 5467 | } |
| 5468 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5469 | if (mddev->delta_disks > 0) { |
| 5470 | spin_lock_irqsave(&conf->device_lock, flags); |
| 5471 | mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) |
| 5472 | - added_devices; |
| 5473 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 5474 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5475 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5476 | mddev->reshape_position = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5477 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5478 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5479 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5480 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5481 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5482 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5483 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
| 5484 | "%s_reshape"); |
| 5485 | if (!mddev->sync_thread) { |
| 5486 | mddev->recovery = 0; |
| 5487 | spin_lock_irq(&conf->device_lock); |
| 5488 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5489 | conf->reshape_progress = MaxSector; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5490 | spin_unlock_irq(&conf->device_lock); |
| 5491 | return -EAGAIN; |
| 5492 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5493 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5494 | md_wakeup_thread(mddev->sync_thread); |
| 5495 | md_new_event(mddev); |
| 5496 | return 0; |
| 5497 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5498 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5499 | /* This is called from the reshape thread and should make any |
| 5500 | * changes needed in 'conf' |
| 5501 | */ |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5502 | static void end_reshape(raid5_conf_t *conf) |
| 5503 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5504 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5505 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5506 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5507 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5508 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5509 | conf->reshape_progress = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5510 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5511 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5512 | |
| 5513 | /* read-ahead size must cover two whole stripes, which is |
| 5514 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 5515 | */ |
| 5516 | { |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5517 | int data_disks = conf->raid_disks - conf->max_degraded; |
| 5518 | int stripe = data_disks * (conf->chunk_size |
| 5519 | / PAGE_SIZE); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5520 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5521 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 5522 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5523 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5524 | } |
| 5525 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5526 | /* This is called from the raid5d thread with mddev_lock held. |
| 5527 | * It makes config changes to the device. |
| 5528 | */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5529 | static void raid5_finish_reshape(mddev_t *mddev) |
| 5530 | { |
| 5531 | struct block_device *bdev; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5532 | raid5_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5533 | |
| 5534 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5535 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5536 | if (mddev->delta_disks > 0) { |
| 5537 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 5538 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5539 | mddev->changed = 1; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5540 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5541 | bdev = bdget_disk(mddev->gendisk, 0); |
| 5542 | if (bdev) { |
| 5543 | mutex_lock(&bdev->bd_inode->i_mutex); |
| 5544 | i_size_write(bdev->bd_inode, |
| 5545 | (loff_t)mddev->array_sectors << 9); |
| 5546 | mutex_unlock(&bdev->bd_inode->i_mutex); |
| 5547 | bdput(bdev); |
| 5548 | } |
| 5549 | } else { |
| 5550 | int d; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5551 | mddev->degraded = conf->raid_disks; |
| 5552 | for (d = 0; d < conf->raid_disks ; d++) |
| 5553 | if (conf->disks[d].rdev && |
| 5554 | test_bit(In_sync, |
| 5555 | &conf->disks[d].rdev->flags)) |
| 5556 | mddev->degraded--; |
| 5557 | for (d = conf->raid_disks ; |
| 5558 | d < conf->raid_disks - mddev->delta_disks; |
| 5559 | d++) |
| 5560 | raid5_remove_disk(mddev, d); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5561 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5562 | mddev->layout = conf->algorithm; |
| 5563 | mddev->chunk_size = conf->chunk_size; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5564 | mddev->reshape_position = MaxSector; |
| 5565 | mddev->delta_disks = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5566 | } |
| 5567 | } |
| 5568 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5569 | static void raid5_quiesce(mddev_t *mddev, int state) |
| 5570 | { |
| 5571 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 5572 | |
| 5573 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5574 | case 2: /* resume for a suspend */ |
| 5575 | wake_up(&conf->wait_for_overlap); |
| 5576 | break; |
| 5577 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5578 | case 1: /* stop all writes */ |
| 5579 | spin_lock_irq(&conf->device_lock); |
| 5580 | conf->quiesce = 1; |
| 5581 | wait_event_lock_irq(conf->wait_for_stripe, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5582 | atomic_read(&conf->active_stripes) == 0 && |
| 5583 | atomic_read(&conf->active_aligned_reads) == 0, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5584 | conf->device_lock, /* nothing */); |
| 5585 | spin_unlock_irq(&conf->device_lock); |
| 5586 | break; |
| 5587 | |
| 5588 | case 0: /* re-enable writes */ |
| 5589 | spin_lock_irq(&conf->device_lock); |
| 5590 | conf->quiesce = 0; |
| 5591 | wake_up(&conf->wait_for_stripe); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5592 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5593 | spin_unlock_irq(&conf->device_lock); |
| 5594 | break; |
| 5595 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5596 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5597 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5598 | |
| 5599 | static void *raid5_takeover_raid1(mddev_t *mddev) |
| 5600 | { |
| 5601 | int chunksect; |
| 5602 | |
| 5603 | if (mddev->raid_disks != 2 || |
| 5604 | mddev->degraded > 1) |
| 5605 | return ERR_PTR(-EINVAL); |
| 5606 | |
| 5607 | /* Should check if there are write-behind devices? */ |
| 5608 | |
| 5609 | chunksect = 64*2; /* 64K by default */ |
| 5610 | |
| 5611 | /* The array must be an exact multiple of chunksize */ |
| 5612 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 5613 | chunksect >>= 1; |
| 5614 | |
| 5615 | if ((chunksect<<9) < STRIPE_SIZE) |
| 5616 | /* array size does not allow a suitable chunk size */ |
| 5617 | return ERR_PTR(-EINVAL); |
| 5618 | |
| 5619 | mddev->new_level = 5; |
| 5620 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 5621 | mddev->new_chunk = chunksect << 9; |
| 5622 | |
| 5623 | return setup_conf(mddev); |
| 5624 | } |
| 5625 | |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 5626 | static void *raid5_takeover_raid6(mddev_t *mddev) |
| 5627 | { |
| 5628 | int new_layout; |
| 5629 | |
| 5630 | switch (mddev->layout) { |
| 5631 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 5632 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 5633 | break; |
| 5634 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 5635 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 5636 | break; |
| 5637 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 5638 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 5639 | break; |
| 5640 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 5641 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 5642 | break; |
| 5643 | case ALGORITHM_PARITY_0_6: |
| 5644 | new_layout = ALGORITHM_PARITY_0; |
| 5645 | break; |
| 5646 | case ALGORITHM_PARITY_N: |
| 5647 | new_layout = ALGORITHM_PARITY_N; |
| 5648 | break; |
| 5649 | default: |
| 5650 | return ERR_PTR(-EINVAL); |
| 5651 | } |
| 5652 | mddev->new_level = 5; |
| 5653 | mddev->new_layout = new_layout; |
| 5654 | mddev->delta_disks = -1; |
| 5655 | mddev->raid_disks -= 1; |
| 5656 | return setup_conf(mddev); |
| 5657 | } |
| 5658 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5659 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5660 | static int raid5_reconfig(mddev_t *mddev, int new_layout, int new_chunk) |
| 5661 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5662 | /* For a 2-drive array, the layout and chunk size can be changed |
| 5663 | * immediately as not restriping is needed. |
| 5664 | * For larger arrays we record the new value - after validation |
| 5665 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5666 | */ |
| 5667 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 5668 | |
| 5669 | if (new_layout >= 0 && !algorithm_valid_raid5(new_layout)) |
| 5670 | return -EINVAL; |
| 5671 | if (new_chunk > 0) { |
| 5672 | if (new_chunk & (new_chunk-1)) |
| 5673 | /* not a power of 2 */ |
| 5674 | return -EINVAL; |
| 5675 | if (new_chunk < PAGE_SIZE) |
| 5676 | return -EINVAL; |
| 5677 | if (mddev->array_sectors & ((new_chunk>>9)-1)) |
| 5678 | /* not factor of array size */ |
| 5679 | return -EINVAL; |
| 5680 | } |
| 5681 | |
| 5682 | /* They look valid */ |
| 5683 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5684 | if (mddev->raid_disks == 2) { |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5685 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5686 | if (new_layout >= 0) { |
| 5687 | conf->algorithm = new_layout; |
| 5688 | mddev->layout = mddev->new_layout = new_layout; |
| 5689 | } |
| 5690 | if (new_chunk > 0) { |
| 5691 | conf->chunk_size = new_chunk; |
| 5692 | mddev->chunk_size = mddev->new_chunk = new_chunk; |
| 5693 | } |
| 5694 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 5695 | md_wakeup_thread(mddev->thread); |
| 5696 | } else { |
| 5697 | if (new_layout >= 0) |
| 5698 | mddev->new_layout = new_layout; |
| 5699 | if (new_chunk > 0) |
| 5700 | mddev->new_chunk = new_chunk; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5701 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5702 | return 0; |
| 5703 | } |
| 5704 | |
| 5705 | static int raid6_reconfig(mddev_t *mddev, int new_layout, int new_chunk) |
| 5706 | { |
| 5707 | if (new_layout >= 0 && !algorithm_valid_raid6(new_layout)) |
| 5708 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5709 | if (new_chunk > 0) { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5710 | if (new_chunk & (new_chunk-1)) |
| 5711 | /* not a power of 2 */ |
| 5712 | return -EINVAL; |
| 5713 | if (new_chunk < PAGE_SIZE) |
| 5714 | return -EINVAL; |
| 5715 | if (mddev->array_sectors & ((new_chunk>>9)-1)) |
| 5716 | /* not factor of array size */ |
| 5717 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5718 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5719 | |
| 5720 | /* They look valid */ |
| 5721 | |
| 5722 | if (new_layout >= 0) |
| 5723 | mddev->new_layout = new_layout; |
| 5724 | if (new_chunk > 0) |
| 5725 | mddev->new_chunk = new_chunk; |
| 5726 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5727 | return 0; |
| 5728 | } |
| 5729 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5730 | static void *raid5_takeover(mddev_t *mddev) |
| 5731 | { |
| 5732 | /* raid5 can take over: |
| 5733 | * raid0 - if all devices are the same - make it a raid4 layout |
| 5734 | * raid1 - if there are two drives. We need to know the chunk size |
| 5735 | * raid4 - trivial - just use a raid4 layout. |
| 5736 | * raid6 - Providing it is a *_6 layout |
| 5737 | * |
| 5738 | * For now, just do raid1 |
| 5739 | */ |
| 5740 | |
| 5741 | if (mddev->level == 1) |
| 5742 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 5743 | if (mddev->level == 4) { |
| 5744 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 5745 | mddev->new_level = 5; |
| 5746 | return setup_conf(mddev); |
| 5747 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 5748 | if (mddev->level == 6) |
| 5749 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5750 | |
| 5751 | return ERR_PTR(-EINVAL); |
| 5752 | } |
| 5753 | |
| 5754 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5755 | static struct mdk_personality raid5_personality; |
| 5756 | |
| 5757 | static void *raid6_takeover(mddev_t *mddev) |
| 5758 | { |
| 5759 | /* Currently can only take over a raid5. We map the |
| 5760 | * personality to an equivalent raid6 personality |
| 5761 | * with the Q block at the end. |
| 5762 | */ |
| 5763 | int new_layout; |
| 5764 | |
| 5765 | if (mddev->pers != &raid5_personality) |
| 5766 | return ERR_PTR(-EINVAL); |
| 5767 | if (mddev->degraded > 1) |
| 5768 | return ERR_PTR(-EINVAL); |
| 5769 | if (mddev->raid_disks > 253) |
| 5770 | return ERR_PTR(-EINVAL); |
| 5771 | if (mddev->raid_disks < 3) |
| 5772 | return ERR_PTR(-EINVAL); |
| 5773 | |
| 5774 | switch (mddev->layout) { |
| 5775 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 5776 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 5777 | break; |
| 5778 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 5779 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 5780 | break; |
| 5781 | case ALGORITHM_LEFT_SYMMETRIC: |
| 5782 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 5783 | break; |
| 5784 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 5785 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 5786 | break; |
| 5787 | case ALGORITHM_PARITY_0: |
| 5788 | new_layout = ALGORITHM_PARITY_0_6; |
| 5789 | break; |
| 5790 | case ALGORITHM_PARITY_N: |
| 5791 | new_layout = ALGORITHM_PARITY_N; |
| 5792 | break; |
| 5793 | default: |
| 5794 | return ERR_PTR(-EINVAL); |
| 5795 | } |
| 5796 | mddev->new_level = 6; |
| 5797 | mddev->new_layout = new_layout; |
| 5798 | mddev->delta_disks = 1; |
| 5799 | mddev->raid_disks += 1; |
| 5800 | return setup_conf(mddev); |
| 5801 | } |
| 5802 | |
| 5803 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5804 | static struct mdk_personality raid6_personality = |
| 5805 | { |
| 5806 | .name = "raid6", |
| 5807 | .level = 6, |
| 5808 | .owner = THIS_MODULE, |
| 5809 | .make_request = make_request, |
| 5810 | .run = run, |
| 5811 | .stop = stop, |
| 5812 | .status = status, |
| 5813 | .error_handler = error, |
| 5814 | .hot_add_disk = raid5_add_disk, |
| 5815 | .hot_remove_disk= raid5_remove_disk, |
| 5816 | .spare_active = raid5_spare_active, |
| 5817 | .sync_request = sync_request, |
| 5818 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5819 | .size = raid5_size, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5820 | .check_reshape = raid5_check_reshape, |
| 5821 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5822 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5823 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5824 | .takeover = raid6_takeover, |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5825 | .reconfig = raid6_reconfig, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5826 | }; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5827 | static struct mdk_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | { |
| 5829 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5830 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5831 | .owner = THIS_MODULE, |
| 5832 | .make_request = make_request, |
| 5833 | .run = run, |
| 5834 | .stop = stop, |
| 5835 | .status = status, |
| 5836 | .error_handler = error, |
| 5837 | .hot_add_disk = raid5_add_disk, |
| 5838 | .hot_remove_disk= raid5_remove_disk, |
| 5839 | .spare_active = raid5_spare_active, |
| 5840 | .sync_request = sync_request, |
| 5841 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5842 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5843 | .check_reshape = raid5_check_reshape, |
| 5844 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5845 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5846 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5847 | .takeover = raid5_takeover, |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5848 | .reconfig = raid5_reconfig, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5849 | }; |
| 5850 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5851 | static struct mdk_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5852 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5853 | .name = "raid4", |
| 5854 | .level = 4, |
| 5855 | .owner = THIS_MODULE, |
| 5856 | .make_request = make_request, |
| 5857 | .run = run, |
| 5858 | .stop = stop, |
| 5859 | .status = status, |
| 5860 | .error_handler = error, |
| 5861 | .hot_add_disk = raid5_add_disk, |
| 5862 | .hot_remove_disk= raid5_remove_disk, |
| 5863 | .spare_active = raid5_spare_active, |
| 5864 | .sync_request = sync_request, |
| 5865 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5866 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 5867 | .check_reshape = raid5_check_reshape, |
| 5868 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5869 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5870 | .quiesce = raid5_quiesce, |
| 5871 | }; |
| 5872 | |
| 5873 | static int __init raid5_init(void) |
| 5874 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5875 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5876 | register_md_personality(&raid5_personality); |
| 5877 | register_md_personality(&raid4_personality); |
| 5878 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5879 | } |
| 5880 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5881 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5882 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5883 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5884 | unregister_md_personality(&raid5_personality); |
| 5885 | unregister_md_personality(&raid4_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5886 | } |
| 5887 | |
| 5888 | module_init(raid5_init); |
| 5889 | module_exit(raid5_exit); |
| 5890 | MODULE_LICENSE("GPL"); |
| 5891 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5892 | MODULE_ALIAS("md-raid5"); |
| 5893 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5894 | MODULE_ALIAS("md-level-5"); |
| 5895 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5896 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 5897 | MODULE_ALIAS("md-raid6"); |
| 5898 | MODULE_ALIAS("md-level-6"); |
| 5899 | |
| 5900 | /* This used to be two separate modules, they were: */ |
| 5901 | MODULE_ALIAS("raid5"); |
| 5902 | MODULE_ALIAS("raid6"); |