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 | |
| 626 | static void ops_complete_compute5(void *stripe_head_ref) |
| 627 | { |
| 628 | struct stripe_head *sh = stripe_head_ref; |
| 629 | int target = sh->ops.target; |
| 630 | struct r5dev *tgt = &sh->dev[target]; |
| 631 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 632 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 633 | (unsigned long long)sh->sector); |
| 634 | |
| 635 | set_bit(R5_UPTODATE, &tgt->flags); |
| 636 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 637 | clear_bit(R5_Wantcompute, &tgt->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 638 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 639 | if (sh->check_state == check_state_compute_run) |
| 640 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 641 | set_bit(STRIPE_HANDLE, &sh->state); |
| 642 | release_stripe(sh); |
| 643 | } |
| 644 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 645 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 646 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
| 647 | struct raid5_percpu *percpu) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 648 | { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 649 | return percpu->scribble + sizeof(struct page *) * (sh->disks + 2); |
| 650 | } |
| 651 | |
| 652 | static struct dma_async_tx_descriptor * |
| 653 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 654 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 655 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 656 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 657 | int target = sh->ops.target; |
| 658 | struct r5dev *tgt = &sh->dev[target]; |
| 659 | struct page *xor_dest = tgt->page; |
| 660 | int count = 0; |
| 661 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 662 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 663 | int i; |
| 664 | |
| 665 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 666 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 667 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 668 | |
| 669 | for (i = disks; i--; ) |
| 670 | if (i != target) |
| 671 | xor_srcs[count++] = sh->dev[i].page; |
| 672 | |
| 673 | atomic_inc(&sh->count); |
| 674 | |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 675 | init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 676 | ops_complete_compute5, sh, to_addr_conv(sh, percpu)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 677 | if (unlikely(count == 1)) |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 678 | 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] | 679 | else |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 680 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 681 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 682 | return tx; |
| 683 | } |
| 684 | |
| 685 | static void ops_complete_prexor(void *stripe_head_ref) |
| 686 | { |
| 687 | struct stripe_head *sh = stripe_head_ref; |
| 688 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 689 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 690 | (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | static struct dma_async_tx_descriptor * |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 694 | ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 695 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 696 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 697 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 698 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 699 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 700 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 701 | |
| 702 | /* existing parity data subtracted */ |
| 703 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 704 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 705 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 706 | (unsigned long long)sh->sector); |
| 707 | |
| 708 | for (i = disks; i--; ) { |
| 709 | struct r5dev *dev = &sh->dev[i]; |
| 710 | /* Only process blocks that are known to be uptodate */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 711 | if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 712 | xor_srcs[count++] = dev->page; |
| 713 | } |
| 714 | |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 715 | init_async_submit(&submit, ASYNC_TX_XOR_DROP_DST, tx, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 716 | ops_complete_prexor, sh, to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 717 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 718 | |
| 719 | return tx; |
| 720 | } |
| 721 | |
| 722 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 723 | 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] | 724 | { |
| 725 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 726 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 727 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 728 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 729 | (unsigned long long)sh->sector); |
| 730 | |
| 731 | for (i = disks; i--; ) { |
| 732 | struct r5dev *dev = &sh->dev[i]; |
| 733 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 734 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 735 | if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 736 | struct bio *wbi; |
| 737 | |
| 738 | spin_lock(&sh->lock); |
| 739 | chosen = dev->towrite; |
| 740 | dev->towrite = NULL; |
| 741 | BUG_ON(dev->written); |
| 742 | wbi = dev->written = chosen; |
| 743 | spin_unlock(&sh->lock); |
| 744 | |
| 745 | while (wbi && wbi->bi_sector < |
| 746 | dev->sector + STRIPE_SECTORS) { |
| 747 | tx = async_copy_data(1, wbi, dev->page, |
| 748 | dev->sector, tx); |
| 749 | wbi = r5_next_bio(wbi, dev->sector); |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | return tx; |
| 755 | } |
| 756 | |
| 757 | static void ops_complete_postxor(void *stripe_head_ref) |
| 758 | { |
| 759 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 760 | int disks = sh->disks, i, pd_idx = sh->pd_idx; |
| 761 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 762 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 763 | (unsigned long long)sh->sector); |
| 764 | |
| 765 | for (i = disks; i--; ) { |
| 766 | struct r5dev *dev = &sh->dev[i]; |
| 767 | if (dev->written || i == pd_idx) |
| 768 | set_bit(R5_UPTODATE, &dev->flags); |
| 769 | } |
| 770 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 771 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 772 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 773 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 774 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 775 | else { |
| 776 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 777 | sh->reconstruct_state = reconstruct_state_result; |
| 778 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 779 | |
| 780 | set_bit(STRIPE_HANDLE, &sh->state); |
| 781 | release_stripe(sh); |
| 782 | } |
| 783 | |
| 784 | static void |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 785 | ops_run_postxor(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 786 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 787 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 788 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 789 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 790 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 791 | int count = 0, pd_idx = sh->pd_idx, i; |
| 792 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 793 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 794 | unsigned long flags; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 795 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 796 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 797 | (unsigned long long)sh->sector); |
| 798 | |
| 799 | /* check if prexor is active which means only process blocks |
| 800 | * that are part of a read-modify-write (written) |
| 801 | */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 802 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 803 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 804 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 805 | for (i = disks; i--; ) { |
| 806 | struct r5dev *dev = &sh->dev[i]; |
| 807 | if (dev->written) |
| 808 | xor_srcs[count++] = dev->page; |
| 809 | } |
| 810 | } else { |
| 811 | xor_dest = sh->dev[pd_idx].page; |
| 812 | for (i = disks; i--; ) { |
| 813 | struct r5dev *dev = &sh->dev[i]; |
| 814 | if (i != pd_idx) |
| 815 | xor_srcs[count++] = dev->page; |
| 816 | } |
| 817 | } |
| 818 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 819 | /* 1/ if we prexor'd then the dest is reused as a source |
| 820 | * 2/ if we did not prexor then we are redoing the parity |
| 821 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 822 | * for the synchronous xor case |
| 823 | */ |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 824 | flags = ASYNC_TX_ACK | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 825 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
| 826 | |
| 827 | atomic_inc(&sh->count); |
| 828 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 829 | init_async_submit(&submit, flags, tx, ops_complete_postxor, sh, |
| 830 | to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 831 | if (unlikely(count == 1)) |
| 832 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
| 833 | else |
| 834 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | static void ops_complete_check(void *stripe_head_ref) |
| 838 | { |
| 839 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 840 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 841 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 842 | (unsigned long long)sh->sector); |
| 843 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 844 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 845 | set_bit(STRIPE_HANDLE, &sh->state); |
| 846 | release_stripe(sh); |
| 847 | } |
| 848 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 849 | static void ops_run_check(struct stripe_head *sh, struct raid5_percpu *percpu) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 850 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 851 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 852 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 853 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 854 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 855 | |
| 856 | int count = 0, pd_idx = sh->pd_idx, i; |
| 857 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 858 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 859 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 860 | (unsigned long long)sh->sector); |
| 861 | |
| 862 | for (i = disks; i--; ) { |
| 863 | struct r5dev *dev = &sh->dev[i]; |
| 864 | if (i != pd_idx) |
| 865 | xor_srcs[count++] = dev->page; |
| 866 | } |
| 867 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 868 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
| 869 | to_addr_conv(sh, percpu)); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 870 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 871 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 872 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 873 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 874 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 875 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 876 | } |
| 877 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 878 | static void raid5_run_ops(struct stripe_head *sh, unsigned long ops_request) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 879 | { |
| 880 | int overlap_clear = 0, i, disks = sh->disks; |
| 881 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 882 | raid5_conf_t *conf = sh->raid_conf; |
| 883 | struct raid5_percpu *percpu; |
| 884 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 885 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 886 | cpu = get_cpu(); |
| 887 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 888 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 889 | ops_run_biofill(sh); |
| 890 | overlap_clear++; |
| 891 | } |
| 892 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 893 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 894 | tx = ops_run_compute5(sh, percpu); |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 895 | /* terminate the chain if postxor is not set to be run */ |
| 896 | if (tx && !test_bit(STRIPE_OP_POSTXOR, &ops_request)) |
| 897 | async_tx_ack(tx); |
| 898 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 899 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 900 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 901 | tx = ops_run_prexor(sh, percpu, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 902 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 903 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 904 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 905 | overlap_clear++; |
| 906 | } |
| 907 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 908 | if (test_bit(STRIPE_OP_POSTXOR, &ops_request)) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 909 | ops_run_postxor(sh, percpu, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 910 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 911 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 912 | ops_run_check(sh, percpu); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 913 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 914 | if (overlap_clear) |
| 915 | for (i = disks; i--; ) { |
| 916 | struct r5dev *dev = &sh->dev[i]; |
| 917 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 918 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 919 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 920 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 921 | } |
| 922 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 923 | static int grow_one_stripe(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | { |
| 925 | struct stripe_head *sh; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 926 | sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL); |
| 927 | if (!sh) |
| 928 | return 0; |
| 929 | memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev)); |
| 930 | sh->raid_conf = conf; |
| 931 | spin_lock_init(&sh->lock); |
| 932 | |
| 933 | if (grow_buffers(sh, conf->raid_disks)) { |
| 934 | shrink_buffers(sh, conf->raid_disks); |
| 935 | kmem_cache_free(conf->slab_cache, sh); |
| 936 | return 0; |
| 937 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 938 | sh->disks = conf->raid_disks; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 939 | /* we just created an active stripe so... */ |
| 940 | atomic_set(&sh->count, 1); |
| 941 | atomic_inc(&conf->active_stripes); |
| 942 | INIT_LIST_HEAD(&sh->lru); |
| 943 | release_stripe(sh); |
| 944 | return 1; |
| 945 | } |
| 946 | |
| 947 | static int grow_stripes(raid5_conf_t *conf, int num) |
| 948 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 949 | struct kmem_cache *sc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | int devs = conf->raid_disks; |
| 951 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 952 | sprintf(conf->cache_name[0], |
| 953 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 954 | sprintf(conf->cache_name[1], |
| 955 | "raid%d-%s-alt", conf->level, mdname(conf->mddev)); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 956 | conf->active_name = 0; |
| 957 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 959 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | if (!sc) |
| 961 | return 1; |
| 962 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 963 | conf->pool_size = devs; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 964 | while (num--) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 965 | if (!grow_one_stripe(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | return 0; |
| 968 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 969 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 970 | /** |
| 971 | * scribble_len - return the required size of the scribble region |
| 972 | * @num - total number of disks in the array |
| 973 | * |
| 974 | * The size must be enough to contain: |
| 975 | * 1/ a struct page pointer for each device in the array +2 |
| 976 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 977 | * (dma_map_page()) or page (page_address()) address. |
| 978 | * |
| 979 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 980 | * calculate over all devices (not just the data blocks), using zeros in place |
| 981 | * of the P and Q blocks. |
| 982 | */ |
| 983 | static size_t scribble_len(int num) |
| 984 | { |
| 985 | size_t len; |
| 986 | |
| 987 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); |
| 988 | |
| 989 | return len; |
| 990 | } |
| 991 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 992 | static int resize_stripes(raid5_conf_t *conf, int newsize) |
| 993 | { |
| 994 | /* Make all the stripes able to hold 'newsize' devices. |
| 995 | * New slots in each stripe get 'page' set to a new page. |
| 996 | * |
| 997 | * This happens in stages: |
| 998 | * 1/ create a new kmem_cache and allocate the required number of |
| 999 | * stripe_heads. |
| 1000 | * 2/ gather all the old stripe_heads and tranfer the pages across |
| 1001 | * to the new stripe_heads. This will have the side effect of |
| 1002 | * freezing the array as once all stripe_heads have been collected, |
| 1003 | * no IO will be possible. Old stripe heads are freed once their |
| 1004 | * pages have been transferred over, and the old kmem_cache is |
| 1005 | * freed when all stripes are done. |
| 1006 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 1007 | * we simple return a failre status - no need to clean anything up. |
| 1008 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 1009 | * If this fails, we don't bother trying the shrink the |
| 1010 | * stripe_heads down again, we just leave them as they are. |
| 1011 | * As each stripe_head is processed the new one is released into |
| 1012 | * active service. |
| 1013 | * |
| 1014 | * Once step2 is started, we cannot afford to wait for a write, |
| 1015 | * so we use GFP_NOIO allocations. |
| 1016 | */ |
| 1017 | struct stripe_head *osh, *nsh; |
| 1018 | LIST_HEAD(newstripes); |
| 1019 | struct disk_info *ndisks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1020 | unsigned long cpu; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1021 | int err; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1022 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1023 | int i; |
| 1024 | |
| 1025 | if (newsize <= conf->pool_size) |
| 1026 | return 0; /* never bother to shrink */ |
| 1027 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1028 | err = md_allow_write(conf->mddev); |
| 1029 | if (err) |
| 1030 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 1031 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1032 | /* Step 1 */ |
| 1033 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 1034 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1035 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1036 | if (!sc) |
| 1037 | return -ENOMEM; |
| 1038 | |
| 1039 | for (i = conf->max_nr_stripes; i; i--) { |
| 1040 | nsh = kmem_cache_alloc(sc, GFP_KERNEL); |
| 1041 | if (!nsh) |
| 1042 | break; |
| 1043 | |
| 1044 | memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev)); |
| 1045 | |
| 1046 | nsh->raid_conf = conf; |
| 1047 | spin_lock_init(&nsh->lock); |
| 1048 | |
| 1049 | list_add(&nsh->lru, &newstripes); |
| 1050 | } |
| 1051 | if (i) { |
| 1052 | /* didn't get enough, give up */ |
| 1053 | while (!list_empty(&newstripes)) { |
| 1054 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1055 | list_del(&nsh->lru); |
| 1056 | kmem_cache_free(sc, nsh); |
| 1057 | } |
| 1058 | kmem_cache_destroy(sc); |
| 1059 | return -ENOMEM; |
| 1060 | } |
| 1061 | /* Step 2 - Must use GFP_NOIO now. |
| 1062 | * OK, we have enough stripes, start collecting inactive |
| 1063 | * stripes and copying them over |
| 1064 | */ |
| 1065 | list_for_each_entry(nsh, &newstripes, lru) { |
| 1066 | spin_lock_irq(&conf->device_lock); |
| 1067 | wait_event_lock_irq(conf->wait_for_stripe, |
| 1068 | !list_empty(&conf->inactive_list), |
| 1069 | conf->device_lock, |
NeilBrown | b3b46be | 2006-03-27 01:18:16 -0800 | [diff] [blame] | 1070 | unplug_slaves(conf->mddev) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1071 | ); |
| 1072 | osh = get_free_stripe(conf); |
| 1073 | spin_unlock_irq(&conf->device_lock); |
| 1074 | atomic_set(&nsh->count, 1); |
| 1075 | for(i=0; i<conf->pool_size; i++) |
| 1076 | nsh->dev[i].page = osh->dev[i].page; |
| 1077 | for( ; i<newsize; i++) |
| 1078 | nsh->dev[i].page = NULL; |
| 1079 | kmem_cache_free(conf->slab_cache, osh); |
| 1080 | } |
| 1081 | kmem_cache_destroy(conf->slab_cache); |
| 1082 | |
| 1083 | /* Step 3. |
| 1084 | * At this point, we are holding all the stripes so the array |
| 1085 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1086 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1087 | */ |
| 1088 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 1089 | if (ndisks) { |
| 1090 | for (i=0; i<conf->raid_disks; i++) |
| 1091 | ndisks[i] = conf->disks[i]; |
| 1092 | kfree(conf->disks); |
| 1093 | conf->disks = ndisks; |
| 1094 | } else |
| 1095 | err = -ENOMEM; |
| 1096 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1097 | get_online_cpus(); |
| 1098 | conf->scribble_len = scribble_len(newsize); |
| 1099 | for_each_present_cpu(cpu) { |
| 1100 | struct raid5_percpu *percpu; |
| 1101 | void *scribble; |
| 1102 | |
| 1103 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 1104 | scribble = kmalloc(conf->scribble_len, GFP_NOIO); |
| 1105 | |
| 1106 | if (scribble) { |
| 1107 | kfree(percpu->scribble); |
| 1108 | percpu->scribble = scribble; |
| 1109 | } else { |
| 1110 | err = -ENOMEM; |
| 1111 | break; |
| 1112 | } |
| 1113 | } |
| 1114 | put_online_cpus(); |
| 1115 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1116 | /* Step 4, return new stripes to service */ |
| 1117 | while(!list_empty(&newstripes)) { |
| 1118 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1119 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1120 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1121 | for (i=conf->raid_disks; i < newsize; i++) |
| 1122 | if (nsh->dev[i].page == NULL) { |
| 1123 | struct page *p = alloc_page(GFP_NOIO); |
| 1124 | nsh->dev[i].page = p; |
| 1125 | if (!p) |
| 1126 | err = -ENOMEM; |
| 1127 | } |
| 1128 | release_stripe(nsh); |
| 1129 | } |
| 1130 | /* critical section pass, GFP_NOIO no longer needed */ |
| 1131 | |
| 1132 | conf->slab_cache = sc; |
| 1133 | conf->active_name = 1-conf->active_name; |
| 1134 | conf->pool_size = newsize; |
| 1135 | return err; |
| 1136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1138 | static int drop_one_stripe(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | { |
| 1140 | struct stripe_head *sh; |
| 1141 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1142 | spin_lock_irq(&conf->device_lock); |
| 1143 | sh = get_free_stripe(conf); |
| 1144 | spin_unlock_irq(&conf->device_lock); |
| 1145 | if (!sh) |
| 1146 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1147 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1148 | shrink_buffers(sh, conf->pool_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1149 | kmem_cache_free(conf->slab_cache, sh); |
| 1150 | atomic_dec(&conf->active_stripes); |
| 1151 | return 1; |
| 1152 | } |
| 1153 | |
| 1154 | static void shrink_stripes(raid5_conf_t *conf) |
| 1155 | { |
| 1156 | while (drop_one_stripe(conf)) |
| 1157 | ; |
| 1158 | |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1159 | if (conf->slab_cache) |
| 1160 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | conf->slab_cache = NULL; |
| 1162 | } |
| 1163 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1164 | static void raid5_end_read_request(struct bio * bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1166 | struct stripe_head *sh = bi->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1168 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1170 | char b[BDEVNAME_SIZE]; |
| 1171 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | |
| 1174 | for (i=0 ; i<disks; i++) |
| 1175 | if (bi == &sh->dev[i].req) |
| 1176 | break; |
| 1177 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1178 | pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
| 1179 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | uptodate); |
| 1181 | if (i == disks) { |
| 1182 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1183 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1188 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1189 | rdev = conf->disks[i].rdev; |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1190 | printk_rl(KERN_INFO "raid5:%s: read error corrected" |
| 1191 | " (%lu sectors at %llu on %s)\n", |
| 1192 | mdname(conf->mddev), STRIPE_SECTORS, |
| 1193 | (unsigned long long)(sh->sector |
| 1194 | + rdev->data_offset), |
| 1195 | bdevname(rdev->bdev, b)); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1196 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1197 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
| 1198 | } |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1199 | if (atomic_read(&conf->disks[i].rdev->read_errors)) |
| 1200 | atomic_set(&conf->disks[i].rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | } else { |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1202 | const char *bdn = bdevname(conf->disks[i].rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1203 | int retry = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1204 | rdev = conf->disks[i].rdev; |
| 1205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1207 | atomic_inc(&rdev->read_errors); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1208 | if (conf->mddev->degraded) |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1209 | printk_rl(KERN_WARNING |
| 1210 | "raid5:%s: read error not correctable " |
| 1211 | "(sector %llu on %s).\n", |
| 1212 | mdname(conf->mddev), |
| 1213 | (unsigned long long)(sh->sector |
| 1214 | + rdev->data_offset), |
| 1215 | bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1216 | else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1217 | /* Oh, no!!! */ |
Bernd Schubert | 6be9d49 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 1218 | printk_rl(KERN_WARNING |
| 1219 | "raid5:%s: read error NOT corrected!! " |
| 1220 | "(sector %llu on %s).\n", |
| 1221 | mdname(conf->mddev), |
| 1222 | (unsigned long long)(sh->sector |
| 1223 | + rdev->data_offset), |
| 1224 | bdn); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1225 | else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1226 | > conf->max_nr_stripes) |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1227 | printk(KERN_WARNING |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1228 | "raid5:%s: Too many read errors, failing device %s.\n", |
| 1229 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1230 | else |
| 1231 | retry = 1; |
| 1232 | if (retry) |
| 1233 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 1234 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1235 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1236 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1237 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1238 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | } |
| 1240 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1242 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1243 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1246 | static void raid5_end_write_request(struct bio *bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1248 | struct stripe_head *sh = bi->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1250 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
| 1252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | for (i=0 ; i<disks; i++) |
| 1254 | if (bi == &sh->dev[i].req) |
| 1255 | break; |
| 1256 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1257 | pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 1259 | uptodate); |
| 1260 | if (i == disks) { |
| 1261 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1262 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | if (!uptodate) |
| 1266 | md_error(conf->mddev, conf->disks[i].rdev); |
| 1267 | |
| 1268 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
| 1269 | |
| 1270 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1271 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1272 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1276 | 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] | 1277 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1278 | 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] | 1279 | { |
| 1280 | struct r5dev *dev = &sh->dev[i]; |
| 1281 | |
| 1282 | bio_init(&dev->req); |
| 1283 | dev->req.bi_io_vec = &dev->vec; |
| 1284 | dev->req.bi_vcnt++; |
| 1285 | dev->req.bi_max_vecs++; |
| 1286 | dev->vec.bv_page = dev->page; |
| 1287 | dev->vec.bv_len = STRIPE_SIZE; |
| 1288 | dev->vec.bv_offset = 0; |
| 1289 | |
| 1290 | dev->req.bi_sector = sh->sector; |
| 1291 | dev->req.bi_private = sh; |
| 1292 | |
| 1293 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1294 | dev->sector = compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | static void error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1298 | { |
| 1299 | char b[BDEVNAME_SIZE]; |
| 1300 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1301 | pr_debug("raid5: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1303 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1304 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1305 | if (test_and_clear_bit(In_sync, &rdev->flags)) { |
| 1306 | unsigned long flags; |
| 1307 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | mddev->degraded++; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1309 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | /* |
| 1311 | * if recovery was running, make sure it aborts. |
| 1312 | */ |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 1313 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1315 | set_bit(Faulty, &rdev->flags); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1316 | printk(KERN_ALERT |
| 1317 | "raid5: Disk failure on %s, disabling device.\n" |
| 1318 | "raid5: Operation continuing on %d devices.\n", |
| 1319 | bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1321 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | |
| 1323 | /* |
| 1324 | * Input: a 'big' sector number, |
| 1325 | * Output: index of the data and parity disk, and the sector # in them. |
| 1326 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1327 | 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] | 1328 | int previous, int *dd_idx, |
| 1329 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | { |
| 1331 | long stripe; |
| 1332 | unsigned long chunk_number; |
| 1333 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1334 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1335 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1337 | int algorithm = previous ? conf->prev_algo |
| 1338 | : conf->algorithm; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1339 | int sectors_per_chunk = previous ? (conf->prev_chunk >> 9) |
| 1340 | : (conf->chunk_size >> 9); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1341 | int raid_disks = previous ? conf->previous_raid_disks |
| 1342 | : conf->raid_disks; |
| 1343 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | |
| 1345 | /* First compute the information on this sector */ |
| 1346 | |
| 1347 | /* |
| 1348 | * Compute the chunk number and the sector offset inside the chunk |
| 1349 | */ |
| 1350 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 1351 | chunk_number = r_sector; |
| 1352 | BUG_ON(r_sector != chunk_number); |
| 1353 | |
| 1354 | /* |
| 1355 | * Compute the stripe number |
| 1356 | */ |
| 1357 | stripe = chunk_number / data_disks; |
| 1358 | |
| 1359 | /* |
| 1360 | * Compute the data disk and parity disk indexes inside the stripe |
| 1361 | */ |
| 1362 | *dd_idx = chunk_number % data_disks; |
| 1363 | |
| 1364 | /* |
| 1365 | * Select the parity disk based on the user selected algorithm. |
| 1366 | */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1367 | pd_idx = qd_idx = ~0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1368 | switch(conf->level) { |
| 1369 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1370 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1371 | break; |
| 1372 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1373 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1375 | pd_idx = data_disks - stripe % raid_disks; |
| 1376 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | (*dd_idx)++; |
| 1378 | break; |
| 1379 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1380 | pd_idx = stripe % raid_disks; |
| 1381 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | (*dd_idx)++; |
| 1383 | break; |
| 1384 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1385 | pd_idx = data_disks - stripe % raid_disks; |
| 1386 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | break; |
| 1388 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1389 | pd_idx = stripe % raid_disks; |
| 1390 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1392 | case ALGORITHM_PARITY_0: |
| 1393 | pd_idx = 0; |
| 1394 | (*dd_idx)++; |
| 1395 | break; |
| 1396 | case ALGORITHM_PARITY_N: |
| 1397 | pd_idx = data_disks; |
| 1398 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | default: |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1400 | printk(KERN_ERR "raid5: unsupported algorithm %d\n", |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1401 | algorithm); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1402 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1403 | } |
| 1404 | break; |
| 1405 | case 6: |
| 1406 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1407 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1408 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1409 | pd_idx = raid_disks - 1 - (stripe % raid_disks); |
| 1410 | qd_idx = pd_idx + 1; |
| 1411 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1412 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1413 | qd_idx = 0; |
| 1414 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1415 | (*dd_idx) += 2; /* D D P Q D */ |
| 1416 | break; |
| 1417 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1418 | pd_idx = stripe % raid_disks; |
| 1419 | qd_idx = pd_idx + 1; |
| 1420 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1421 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1422 | qd_idx = 0; |
| 1423 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1424 | (*dd_idx) += 2; /* D D P Q D */ |
| 1425 | break; |
| 1426 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1427 | pd_idx = raid_disks - 1 - (stripe % raid_disks); |
| 1428 | qd_idx = (pd_idx + 1) % raid_disks; |
| 1429 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1430 | break; |
| 1431 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1432 | pd_idx = stripe % raid_disks; |
| 1433 | qd_idx = (pd_idx + 1) % raid_disks; |
| 1434 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1435 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1436 | |
| 1437 | case ALGORITHM_PARITY_0: |
| 1438 | pd_idx = 0; |
| 1439 | qd_idx = 1; |
| 1440 | (*dd_idx) += 2; |
| 1441 | break; |
| 1442 | case ALGORITHM_PARITY_N: |
| 1443 | pd_idx = data_disks; |
| 1444 | qd_idx = data_disks + 1; |
| 1445 | break; |
| 1446 | |
| 1447 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 1448 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 1449 | * of blocks for computing Q is different. |
| 1450 | */ |
| 1451 | pd_idx = stripe % raid_disks; |
| 1452 | qd_idx = pd_idx + 1; |
| 1453 | if (pd_idx == raid_disks-1) { |
| 1454 | (*dd_idx)++; /* Q D D D P */ |
| 1455 | qd_idx = 0; |
| 1456 | } else if (*dd_idx >= pd_idx) |
| 1457 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1458 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1459 | break; |
| 1460 | |
| 1461 | case ALGORITHM_ROTATING_N_RESTART: |
| 1462 | /* Same a left_asymmetric, by first stripe is |
| 1463 | * D D D P Q rather than |
| 1464 | * Q D D D P |
| 1465 | */ |
| 1466 | pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks); |
| 1467 | qd_idx = pd_idx + 1; |
| 1468 | if (pd_idx == raid_disks-1) { |
| 1469 | (*dd_idx)++; /* Q D D D P */ |
| 1470 | qd_idx = 0; |
| 1471 | } else if (*dd_idx >= pd_idx) |
| 1472 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1473 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1474 | break; |
| 1475 | |
| 1476 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 1477 | /* Same as left_symmetric but Q is before P */ |
| 1478 | pd_idx = raid_disks - 1 - (stripe % raid_disks); |
| 1479 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 1480 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1481 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1482 | break; |
| 1483 | |
| 1484 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 1485 | /* RAID5 left_asymmetric, with Q on last device */ |
| 1486 | pd_idx = data_disks - stripe % (raid_disks-1); |
| 1487 | if (*dd_idx >= pd_idx) |
| 1488 | (*dd_idx)++; |
| 1489 | qd_idx = raid_disks - 1; |
| 1490 | break; |
| 1491 | |
| 1492 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 1493 | pd_idx = stripe % (raid_disks-1); |
| 1494 | if (*dd_idx >= pd_idx) |
| 1495 | (*dd_idx)++; |
| 1496 | qd_idx = raid_disks - 1; |
| 1497 | break; |
| 1498 | |
| 1499 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 1500 | pd_idx = data_disks - stripe % (raid_disks-1); |
| 1501 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 1502 | qd_idx = raid_disks - 1; |
| 1503 | break; |
| 1504 | |
| 1505 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 1506 | pd_idx = stripe % (raid_disks-1); |
| 1507 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 1508 | qd_idx = raid_disks - 1; |
| 1509 | break; |
| 1510 | |
| 1511 | case ALGORITHM_PARITY_0_6: |
| 1512 | pd_idx = 0; |
| 1513 | (*dd_idx)++; |
| 1514 | qd_idx = raid_disks - 1; |
| 1515 | break; |
| 1516 | |
| 1517 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1518 | default: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1519 | printk(KERN_CRIT "raid6: unsupported algorithm %d\n", |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1520 | algorithm); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1521 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1522 | } |
| 1523 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1526 | if (sh) { |
| 1527 | sh->pd_idx = pd_idx; |
| 1528 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1529 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1530 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | /* |
| 1532 | * Finally, compute the new sector number |
| 1533 | */ |
| 1534 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 1535 | return new_sector; |
| 1536 | } |
| 1537 | |
| 1538 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1539 | 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] | 1540 | { |
| 1541 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 1542 | int raid_disks = sh->disks; |
| 1543 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | sector_t new_sector = sh->sector, check; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1545 | int sectors_per_chunk = previous ? (conf->prev_chunk >> 9) |
| 1546 | : (conf->chunk_size >> 9); |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1547 | int algorithm = previous ? conf->prev_algo |
| 1548 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | sector_t stripe; |
| 1550 | int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1551 | int chunk_number, dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1553 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 1557 | stripe = new_sector; |
| 1558 | BUG_ON(new_sector != stripe); |
| 1559 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1560 | if (i == sh->pd_idx) |
| 1561 | return 0; |
| 1562 | switch(conf->level) { |
| 1563 | case 4: break; |
| 1564 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1565 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 1567 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 1568 | if (i > sh->pd_idx) |
| 1569 | i--; |
| 1570 | break; |
| 1571 | case ALGORITHM_LEFT_SYMMETRIC: |
| 1572 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 1573 | if (i < sh->pd_idx) |
| 1574 | i += raid_disks; |
| 1575 | i -= (sh->pd_idx + 1); |
| 1576 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1577 | case ALGORITHM_PARITY_0: |
| 1578 | i -= 1; |
| 1579 | break; |
| 1580 | case ALGORITHM_PARITY_N: |
| 1581 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | default: |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1583 | printk(KERN_ERR "raid5: unsupported algorithm %d\n", |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1584 | algorithm); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1585 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1586 | } |
| 1587 | break; |
| 1588 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1589 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1590 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1591 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1592 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 1593 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1594 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 1595 | case ALGORITHM_ROTATING_N_RESTART: |
| 1596 | if (sh->pd_idx == raid_disks-1) |
| 1597 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1598 | else if (i > sh->pd_idx) |
| 1599 | i -= 2; /* D D P Q D */ |
| 1600 | break; |
| 1601 | case ALGORITHM_LEFT_SYMMETRIC: |
| 1602 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 1603 | if (sh->pd_idx == raid_disks-1) |
| 1604 | i--; /* Q D D D P */ |
| 1605 | else { |
| 1606 | /* D D P Q D */ |
| 1607 | if (i < sh->pd_idx) |
| 1608 | i += raid_disks; |
| 1609 | i -= (sh->pd_idx + 2); |
| 1610 | } |
| 1611 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1612 | case ALGORITHM_PARITY_0: |
| 1613 | i -= 2; |
| 1614 | break; |
| 1615 | case ALGORITHM_PARITY_N: |
| 1616 | break; |
| 1617 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 1618 | if (sh->pd_idx == 0) |
| 1619 | i--; /* P D D D Q */ |
| 1620 | else if (i > sh->pd_idx) |
| 1621 | i -= 2; /* D D Q P D */ |
| 1622 | break; |
| 1623 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 1624 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 1625 | if (i > sh->pd_idx) |
| 1626 | i--; |
| 1627 | break; |
| 1628 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 1629 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 1630 | if (i < sh->pd_idx) |
| 1631 | i += data_disks + 1; |
| 1632 | i -= (sh->pd_idx + 1); |
| 1633 | break; |
| 1634 | case ALGORITHM_PARITY_0_6: |
| 1635 | i -= 1; |
| 1636 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1637 | default: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1638 | printk(KERN_CRIT "raid6: unsupported algorithm %d\n", |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1639 | algorithm); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1640 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1641 | } |
| 1642 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | chunk_number = stripe * data_disks + i; |
| 1646 | r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset; |
| 1647 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1648 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1649 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1650 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 1651 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1652 | printk(KERN_ERR "compute_blocknr: map not correct\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | return 0; |
| 1654 | } |
| 1655 | return r_sector; |
| 1656 | } |
| 1657 | |
| 1658 | |
| 1659 | |
| 1660 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1661 | * Copy data between a page in the stripe cache, and one or more bion |
| 1662 | * The page could align with the middle of the bio, or there could be |
| 1663 | * several bion, each with several bio_vecs, which cover part of the page |
| 1664 | * Multiple bion are linked together on bi_next. There may be extras |
| 1665 | * at the end of this list. We ignore them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | */ |
| 1667 | static void copy_data(int frombio, struct bio *bio, |
| 1668 | struct page *page, |
| 1669 | sector_t sector) |
| 1670 | { |
| 1671 | char *pa = page_address(page); |
| 1672 | struct bio_vec *bvl; |
| 1673 | int i; |
| 1674 | int page_offset; |
| 1675 | |
| 1676 | if (bio->bi_sector >= sector) |
| 1677 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 1678 | else |
| 1679 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
| 1680 | bio_for_each_segment(bvl, bio, i) { |
| 1681 | int len = bio_iovec_idx(bio,i)->bv_len; |
| 1682 | int clen; |
| 1683 | int b_offset = 0; |
| 1684 | |
| 1685 | if (page_offset < 0) { |
| 1686 | b_offset = -page_offset; |
| 1687 | page_offset += b_offset; |
| 1688 | len -= b_offset; |
| 1689 | } |
| 1690 | |
| 1691 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 1692 | clen = STRIPE_SIZE - page_offset; |
| 1693 | else clen = len; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | if (clen > 0) { |
| 1696 | char *ba = __bio_kmap_atomic(bio, i, KM_USER0); |
| 1697 | if (frombio) |
| 1698 | memcpy(pa+page_offset, ba+b_offset, clen); |
| 1699 | else |
| 1700 | memcpy(ba+b_offset, pa+page_offset, clen); |
| 1701 | __bio_kunmap_atomic(ba, KM_USER0); |
| 1702 | } |
| 1703 | if (clen < len) /* hit end of page */ |
| 1704 | break; |
| 1705 | page_offset += len; |
| 1706 | } |
| 1707 | } |
| 1708 | |
Dan Williams | 9bc89cd | 2007-01-02 11:10:44 -0700 | [diff] [blame] | 1709 | #define check_xor() do { \ |
| 1710 | if (count == MAX_XOR_BLOCKS) { \ |
| 1711 | xor_blocks(count, STRIPE_SIZE, dest, ptr);\ |
| 1712 | count = 0; \ |
| 1713 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | } while(0) |
| 1715 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1716 | static void compute_parity6(struct stripe_head *sh, int method) |
| 1717 | { |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1718 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1719 | int i, pd_idx, qd_idx, d0_idx, disks = sh->disks, count; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1720 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1721 | struct bio *chosen; |
| 1722 | /**** FIX THIS: This could be very bad if disks is close to 256 ****/ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1723 | void *ptrs[syndrome_disks+2]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1724 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1725 | pd_idx = sh->pd_idx; |
| 1726 | qd_idx = sh->qd_idx; |
| 1727 | d0_idx = raid6_d0(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1728 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1729 | pr_debug("compute_parity, stripe %llu, method %d\n", |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1730 | (unsigned long long)sh->sector, method); |
| 1731 | |
| 1732 | switch(method) { |
| 1733 | case READ_MODIFY_WRITE: |
| 1734 | BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */ |
| 1735 | case RECONSTRUCT_WRITE: |
| 1736 | for (i= disks; i-- ;) |
| 1737 | if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) { |
| 1738 | chosen = sh->dev[i].towrite; |
| 1739 | sh->dev[i].towrite = NULL; |
| 1740 | |
| 1741 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 1742 | wake_up(&conf->wait_for_overlap); |
| 1743 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 1744 | BUG_ON(sh->dev[i].written); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1745 | sh->dev[i].written = chosen; |
| 1746 | } |
| 1747 | break; |
| 1748 | case CHECK_PARITY: |
| 1749 | BUG(); /* Not implemented yet */ |
| 1750 | } |
| 1751 | |
| 1752 | for (i = disks; i--;) |
| 1753 | if (sh->dev[i].written) { |
| 1754 | sector_t sector = sh->dev[i].sector; |
| 1755 | struct bio *wbi = sh->dev[i].written; |
| 1756 | while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) { |
| 1757 | copy_data(1, wbi, sh->dev[i].page, sector); |
| 1758 | wbi = r5_next_bio(wbi, sector); |
| 1759 | } |
| 1760 | |
| 1761 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1762 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
| 1763 | } |
| 1764 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1765 | /* Note that unlike RAID-5, the ordering of the disks matters greatly.*/ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1766 | |
| 1767 | for (i = 0; i < disks; i++) |
| 1768 | ptrs[i] = (void *)raid6_empty_zero_page; |
| 1769 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1770 | count = 0; |
| 1771 | i = d0_idx; |
| 1772 | do { |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1773 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 1774 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1775 | ptrs[slot] = page_address(sh->dev[i].page); |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1776 | if (slot < syndrome_disks && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1777 | !test_bit(R5_UPTODATE, &sh->dev[i].flags)) { |
| 1778 | printk(KERN_ERR "block %d/%d not uptodate " |
| 1779 | "on parity calc\n", i, count); |
| 1780 | BUG(); |
| 1781 | } |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1782 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1783 | i = raid6_next_disk(i, disks); |
| 1784 | } while (i != d0_idx); |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1785 | BUG_ON(count != syndrome_disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1786 | |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1787 | raid6_call.gen_syndrome(syndrome_disks+2, STRIPE_SIZE, ptrs); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1788 | |
| 1789 | switch(method) { |
| 1790 | case RECONSTRUCT_WRITE: |
| 1791 | set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 1792 | set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags); |
| 1793 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 1794 | set_bit(R5_LOCKED, &sh->dev[qd_idx].flags); |
| 1795 | break; |
| 1796 | case UPDATE_PARITY: |
| 1797 | set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 1798 | set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags); |
| 1799 | break; |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | |
| 1804 | /* Compute one missing block */ |
| 1805 | static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero) |
| 1806 | { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 1807 | int i, count, disks = sh->disks; |
Dan Williams | 9bc89cd | 2007-01-02 11:10:44 -0700 | [diff] [blame] | 1808 | void *ptr[MAX_XOR_BLOCKS], *dest, *p; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1809 | int qd_idx = sh->qd_idx; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1810 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1811 | pr_debug("compute_block_1, stripe %llu, idx %d\n", |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1812 | (unsigned long long)sh->sector, dd_idx); |
| 1813 | |
| 1814 | if ( dd_idx == qd_idx ) { |
| 1815 | /* We're actually computing the Q drive */ |
| 1816 | compute_parity6(sh, UPDATE_PARITY); |
| 1817 | } else { |
Dan Williams | 9bc89cd | 2007-01-02 11:10:44 -0700 | [diff] [blame] | 1818 | dest = page_address(sh->dev[dd_idx].page); |
| 1819 | if (!nozero) memset(dest, 0, STRIPE_SIZE); |
| 1820 | count = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1821 | for (i = disks ; i--; ) { |
| 1822 | if (i == dd_idx || i == qd_idx) |
| 1823 | continue; |
| 1824 | p = page_address(sh->dev[i].page); |
| 1825 | if (test_bit(R5_UPTODATE, &sh->dev[i].flags)) |
| 1826 | ptr[count++] = p; |
| 1827 | else |
| 1828 | printk("compute_block() %d, stripe %llu, %d" |
| 1829 | " not present\n", dd_idx, |
| 1830 | (unsigned long long)sh->sector, i); |
| 1831 | |
| 1832 | check_xor(); |
| 1833 | } |
Dan Williams | 9bc89cd | 2007-01-02 11:10:44 -0700 | [diff] [blame] | 1834 | if (count) |
| 1835 | xor_blocks(count, STRIPE_SIZE, dest, ptr); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1836 | if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags); |
| 1837 | else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags); |
| 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | /* Compute two missing blocks */ |
| 1842 | static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2) |
| 1843 | { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 1844 | int i, count, disks = sh->disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1845 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1846 | int d0_idx = raid6_d0(sh); |
| 1847 | int faila = -1, failb = -1; |
| 1848 | /**** FIX THIS: This could be very bad if disks is close to 256 ****/ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1849 | void *ptrs[syndrome_disks+2]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1850 | |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1851 | for (i = 0; i < disks ; i++) |
| 1852 | ptrs[i] = (void *)raid6_empty_zero_page; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1853 | count = 0; |
| 1854 | i = d0_idx; |
| 1855 | do { |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1856 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 1857 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1858 | ptrs[slot] = page_address(sh->dev[i].page); |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1859 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1860 | if (i == dd_idx1) |
| 1861 | faila = slot; |
| 1862 | if (i == dd_idx2) |
| 1863 | failb = slot; |
| 1864 | i = raid6_next_disk(i, disks); |
| 1865 | } while (i != d0_idx); |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1866 | BUG_ON(count != syndrome_disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1867 | |
| 1868 | BUG_ON(faila == failb); |
| 1869 | if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; } |
| 1870 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1871 | pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n", |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1872 | (unsigned long long)sh->sector, dd_idx1, dd_idx2, |
| 1873 | faila, failb); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1874 | |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1875 | if (failb == syndrome_disks+1) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1876 | /* Q disk is one of the missing disks */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1877 | if (faila == syndrome_disks) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1878 | /* Missing P+Q, just recompute */ |
| 1879 | compute_parity6(sh, UPDATE_PARITY); |
| 1880 | return; |
| 1881 | } else { |
| 1882 | /* We're missing D+Q; recompute D from P */ |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1883 | compute_block_1(sh, ((dd_idx1 == sh->qd_idx) ? |
| 1884 | dd_idx2 : dd_idx1), |
| 1885 | 0); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1886 | compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */ |
| 1887 | return; |
| 1888 | } |
| 1889 | } |
| 1890 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1891 | /* We're missing D+P or D+D; */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1892 | if (failb == syndrome_disks) { |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1893 | /* We're missing D+P. */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1894 | raid6_datap_recov(syndrome_disks+2, STRIPE_SIZE, faila, ptrs); |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1895 | } else { |
| 1896 | /* We're missing D+D. */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1897 | raid6_2data_recov(syndrome_disks+2, STRIPE_SIZE, faila, failb, |
| 1898 | ptrs); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1899 | } |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1900 | |
| 1901 | /* Both the above update both missing blocks */ |
| 1902 | set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags); |
| 1903 | set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1904 | } |
| 1905 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1906 | static void |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 1907 | schedule_reconstruction5(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1908 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1909 | { |
| 1910 | int i, pd_idx = sh->pd_idx, disks = sh->disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1911 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1912 | if (rcw) { |
| 1913 | /* if we are not expanding this is a proper write request, and |
| 1914 | * there will be bios with new data to be drained into the |
| 1915 | * stripe cache |
| 1916 | */ |
| 1917 | if (!expand) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1918 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 1919 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 1920 | } else |
| 1921 | sh->reconstruct_state = reconstruct_state_run; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1922 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1923 | set_bit(STRIPE_OP_POSTXOR, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1924 | |
| 1925 | for (i = disks; i--; ) { |
| 1926 | struct r5dev *dev = &sh->dev[i]; |
| 1927 | |
| 1928 | if (dev->towrite) { |
| 1929 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1930 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1931 | if (!expand) |
| 1932 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1933 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1934 | } |
| 1935 | } |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1936 | if (s->locked + 1 == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 1937 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 1938 | atomic_inc(&sh->raid_conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1939 | } else { |
| 1940 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 1941 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
| 1942 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1943 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1944 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 1945 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 1946 | set_bit(STRIPE_OP_POSTXOR, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1947 | |
| 1948 | for (i = disks; i--; ) { |
| 1949 | struct r5dev *dev = &sh->dev[i]; |
| 1950 | if (i == pd_idx) |
| 1951 | continue; |
| 1952 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1953 | if (dev->towrite && |
| 1954 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1955 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 1956 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1957 | set_bit(R5_LOCKED, &dev->flags); |
| 1958 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1959 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1960 | } |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | /* keep the parity disk locked while asynchronous operations |
| 1965 | * are in flight |
| 1966 | */ |
| 1967 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 1968 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1969 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1970 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1971 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1972 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1973 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1974 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | /* |
| 1977 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1978 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | * The bi_next chain must be in order. |
| 1980 | */ |
| 1981 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) |
| 1982 | { |
| 1983 | struct bio **bip; |
| 1984 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1985 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1987 | pr_debug("adding bh b#%llu to stripe s#%llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | (unsigned long long)bi->bi_sector, |
| 1989 | (unsigned long long)sh->sector); |
| 1990 | |
| 1991 | |
| 1992 | spin_lock(&sh->lock); |
| 1993 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1994 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | bip = &sh->dev[dd_idx].towrite; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1996 | if (*bip == NULL && sh->dev[dd_idx].written == NULL) |
| 1997 | firstwrite = 1; |
| 1998 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | bip = &sh->dev[dd_idx].toread; |
| 2000 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { |
| 2001 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) |
| 2002 | goto overlap; |
| 2003 | bip = & (*bip)->bi_next; |
| 2004 | } |
| 2005 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) |
| 2006 | goto overlap; |
| 2007 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2008 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | if (*bip) |
| 2010 | bi->bi_next = *bip; |
| 2011 | *bip = bi; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2012 | bi->bi_phys_segments++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | spin_unlock_irq(&conf->device_lock); |
| 2014 | spin_unlock(&sh->lock); |
| 2015 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2016 | 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] | 2017 | (unsigned long long)bi->bi_sector, |
| 2018 | (unsigned long long)sh->sector, dd_idx); |
| 2019 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2020 | if (conf->mddev->bitmap && firstwrite) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2021 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 2022 | STRIPE_SECTORS, 0); |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 2023 | sh->bm_seq = conf->seq_flush+1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2024 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 2025 | } |
| 2026 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | if (forwrite) { |
| 2028 | /* check if page is covered */ |
| 2029 | sector_t sector = sh->dev[dd_idx].sector; |
| 2030 | for (bi=sh->dev[dd_idx].towrite; |
| 2031 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
| 2032 | bi && bi->bi_sector <= sector; |
| 2033 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
| 2034 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) |
| 2035 | sector = bi->bi_sector + (bi->bi_size>>9); |
| 2036 | } |
| 2037 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
| 2038 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); |
| 2039 | } |
| 2040 | return 1; |
| 2041 | |
| 2042 | overlap: |
| 2043 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
| 2044 | spin_unlock_irq(&conf->device_lock); |
| 2045 | spin_unlock(&sh->lock); |
| 2046 | return 0; |
| 2047 | } |
| 2048 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2049 | static void end_reshape(raid5_conf_t *conf); |
| 2050 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2051 | static int page_is_zero(struct page *p) |
| 2052 | { |
| 2053 | char *a = page_address(p); |
| 2054 | return ((*(u32*)a) == 0 && |
| 2055 | memcmp(a, a+4, STRIPE_SIZE-4)==0); |
| 2056 | } |
| 2057 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2058 | static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous, |
| 2059 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2060 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2061 | int sectors_per_chunk = |
| 2062 | previous ? (conf->prev_chunk >> 9) |
| 2063 | : (conf->chunk_size >> 9); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2064 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2065 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2066 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2067 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2068 | raid5_compute_sector(conf, |
| 2069 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2070 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2071 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2072 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2073 | } |
| 2074 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2075 | static void |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2076 | handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2077 | struct stripe_head_state *s, int disks, |
| 2078 | struct bio **return_bi) |
| 2079 | { |
| 2080 | int i; |
| 2081 | for (i = disks; i--; ) { |
| 2082 | struct bio *bi; |
| 2083 | int bitmap_end = 0; |
| 2084 | |
| 2085 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
| 2086 | mdk_rdev_t *rdev; |
| 2087 | rcu_read_lock(); |
| 2088 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 2089 | if (rdev && test_bit(In_sync, &rdev->flags)) |
| 2090 | /* multiple read failures in one stripe */ |
| 2091 | md_error(conf->mddev, rdev); |
| 2092 | rcu_read_unlock(); |
| 2093 | } |
| 2094 | spin_lock_irq(&conf->device_lock); |
| 2095 | /* fail all writes first */ |
| 2096 | bi = sh->dev[i].towrite; |
| 2097 | sh->dev[i].towrite = NULL; |
| 2098 | if (bi) { |
| 2099 | s->to_write--; |
| 2100 | bitmap_end = 1; |
| 2101 | } |
| 2102 | |
| 2103 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2104 | wake_up(&conf->wait_for_overlap); |
| 2105 | |
| 2106 | while (bi && bi->bi_sector < |
| 2107 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2108 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 2109 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2110 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2111 | md_write_end(conf->mddev); |
| 2112 | bi->bi_next = *return_bi; |
| 2113 | *return_bi = bi; |
| 2114 | } |
| 2115 | bi = nextbi; |
| 2116 | } |
| 2117 | /* and fail all 'written' */ |
| 2118 | bi = sh->dev[i].written; |
| 2119 | sh->dev[i].written = NULL; |
| 2120 | if (bi) bitmap_end = 1; |
| 2121 | while (bi && bi->bi_sector < |
| 2122 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2123 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 2124 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2125 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2126 | md_write_end(conf->mddev); |
| 2127 | bi->bi_next = *return_bi; |
| 2128 | *return_bi = bi; |
| 2129 | } |
| 2130 | bi = bi2; |
| 2131 | } |
| 2132 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2133 | /* fail any reads if this device is non-operational and |
| 2134 | * the data has not reached the cache yet. |
| 2135 | */ |
| 2136 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
| 2137 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 2138 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2139 | bi = sh->dev[i].toread; |
| 2140 | sh->dev[i].toread = NULL; |
| 2141 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2142 | wake_up(&conf->wait_for_overlap); |
| 2143 | if (bi) s->to_read--; |
| 2144 | while (bi && bi->bi_sector < |
| 2145 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2146 | struct bio *nextbi = |
| 2147 | r5_next_bio(bi, sh->dev[i].sector); |
| 2148 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2149 | if (!raid5_dec_bi_phys_segments(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2150 | bi->bi_next = *return_bi; |
| 2151 | *return_bi = bi; |
| 2152 | } |
| 2153 | bi = nextbi; |
| 2154 | } |
| 2155 | } |
| 2156 | spin_unlock_irq(&conf->device_lock); |
| 2157 | if (bitmap_end) |
| 2158 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2159 | STRIPE_SECTORS, 0, 0); |
| 2160 | } |
| 2161 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2162 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2163 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2164 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2165 | } |
| 2166 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2167 | /* fetch_block5 - checks the given member device to see if its data needs |
| 2168 | * to be read or computed to satisfy a request. |
| 2169 | * |
| 2170 | * Returns 1 when no more member devices need to be checked, otherwise returns |
| 2171 | * 0 to tell the loop in handle_stripe_fill5 to continue |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2172 | */ |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2173 | static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s, |
| 2174 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2175 | { |
| 2176 | struct r5dev *dev = &sh->dev[disk_idx]; |
| 2177 | struct r5dev *failed_dev = &sh->dev[s->failed_num]; |
| 2178 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2179 | /* is the data in this block needed, and can we get it? */ |
| 2180 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2181 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2182 | (dev->toread || |
| 2183 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2184 | s->syncing || s->expanding || |
| 2185 | (s->failed && |
| 2186 | (failed_dev->toread || |
| 2187 | (failed_dev->towrite && |
| 2188 | !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) { |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2189 | /* We would like to get this block, possibly by computing it, |
| 2190 | * otherwise read it if the backing disk is insync |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2191 | */ |
| 2192 | if ((s->uptodate == disks - 1) && |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2193 | (s->failed && disk_idx == s->failed_num)) { |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2194 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2195 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2196 | set_bit(R5_Wantcompute, &dev->flags); |
| 2197 | sh->ops.target = disk_idx; |
| 2198 | s->req_compute = 1; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2199 | /* Careful: from this point on 'uptodate' is in the eye |
| 2200 | * of raid5_run_ops which services 'compute' operations |
| 2201 | * before writes. R5_Wantcompute flags a block that will |
| 2202 | * be R5_UPTODATE by the time it is needed for a |
| 2203 | * subsequent operation. |
| 2204 | */ |
| 2205 | s->uptodate++; |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2206 | return 1; /* uptodate + compute == disks */ |
Dan Williams | 7a1fc53 | 2008-07-10 04:54:57 -0700 | [diff] [blame] | 2207 | } else if (test_bit(R5_Insync, &dev->flags)) { |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2208 | set_bit(R5_LOCKED, &dev->flags); |
| 2209 | set_bit(R5_Wantread, &dev->flags); |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2210 | s->locked++; |
| 2211 | pr_debug("Reading block %d (sync=%d)\n", disk_idx, |
| 2212 | s->syncing); |
| 2213 | } |
| 2214 | } |
| 2215 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2216 | return 0; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2217 | } |
| 2218 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2219 | /** |
| 2220 | * handle_stripe_fill5 - read or compute data to satisfy pending requests. |
| 2221 | */ |
| 2222 | static void handle_stripe_fill5(struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2223 | struct stripe_head_state *s, int disks) |
| 2224 | { |
| 2225 | int i; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2226 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2227 | /* look for blocks to read/compute, skip this if a compute |
| 2228 | * is already in flight, or if the stripe contents are in the |
| 2229 | * midst of changing due to a write |
| 2230 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2231 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2232 | !sh->reconstruct_state) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2233 | for (i = disks; i--; ) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2234 | if (fetch_block5(sh, s, i, disks)) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2235 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2236 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2237 | } |
| 2238 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2239 | static void handle_stripe_fill6(struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2240 | struct stripe_head_state *s, struct r6_state *r6s, |
| 2241 | int disks) |
| 2242 | { |
| 2243 | int i; |
| 2244 | for (i = disks; i--; ) { |
| 2245 | struct r5dev *dev = &sh->dev[i]; |
| 2246 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 2247 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2248 | (dev->toread || (dev->towrite && |
| 2249 | !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2250 | s->syncing || s->expanding || |
| 2251 | (s->failed >= 1 && |
| 2252 | (sh->dev[r6s->failed_num[0]].toread || |
| 2253 | s->to_write)) || |
| 2254 | (s->failed >= 2 && |
| 2255 | (sh->dev[r6s->failed_num[1]].toread || |
| 2256 | s->to_write)))) { |
| 2257 | /* we would like to get this block, possibly |
| 2258 | * by computing it, but we might not be able to |
| 2259 | */ |
Dan Williams | c337869 | 2008-06-05 22:45:54 -0700 | [diff] [blame] | 2260 | if ((s->uptodate == disks - 1) && |
| 2261 | (s->failed && (i == r6s->failed_num[0] || |
| 2262 | i == r6s->failed_num[1]))) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2263 | pr_debug("Computing stripe %llu block %d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2264 | (unsigned long long)sh->sector, i); |
| 2265 | compute_block_1(sh, i, 0); |
| 2266 | s->uptodate++; |
| 2267 | } else if ( s->uptodate == disks-2 && s->failed >= 2 ) { |
| 2268 | /* Computing 2-failure is *very* expensive; only |
| 2269 | * do it if failed >= 2 |
| 2270 | */ |
| 2271 | int other; |
| 2272 | for (other = disks; other--; ) { |
| 2273 | if (other == i) |
| 2274 | continue; |
| 2275 | if (!test_bit(R5_UPTODATE, |
| 2276 | &sh->dev[other].flags)) |
| 2277 | break; |
| 2278 | } |
| 2279 | BUG_ON(other < 0); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2280 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2281 | (unsigned long long)sh->sector, |
| 2282 | i, other); |
| 2283 | compute_block_2(sh, i, other); |
| 2284 | s->uptodate += 2; |
| 2285 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 2286 | set_bit(R5_LOCKED, &dev->flags); |
| 2287 | set_bit(R5_Wantread, &dev->flags); |
| 2288 | s->locked++; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2289 | pr_debug("Reading block %d (sync=%d)\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2290 | i, s->syncing); |
| 2291 | } |
| 2292 | } |
| 2293 | } |
| 2294 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2295 | } |
| 2296 | |
| 2297 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2298 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2299 | * any written block on an uptodate or failed drive can be returned. |
| 2300 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 2301 | * never LOCKED, so we don't need to test 'failed' directly. |
| 2302 | */ |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2303 | static void handle_stripe_clean_event(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2304 | struct stripe_head *sh, int disks, struct bio **return_bi) |
| 2305 | { |
| 2306 | int i; |
| 2307 | struct r5dev *dev; |
| 2308 | |
| 2309 | for (i = disks; i--; ) |
| 2310 | if (sh->dev[i].written) { |
| 2311 | dev = &sh->dev[i]; |
| 2312 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 2313 | test_bit(R5_UPTODATE, &dev->flags)) { |
| 2314 | /* We can return any write requests */ |
| 2315 | struct bio *wbi, *wbi2; |
| 2316 | int bitmap_end = 0; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2317 | pr_debug("Return write for disc %d\n", i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2318 | spin_lock_irq(&conf->device_lock); |
| 2319 | wbi = dev->written; |
| 2320 | dev->written = NULL; |
| 2321 | while (wbi && wbi->bi_sector < |
| 2322 | dev->sector + STRIPE_SECTORS) { |
| 2323 | wbi2 = r5_next_bio(wbi, dev->sector); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 2324 | if (!raid5_dec_bi_phys_segments(wbi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2325 | md_write_end(conf->mddev); |
| 2326 | wbi->bi_next = *return_bi; |
| 2327 | *return_bi = wbi; |
| 2328 | } |
| 2329 | wbi = wbi2; |
| 2330 | } |
| 2331 | if (dev->towrite == NULL) |
| 2332 | bitmap_end = 1; |
| 2333 | spin_unlock_irq(&conf->device_lock); |
| 2334 | if (bitmap_end) |
| 2335 | bitmap_endwrite(conf->mddev->bitmap, |
| 2336 | sh->sector, |
| 2337 | STRIPE_SECTORS, |
| 2338 | !test_bit(STRIPE_DEGRADED, &sh->state), |
| 2339 | 0); |
| 2340 | } |
| 2341 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2342 | |
| 2343 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2344 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2345 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2346 | } |
| 2347 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2348 | static void handle_stripe_dirtying5(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2349 | struct stripe_head *sh, struct stripe_head_state *s, int disks) |
| 2350 | { |
| 2351 | int rmw = 0, rcw = 0, i; |
| 2352 | for (i = disks; i--; ) { |
| 2353 | /* would I have to read this buffer for read_modify_write */ |
| 2354 | struct r5dev *dev = &sh->dev[i]; |
| 2355 | if ((dev->towrite || i == sh->pd_idx) && |
| 2356 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2357 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2358 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2359 | if (test_bit(R5_Insync, &dev->flags)) |
| 2360 | rmw++; |
| 2361 | else |
| 2362 | rmw += 2*disks; /* cannot read it */ |
| 2363 | } |
| 2364 | /* Would I have to read this buffer for reconstruct_write */ |
| 2365 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 2366 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2367 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2368 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2369 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2370 | else |
| 2371 | rcw += 2*disks; |
| 2372 | } |
| 2373 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2374 | pr_debug("for sector %llu, rmw=%d rcw=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2375 | (unsigned long long)sh->sector, rmw, rcw); |
| 2376 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2377 | if (rmw < rcw && rmw > 0) |
| 2378 | /* prefer read-modify-write, but need to get some data */ |
| 2379 | for (i = disks; i--; ) { |
| 2380 | struct r5dev *dev = &sh->dev[i]; |
| 2381 | if ((dev->towrite || i == sh->pd_idx) && |
| 2382 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2383 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2384 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2385 | test_bit(R5_Insync, &dev->flags)) { |
| 2386 | if ( |
| 2387 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2388 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2389 | "%d for r-m-w\n", i); |
| 2390 | set_bit(R5_LOCKED, &dev->flags); |
| 2391 | set_bit(R5_Wantread, &dev->flags); |
| 2392 | s->locked++; |
| 2393 | } else { |
| 2394 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2395 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2396 | } |
| 2397 | } |
| 2398 | } |
| 2399 | if (rcw <= rmw && rcw > 0) |
| 2400 | /* want reconstruct write, but need to get some data */ |
| 2401 | for (i = disks; i--; ) { |
| 2402 | struct r5dev *dev = &sh->dev[i]; |
| 2403 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 2404 | i != sh->pd_idx && |
| 2405 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2406 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2407 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2408 | test_bit(R5_Insync, &dev->flags)) { |
| 2409 | if ( |
| 2410 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2411 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2412 | "%d for Reconstruct\n", i); |
| 2413 | set_bit(R5_LOCKED, &dev->flags); |
| 2414 | set_bit(R5_Wantread, &dev->flags); |
| 2415 | s->locked++; |
| 2416 | } else { |
| 2417 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2418 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2419 | } |
| 2420 | } |
| 2421 | } |
| 2422 | /* now if nothing is locked, and if we have enough data, |
| 2423 | * we can start a write request |
| 2424 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2425 | /* since handle_stripe can be called at any time we need to handle the |
| 2426 | * case where a compute block operation has been submitted and then a |
| 2427 | * subsequent call wants to start a write request. raid5_run_ops only |
| 2428 | * handles the case where compute block and postxor are requested |
| 2429 | * simultaneously. If this is not the case then new writes need to be |
| 2430 | * held off until the compute completes. |
| 2431 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2432 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2433 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
| 2434 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2435 | schedule_reconstruction5(sh, s, rcw == 0, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2436 | } |
| 2437 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2438 | static void handle_stripe_dirtying6(raid5_conf_t *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2439 | struct stripe_head *sh, struct stripe_head_state *s, |
| 2440 | struct r6_state *r6s, int disks) |
| 2441 | { |
| 2442 | int rcw = 0, must_compute = 0, pd_idx = sh->pd_idx, i; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2443 | int qd_idx = sh->qd_idx; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2444 | for (i = disks; i--; ) { |
| 2445 | struct r5dev *dev = &sh->dev[i]; |
| 2446 | /* Would I have to read this buffer for reconstruct_write */ |
| 2447 | if (!test_bit(R5_OVERWRITE, &dev->flags) |
| 2448 | && i != pd_idx && i != qd_idx |
| 2449 | && (!test_bit(R5_LOCKED, &dev->flags) |
| 2450 | ) && |
| 2451 | !test_bit(R5_UPTODATE, &dev->flags)) { |
| 2452 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
| 2453 | else { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2454 | pr_debug("raid6: must_compute: " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2455 | "disk %d flags=%#lx\n", i, dev->flags); |
| 2456 | must_compute++; |
| 2457 | } |
| 2458 | } |
| 2459 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2460 | pr_debug("for sector %llu, rcw=%d, must_compute=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2461 | (unsigned long long)sh->sector, rcw, must_compute); |
| 2462 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2463 | |
| 2464 | if (rcw > 0) |
| 2465 | /* want reconstruct write, but need to get some data */ |
| 2466 | for (i = disks; i--; ) { |
| 2467 | struct r5dev *dev = &sh->dev[i]; |
| 2468 | if (!test_bit(R5_OVERWRITE, &dev->flags) |
| 2469 | && !(s->failed == 0 && (i == pd_idx || i == qd_idx)) |
| 2470 | && !test_bit(R5_LOCKED, &dev->flags) && |
| 2471 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2472 | test_bit(R5_Insync, &dev->flags)) { |
| 2473 | if ( |
| 2474 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2475 | pr_debug("Read_old stripe %llu " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2476 | "block %d for Reconstruct\n", |
| 2477 | (unsigned long long)sh->sector, i); |
| 2478 | set_bit(R5_LOCKED, &dev->flags); |
| 2479 | set_bit(R5_Wantread, &dev->flags); |
| 2480 | s->locked++; |
| 2481 | } else { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2482 | pr_debug("Request delayed stripe %llu " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2483 | "block %d for Reconstruct\n", |
| 2484 | (unsigned long long)sh->sector, i); |
| 2485 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2486 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2487 | } |
| 2488 | } |
| 2489 | } |
| 2490 | /* now if nothing is locked, and if we have enough data, we can start a |
| 2491 | * write request |
| 2492 | */ |
| 2493 | if (s->locked == 0 && rcw == 0 && |
| 2494 | !test_bit(STRIPE_BIT_DELAY, &sh->state)) { |
| 2495 | if (must_compute > 0) { |
| 2496 | /* We have failed blocks and need to compute them */ |
| 2497 | switch (s->failed) { |
| 2498 | case 0: |
| 2499 | BUG(); |
| 2500 | case 1: |
| 2501 | compute_block_1(sh, r6s->failed_num[0], 0); |
| 2502 | break; |
| 2503 | case 2: |
| 2504 | compute_block_2(sh, r6s->failed_num[0], |
| 2505 | r6s->failed_num[1]); |
| 2506 | break; |
| 2507 | default: /* This request should have been failed? */ |
| 2508 | BUG(); |
| 2509 | } |
| 2510 | } |
| 2511 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2512 | pr_debug("Computing parity for stripe %llu\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2513 | (unsigned long long)sh->sector); |
| 2514 | compute_parity6(sh, RECONSTRUCT_WRITE); |
| 2515 | /* now every locked buffer is ready to be written */ |
| 2516 | for (i = disks; i--; ) |
| 2517 | if (test_bit(R5_LOCKED, &sh->dev[i].flags)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2518 | pr_debug("Writing stripe %llu block %d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2519 | (unsigned long long)sh->sector, i); |
| 2520 | s->locked++; |
| 2521 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 2522 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2523 | if (s->locked == disks) |
| 2524 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2525 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2526 | /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */ |
| 2527 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2528 | |
| 2529 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 2530 | atomic_dec(&conf->preread_active_stripes); |
| 2531 | if (atomic_read(&conf->preread_active_stripes) < |
| 2532 | IO_THRESHOLD) |
| 2533 | md_wakeup_thread(conf->mddev->thread); |
| 2534 | } |
| 2535 | } |
| 2536 | } |
| 2537 | |
| 2538 | static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh, |
| 2539 | struct stripe_head_state *s, int disks) |
| 2540 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2541 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2542 | |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2543 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2544 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2545 | switch (sh->check_state) { |
| 2546 | case check_state_idle: |
| 2547 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2548 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2549 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2550 | sh->check_state = check_state_run; |
| 2551 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2552 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2553 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2554 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2555 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2556 | dev = &sh->dev[s->failed_num]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2557 | /* fall through */ |
| 2558 | case check_state_compute_result: |
| 2559 | sh->check_state = check_state_idle; |
| 2560 | if (!dev) |
| 2561 | dev = &sh->dev[sh->pd_idx]; |
| 2562 | |
| 2563 | /* check that a write has not made the stripe insync */ |
| 2564 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2565 | break; |
| 2566 | |
| 2567 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2568 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 2569 | BUG_ON(s->uptodate != disks); |
| 2570 | |
| 2571 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2572 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2573 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2574 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2575 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2576 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2577 | break; |
| 2578 | case check_state_run: |
| 2579 | break; /* we will be called again upon completion */ |
| 2580 | case check_state_check_result: |
| 2581 | sh->check_state = check_state_idle; |
| 2582 | |
| 2583 | /* if a failure occurred during the check operation, leave |
| 2584 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 2585 | */ |
| 2586 | if (s->failed) |
| 2587 | break; |
| 2588 | |
| 2589 | /* handle a successful check operation, if parity is correct |
| 2590 | * we are done. Otherwise update the mismatch count and repair |
| 2591 | * parity if !MD_RECOVERY_CHECK |
| 2592 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame^] | 2593 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2594 | /* parity is correct (on disc, |
| 2595 | * not in buffer any more) |
| 2596 | */ |
| 2597 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2598 | else { |
| 2599 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2600 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2601 | /* don't try to repair!! */ |
| 2602 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2603 | else { |
| 2604 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2605 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2606 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2607 | set_bit(R5_Wantcompute, |
| 2608 | &sh->dev[sh->pd_idx].flags); |
| 2609 | sh->ops.target = sh->pd_idx; |
| 2610 | s->uptodate++; |
| 2611 | } |
| 2612 | } |
| 2613 | break; |
| 2614 | case check_state_compute_run: |
| 2615 | break; |
| 2616 | default: |
| 2617 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 2618 | __func__, sh->check_state, |
| 2619 | (unsigned long long) sh->sector); |
| 2620 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2621 | } |
| 2622 | } |
| 2623 | |
| 2624 | |
| 2625 | 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] | 2626 | struct stripe_head_state *s, |
| 2627 | struct r6_state *r6s, int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2628 | { |
| 2629 | int update_p = 0, update_q = 0; |
| 2630 | struct r5dev *dev; |
| 2631 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2632 | int qd_idx = sh->qd_idx; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2633 | unsigned long cpu; |
| 2634 | struct page *tmp_page; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2635 | |
| 2636 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2637 | |
| 2638 | BUG_ON(s->failed > 2); |
| 2639 | BUG_ON(s->uptodate < disks); |
| 2640 | /* Want to check and possibly repair P and Q. |
| 2641 | * However there could be one 'failed' device, in which |
| 2642 | * case we can only check one of them, possibly using the |
| 2643 | * other to generate missing data |
| 2644 | */ |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2645 | cpu = get_cpu(); |
| 2646 | tmp_page = per_cpu_ptr(conf->percpu, cpu)->spare_page; |
| 2647 | if (s->failed == r6s->q_failed) { |
| 2648 | /* The only possible failed device holds 'Q', so it |
| 2649 | * makes sense to check P (If anything else were failed, |
| 2650 | * we would have used P to recreate it). |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2651 | */ |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2652 | compute_block_1(sh, pd_idx, 1); |
| 2653 | if (!page_is_zero(sh->dev[pd_idx].page)) { |
| 2654 | compute_block_1(sh, pd_idx, 0); |
| 2655 | update_p = 1; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2656 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2657 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 2658 | if (!r6s->q_failed && s->failed < 2) { |
| 2659 | /* q is not failed, and we didn't use it to generate |
| 2660 | * anything, so it makes sense to check it |
| 2661 | */ |
| 2662 | memcpy(page_address(tmp_page), |
| 2663 | page_address(sh->dev[qd_idx].page), |
| 2664 | STRIPE_SIZE); |
| 2665 | compute_parity6(sh, UPDATE_PARITY); |
| 2666 | if (memcmp(page_address(tmp_page), |
| 2667 | page_address(sh->dev[qd_idx].page), |
| 2668 | STRIPE_SIZE) != 0) { |
| 2669 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 2670 | update_q = 1; |
| 2671 | } |
| 2672 | } |
| 2673 | put_cpu(); |
| 2674 | |
| 2675 | if (update_p || update_q) { |
| 2676 | conf->mddev->resync_mismatches += STRIPE_SECTORS; |
| 2677 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2678 | /* don't try to repair!! */ |
| 2679 | update_p = update_q = 0; |
| 2680 | } |
| 2681 | |
| 2682 | /* now write out any block on a failed drive, |
| 2683 | * or P or Q if they need it |
| 2684 | */ |
| 2685 | |
| 2686 | if (s->failed == 2) { |
| 2687 | dev = &sh->dev[r6s->failed_num[1]]; |
| 2688 | s->locked++; |
| 2689 | set_bit(R5_LOCKED, &dev->flags); |
| 2690 | set_bit(R5_Wantwrite, &dev->flags); |
| 2691 | } |
| 2692 | if (s->failed >= 1) { |
| 2693 | dev = &sh->dev[r6s->failed_num[0]]; |
| 2694 | s->locked++; |
| 2695 | set_bit(R5_LOCKED, &dev->flags); |
| 2696 | set_bit(R5_Wantwrite, &dev->flags); |
| 2697 | } |
| 2698 | |
| 2699 | if (update_p) { |
| 2700 | dev = &sh->dev[pd_idx]; |
| 2701 | s->locked++; |
| 2702 | set_bit(R5_LOCKED, &dev->flags); |
| 2703 | set_bit(R5_Wantwrite, &dev->flags); |
| 2704 | } |
| 2705 | if (update_q) { |
| 2706 | dev = &sh->dev[qd_idx]; |
| 2707 | s->locked++; |
| 2708 | set_bit(R5_LOCKED, &dev->flags); |
| 2709 | set_bit(R5_Wantwrite, &dev->flags); |
| 2710 | } |
| 2711 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 2712 | |
| 2713 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2714 | } |
| 2715 | |
| 2716 | static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh, |
| 2717 | struct r6_state *r6s) |
| 2718 | { |
| 2719 | int i; |
| 2720 | |
| 2721 | /* We have read all the blocks in this stripe and now we need to |
| 2722 | * copy some of them into a target stripe for expand. |
| 2723 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2724 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2725 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 2726 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 2727 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2728 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2729 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2730 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2731 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2732 | sector_t bn = compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2733 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 2734 | &dd_idx, NULL); |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2735 | sh2 = get_active_stripe(conf, s, 0, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2736 | if (sh2 == NULL) |
| 2737 | /* so far only the early blocks of this stripe |
| 2738 | * have been requested. When later blocks |
| 2739 | * get requested, we will try again |
| 2740 | */ |
| 2741 | continue; |
| 2742 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 2743 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 2744 | /* must have already done this block */ |
| 2745 | release_stripe(sh2); |
| 2746 | continue; |
| 2747 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2748 | |
| 2749 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2750 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2751 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 2752 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2753 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2754 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2755 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 2756 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 2757 | for (j = 0; j < conf->raid_disks; j++) |
| 2758 | if (j != sh2->pd_idx && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2759 | (!r6s || j != sh2->qd_idx) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2760 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 2761 | break; |
| 2762 | if (j == conf->raid_disks) { |
| 2763 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 2764 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 2765 | } |
| 2766 | release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2767 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2768 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 2769 | /* done submitting copies, wait for them to complete */ |
| 2770 | if (tx) { |
| 2771 | async_tx_ack(tx); |
| 2772 | dma_wait_for_async_tx(tx); |
| 2773 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2774 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | /* |
| 2778 | * handle_stripe - do things to a stripe. |
| 2779 | * |
| 2780 | * We lock the stripe and then examine the state of various bits |
| 2781 | * to see what needs to be done. |
| 2782 | * Possible results: |
| 2783 | * return some read request which now have data |
| 2784 | * return some write requests which are safely on disc |
| 2785 | * schedule a read on some buffers |
| 2786 | * schedule a write of some buffers |
| 2787 | * return confirmation of parity correctness |
| 2788 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | * buffers are taken off read_list or write_list, and bh_cache buffers |
| 2790 | * get BH_Lock set before the stripe lock is released. |
| 2791 | * |
| 2792 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2793 | |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 2794 | static bool handle_stripe5(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | { |
| 2796 | raid5_conf_t *conf = sh->raid_conf; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2797 | int disks = sh->disks, i; |
| 2798 | struct bio *return_bi = NULL; |
| 2799 | struct stripe_head_state s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | struct r5dev *dev; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2801 | mdk_rdev_t *blocked_rdev = NULL; |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 2802 | int prexor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2804 | memset(&s, 0, sizeof(s)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2805 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d " |
| 2806 | "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state, |
| 2807 | atomic_read(&sh->count), sh->pd_idx, sh->check_state, |
| 2808 | sh->reconstruct_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | |
| 2810 | spin_lock(&sh->lock); |
| 2811 | clear_bit(STRIPE_HANDLE, &sh->state); |
| 2812 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 2813 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2814 | s.syncing = test_bit(STRIPE_SYNCING, &sh->state); |
| 2815 | s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 2816 | s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | /* Now to look around and see what can be done */ |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 2819 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | for (i=disks; i--; ) { |
| 2821 | mdk_rdev_t *rdev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2822 | struct r5dev *dev = &sh->dev[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | clear_bit(R5_Insync, &dev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2825 | pr_debug("check %d: state 0x%lx toread %p read %p write %p " |
| 2826 | "written %p\n", i, dev->flags, dev->toread, dev->read, |
| 2827 | dev->towrite, dev->written); |
| 2828 | |
| 2829 | /* maybe we can request a biofill operation |
| 2830 | * |
| 2831 | * new wantfill requests are only permitted while |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2832 | * ops_complete_biofill is guaranteed to be inactive |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2833 | */ |
| 2834 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2835 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2836 | set_bit(R5_Wantfill, &dev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | |
| 2838 | /* now count some things */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2839 | if (test_bit(R5_LOCKED, &dev->flags)) s.locked++; |
| 2840 | if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2841 | if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2843 | if (test_bit(R5_Wantfill, &dev->flags)) |
| 2844 | s.to_fill++; |
| 2845 | else if (dev->toread) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2846 | s.to_read++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2847 | if (dev->towrite) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2848 | s.to_write++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2850 | s.non_overwrite++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2851 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2852 | if (dev->written) |
| 2853 | s.written++; |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 2854 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2855 | if (blocked_rdev == NULL && |
| 2856 | rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2857 | blocked_rdev = rdev; |
| 2858 | atomic_inc(&rdev->nr_pending); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2859 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2860 | if (!rdev || !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 2861 | /* The ReadError flag will just be confusing now */ |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2862 | clear_bit(R5_ReadError, &dev->flags); |
| 2863 | clear_bit(R5_ReWrite, &dev->flags); |
| 2864 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2865 | if (!rdev || !test_bit(In_sync, &rdev->flags) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2866 | || test_bit(R5_ReadError, &dev->flags)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2867 | s.failed++; |
| 2868 | s.failed_num = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | } else |
| 2870 | set_bit(R5_Insync, &dev->flags); |
| 2871 | } |
NeilBrown | 9910f16 | 2006-01-06 00:20:24 -0800 | [diff] [blame] | 2872 | rcu_read_unlock(); |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2873 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2874 | if (unlikely(blocked_rdev)) { |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2875 | if (s.syncing || s.expanding || s.expanded || |
| 2876 | s.to_write || s.written) { |
| 2877 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2878 | goto unlock; |
| 2879 | } |
| 2880 | /* There is nothing for the blocked_rdev to block */ |
| 2881 | rdev_dec_pending(blocked_rdev, conf->mddev); |
| 2882 | blocked_rdev = NULL; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2883 | } |
| 2884 | |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2885 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 2886 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 2887 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 2888 | } |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2889 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2890 | pr_debug("locked=%d uptodate=%d to_read=%d" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | " to_write=%d failed=%d failed_num=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2892 | s.locked, s.uptodate, s.to_read, s.to_write, |
| 2893 | s.failed, s.failed_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | /* check if the array has lost two devices and, if so, some requests might |
| 2895 | * need to be failed |
| 2896 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2897 | if (s.failed > 1 && s.to_read+s.to_write+s.written) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2898 | handle_failed_stripe(conf, sh, &s, disks, &return_bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2899 | if (s.failed > 1 && s.syncing) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2900 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); |
| 2901 | clear_bit(STRIPE_SYNCING, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2902 | s.syncing = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | /* might be able to return some write requests if the parity block |
| 2906 | * is safe, or on a failed drive |
| 2907 | */ |
| 2908 | dev = &sh->dev[sh->pd_idx]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2909 | if ( s.written && |
| 2910 | ((test_bit(R5_Insync, &dev->flags) && |
| 2911 | !test_bit(R5_LOCKED, &dev->flags) && |
| 2912 | test_bit(R5_UPTODATE, &dev->flags)) || |
| 2913 | (s.failed == 1 && s.failed_num == sh->pd_idx))) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2914 | handle_stripe_clean_event(conf, sh, disks, &return_bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | |
| 2916 | /* Now we might consider reading some blocks, either to check/generate |
| 2917 | * parity, or to satisfy requests |
| 2918 | * or to load a block that is being partially written. |
| 2919 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2920 | if (s.to_read || s.non_overwrite || |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2921 | (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2922 | handle_stripe_fill5(sh, &s, disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2924 | /* Now we check to see if any write operations have recently |
| 2925 | * completed |
| 2926 | */ |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 2927 | prexor = 0; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2928 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 2929 | prexor = 1; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2930 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 2931 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2932 | sh->reconstruct_state = reconstruct_state_idle; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2933 | |
| 2934 | /* All the 'written' buffers and the parity block are ready to |
| 2935 | * be written back to disk |
| 2936 | */ |
| 2937 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags)); |
| 2938 | for (i = disks; i--; ) { |
| 2939 | dev = &sh->dev[i]; |
| 2940 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 2941 | (i == sh->pd_idx || dev->written)) { |
| 2942 | pr_debug("Writing block %d\n", i); |
| 2943 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | e0a115e | 2008-06-05 22:45:52 -0700 | [diff] [blame] | 2944 | if (prexor) |
| 2945 | continue; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2946 | if (!test_bit(R5_Insync, &dev->flags) || |
| 2947 | (i == sh->pd_idx && s.failed == 0)) |
| 2948 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2949 | } |
| 2950 | } |
| 2951 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 2952 | atomic_dec(&conf->preread_active_stripes); |
| 2953 | if (atomic_read(&conf->preread_active_stripes) < |
| 2954 | IO_THRESHOLD) |
| 2955 | md_wakeup_thread(conf->mddev->thread); |
| 2956 | } |
| 2957 | } |
| 2958 | |
| 2959 | /* Now to consider new write requests and what else, if anything |
| 2960 | * should be read. We do not handle new writes when: |
| 2961 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 2962 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 2963 | * block. |
| 2964 | */ |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2965 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2966 | handle_stripe_dirtying5(conf, sh, &s, disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | |
| 2968 | /* 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] | 2969 | * Any reads will already have been scheduled, so we just see if enough |
| 2970 | * data is available. The parity check is held off while parity |
| 2971 | * dependent operations are in flight. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2972 | */ |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2973 | if (sh->check_state || |
| 2974 | (s.syncing && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2975 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2976 | !test_bit(STRIPE_INSYNC, &sh->state))) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2977 | handle_parity_checks5(conf, sh, &s, disks); |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2978 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2979 | if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); |
| 2981 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 2982 | } |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2983 | |
| 2984 | /* If the failed drive is just a ReadError, then we might need to progress |
| 2985 | * the repair/check process |
| 2986 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2987 | if (s.failed == 1 && !conf->mddev->ro && |
| 2988 | test_bit(R5_ReadError, &sh->dev[s.failed_num].flags) |
| 2989 | && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags) |
| 2990 | && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags) |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2991 | ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2992 | dev = &sh->dev[s.failed_num]; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2993 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 2994 | set_bit(R5_Wantwrite, &dev->flags); |
| 2995 | set_bit(R5_ReWrite, &dev->flags); |
| 2996 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2997 | s.locked++; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2998 | } else { |
| 2999 | /* let's read it back */ |
| 3000 | set_bit(R5_Wantread, &dev->flags); |
| 3001 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3002 | s.locked++; |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 3003 | } |
| 3004 | } |
| 3005 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3006 | /* Finish reconstruct operations initiated by the expansion process */ |
| 3007 | if (sh->reconstruct_state == reconstruct_state_result) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3008 | struct stripe_head *sh2 |
| 3009 | = get_active_stripe(conf, sh->sector, 1, 1); |
| 3010 | if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) { |
| 3011 | /* sh cannot be written until sh2 has been read. |
| 3012 | * so arrange for sh to be delayed a little |
| 3013 | */ |
| 3014 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3015 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3016 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3017 | &sh2->state)) |
| 3018 | atomic_inc(&conf->preread_active_stripes); |
| 3019 | release_stripe(sh2); |
| 3020 | goto unlock; |
| 3021 | } |
| 3022 | if (sh2) |
| 3023 | release_stripe(sh2); |
| 3024 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3025 | sh->reconstruct_state = reconstruct_state_idle; |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3026 | clear_bit(STRIPE_EXPANDING, &sh->state); |
Dan Williams | 2339788 | 2008-07-23 20:05:34 -0700 | [diff] [blame] | 3027 | for (i = conf->raid_disks; i--; ) { |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3028 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
Dan Williams | 2339788 | 2008-07-23 20:05:34 -0700 | [diff] [blame] | 3029 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
Neil Brown | efe3114 | 2008-06-28 08:31:14 +1000 | [diff] [blame] | 3030 | s.locked++; |
Dan Williams | 2339788 | 2008-07-23 20:05:34 -0700 | [diff] [blame] | 3031 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3032 | } |
| 3033 | |
| 3034 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3035 | !sh->reconstruct_state) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3036 | /* Need to write out all blocks after computing parity */ |
| 3037 | sh->disks = conf->raid_disks; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3038 | stripe_set_idx(sh->sector, conf, 0, sh); |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3039 | schedule_reconstruction5(sh, &s, 1, 1); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3040 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3041 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3042 | atomic_dec(&conf->reshape_stripes); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3043 | wake_up(&conf->wait_for_overlap); |
| 3044 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3045 | } |
| 3046 | |
Dan Williams | 0f94e87 | 2008-01-08 15:32:53 -0800 | [diff] [blame] | 3047 | if (s.expanding && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3048 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3049 | handle_stripe_expansion(conf, sh, NULL); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3050 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3051 | unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3052 | spin_unlock(&sh->lock); |
| 3053 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3054 | /* wait for this device to become unblocked */ |
| 3055 | if (unlikely(blocked_rdev)) |
| 3056 | md_wait_for_blocked_rdev(blocked_rdev, conf->mddev); |
| 3057 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3058 | if (s.ops_request) |
| 3059 | raid5_run_ops(sh, s.ops_request); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3060 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 3061 | ops_run_io(sh, &s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3062 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3063 | return_io(return_bi); |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 3064 | |
| 3065 | return blocked_rdev == NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | } |
| 3067 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3068 | static bool handle_stripe6(struct stripe_head *sh) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3069 | { |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3070 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3071 | int disks = sh->disks; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3072 | struct bio *return_bi = NULL; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3073 | int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3074 | struct stripe_head_state s; |
| 3075 | struct r6_state r6s; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3076 | struct r5dev *dev, *pdev, *qdev; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3077 | mdk_rdev_t *blocked_rdev = NULL; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3078 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3079 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3080 | "pd_idx=%d, qd_idx=%d\n", |
| 3081 | (unsigned long long)sh->sector, sh->state, |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3082 | atomic_read(&sh->count), pd_idx, qd_idx); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3083 | memset(&s, 0, sizeof(s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3084 | |
| 3085 | spin_lock(&sh->lock); |
| 3086 | clear_bit(STRIPE_HANDLE, &sh->state); |
| 3087 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3088 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3089 | s.syncing = test_bit(STRIPE_SYNCING, &sh->state); |
| 3090 | s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3091 | s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3092 | /* Now to look around and see what can be done */ |
| 3093 | |
| 3094 | rcu_read_lock(); |
| 3095 | for (i=disks; i--; ) { |
| 3096 | mdk_rdev_t *rdev; |
| 3097 | dev = &sh->dev[i]; |
| 3098 | clear_bit(R5_Insync, &dev->flags); |
| 3099 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3100 | 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] | 3101 | i, dev->flags, dev->toread, dev->towrite, dev->written); |
| 3102 | /* maybe we can reply to a read */ |
| 3103 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) { |
| 3104 | struct bio *rbi, *rbi2; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3105 | pr_debug("Return read for disc %d\n", i); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3106 | spin_lock_irq(&conf->device_lock); |
| 3107 | rbi = dev->toread; |
| 3108 | dev->toread = NULL; |
| 3109 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 3110 | wake_up(&conf->wait_for_overlap); |
| 3111 | spin_unlock_irq(&conf->device_lock); |
| 3112 | while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) { |
| 3113 | copy_data(0, rbi, dev->page, dev->sector); |
| 3114 | rbi2 = r5_next_bio(rbi, dev->sector); |
| 3115 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3116 | if (!raid5_dec_bi_phys_segments(rbi)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3117 | rbi->bi_next = return_bi; |
| 3118 | return_bi = rbi; |
| 3119 | } |
| 3120 | spin_unlock_irq(&conf->device_lock); |
| 3121 | rbi = rbi2; |
| 3122 | } |
| 3123 | } |
| 3124 | |
| 3125 | /* now count some things */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3126 | if (test_bit(R5_LOCKED, &dev->flags)) s.locked++; |
| 3127 | if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3128 | |
| 3129 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3130 | if (dev->toread) |
| 3131 | s.to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3132 | if (dev->towrite) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3133 | s.to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3134 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3135 | s.non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3136 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3137 | if (dev->written) |
| 3138 | s.written++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3139 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3140 | if (blocked_rdev == NULL && |
| 3141 | rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3142 | blocked_rdev = rdev; |
| 3143 | atomic_inc(&rdev->nr_pending); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3144 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3145 | if (!rdev || !test_bit(In_sync, &rdev->flags)) { |
| 3146 | /* The ReadError flag will just be confusing now */ |
| 3147 | clear_bit(R5_ReadError, &dev->flags); |
| 3148 | clear_bit(R5_ReWrite, &dev->flags); |
| 3149 | } |
| 3150 | if (!rdev || !test_bit(In_sync, &rdev->flags) |
| 3151 | || test_bit(R5_ReadError, &dev->flags)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3152 | if (s.failed < 2) |
| 3153 | r6s.failed_num[s.failed] = i; |
| 3154 | s.failed++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3155 | } else |
| 3156 | set_bit(R5_Insync, &dev->flags); |
| 3157 | } |
| 3158 | rcu_read_unlock(); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3159 | |
| 3160 | if (unlikely(blocked_rdev)) { |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3161 | if (s.syncing || s.expanding || s.expanded || |
| 3162 | s.to_write || s.written) { |
| 3163 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3164 | goto unlock; |
| 3165 | } |
| 3166 | /* There is nothing for the blocked_rdev to block */ |
| 3167 | rdev_dec_pending(blocked_rdev, conf->mddev); |
| 3168 | blocked_rdev = NULL; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3169 | } |
NeilBrown | ac4090d | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3170 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3171 | pr_debug("locked=%d uptodate=%d to_read=%d" |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3172 | " to_write=%d failed=%d failed_num=%d,%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3173 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 3174 | r6s.failed_num[0], r6s.failed_num[1]); |
| 3175 | /* check if the array has lost >2 devices and, if so, some requests |
| 3176 | * might need to be failed |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3177 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3178 | if (s.failed > 2 && s.to_read+s.to_write+s.written) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3179 | handle_failed_stripe(conf, sh, &s, disks, &return_bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3180 | if (s.failed > 2 && s.syncing) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3181 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); |
| 3182 | clear_bit(STRIPE_SYNCING, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3183 | s.syncing = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | /* |
| 3187 | * might be able to return some write requests if the parity blocks |
| 3188 | * are safe, or on a failed drive |
| 3189 | */ |
| 3190 | pdev = &sh->dev[pd_idx]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3191 | r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx) |
| 3192 | || (s.failed >= 2 && r6s.failed_num[1] == pd_idx); |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3193 | qdev = &sh->dev[qd_idx]; |
| 3194 | r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx) |
| 3195 | || (s.failed >= 2 && r6s.failed_num[1] == qd_idx); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3196 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3197 | if ( s.written && |
| 3198 | ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3199 | && !test_bit(R5_LOCKED, &pdev->flags) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3200 | && test_bit(R5_UPTODATE, &pdev->flags)))) && |
| 3201 | ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3202 | && !test_bit(R5_LOCKED, &qdev->flags) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3203 | && test_bit(R5_UPTODATE, &qdev->flags))))) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3204 | handle_stripe_clean_event(conf, sh, disks, &return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3205 | |
| 3206 | /* Now we might consider reading some blocks, either to check/generate |
| 3207 | * parity, or to satisfy requests |
| 3208 | * or to load a block that is being partially written. |
| 3209 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3210 | if (s.to_read || s.non_overwrite || (s.to_write && s.failed) || |
| 3211 | (s.syncing && (s.uptodate < disks)) || s.expanding) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3212 | handle_stripe_fill6(sh, &s, &r6s, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3213 | |
| 3214 | /* now to consider writing and what else, if anything should be read */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3215 | if (s.to_write) |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3216 | handle_stripe_dirtying6(conf, sh, &s, &r6s, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3217 | |
| 3218 | /* 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] | 3219 | * Any reads will already have been scheduled, so we just see if enough |
| 3220 | * data is available |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3221 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3222 | if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3223 | handle_parity_checks6(conf, sh, &s, &r6s, disks); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3224 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3225 | if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3226 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); |
| 3227 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 3228 | } |
| 3229 | |
| 3230 | /* If the failed drives are just a ReadError, then we might need |
| 3231 | * to progress the repair/check process |
| 3232 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3233 | if (s.failed <= 2 && !conf->mddev->ro) |
| 3234 | for (i = 0; i < s.failed; i++) { |
| 3235 | dev = &sh->dev[r6s.failed_num[i]]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3236 | if (test_bit(R5_ReadError, &dev->flags) |
| 3237 | && !test_bit(R5_LOCKED, &dev->flags) |
| 3238 | && test_bit(R5_UPTODATE, &dev->flags) |
| 3239 | ) { |
| 3240 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3241 | set_bit(R5_Wantwrite, &dev->flags); |
| 3242 | set_bit(R5_ReWrite, &dev->flags); |
| 3243 | set_bit(R5_LOCKED, &dev->flags); |
| 3244 | } else { |
| 3245 | /* let's read it back */ |
| 3246 | set_bit(R5_Wantread, &dev->flags); |
| 3247 | set_bit(R5_LOCKED, &dev->flags); |
| 3248 | } |
| 3249 | } |
| 3250 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3251 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3252 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3253 | struct stripe_head *sh2 |
| 3254 | = get_active_stripe(conf, sh->sector, 1, 1); |
| 3255 | if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) { |
| 3256 | /* sh cannot be written until sh2 has been read. |
| 3257 | * so arrange for sh to be delayed a little |
| 3258 | */ |
| 3259 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3260 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3261 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3262 | &sh2->state)) |
| 3263 | atomic_inc(&conf->preread_active_stripes); |
| 3264 | release_stripe(sh2); |
| 3265 | goto unlock; |
| 3266 | } |
| 3267 | if (sh2) |
| 3268 | release_stripe(sh2); |
| 3269 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3270 | /* Need to write out all blocks after computing P&Q */ |
| 3271 | sh->disks = conf->raid_disks; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3272 | stripe_set_idx(sh->sector, conf, 0, sh); |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3273 | compute_parity6(sh, RECONSTRUCT_WRITE); |
| 3274 | for (i = conf->raid_disks ; i-- ; ) { |
| 3275 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3276 | s.locked++; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3277 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 3278 | } |
| 3279 | clear_bit(STRIPE_EXPANDING, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3280 | } else if (s.expanded) { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3281 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3282 | atomic_dec(&conf->reshape_stripes); |
| 3283 | wake_up(&conf->wait_for_overlap); |
| 3284 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3285 | } |
| 3286 | |
Dan Williams | 0f94e87 | 2008-01-08 15:32:53 -0800 | [diff] [blame] | 3287 | if (s.expanding && s.locked == 0 && |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3288 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3289 | handle_stripe_expansion(conf, sh, &r6s); |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3290 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3291 | unlock: |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3292 | spin_unlock(&sh->lock); |
| 3293 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3294 | /* wait for this device to become unblocked */ |
| 3295 | if (unlikely(blocked_rdev)) |
| 3296 | md_wait_for_blocked_rdev(blocked_rdev, conf->mddev); |
| 3297 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 3298 | ops_run_io(sh, &s); |
| 3299 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3300 | return_io(return_bi); |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 3301 | |
| 3302 | return blocked_rdev == NULL; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3303 | } |
| 3304 | |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 3305 | /* returns true if the stripe was handled */ |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3306 | static bool handle_stripe(struct stripe_head *sh) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3307 | { |
| 3308 | if (sh->raid_conf->level == 6) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3309 | return handle_stripe6(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3310 | else |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 3311 | return handle_stripe5(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3312 | } |
| 3313 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 3314 | static void raid5_activate_delayed(raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3315 | { |
| 3316 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 3317 | while (!list_empty(&conf->delayed_list)) { |
| 3318 | struct list_head *l = conf->delayed_list.next; |
| 3319 | struct stripe_head *sh; |
| 3320 | sh = list_entry(l, struct stripe_head, lru); |
| 3321 | list_del_init(l); |
| 3322 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3323 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3324 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3325 | list_add_tail(&sh->lru, &conf->hold_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | } |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 3327 | } else |
| 3328 | blk_plug_device(conf->mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | } |
| 3330 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 3331 | static void activate_bit_delay(raid5_conf_t *conf) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3332 | { |
| 3333 | /* device_lock is held */ |
| 3334 | struct list_head head; |
| 3335 | list_add(&head, &conf->bitmap_list); |
| 3336 | list_del_init(&conf->bitmap_list); |
| 3337 | while (!list_empty(&head)) { |
| 3338 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
| 3339 | list_del_init(&sh->lru); |
| 3340 | atomic_inc(&sh->count); |
| 3341 | __release_stripe(conf, sh); |
| 3342 | } |
| 3343 | } |
| 3344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3345 | static void unplug_slaves(mddev_t *mddev) |
| 3346 | { |
| 3347 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 3348 | int i; |
| 3349 | |
| 3350 | rcu_read_lock(); |
| 3351 | for (i=0; i<mddev->raid_disks; i++) { |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 3352 | mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3353 | if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3354 | struct request_queue *r_queue = bdev_get_queue(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3355 | |
| 3356 | atomic_inc(&rdev->nr_pending); |
| 3357 | rcu_read_unlock(); |
| 3358 | |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 3359 | blk_unplug(r_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | |
| 3361 | rdev_dec_pending(rdev, mddev); |
| 3362 | rcu_read_lock(); |
| 3363 | } |
| 3364 | } |
| 3365 | rcu_read_unlock(); |
| 3366 | } |
| 3367 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3368 | static void raid5_unplug_device(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3369 | { |
| 3370 | mddev_t *mddev = q->queuedata; |
| 3371 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 3372 | unsigned long flags; |
| 3373 | |
| 3374 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3375 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3376 | if (blk_remove_plug(q)) { |
| 3377 | conf->seq_flush++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3378 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3379 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3380 | md_wakeup_thread(mddev->thread); |
| 3381 | |
| 3382 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3383 | |
| 3384 | unplug_slaves(mddev); |
| 3385 | } |
| 3386 | |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3387 | static int raid5_congested(void *data, int bits) |
| 3388 | { |
| 3389 | mddev_t *mddev = data; |
| 3390 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 3391 | |
| 3392 | /* No difference between reads and writes. Just check |
| 3393 | * how busy the stripe_cache is |
| 3394 | */ |
| 3395 | if (conf->inactive_blocked) |
| 3396 | return 1; |
| 3397 | if (conf->quiesce) |
| 3398 | return 1; |
| 3399 | if (list_empty_careful(&conf->inactive_list)) |
| 3400 | return 1; |
| 3401 | |
| 3402 | return 0; |
| 3403 | } |
| 3404 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3405 | /* We want read requests to align with chunks where possible, |
| 3406 | * but write requests don't need to. |
| 3407 | */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3408 | static int raid5_mergeable_bvec(struct request_queue *q, |
| 3409 | struct bvec_merge_data *bvm, |
| 3410 | struct bio_vec *biovec) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3411 | { |
| 3412 | mddev_t *mddev = q->queuedata; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3413 | 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] | 3414 | int max; |
| 3415 | unsigned int chunk_sectors = mddev->chunk_size >> 9; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3416 | unsigned int bio_sectors = bvm->bi_size >> 9; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3417 | |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3418 | if ((bvm->bi_rw & 1) == WRITE) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3419 | return biovec->bv_len; /* always allow writes to be mergeable */ |
| 3420 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3421 | if (mddev->new_chunk < mddev->chunk_size) |
| 3422 | chunk_sectors = mddev->new_chunk >> 9; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3423 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 3424 | if (max < 0) max = 0; |
| 3425 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 3426 | return biovec->bv_len; |
| 3427 | else |
| 3428 | return max; |
| 3429 | } |
| 3430 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3431 | |
| 3432 | static int in_chunk_boundary(mddev_t *mddev, struct bio *bio) |
| 3433 | { |
| 3434 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); |
| 3435 | unsigned int chunk_sectors = mddev->chunk_size >> 9; |
| 3436 | unsigned int bio_sectors = bio->bi_size >> 9; |
| 3437 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3438 | if (mddev->new_chunk < mddev->chunk_size) |
| 3439 | chunk_sectors = mddev->new_chunk >> 9; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3440 | return chunk_sectors >= |
| 3441 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 3442 | } |
| 3443 | |
| 3444 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3445 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 3446 | * later sampled by raid5d. |
| 3447 | */ |
| 3448 | static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf) |
| 3449 | { |
| 3450 | unsigned long flags; |
| 3451 | |
| 3452 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3453 | |
| 3454 | bi->bi_next = conf->retry_read_aligned_list; |
| 3455 | conf->retry_read_aligned_list = bi; |
| 3456 | |
| 3457 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3458 | md_wakeup_thread(conf->mddev->thread); |
| 3459 | } |
| 3460 | |
| 3461 | |
| 3462 | static struct bio *remove_bio_from_retry(raid5_conf_t *conf) |
| 3463 | { |
| 3464 | struct bio *bi; |
| 3465 | |
| 3466 | bi = conf->retry_read_aligned; |
| 3467 | if (bi) { |
| 3468 | conf->retry_read_aligned = NULL; |
| 3469 | return bi; |
| 3470 | } |
| 3471 | bi = conf->retry_read_aligned_list; |
| 3472 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3473 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3474 | bi->bi_next = NULL; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3475 | /* |
| 3476 | * this sets the active strip count to 1 and the processed |
| 3477 | * strip count to zero (upper 8 bits) |
| 3478 | */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3479 | bi->bi_phys_segments = 1; /* biased count of active stripes */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3480 | } |
| 3481 | |
| 3482 | return bi; |
| 3483 | } |
| 3484 | |
| 3485 | |
| 3486 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3487 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 3488 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 3489 | * first). |
| 3490 | * If the read failed.. |
| 3491 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3492 | static void raid5_align_endio(struct bio *bi, int error) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3493 | { |
| 3494 | struct bio* raid_bi = bi->bi_private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3495 | mddev_t *mddev; |
| 3496 | raid5_conf_t *conf; |
| 3497 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
| 3498 | mdk_rdev_t *rdev; |
| 3499 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3500 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3501 | |
| 3502 | mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata; |
| 3503 | conf = mddev_to_conf(mddev); |
| 3504 | rdev = (void*)raid_bi->bi_next; |
| 3505 | raid_bi->bi_next = NULL; |
| 3506 | |
| 3507 | rdev_dec_pending(rdev, conf->mddev); |
| 3508 | |
| 3509 | if (!error && uptodate) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3510 | bio_endio(raid_bi, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3511 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 3512 | wake_up(&conf->wait_for_stripe); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3513 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3514 | } |
| 3515 | |
| 3516 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3517 | 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] | 3518 | |
| 3519 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3520 | } |
| 3521 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3522 | static int bio_fits_rdev(struct bio *bi) |
| 3523 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3524 | struct request_queue *q = bdev_get_queue(bi->bi_bdev); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3525 | |
| 3526 | if ((bi->bi_size>>9) > q->max_sectors) |
| 3527 | return 0; |
| 3528 | blk_recount_segments(q, bi); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3529 | if (bi->bi_phys_segments > q->max_phys_segments) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3530 | return 0; |
| 3531 | |
| 3532 | if (q->merge_bvec_fn) |
| 3533 | /* it's too hard to apply the merge_bvec_fn at this stage, |
| 3534 | * just just give up |
| 3535 | */ |
| 3536 | return 0; |
| 3537 | |
| 3538 | return 1; |
| 3539 | } |
| 3540 | |
| 3541 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3542 | 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] | 3543 | { |
| 3544 | mddev_t *mddev = q->queuedata; |
| 3545 | raid5_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3546 | unsigned int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3547 | struct bio* align_bi; |
| 3548 | mdk_rdev_t *rdev; |
| 3549 | |
| 3550 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3551 | pr_debug("chunk_aligned_read : non aligned\n"); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3552 | return 0; |
| 3553 | } |
| 3554 | /* |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3555 | * use bio_clone to make a copy of the bio |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3556 | */ |
| 3557 | align_bi = bio_clone(raid_bio, GFP_NOIO); |
| 3558 | if (!align_bi) |
| 3559 | return 0; |
| 3560 | /* |
| 3561 | * set bi_end_io to a new function, and set bi_private to the |
| 3562 | * original bio. |
| 3563 | */ |
| 3564 | align_bi->bi_end_io = raid5_align_endio; |
| 3565 | align_bi->bi_private = raid_bio; |
| 3566 | /* |
| 3567 | * compute position |
| 3568 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3569 | align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector, |
| 3570 | 0, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3571 | &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3572 | |
| 3573 | rcu_read_lock(); |
| 3574 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 3575 | if (rdev && test_bit(In_sync, &rdev->flags)) { |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3576 | atomic_inc(&rdev->nr_pending); |
| 3577 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3578 | raid_bio->bi_next = (void*)rdev; |
| 3579 | align_bi->bi_bdev = rdev->bdev; |
| 3580 | align_bi->bi_flags &= ~(1 << BIO_SEG_VALID); |
| 3581 | align_bi->bi_sector += rdev->data_offset; |
| 3582 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3583 | if (!bio_fits_rdev(align_bi)) { |
| 3584 | /* too big in some way */ |
| 3585 | bio_put(align_bi); |
| 3586 | rdev_dec_pending(rdev, mddev); |
| 3587 | return 0; |
| 3588 | } |
| 3589 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3590 | spin_lock_irq(&conf->device_lock); |
| 3591 | wait_event_lock_irq(conf->wait_for_stripe, |
| 3592 | conf->quiesce == 0, |
| 3593 | conf->device_lock, /* nothing */); |
| 3594 | atomic_inc(&conf->active_aligned_reads); |
| 3595 | spin_unlock_irq(&conf->device_lock); |
| 3596 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3597 | generic_make_request(align_bi); |
| 3598 | return 1; |
| 3599 | } else { |
| 3600 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3601 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3602 | return 0; |
| 3603 | } |
| 3604 | } |
| 3605 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3606 | /* __get_priority_stripe - get the next stripe to process |
| 3607 | * |
| 3608 | * Full stripe writes are allowed to pass preread active stripes up until |
| 3609 | * the bypass_threshold is exceeded. In general the bypass_count |
| 3610 | * increments when the handle_list is handled before the hold_list; however, it |
| 3611 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 3612 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 3613 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 3614 | * handle_list. |
| 3615 | */ |
| 3616 | static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf) |
| 3617 | { |
| 3618 | struct stripe_head *sh; |
| 3619 | |
| 3620 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 3621 | __func__, |
| 3622 | list_empty(&conf->handle_list) ? "empty" : "busy", |
| 3623 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 3624 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 3625 | |
| 3626 | if (!list_empty(&conf->handle_list)) { |
| 3627 | sh = list_entry(conf->handle_list.next, typeof(*sh), lru); |
| 3628 | |
| 3629 | if (list_empty(&conf->hold_list)) |
| 3630 | conf->bypass_count = 0; |
| 3631 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 3632 | if (conf->hold_list.next == conf->last_hold) |
| 3633 | conf->bypass_count++; |
| 3634 | else { |
| 3635 | conf->last_hold = conf->hold_list.next; |
| 3636 | conf->bypass_count -= conf->bypass_threshold; |
| 3637 | if (conf->bypass_count < 0) |
| 3638 | conf->bypass_count = 0; |
| 3639 | } |
| 3640 | } |
| 3641 | } else if (!list_empty(&conf->hold_list) && |
| 3642 | ((conf->bypass_threshold && |
| 3643 | conf->bypass_count > conf->bypass_threshold) || |
| 3644 | atomic_read(&conf->pending_full_writes) == 0)) { |
| 3645 | sh = list_entry(conf->hold_list.next, |
| 3646 | typeof(*sh), lru); |
| 3647 | conf->bypass_count -= conf->bypass_threshold; |
| 3648 | if (conf->bypass_count < 0) |
| 3649 | conf->bypass_count = 0; |
| 3650 | } else |
| 3651 | return NULL; |
| 3652 | |
| 3653 | list_del_init(&sh->lru); |
| 3654 | atomic_inc(&sh->count); |
| 3655 | BUG_ON(atomic_read(&sh->count) != 1); |
| 3656 | return sh; |
| 3657 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3658 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3659 | static int make_request(struct request_queue *q, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3660 | { |
| 3661 | mddev_t *mddev = q->queuedata; |
| 3662 | raid5_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3663 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3664 | sector_t new_sector; |
| 3665 | sector_t logical_sector, last_sector; |
| 3666 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 3667 | const int rw = bio_data_dir(bi); |
Tejun Heo | c995905 | 2008-08-25 19:47:21 +0900 | [diff] [blame] | 3668 | int cpu, remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3669 | |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 3670 | if (unlikely(bio_barrier(bi))) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3671 | bio_endio(bi, -EOPNOTSUPP); |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 3672 | return 0; |
| 3673 | } |
| 3674 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 3675 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3676 | |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 3677 | cpu = part_stat_lock(); |
| 3678 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 3679 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], |
| 3680 | bio_sectors(bi)); |
| 3681 | part_stat_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3682 | |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 3683 | if (rw == READ && |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 3684 | mddev->reshape_position == MaxSector && |
| 3685 | chunk_aligned_read(q,bi)) |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3686 | return 0; |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 3687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3688 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 3689 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 3690 | bi->bi_next = NULL; |
| 3691 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 3692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3693 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
| 3694 | DEFINE_WAIT(w); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3695 | int disks, data_disks; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3696 | int previous; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3697 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3698 | retry: |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3699 | previous = 0; |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3700 | disks = conf->raid_disks; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3701 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3702 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3703 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 3704 | * 64bit on a 32bit platform, and so it might be |
| 3705 | * possible to see a half-updated value |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3706 | * Ofcourse reshape_progress could change after |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 3707 | * the lock is dropped, so once we get a reference |
| 3708 | * to the stripe that we think it is, we will have |
| 3709 | * to check again. |
| 3710 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3711 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3712 | if (mddev->delta_disks < 0 |
| 3713 | ? logical_sector < conf->reshape_progress |
| 3714 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3715 | disks = conf->previous_raid_disks; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3716 | previous = 1; |
| 3717 | } else { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3718 | if (mddev->delta_disks < 0 |
| 3719 | ? logical_sector < conf->reshape_safe |
| 3720 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 3721 | spin_unlock_irq(&conf->device_lock); |
| 3722 | schedule(); |
| 3723 | goto retry; |
| 3724 | } |
| 3725 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3726 | spin_unlock_irq(&conf->device_lock); |
| 3727 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3728 | data_disks = disks - conf->max_degraded; |
| 3729 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3730 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 3731 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3732 | &dd_idx, NULL); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3733 | pr_debug("raid5: make_request, sector %llu logical %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3734 | (unsigned long long)new_sector, |
| 3735 | (unsigned long long)logical_sector); |
| 3736 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3737 | sh = get_active_stripe(conf, new_sector, previous, |
| 3738 | (bi->bi_rw&RWA_MASK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3739 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3740 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3741 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 3742 | * stripe, so we must do the range check again. |
| 3743 | * Expansion could still move past after this |
| 3744 | * test, but as we are holding a reference to |
| 3745 | * 'sh', we know that if that happens, |
| 3746 | * STRIPE_EXPANDING will get set and the expansion |
| 3747 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3748 | */ |
| 3749 | int must_retry = 0; |
| 3750 | spin_lock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 3751 | if (mddev->delta_disks < 0 |
| 3752 | ? logical_sector >= conf->reshape_progress |
| 3753 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3754 | /* mismatch, need to try again */ |
| 3755 | must_retry = 1; |
| 3756 | spin_unlock_irq(&conf->device_lock); |
| 3757 | if (must_retry) { |
| 3758 | release_stripe(sh); |
| 3759 | goto retry; |
| 3760 | } |
| 3761 | } |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3762 | /* FIXME what if we get a false positive because these |
| 3763 | * are being updated. |
| 3764 | */ |
| 3765 | if (logical_sector >= mddev->suspend_lo && |
| 3766 | logical_sector < mddev->suspend_hi) { |
| 3767 | release_stripe(sh); |
| 3768 | schedule(); |
| 3769 | goto retry; |
| 3770 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 3771 | |
| 3772 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
| 3773 | !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) { |
| 3774 | /* Stripe is busy expanding or |
| 3775 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | * and wait a while |
| 3777 | */ |
| 3778 | raid5_unplug_device(mddev->queue); |
| 3779 | release_stripe(sh); |
| 3780 | schedule(); |
| 3781 | goto retry; |
| 3782 | } |
| 3783 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 3784 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3785 | clear_bit(STRIPE_DELAYED, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3786 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3787 | } else { |
| 3788 | /* cannot get stripe for read-ahead, just give-up */ |
| 3789 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 3790 | finish_wait(&conf->wait_for_overlap, &w); |
| 3791 | break; |
| 3792 | } |
| 3793 | |
| 3794 | } |
| 3795 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3796 | remaining = raid5_dec_bi_phys_segments(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 3797 | spin_unlock_irq(&conf->device_lock); |
| 3798 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3799 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3800 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3801 | md_write_end(mddev); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3802 | |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 3803 | bio_endio(bi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3804 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3805 | return 0; |
| 3806 | } |
| 3807 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3808 | static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks); |
| 3809 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3810 | 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] | 3811 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3812 | /* reshaping is quite different to recovery/resync so it is |
| 3813 | * handled quite separately ... here. |
| 3814 | * |
| 3815 | * On each call to sync_request, we gather one chunk worth of |
| 3816 | * destination stripes and flag them as expanding. |
| 3817 | * Then we find all the source stripes and request reads. |
| 3818 | * As the reads complete, handle_stripe will copy the data |
| 3819 | * into the destination stripe and release that stripe. |
| 3820 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 3822 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3823 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3824 | int raid_disks = conf->previous_raid_disks; |
| 3825 | int data_disks = raid_disks - conf->max_degraded; |
| 3826 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3827 | int i; |
| 3828 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 3829 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 3830 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3831 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3832 | struct list_head stripes; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3833 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3834 | if (sector_nr == 0) { |
| 3835 | /* If restarting in the middle, skip the initial sectors */ |
| 3836 | if (mddev->delta_disks < 0 && |
| 3837 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 3838 | sector_nr = raid5_size(mddev, 0, 0) |
| 3839 | - conf->reshape_progress; |
| 3840 | } else if (mddev->delta_disks > 0 && |
| 3841 | conf->reshape_progress > 0) |
| 3842 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3843 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3844 | if (sector_nr) { |
| 3845 | *skipped = 1; |
| 3846 | return sector_nr; |
| 3847 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3848 | } |
| 3849 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3850 | /* We need to process a full chunk at a time. |
| 3851 | * If old and new chunk sizes differ, we need to process the |
| 3852 | * largest of these |
| 3853 | */ |
| 3854 | if (mddev->new_chunk > mddev->chunk_size) |
| 3855 | reshape_sectors = mddev->new_chunk / 512; |
| 3856 | else |
| 3857 | reshape_sectors = mddev->chunk_size / 512; |
| 3858 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3859 | /* we update the metadata when there is more than 3Meg |
| 3860 | * in the block range (that is rather arbitrary, should |
| 3861 | * probably be time based) or when the data about to be |
| 3862 | * copied would over-write the source of the data at |
| 3863 | * the front of the range. |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3864 | * i.e. one new_stripe along from reshape_progress new_maps |
| 3865 | * to after where reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3866 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3867 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3868 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 3869 | readpos = conf->reshape_progress; |
| 3870 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3871 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3872 | sector_div(safepos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3873 | if (mddev->delta_disks < 0) { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3874 | writepos -= reshape_sectors; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 3875 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3876 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3877 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3878 | writepos += reshape_sectors; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 3879 | readpos -= reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3880 | safepos -= reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3881 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3882 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 3883 | /* 'writepos' is the most advanced device address we might write. |
| 3884 | * 'readpos' is the least advanced device address we might read. |
| 3885 | * 'safepos' is the least address recorded in the metadata as having |
| 3886 | * been reshaped. |
| 3887 | * If 'readpos' is behind 'writepos', then there is no way that we can |
| 3888 | * ensure safety in the face of a crash - that must be done by userspace |
| 3889 | * making a backup of the data. So in that case there is no particular |
| 3890 | * rush to update metadata. |
| 3891 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 3892 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 3893 | * we can be safe in the event of a crash. |
| 3894 | * So we insist on updating metadata if safepos is behind writepos and |
| 3895 | * readpos is beyond writepos. |
| 3896 | * In any case, update the metadata every 10 seconds. |
| 3897 | * Maybe that number should be configurable, but I'm not sure it is |
| 3898 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 3899 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3900 | if ((mddev->delta_disks < 0 |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 3901 | ? (safepos > writepos && readpos < writepos) |
| 3902 | : (safepos < writepos && readpos > writepos)) || |
| 3903 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3904 | /* Cannot proceed until we've updated the superblock... */ |
| 3905 | wait_event(conf->wait_for_overlap, |
| 3906 | atomic_read(&conf->reshape_stripes)==0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3907 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 3908 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3909 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3910 | md_wakeup_thread(mddev->thread); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3911 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3912 | kthread_should_stop()); |
| 3913 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3914 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3915 | spin_unlock_irq(&conf->device_lock); |
| 3916 | wake_up(&conf->wait_for_overlap); |
| 3917 | } |
| 3918 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 3919 | if (mddev->delta_disks < 0) { |
| 3920 | BUG_ON(conf->reshape_progress == 0); |
| 3921 | stripe_addr = writepos; |
| 3922 | BUG_ON((mddev->dev_sectors & |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3923 | ~((sector_t)reshape_sectors - 1)) |
| 3924 | - reshape_sectors - stripe_addr |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 3925 | != sector_nr); |
| 3926 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3927 | BUG_ON(writepos != sector_nr + reshape_sectors); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 3928 | stripe_addr = sector_nr; |
| 3929 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3930 | INIT_LIST_HEAD(&stripes); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3931 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3932 | int j; |
| 3933 | int skipped = 0; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 3934 | sh = get_active_stripe(conf, stripe_addr+i, 0, 0); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3935 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 3936 | atomic_inc(&conf->reshape_stripes); |
| 3937 | /* If any of this stripe is beyond the end of the old |
| 3938 | * array, then we need to zero those blocks |
| 3939 | */ |
| 3940 | for (j=sh->disks; j--;) { |
| 3941 | sector_t s; |
| 3942 | if (j == sh->pd_idx) |
| 3943 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3944 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3945 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3946 | continue; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3947 | s = compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3948 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3949 | skipped = 1; |
| 3950 | continue; |
| 3951 | } |
| 3952 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 3953 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 3954 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 3955 | } |
| 3956 | if (!skipped) { |
| 3957 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3958 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3959 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3960 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3961 | } |
| 3962 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3963 | if (mddev->delta_disks < 0) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3964 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 3965 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 3966 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3967 | spin_unlock_irq(&conf->device_lock); |
| 3968 | /* Ok, those stripe are ready. We can start scheduling |
| 3969 | * reads on the source stripes. |
| 3970 | * The source stripes are determined by mapping the first and last |
| 3971 | * block on the destination stripes. |
| 3972 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3973 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 3974 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3975 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3976 | last_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 3977 | raid5_compute_sector(conf, ((stripe_addr+conf->chunk_size/512) |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3978 | *(new_data_disks) - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3979 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3980 | if (last_sector >= mddev->dev_sectors) |
| 3981 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3982 | while (first_sector <= last_sector) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3983 | sh = get_active_stripe(conf, first_sector, 1, 0); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3984 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3985 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3986 | release_stripe(sh); |
| 3987 | first_sector += STRIPE_SECTORS; |
| 3988 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 3989 | /* Now that the sources are clearly marked, we can release |
| 3990 | * the destination stripes |
| 3991 | */ |
| 3992 | while (!list_empty(&stripes)) { |
| 3993 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 3994 | list_del_init(&sh->lru); |
| 3995 | release_stripe(sh); |
| 3996 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3997 | /* If this takes us to the resync_max point where we have to pause, |
| 3998 | * then we need to write out the superblock. |
| 3999 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4000 | sector_nr += reshape_sectors; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4001 | if (sector_nr >= mddev->resync_max) { |
| 4002 | /* Cannot proceed until we've updated the superblock... */ |
| 4003 | wait_event(conf->wait_for_overlap, |
| 4004 | atomic_read(&conf->reshape_stripes) == 0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4005 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4006 | conf->reshape_checkpoint = jiffies; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4007 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 4008 | md_wakeup_thread(mddev->thread); |
| 4009 | wait_event(mddev->sb_wait, |
| 4010 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) |
| 4011 | || kthread_should_stop()); |
| 4012 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4013 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4014 | spin_unlock_irq(&conf->device_lock); |
| 4015 | wake_up(&conf->wait_for_overlap); |
| 4016 | } |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4017 | return reshape_sectors; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4018 | } |
| 4019 | |
| 4020 | /* FIXME go_faster isn't used */ |
| 4021 | static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster) |
| 4022 | { |
| 4023 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 4024 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4025 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4026 | int sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4027 | int still_degraded = 0; |
| 4028 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4029 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4030 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | /* just being told to finish up .. nothing much to do */ |
| 4032 | unplug_slaves(mddev); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 4033 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4034 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 4035 | end_reshape(conf); |
| 4036 | return 0; |
| 4037 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4038 | |
| 4039 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 4040 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 4041 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4042 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4043 | conf->fullsync = 0; |
| 4044 | bitmap_close_sync(mddev->bitmap); |
| 4045 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4046 | return 0; |
| 4047 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4048 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4049 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4050 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4051 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4052 | /* No need to check resync_max as we never do more than one |
| 4053 | * stripe, and as resync_max will always be on a chunk boundary, |
| 4054 | * if the check in md_do_sync didn't fire, there is no chance |
| 4055 | * of overstepping resync_max here |
| 4056 | */ |
| 4057 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4058 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4059 | * to resync, then assert that we are finished, because there is |
| 4060 | * nothing we can do. |
| 4061 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 4062 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4063 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4064 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 4065 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4066 | return rv; |
| 4067 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4068 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
NeilBrown | 3855ad9 | 2005-11-08 21:39:38 -0800 | [diff] [blame] | 4069 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4070 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
| 4071 | /* we can skip this block, and probably more */ |
| 4072 | sync_blocks /= STRIPE_SECTORS; |
| 4073 | *skipped = 1; |
| 4074 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 4075 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4076 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 4077 | |
| 4078 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 4079 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4080 | sh = get_active_stripe(conf, sector_nr, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4081 | if (sh == NULL) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4082 | sh = get_active_stripe(conf, sector_nr, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4083 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4084 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4085 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 4086 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4087 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4088 | /* Need to check if array will still be degraded after recovery/resync |
| 4089 | * We don't need to check the 'failed' flag as when that gets set, |
| 4090 | * recovery aborts. |
| 4091 | */ |
| 4092 | for (i=0; i<mddev->raid_disks; i++) |
| 4093 | if (conf->disks[i].rdev == NULL) |
| 4094 | still_degraded = 1; |
| 4095 | |
| 4096 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 4097 | |
| 4098 | spin_lock(&sh->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4099 | set_bit(STRIPE_SYNCING, &sh->state); |
| 4100 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 4101 | spin_unlock(&sh->lock); |
| 4102 | |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 4103 | /* wait for any blocked device to be handled */ |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4104 | while (unlikely(!handle_stripe(sh))) |
Dan Williams | df10cfb | 2008-07-28 23:10:39 -0700 | [diff] [blame] | 4105 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4106 | release_stripe(sh); |
| 4107 | |
| 4108 | return STRIPE_SECTORS; |
| 4109 | } |
| 4110 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4111 | static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio) |
| 4112 | { |
| 4113 | /* We may not be able to submit a whole bio at once as there |
| 4114 | * may not be enough stripe_heads available. |
| 4115 | * We cannot pre-allocate enough stripe_heads as we may need |
| 4116 | * more than exist in the cache (if we allow ever large chunks). |
| 4117 | * So we do one stripe head at a time and record in |
| 4118 | * ->bi_hw_segments how many have been done. |
| 4119 | * |
| 4120 | * We *know* that this entire raid_bio is in one chunk, so |
| 4121 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 4122 | */ |
| 4123 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4124 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4125 | sector_t sector, logical_sector, last_sector; |
| 4126 | int scnt = 0; |
| 4127 | int remaining; |
| 4128 | int handled = 0; |
| 4129 | |
| 4130 | logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4131 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4132 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4133 | last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9); |
| 4134 | |
| 4135 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4136 | logical_sector += STRIPE_SECTORS, |
| 4137 | sector += STRIPE_SECTORS, |
| 4138 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4139 | |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4140 | if (scnt < raid5_bi_hw_segments(raid_bio)) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4141 | /* already done this stripe */ |
| 4142 | continue; |
| 4143 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4144 | sh = get_active_stripe(conf, sector, 0, 1); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4145 | |
| 4146 | if (!sh) { |
| 4147 | /* failed to get a stripe - must wait */ |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4148 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4149 | conf->retry_read_aligned = raid_bio; |
| 4150 | return handled; |
| 4151 | } |
| 4152 | |
| 4153 | set_bit(R5_ReadError, &sh->dev[dd_idx].flags); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4154 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { |
| 4155 | release_stripe(sh); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4156 | raid5_set_bi_hw_segments(raid_bio, scnt); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4157 | conf->retry_read_aligned = raid_bio; |
| 4158 | return handled; |
| 4159 | } |
| 4160 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4161 | handle_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4162 | release_stripe(sh); |
| 4163 | handled++; |
| 4164 | } |
| 4165 | spin_lock_irq(&conf->device_lock); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4166 | remaining = raid5_dec_bi_phys_segments(raid_bio); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4167 | spin_unlock_irq(&conf->device_lock); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4168 | if (remaining == 0) |
| 4169 | bio_endio(raid_bio, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4170 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 4171 | wake_up(&conf->wait_for_stripe); |
| 4172 | return handled; |
| 4173 | } |
| 4174 | |
| 4175 | |
| 4176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | /* |
| 4178 | * This is our raid5 kernel thread. |
| 4179 | * |
| 4180 | * We scan the hash table for stripes which can be handled now. |
| 4181 | * During the scan, completed stripes are saved for us by the interrupt |
| 4182 | * handler, so that they will not have to wait for our next wakeup. |
| 4183 | */ |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 4184 | static void raid5d(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4185 | { |
| 4186 | struct stripe_head *sh; |
| 4187 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 4188 | int handled; |
| 4189 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4190 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | |
| 4192 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | |
| 4194 | handled = 0; |
| 4195 | spin_lock_irq(&conf->device_lock); |
| 4196 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4197 | struct bio *bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 4199 | if (conf->seq_flush != conf->seq_write) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4200 | int seq = conf->seq_flush; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4201 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4202 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4203 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4204 | conf->seq_write = seq; |
| 4205 | activate_bit_delay(conf); |
| 4206 | } |
| 4207 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4208 | while ((bio = remove_bio_from_retry(conf))) { |
| 4209 | int ok; |
| 4210 | spin_unlock_irq(&conf->device_lock); |
| 4211 | ok = retry_aligned_read(conf, bio); |
| 4212 | spin_lock_irq(&conf->device_lock); |
| 4213 | if (!ok) |
| 4214 | break; |
| 4215 | handled++; |
| 4216 | } |
| 4217 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4218 | sh = __get_priority_stripe(conf); |
| 4219 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4220 | if (!sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4221 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4222 | spin_unlock_irq(&conf->device_lock); |
| 4223 | |
| 4224 | handled++; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4225 | handle_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4226 | release_stripe(sh); |
| 4227 | |
| 4228 | spin_lock_irq(&conf->device_lock); |
| 4229 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4230 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | |
| 4232 | spin_unlock_irq(&conf->device_lock); |
| 4233 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4234 | async_tx_issue_pending_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4235 | unplug_slaves(mddev); |
| 4236 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4237 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4238 | } |
| 4239 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4240 | static ssize_t |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4241 | raid5_show_stripe_cache_size(mddev_t *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4242 | { |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4243 | raid5_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4244 | if (conf) |
| 4245 | return sprintf(page, "%d\n", conf->max_nr_stripes); |
| 4246 | else |
| 4247 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4248 | } |
| 4249 | |
| 4250 | static ssize_t |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4251 | 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] | 4252 | { |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4253 | raid5_conf_t *conf = mddev_to_conf(mddev); |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4254 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4255 | int err; |
| 4256 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4257 | if (len >= PAGE_SIZE) |
| 4258 | return -EINVAL; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4259 | if (!conf) |
| 4260 | return -ENODEV; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4261 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4262 | if (strict_strtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4263 | return -EINVAL; |
| 4264 | if (new <= 16 || new > 32768) |
| 4265 | return -EINVAL; |
| 4266 | while (new < conf->max_nr_stripes) { |
| 4267 | if (drop_one_stripe(conf)) |
| 4268 | conf->max_nr_stripes--; |
| 4269 | else |
| 4270 | break; |
| 4271 | } |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4272 | err = md_allow_write(mddev); |
| 4273 | if (err) |
| 4274 | return err; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4275 | while (new > conf->max_nr_stripes) { |
| 4276 | if (grow_one_stripe(conf)) |
| 4277 | conf->max_nr_stripes++; |
| 4278 | else break; |
| 4279 | } |
| 4280 | return len; |
| 4281 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4282 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4283 | static struct md_sysfs_entry |
| 4284 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 4285 | raid5_show_stripe_cache_size, |
| 4286 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4287 | |
| 4288 | static ssize_t |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4289 | raid5_show_preread_threshold(mddev_t *mddev, char *page) |
| 4290 | { |
| 4291 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 4292 | if (conf) |
| 4293 | return sprintf(page, "%d\n", conf->bypass_threshold); |
| 4294 | else |
| 4295 | return 0; |
| 4296 | } |
| 4297 | |
| 4298 | static ssize_t |
| 4299 | raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len) |
| 4300 | { |
| 4301 | raid5_conf_t *conf = mddev_to_conf(mddev); |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4302 | unsigned long new; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4303 | if (len >= PAGE_SIZE) |
| 4304 | return -EINVAL; |
| 4305 | if (!conf) |
| 4306 | return -ENODEV; |
| 4307 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4308 | if (strict_strtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4309 | return -EINVAL; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4310 | if (new > conf->max_nr_stripes) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4311 | return -EINVAL; |
| 4312 | conf->bypass_threshold = new; |
| 4313 | return len; |
| 4314 | } |
| 4315 | |
| 4316 | static struct md_sysfs_entry |
| 4317 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 4318 | S_IRUGO | S_IWUSR, |
| 4319 | raid5_show_preread_threshold, |
| 4320 | raid5_store_preread_threshold); |
| 4321 | |
| 4322 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4323 | stripe_cache_active_show(mddev_t *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4324 | { |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4325 | raid5_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4326 | if (conf) |
| 4327 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 4328 | else |
| 4329 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4330 | } |
| 4331 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4332 | static struct md_sysfs_entry |
| 4333 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4334 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4335 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4336 | &raid5_stripecache_size.attr, |
| 4337 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4338 | &raid5_preread_bypass_threshold.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4339 | NULL, |
| 4340 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4341 | static struct attribute_group raid5_attrs_group = { |
| 4342 | .name = NULL, |
| 4343 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4344 | }; |
| 4345 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4346 | static sector_t |
| 4347 | raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks) |
| 4348 | { |
| 4349 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 4350 | |
| 4351 | if (!sectors) |
| 4352 | sectors = mddev->dev_sectors; |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 4353 | if (!raid_disks) { |
| 4354 | /* size is defined by the smallest of previous and new size */ |
| 4355 | if (conf->raid_disks < conf->previous_raid_disks) |
| 4356 | raid_disks = conf->raid_disks; |
| 4357 | else |
| 4358 | raid_disks = conf->previous_raid_disks; |
| 4359 | } |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4360 | |
| 4361 | sectors &= ~((sector_t)mddev->chunk_size/512 - 1); |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4362 | sectors &= ~((sector_t)mddev->new_chunk/512 - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4363 | return sectors * (raid_disks - conf->max_degraded); |
| 4364 | } |
| 4365 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4366 | static void raid5_free_percpu(raid5_conf_t *conf) |
| 4367 | { |
| 4368 | struct raid5_percpu *percpu; |
| 4369 | unsigned long cpu; |
| 4370 | |
| 4371 | if (!conf->percpu) |
| 4372 | return; |
| 4373 | |
| 4374 | get_online_cpus(); |
| 4375 | for_each_possible_cpu(cpu) { |
| 4376 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4377 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4378 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4379 | } |
| 4380 | #ifdef CONFIG_HOTPLUG_CPU |
| 4381 | unregister_cpu_notifier(&conf->cpu_notify); |
| 4382 | #endif |
| 4383 | put_online_cpus(); |
| 4384 | |
| 4385 | free_percpu(conf->percpu); |
| 4386 | } |
| 4387 | |
Dan Williams | a11034b | 2009-07-14 11:48:16 -0700 | [diff] [blame] | 4388 | static void free_conf(raid5_conf_t *conf) |
| 4389 | { |
| 4390 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4391 | raid5_free_percpu(conf); |
Dan Williams | a11034b | 2009-07-14 11:48:16 -0700 | [diff] [blame] | 4392 | kfree(conf->disks); |
| 4393 | kfree(conf->stripe_hashtbl); |
| 4394 | kfree(conf); |
| 4395 | } |
| 4396 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4397 | #ifdef CONFIG_HOTPLUG_CPU |
| 4398 | static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action, |
| 4399 | void *hcpu) |
| 4400 | { |
| 4401 | raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify); |
| 4402 | long cpu = (long)hcpu; |
| 4403 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4404 | |
| 4405 | switch (action) { |
| 4406 | case CPU_UP_PREPARE: |
| 4407 | case CPU_UP_PREPARE_FROZEN: |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4408 | if (conf->level == 6 && !percpu->spare_page) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4409 | percpu->spare_page = alloc_page(GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4410 | if (!percpu->scribble) |
| 4411 | percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
| 4412 | |
| 4413 | if (!percpu->scribble || |
| 4414 | (conf->level == 6 && !percpu->spare_page)) { |
| 4415 | safe_put_page(percpu->spare_page); |
| 4416 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4417 | pr_err("%s: failed memory allocation for cpu%ld\n", |
| 4418 | __func__, cpu); |
| 4419 | return NOTIFY_BAD; |
| 4420 | } |
| 4421 | break; |
| 4422 | case CPU_DEAD: |
| 4423 | case CPU_DEAD_FROZEN: |
| 4424 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4425 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4426 | percpu->spare_page = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4427 | percpu->scribble = NULL; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4428 | break; |
| 4429 | default: |
| 4430 | break; |
| 4431 | } |
| 4432 | return NOTIFY_OK; |
| 4433 | } |
| 4434 | #endif |
| 4435 | |
| 4436 | static int raid5_alloc_percpu(raid5_conf_t *conf) |
| 4437 | { |
| 4438 | unsigned long cpu; |
| 4439 | struct page *spare_page; |
| 4440 | struct raid5_percpu *allcpus; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4441 | void *scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4442 | int err; |
| 4443 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4444 | allcpus = alloc_percpu(struct raid5_percpu); |
| 4445 | if (!allcpus) |
| 4446 | return -ENOMEM; |
| 4447 | conf->percpu = allcpus; |
| 4448 | |
| 4449 | get_online_cpus(); |
| 4450 | err = 0; |
| 4451 | for_each_present_cpu(cpu) { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4452 | if (conf->level == 6) { |
| 4453 | spare_page = alloc_page(GFP_KERNEL); |
| 4454 | if (!spare_page) { |
| 4455 | err = -ENOMEM; |
| 4456 | break; |
| 4457 | } |
| 4458 | per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page; |
| 4459 | } |
| 4460 | scribble = kmalloc(scribble_len(conf->raid_disks), GFP_KERNEL); |
| 4461 | if (!scribble) { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4462 | err = -ENOMEM; |
| 4463 | break; |
| 4464 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4465 | per_cpu_ptr(conf->percpu, cpu)->scribble = scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4466 | } |
| 4467 | #ifdef CONFIG_HOTPLUG_CPU |
| 4468 | conf->cpu_notify.notifier_call = raid456_cpu_notify; |
| 4469 | conf->cpu_notify.priority = 0; |
| 4470 | if (err == 0) |
| 4471 | err = register_cpu_notifier(&conf->cpu_notify); |
| 4472 | #endif |
| 4473 | put_online_cpus(); |
| 4474 | |
| 4475 | return err; |
| 4476 | } |
| 4477 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4478 | static raid5_conf_t *setup_conf(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4479 | { |
| 4480 | raid5_conf_t *conf; |
| 4481 | int raid_disk, memory; |
| 4482 | mdk_rdev_t *rdev; |
| 4483 | struct disk_info *disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4485 | if (mddev->new_level != 5 |
| 4486 | && mddev->new_level != 4 |
| 4487 | && mddev->new_level != 6) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4488 | 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] | 4489 | mdname(mddev), mddev->new_level); |
| 4490 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4492 | if ((mddev->new_level == 5 |
| 4493 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 4494 | (mddev->new_level == 6 |
| 4495 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4496 | printk(KERN_ERR "raid5: %s: layout %d not supported\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4497 | mdname(mddev), mddev->new_layout); |
| 4498 | return ERR_PTR(-EIO); |
| 4499 | } |
| 4500 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
| 4501 | printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n", |
| 4502 | mdname(mddev), mddev->raid_disks); |
| 4503 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4504 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4506 | if (!mddev->new_chunk || mddev->new_chunk % PAGE_SIZE) { |
| 4507 | printk(KERN_ERR "raid5: invalid chunk size %d for %s\n", |
| 4508 | mddev->new_chunk, mdname(mddev)); |
| 4509 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4510 | } |
| 4511 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4512 | conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL); |
| 4513 | if (conf == NULL) |
| 4514 | goto abort; |
| 4515 | |
| 4516 | conf->raid_disks = mddev->raid_disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4517 | conf->scribble_len = scribble_len(conf->raid_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4518 | if (mddev->reshape_position == MaxSector) |
| 4519 | conf->previous_raid_disks = mddev->raid_disks; |
| 4520 | else |
| 4521 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
| 4522 | |
| 4523 | conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info), |
| 4524 | GFP_KERNEL); |
| 4525 | if (!conf->disks) |
| 4526 | goto abort; |
| 4527 | |
| 4528 | conf->mddev = mddev; |
| 4529 | |
| 4530 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 4531 | goto abort; |
| 4532 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4533 | conf->level = mddev->new_level; |
| 4534 | if (raid5_alloc_percpu(conf) != 0) |
| 4535 | goto abort; |
| 4536 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4537 | spin_lock_init(&conf->device_lock); |
| 4538 | init_waitqueue_head(&conf->wait_for_stripe); |
| 4539 | init_waitqueue_head(&conf->wait_for_overlap); |
| 4540 | INIT_LIST_HEAD(&conf->handle_list); |
| 4541 | INIT_LIST_HEAD(&conf->hold_list); |
| 4542 | INIT_LIST_HEAD(&conf->delayed_list); |
| 4543 | INIT_LIST_HEAD(&conf->bitmap_list); |
| 4544 | INIT_LIST_HEAD(&conf->inactive_list); |
| 4545 | atomic_set(&conf->active_stripes, 0); |
| 4546 | atomic_set(&conf->preread_active_stripes, 0); |
| 4547 | atomic_set(&conf->active_aligned_reads, 0); |
| 4548 | conf->bypass_threshold = BYPASS_THRESHOLD; |
| 4549 | |
| 4550 | pr_debug("raid5: run(%s) called.\n", mdname(mddev)); |
| 4551 | |
| 4552 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 4553 | raid_disk = rdev->raid_disk; |
| 4554 | if (raid_disk >= conf->raid_disks |
| 4555 | || raid_disk < 0) |
| 4556 | continue; |
| 4557 | disk = conf->disks + raid_disk; |
| 4558 | |
| 4559 | disk->rdev = rdev; |
| 4560 | |
| 4561 | if (test_bit(In_sync, &rdev->flags)) { |
| 4562 | char b[BDEVNAME_SIZE]; |
| 4563 | printk(KERN_INFO "raid5: device %s operational as raid" |
| 4564 | " disk %d\n", bdevname(rdev->bdev,b), |
| 4565 | raid_disk); |
| 4566 | } else |
| 4567 | /* Cannot rely on bitmap to complete recovery */ |
| 4568 | conf->fullsync = 1; |
| 4569 | } |
| 4570 | |
| 4571 | conf->chunk_size = mddev->new_chunk; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4572 | if (conf->level == 6) |
| 4573 | conf->max_degraded = 2; |
| 4574 | else |
| 4575 | conf->max_degraded = 1; |
| 4576 | conf->algorithm = mddev->new_layout; |
| 4577 | conf->max_nr_stripes = NR_STRIPES; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4578 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4579 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4580 | conf->prev_chunk = mddev->chunk_size; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4581 | conf->prev_algo = mddev->layout; |
| 4582 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4583 | |
| 4584 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
| 4585 | conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
| 4586 | if (grow_stripes(conf, conf->max_nr_stripes)) { |
| 4587 | printk(KERN_ERR |
| 4588 | "raid5: couldn't allocate %dkB for buffers\n", memory); |
| 4589 | goto abort; |
| 4590 | } else |
| 4591 | printk(KERN_INFO "raid5: allocated %dkB for %s\n", |
| 4592 | memory, mdname(mddev)); |
| 4593 | |
| 4594 | conf->thread = md_register_thread(raid5d, mddev, "%s_raid5"); |
| 4595 | if (!conf->thread) { |
| 4596 | printk(KERN_ERR |
| 4597 | "raid5: couldn't allocate thread for %s\n", |
| 4598 | mdname(mddev)); |
| 4599 | goto abort; |
| 4600 | } |
| 4601 | |
| 4602 | return conf; |
| 4603 | |
| 4604 | abort: |
| 4605 | if (conf) { |
Dan Williams | a11034b | 2009-07-14 11:48:16 -0700 | [diff] [blame] | 4606 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4607 | return ERR_PTR(-EIO); |
| 4608 | } else |
| 4609 | return ERR_PTR(-ENOMEM); |
| 4610 | } |
| 4611 | |
| 4612 | static int run(mddev_t *mddev) |
| 4613 | { |
| 4614 | raid5_conf_t *conf; |
| 4615 | int working_disks = 0; |
| 4616 | mdk_rdev_t *rdev; |
| 4617 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4618 | if (mddev->reshape_position != MaxSector) { |
| 4619 | /* Check that we can continue the reshape. |
| 4620 | * Currently only disks can change, it must |
| 4621 | * increase, and we must be past the point where |
| 4622 | * a stripe over-writes itself |
| 4623 | */ |
| 4624 | sector_t here_new, here_old; |
| 4625 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 4626 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4627 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 4628 | if (mddev->new_level != mddev->level) { |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4629 | printk(KERN_ERR "raid5: %s: unsupported reshape " |
| 4630 | "required - aborting.\n", |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4631 | mdname(mddev)); |
| 4632 | return -EINVAL; |
| 4633 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4634 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 4635 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4636 | * further up in new geometry must map after here in old |
| 4637 | * geometry. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4638 | */ |
| 4639 | here_new = mddev->reshape_position; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4640 | if (sector_div(here_new, (mddev->new_chunk>>9)* |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4641 | (mddev->raid_disks - max_degraded))) { |
| 4642 | printk(KERN_ERR "raid5: reshape_position not " |
| 4643 | "on a stripe boundary\n"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4644 | return -EINVAL; |
| 4645 | } |
| 4646 | /* here_new is the stripe we will write to */ |
| 4647 | here_old = mddev->reshape_position; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4648 | sector_div(here_old, (mddev->chunk_size>>9)* |
| 4649 | (old_disks-max_degraded)); |
| 4650 | /* here_old is the first stripe that we might need to read |
| 4651 | * from */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4652 | if (here_new >= here_old) { |
| 4653 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4654 | printk(KERN_ERR "raid5: reshape_position too early for " |
| 4655 | "auto-recovery - aborting.\n"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4656 | return -EINVAL; |
| 4657 | } |
| 4658 | printk(KERN_INFO "raid5: reshape will continue\n"); |
| 4659 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4660 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4661 | BUG_ON(mddev->level != mddev->new_level); |
| 4662 | BUG_ON(mddev->layout != mddev->new_layout); |
| 4663 | BUG_ON(mddev->chunk_size != mddev->new_chunk); |
| 4664 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4665 | } |
| 4666 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4667 | if (mddev->private == NULL) |
| 4668 | conf = setup_conf(mddev); |
| 4669 | else |
| 4670 | conf = mddev->private; |
| 4671 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4672 | if (IS_ERR(conf)) |
| 4673 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 4674 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4675 | mddev->thread = conf->thread; |
| 4676 | conf->thread = NULL; |
| 4677 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4680 | * 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] | 4681 | */ |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4682 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4683 | if (rdev->raid_disk >= 0 && |
| 4684 | test_bit(In_sync, &rdev->flags)) |
| 4685 | working_disks++; |
| 4686 | |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 4687 | mddev->degraded = conf->raid_disks - working_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4688 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4689 | if (mddev->degraded > conf->max_degraded) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4690 | printk(KERN_ERR "raid5: not enough operational devices for %s" |
| 4691 | " (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 4692 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4693 | goto abort; |
| 4694 | } |
| 4695 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4696 | /* device size must be a multiple of chunk size */ |
| 4697 | mddev->dev_sectors &= ~(mddev->chunk_size / 512 - 1); |
| 4698 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4699 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4700 | if (mddev->degraded > 0 && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4701 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4702 | if (mddev->ok_start_degraded) |
| 4703 | printk(KERN_WARNING |
| 4704 | "raid5: starting dirty degraded array: %s" |
| 4705 | "- data corruption possible.\n", |
| 4706 | mdname(mddev)); |
| 4707 | else { |
| 4708 | printk(KERN_ERR |
| 4709 | "raid5: cannot start dirty degraded array for %s\n", |
| 4710 | mdname(mddev)); |
| 4711 | goto abort; |
| 4712 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4713 | } |
| 4714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4715 | if (mddev->degraded == 0) |
| 4716 | printk("raid5: raid level %d set %s active with %d out of %d" |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4717 | " devices, algorithm %d\n", conf->level, mdname(mddev), |
| 4718 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 4719 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4720 | else |
| 4721 | printk(KERN_ALERT "raid5: raid level %d set %s active with %d" |
| 4722 | " out of %d devices, algorithm %d\n", conf->level, |
| 4723 | mdname(mddev), mddev->raid_disks - mddev->degraded, |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 4724 | mddev->raid_disks, mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4725 | |
| 4726 | print_raid5_conf(conf); |
| 4727 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4728 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4729 | printk("...ok start reshape thread\n"); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4730 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4731 | atomic_set(&conf->reshape_stripes, 0); |
| 4732 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 4733 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 4734 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 4735 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 4736 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
| 4737 | "%s_reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4738 | } |
| 4739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4740 | /* read-ahead size must cover two whole stripes, which is |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4741 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4742 | */ |
| 4743 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4744 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 4745 | int stripe = data_disks * |
NeilBrown | 8932c2e | 2006-06-26 00:27:36 -0700 | [diff] [blame] | 4746 | (mddev->chunk_size / PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4747 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 4748 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 4749 | } |
| 4750 | |
| 4751 | /* Ok, everything is just fine now */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4752 | if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
| 4753 | printk(KERN_WARNING |
| 4754 | "raid5: failed to create sysfs attributes for %s\n", |
| 4755 | mdname(mddev)); |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 4756 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4757 | mddev->queue->queue_lock = &conf->device_lock; |
| 4758 | |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 4759 | mddev->queue->unplug_fn = raid5_unplug_device; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4760 | mddev->queue->backing_dev_info.congested_data = mddev; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4761 | mddev->queue->backing_dev_info.congested_fn = raid5_congested; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4762 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 4763 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 4764 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 4765 | blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); |
| 4766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4767 | return 0; |
| 4768 | abort: |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4769 | md_unregister_thread(mddev->thread); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4770 | mddev->thread = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | if (conf) { |
| 4772 | print_raid5_conf(conf); |
Dan Williams | a11034b | 2009-07-14 11:48:16 -0700 | [diff] [blame] | 4773 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4774 | } |
| 4775 | mddev->private = NULL; |
| 4776 | printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev)); |
| 4777 | return -EIO; |
| 4778 | } |
| 4779 | |
| 4780 | |
| 4781 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4782 | static int stop(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4783 | { |
| 4784 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 4785 | |
| 4786 | md_unregister_thread(mddev->thread); |
| 4787 | mddev->thread = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4788 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4789 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4790 | sysfs_remove_group(&mddev->kobj, &raid5_attrs_group); |
Dan Williams | a11034b | 2009-07-14 11:48:16 -0700 | [diff] [blame] | 4791 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4792 | mddev->private = NULL; |
| 4793 | return 0; |
| 4794 | } |
| 4795 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4796 | #ifdef DEBUG |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4797 | static void print_sh(struct seq_file *seq, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4798 | { |
| 4799 | int i; |
| 4800 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4801 | seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n", |
| 4802 | (unsigned long long)sh->sector, sh->pd_idx, sh->state); |
| 4803 | seq_printf(seq, "sh %llu, count %d.\n", |
| 4804 | (unsigned long long)sh->sector, atomic_read(&sh->count)); |
| 4805 | seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4806 | for (i = 0; i < sh->disks; i++) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4807 | seq_printf(seq, "(cache%d: %p %ld) ", |
| 4808 | i, sh->dev[i].page, sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4809 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4810 | seq_printf(seq, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4811 | } |
| 4812 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4813 | static void printall(struct seq_file *seq, raid5_conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4814 | { |
| 4815 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 4816 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4817 | int i; |
| 4818 | |
| 4819 | spin_lock_irq(&conf->device_lock); |
| 4820 | for (i = 0; i < NR_HASH; i++) { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 4821 | hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | if (sh->raid_conf != conf) |
| 4823 | continue; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4824 | print_sh(seq, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | } |
| 4826 | } |
| 4827 | spin_unlock_irq(&conf->device_lock); |
| 4828 | } |
| 4829 | #endif |
| 4830 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4831 | static void status(struct seq_file *seq, mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4832 | { |
| 4833 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 4834 | int i; |
| 4835 | |
| 4836 | 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] | 4837 | 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] | 4838 | for (i = 0; i < conf->raid_disks; i++) |
| 4839 | seq_printf (seq, "%s", |
| 4840 | conf->disks[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4841 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4842 | seq_printf (seq, "]"); |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4843 | #ifdef DEBUG |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4844 | seq_printf (seq, "\n"); |
| 4845 | printall(seq, conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4846 | #endif |
| 4847 | } |
| 4848 | |
| 4849 | static void print_raid5_conf (raid5_conf_t *conf) |
| 4850 | { |
| 4851 | int i; |
| 4852 | struct disk_info *tmp; |
| 4853 | |
| 4854 | printk("RAID5 conf printout:\n"); |
| 4855 | if (!conf) { |
| 4856 | printk("(conf==NULL)\n"); |
| 4857 | return; |
| 4858 | } |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 4859 | printk(" --- rd:%d wd:%d\n", conf->raid_disks, |
| 4860 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | |
| 4862 | for (i = 0; i < conf->raid_disks; i++) { |
| 4863 | char b[BDEVNAME_SIZE]; |
| 4864 | tmp = conf->disks + i; |
| 4865 | if (tmp->rdev) |
| 4866 | printk(" disk %d, o:%d, dev:%s\n", |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4867 | i, !test_bit(Faulty, &tmp->rdev->flags), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | bdevname(tmp->rdev->bdev,b)); |
| 4869 | } |
| 4870 | } |
| 4871 | |
| 4872 | static int raid5_spare_active(mddev_t *mddev) |
| 4873 | { |
| 4874 | int i; |
| 4875 | raid5_conf_t *conf = mddev->private; |
| 4876 | struct disk_info *tmp; |
| 4877 | |
| 4878 | for (i = 0; i < conf->raid_disks; i++) { |
| 4879 | tmp = conf->disks + i; |
| 4880 | if (tmp->rdev |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4881 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 4882 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
| 4883 | unsigned long flags; |
| 4884 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4885 | mddev->degraded--; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 4886 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4887 | } |
| 4888 | } |
| 4889 | print_raid5_conf(conf); |
| 4890 | return 0; |
| 4891 | } |
| 4892 | |
| 4893 | static int raid5_remove_disk(mddev_t *mddev, int number) |
| 4894 | { |
| 4895 | raid5_conf_t *conf = mddev->private; |
| 4896 | int err = 0; |
| 4897 | mdk_rdev_t *rdev; |
| 4898 | struct disk_info *p = conf->disks + number; |
| 4899 | |
| 4900 | print_raid5_conf(conf); |
| 4901 | rdev = p->rdev; |
| 4902 | if (rdev) { |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4903 | if (number >= conf->raid_disks && |
| 4904 | conf->reshape_progress == MaxSector) |
| 4905 | clear_bit(In_sync, &rdev->flags); |
| 4906 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4907 | if (test_bit(In_sync, &rdev->flags) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4908 | atomic_read(&rdev->nr_pending)) { |
| 4909 | err = -EBUSY; |
| 4910 | goto abort; |
| 4911 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 4912 | /* Only remove non-faulty devices if recovery |
| 4913 | * isn't possible. |
| 4914 | */ |
| 4915 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4916 | mddev->degraded <= conf->max_degraded && |
| 4917 | number < conf->raid_disks) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 4918 | err = -EBUSY; |
| 4919 | goto abort; |
| 4920 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | p->rdev = NULL; |
Paul E. McKenney | fbd568a3e | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 4922 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | if (atomic_read(&rdev->nr_pending)) { |
| 4924 | /* lost the race, try later */ |
| 4925 | err = -EBUSY; |
| 4926 | p->rdev = rdev; |
| 4927 | } |
| 4928 | } |
| 4929 | abort: |
| 4930 | |
| 4931 | print_raid5_conf(conf); |
| 4932 | return err; |
| 4933 | } |
| 4934 | |
| 4935 | static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) |
| 4936 | { |
| 4937 | raid5_conf_t *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 4938 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4939 | int disk; |
| 4940 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 4941 | int first = 0; |
| 4942 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4943 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4944 | if (mddev->degraded > conf->max_degraded) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4945 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 4946 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 4948 | if (rdev->raid_disk >= 0) |
| 4949 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4950 | |
| 4951 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4952 | * find the disk ... but prefer rdev->saved_raid_disk |
| 4953 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4954 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4955 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 4956 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4957 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
| 4958 | disk = rdev->saved_raid_disk; |
| 4959 | else |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 4960 | disk = first; |
| 4961 | for ( ; disk <= last ; disk++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4962 | if ((p=conf->disks + disk)->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4963 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4964 | rdev->raid_disk = disk; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 4965 | err = 0; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4966 | if (rdev->saved_raid_disk != disk) |
| 4967 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 4968 | rcu_assign_pointer(p->rdev, rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | break; |
| 4970 | } |
| 4971 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 4972 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | } |
| 4974 | |
| 4975 | static int raid5_resize(mddev_t *mddev, sector_t sectors) |
| 4976 | { |
| 4977 | /* no resync is happening, and there is enough space |
| 4978 | * on all devices, so we can resize. |
| 4979 | * We need to make sure resync covers any new space. |
| 4980 | * If the array is shrinking we should possibly wait until |
| 4981 | * any io in the removed space completes, but it hardly seems |
| 4982 | * worth it. |
| 4983 | */ |
| 4984 | sectors &= ~((sector_t)mddev->chunk_size/512 - 1); |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 4985 | md_set_array_sectors(mddev, raid5_size(mddev, sectors, |
| 4986 | mddev->raid_disks)); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4987 | if (mddev->array_sectors > |
| 4988 | raid5_size(mddev, sectors, mddev->raid_disks)) |
| 4989 | return -EINVAL; |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4990 | set_capacity(mddev->gendisk, mddev->array_sectors); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4991 | mddev->changed = 1; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4992 | if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) { |
| 4993 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4994 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4995 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4996 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 4997 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4998 | return 0; |
| 4999 | } |
| 5000 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5001 | static int raid5_check_reshape(mddev_t *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5002 | { |
| 5003 | raid5_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5004 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5005 | if (mddev->delta_disks == 0 && |
| 5006 | mddev->new_layout == mddev->layout && |
| 5007 | mddev->new_chunk == mddev->chunk_size) |
| 5008 | return -EINVAL; /* nothing to do */ |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5009 | if (mddev->bitmap) |
| 5010 | /* Cannot grow a bitmap yet */ |
| 5011 | return -EBUSY; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5012 | if (mddev->degraded > conf->max_degraded) |
| 5013 | return -EINVAL; |
| 5014 | if (mddev->delta_disks < 0) { |
| 5015 | /* We might be able to shrink, but the devices must |
| 5016 | * be made bigger first. |
| 5017 | * For raid6, 4 is the minimum size. |
| 5018 | * Otherwise 2 is the minimum |
| 5019 | */ |
| 5020 | int min = 2; |
| 5021 | if (mddev->level == 6) |
| 5022 | min = 4; |
| 5023 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 5024 | return -EINVAL; |
| 5025 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5026 | |
| 5027 | /* Can only proceed if there are plenty of stripe_heads. |
| 5028 | * We need a minimum of one full stripe,, and for sensible progress |
| 5029 | * it is best to have about 4 times that. |
| 5030 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 5031 | * allow for chunk sizes up to 256K, which is probably OK. |
| 5032 | * If the chunk size is greater, user-space should request more |
| 5033 | * stripe_heads first. |
| 5034 | */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5035 | if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes || |
| 5036 | (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5037 | printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n", |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 5038 | (max(mddev->chunk_size, mddev->new_chunk) |
| 5039 | / STRIPE_SIZE)*4); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5040 | return -ENOSPC; |
| 5041 | } |
| 5042 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5043 | return resize_stripes(conf, conf->raid_disks + mddev->delta_disks); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5044 | } |
| 5045 | |
| 5046 | static int raid5_start_reshape(mddev_t *mddev) |
| 5047 | { |
| 5048 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 5049 | mdk_rdev_t *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5050 | int spares = 0; |
| 5051 | int added_devices = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5052 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5053 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5054 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5055 | return -EBUSY; |
| 5056 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5057 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5058 | if (rdev->raid_disk < 0 && |
| 5059 | !test_bit(Faulty, &rdev->flags)) |
| 5060 | spares++; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5061 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5062 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5063 | /* Not enough devices even to make a degraded array |
| 5064 | * of that size |
| 5065 | */ |
| 5066 | return -EINVAL; |
| 5067 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5068 | /* Refuse to reduce size of the array. Any reductions in |
| 5069 | * array size must be through explicit setting of array_size |
| 5070 | * attribute. |
| 5071 | */ |
| 5072 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 5073 | < mddev->array_sectors) { |
| 5074 | printk(KERN_ERR "md: %s: array size must be reduced " |
| 5075 | "before number of disks\n", mdname(mddev)); |
| 5076 | return -EINVAL; |
| 5077 | } |
| 5078 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5079 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5080 | spin_lock_irq(&conf->device_lock); |
| 5081 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5082 | conf->raid_disks += mddev->delta_disks; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5083 | conf->prev_chunk = conf->chunk_size; |
| 5084 | conf->chunk_size = mddev->new_chunk; |
| 5085 | conf->prev_algo = conf->algorithm; |
| 5086 | conf->algorithm = mddev->new_layout; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5087 | if (mddev->delta_disks < 0) |
| 5088 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 5089 | else |
| 5090 | conf->reshape_progress = 0; |
| 5091 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 5092 | conf->generation++; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5093 | spin_unlock_irq(&conf->device_lock); |
| 5094 | |
| 5095 | /* Add some new drives, as many as will fit. |
| 5096 | * We know there are enough to make the newly sized array work. |
| 5097 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5098 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5099 | if (rdev->raid_disk < 0 && |
| 5100 | !test_bit(Faulty, &rdev->flags)) { |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5101 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5102 | char nm[20]; |
| 5103 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5104 | added_devices++; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5105 | rdev->recovery_offset = 0; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5106 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5107 | if (sysfs_create_link(&mddev->kobj, |
| 5108 | &rdev->kobj, nm)) |
| 5109 | printk(KERN_WARNING |
| 5110 | "raid5: failed to create " |
| 5111 | " link %s for %s\n", |
| 5112 | nm, mdname(mddev)); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5113 | } else |
| 5114 | break; |
| 5115 | } |
| 5116 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5117 | if (mddev->delta_disks > 0) { |
| 5118 | spin_lock_irqsave(&conf->device_lock, flags); |
| 5119 | mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) |
| 5120 | - added_devices; |
| 5121 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 5122 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5123 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5124 | mddev->reshape_position = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5125 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5126 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5127 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5128 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5129 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5130 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5131 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
| 5132 | "%s_reshape"); |
| 5133 | if (!mddev->sync_thread) { |
| 5134 | mddev->recovery = 0; |
| 5135 | spin_lock_irq(&conf->device_lock); |
| 5136 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5137 | conf->reshape_progress = MaxSector; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5138 | spin_unlock_irq(&conf->device_lock); |
| 5139 | return -EAGAIN; |
| 5140 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5141 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5142 | md_wakeup_thread(mddev->sync_thread); |
| 5143 | md_new_event(mddev); |
| 5144 | return 0; |
| 5145 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5146 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5147 | /* This is called from the reshape thread and should make any |
| 5148 | * changes needed in 'conf' |
| 5149 | */ |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5150 | static void end_reshape(raid5_conf_t *conf) |
| 5151 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5152 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5153 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5154 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5155 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5156 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5157 | conf->reshape_progress = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5158 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5159 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5160 | |
| 5161 | /* read-ahead size must cover two whole stripes, which is |
| 5162 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 5163 | */ |
| 5164 | { |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5165 | int data_disks = conf->raid_disks - conf->max_degraded; |
| 5166 | int stripe = data_disks * (conf->chunk_size |
| 5167 | / PAGE_SIZE); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5168 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5169 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 5170 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5171 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5172 | } |
| 5173 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5174 | /* This is called from the raid5d thread with mddev_lock held. |
| 5175 | * It makes config changes to the device. |
| 5176 | */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5177 | static void raid5_finish_reshape(mddev_t *mddev) |
| 5178 | { |
| 5179 | struct block_device *bdev; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5180 | raid5_conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5181 | |
| 5182 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5183 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5184 | if (mddev->delta_disks > 0) { |
| 5185 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 5186 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5187 | mddev->changed = 1; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5188 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5189 | bdev = bdget_disk(mddev->gendisk, 0); |
| 5190 | if (bdev) { |
| 5191 | mutex_lock(&bdev->bd_inode->i_mutex); |
| 5192 | i_size_write(bdev->bd_inode, |
| 5193 | (loff_t)mddev->array_sectors << 9); |
| 5194 | mutex_unlock(&bdev->bd_inode->i_mutex); |
| 5195 | bdput(bdev); |
| 5196 | } |
| 5197 | } else { |
| 5198 | int d; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5199 | mddev->degraded = conf->raid_disks; |
| 5200 | for (d = 0; d < conf->raid_disks ; d++) |
| 5201 | if (conf->disks[d].rdev && |
| 5202 | test_bit(In_sync, |
| 5203 | &conf->disks[d].rdev->flags)) |
| 5204 | mddev->degraded--; |
| 5205 | for (d = conf->raid_disks ; |
| 5206 | d < conf->raid_disks - mddev->delta_disks; |
| 5207 | d++) |
| 5208 | raid5_remove_disk(mddev, d); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5209 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5210 | mddev->layout = conf->algorithm; |
| 5211 | mddev->chunk_size = conf->chunk_size; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5212 | mddev->reshape_position = MaxSector; |
| 5213 | mddev->delta_disks = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5214 | } |
| 5215 | } |
| 5216 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5217 | static void raid5_quiesce(mddev_t *mddev, int state) |
| 5218 | { |
| 5219 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 5220 | |
| 5221 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5222 | case 2: /* resume for a suspend */ |
| 5223 | wake_up(&conf->wait_for_overlap); |
| 5224 | break; |
| 5225 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5226 | case 1: /* stop all writes */ |
| 5227 | spin_lock_irq(&conf->device_lock); |
| 5228 | conf->quiesce = 1; |
| 5229 | wait_event_lock_irq(conf->wait_for_stripe, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5230 | atomic_read(&conf->active_stripes) == 0 && |
| 5231 | atomic_read(&conf->active_aligned_reads) == 0, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5232 | conf->device_lock, /* nothing */); |
| 5233 | spin_unlock_irq(&conf->device_lock); |
| 5234 | break; |
| 5235 | |
| 5236 | case 0: /* re-enable writes */ |
| 5237 | spin_lock_irq(&conf->device_lock); |
| 5238 | conf->quiesce = 0; |
| 5239 | wake_up(&conf->wait_for_stripe); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5240 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5241 | spin_unlock_irq(&conf->device_lock); |
| 5242 | break; |
| 5243 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5244 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5245 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5246 | |
| 5247 | static void *raid5_takeover_raid1(mddev_t *mddev) |
| 5248 | { |
| 5249 | int chunksect; |
| 5250 | |
| 5251 | if (mddev->raid_disks != 2 || |
| 5252 | mddev->degraded > 1) |
| 5253 | return ERR_PTR(-EINVAL); |
| 5254 | |
| 5255 | /* Should check if there are write-behind devices? */ |
| 5256 | |
| 5257 | chunksect = 64*2; /* 64K by default */ |
| 5258 | |
| 5259 | /* The array must be an exact multiple of chunksize */ |
| 5260 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 5261 | chunksect >>= 1; |
| 5262 | |
| 5263 | if ((chunksect<<9) < STRIPE_SIZE) |
| 5264 | /* array size does not allow a suitable chunk size */ |
| 5265 | return ERR_PTR(-EINVAL); |
| 5266 | |
| 5267 | mddev->new_level = 5; |
| 5268 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 5269 | mddev->new_chunk = chunksect << 9; |
| 5270 | |
| 5271 | return setup_conf(mddev); |
| 5272 | } |
| 5273 | |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 5274 | static void *raid5_takeover_raid6(mddev_t *mddev) |
| 5275 | { |
| 5276 | int new_layout; |
| 5277 | |
| 5278 | switch (mddev->layout) { |
| 5279 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 5280 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 5281 | break; |
| 5282 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 5283 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 5284 | break; |
| 5285 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 5286 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 5287 | break; |
| 5288 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 5289 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 5290 | break; |
| 5291 | case ALGORITHM_PARITY_0_6: |
| 5292 | new_layout = ALGORITHM_PARITY_0; |
| 5293 | break; |
| 5294 | case ALGORITHM_PARITY_N: |
| 5295 | new_layout = ALGORITHM_PARITY_N; |
| 5296 | break; |
| 5297 | default: |
| 5298 | return ERR_PTR(-EINVAL); |
| 5299 | } |
| 5300 | mddev->new_level = 5; |
| 5301 | mddev->new_layout = new_layout; |
| 5302 | mddev->delta_disks = -1; |
| 5303 | mddev->raid_disks -= 1; |
| 5304 | return setup_conf(mddev); |
| 5305 | } |
| 5306 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5307 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5308 | static int raid5_reconfig(mddev_t *mddev, int new_layout, int new_chunk) |
| 5309 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5310 | /* For a 2-drive array, the layout and chunk size can be changed |
| 5311 | * immediately as not restriping is needed. |
| 5312 | * For larger arrays we record the new value - after validation |
| 5313 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5314 | */ |
| 5315 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 5316 | |
| 5317 | if (new_layout >= 0 && !algorithm_valid_raid5(new_layout)) |
| 5318 | return -EINVAL; |
| 5319 | if (new_chunk > 0) { |
| 5320 | if (new_chunk & (new_chunk-1)) |
| 5321 | /* not a power of 2 */ |
| 5322 | return -EINVAL; |
| 5323 | if (new_chunk < PAGE_SIZE) |
| 5324 | return -EINVAL; |
| 5325 | if (mddev->array_sectors & ((new_chunk>>9)-1)) |
| 5326 | /* not factor of array size */ |
| 5327 | return -EINVAL; |
| 5328 | } |
| 5329 | |
| 5330 | /* They look valid */ |
| 5331 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5332 | if (mddev->raid_disks == 2) { |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5333 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5334 | if (new_layout >= 0) { |
| 5335 | conf->algorithm = new_layout; |
| 5336 | mddev->layout = mddev->new_layout = new_layout; |
| 5337 | } |
| 5338 | if (new_chunk > 0) { |
| 5339 | conf->chunk_size = new_chunk; |
| 5340 | mddev->chunk_size = mddev->new_chunk = new_chunk; |
| 5341 | } |
| 5342 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 5343 | md_wakeup_thread(mddev->thread); |
| 5344 | } else { |
| 5345 | if (new_layout >= 0) |
| 5346 | mddev->new_layout = new_layout; |
| 5347 | if (new_chunk > 0) |
| 5348 | mddev->new_chunk = new_chunk; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5349 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5350 | return 0; |
| 5351 | } |
| 5352 | |
| 5353 | static int raid6_reconfig(mddev_t *mddev, int new_layout, int new_chunk) |
| 5354 | { |
| 5355 | if (new_layout >= 0 && !algorithm_valid_raid6(new_layout)) |
| 5356 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5357 | if (new_chunk > 0) { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5358 | if (new_chunk & (new_chunk-1)) |
| 5359 | /* not a power of 2 */ |
| 5360 | return -EINVAL; |
| 5361 | if (new_chunk < PAGE_SIZE) |
| 5362 | return -EINVAL; |
| 5363 | if (mddev->array_sectors & ((new_chunk>>9)-1)) |
| 5364 | /* not factor of array size */ |
| 5365 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5366 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5367 | |
| 5368 | /* They look valid */ |
| 5369 | |
| 5370 | if (new_layout >= 0) |
| 5371 | mddev->new_layout = new_layout; |
| 5372 | if (new_chunk > 0) |
| 5373 | mddev->new_chunk = new_chunk; |
| 5374 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5375 | return 0; |
| 5376 | } |
| 5377 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5378 | static void *raid5_takeover(mddev_t *mddev) |
| 5379 | { |
| 5380 | /* raid5 can take over: |
| 5381 | * raid0 - if all devices are the same - make it a raid4 layout |
| 5382 | * raid1 - if there are two drives. We need to know the chunk size |
| 5383 | * raid4 - trivial - just use a raid4 layout. |
| 5384 | * raid6 - Providing it is a *_6 layout |
| 5385 | * |
| 5386 | * For now, just do raid1 |
| 5387 | */ |
| 5388 | |
| 5389 | if (mddev->level == 1) |
| 5390 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 5391 | if (mddev->level == 4) { |
| 5392 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 5393 | mddev->new_level = 5; |
| 5394 | return setup_conf(mddev); |
| 5395 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 5396 | if (mddev->level == 6) |
| 5397 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5398 | |
| 5399 | return ERR_PTR(-EINVAL); |
| 5400 | } |
| 5401 | |
| 5402 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5403 | static struct mdk_personality raid5_personality; |
| 5404 | |
| 5405 | static void *raid6_takeover(mddev_t *mddev) |
| 5406 | { |
| 5407 | /* Currently can only take over a raid5. We map the |
| 5408 | * personality to an equivalent raid6 personality |
| 5409 | * with the Q block at the end. |
| 5410 | */ |
| 5411 | int new_layout; |
| 5412 | |
| 5413 | if (mddev->pers != &raid5_personality) |
| 5414 | return ERR_PTR(-EINVAL); |
| 5415 | if (mddev->degraded > 1) |
| 5416 | return ERR_PTR(-EINVAL); |
| 5417 | if (mddev->raid_disks > 253) |
| 5418 | return ERR_PTR(-EINVAL); |
| 5419 | if (mddev->raid_disks < 3) |
| 5420 | return ERR_PTR(-EINVAL); |
| 5421 | |
| 5422 | switch (mddev->layout) { |
| 5423 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 5424 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 5425 | break; |
| 5426 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 5427 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 5428 | break; |
| 5429 | case ALGORITHM_LEFT_SYMMETRIC: |
| 5430 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 5431 | break; |
| 5432 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 5433 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 5434 | break; |
| 5435 | case ALGORITHM_PARITY_0: |
| 5436 | new_layout = ALGORITHM_PARITY_0_6; |
| 5437 | break; |
| 5438 | case ALGORITHM_PARITY_N: |
| 5439 | new_layout = ALGORITHM_PARITY_N; |
| 5440 | break; |
| 5441 | default: |
| 5442 | return ERR_PTR(-EINVAL); |
| 5443 | } |
| 5444 | mddev->new_level = 6; |
| 5445 | mddev->new_layout = new_layout; |
| 5446 | mddev->delta_disks = 1; |
| 5447 | mddev->raid_disks += 1; |
| 5448 | return setup_conf(mddev); |
| 5449 | } |
| 5450 | |
| 5451 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5452 | static struct mdk_personality raid6_personality = |
| 5453 | { |
| 5454 | .name = "raid6", |
| 5455 | .level = 6, |
| 5456 | .owner = THIS_MODULE, |
| 5457 | .make_request = make_request, |
| 5458 | .run = run, |
| 5459 | .stop = stop, |
| 5460 | .status = status, |
| 5461 | .error_handler = error, |
| 5462 | .hot_add_disk = raid5_add_disk, |
| 5463 | .hot_remove_disk= raid5_remove_disk, |
| 5464 | .spare_active = raid5_spare_active, |
| 5465 | .sync_request = sync_request, |
| 5466 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5467 | .size = raid5_size, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5468 | .check_reshape = raid5_check_reshape, |
| 5469 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5470 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5471 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5472 | .takeover = raid6_takeover, |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5473 | .reconfig = raid6_reconfig, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5474 | }; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5475 | static struct mdk_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | { |
| 5477 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5478 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | .owner = THIS_MODULE, |
| 5480 | .make_request = make_request, |
| 5481 | .run = run, |
| 5482 | .stop = stop, |
| 5483 | .status = status, |
| 5484 | .error_handler = error, |
| 5485 | .hot_add_disk = raid5_add_disk, |
| 5486 | .hot_remove_disk= raid5_remove_disk, |
| 5487 | .spare_active = raid5_spare_active, |
| 5488 | .sync_request = sync_request, |
| 5489 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5490 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5491 | .check_reshape = raid5_check_reshape, |
| 5492 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5493 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5494 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5495 | .takeover = raid5_takeover, |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 5496 | .reconfig = raid5_reconfig, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | }; |
| 5498 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5499 | static struct mdk_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5500 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5501 | .name = "raid4", |
| 5502 | .level = 4, |
| 5503 | .owner = THIS_MODULE, |
| 5504 | .make_request = make_request, |
| 5505 | .run = run, |
| 5506 | .stop = stop, |
| 5507 | .status = status, |
| 5508 | .error_handler = error, |
| 5509 | .hot_add_disk = raid5_add_disk, |
| 5510 | .hot_remove_disk= raid5_remove_disk, |
| 5511 | .spare_active = raid5_spare_active, |
| 5512 | .sync_request = sync_request, |
| 5513 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5514 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 5515 | .check_reshape = raid5_check_reshape, |
| 5516 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5517 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5518 | .quiesce = raid5_quiesce, |
| 5519 | }; |
| 5520 | |
| 5521 | static int __init raid5_init(void) |
| 5522 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5523 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5524 | register_md_personality(&raid5_personality); |
| 5525 | register_md_personality(&raid4_personality); |
| 5526 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5527 | } |
| 5528 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5529 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5530 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5531 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5532 | unregister_md_personality(&raid5_personality); |
| 5533 | unregister_md_personality(&raid4_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5534 | } |
| 5535 | |
| 5536 | module_init(raid5_init); |
| 5537 | module_exit(raid5_exit); |
| 5538 | MODULE_LICENSE("GPL"); |
| 5539 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5540 | MODULE_ALIAS("md-raid5"); |
| 5541 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5542 | MODULE_ALIAS("md-level-5"); |
| 5543 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5544 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 5545 | MODULE_ALIAS("md-raid6"); |
| 5546 | MODULE_ALIAS("md-level-6"); |
| 5547 | |
| 5548 | /* This used to be two separate modules, they were: */ |
| 5549 | MODULE_ALIAS("raid5"); |
| 5550 | MODULE_ALIAS("raid6"); |