md/raid1: fix request counting bug in new 'barrier' code.
[linux-2.6-block.git] / drivers / md / raid5.c
CommitLineData
1da177e4
LT
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
16a53ecc 5 * Copyright (C) 2002, 2003 H. Peter Anvin
1da177e4 6 *
16a53ecc
N
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!
1da177e4
LT
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
ae3c20cc
N
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.
7c13edc8
N
30 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
ae3c20cc
N
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
7c13edc8 35 * the number of the batch it will be in. This is seq_flush+1.
ae3c20cc
N
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 */
1da177e4 45
bff61975 46#include <linux/blkdev.h>
f6705578 47#include <linux/kthread.h>
f701d589 48#include <linux/raid/pq.h>
91c00924 49#include <linux/async_tx.h>
056075c7 50#include <linux/module.h>
07a3b417 51#include <linux/async.h>
bff61975 52#include <linux/seq_file.h>
36d1c647 53#include <linux/cpu.h>
5a0e3ad6 54#include <linux/slab.h>
8bda470e 55#include <linux/ratelimit.h>
851c30c9 56#include <linux/nodemask.h>
a9add5d9
N
57#include <trace/events/block.h>
58
43b2e5d8 59#include "md.h"
bff61975 60#include "raid5.h"
54071b38 61#include "raid0.h"
ef740c37 62#include "bitmap.h"
72626685 63
851c30c9
SL
64#define cpu_to_group(cpu) cpu_to_node(cpu)
65#define ANY_GROUP NUMA_NO_NODE
66
67static struct workqueue_struct *raid5_wq;
1da177e4
LT
68/*
69 * Stripe cache
70 */
71
72#define NR_STRIPES 256
73#define STRIPE_SIZE PAGE_SIZE
74#define STRIPE_SHIFT (PAGE_SHIFT - 9)
75#define STRIPE_SECTORS (STRIPE_SIZE>>9)
76#define IO_THRESHOLD 1
8b3e6cdc 77#define BYPASS_THRESHOLD 1
fccddba0 78#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
1da177e4 79#define HASH_MASK (NR_HASH - 1)
bfc90cb0 80#define MAX_STRIPE_BATCH 8
1da177e4 81
d1688a6d 82static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
db298e19
N
83{
84 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
85 return &conf->stripe_hashtbl[hash];
86}
1da177e4 87
566c09c5
SL
88static inline int stripe_hash_locks_hash(sector_t sect)
89{
90 return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK;
91}
92
93static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
94{
95 spin_lock_irq(conf->hash_locks + hash);
96 spin_lock(&conf->device_lock);
97}
98
99static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
100{
101 spin_unlock(&conf->device_lock);
102 spin_unlock_irq(conf->hash_locks + hash);
103}
104
105static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
106{
107 int i;
108 local_irq_disable();
109 spin_lock(conf->hash_locks);
110 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
111 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
112 spin_lock(&conf->device_lock);
113}
114
115static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
116{
117 int i;
118 spin_unlock(&conf->device_lock);
119 for (i = NR_STRIPE_HASH_LOCKS; i; i--)
120 spin_unlock(conf->hash_locks + i - 1);
121 local_irq_enable();
122}
123
1da177e4
LT
124/* bio's attached to a stripe+device for I/O are linked together in bi_sector
125 * order without overlap. There may be several bio's per stripe+device, and
126 * a bio could span several devices.
127 * When walking this list for a particular stripe+device, we must never proceed
128 * beyond a bio that extends past this device, as the next bio might no longer
129 * be valid.
db298e19 130 * This function is used to determine the 'next' bio in the list, given the sector
1da177e4
LT
131 * of the current stripe+device
132 */
db298e19
N
133static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
134{
aa8b57aa 135 int sectors = bio_sectors(bio);
db298e19
N
136 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
137 return bio->bi_next;
138 else
139 return NULL;
140}
1da177e4 141
960e739d 142/*
5b99c2ff
JA
143 * We maintain a biased count of active stripes in the bottom 16 bits of
144 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
960e739d 145 */
e7836bd6 146static inline int raid5_bi_processed_stripes(struct bio *bio)
960e739d 147{
e7836bd6
SL
148 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
149 return (atomic_read(segments) >> 16) & 0xffff;
960e739d
JA
150}
151
e7836bd6 152static inline int raid5_dec_bi_active_stripes(struct bio *bio)
960e739d 153{
e7836bd6
SL
154 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
155 return atomic_sub_return(1, segments) & 0xffff;
960e739d
JA
156}
157
e7836bd6 158static inline void raid5_inc_bi_active_stripes(struct bio *bio)
960e739d 159{
e7836bd6
SL
160 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
161 atomic_inc(segments);
960e739d
JA
162}
163
e7836bd6
SL
164static inline void raid5_set_bi_processed_stripes(struct bio *bio,
165 unsigned int cnt)
960e739d 166{
e7836bd6
SL
167 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
168 int old, new;
960e739d 169
e7836bd6
SL
170 do {
171 old = atomic_read(segments);
172 new = (old & 0xffff) | (cnt << 16);
173 } while (atomic_cmpxchg(segments, old, new) != old);
960e739d
JA
174}
175
e7836bd6 176static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
960e739d 177{
e7836bd6
SL
178 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
179 atomic_set(segments, cnt);
960e739d
JA
180}
181
d0dabf7e
N
182/* Find first data disk in a raid6 stripe */
183static inline int raid6_d0(struct stripe_head *sh)
184{
67cc2b81
N
185 if (sh->ddf_layout)
186 /* ddf always start from first device */
187 return 0;
188 /* md starts just after Q block */
d0dabf7e
N
189 if (sh->qd_idx == sh->disks - 1)
190 return 0;
191 else
192 return sh->qd_idx + 1;
193}
16a53ecc
N
194static inline int raid6_next_disk(int disk, int raid_disks)
195{
196 disk++;
197 return (disk < raid_disks) ? disk : 0;
198}
a4456856 199
d0dabf7e
N
200/* When walking through the disks in a raid5, starting at raid6_d0,
201 * We need to map each disk to a 'slot', where the data disks are slot
202 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
203 * is raid_disks-1. This help does that mapping.
204 */
67cc2b81
N
205static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
206 int *count, int syndrome_disks)
d0dabf7e 207{
6629542e 208 int slot = *count;
67cc2b81 209
e4424fee 210 if (sh->ddf_layout)
6629542e 211 (*count)++;
d0dabf7e 212 if (idx == sh->pd_idx)
67cc2b81 213 return syndrome_disks;
d0dabf7e 214 if (idx == sh->qd_idx)
67cc2b81 215 return syndrome_disks + 1;
e4424fee 216 if (!sh->ddf_layout)
6629542e 217 (*count)++;
d0dabf7e
N
218 return slot;
219}
220
a4456856
DW
221static void return_io(struct bio *return_bi)
222{
223 struct bio *bi = return_bi;
224 while (bi) {
a4456856
DW
225
226 return_bi = bi->bi_next;
227 bi->bi_next = NULL;
228 bi->bi_size = 0;
0a82a8d1
LT
229 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
230 bi, 0);
0e13fe23 231 bio_endio(bi, 0);
a4456856
DW
232 bi = return_bi;
233 }
234}
235
d1688a6d 236static void print_raid5_conf (struct r5conf *conf);
1da177e4 237
600aa109
DW
238static int stripe_operations_active(struct stripe_head *sh)
239{
240 return sh->check_state || sh->reconstruct_state ||
241 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
242 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
243}
244
851c30c9
SL
245static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
246{
247 struct r5conf *conf = sh->raid_conf;
248 struct r5worker_group *group;
bfc90cb0 249 int thread_cnt;
851c30c9
SL
250 int i, cpu = sh->cpu;
251
252 if (!cpu_online(cpu)) {
253 cpu = cpumask_any(cpu_online_mask);
254 sh->cpu = cpu;
255 }
256
257 if (list_empty(&sh->lru)) {
258 struct r5worker_group *group;
259 group = conf->worker_groups + cpu_to_group(cpu);
260 list_add_tail(&sh->lru, &group->handle_list);
bfc90cb0
SL
261 group->stripes_cnt++;
262 sh->group = group;
851c30c9
SL
263 }
264
265 if (conf->worker_cnt_per_group == 0) {
266 md_wakeup_thread(conf->mddev->thread);
267 return;
268 }
269
270 group = conf->worker_groups + cpu_to_group(sh->cpu);
271
bfc90cb0
SL
272 group->workers[0].working = true;
273 /* at least one worker should run to avoid race */
274 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
275
276 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
277 /* wakeup more workers */
278 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
279 if (group->workers[i].working == false) {
280 group->workers[i].working = true;
281 queue_work_on(sh->cpu, raid5_wq,
282 &group->workers[i].work);
283 thread_cnt--;
284 }
285 }
851c30c9
SL
286}
287
566c09c5
SL
288static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
289 struct list_head *temp_inactive_list)
1da177e4 290{
4eb788df
SL
291 BUG_ON(!list_empty(&sh->lru));
292 BUG_ON(atomic_read(&conf->active_stripes)==0);
293 if (test_bit(STRIPE_HANDLE, &sh->state)) {
294 if (test_bit(STRIPE_DELAYED, &sh->state) &&
295 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
296 list_add_tail(&sh->lru, &conf->delayed_list);
297 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
298 sh->bm_seq - conf->seq_write > 0)
299 list_add_tail(&sh->lru, &conf->bitmap_list);
300 else {
301 clear_bit(STRIPE_DELAYED, &sh->state);
302 clear_bit(STRIPE_BIT_DELAY, &sh->state);
851c30c9
SL
303 if (conf->worker_cnt_per_group == 0) {
304 list_add_tail(&sh->lru, &conf->handle_list);
305 } else {
306 raid5_wakeup_stripe_thread(sh);
307 return;
308 }
4eb788df
SL
309 }
310 md_wakeup_thread(conf->mddev->thread);
311 } else {
312 BUG_ON(stripe_operations_active(sh));
313 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
314 if (atomic_dec_return(&conf->preread_active_stripes)
315 < IO_THRESHOLD)
316 md_wakeup_thread(conf->mddev->thread);
317 atomic_dec(&conf->active_stripes);
566c09c5
SL
318 if (!test_bit(STRIPE_EXPANDING, &sh->state))
319 list_add_tail(&sh->lru, temp_inactive_list);
1da177e4
LT
320 }
321}
d0dabf7e 322
566c09c5
SL
323static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
324 struct list_head *temp_inactive_list)
4eb788df
SL
325{
326 if (atomic_dec_and_test(&sh->count))
566c09c5
SL
327 do_release_stripe(conf, sh, temp_inactive_list);
328}
329
330/*
331 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
332 *
333 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
334 * given time. Adding stripes only takes device lock, while deleting stripes
335 * only takes hash lock.
336 */
337static void release_inactive_stripe_list(struct r5conf *conf,
338 struct list_head *temp_inactive_list,
339 int hash)
340{
341 int size;
342 bool do_wakeup = false;
343 unsigned long flags;
344
345 if (hash == NR_STRIPE_HASH_LOCKS) {
346 size = NR_STRIPE_HASH_LOCKS;
347 hash = NR_STRIPE_HASH_LOCKS - 1;
348 } else
349 size = 1;
350 while (size) {
351 struct list_head *list = &temp_inactive_list[size - 1];
352
353 /*
354 * We don't hold any lock here yet, get_active_stripe() might
355 * remove stripes from the list
356 */
357 if (!list_empty_careful(list)) {
358 spin_lock_irqsave(conf->hash_locks + hash, flags);
4bda556a
SL
359 if (list_empty(conf->inactive_list + hash) &&
360 !list_empty(list))
361 atomic_dec(&conf->empty_inactive_list_nr);
566c09c5
SL
362 list_splice_tail_init(list, conf->inactive_list + hash);
363 do_wakeup = true;
364 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
365 }
366 size--;
367 hash--;
368 }
369
370 if (do_wakeup) {
371 wake_up(&conf->wait_for_stripe);
372 if (conf->retry_read_aligned)
373 md_wakeup_thread(conf->mddev->thread);
374 }
4eb788df
SL
375}
376
773ca82f 377/* should hold conf->device_lock already */
566c09c5
SL
378static int release_stripe_list(struct r5conf *conf,
379 struct list_head *temp_inactive_list)
773ca82f
SL
380{
381 struct stripe_head *sh;
382 int count = 0;
383 struct llist_node *head;
384
385 head = llist_del_all(&conf->released_stripes);
d265d9dc 386 head = llist_reverse_order(head);
773ca82f 387 while (head) {
566c09c5
SL
388 int hash;
389
773ca82f
SL
390 sh = llist_entry(head, struct stripe_head, release_list);
391 head = llist_next(head);
392 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
393 smp_mb();
394 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
395 /*
396 * Don't worry the bit is set here, because if the bit is set
397 * again, the count is always > 1. This is true for
398 * STRIPE_ON_UNPLUG_LIST bit too.
399 */
566c09c5
SL
400 hash = sh->hash_lock_index;
401 __release_stripe(conf, sh, &temp_inactive_list[hash]);
773ca82f
SL
402 count++;
403 }
404
405 return count;
406}
407
1da177e4
LT
408static void release_stripe(struct stripe_head *sh)
409{
d1688a6d 410 struct r5conf *conf = sh->raid_conf;
1da177e4 411 unsigned long flags;
566c09c5
SL
412 struct list_head list;
413 int hash;
773ca82f 414 bool wakeup;
16a53ecc 415
ad4068de 416 if (unlikely(!conf->mddev->thread) ||
417 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
773ca82f
SL
418 goto slow_path;
419 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
420 if (wakeup)
421 md_wakeup_thread(conf->mddev->thread);
422 return;
423slow_path:
4eb788df 424 local_irq_save(flags);
773ca82f 425 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
4eb788df 426 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
566c09c5
SL
427 INIT_LIST_HEAD(&list);
428 hash = sh->hash_lock_index;
429 do_release_stripe(conf, sh, &list);
4eb788df 430 spin_unlock(&conf->device_lock);
566c09c5 431 release_inactive_stripe_list(conf, &list, hash);
4eb788df
SL
432 }
433 local_irq_restore(flags);
1da177e4
LT
434}
435
fccddba0 436static inline void remove_hash(struct stripe_head *sh)
1da177e4 437{
45b4233c
DW
438 pr_debug("remove_hash(), stripe %llu\n",
439 (unsigned long long)sh->sector);
1da177e4 440
fccddba0 441 hlist_del_init(&sh->hash);
1da177e4
LT
442}
443
d1688a6d 444static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
1da177e4 445{
fccddba0 446 struct hlist_head *hp = stripe_hash(conf, sh->sector);
1da177e4 447
45b4233c
DW
448 pr_debug("insert_hash(), stripe %llu\n",
449 (unsigned long long)sh->sector);
1da177e4 450
fccddba0 451 hlist_add_head(&sh->hash, hp);
1da177e4
LT
452}
453
454
455/* find an idle stripe, make sure it is unhashed, and return it. */
566c09c5 456static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
1da177e4
LT
457{
458 struct stripe_head *sh = NULL;
459 struct list_head *first;
460
566c09c5 461 if (list_empty(conf->inactive_list + hash))
1da177e4 462 goto out;
566c09c5 463 first = (conf->inactive_list + hash)->next;
1da177e4
LT
464 sh = list_entry(first, struct stripe_head, lru);
465 list_del_init(first);
466 remove_hash(sh);
467 atomic_inc(&conf->active_stripes);
566c09c5 468 BUG_ON(hash != sh->hash_lock_index);
4bda556a
SL
469 if (list_empty(conf->inactive_list + hash))
470 atomic_inc(&conf->empty_inactive_list_nr);
1da177e4
LT
471out:
472 return sh;
473}
474
e4e11e38 475static void shrink_buffers(struct stripe_head *sh)
1da177e4
LT
476{
477 struct page *p;
478 int i;
e4e11e38 479 int num = sh->raid_conf->pool_size;
1da177e4 480
e4e11e38 481 for (i = 0; i < num ; i++) {
1da177e4
LT
482 p = sh->dev[i].page;
483 if (!p)
484 continue;
485 sh->dev[i].page = NULL;
2d1f3b5d 486 put_page(p);
1da177e4
LT
487 }
488}
489
e4e11e38 490static int grow_buffers(struct stripe_head *sh)
1da177e4
LT
491{
492 int i;
e4e11e38 493 int num = sh->raid_conf->pool_size;
1da177e4 494
e4e11e38 495 for (i = 0; i < num; i++) {
1da177e4
LT
496 struct page *page;
497
498 if (!(page = alloc_page(GFP_KERNEL))) {
499 return 1;
500 }
501 sh->dev[i].page = page;
502 }
503 return 0;
504}
505
784052ec 506static void raid5_build_block(struct stripe_head *sh, int i, int previous);
d1688a6d 507static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
911d4ee8 508 struct stripe_head *sh);
1da177e4 509
b5663ba4 510static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
1da177e4 511{
d1688a6d 512 struct r5conf *conf = sh->raid_conf;
566c09c5 513 int i, seq;
1da177e4 514
78bafebd
ES
515 BUG_ON(atomic_read(&sh->count) != 0);
516 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
600aa109 517 BUG_ON(stripe_operations_active(sh));
d84e0f10 518
45b4233c 519 pr_debug("init_stripe called, stripe %llu\n",
1da177e4
LT
520 (unsigned long long)sh->sector);
521
522 remove_hash(sh);
566c09c5
SL
523retry:
524 seq = read_seqcount_begin(&conf->gen_lock);
86b42c71 525 sh->generation = conf->generation - previous;
b5663ba4 526 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
1da177e4 527 sh->sector = sector;
911d4ee8 528 stripe_set_idx(sector, conf, previous, sh);
1da177e4
LT
529 sh->state = 0;
530
7ecaa1e6
N
531
532 for (i = sh->disks; i--; ) {
1da177e4
LT
533 struct r5dev *dev = &sh->dev[i];
534
d84e0f10 535 if (dev->toread || dev->read || dev->towrite || dev->written ||
1da177e4 536 test_bit(R5_LOCKED, &dev->flags)) {
d84e0f10 537 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
1da177e4 538 (unsigned long long)sh->sector, i, dev->toread,
d84e0f10 539 dev->read, dev->towrite, dev->written,
1da177e4 540 test_bit(R5_LOCKED, &dev->flags));
8cfa7b0f 541 WARN_ON(1);
1da177e4
LT
542 }
543 dev->flags = 0;
784052ec 544 raid5_build_block(sh, i, previous);
1da177e4 545 }
566c09c5
SL
546 if (read_seqcount_retry(&conf->gen_lock, seq))
547 goto retry;
1da177e4 548 insert_hash(conf, sh);
851c30c9 549 sh->cpu = smp_processor_id();
1da177e4
LT
550}
551
d1688a6d 552static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
86b42c71 553 short generation)
1da177e4
LT
554{
555 struct stripe_head *sh;
556
45b4233c 557 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
b67bfe0d 558 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
86b42c71 559 if (sh->sector == sector && sh->generation == generation)
1da177e4 560 return sh;
45b4233c 561 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
1da177e4
LT
562 return NULL;
563}
564
674806d6
N
565/*
566 * Need to check if array has failed when deciding whether to:
567 * - start an array
568 * - remove non-faulty devices
569 * - add a spare
570 * - allow a reshape
571 * This determination is simple when no reshape is happening.
572 * However if there is a reshape, we need to carefully check
573 * both the before and after sections.
574 * This is because some failed devices may only affect one
575 * of the two sections, and some non-in_sync devices may
576 * be insync in the section most affected by failed devices.
577 */
908f4fbd 578static int calc_degraded(struct r5conf *conf)
674806d6 579{
908f4fbd 580 int degraded, degraded2;
674806d6 581 int i;
674806d6
N
582
583 rcu_read_lock();
584 degraded = 0;
585 for (i = 0; i < conf->previous_raid_disks; i++) {
3cb03002 586 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
e5c86471
N
587 if (rdev && test_bit(Faulty, &rdev->flags))
588 rdev = rcu_dereference(conf->disks[i].replacement);
674806d6
N
589 if (!rdev || test_bit(Faulty, &rdev->flags))
590 degraded++;
591 else if (test_bit(In_sync, &rdev->flags))
592 ;
593 else
594 /* not in-sync or faulty.
595 * If the reshape increases the number of devices,
596 * this is being recovered by the reshape, so
597 * this 'previous' section is not in_sync.
598 * If the number of devices is being reduced however,
599 * the device can only be part of the array if
600 * we are reverting a reshape, so this section will
601 * be in-sync.
602 */
603 if (conf->raid_disks >= conf->previous_raid_disks)
604 degraded++;
605 }
606 rcu_read_unlock();
908f4fbd
N
607 if (conf->raid_disks == conf->previous_raid_disks)
608 return degraded;
674806d6 609 rcu_read_lock();
908f4fbd 610 degraded2 = 0;
674806d6 611 for (i = 0; i < conf->raid_disks; i++) {
3cb03002 612 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
e5c86471
N
613 if (rdev && test_bit(Faulty, &rdev->flags))
614 rdev = rcu_dereference(conf->disks[i].replacement);
674806d6 615 if (!rdev || test_bit(Faulty, &rdev->flags))
908f4fbd 616 degraded2++;
674806d6
N
617 else if (test_bit(In_sync, &rdev->flags))
618 ;
619 else
620 /* not in-sync or faulty.
621 * If reshape increases the number of devices, this
622 * section has already been recovered, else it
623 * almost certainly hasn't.
624 */
625 if (conf->raid_disks <= conf->previous_raid_disks)
908f4fbd 626 degraded2++;
674806d6
N
627 }
628 rcu_read_unlock();
908f4fbd
N
629 if (degraded2 > degraded)
630 return degraded2;
631 return degraded;
632}
633
634static int has_failed(struct r5conf *conf)
635{
636 int degraded;
637
638 if (conf->mddev->reshape_position == MaxSector)
639 return conf->mddev->degraded > conf->max_degraded;
640
641 degraded = calc_degraded(conf);
674806d6
N
642 if (degraded > conf->max_degraded)
643 return 1;
644 return 0;
645}
646
b5663ba4 647static struct stripe_head *
d1688a6d 648get_active_stripe(struct r5conf *conf, sector_t sector,
a8c906ca 649 int previous, int noblock, int noquiesce)
1da177e4
LT
650{
651 struct stripe_head *sh;
566c09c5 652 int hash = stripe_hash_locks_hash(sector);
1da177e4 653
45b4233c 654 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
1da177e4 655
566c09c5 656 spin_lock_irq(conf->hash_locks + hash);
1da177e4
LT
657
658 do {
72626685 659 wait_event_lock_irq(conf->wait_for_stripe,
a8c906ca 660 conf->quiesce == 0 || noquiesce,
566c09c5 661 *(conf->hash_locks + hash));
86b42c71 662 sh = __find_stripe(conf, sector, conf->generation - previous);
1da177e4
LT
663 if (!sh) {
664 if (!conf->inactive_blocked)
566c09c5 665 sh = get_free_stripe(conf, hash);
1da177e4
LT
666 if (noblock && sh == NULL)
667 break;
668 if (!sh) {
669 conf->inactive_blocked = 1;
566c09c5
SL
670 wait_event_lock_irq(
671 conf->wait_for_stripe,
672 !list_empty(conf->inactive_list + hash) &&
673 (atomic_read(&conf->active_stripes)
674 < (conf->max_nr_stripes * 3 / 4)
675 || !conf->inactive_blocked),
676 *(conf->hash_locks + hash));
1da177e4
LT
677 conf->inactive_blocked = 0;
678 } else
b5663ba4 679 init_stripe(sh, sector, previous);
1da177e4 680 } else {
6d183de4 681 spin_lock(&conf->device_lock);
1da177e4 682 if (atomic_read(&sh->count)) {
ab69ae12 683 BUG_ON(!list_empty(&sh->lru)
8811b596 684 && !test_bit(STRIPE_EXPANDING, &sh->state)
773ca82f 685 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)
6d183de4 686 );
1da177e4
LT
687 } else {
688 if (!test_bit(STRIPE_HANDLE, &sh->state))
689 atomic_inc(&conf->active_stripes);
6d183de4 690 BUG_ON(list_empty(&sh->lru));
16a53ecc 691 list_del_init(&sh->lru);
bfc90cb0
SL
692 if (sh->group) {
693 sh->group->stripes_cnt--;
694 sh->group = NULL;
695 }
1da177e4 696 }
6d183de4 697 spin_unlock(&conf->device_lock);
1da177e4
LT
698 }
699 } while (sh == NULL);
700
701 if (sh)
702 atomic_inc(&sh->count);
703
566c09c5 704 spin_unlock_irq(conf->hash_locks + hash);
1da177e4
LT
705 return sh;
706}
707
05616be5
N
708/* Determine if 'data_offset' or 'new_data_offset' should be used
709 * in this stripe_head.
710 */
711static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
712{
713 sector_t progress = conf->reshape_progress;
714 /* Need a memory barrier to make sure we see the value
715 * of conf->generation, or ->data_offset that was set before
716 * reshape_progress was updated.
717 */
718 smp_rmb();
719 if (progress == MaxSector)
720 return 0;
721 if (sh->generation == conf->generation - 1)
722 return 0;
723 /* We are in a reshape, and this is a new-generation stripe,
724 * so use new_data_offset.
725 */
726 return 1;
727}
728
6712ecf8
N
729static void
730raid5_end_read_request(struct bio *bi, int error);
731static void
732raid5_end_write_request(struct bio *bi, int error);
91c00924 733
c4e5ac0a 734static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
91c00924 735{
d1688a6d 736 struct r5conf *conf = sh->raid_conf;
91c00924
DW
737 int i, disks = sh->disks;
738
739 might_sleep();
740
741 for (i = disks; i--; ) {
742 int rw;
9a3e1101 743 int replace_only = 0;
977df362
N
744 struct bio *bi, *rbi;
745 struct md_rdev *rdev, *rrdev = NULL;
e9c7469b
TH
746 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
747 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
748 rw = WRITE_FUA;
749 else
750 rw = WRITE;
9e444768 751 if (test_bit(R5_Discard, &sh->dev[i].flags))
620125f2 752 rw |= REQ_DISCARD;
e9c7469b 753 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
91c00924 754 rw = READ;
9a3e1101
N
755 else if (test_and_clear_bit(R5_WantReplace,
756 &sh->dev[i].flags)) {
757 rw = WRITE;
758 replace_only = 1;
759 } else
91c00924 760 continue;
bc0934f0
SL
761 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
762 rw |= REQ_SYNC;
91c00924
DW
763
764 bi = &sh->dev[i].req;
977df362 765 rbi = &sh->dev[i].rreq; /* For writing to replacement */
91c00924 766
91c00924 767 rcu_read_lock();
9a3e1101 768 rrdev = rcu_dereference(conf->disks[i].replacement);
dd054fce
N
769 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
770 rdev = rcu_dereference(conf->disks[i].rdev);
771 if (!rdev) {
772 rdev = rrdev;
773 rrdev = NULL;
774 }
9a3e1101
N
775 if (rw & WRITE) {
776 if (replace_only)
777 rdev = NULL;
dd054fce
N
778 if (rdev == rrdev)
779 /* We raced and saw duplicates */
780 rrdev = NULL;
9a3e1101 781 } else {
dd054fce 782 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
9a3e1101
N
783 rdev = rrdev;
784 rrdev = NULL;
785 }
977df362 786
91c00924
DW
787 if (rdev && test_bit(Faulty, &rdev->flags))
788 rdev = NULL;
789 if (rdev)
790 atomic_inc(&rdev->nr_pending);
977df362
N
791 if (rrdev && test_bit(Faulty, &rrdev->flags))
792 rrdev = NULL;
793 if (rrdev)
794 atomic_inc(&rrdev->nr_pending);
91c00924
DW
795 rcu_read_unlock();
796
73e92e51 797 /* We have already checked bad blocks for reads. Now
977df362
N
798 * need to check for writes. We never accept write errors
799 * on the replacement, so we don't to check rrdev.
73e92e51
N
800 */
801 while ((rw & WRITE) && rdev &&
802 test_bit(WriteErrorSeen, &rdev->flags)) {
803 sector_t first_bad;
804 int bad_sectors;
805 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
806 &first_bad, &bad_sectors);
807 if (!bad)
808 break;
809
810 if (bad < 0) {
811 set_bit(BlockedBadBlocks, &rdev->flags);
812 if (!conf->mddev->external &&
813 conf->mddev->flags) {
814 /* It is very unlikely, but we might
815 * still need to write out the
816 * bad block log - better give it
817 * a chance*/
818 md_check_recovery(conf->mddev);
819 }
1850753d 820 /*
821 * Because md_wait_for_blocked_rdev
822 * will dec nr_pending, we must
823 * increment it first.
824 */
825 atomic_inc(&rdev->nr_pending);
73e92e51
N
826 md_wait_for_blocked_rdev(rdev, conf->mddev);
827 } else {
828 /* Acknowledged bad block - skip the write */
829 rdev_dec_pending(rdev, conf->mddev);
830 rdev = NULL;
831 }
832 }
833
91c00924 834 if (rdev) {
9a3e1101
N
835 if (s->syncing || s->expanding || s->expanded
836 || s->replacing)
91c00924
DW
837 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
838
2b7497f0
DW
839 set_bit(STRIPE_IO_STARTED, &sh->state);
840
2f6db2a7 841 bio_reset(bi);
91c00924 842 bi->bi_bdev = rdev->bdev;
2f6db2a7
KO
843 bi->bi_rw = rw;
844 bi->bi_end_io = (rw & WRITE)
845 ? raid5_end_write_request
846 : raid5_end_read_request;
847 bi->bi_private = sh;
848
91c00924 849 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
e46b272b 850 __func__, (unsigned long long)sh->sector,
91c00924
DW
851 bi->bi_rw, i);
852 atomic_inc(&sh->count);
05616be5
N
853 if (use_new_offset(conf, sh))
854 bi->bi_sector = (sh->sector
855 + rdev->new_data_offset);
856 else
857 bi->bi_sector = (sh->sector
858 + rdev->data_offset);
3f9e7c14 859 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
e59aa23f 860 bi->bi_rw |= REQ_NOMERGE;
3f9e7c14 861
4997b72e 862 bi->bi_vcnt = 1;
91c00924
DW
863 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
864 bi->bi_io_vec[0].bv_offset = 0;
865 bi->bi_size = STRIPE_SIZE;
37c61ff3
SL
866 /*
867 * If this is discard request, set bi_vcnt 0. We don't
868 * want to confuse SCSI because SCSI will replace payload
869 */
870 if (rw & REQ_DISCARD)
871 bi->bi_vcnt = 0;
977df362
N
872 if (rrdev)
873 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
e3620a3a
JB
874
875 if (conf->mddev->gendisk)
876 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
877 bi, disk_devt(conf->mddev->gendisk),
878 sh->dev[i].sector);
91c00924 879 generic_make_request(bi);
977df362
N
880 }
881 if (rrdev) {
9a3e1101
N
882 if (s->syncing || s->expanding || s->expanded
883 || s->replacing)
977df362
N
884 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
885
886 set_bit(STRIPE_IO_STARTED, &sh->state);
887
2f6db2a7 888 bio_reset(rbi);
977df362 889 rbi->bi_bdev = rrdev->bdev;
2f6db2a7
KO
890 rbi->bi_rw = rw;
891 BUG_ON(!(rw & WRITE));
892 rbi->bi_end_io = raid5_end_write_request;
893 rbi->bi_private = sh;
894
977df362
N
895 pr_debug("%s: for %llu schedule op %ld on "
896 "replacement disc %d\n",
897 __func__, (unsigned long long)sh->sector,
898 rbi->bi_rw, i);
899 atomic_inc(&sh->count);
05616be5
N
900 if (use_new_offset(conf, sh))
901 rbi->bi_sector = (sh->sector
902 + rrdev->new_data_offset);
903 else
904 rbi->bi_sector = (sh->sector
905 + rrdev->data_offset);
4997b72e 906 rbi->bi_vcnt = 1;
977df362
N
907 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
908 rbi->bi_io_vec[0].bv_offset = 0;
909 rbi->bi_size = STRIPE_SIZE;
37c61ff3
SL
910 /*
911 * If this is discard request, set bi_vcnt 0. We don't
912 * want to confuse SCSI because SCSI will replace payload
913 */
914 if (rw & REQ_DISCARD)
915 rbi->bi_vcnt = 0;
e3620a3a
JB
916 if (conf->mddev->gendisk)
917 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
918 rbi, disk_devt(conf->mddev->gendisk),
919 sh->dev[i].sector);
977df362
N
920 generic_make_request(rbi);
921 }
922 if (!rdev && !rrdev) {
b062962e 923 if (rw & WRITE)
91c00924
DW
924 set_bit(STRIPE_DEGRADED, &sh->state);
925 pr_debug("skip op %ld on disc %d for sector %llu\n",
926 bi->bi_rw, i, (unsigned long long)sh->sector);
927 clear_bit(R5_LOCKED, &sh->dev[i].flags);
928 set_bit(STRIPE_HANDLE, &sh->state);
929 }
930 }
931}
932
933static struct dma_async_tx_descriptor *
934async_copy_data(int frombio, struct bio *bio, struct page *page,
935 sector_t sector, struct dma_async_tx_descriptor *tx)
936{
937 struct bio_vec *bvl;
938 struct page *bio_page;
939 int i;
940 int page_offset;
a08abd8c 941 struct async_submit_ctl submit;
0403e382 942 enum async_tx_flags flags = 0;
91c00924
DW
943
944 if (bio->bi_sector >= sector)
945 page_offset = (signed)(bio->bi_sector - sector) * 512;
946 else
947 page_offset = (signed)(sector - bio->bi_sector) * -512;
a08abd8c 948
0403e382
DW
949 if (frombio)
950 flags |= ASYNC_TX_FENCE;
951 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
952
91c00924 953 bio_for_each_segment(bvl, bio, i) {
fcde9075 954 int len = bvl->bv_len;
91c00924
DW
955 int clen;
956 int b_offset = 0;
957
958 if (page_offset < 0) {
959 b_offset = -page_offset;
960 page_offset += b_offset;
961 len -= b_offset;
962 }
963
964 if (len > 0 && page_offset + len > STRIPE_SIZE)
965 clen = STRIPE_SIZE - page_offset;
966 else
967 clen = len;
968
969 if (clen > 0) {
fcde9075
NK
970 b_offset += bvl->bv_offset;
971 bio_page = bvl->bv_page;
91c00924
DW
972 if (frombio)
973 tx = async_memcpy(page, bio_page, page_offset,
a08abd8c 974 b_offset, clen, &submit);
91c00924
DW
975 else
976 tx = async_memcpy(bio_page, page, b_offset,
a08abd8c 977 page_offset, clen, &submit);
91c00924 978 }
a08abd8c
DW
979 /* chain the operations */
980 submit.depend_tx = tx;
981
91c00924
DW
982 if (clen < len) /* hit end of page */
983 break;
984 page_offset += len;
985 }
986
987 return tx;
988}
989
990static void ops_complete_biofill(void *stripe_head_ref)
991{
992 struct stripe_head *sh = stripe_head_ref;
993 struct bio *return_bi = NULL;
e4d84909 994 int i;
91c00924 995
e46b272b 996 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
997 (unsigned long long)sh->sector);
998
999 /* clear completed biofills */
1000 for (i = sh->disks; i--; ) {
1001 struct r5dev *dev = &sh->dev[i];
91c00924
DW
1002
1003 /* acknowledge completion of a biofill operation */
e4d84909
DW
1004 /* and check if we need to reply to a read request,
1005 * new R5_Wantfill requests are held off until
83de75cc 1006 * !STRIPE_BIOFILL_RUN
e4d84909
DW
1007 */
1008 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
91c00924 1009 struct bio *rbi, *rbi2;
91c00924 1010
91c00924
DW
1011 BUG_ON(!dev->read);
1012 rbi = dev->read;
1013 dev->read = NULL;
1014 while (rbi && rbi->bi_sector <
1015 dev->sector + STRIPE_SECTORS) {
1016 rbi2 = r5_next_bio(rbi, dev->sector);
e7836bd6 1017 if (!raid5_dec_bi_active_stripes(rbi)) {
91c00924
DW
1018 rbi->bi_next = return_bi;
1019 return_bi = rbi;
1020 }
91c00924
DW
1021 rbi = rbi2;
1022 }
1023 }
1024 }
83de75cc 1025 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
91c00924
DW
1026
1027 return_io(return_bi);
1028
e4d84909 1029 set_bit(STRIPE_HANDLE, &sh->state);
91c00924
DW
1030 release_stripe(sh);
1031}
1032
1033static void ops_run_biofill(struct stripe_head *sh)
1034{
1035 struct dma_async_tx_descriptor *tx = NULL;
a08abd8c 1036 struct async_submit_ctl submit;
91c00924
DW
1037 int i;
1038
e46b272b 1039 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1040 (unsigned long long)sh->sector);
1041
1042 for (i = sh->disks; i--; ) {
1043 struct r5dev *dev = &sh->dev[i];
1044 if (test_bit(R5_Wantfill, &dev->flags)) {
1045 struct bio *rbi;
b17459c0 1046 spin_lock_irq(&sh->stripe_lock);
91c00924
DW
1047 dev->read = rbi = dev->toread;
1048 dev->toread = NULL;
b17459c0 1049 spin_unlock_irq(&sh->stripe_lock);
91c00924
DW
1050 while (rbi && rbi->bi_sector <
1051 dev->sector + STRIPE_SECTORS) {
1052 tx = async_copy_data(0, rbi, dev->page,
1053 dev->sector, tx);
1054 rbi = r5_next_bio(rbi, dev->sector);
1055 }
1056 }
1057 }
1058
1059 atomic_inc(&sh->count);
a08abd8c
DW
1060 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1061 async_trigger_callback(&submit);
91c00924
DW
1062}
1063
4e7d2c0a 1064static void mark_target_uptodate(struct stripe_head *sh, int target)
91c00924 1065{
4e7d2c0a 1066 struct r5dev *tgt;
91c00924 1067
4e7d2c0a
DW
1068 if (target < 0)
1069 return;
91c00924 1070
4e7d2c0a 1071 tgt = &sh->dev[target];
91c00924
DW
1072 set_bit(R5_UPTODATE, &tgt->flags);
1073 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1074 clear_bit(R5_Wantcompute, &tgt->flags);
4e7d2c0a
DW
1075}
1076
ac6b53b6 1077static void ops_complete_compute(void *stripe_head_ref)
91c00924
DW
1078{
1079 struct stripe_head *sh = stripe_head_ref;
91c00924 1080
e46b272b 1081 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1082 (unsigned long long)sh->sector);
1083
ac6b53b6 1084 /* mark the computed target(s) as uptodate */
4e7d2c0a 1085 mark_target_uptodate(sh, sh->ops.target);
ac6b53b6 1086 mark_target_uptodate(sh, sh->ops.target2);
4e7d2c0a 1087
ecc65c9b
DW
1088 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1089 if (sh->check_state == check_state_compute_run)
1090 sh->check_state = check_state_compute_result;
91c00924
DW
1091 set_bit(STRIPE_HANDLE, &sh->state);
1092 release_stripe(sh);
1093}
1094
d6f38f31
DW
1095/* return a pointer to the address conversion region of the scribble buffer */
1096static addr_conv_t *to_addr_conv(struct stripe_head *sh,
1097 struct raid5_percpu *percpu)
1098{
1099 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
1100}
1101
1102static struct dma_async_tx_descriptor *
1103ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
91c00924 1104{
91c00924 1105 int disks = sh->disks;
d6f38f31 1106 struct page **xor_srcs = percpu->scribble;
91c00924
DW
1107 int target = sh->ops.target;
1108 struct r5dev *tgt = &sh->dev[target];
1109 struct page *xor_dest = tgt->page;
1110 int count = 0;
1111 struct dma_async_tx_descriptor *tx;
a08abd8c 1112 struct async_submit_ctl submit;
91c00924
DW
1113 int i;
1114
1115 pr_debug("%s: stripe %llu block: %d\n",
e46b272b 1116 __func__, (unsigned long long)sh->sector, target);
91c00924
DW
1117 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1118
1119 for (i = disks; i--; )
1120 if (i != target)
1121 xor_srcs[count++] = sh->dev[i].page;
1122
1123 atomic_inc(&sh->count);
1124
0403e382 1125 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
ac6b53b6 1126 ops_complete_compute, sh, to_addr_conv(sh, percpu));
91c00924 1127 if (unlikely(count == 1))
a08abd8c 1128 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
91c00924 1129 else
a08abd8c 1130 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
91c00924 1131
91c00924
DW
1132 return tx;
1133}
1134
ac6b53b6
DW
1135/* set_syndrome_sources - populate source buffers for gen_syndrome
1136 * @srcs - (struct page *) array of size sh->disks
1137 * @sh - stripe_head to parse
1138 *
1139 * Populates srcs in proper layout order for the stripe and returns the
1140 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1141 * destination buffer is recorded in srcs[count] and the Q destination
1142 * is recorded in srcs[count+1]].
1143 */
1144static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
1145{
1146 int disks = sh->disks;
1147 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1148 int d0_idx = raid6_d0(sh);
1149 int count;
1150 int i;
1151
1152 for (i = 0; i < disks; i++)
5dd33c9a 1153 srcs[i] = NULL;
ac6b53b6
DW
1154
1155 count = 0;
1156 i = d0_idx;
1157 do {
1158 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1159
1160 srcs[slot] = sh->dev[i].page;
1161 i = raid6_next_disk(i, disks);
1162 } while (i != d0_idx);
ac6b53b6 1163
e4424fee 1164 return syndrome_disks;
ac6b53b6
DW
1165}
1166
1167static struct dma_async_tx_descriptor *
1168ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1169{
1170 int disks = sh->disks;
1171 struct page **blocks = percpu->scribble;
1172 int target;
1173 int qd_idx = sh->qd_idx;
1174 struct dma_async_tx_descriptor *tx;
1175 struct async_submit_ctl submit;
1176 struct r5dev *tgt;
1177 struct page *dest;
1178 int i;
1179 int count;
1180
1181 if (sh->ops.target < 0)
1182 target = sh->ops.target2;
1183 else if (sh->ops.target2 < 0)
1184 target = sh->ops.target;
91c00924 1185 else
ac6b53b6
DW
1186 /* we should only have one valid target */
1187 BUG();
1188 BUG_ON(target < 0);
1189 pr_debug("%s: stripe %llu block: %d\n",
1190 __func__, (unsigned long long)sh->sector, target);
1191
1192 tgt = &sh->dev[target];
1193 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1194 dest = tgt->page;
1195
1196 atomic_inc(&sh->count);
1197
1198 if (target == qd_idx) {
1199 count = set_syndrome_sources(blocks, sh);
1200 blocks[count] = NULL; /* regenerating p is not necessary */
1201 BUG_ON(blocks[count+1] != dest); /* q should already be set */
0403e382
DW
1202 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1203 ops_complete_compute, sh,
ac6b53b6
DW
1204 to_addr_conv(sh, percpu));
1205 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1206 } else {
1207 /* Compute any data- or p-drive using XOR */
1208 count = 0;
1209 for (i = disks; i-- ; ) {
1210 if (i == target || i == qd_idx)
1211 continue;
1212 blocks[count++] = sh->dev[i].page;
1213 }
1214
0403e382
DW
1215 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1216 NULL, ops_complete_compute, sh,
ac6b53b6
DW
1217 to_addr_conv(sh, percpu));
1218 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1219 }
91c00924 1220
91c00924
DW
1221 return tx;
1222}
1223
ac6b53b6
DW
1224static struct dma_async_tx_descriptor *
1225ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1226{
1227 int i, count, disks = sh->disks;
1228 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1229 int d0_idx = raid6_d0(sh);
1230 int faila = -1, failb = -1;
1231 int target = sh->ops.target;
1232 int target2 = sh->ops.target2;
1233 struct r5dev *tgt = &sh->dev[target];
1234 struct r5dev *tgt2 = &sh->dev[target2];
1235 struct dma_async_tx_descriptor *tx;
1236 struct page **blocks = percpu->scribble;
1237 struct async_submit_ctl submit;
1238
1239 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1240 __func__, (unsigned long long)sh->sector, target, target2);
1241 BUG_ON(target < 0 || target2 < 0);
1242 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1243 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1244
6c910a78 1245 /* we need to open-code set_syndrome_sources to handle the
ac6b53b6
DW
1246 * slot number conversion for 'faila' and 'failb'
1247 */
1248 for (i = 0; i < disks ; i++)
5dd33c9a 1249 blocks[i] = NULL;
ac6b53b6
DW
1250 count = 0;
1251 i = d0_idx;
1252 do {
1253 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1254
1255 blocks[slot] = sh->dev[i].page;
1256
1257 if (i == target)
1258 faila = slot;
1259 if (i == target2)
1260 failb = slot;
1261 i = raid6_next_disk(i, disks);
1262 } while (i != d0_idx);
ac6b53b6
DW
1263
1264 BUG_ON(faila == failb);
1265 if (failb < faila)
1266 swap(faila, failb);
1267 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1268 __func__, (unsigned long long)sh->sector, faila, failb);
1269
1270 atomic_inc(&sh->count);
1271
1272 if (failb == syndrome_disks+1) {
1273 /* Q disk is one of the missing disks */
1274 if (faila == syndrome_disks) {
1275 /* Missing P+Q, just recompute */
0403e382
DW
1276 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1277 ops_complete_compute, sh,
1278 to_addr_conv(sh, percpu));
e4424fee 1279 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
ac6b53b6
DW
1280 STRIPE_SIZE, &submit);
1281 } else {
1282 struct page *dest;
1283 int data_target;
1284 int qd_idx = sh->qd_idx;
1285
1286 /* Missing D+Q: recompute D from P, then recompute Q */
1287 if (target == qd_idx)
1288 data_target = target2;
1289 else
1290 data_target = target;
1291
1292 count = 0;
1293 for (i = disks; i-- ; ) {
1294 if (i == data_target || i == qd_idx)
1295 continue;
1296 blocks[count++] = sh->dev[i].page;
1297 }
1298 dest = sh->dev[data_target].page;
0403e382
DW
1299 init_async_submit(&submit,
1300 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1301 NULL, NULL, NULL,
1302 to_addr_conv(sh, percpu));
ac6b53b6
DW
1303 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1304 &submit);
1305
1306 count = set_syndrome_sources(blocks, sh);
0403e382
DW
1307 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1308 ops_complete_compute, sh,
1309 to_addr_conv(sh, percpu));
ac6b53b6
DW
1310 return async_gen_syndrome(blocks, 0, count+2,
1311 STRIPE_SIZE, &submit);
1312 }
ac6b53b6 1313 } else {
6c910a78
DW
1314 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1315 ops_complete_compute, sh,
1316 to_addr_conv(sh, percpu));
1317 if (failb == syndrome_disks) {
1318 /* We're missing D+P. */
1319 return async_raid6_datap_recov(syndrome_disks+2,
1320 STRIPE_SIZE, faila,
1321 blocks, &submit);
1322 } else {
1323 /* We're missing D+D. */
1324 return async_raid6_2data_recov(syndrome_disks+2,
1325 STRIPE_SIZE, faila, failb,
1326 blocks, &submit);
1327 }
ac6b53b6
DW
1328 }
1329}
1330
1331
91c00924
DW
1332static void ops_complete_prexor(void *stripe_head_ref)
1333{
1334 struct stripe_head *sh = stripe_head_ref;
1335
e46b272b 1336 pr_debug("%s: stripe %llu\n", __func__,
91c00924 1337 (unsigned long long)sh->sector);
91c00924
DW
1338}
1339
1340static struct dma_async_tx_descriptor *
d6f38f31
DW
1341ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1342 struct dma_async_tx_descriptor *tx)
91c00924 1343{
91c00924 1344 int disks = sh->disks;
d6f38f31 1345 struct page **xor_srcs = percpu->scribble;
91c00924 1346 int count = 0, pd_idx = sh->pd_idx, i;
a08abd8c 1347 struct async_submit_ctl submit;
91c00924
DW
1348
1349 /* existing parity data subtracted */
1350 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1351
e46b272b 1352 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1353 (unsigned long long)sh->sector);
1354
1355 for (i = disks; i--; ) {
1356 struct r5dev *dev = &sh->dev[i];
1357 /* Only process blocks that are known to be uptodate */
d8ee0728 1358 if (test_bit(R5_Wantdrain, &dev->flags))
91c00924
DW
1359 xor_srcs[count++] = dev->page;
1360 }
1361
0403e382 1362 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
d6f38f31 1363 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
a08abd8c 1364 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
91c00924
DW
1365
1366 return tx;
1367}
1368
1369static struct dma_async_tx_descriptor *
d8ee0728 1370ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
91c00924
DW
1371{
1372 int disks = sh->disks;
d8ee0728 1373 int i;
91c00924 1374
e46b272b 1375 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1376 (unsigned long long)sh->sector);
1377
1378 for (i = disks; i--; ) {
1379 struct r5dev *dev = &sh->dev[i];
1380 struct bio *chosen;
91c00924 1381
d8ee0728 1382 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
91c00924
DW
1383 struct bio *wbi;
1384
b17459c0 1385 spin_lock_irq(&sh->stripe_lock);
91c00924
DW
1386 chosen = dev->towrite;
1387 dev->towrite = NULL;
1388 BUG_ON(dev->written);
1389 wbi = dev->written = chosen;
b17459c0 1390 spin_unlock_irq(&sh->stripe_lock);
91c00924
DW
1391
1392 while (wbi && wbi->bi_sector <
1393 dev->sector + STRIPE_SECTORS) {
e9c7469b
TH
1394 if (wbi->bi_rw & REQ_FUA)
1395 set_bit(R5_WantFUA, &dev->flags);
bc0934f0
SL
1396 if (wbi->bi_rw & REQ_SYNC)
1397 set_bit(R5_SyncIO, &dev->flags);
9e444768 1398 if (wbi->bi_rw & REQ_DISCARD)
620125f2 1399 set_bit(R5_Discard, &dev->flags);
9e444768 1400 else
620125f2
SL
1401 tx = async_copy_data(1, wbi, dev->page,
1402 dev->sector, tx);
91c00924
DW
1403 wbi = r5_next_bio(wbi, dev->sector);
1404 }
1405 }
1406 }
1407
1408 return tx;
1409}
1410
ac6b53b6 1411static void ops_complete_reconstruct(void *stripe_head_ref)
91c00924
DW
1412{
1413 struct stripe_head *sh = stripe_head_ref;
ac6b53b6
DW
1414 int disks = sh->disks;
1415 int pd_idx = sh->pd_idx;
1416 int qd_idx = sh->qd_idx;
1417 int i;
9e444768 1418 bool fua = false, sync = false, discard = false;
91c00924 1419
e46b272b 1420 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1421 (unsigned long long)sh->sector);
1422
bc0934f0 1423 for (i = disks; i--; ) {
e9c7469b 1424 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
bc0934f0 1425 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
9e444768 1426 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
bc0934f0 1427 }
e9c7469b 1428
91c00924
DW
1429 for (i = disks; i--; ) {
1430 struct r5dev *dev = &sh->dev[i];
ac6b53b6 1431
e9c7469b 1432 if (dev->written || i == pd_idx || i == qd_idx) {
9e444768
SL
1433 if (!discard)
1434 set_bit(R5_UPTODATE, &dev->flags);
e9c7469b
TH
1435 if (fua)
1436 set_bit(R5_WantFUA, &dev->flags);
bc0934f0
SL
1437 if (sync)
1438 set_bit(R5_SyncIO, &dev->flags);
e9c7469b 1439 }
91c00924
DW
1440 }
1441
d8ee0728
DW
1442 if (sh->reconstruct_state == reconstruct_state_drain_run)
1443 sh->reconstruct_state = reconstruct_state_drain_result;
1444 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1445 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1446 else {
1447 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1448 sh->reconstruct_state = reconstruct_state_result;
1449 }
91c00924
DW
1450
1451 set_bit(STRIPE_HANDLE, &sh->state);
1452 release_stripe(sh);
1453}
1454
1455static void
ac6b53b6
DW
1456ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1457 struct dma_async_tx_descriptor *tx)
91c00924 1458{
91c00924 1459 int disks = sh->disks;
d6f38f31 1460 struct page **xor_srcs = percpu->scribble;
a08abd8c 1461 struct async_submit_ctl submit;
91c00924
DW
1462 int count = 0, pd_idx = sh->pd_idx, i;
1463 struct page *xor_dest;
d8ee0728 1464 int prexor = 0;
91c00924 1465 unsigned long flags;
91c00924 1466
e46b272b 1467 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1468 (unsigned long long)sh->sector);
1469
620125f2
SL
1470 for (i = 0; i < sh->disks; i++) {
1471 if (pd_idx == i)
1472 continue;
1473 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1474 break;
1475 }
1476 if (i >= sh->disks) {
1477 atomic_inc(&sh->count);
620125f2
SL
1478 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1479 ops_complete_reconstruct(sh);
1480 return;
1481 }
91c00924
DW
1482 /* check if prexor is active which means only process blocks
1483 * that are part of a read-modify-write (written)
1484 */
d8ee0728
DW
1485 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1486 prexor = 1;
91c00924
DW
1487 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1488 for (i = disks; i--; ) {
1489 struct r5dev *dev = &sh->dev[i];
1490 if (dev->written)
1491 xor_srcs[count++] = dev->page;
1492 }
1493 } else {
1494 xor_dest = sh->dev[pd_idx].page;
1495 for (i = disks; i--; ) {
1496 struct r5dev *dev = &sh->dev[i];
1497 if (i != pd_idx)
1498 xor_srcs[count++] = dev->page;
1499 }
1500 }
1501
91c00924
DW
1502 /* 1/ if we prexor'd then the dest is reused as a source
1503 * 2/ if we did not prexor then we are redoing the parity
1504 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1505 * for the synchronous xor case
1506 */
88ba2aa5 1507 flags = ASYNC_TX_ACK |
91c00924
DW
1508 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1509
1510 atomic_inc(&sh->count);
1511
ac6b53b6 1512 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
d6f38f31 1513 to_addr_conv(sh, percpu));
a08abd8c
DW
1514 if (unlikely(count == 1))
1515 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1516 else
1517 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
91c00924
DW
1518}
1519
ac6b53b6
DW
1520static void
1521ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1522 struct dma_async_tx_descriptor *tx)
1523{
1524 struct async_submit_ctl submit;
1525 struct page **blocks = percpu->scribble;
620125f2 1526 int count, i;
ac6b53b6
DW
1527
1528 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1529
620125f2
SL
1530 for (i = 0; i < sh->disks; i++) {
1531 if (sh->pd_idx == i || sh->qd_idx == i)
1532 continue;
1533 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1534 break;
1535 }
1536 if (i >= sh->disks) {
1537 atomic_inc(&sh->count);
620125f2
SL
1538 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1539 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1540 ops_complete_reconstruct(sh);
1541 return;
1542 }
1543
ac6b53b6
DW
1544 count = set_syndrome_sources(blocks, sh);
1545
1546 atomic_inc(&sh->count);
1547
1548 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1549 sh, to_addr_conv(sh, percpu));
1550 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
91c00924
DW
1551}
1552
1553static void ops_complete_check(void *stripe_head_ref)
1554{
1555 struct stripe_head *sh = stripe_head_ref;
91c00924 1556
e46b272b 1557 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1558 (unsigned long long)sh->sector);
1559
ecc65c9b 1560 sh->check_state = check_state_check_result;
91c00924
DW
1561 set_bit(STRIPE_HANDLE, &sh->state);
1562 release_stripe(sh);
1563}
1564
ac6b53b6 1565static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
91c00924 1566{
91c00924 1567 int disks = sh->disks;
ac6b53b6
DW
1568 int pd_idx = sh->pd_idx;
1569 int qd_idx = sh->qd_idx;
1570 struct page *xor_dest;
d6f38f31 1571 struct page **xor_srcs = percpu->scribble;
91c00924 1572 struct dma_async_tx_descriptor *tx;
a08abd8c 1573 struct async_submit_ctl submit;
ac6b53b6
DW
1574 int count;
1575 int i;
91c00924 1576
e46b272b 1577 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1578 (unsigned long long)sh->sector);
1579
ac6b53b6
DW
1580 count = 0;
1581 xor_dest = sh->dev[pd_idx].page;
1582 xor_srcs[count++] = xor_dest;
91c00924 1583 for (i = disks; i--; ) {
ac6b53b6
DW
1584 if (i == pd_idx || i == qd_idx)
1585 continue;
1586 xor_srcs[count++] = sh->dev[i].page;
91c00924
DW
1587 }
1588
d6f38f31
DW
1589 init_async_submit(&submit, 0, NULL, NULL, NULL,
1590 to_addr_conv(sh, percpu));
099f53cb 1591 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
a08abd8c 1592 &sh->ops.zero_sum_result, &submit);
91c00924 1593
91c00924 1594 atomic_inc(&sh->count);
a08abd8c
DW
1595 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1596 tx = async_trigger_callback(&submit);
91c00924
DW
1597}
1598
ac6b53b6
DW
1599static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1600{
1601 struct page **srcs = percpu->scribble;
1602 struct async_submit_ctl submit;
1603 int count;
1604
1605 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1606 (unsigned long long)sh->sector, checkp);
1607
1608 count = set_syndrome_sources(srcs, sh);
1609 if (!checkp)
1610 srcs[count] = NULL;
91c00924 1611
91c00924 1612 atomic_inc(&sh->count);
ac6b53b6
DW
1613 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1614 sh, to_addr_conv(sh, percpu));
1615 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1616 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
91c00924
DW
1617}
1618
51acbcec 1619static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
91c00924
DW
1620{
1621 int overlap_clear = 0, i, disks = sh->disks;
1622 struct dma_async_tx_descriptor *tx = NULL;
d1688a6d 1623 struct r5conf *conf = sh->raid_conf;
ac6b53b6 1624 int level = conf->level;
d6f38f31
DW
1625 struct raid5_percpu *percpu;
1626 unsigned long cpu;
91c00924 1627
d6f38f31
DW
1628 cpu = get_cpu();
1629 percpu = per_cpu_ptr(conf->percpu, cpu);
83de75cc 1630 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
91c00924
DW
1631 ops_run_biofill(sh);
1632 overlap_clear++;
1633 }
1634
7b3a871e 1635 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
ac6b53b6
DW
1636 if (level < 6)
1637 tx = ops_run_compute5(sh, percpu);
1638 else {
1639 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1640 tx = ops_run_compute6_1(sh, percpu);
1641 else
1642 tx = ops_run_compute6_2(sh, percpu);
1643 }
1644 /* terminate the chain if reconstruct is not set to be run */
1645 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
7b3a871e
DW
1646 async_tx_ack(tx);
1647 }
91c00924 1648
600aa109 1649 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
d6f38f31 1650 tx = ops_run_prexor(sh, percpu, tx);
91c00924 1651
600aa109 1652 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
d8ee0728 1653 tx = ops_run_biodrain(sh, tx);
91c00924
DW
1654 overlap_clear++;
1655 }
1656
ac6b53b6
DW
1657 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1658 if (level < 6)
1659 ops_run_reconstruct5(sh, percpu, tx);
1660 else
1661 ops_run_reconstruct6(sh, percpu, tx);
1662 }
91c00924 1663
ac6b53b6
DW
1664 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1665 if (sh->check_state == check_state_run)
1666 ops_run_check_p(sh, percpu);
1667 else if (sh->check_state == check_state_run_q)
1668 ops_run_check_pq(sh, percpu, 0);
1669 else if (sh->check_state == check_state_run_pq)
1670 ops_run_check_pq(sh, percpu, 1);
1671 else
1672 BUG();
1673 }
91c00924 1674
91c00924
DW
1675 if (overlap_clear)
1676 for (i = disks; i--; ) {
1677 struct r5dev *dev = &sh->dev[i];
1678 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1679 wake_up(&sh->raid_conf->wait_for_overlap);
1680 }
d6f38f31 1681 put_cpu();
91c00924
DW
1682}
1683
566c09c5 1684static int grow_one_stripe(struct r5conf *conf, int hash)
1da177e4
LT
1685{
1686 struct stripe_head *sh;
6ce32846 1687 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
3f294f4f
N
1688 if (!sh)
1689 return 0;
6ce32846 1690
3f294f4f 1691 sh->raid_conf = conf;
3f294f4f 1692
b17459c0
SL
1693 spin_lock_init(&sh->stripe_lock);
1694
e4e11e38
N
1695 if (grow_buffers(sh)) {
1696 shrink_buffers(sh);
3f294f4f
N
1697 kmem_cache_free(conf->slab_cache, sh);
1698 return 0;
1699 }
566c09c5 1700 sh->hash_lock_index = hash;
3f294f4f
N
1701 /* we just created an active stripe so... */
1702 atomic_set(&sh->count, 1);
1703 atomic_inc(&conf->active_stripes);
1704 INIT_LIST_HEAD(&sh->lru);
1705 release_stripe(sh);
1706 return 1;
1707}
1708
d1688a6d 1709static int grow_stripes(struct r5conf *conf, int num)
3f294f4f 1710{
e18b890b 1711 struct kmem_cache *sc;
5e5e3e78 1712 int devs = max(conf->raid_disks, conf->previous_raid_disks);
566c09c5 1713 int hash;
1da177e4 1714
f4be6b43
N
1715 if (conf->mddev->gendisk)
1716 sprintf(conf->cache_name[0],
1717 "raid%d-%s", conf->level, mdname(conf->mddev));
1718 else
1719 sprintf(conf->cache_name[0],
1720 "raid%d-%p", conf->level, conf->mddev);
1721 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1722
ad01c9e3
N
1723 conf->active_name = 0;
1724 sc = kmem_cache_create(conf->cache_name[conf->active_name],
1da177e4 1725 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
20c2df83 1726 0, 0, NULL);
1da177e4
LT
1727 if (!sc)
1728 return 1;
1729 conf->slab_cache = sc;
ad01c9e3 1730 conf->pool_size = devs;
566c09c5
SL
1731 hash = conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
1732 while (num--) {
1733 if (!grow_one_stripe(conf, hash))
1da177e4 1734 return 1;
566c09c5
SL
1735 conf->max_nr_stripes++;
1736 hash = (hash + 1) % NR_STRIPE_HASH_LOCKS;
1737 }
1da177e4
LT
1738 return 0;
1739}
29269553 1740
d6f38f31
DW
1741/**
1742 * scribble_len - return the required size of the scribble region
1743 * @num - total number of disks in the array
1744 *
1745 * The size must be enough to contain:
1746 * 1/ a struct page pointer for each device in the array +2
1747 * 2/ room to convert each entry in (1) to its corresponding dma
1748 * (dma_map_page()) or page (page_address()) address.
1749 *
1750 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1751 * calculate over all devices (not just the data blocks), using zeros in place
1752 * of the P and Q blocks.
1753 */
1754static size_t scribble_len(int num)
1755{
1756 size_t len;
1757
1758 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1759
1760 return len;
1761}
1762
d1688a6d 1763static int resize_stripes(struct r5conf *conf, int newsize)
ad01c9e3
N
1764{
1765 /* Make all the stripes able to hold 'newsize' devices.
1766 * New slots in each stripe get 'page' set to a new page.
1767 *
1768 * This happens in stages:
1769 * 1/ create a new kmem_cache and allocate the required number of
1770 * stripe_heads.
83f0d77a 1771 * 2/ gather all the old stripe_heads and transfer the pages across
ad01c9e3
N
1772 * to the new stripe_heads. This will have the side effect of
1773 * freezing the array as once all stripe_heads have been collected,
1774 * no IO will be possible. Old stripe heads are freed once their
1775 * pages have been transferred over, and the old kmem_cache is
1776 * freed when all stripes are done.
1777 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1778 * we simple return a failre status - no need to clean anything up.
1779 * 4/ allocate new pages for the new slots in the new stripe_heads.
1780 * If this fails, we don't bother trying the shrink the
1781 * stripe_heads down again, we just leave them as they are.
1782 * As each stripe_head is processed the new one is released into
1783 * active service.
1784 *
1785 * Once step2 is started, we cannot afford to wait for a write,
1786 * so we use GFP_NOIO allocations.
1787 */
1788 struct stripe_head *osh, *nsh;
1789 LIST_HEAD(newstripes);
1790 struct disk_info *ndisks;
d6f38f31 1791 unsigned long cpu;
b5470dc5 1792 int err;
e18b890b 1793 struct kmem_cache *sc;
ad01c9e3 1794 int i;
566c09c5 1795 int hash, cnt;
ad01c9e3
N
1796
1797 if (newsize <= conf->pool_size)
1798 return 0; /* never bother to shrink */
1799
b5470dc5
DW
1800 err = md_allow_write(conf->mddev);
1801 if (err)
1802 return err;
2a2275d6 1803
ad01c9e3
N
1804 /* Step 1 */
1805 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1806 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
20c2df83 1807 0, 0, NULL);
ad01c9e3
N
1808 if (!sc)
1809 return -ENOMEM;
1810
1811 for (i = conf->max_nr_stripes; i; i--) {
6ce32846 1812 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
ad01c9e3
N
1813 if (!nsh)
1814 break;
1815
ad01c9e3 1816 nsh->raid_conf = conf;
cb13ff69 1817 spin_lock_init(&nsh->stripe_lock);
ad01c9e3
N
1818
1819 list_add(&nsh->lru, &newstripes);
1820 }
1821 if (i) {
1822 /* didn't get enough, give up */
1823 while (!list_empty(&newstripes)) {
1824 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1825 list_del(&nsh->lru);
1826 kmem_cache_free(sc, nsh);
1827 }
1828 kmem_cache_destroy(sc);
1829 return -ENOMEM;
1830 }
1831 /* Step 2 - Must use GFP_NOIO now.
1832 * OK, we have enough stripes, start collecting inactive
1833 * stripes and copying them over
1834 */
566c09c5
SL
1835 hash = 0;
1836 cnt = 0;
ad01c9e3 1837 list_for_each_entry(nsh, &newstripes, lru) {
566c09c5
SL
1838 lock_device_hash_lock(conf, hash);
1839 wait_event_cmd(conf->wait_for_stripe,
1840 !list_empty(conf->inactive_list + hash),
1841 unlock_device_hash_lock(conf, hash),
1842 lock_device_hash_lock(conf, hash));
1843 osh = get_free_stripe(conf, hash);
1844 unlock_device_hash_lock(conf, hash);
ad01c9e3
N
1845 atomic_set(&nsh->count, 1);
1846 for(i=0; i<conf->pool_size; i++)
1847 nsh->dev[i].page = osh->dev[i].page;
1848 for( ; i<newsize; i++)
1849 nsh->dev[i].page = NULL;
566c09c5 1850 nsh->hash_lock_index = hash;
ad01c9e3 1851 kmem_cache_free(conf->slab_cache, osh);
566c09c5
SL
1852 cnt++;
1853 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
1854 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
1855 hash++;
1856 cnt = 0;
1857 }
ad01c9e3
N
1858 }
1859 kmem_cache_destroy(conf->slab_cache);
1860
1861 /* Step 3.
1862 * At this point, we are holding all the stripes so the array
1863 * is completely stalled, so now is a good time to resize
d6f38f31 1864 * conf->disks and the scribble region
ad01c9e3
N
1865 */
1866 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1867 if (ndisks) {
1868 for (i=0; i<conf->raid_disks; i++)
1869 ndisks[i] = conf->disks[i];
1870 kfree(conf->disks);
1871 conf->disks = ndisks;
1872 } else
1873 err = -ENOMEM;
1874
d6f38f31
DW
1875 get_online_cpus();
1876 conf->scribble_len = scribble_len(newsize);
1877 for_each_present_cpu(cpu) {
1878 struct raid5_percpu *percpu;
1879 void *scribble;
1880
1881 percpu = per_cpu_ptr(conf->percpu, cpu);
1882 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1883
1884 if (scribble) {
1885 kfree(percpu->scribble);
1886 percpu->scribble = scribble;
1887 } else {
1888 err = -ENOMEM;
1889 break;
1890 }
1891 }
1892 put_online_cpus();
1893
ad01c9e3
N
1894 /* Step 4, return new stripes to service */
1895 while(!list_empty(&newstripes)) {
1896 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1897 list_del_init(&nsh->lru);
d6f38f31 1898
ad01c9e3
N
1899 for (i=conf->raid_disks; i < newsize; i++)
1900 if (nsh->dev[i].page == NULL) {
1901 struct page *p = alloc_page(GFP_NOIO);
1902 nsh->dev[i].page = p;
1903 if (!p)
1904 err = -ENOMEM;
1905 }
1906 release_stripe(nsh);
1907 }
1908 /* critical section pass, GFP_NOIO no longer needed */
1909
1910 conf->slab_cache = sc;
1911 conf->active_name = 1-conf->active_name;
1912 conf->pool_size = newsize;
1913 return err;
1914}
1da177e4 1915
566c09c5 1916static int drop_one_stripe(struct r5conf *conf, int hash)
1da177e4
LT
1917{
1918 struct stripe_head *sh;
1919
566c09c5
SL
1920 spin_lock_irq(conf->hash_locks + hash);
1921 sh = get_free_stripe(conf, hash);
1922 spin_unlock_irq(conf->hash_locks + hash);
3f294f4f
N
1923 if (!sh)
1924 return 0;
78bafebd 1925 BUG_ON(atomic_read(&sh->count));
e4e11e38 1926 shrink_buffers(sh);
3f294f4f
N
1927 kmem_cache_free(conf->slab_cache, sh);
1928 atomic_dec(&conf->active_stripes);
1929 return 1;
1930}
1931
d1688a6d 1932static void shrink_stripes(struct r5conf *conf)
3f294f4f 1933{
566c09c5
SL
1934 int hash;
1935 for (hash = 0; hash < NR_STRIPE_HASH_LOCKS; hash++)
1936 while (drop_one_stripe(conf, hash))
1937 ;
3f294f4f 1938
29fc7e3e
N
1939 if (conf->slab_cache)
1940 kmem_cache_destroy(conf->slab_cache);
1da177e4
LT
1941 conf->slab_cache = NULL;
1942}
1943
6712ecf8 1944static void raid5_end_read_request(struct bio * bi, int error)
1da177e4 1945{
99c0fb5f 1946 struct stripe_head *sh = bi->bi_private;
d1688a6d 1947 struct r5conf *conf = sh->raid_conf;
7ecaa1e6 1948 int disks = sh->disks, i;
1da177e4 1949 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
d6950432 1950 char b[BDEVNAME_SIZE];
dd054fce 1951 struct md_rdev *rdev = NULL;
05616be5 1952 sector_t s;
1da177e4
LT
1953
1954 for (i=0 ; i<disks; i++)
1955 if (bi == &sh->dev[i].req)
1956 break;
1957
45b4233c
DW
1958 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1959 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1da177e4
LT
1960 uptodate);
1961 if (i == disks) {
1962 BUG();
6712ecf8 1963 return;
1da177e4 1964 }
14a75d3e 1965 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
dd054fce
N
1966 /* If replacement finished while this request was outstanding,
1967 * 'replacement' might be NULL already.
1968 * In that case it moved down to 'rdev'.
1969 * rdev is not removed until all requests are finished.
1970 */
14a75d3e 1971 rdev = conf->disks[i].replacement;
dd054fce 1972 if (!rdev)
14a75d3e 1973 rdev = conf->disks[i].rdev;
1da177e4 1974
05616be5
N
1975 if (use_new_offset(conf, sh))
1976 s = sh->sector + rdev->new_data_offset;
1977 else
1978 s = sh->sector + rdev->data_offset;
1da177e4 1979 if (uptodate) {
1da177e4 1980 set_bit(R5_UPTODATE, &sh->dev[i].flags);
4e5314b5 1981 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
14a75d3e
N
1982 /* Note that this cannot happen on a
1983 * replacement device. We just fail those on
1984 * any error
1985 */
8bda470e
CD
1986 printk_ratelimited(
1987 KERN_INFO
1988 "md/raid:%s: read error corrected"
1989 " (%lu sectors at %llu on %s)\n",
1990 mdname(conf->mddev), STRIPE_SECTORS,
05616be5 1991 (unsigned long long)s,
8bda470e 1992 bdevname(rdev->bdev, b));
ddd5115f 1993 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
4e5314b5
N
1994 clear_bit(R5_ReadError, &sh->dev[i].flags);
1995 clear_bit(R5_ReWrite, &sh->dev[i].flags);
3f9e7c14 1996 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1997 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1998
14a75d3e
N
1999 if (atomic_read(&rdev->read_errors))
2000 atomic_set(&rdev->read_errors, 0);
1da177e4 2001 } else {
14a75d3e 2002 const char *bdn = bdevname(rdev->bdev, b);
ba22dcbf 2003 int retry = 0;
2e8ac303 2004 int set_bad = 0;
d6950432 2005
1da177e4 2006 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
d6950432 2007 atomic_inc(&rdev->read_errors);
14a75d3e
N
2008 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
2009 printk_ratelimited(
2010 KERN_WARNING
2011 "md/raid:%s: read error on replacement device "
2012 "(sector %llu on %s).\n",
2013 mdname(conf->mddev),
05616be5 2014 (unsigned long long)s,
14a75d3e 2015 bdn);
2e8ac303 2016 else if (conf->mddev->degraded >= conf->max_degraded) {
2017 set_bad = 1;
8bda470e
CD
2018 printk_ratelimited(
2019 KERN_WARNING
2020 "md/raid:%s: read error not correctable "
2021 "(sector %llu on %s).\n",
2022 mdname(conf->mddev),
05616be5 2023 (unsigned long long)s,
8bda470e 2024 bdn);
2e8ac303 2025 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
4e5314b5 2026 /* Oh, no!!! */
2e8ac303 2027 set_bad = 1;
8bda470e
CD
2028 printk_ratelimited(
2029 KERN_WARNING
2030 "md/raid:%s: read error NOT corrected!! "
2031 "(sector %llu on %s).\n",
2032 mdname(conf->mddev),
05616be5 2033 (unsigned long long)s,
8bda470e 2034 bdn);
2e8ac303 2035 } else if (atomic_read(&rdev->read_errors)
ba22dcbf 2036 > conf->max_nr_stripes)
14f8d26b 2037 printk(KERN_WARNING
0c55e022 2038 "md/raid:%s: Too many read errors, failing device %s.\n",
d6950432 2039 mdname(conf->mddev), bdn);
ba22dcbf
N
2040 else
2041 retry = 1;
edfa1f65
BY
2042 if (set_bad && test_bit(In_sync, &rdev->flags)
2043 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2044 retry = 1;
ba22dcbf 2045 if (retry)
3f9e7c14 2046 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2047 set_bit(R5_ReadError, &sh->dev[i].flags);
2048 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2049 } else
2050 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
ba22dcbf 2051 else {
4e5314b5
N
2052 clear_bit(R5_ReadError, &sh->dev[i].flags);
2053 clear_bit(R5_ReWrite, &sh->dev[i].flags);
2e8ac303 2054 if (!(set_bad
2055 && test_bit(In_sync, &rdev->flags)
2056 && rdev_set_badblocks(
2057 rdev, sh->sector, STRIPE_SECTORS, 0)))
2058 md_error(conf->mddev, rdev);
ba22dcbf 2059 }
1da177e4 2060 }
14a75d3e 2061 rdev_dec_pending(rdev, conf->mddev);
1da177e4
LT
2062 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2063 set_bit(STRIPE_HANDLE, &sh->state);
2064 release_stripe(sh);
1da177e4
LT
2065}
2066
d710e138 2067static void raid5_end_write_request(struct bio *bi, int error)
1da177e4 2068{
99c0fb5f 2069 struct stripe_head *sh = bi->bi_private;
d1688a6d 2070 struct r5conf *conf = sh->raid_conf;
7ecaa1e6 2071 int disks = sh->disks, i;
977df362 2072 struct md_rdev *uninitialized_var(rdev);
1da177e4 2073 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
b84db560
N
2074 sector_t first_bad;
2075 int bad_sectors;
977df362 2076 int replacement = 0;
1da177e4 2077
977df362
N
2078 for (i = 0 ; i < disks; i++) {
2079 if (bi == &sh->dev[i].req) {
2080 rdev = conf->disks[i].rdev;
1da177e4 2081 break;
977df362
N
2082 }
2083 if (bi == &sh->dev[i].rreq) {
2084 rdev = conf->disks[i].replacement;
dd054fce
N
2085 if (rdev)
2086 replacement = 1;
2087 else
2088 /* rdev was removed and 'replacement'
2089 * replaced it. rdev is not removed
2090 * until all requests are finished.
2091 */
2092 rdev = conf->disks[i].rdev;
977df362
N
2093 break;
2094 }
2095 }
45b4233c 2096 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
1da177e4
LT
2097 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
2098 uptodate);
2099 if (i == disks) {
2100 BUG();
6712ecf8 2101 return;
1da177e4
LT
2102 }
2103
977df362
N
2104 if (replacement) {
2105 if (!uptodate)
2106 md_error(conf->mddev, rdev);
2107 else if (is_badblock(rdev, sh->sector,
2108 STRIPE_SECTORS,
2109 &first_bad, &bad_sectors))
2110 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2111 } else {
2112 if (!uptodate) {
2113 set_bit(WriteErrorSeen, &rdev->flags);
2114 set_bit(R5_WriteError, &sh->dev[i].flags);
3a6de292
N
2115 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2116 set_bit(MD_RECOVERY_NEEDED,
2117 &rdev->mddev->recovery);
977df362
N
2118 } else if (is_badblock(rdev, sh->sector,
2119 STRIPE_SECTORS,
c0b32972 2120 &first_bad, &bad_sectors)) {
977df362 2121 set_bit(R5_MadeGood, &sh->dev[i].flags);
c0b32972
N
2122 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2123 /* That was a successful write so make
2124 * sure it looks like we already did
2125 * a re-write.
2126 */
2127 set_bit(R5_ReWrite, &sh->dev[i].flags);
2128 }
977df362
N
2129 }
2130 rdev_dec_pending(rdev, conf->mddev);
1da177e4 2131
977df362
N
2132 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2133 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1da177e4 2134 set_bit(STRIPE_HANDLE, &sh->state);
c04be0aa 2135 release_stripe(sh);
1da177e4
LT
2136}
2137
784052ec 2138static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
1da177e4 2139
784052ec 2140static void raid5_build_block(struct stripe_head *sh, int i, int previous)
1da177e4
LT
2141{
2142 struct r5dev *dev = &sh->dev[i];
2143
2144 bio_init(&dev->req);
2145 dev->req.bi_io_vec = &dev->vec;
2146 dev->req.bi_vcnt++;
2147 dev->req.bi_max_vecs++;
1da177e4 2148 dev->req.bi_private = sh;
995c4275 2149 dev->vec.bv_page = dev->page;
1da177e4 2150
977df362
N
2151 bio_init(&dev->rreq);
2152 dev->rreq.bi_io_vec = &dev->rvec;
2153 dev->rreq.bi_vcnt++;
2154 dev->rreq.bi_max_vecs++;
2155 dev->rreq.bi_private = sh;
2156 dev->rvec.bv_page = dev->page;
2157
1da177e4 2158 dev->flags = 0;
784052ec 2159 dev->sector = compute_blocknr(sh, i, previous);
1da177e4
LT
2160}
2161
fd01b88c 2162static void error(struct mddev *mddev, struct md_rdev *rdev)
1da177e4
LT
2163{
2164 char b[BDEVNAME_SIZE];
d1688a6d 2165 struct r5conf *conf = mddev->private;
908f4fbd 2166 unsigned long flags;
0c55e022 2167 pr_debug("raid456: error called\n");
1da177e4 2168
908f4fbd
N
2169 spin_lock_irqsave(&conf->device_lock, flags);
2170 clear_bit(In_sync, &rdev->flags);
2171 mddev->degraded = calc_degraded(conf);
2172 spin_unlock_irqrestore(&conf->device_lock, flags);
2173 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2174
de393cde 2175 set_bit(Blocked, &rdev->flags);
6f8d0c77
N
2176 set_bit(Faulty, &rdev->flags);
2177 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2178 printk(KERN_ALERT
2179 "md/raid:%s: Disk failure on %s, disabling device.\n"
2180 "md/raid:%s: Operation continuing on %d devices.\n",
2181 mdname(mddev),
2182 bdevname(rdev->bdev, b),
2183 mdname(mddev),
2184 conf->raid_disks - mddev->degraded);
16a53ecc 2185}
1da177e4
LT
2186
2187/*
2188 * Input: a 'big' sector number,
2189 * Output: index of the data and parity disk, and the sector # in them.
2190 */
d1688a6d 2191static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
911d4ee8
N
2192 int previous, int *dd_idx,
2193 struct stripe_head *sh)
1da177e4 2194{
6e3b96ed 2195 sector_t stripe, stripe2;
35f2a591 2196 sector_t chunk_number;
1da177e4 2197 unsigned int chunk_offset;
911d4ee8 2198 int pd_idx, qd_idx;
67cc2b81 2199 int ddf_layout = 0;
1da177e4 2200 sector_t new_sector;
e183eaed
N
2201 int algorithm = previous ? conf->prev_algo
2202 : conf->algorithm;
09c9e5fa
AN
2203 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2204 : conf->chunk_sectors;
112bf897
N
2205 int raid_disks = previous ? conf->previous_raid_disks
2206 : conf->raid_disks;
2207 int data_disks = raid_disks - conf->max_degraded;
1da177e4
LT
2208
2209 /* First compute the information on this sector */
2210
2211 /*
2212 * Compute the chunk number and the sector offset inside the chunk
2213 */
2214 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2215 chunk_number = r_sector;
1da177e4
LT
2216
2217 /*
2218 * Compute the stripe number
2219 */
35f2a591
N
2220 stripe = chunk_number;
2221 *dd_idx = sector_div(stripe, data_disks);
6e3b96ed 2222 stripe2 = stripe;
1da177e4
LT
2223 /*
2224 * Select the parity disk based on the user selected algorithm.
2225 */
84789554 2226 pd_idx = qd_idx = -1;
16a53ecc
N
2227 switch(conf->level) {
2228 case 4:
911d4ee8 2229 pd_idx = data_disks;
16a53ecc
N
2230 break;
2231 case 5:
e183eaed 2232 switch (algorithm) {
1da177e4 2233 case ALGORITHM_LEFT_ASYMMETRIC:
6e3b96ed 2234 pd_idx = data_disks - sector_div(stripe2, raid_disks);
911d4ee8 2235 if (*dd_idx >= pd_idx)
1da177e4
LT
2236 (*dd_idx)++;
2237 break;
2238 case ALGORITHM_RIGHT_ASYMMETRIC:
6e3b96ed 2239 pd_idx = sector_div(stripe2, raid_disks);
911d4ee8 2240 if (*dd_idx >= pd_idx)
1da177e4
LT
2241 (*dd_idx)++;
2242 break;
2243 case ALGORITHM_LEFT_SYMMETRIC:
6e3b96ed 2244 pd_idx = data_disks - sector_div(stripe2, raid_disks);
911d4ee8 2245 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
1da177e4
LT
2246 break;
2247 case ALGORITHM_RIGHT_SYMMETRIC:
6e3b96ed 2248 pd_idx = sector_div(stripe2, raid_disks);
911d4ee8 2249 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
1da177e4 2250 break;
99c0fb5f
N
2251 case ALGORITHM_PARITY_0:
2252 pd_idx = 0;
2253 (*dd_idx)++;
2254 break;
2255 case ALGORITHM_PARITY_N:
2256 pd_idx = data_disks;
2257 break;
1da177e4 2258 default:
99c0fb5f 2259 BUG();
16a53ecc
N
2260 }
2261 break;
2262 case 6:
2263
e183eaed 2264 switch (algorithm) {
16a53ecc 2265 case ALGORITHM_LEFT_ASYMMETRIC:
6e3b96ed 2266 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
911d4ee8
N
2267 qd_idx = pd_idx + 1;
2268 if (pd_idx == raid_disks-1) {
99c0fb5f 2269 (*dd_idx)++; /* Q D D D P */
911d4ee8
N
2270 qd_idx = 0;
2271 } else if (*dd_idx >= pd_idx)
16a53ecc
N
2272 (*dd_idx) += 2; /* D D P Q D */
2273 break;
2274 case ALGORITHM_RIGHT_ASYMMETRIC:
6e3b96ed 2275 pd_idx = sector_div(stripe2, raid_disks);
911d4ee8
N
2276 qd_idx = pd_idx + 1;
2277 if (pd_idx == raid_disks-1) {
99c0fb5f 2278 (*dd_idx)++; /* Q D D D P */
911d4ee8
N
2279 qd_idx = 0;
2280 } else if (*dd_idx >= pd_idx)
16a53ecc
N
2281 (*dd_idx) += 2; /* D D P Q D */
2282 break;
2283 case ALGORITHM_LEFT_SYMMETRIC:
6e3b96ed 2284 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
911d4ee8
N
2285 qd_idx = (pd_idx + 1) % raid_disks;
2286 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
16a53ecc
N
2287 break;
2288 case ALGORITHM_RIGHT_SYMMETRIC:
6e3b96ed 2289 pd_idx = sector_div(stripe2, raid_disks);
911d4ee8
N
2290 qd_idx = (pd_idx + 1) % raid_disks;
2291 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
16a53ecc 2292 break;
99c0fb5f
N
2293
2294 case ALGORITHM_PARITY_0:
2295 pd_idx = 0;
2296 qd_idx = 1;
2297 (*dd_idx) += 2;
2298 break;
2299 case ALGORITHM_PARITY_N:
2300 pd_idx = data_disks;
2301 qd_idx = data_disks + 1;
2302 break;
2303
2304 case ALGORITHM_ROTATING_ZERO_RESTART:
2305 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2306 * of blocks for computing Q is different.
2307 */
6e3b96ed 2308 pd_idx = sector_div(stripe2, raid_disks);
99c0fb5f
N
2309 qd_idx = pd_idx + 1;
2310 if (pd_idx == raid_disks-1) {
2311 (*dd_idx)++; /* Q D D D P */
2312 qd_idx = 0;
2313 } else if (*dd_idx >= pd_idx)
2314 (*dd_idx) += 2; /* D D P Q D */
67cc2b81 2315 ddf_layout = 1;
99c0fb5f
N
2316 break;
2317
2318 case ALGORITHM_ROTATING_N_RESTART:
2319 /* Same a left_asymmetric, by first stripe is
2320 * D D D P Q rather than
2321 * Q D D D P
2322 */
6e3b96ed
N
2323 stripe2 += 1;
2324 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
99c0fb5f
N
2325 qd_idx = pd_idx + 1;
2326 if (pd_idx == raid_disks-1) {
2327 (*dd_idx)++; /* Q D D D P */
2328 qd_idx = 0;
2329 } else if (*dd_idx >= pd_idx)
2330 (*dd_idx) += 2; /* D D P Q D */
67cc2b81 2331 ddf_layout = 1;
99c0fb5f
N
2332 break;
2333
2334 case ALGORITHM_ROTATING_N_CONTINUE:
2335 /* Same as left_symmetric but Q is before P */
6e3b96ed 2336 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
99c0fb5f
N
2337 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2338 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
67cc2b81 2339 ddf_layout = 1;
99c0fb5f
N
2340 break;
2341
2342 case ALGORITHM_LEFT_ASYMMETRIC_6:
2343 /* RAID5 left_asymmetric, with Q on last device */
6e3b96ed 2344 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
99c0fb5f
N
2345 if (*dd_idx >= pd_idx)
2346 (*dd_idx)++;
2347 qd_idx = raid_disks - 1;
2348 break;
2349
2350 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6e3b96ed 2351 pd_idx = sector_div(stripe2, raid_disks-1);
99c0fb5f
N
2352 if (*dd_idx >= pd_idx)
2353 (*dd_idx)++;
2354 qd_idx = raid_disks - 1;
2355 break;
2356
2357 case ALGORITHM_LEFT_SYMMETRIC_6:
6e3b96ed 2358 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
99c0fb5f
N
2359 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2360 qd_idx = raid_disks - 1;
2361 break;
2362
2363 case ALGORITHM_RIGHT_SYMMETRIC_6:
6e3b96ed 2364 pd_idx = sector_div(stripe2, raid_disks-1);
99c0fb5f
N
2365 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2366 qd_idx = raid_disks - 1;
2367 break;
2368
2369 case ALGORITHM_PARITY_0_6:
2370 pd_idx = 0;
2371 (*dd_idx)++;
2372 qd_idx = raid_disks - 1;
2373 break;
2374
16a53ecc 2375 default:
99c0fb5f 2376 BUG();
16a53ecc
N
2377 }
2378 break;
1da177e4
LT
2379 }
2380
911d4ee8
N
2381 if (sh) {
2382 sh->pd_idx = pd_idx;
2383 sh->qd_idx = qd_idx;
67cc2b81 2384 sh->ddf_layout = ddf_layout;
911d4ee8 2385 }
1da177e4
LT
2386 /*
2387 * Finally, compute the new sector number
2388 */
2389 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2390 return new_sector;
2391}
2392
2393
784052ec 2394static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
1da177e4 2395{
d1688a6d 2396 struct r5conf *conf = sh->raid_conf;
b875e531
N
2397 int raid_disks = sh->disks;
2398 int data_disks = raid_disks - conf->max_degraded;
1da177e4 2399 sector_t new_sector = sh->sector, check;
09c9e5fa
AN
2400 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2401 : conf->chunk_sectors;
e183eaed
N
2402 int algorithm = previous ? conf->prev_algo
2403 : conf->algorithm;
1da177e4
LT
2404 sector_t stripe;
2405 int chunk_offset;
35f2a591
N
2406 sector_t chunk_number;
2407 int dummy1, dd_idx = i;
1da177e4 2408 sector_t r_sector;
911d4ee8 2409 struct stripe_head sh2;
1da177e4 2410
16a53ecc 2411
1da177e4
LT
2412 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2413 stripe = new_sector;
1da177e4 2414
16a53ecc
N
2415 if (i == sh->pd_idx)
2416 return 0;
2417 switch(conf->level) {
2418 case 4: break;
2419 case 5:
e183eaed 2420 switch (algorithm) {
1da177e4
LT
2421 case ALGORITHM_LEFT_ASYMMETRIC:
2422 case ALGORITHM_RIGHT_ASYMMETRIC:
2423 if (i > sh->pd_idx)
2424 i--;
2425 break;
2426 case ALGORITHM_LEFT_SYMMETRIC:
2427 case ALGORITHM_RIGHT_SYMMETRIC:
2428 if (i < sh->pd_idx)
2429 i += raid_disks;
2430 i -= (sh->pd_idx + 1);
2431 break;
99c0fb5f
N
2432 case ALGORITHM_PARITY_0:
2433 i -= 1;
2434 break;
2435 case ALGORITHM_PARITY_N:
2436 break;
1da177e4 2437 default:
99c0fb5f 2438 BUG();
16a53ecc
N
2439 }
2440 break;
2441 case 6:
d0dabf7e 2442 if (i == sh->qd_idx)
16a53ecc 2443 return 0; /* It is the Q disk */
e183eaed 2444 switch (algorithm) {
16a53ecc
N
2445 case ALGORITHM_LEFT_ASYMMETRIC:
2446 case ALGORITHM_RIGHT_ASYMMETRIC:
99c0fb5f
N
2447 case ALGORITHM_ROTATING_ZERO_RESTART:
2448 case ALGORITHM_ROTATING_N_RESTART:
2449 if (sh->pd_idx == raid_disks-1)
2450 i--; /* Q D D D P */
16a53ecc
N
2451 else if (i > sh->pd_idx)
2452 i -= 2; /* D D P Q D */
2453 break;
2454 case ALGORITHM_LEFT_SYMMETRIC:
2455 case ALGORITHM_RIGHT_SYMMETRIC:
2456 if (sh->pd_idx == raid_disks-1)
2457 i--; /* Q D D D P */
2458 else {
2459 /* D D P Q D */
2460 if (i < sh->pd_idx)
2461 i += raid_disks;
2462 i -= (sh->pd_idx + 2);
2463 }
2464 break;
99c0fb5f
N
2465 case ALGORITHM_PARITY_0:
2466 i -= 2;
2467 break;
2468 case ALGORITHM_PARITY_N:
2469 break;
2470 case ALGORITHM_ROTATING_N_CONTINUE:
e4424fee 2471 /* Like left_symmetric, but P is before Q */
99c0fb5f
N
2472 if (sh->pd_idx == 0)
2473 i--; /* P D D D Q */
e4424fee
N
2474 else {
2475 /* D D Q P D */
2476 if (i < sh->pd_idx)
2477 i += raid_disks;
2478 i -= (sh->pd_idx + 1);
2479 }
99c0fb5f
N
2480 break;
2481 case ALGORITHM_LEFT_ASYMMETRIC_6:
2482 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2483 if (i > sh->pd_idx)
2484 i--;
2485 break;
2486 case ALGORITHM_LEFT_SYMMETRIC_6:
2487 case ALGORITHM_RIGHT_SYMMETRIC_6:
2488 if (i < sh->pd_idx)
2489 i += data_disks + 1;
2490 i -= (sh->pd_idx + 1);
2491 break;
2492 case ALGORITHM_PARITY_0_6:
2493 i -= 1;
2494 break;
16a53ecc 2495 default:
99c0fb5f 2496 BUG();
16a53ecc
N
2497 }
2498 break;
1da177e4
LT
2499 }
2500
2501 chunk_number = stripe * data_disks + i;
35f2a591 2502 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
1da177e4 2503
112bf897 2504 check = raid5_compute_sector(conf, r_sector,
784052ec 2505 previous, &dummy1, &sh2);
911d4ee8
N
2506 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2507 || sh2.qd_idx != sh->qd_idx) {
0c55e022
N
2508 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2509 mdname(conf->mddev));
1da177e4
LT
2510 return 0;
2511 }
2512 return r_sector;
2513}
2514
2515
600aa109 2516static void
c0f7bddb 2517schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
600aa109 2518 int rcw, int expand)
e33129d8
DW
2519{
2520 int i, pd_idx = sh->pd_idx, disks = sh->disks;
d1688a6d 2521 struct r5conf *conf = sh->raid_conf;
c0f7bddb 2522 int level = conf->level;
e33129d8
DW
2523
2524 if (rcw) {
e33129d8
DW
2525
2526 for (i = disks; i--; ) {
2527 struct r5dev *dev = &sh->dev[i];
2528
2529 if (dev->towrite) {
2530 set_bit(R5_LOCKED, &dev->flags);
d8ee0728 2531 set_bit(R5_Wantdrain, &dev->flags);
e33129d8
DW
2532 if (!expand)
2533 clear_bit(R5_UPTODATE, &dev->flags);
600aa109 2534 s->locked++;
e33129d8
DW
2535 }
2536 }
ce7d363a
N
2537 /* if we are not expanding this is a proper write request, and
2538 * there will be bios with new data to be drained into the
2539 * stripe cache
2540 */
2541 if (!expand) {
2542 if (!s->locked)
2543 /* False alarm, nothing to do */
2544 return;
2545 sh->reconstruct_state = reconstruct_state_drain_run;
2546 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2547 } else
2548 sh->reconstruct_state = reconstruct_state_run;
2549
2550 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2551
c0f7bddb 2552 if (s->locked + conf->max_degraded == disks)
8b3e6cdc 2553 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
c0f7bddb 2554 atomic_inc(&conf->pending_full_writes);
e33129d8 2555 } else {
c0f7bddb 2556 BUG_ON(level == 6);
e33129d8
DW
2557 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2558 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2559
e33129d8
DW
2560 for (i = disks; i--; ) {
2561 struct r5dev *dev = &sh->dev[i];
2562 if (i == pd_idx)
2563 continue;
2564
e33129d8
DW
2565 if (dev->towrite &&
2566 (test_bit(R5_UPTODATE, &dev->flags) ||
d8ee0728
DW
2567 test_bit(R5_Wantcompute, &dev->flags))) {
2568 set_bit(R5_Wantdrain, &dev->flags);
e33129d8
DW
2569 set_bit(R5_LOCKED, &dev->flags);
2570 clear_bit(R5_UPTODATE, &dev->flags);
600aa109 2571 s->locked++;
e33129d8
DW
2572 }
2573 }
ce7d363a
N
2574 if (!s->locked)
2575 /* False alarm - nothing to do */
2576 return;
2577 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2578 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2579 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2580 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
e33129d8
DW
2581 }
2582
c0f7bddb 2583 /* keep the parity disk(s) locked while asynchronous operations
e33129d8
DW
2584 * are in flight
2585 */
2586 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2587 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
600aa109 2588 s->locked++;
e33129d8 2589
c0f7bddb
YT
2590 if (level == 6) {
2591 int qd_idx = sh->qd_idx;
2592 struct r5dev *dev = &sh->dev[qd_idx];
2593
2594 set_bit(R5_LOCKED, &dev->flags);
2595 clear_bit(R5_UPTODATE, &dev->flags);
2596 s->locked++;
2597 }
2598
600aa109 2599 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
e46b272b 2600 __func__, (unsigned long long)sh->sector,
600aa109 2601 s->locked, s->ops_request);
e33129d8 2602}
16a53ecc 2603
1da177e4
LT
2604/*
2605 * Each stripe/dev can have one or more bion attached.
16a53ecc 2606 * toread/towrite point to the first in a chain.
1da177e4
LT
2607 * The bi_next chain must be in order.
2608 */
2609static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2610{
2611 struct bio **bip;
d1688a6d 2612 struct r5conf *conf = sh->raid_conf;
72626685 2613 int firstwrite=0;
1da177e4 2614
cbe47ec5 2615 pr_debug("adding bi b#%llu to stripe s#%llu\n",
1da177e4
LT
2616 (unsigned long long)bi->bi_sector,
2617 (unsigned long long)sh->sector);
2618
b17459c0
SL
2619 /*
2620 * If several bio share a stripe. The bio bi_phys_segments acts as a
2621 * reference count to avoid race. The reference count should already be
2622 * increased before this function is called (for example, in
2623 * make_request()), so other bio sharing this stripe will not free the
2624 * stripe. If a stripe is owned by one stripe, the stripe lock will
2625 * protect it.
2626 */
2627 spin_lock_irq(&sh->stripe_lock);
72626685 2628 if (forwrite) {
1da177e4 2629 bip = &sh->dev[dd_idx].towrite;
7eaf7e8e 2630 if (*bip == NULL)
72626685
N
2631 firstwrite = 1;
2632 } else
1da177e4
LT
2633 bip = &sh->dev[dd_idx].toread;
2634 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
f73a1c7d 2635 if (bio_end_sector(*bip) > bi->bi_sector)
1da177e4
LT
2636 goto overlap;
2637 bip = & (*bip)->bi_next;
2638 }
f73a1c7d 2639 if (*bip && (*bip)->bi_sector < bio_end_sector(bi))
1da177e4
LT
2640 goto overlap;
2641
78bafebd 2642 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
1da177e4
LT
2643 if (*bip)
2644 bi->bi_next = *bip;
2645 *bip = bi;
e7836bd6 2646 raid5_inc_bi_active_stripes(bi);
72626685 2647
1da177e4
LT
2648 if (forwrite) {
2649 /* check if page is covered */
2650 sector_t sector = sh->dev[dd_idx].sector;
2651 for (bi=sh->dev[dd_idx].towrite;
2652 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2653 bi && bi->bi_sector <= sector;
2654 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
f73a1c7d
KO
2655 if (bio_end_sector(bi) >= sector)
2656 sector = bio_end_sector(bi);
1da177e4
LT
2657 }
2658 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2659 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2660 }
cbe47ec5
N
2661
2662 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2663 (unsigned long long)(*bip)->bi_sector,
2664 (unsigned long long)sh->sector, dd_idx);
b97390ae 2665 spin_unlock_irq(&sh->stripe_lock);
cbe47ec5
N
2666
2667 if (conf->mddev->bitmap && firstwrite) {
2668 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2669 STRIPE_SECTORS, 0);
2670 sh->bm_seq = conf->seq_flush+1;
2671 set_bit(STRIPE_BIT_DELAY, &sh->state);
2672 }
1da177e4
LT
2673 return 1;
2674
2675 overlap:
2676 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
b17459c0 2677 spin_unlock_irq(&sh->stripe_lock);
1da177e4
LT
2678 return 0;
2679}
2680
d1688a6d 2681static void end_reshape(struct r5conf *conf);
29269553 2682
d1688a6d 2683static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
911d4ee8 2684 struct stripe_head *sh)
ccfcc3c1 2685{
784052ec 2686 int sectors_per_chunk =
09c9e5fa 2687 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
911d4ee8 2688 int dd_idx;
2d2063ce 2689 int chunk_offset = sector_div(stripe, sectors_per_chunk);
112bf897 2690 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
2d2063ce 2691
112bf897
N
2692 raid5_compute_sector(conf,
2693 stripe * (disks - conf->max_degraded)
b875e531 2694 *sectors_per_chunk + chunk_offset,
112bf897 2695 previous,
911d4ee8 2696 &dd_idx, sh);
ccfcc3c1
N
2697}
2698
a4456856 2699static void
d1688a6d 2700handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
a4456856
DW
2701 struct stripe_head_state *s, int disks,
2702 struct bio **return_bi)
2703{
2704 int i;
2705 for (i = disks; i--; ) {
2706 struct bio *bi;
2707 int bitmap_end = 0;
2708
2709 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
3cb03002 2710 struct md_rdev *rdev;
a4456856
DW
2711 rcu_read_lock();
2712 rdev = rcu_dereference(conf->disks[i].rdev);
2713 if (rdev && test_bit(In_sync, &rdev->flags))
7f0da59b
N
2714 atomic_inc(&rdev->nr_pending);
2715 else
2716 rdev = NULL;
a4456856 2717 rcu_read_unlock();
7f0da59b
N
2718 if (rdev) {
2719 if (!rdev_set_badblocks(
2720 rdev,
2721 sh->sector,
2722 STRIPE_SECTORS, 0))
2723 md_error(conf->mddev, rdev);
2724 rdev_dec_pending(rdev, conf->mddev);
2725 }
a4456856 2726 }
b17459c0 2727 spin_lock_irq(&sh->stripe_lock);
a4456856
DW
2728 /* fail all writes first */
2729 bi = sh->dev[i].towrite;
2730 sh->dev[i].towrite = NULL;
b17459c0 2731 spin_unlock_irq(&sh->stripe_lock);
1ed850f3 2732 if (bi)
a4456856 2733 bitmap_end = 1;
a4456856
DW
2734
2735 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2736 wake_up(&conf->wait_for_overlap);
2737
2738 while (bi && bi->bi_sector <
2739 sh->dev[i].sector + STRIPE_SECTORS) {
2740 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2741 clear_bit(BIO_UPTODATE, &bi->bi_flags);
e7836bd6 2742 if (!raid5_dec_bi_active_stripes(bi)) {
a4456856
DW
2743 md_write_end(conf->mddev);
2744 bi->bi_next = *return_bi;
2745 *return_bi = bi;
2746 }
2747 bi = nextbi;
2748 }
7eaf7e8e
SL
2749 if (bitmap_end)
2750 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2751 STRIPE_SECTORS, 0, 0);
2752 bitmap_end = 0;
a4456856
DW
2753 /* and fail all 'written' */
2754 bi = sh->dev[i].written;
2755 sh->dev[i].written = NULL;
2756 if (bi) bitmap_end = 1;
2757 while (bi && bi->bi_sector <
2758 sh->dev[i].sector + STRIPE_SECTORS) {
2759 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2760 clear_bit(BIO_UPTODATE, &bi->bi_flags);
e7836bd6 2761 if (!raid5_dec_bi_active_stripes(bi)) {
a4456856
DW
2762 md_write_end(conf->mddev);
2763 bi->bi_next = *return_bi;
2764 *return_bi = bi;
2765 }
2766 bi = bi2;
2767 }
2768
b5e98d65
DW
2769 /* fail any reads if this device is non-operational and
2770 * the data has not reached the cache yet.
2771 */
2772 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2773 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2774 test_bit(R5_ReadError, &sh->dev[i].flags))) {
143c4d05 2775 spin_lock_irq(&sh->stripe_lock);
a4456856
DW
2776 bi = sh->dev[i].toread;
2777 sh->dev[i].toread = NULL;
143c4d05 2778 spin_unlock_irq(&sh->stripe_lock);
a4456856
DW
2779 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2780 wake_up(&conf->wait_for_overlap);
a4456856
DW
2781 while (bi && bi->bi_sector <
2782 sh->dev[i].sector + STRIPE_SECTORS) {
2783 struct bio *nextbi =
2784 r5_next_bio(bi, sh->dev[i].sector);
2785 clear_bit(BIO_UPTODATE, &bi->bi_flags);
e7836bd6 2786 if (!raid5_dec_bi_active_stripes(bi)) {
a4456856
DW
2787 bi->bi_next = *return_bi;
2788 *return_bi = bi;
2789 }
2790 bi = nextbi;
2791 }
2792 }
a4456856
DW
2793 if (bitmap_end)
2794 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2795 STRIPE_SECTORS, 0, 0);
8cfa7b0f
N
2796 /* If we were in the middle of a write the parity block might
2797 * still be locked - so just clear all R5_LOCKED flags
2798 */
2799 clear_bit(R5_LOCKED, &sh->dev[i].flags);
a4456856
DW
2800 }
2801
8b3e6cdc
DW
2802 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2803 if (atomic_dec_and_test(&conf->pending_full_writes))
2804 md_wakeup_thread(conf->mddev->thread);
a4456856
DW
2805}
2806
7f0da59b 2807static void
d1688a6d 2808handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
7f0da59b
N
2809 struct stripe_head_state *s)
2810{
2811 int abort = 0;
2812 int i;
2813
7f0da59b 2814 clear_bit(STRIPE_SYNCING, &sh->state);
f8dfcffd
N
2815 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
2816 wake_up(&conf->wait_for_overlap);
7f0da59b 2817 s->syncing = 0;
9a3e1101 2818 s->replacing = 0;
7f0da59b 2819 /* There is nothing more to do for sync/check/repair.
18b9837e
N
2820 * Don't even need to abort as that is handled elsewhere
2821 * if needed, and not always wanted e.g. if there is a known
2822 * bad block here.
9a3e1101 2823 * For recover/replace we need to record a bad block on all
7f0da59b
N
2824 * non-sync devices, or abort the recovery
2825 */
18b9837e
N
2826 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2827 /* During recovery devices cannot be removed, so
2828 * locking and refcounting of rdevs is not needed
2829 */
2830 for (i = 0; i < conf->raid_disks; i++) {
2831 struct md_rdev *rdev = conf->disks[i].rdev;
2832 if (rdev
2833 && !test_bit(Faulty, &rdev->flags)
2834 && !test_bit(In_sync, &rdev->flags)
2835 && !rdev_set_badblocks(rdev, sh->sector,
2836 STRIPE_SECTORS, 0))
2837 abort = 1;
2838 rdev = conf->disks[i].replacement;
2839 if (rdev
2840 && !test_bit(Faulty, &rdev->flags)
2841 && !test_bit(In_sync, &rdev->flags)
2842 && !rdev_set_badblocks(rdev, sh->sector,
2843 STRIPE_SECTORS, 0))
2844 abort = 1;
2845 }
2846 if (abort)
2847 conf->recovery_disabled =
2848 conf->mddev->recovery_disabled;
7f0da59b 2849 }
18b9837e 2850 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
7f0da59b
N
2851}
2852
9a3e1101
N
2853static int want_replace(struct stripe_head *sh, int disk_idx)
2854{
2855 struct md_rdev *rdev;
2856 int rv = 0;
2857 /* Doing recovery so rcu locking not required */
2858 rdev = sh->raid_conf->disks[disk_idx].replacement;
2859 if (rdev
2860 && !test_bit(Faulty, &rdev->flags)
2861 && !test_bit(In_sync, &rdev->flags)
2862 && (rdev->recovery_offset <= sh->sector
2863 || rdev->mddev->recovery_cp <= sh->sector))
2864 rv = 1;
2865
2866 return rv;
2867}
2868
93b3dbce 2869/* fetch_block - checks the given member device to see if its data needs
1fe797e6
DW
2870 * to be read or computed to satisfy a request.
2871 *
2872 * Returns 1 when no more member devices need to be checked, otherwise returns
93b3dbce 2873 * 0 to tell the loop in handle_stripe_fill to continue
f38e1219 2874 */
93b3dbce
N
2875static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2876 int disk_idx, int disks)
a4456856 2877{
5599becc 2878 struct r5dev *dev = &sh->dev[disk_idx];
f2b3b44d
N
2879 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2880 &sh->dev[s->failed_num[1]] };
5599becc 2881
93b3dbce 2882 /* is the data in this block needed, and can we get it? */
5599becc
YT
2883 if (!test_bit(R5_LOCKED, &dev->flags) &&
2884 !test_bit(R5_UPTODATE, &dev->flags) &&
2885 (dev->toread ||
2886 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2887 s->syncing || s->expanding ||
9a3e1101 2888 (s->replacing && want_replace(sh, disk_idx)) ||
5d35e09c
N
2889 (s->failed >= 1 && fdev[0]->toread) ||
2890 (s->failed >= 2 && fdev[1]->toread) ||
93b3dbce
N
2891 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2892 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2893 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
5599becc
YT
2894 /* we would like to get this block, possibly by computing it,
2895 * otherwise read it if the backing disk is insync
2896 */
2897 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2898 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2899 if ((s->uptodate == disks - 1) &&
f2b3b44d
N
2900 (s->failed && (disk_idx == s->failed_num[0] ||
2901 disk_idx == s->failed_num[1]))) {
5599becc
YT
2902 /* have disk failed, and we're requested to fetch it;
2903 * do compute it
a4456856 2904 */
5599becc
YT
2905 pr_debug("Computing stripe %llu block %d\n",
2906 (unsigned long long)sh->sector, disk_idx);
2907 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2908 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2909 set_bit(R5_Wantcompute, &dev->flags);
2910 sh->ops.target = disk_idx;
2911 sh->ops.target2 = -1; /* no 2nd target */
2912 s->req_compute = 1;
93b3dbce
N
2913 /* Careful: from this point on 'uptodate' is in the eye
2914 * of raid_run_ops which services 'compute' operations
2915 * before writes. R5_Wantcompute flags a block that will
2916 * be R5_UPTODATE by the time it is needed for a
2917 * subsequent operation.
2918 */
5599becc
YT
2919 s->uptodate++;
2920 return 1;
2921 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2922 /* Computing 2-failure is *very* expensive; only
2923 * do it if failed >= 2
2924 */
2925 int other;
2926 for (other = disks; other--; ) {
2927 if (other == disk_idx)
2928 continue;
2929 if (!test_bit(R5_UPTODATE,
2930 &sh->dev[other].flags))
2931 break;
a4456856 2932 }
5599becc
YT
2933 BUG_ON(other < 0);
2934 pr_debug("Computing stripe %llu blocks %d,%d\n",
2935 (unsigned long long)sh->sector,
2936 disk_idx, other);
2937 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2938 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2939 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2940 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2941 sh->ops.target = disk_idx;
2942 sh->ops.target2 = other;
2943 s->uptodate += 2;
2944 s->req_compute = 1;
2945 return 1;
2946 } else if (test_bit(R5_Insync, &dev->flags)) {
2947 set_bit(R5_LOCKED, &dev->flags);
2948 set_bit(R5_Wantread, &dev->flags);
2949 s->locked++;
2950 pr_debug("Reading block %d (sync=%d)\n",
2951 disk_idx, s->syncing);
a4456856
DW
2952 }
2953 }
5599becc
YT
2954
2955 return 0;
2956}
2957
2958/**
93b3dbce 2959 * handle_stripe_fill - read or compute data to satisfy pending requests.
5599becc 2960 */
93b3dbce
N
2961static void handle_stripe_fill(struct stripe_head *sh,
2962 struct stripe_head_state *s,
2963 int disks)
5599becc
YT
2964{
2965 int i;
2966
2967 /* look for blocks to read/compute, skip this if a compute
2968 * is already in flight, or if the stripe contents are in the
2969 * midst of changing due to a write
2970 */
2971 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2972 !sh->reconstruct_state)
2973 for (i = disks; i--; )
93b3dbce 2974 if (fetch_block(sh, s, i, disks))
5599becc 2975 break;
a4456856
DW
2976 set_bit(STRIPE_HANDLE, &sh->state);
2977}
2978
2979
1fe797e6 2980/* handle_stripe_clean_event
a4456856
DW
2981 * any written block on an uptodate or failed drive can be returned.
2982 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2983 * never LOCKED, so we don't need to test 'failed' directly.
2984 */
d1688a6d 2985static void handle_stripe_clean_event(struct r5conf *conf,
a4456856
DW
2986 struct stripe_head *sh, int disks, struct bio **return_bi)
2987{
2988 int i;
2989 struct r5dev *dev;
f8dfcffd 2990 int discard_pending = 0;
a4456856
DW
2991
2992 for (i = disks; i--; )
2993 if (sh->dev[i].written) {
2994 dev = &sh->dev[i];
2995 if (!test_bit(R5_LOCKED, &dev->flags) &&
9e444768 2996 (test_bit(R5_UPTODATE, &dev->flags) ||
ca64cae9 2997 test_bit(R5_Discard, &dev->flags))) {
a4456856
DW
2998 /* We can return any write requests */
2999 struct bio *wbi, *wbi2;
45b4233c 3000 pr_debug("Return write for disc %d\n", i);
ca64cae9
N
3001 if (test_and_clear_bit(R5_Discard, &dev->flags))
3002 clear_bit(R5_UPTODATE, &dev->flags);
a4456856
DW
3003 wbi = dev->written;
3004 dev->written = NULL;
3005 while (wbi && wbi->bi_sector <
3006 dev->sector + STRIPE_SECTORS) {
3007 wbi2 = r5_next_bio(wbi, dev->sector);
e7836bd6 3008 if (!raid5_dec_bi_active_stripes(wbi)) {
a4456856
DW
3009 md_write_end(conf->mddev);
3010 wbi->bi_next = *return_bi;
3011 *return_bi = wbi;
3012 }
3013 wbi = wbi2;
3014 }
7eaf7e8e
SL
3015 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3016 STRIPE_SECTORS,
a4456856 3017 !test_bit(STRIPE_DEGRADED, &sh->state),
7eaf7e8e 3018 0);
f8dfcffd
N
3019 } else if (test_bit(R5_Discard, &dev->flags))
3020 discard_pending = 1;
3021 }
3022 if (!discard_pending &&
3023 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
3024 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3025 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3026 if (sh->qd_idx >= 0) {
3027 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3028 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3029 }
3030 /* now that discard is done we can proceed with any sync */
3031 clear_bit(STRIPE_DISCARD, &sh->state);
d47648fc
SL
3032 /*
3033 * SCSI discard will change some bio fields and the stripe has
3034 * no updated data, so remove it from hash list and the stripe
3035 * will be reinitialized
3036 */
3037 spin_lock_irq(&conf->device_lock);
3038 remove_hash(sh);
3039 spin_unlock_irq(&conf->device_lock);
f8dfcffd
N
3040 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3041 set_bit(STRIPE_HANDLE, &sh->state);
3042
3043 }
8b3e6cdc
DW
3044
3045 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3046 if (atomic_dec_and_test(&conf->pending_full_writes))
3047 md_wakeup_thread(conf->mddev->thread);
a4456856
DW
3048}
3049
d1688a6d 3050static void handle_stripe_dirtying(struct r5conf *conf,
c8ac1803
N
3051 struct stripe_head *sh,
3052 struct stripe_head_state *s,
3053 int disks)
a4456856
DW
3054{
3055 int rmw = 0, rcw = 0, i;
a7854487
AL
3056 sector_t recovery_cp = conf->mddev->recovery_cp;
3057
3058 /* RAID6 requires 'rcw' in current implementation.
3059 * Otherwise, check whether resync is now happening or should start.
3060 * If yes, then the array is dirty (after unclean shutdown or
3061 * initial creation), so parity in some stripes might be inconsistent.
3062 * In this case, we need to always do reconstruct-write, to ensure
3063 * that in case of drive failure or read-error correction, we
3064 * generate correct data from the parity.
3065 */
3066 if (conf->max_degraded == 2 ||
3067 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
3068 /* Calculate the real rcw later - for now make it
c8ac1803
N
3069 * look like rcw is cheaper
3070 */
3071 rcw = 1; rmw = 2;
a7854487
AL
3072 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
3073 conf->max_degraded, (unsigned long long)recovery_cp,
3074 (unsigned long long)sh->sector);
c8ac1803 3075 } else for (i = disks; i--; ) {
a4456856
DW
3076 /* would I have to read this buffer for read_modify_write */
3077 struct r5dev *dev = &sh->dev[i];
3078 if ((dev->towrite || i == sh->pd_idx) &&
3079 !test_bit(R5_LOCKED, &dev->flags) &&
f38e1219
DW
3080 !(test_bit(R5_UPTODATE, &dev->flags) ||
3081 test_bit(R5_Wantcompute, &dev->flags))) {
a4456856
DW
3082 if (test_bit(R5_Insync, &dev->flags))
3083 rmw++;
3084 else
3085 rmw += 2*disks; /* cannot read it */
3086 }
3087 /* Would I have to read this buffer for reconstruct_write */
3088 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
3089 !test_bit(R5_LOCKED, &dev->flags) &&
f38e1219
DW
3090 !(test_bit(R5_UPTODATE, &dev->flags) ||
3091 test_bit(R5_Wantcompute, &dev->flags))) {
3092 if (test_bit(R5_Insync, &dev->flags)) rcw++;
a4456856
DW
3093 else
3094 rcw += 2*disks;
3095 }
3096 }
45b4233c 3097 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
a4456856
DW
3098 (unsigned long long)sh->sector, rmw, rcw);
3099 set_bit(STRIPE_HANDLE, &sh->state);
a9add5d9 3100 if (rmw < rcw && rmw > 0) {
a4456856 3101 /* prefer read-modify-write, but need to get some data */
e3620a3a
JB
3102 if (conf->mddev->queue)
3103 blk_add_trace_msg(conf->mddev->queue,
3104 "raid5 rmw %llu %d",
3105 (unsigned long long)sh->sector, rmw);
a4456856
DW
3106 for (i = disks; i--; ) {
3107 struct r5dev *dev = &sh->dev[i];
3108 if ((dev->towrite || i == sh->pd_idx) &&
3109 !test_bit(R5_LOCKED, &dev->flags) &&
f38e1219
DW
3110 !(test_bit(R5_UPTODATE, &dev->flags) ||
3111 test_bit(R5_Wantcompute, &dev->flags)) &&
a4456856
DW
3112 test_bit(R5_Insync, &dev->flags)) {
3113 if (
3114 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
45b4233c 3115 pr_debug("Read_old block "
a9add5d9 3116 "%d for r-m-w\n", i);
a4456856
DW
3117 set_bit(R5_LOCKED, &dev->flags);
3118 set_bit(R5_Wantread, &dev->flags);
3119 s->locked++;
3120 } else {
3121 set_bit(STRIPE_DELAYED, &sh->state);
3122 set_bit(STRIPE_HANDLE, &sh->state);
3123 }
3124 }
3125 }
a9add5d9 3126 }
c8ac1803 3127 if (rcw <= rmw && rcw > 0) {
a4456856 3128 /* want reconstruct write, but need to get some data */
a9add5d9 3129 int qread =0;
c8ac1803 3130 rcw = 0;
a4456856
DW
3131 for (i = disks; i--; ) {
3132 struct r5dev *dev = &sh->dev[i];
3133 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
c8ac1803 3134 i != sh->pd_idx && i != sh->qd_idx &&
a4456856 3135 !test_bit(R5_LOCKED, &dev->flags) &&
f38e1219 3136 !(test_bit(R5_UPTODATE, &dev->flags) ||
c8ac1803
N
3137 test_bit(R5_Wantcompute, &dev->flags))) {
3138 rcw++;
3139 if (!test_bit(R5_Insync, &dev->flags))
3140 continue; /* it's a failed drive */
a4456856
DW
3141 if (
3142 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
45b4233c 3143 pr_debug("Read_old block "
a4456856
DW
3144 "%d for Reconstruct\n", i);
3145 set_bit(R5_LOCKED, &dev->flags);
3146 set_bit(R5_Wantread, &dev->flags);
3147 s->locked++;
a9add5d9 3148 qread++;
a4456856
DW
3149 } else {
3150 set_bit(STRIPE_DELAYED, &sh->state);
3151 set_bit(STRIPE_HANDLE, &sh->state);
3152 }
3153 }
3154 }
e3620a3a 3155 if (rcw && conf->mddev->queue)
a9add5d9
N
3156 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3157 (unsigned long long)sh->sector,
3158 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
c8ac1803 3159 }
a4456856
DW
3160 /* now if nothing is locked, and if we have enough data,
3161 * we can start a write request
3162 */
f38e1219
DW
3163 /* since handle_stripe can be called at any time we need to handle the
3164 * case where a compute block operation has been submitted and then a
ac6b53b6
DW
3165 * subsequent call wants to start a write request. raid_run_ops only
3166 * handles the case where compute block and reconstruct are requested
f38e1219
DW
3167 * simultaneously. If this is not the case then new writes need to be
3168 * held off until the compute completes.
3169 */
976ea8d4
DW
3170 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3171 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
3172 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
c0f7bddb 3173 schedule_reconstruction(sh, s, rcw == 0, 0);
a4456856
DW
3174}
3175
d1688a6d 3176static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
a4456856
DW
3177 struct stripe_head_state *s, int disks)
3178{
ecc65c9b 3179 struct r5dev *dev = NULL;
bd2ab670 3180
a4456856 3181 set_bit(STRIPE_HANDLE, &sh->state);
e89f8962 3182
ecc65c9b
DW
3183 switch (sh->check_state) {
3184 case check_state_idle:
3185 /* start a new check operation if there are no failures */
bd2ab670 3186 if (s->failed == 0) {
bd2ab670 3187 BUG_ON(s->uptodate != disks);
ecc65c9b
DW
3188 sh->check_state = check_state_run;
3189 set_bit(STRIPE_OP_CHECK, &s->ops_request);
bd2ab670 3190 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
bd2ab670 3191 s->uptodate--;
ecc65c9b 3192 break;
bd2ab670 3193 }
f2b3b44d 3194 dev = &sh->dev[s->failed_num[0]];
ecc65c9b
DW
3195 /* fall through */
3196 case check_state_compute_result:
3197 sh->check_state = check_state_idle;
3198 if (!dev)
3199 dev = &sh->dev[sh->pd_idx];
3200
3201 /* check that a write has not made the stripe insync */
3202 if (test_bit(STRIPE_INSYNC, &sh->state))
3203 break;
c8894419 3204
a4456856 3205 /* either failed parity check, or recovery is happening */
a4456856
DW
3206 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3207 BUG_ON(s->uptodate != disks);
3208
3209 set_bit(R5_LOCKED, &dev->flags);
ecc65c9b 3210 s->locked++;
a4456856 3211 set_bit(R5_Wantwrite, &dev->flags);
830ea016 3212
a4456856 3213 clear_bit(STRIPE_DEGRADED, &sh->state);
a4456856 3214 set_bit(STRIPE_INSYNC, &sh->state);
ecc65c9b
DW
3215 break;
3216 case check_state_run:
3217 break; /* we will be called again upon completion */
3218 case check_state_check_result:
3219 sh->check_state = check_state_idle;
3220
3221 /* if a failure occurred during the check operation, leave
3222 * STRIPE_INSYNC not set and let the stripe be handled again
3223 */
3224 if (s->failed)
3225 break;
3226
3227 /* handle a successful check operation, if parity is correct
3228 * we are done. Otherwise update the mismatch count and repair
3229 * parity if !MD_RECOVERY_CHECK
3230 */
ad283ea4 3231 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
ecc65c9b
DW
3232 /* parity is correct (on disc,
3233 * not in buffer any more)
3234 */
3235 set_bit(STRIPE_INSYNC, &sh->state);
3236 else {
7f7583d4 3237 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
ecc65c9b
DW
3238 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3239 /* don't try to repair!! */
3240 set_bit(STRIPE_INSYNC, &sh->state);
3241 else {
3242 sh->check_state = check_state_compute_run;
976ea8d4 3243 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
ecc65c9b
DW
3244 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3245 set_bit(R5_Wantcompute,
3246 &sh->dev[sh->pd_idx].flags);
3247 sh->ops.target = sh->pd_idx;
ac6b53b6 3248 sh->ops.target2 = -1;
ecc65c9b
DW
3249 s->uptodate++;
3250 }
3251 }
3252 break;
3253 case check_state_compute_run:
3254 break;
3255 default:
3256 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3257 __func__, sh->check_state,
3258 (unsigned long long) sh->sector);
3259 BUG();
a4456856
DW
3260 }
3261}
3262
3263
d1688a6d 3264static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
36d1c647 3265 struct stripe_head_state *s,
f2b3b44d 3266 int disks)
a4456856 3267{
a4456856 3268 int pd_idx = sh->pd_idx;
34e04e87 3269 int qd_idx = sh->qd_idx;
d82dfee0 3270 struct r5dev *dev;
a4456856
DW
3271
3272 set_bit(STRIPE_HANDLE, &sh->state);
3273
3274 BUG_ON(s->failed > 2);
d82dfee0 3275
a4456856
DW
3276 /* Want to check and possibly repair P and Q.
3277 * However there could be one 'failed' device, in which
3278 * case we can only check one of them, possibly using the
3279 * other to generate missing data
3280 */
3281
d82dfee0
DW
3282 switch (sh->check_state) {
3283 case check_state_idle:
3284 /* start a new check operation if there are < 2 failures */
f2b3b44d 3285 if (s->failed == s->q_failed) {
d82dfee0 3286 /* The only possible failed device holds Q, so it
a4456856
DW
3287 * makes sense to check P (If anything else were failed,
3288 * we would have used P to recreate it).
3289 */
d82dfee0 3290 sh->check_state = check_state_run;
a4456856 3291 }
f2b3b44d 3292 if (!s->q_failed && s->failed < 2) {
d82dfee0 3293 /* Q is not failed, and we didn't use it to generate
a4456856
DW
3294 * anything, so it makes sense to check it
3295 */
d82dfee0
DW
3296 if (sh->check_state == check_state_run)
3297 sh->check_state = check_state_run_pq;
3298 else
3299 sh->check_state = check_state_run_q;
a4456856 3300 }
a4456856 3301
d82dfee0
DW
3302 /* discard potentially stale zero_sum_result */
3303 sh->ops.zero_sum_result = 0;
a4456856 3304
d82dfee0
DW
3305 if (sh->check_state == check_state_run) {
3306 /* async_xor_zero_sum destroys the contents of P */
3307 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3308 s->uptodate--;
a4456856 3309 }
d82dfee0
DW
3310 if (sh->check_state >= check_state_run &&
3311 sh->check_state <= check_state_run_pq) {
3312 /* async_syndrome_zero_sum preserves P and Q, so
3313 * no need to mark them !uptodate here
3314 */
3315 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3316 break;
a4456856
DW
3317 }
3318
d82dfee0
DW
3319 /* we have 2-disk failure */
3320 BUG_ON(s->failed != 2);
3321 /* fall through */
3322 case check_state_compute_result:
3323 sh->check_state = check_state_idle;
a4456856 3324
d82dfee0
DW
3325 /* check that a write has not made the stripe insync */
3326 if (test_bit(STRIPE_INSYNC, &sh->state))
3327 break;
a4456856
DW
3328
3329 /* now write out any block on a failed drive,
d82dfee0 3330 * or P or Q if they were recomputed
a4456856 3331 */
d82dfee0 3332 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
a4456856 3333 if (s->failed == 2) {
f2b3b44d 3334 dev = &sh->dev[s->failed_num[1]];
a4456856
DW
3335 s->locked++;
3336 set_bit(R5_LOCKED, &dev->flags);
3337 set_bit(R5_Wantwrite, &dev->flags);
3338 }
3339 if (s->failed >= 1) {
f2b3b44d 3340 dev = &sh->dev[s->failed_num[0]];
a4456856
DW
3341 s->locked++;
3342 set_bit(R5_LOCKED, &dev->flags);
3343 set_bit(R5_Wantwrite, &dev->flags);
3344 }
d82dfee0 3345 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
a4456856
DW
3346 dev = &sh->dev[pd_idx];
3347 s->locked++;
3348 set_bit(R5_LOCKED, &dev->flags);
3349 set_bit(R5_Wantwrite, &dev->flags);
3350 }
d82dfee0 3351 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
a4456856
DW
3352 dev = &sh->dev[qd_idx];
3353 s->locked++;
3354 set_bit(R5_LOCKED, &dev->flags);
3355 set_bit(R5_Wantwrite, &dev->flags);
3356 }
3357 clear_bit(STRIPE_DEGRADED, &sh->state);
3358
3359 set_bit(STRIPE_INSYNC, &sh->state);
d82dfee0
DW
3360 break;
3361 case check_state_run:
3362 case check_state_run_q:
3363 case check_state_run_pq:
3364 break; /* we will be called again upon completion */
3365 case check_state_check_result:
3366 sh->check_state = check_state_idle;
3367
3368 /* handle a successful check operation, if parity is correct
3369 * we are done. Otherwise update the mismatch count and repair
3370 * parity if !MD_RECOVERY_CHECK
3371 */
3372 if (sh->ops.zero_sum_result == 0) {
3373 /* both parities are correct */
3374 if (!s->failed)
3375 set_bit(STRIPE_INSYNC, &sh->state);
3376 else {
3377 /* in contrast to the raid5 case we can validate
3378 * parity, but still have a failure to write
3379 * back
3380 */
3381 sh->check_state = check_state_compute_result;
3382 /* Returning at this point means that we may go
3383 * off and bring p and/or q uptodate again so
3384 * we make sure to check zero_sum_result again
3385 * to verify if p or q need writeback
3386 */
3387 }
3388 } else {
7f7583d4 3389 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
d82dfee0
DW
3390 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3391 /* don't try to repair!! */
3392 set_bit(STRIPE_INSYNC, &sh->state);
3393 else {
3394 int *target = &sh->ops.target;
3395
3396 sh->ops.target = -1;
3397 sh->ops.target2 = -1;
3398 sh->check_state = check_state_compute_run;
3399 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3400 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3401 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3402 set_bit(R5_Wantcompute,
3403 &sh->dev[pd_idx].flags);
3404 *target = pd_idx;
3405 target = &sh->ops.target2;
3406 s->uptodate++;
3407 }
3408 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3409 set_bit(R5_Wantcompute,
3410 &sh->dev[qd_idx].flags);
3411 *target = qd_idx;
3412 s->uptodate++;
3413 }
3414 }
3415 }
3416 break;
3417 case check_state_compute_run:
3418 break;
3419 default:
3420 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3421 __func__, sh->check_state,
3422 (unsigned long long) sh->sector);
3423 BUG();
a4456856
DW
3424 }
3425}
3426
d1688a6d 3427static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
a4456856
DW
3428{
3429 int i;
3430
3431 /* We have read all the blocks in this stripe and now we need to
3432 * copy some of them into a target stripe for expand.
3433 */
f0a50d37 3434 struct dma_async_tx_descriptor *tx = NULL;
a4456856
DW
3435 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3436 for (i = 0; i < sh->disks; i++)
34e04e87 3437 if (i != sh->pd_idx && i != sh->qd_idx) {
911d4ee8 3438 int dd_idx, j;
a4456856 3439 struct stripe_head *sh2;
a08abd8c 3440 struct async_submit_ctl submit;
a4456856 3441
784052ec 3442 sector_t bn = compute_blocknr(sh, i, 1);
911d4ee8
N
3443 sector_t s = raid5_compute_sector(conf, bn, 0,
3444 &dd_idx, NULL);
a8c906ca 3445 sh2 = get_active_stripe(conf, s, 0, 1, 1);
a4456856
DW
3446 if (sh2 == NULL)
3447 /* so far only the early blocks of this stripe
3448 * have been requested. When later blocks
3449 * get requested, we will try again
3450 */
3451 continue;
3452 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3453 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3454 /* must have already done this block */
3455 release_stripe(sh2);
3456 continue;
3457 }
f0a50d37
DW
3458
3459 /* place all the copies on one channel */
a08abd8c 3460 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
f0a50d37 3461 tx = async_memcpy(sh2->dev[dd_idx].page,
88ba2aa5 3462 sh->dev[i].page, 0, 0, STRIPE_SIZE,
a08abd8c 3463 &submit);
f0a50d37 3464
a4456856
DW
3465 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3466 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3467 for (j = 0; j < conf->raid_disks; j++)
3468 if (j != sh2->pd_idx &&
86c374ba 3469 j != sh2->qd_idx &&
a4456856
DW
3470 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3471 break;
3472 if (j == conf->raid_disks) {
3473 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3474 set_bit(STRIPE_HANDLE, &sh2->state);
3475 }
3476 release_stripe(sh2);
f0a50d37 3477
a4456856 3478 }
a2e08551 3479 /* done submitting copies, wait for them to complete */
749586b7 3480 async_tx_quiesce(&tx);
a4456856 3481}
1da177e4
LT
3482
3483/*
3484 * handle_stripe - do things to a stripe.
3485 *
9a3e1101
N
3486 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3487 * state of various bits to see what needs to be done.
1da177e4 3488 * Possible results:
9a3e1101
N
3489 * return some read requests which now have data
3490 * return some write requests which are safely on storage
1da177e4
LT
3491 * schedule a read on some buffers
3492 * schedule a write of some buffers
3493 * return confirmation of parity correctness
3494 *
1da177e4 3495 */
a4456856 3496
acfe726b 3497static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
1da177e4 3498{
d1688a6d 3499 struct r5conf *conf = sh->raid_conf;
f416885e 3500 int disks = sh->disks;
474af965
N
3501 struct r5dev *dev;
3502 int i;
9a3e1101 3503 int do_recovery = 0;
1da177e4 3504
acfe726b
N
3505 memset(s, 0, sizeof(*s));
3506
acfe726b
N
3507 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3508 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3509 s->failed_num[0] = -1;
3510 s->failed_num[1] = -1;
1da177e4 3511
acfe726b 3512 /* Now to look around and see what can be done */
1da177e4 3513 rcu_read_lock();
16a53ecc 3514 for (i=disks; i--; ) {
3cb03002 3515 struct md_rdev *rdev;
31c176ec
N
3516 sector_t first_bad;
3517 int bad_sectors;
3518 int is_bad = 0;
acfe726b 3519
16a53ecc 3520 dev = &sh->dev[i];
1da177e4 3521
45b4233c 3522 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
9a3e1101
N
3523 i, dev->flags,
3524 dev->toread, dev->towrite, dev->written);
6c0069c0
YT
3525 /* maybe we can reply to a read
3526 *
3527 * new wantfill requests are only permitted while
3528 * ops_complete_biofill is guaranteed to be inactive
3529 */
3530 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3531 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3532 set_bit(R5_Wantfill, &dev->flags);
1da177e4 3533
16a53ecc 3534 /* now count some things */
cc94015a
N
3535 if (test_bit(R5_LOCKED, &dev->flags))
3536 s->locked++;
3537 if (test_bit(R5_UPTODATE, &dev->flags))
3538 s->uptodate++;
2d6e4ecc 3539 if (test_bit(R5_Wantcompute, &dev->flags)) {
cc94015a
N
3540 s->compute++;
3541 BUG_ON(s->compute > 2);
2d6e4ecc 3542 }
1da177e4 3543
acfe726b 3544 if (test_bit(R5_Wantfill, &dev->flags))
cc94015a 3545 s->to_fill++;
acfe726b 3546 else if (dev->toread)
cc94015a 3547 s->to_read++;
16a53ecc 3548 if (dev->towrite) {
cc94015a 3549 s->to_write++;
16a53ecc 3550 if (!test_bit(R5_OVERWRITE, &dev->flags))
cc94015a 3551 s->non_overwrite++;
16a53ecc 3552 }
a4456856 3553 if (dev->written)
cc94015a 3554 s->written++;
14a75d3e
N
3555 /* Prefer to use the replacement for reads, but only
3556 * if it is recovered enough and has no bad blocks.
3557 */
3558 rdev = rcu_dereference(conf->disks[i].replacement);
3559 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3560 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3561 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3562 &first_bad, &bad_sectors))
3563 set_bit(R5_ReadRepl, &dev->flags);
3564 else {
9a3e1101
N
3565 if (rdev)
3566 set_bit(R5_NeedReplace, &dev->flags);
14a75d3e
N
3567 rdev = rcu_dereference(conf->disks[i].rdev);
3568 clear_bit(R5_ReadRepl, &dev->flags);
3569 }
9283d8c5
N
3570 if (rdev && test_bit(Faulty, &rdev->flags))
3571 rdev = NULL;
31c176ec
N
3572 if (rdev) {
3573 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3574 &first_bad, &bad_sectors);
3575 if (s->blocked_rdev == NULL
3576 && (test_bit(Blocked, &rdev->flags)
3577 || is_bad < 0)) {
3578 if (is_bad < 0)
3579 set_bit(BlockedBadBlocks,
3580 &rdev->flags);
3581 s->blocked_rdev = rdev;
3582 atomic_inc(&rdev->nr_pending);
3583 }
6bfe0b49 3584 }
415e72d0
N
3585 clear_bit(R5_Insync, &dev->flags);
3586 if (!rdev)
3587 /* Not in-sync */;
31c176ec
N
3588 else if (is_bad) {
3589 /* also not in-sync */
18b9837e
N
3590 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3591 test_bit(R5_UPTODATE, &dev->flags)) {
31c176ec
N
3592 /* treat as in-sync, but with a read error
3593 * which we can now try to correct
3594 */
3595 set_bit(R5_Insync, &dev->flags);
3596 set_bit(R5_ReadError, &dev->flags);
3597 }
3598 } else if (test_bit(In_sync, &rdev->flags))
415e72d0 3599 set_bit(R5_Insync, &dev->flags);
30d7a483 3600 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
415e72d0 3601 /* in sync if before recovery_offset */
30d7a483
N
3602 set_bit(R5_Insync, &dev->flags);
3603 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3604 test_bit(R5_Expanded, &dev->flags))
3605 /* If we've reshaped into here, we assume it is Insync.
3606 * We will shortly update recovery_offset to make
3607 * it official.
3608 */
3609 set_bit(R5_Insync, &dev->flags);
3610
1cc03eb9 3611 if (test_bit(R5_WriteError, &dev->flags)) {
14a75d3e
N
3612 /* This flag does not apply to '.replacement'
3613 * only to .rdev, so make sure to check that*/
3614 struct md_rdev *rdev2 = rcu_dereference(
3615 conf->disks[i].rdev);
3616 if (rdev2 == rdev)
3617 clear_bit(R5_Insync, &dev->flags);
3618 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
bc2607f3 3619 s->handle_bad_blocks = 1;
14a75d3e 3620 atomic_inc(&rdev2->nr_pending);
bc2607f3
N
3621 } else
3622 clear_bit(R5_WriteError, &dev->flags);
3623 }
1cc03eb9 3624 if (test_bit(R5_MadeGood, &dev->flags)) {
14a75d3e
N
3625 /* This flag does not apply to '.replacement'
3626 * only to .rdev, so make sure to check that*/
3627 struct md_rdev *rdev2 = rcu_dereference(
3628 conf->disks[i].rdev);
3629 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
b84db560 3630 s->handle_bad_blocks = 1;
14a75d3e 3631 atomic_inc(&rdev2->nr_pending);
b84db560
N
3632 } else
3633 clear_bit(R5_MadeGood, &dev->flags);
3634 }
977df362
N
3635 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3636 struct md_rdev *rdev2 = rcu_dereference(
3637 conf->disks[i].replacement);
3638 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3639 s->handle_bad_blocks = 1;
3640 atomic_inc(&rdev2->nr_pending);
3641 } else
3642 clear_bit(R5_MadeGoodRepl, &dev->flags);
3643 }
415e72d0 3644 if (!test_bit(R5_Insync, &dev->flags)) {
16a53ecc
N
3645 /* The ReadError flag will just be confusing now */
3646 clear_bit(R5_ReadError, &dev->flags);
3647 clear_bit(R5_ReWrite, &dev->flags);
1da177e4 3648 }
415e72d0
N
3649 if (test_bit(R5_ReadError, &dev->flags))
3650 clear_bit(R5_Insync, &dev->flags);
3651 if (!test_bit(R5_Insync, &dev->flags)) {
cc94015a
N
3652 if (s->failed < 2)
3653 s->failed_num[s->failed] = i;
3654 s->failed++;
9a3e1101
N
3655 if (rdev && !test_bit(Faulty, &rdev->flags))
3656 do_recovery = 1;
415e72d0 3657 }
1da177e4 3658 }
9a3e1101
N
3659 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3660 /* If there is a failed device being replaced,
3661 * we must be recovering.
3662 * else if we are after recovery_cp, we must be syncing
c6d2e084 3663 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
9a3e1101
N
3664 * else we can only be replacing
3665 * sync and recovery both need to read all devices, and so
3666 * use the same flag.
3667 */
3668 if (do_recovery ||
c6d2e084 3669 sh->sector >= conf->mddev->recovery_cp ||
3670 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
9a3e1101
N
3671 s->syncing = 1;
3672 else
3673 s->replacing = 1;
3674 }
1da177e4 3675 rcu_read_unlock();
cc94015a
N
3676}
3677
3678static void handle_stripe(struct stripe_head *sh)
3679{
3680 struct stripe_head_state s;
d1688a6d 3681 struct r5conf *conf = sh->raid_conf;
3687c061 3682 int i;
84789554
N
3683 int prexor;
3684 int disks = sh->disks;
474af965 3685 struct r5dev *pdev, *qdev;
cc94015a
N
3686
3687 clear_bit(STRIPE_HANDLE, &sh->state);
257a4b42 3688 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
cc94015a
N
3689 /* already being handled, ensure it gets handled
3690 * again when current action finishes */
3691 set_bit(STRIPE_HANDLE, &sh->state);
3692 return;
3693 }
3694
f8dfcffd
N
3695 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3696 spin_lock(&sh->stripe_lock);
3697 /* Cannot process 'sync' concurrently with 'discard' */
3698 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
3699 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3700 set_bit(STRIPE_SYNCING, &sh->state);
3701 clear_bit(STRIPE_INSYNC, &sh->state);
f94c0b66 3702 clear_bit(STRIPE_REPLACED, &sh->state);
f8dfcffd
N
3703 }
3704 spin_unlock(&sh->stripe_lock);
cc94015a
N
3705 }
3706 clear_bit(STRIPE_DELAYED, &sh->state);
3707
3708 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3709 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3710 (unsigned long long)sh->sector, sh->state,
3711 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3712 sh->check_state, sh->reconstruct_state);
3687c061 3713
acfe726b 3714 analyse_stripe(sh, &s);
c5a31000 3715
bc2607f3
N
3716 if (s.handle_bad_blocks) {
3717 set_bit(STRIPE_HANDLE, &sh->state);
3718 goto finish;
3719 }
3720
474af965
N
3721 if (unlikely(s.blocked_rdev)) {
3722 if (s.syncing || s.expanding || s.expanded ||
9a3e1101 3723 s.replacing || s.to_write || s.written) {
474af965
N
3724 set_bit(STRIPE_HANDLE, &sh->state);
3725 goto finish;
3726 }
3727 /* There is nothing for the blocked_rdev to block */
3728 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3729 s.blocked_rdev = NULL;
3730 }
3731
3732 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3733 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3734 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3735 }
3736
3737 pr_debug("locked=%d uptodate=%d to_read=%d"
3738 " to_write=%d failed=%d failed_num=%d,%d\n",
3739 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3740 s.failed_num[0], s.failed_num[1]);
3741 /* check if the array has lost more than max_degraded devices and,
3742 * if so, some requests might need to be failed.
3743 */
9a3f530f
N
3744 if (s.failed > conf->max_degraded) {
3745 sh->check_state = 0;
3746 sh->reconstruct_state = 0;
3747 if (s.to_read+s.to_write+s.written)
3748 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
9a3e1101 3749 if (s.syncing + s.replacing)
9a3f530f
N
3750 handle_failed_sync(conf, sh, &s);
3751 }
474af965 3752
84789554
N
3753 /* Now we check to see if any write operations have recently
3754 * completed
3755 */
3756 prexor = 0;
3757 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3758 prexor = 1;
3759 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3760 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3761 sh->reconstruct_state = reconstruct_state_idle;
3762
3763 /* All the 'written' buffers and the parity block are ready to
3764 * be written back to disk
3765 */
9e444768
SL
3766 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3767 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
84789554 3768 BUG_ON(sh->qd_idx >= 0 &&
9e444768
SL
3769 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3770 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
84789554
N
3771 for (i = disks; i--; ) {
3772 struct r5dev *dev = &sh->dev[i];
3773 if (test_bit(R5_LOCKED, &dev->flags) &&
3774 (i == sh->pd_idx || i == sh->qd_idx ||
3775 dev->written)) {
3776 pr_debug("Writing block %d\n", i);
3777 set_bit(R5_Wantwrite, &dev->flags);
3778 if (prexor)
3779 continue;
3780 if (!test_bit(R5_Insync, &dev->flags) ||
3781 ((i == sh->pd_idx || i == sh->qd_idx) &&
3782 s.failed == 0))
3783 set_bit(STRIPE_INSYNC, &sh->state);
3784 }
3785 }
3786 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3787 s.dec_preread_active = 1;
3788 }
3789
ef5b7c69
N
3790 /*
3791 * might be able to return some write requests if the parity blocks
3792 * are safe, or on a failed drive
3793 */
3794 pdev = &sh->dev[sh->pd_idx];
3795 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3796 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3797 qdev = &sh->dev[sh->qd_idx];
3798 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3799 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3800 || conf->level < 6;
3801
3802 if (s.written &&
3803 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3804 && !test_bit(R5_LOCKED, &pdev->flags)
3805 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3806 test_bit(R5_Discard, &pdev->flags))))) &&
3807 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3808 && !test_bit(R5_LOCKED, &qdev->flags)
3809 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3810 test_bit(R5_Discard, &qdev->flags))))))
3811 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3812
3813 /* Now we might consider reading some blocks, either to check/generate
3814 * parity, or to satisfy requests
3815 * or to load a block that is being partially written.
3816 */
3817 if (s.to_read || s.non_overwrite
3818 || (conf->level == 6 && s.to_write && s.failed)
3819 || (s.syncing && (s.uptodate + s.compute < disks))
3820 || s.replacing
3821 || s.expanding)
3822 handle_stripe_fill(sh, &s, disks);
3823
84789554
N
3824 /* Now to consider new write requests and what else, if anything
3825 * should be read. We do not handle new writes when:
3826 * 1/ A 'write' operation (copy+xor) is already in flight.
3827 * 2/ A 'check' operation is in flight, as it may clobber the parity
3828 * block.
3829 */
3830 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3831 handle_stripe_dirtying(conf, sh, &s, disks);
3832
3833 /* maybe we need to check and possibly fix the parity for this stripe
3834 * Any reads will already have been scheduled, so we just see if enough
3835 * data is available. The parity check is held off while parity
3836 * dependent operations are in flight.
3837 */
3838 if (sh->check_state ||
3839 (s.syncing && s.locked == 0 &&
3840 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3841 !test_bit(STRIPE_INSYNC, &sh->state))) {
3842 if (conf->level == 6)
3843 handle_parity_checks6(conf, sh, &s, disks);
3844 else
3845 handle_parity_checks5(conf, sh, &s, disks);
3846 }
c5a31000 3847
f94c0b66
N
3848 if ((s.replacing || s.syncing) && s.locked == 0
3849 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
3850 && !test_bit(STRIPE_REPLACED, &sh->state)) {
9a3e1101
N
3851 /* Write out to replacement devices where possible */
3852 for (i = 0; i < conf->raid_disks; i++)
f94c0b66
N
3853 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3854 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
9a3e1101
N
3855 set_bit(R5_WantReplace, &sh->dev[i].flags);
3856 set_bit(R5_LOCKED, &sh->dev[i].flags);
3857 s.locked++;
3858 }
f94c0b66
N
3859 if (s.replacing)
3860 set_bit(STRIPE_INSYNC, &sh->state);
3861 set_bit(STRIPE_REPLACED, &sh->state);
9a3e1101
N
3862 }
3863 if ((s.syncing || s.replacing) && s.locked == 0 &&
f94c0b66 3864 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
9a3e1101 3865 test_bit(STRIPE_INSYNC, &sh->state)) {
c5a31000
N
3866 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3867 clear_bit(STRIPE_SYNCING, &sh->state);
f8dfcffd
N
3868 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3869 wake_up(&conf->wait_for_overlap);
c5a31000
N
3870 }
3871
3872 /* If the failed drives are just a ReadError, then we might need
3873 * to progress the repair/check process
3874 */
3875 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3876 for (i = 0; i < s.failed; i++) {
3877 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3878 if (test_bit(R5_ReadError, &dev->flags)
3879 && !test_bit(R5_LOCKED, &dev->flags)
3880 && test_bit(R5_UPTODATE, &dev->flags)
3881 ) {
3882 if (!test_bit(R5_ReWrite, &dev->flags)) {
3883 set_bit(R5_Wantwrite, &dev->flags);
3884 set_bit(R5_ReWrite, &dev->flags);
3885 set_bit(R5_LOCKED, &dev->flags);
3886 s.locked++;
3887 } else {
3888 /* let's read it back */
3889 set_bit(R5_Wantread, &dev->flags);
3890 set_bit(R5_LOCKED, &dev->flags);
3891 s.locked++;
3892 }
3893 }
3894 }
3895
3896
3687c061
N
3897 /* Finish reconstruct operations initiated by the expansion process */
3898 if (sh->reconstruct_state == reconstruct_state_result) {
3899 struct stripe_head *sh_src
3900 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3901 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3902 /* sh cannot be written until sh_src has been read.
3903 * so arrange for sh to be delayed a little
3904 */
3905 set_bit(STRIPE_DELAYED, &sh->state);
3906 set_bit(STRIPE_HANDLE, &sh->state);
3907 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3908 &sh_src->state))
3909 atomic_inc(&conf->preread_active_stripes);
3910 release_stripe(sh_src);
3911 goto finish;
3912 }
3913 if (sh_src)
3914 release_stripe(sh_src);
3915
3916 sh->reconstruct_state = reconstruct_state_idle;
3917 clear_bit(STRIPE_EXPANDING, &sh->state);
3918 for (i = conf->raid_disks; i--; ) {
3919 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3920 set_bit(R5_LOCKED, &sh->dev[i].flags);
3921 s.locked++;
3922 }
3923 }
f416885e 3924
3687c061
N
3925 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3926 !sh->reconstruct_state) {
3927 /* Need to write out all blocks after computing parity */
3928 sh->disks = conf->raid_disks;
3929 stripe_set_idx(sh->sector, conf, 0, sh);
3930 schedule_reconstruction(sh, &s, 1, 1);
3931 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3932 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3933 atomic_dec(&conf->reshape_stripes);
3934 wake_up(&conf->wait_for_overlap);
3935 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3936 }
3937
3938 if (s.expanding && s.locked == 0 &&
3939 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3940 handle_stripe_expansion(conf, sh);
16a53ecc 3941
3687c061 3942finish:
6bfe0b49 3943 /* wait for this device to become unblocked */
5f066c63
N
3944 if (unlikely(s.blocked_rdev)) {
3945 if (conf->mddev->external)
3946 md_wait_for_blocked_rdev(s.blocked_rdev,
3947 conf->mddev);
3948 else
3949 /* Internal metadata will immediately
3950 * be written by raid5d, so we don't
3951 * need to wait here.
3952 */
3953 rdev_dec_pending(s.blocked_rdev,
3954 conf->mddev);
3955 }
6bfe0b49 3956
bc2607f3
N
3957 if (s.handle_bad_blocks)
3958 for (i = disks; i--; ) {
3cb03002 3959 struct md_rdev *rdev;
bc2607f3
N
3960 struct r5dev *dev = &sh->dev[i];
3961 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3962 /* We own a safe reference to the rdev */
3963 rdev = conf->disks[i].rdev;
3964 if (!rdev_set_badblocks(rdev, sh->sector,
3965 STRIPE_SECTORS, 0))
3966 md_error(conf->mddev, rdev);
3967 rdev_dec_pending(rdev, conf->mddev);
3968 }
b84db560
N
3969 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3970 rdev = conf->disks[i].rdev;
3971 rdev_clear_badblocks(rdev, sh->sector,
c6563a8c 3972 STRIPE_SECTORS, 0);
b84db560
N
3973 rdev_dec_pending(rdev, conf->mddev);
3974 }
977df362
N
3975 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3976 rdev = conf->disks[i].replacement;
dd054fce
N
3977 if (!rdev)
3978 /* rdev have been moved down */
3979 rdev = conf->disks[i].rdev;
977df362 3980 rdev_clear_badblocks(rdev, sh->sector,
c6563a8c 3981 STRIPE_SECTORS, 0);
977df362
N
3982 rdev_dec_pending(rdev, conf->mddev);
3983 }
bc2607f3
N
3984 }
3985
6c0069c0
YT
3986 if (s.ops_request)
3987 raid_run_ops(sh, s.ops_request);
3988
f0e43bcd 3989 ops_run_io(sh, &s);
16a53ecc 3990
c5709ef6 3991 if (s.dec_preread_active) {
729a1866 3992 /* We delay this until after ops_run_io so that if make_request
e9c7469b 3993 * is waiting on a flush, it won't continue until the writes
729a1866
N
3994 * have actually been submitted.
3995 */
3996 atomic_dec(&conf->preread_active_stripes);
3997 if (atomic_read(&conf->preread_active_stripes) <
3998 IO_THRESHOLD)
3999 md_wakeup_thread(conf->mddev->thread);
4000 }
4001
c5709ef6 4002 return_io(s.return_bi);
16a53ecc 4003
257a4b42 4004 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
16a53ecc
N
4005}
4006
d1688a6d 4007static void raid5_activate_delayed(struct r5conf *conf)
16a53ecc
N
4008{
4009 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
4010 while (!list_empty(&conf->delayed_list)) {
4011 struct list_head *l = conf->delayed_list.next;
4012 struct stripe_head *sh;
4013 sh = list_entry(l, struct stripe_head, lru);
4014 list_del_init(l);
4015 clear_bit(STRIPE_DELAYED, &sh->state);
4016 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4017 atomic_inc(&conf->preread_active_stripes);
8b3e6cdc 4018 list_add_tail(&sh->lru, &conf->hold_list);
851c30c9 4019 raid5_wakeup_stripe_thread(sh);
16a53ecc 4020 }
482c0834 4021 }
16a53ecc
N
4022}
4023
566c09c5
SL
4024static void activate_bit_delay(struct r5conf *conf,
4025 struct list_head *temp_inactive_list)
16a53ecc
N
4026{
4027 /* device_lock is held */
4028 struct list_head head;
4029 list_add(&head, &conf->bitmap_list);
4030 list_del_init(&conf->bitmap_list);
4031 while (!list_empty(&head)) {
4032 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
566c09c5 4033 int hash;
16a53ecc
N
4034 list_del_init(&sh->lru);
4035 atomic_inc(&sh->count);
566c09c5
SL
4036 hash = sh->hash_lock_index;
4037 __release_stripe(conf, sh, &temp_inactive_list[hash]);
16a53ecc
N
4038 }
4039}
4040
fd01b88c 4041int md_raid5_congested(struct mddev *mddev, int bits)
f022b2fd 4042{
d1688a6d 4043 struct r5conf *conf = mddev->private;
f022b2fd
N
4044
4045 /* No difference between reads and writes. Just check
4046 * how busy the stripe_cache is
4047 */
3fa841d7 4048
f022b2fd
N
4049 if (conf->inactive_blocked)
4050 return 1;
4051 if (conf->quiesce)
4052 return 1;
4bda556a 4053 if (atomic_read(&conf->empty_inactive_list_nr))
f022b2fd
N
4054 return 1;
4055
4056 return 0;
4057}
11d8a6e3
N
4058EXPORT_SYMBOL_GPL(md_raid5_congested);
4059
4060static int raid5_congested(void *data, int bits)
4061{
fd01b88c 4062 struct mddev *mddev = data;
11d8a6e3
N
4063
4064 return mddev_congested(mddev, bits) ||
4065 md_raid5_congested(mddev, bits);
4066}
f022b2fd 4067
23032a0e
RBJ
4068/* We want read requests to align with chunks where possible,
4069 * but write requests don't need to.
4070 */
cc371e66
AK
4071static int raid5_mergeable_bvec(struct request_queue *q,
4072 struct bvec_merge_data *bvm,
4073 struct bio_vec *biovec)
23032a0e 4074{
fd01b88c 4075 struct mddev *mddev = q->queuedata;
cc371e66 4076 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
23032a0e 4077 int max;
9d8f0363 4078 unsigned int chunk_sectors = mddev->chunk_sectors;
cc371e66 4079 unsigned int bio_sectors = bvm->bi_size >> 9;
23032a0e 4080
cc371e66 4081 if ((bvm->bi_rw & 1) == WRITE)
23032a0e
RBJ
4082 return biovec->bv_len; /* always allow writes to be mergeable */
4083
664e7c41
AN
4084 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
4085 chunk_sectors = mddev->new_chunk_sectors;
23032a0e
RBJ
4086 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
4087 if (max < 0) max = 0;
4088 if (max <= biovec->bv_len && bio_sectors == 0)
4089 return biovec->bv_len;
4090 else
4091 return max;
4092}
4093
f679623f 4094
fd01b88c 4095static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
f679623f
RBJ
4096{
4097 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
9d8f0363 4098 unsigned int chunk_sectors = mddev->chunk_sectors;
aa8b57aa 4099 unsigned int bio_sectors = bio_sectors(bio);
f679623f 4100
664e7c41
AN
4101 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
4102 chunk_sectors = mddev->new_chunk_sectors;
f679623f
RBJ
4103 return chunk_sectors >=
4104 ((sector & (chunk_sectors - 1)) + bio_sectors);
4105}
4106
46031f9a
RBJ
4107/*
4108 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4109 * later sampled by raid5d.
4110 */
d1688a6d 4111static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
46031f9a
RBJ
4112{
4113 unsigned long flags;
4114
4115 spin_lock_irqsave(&conf->device_lock, flags);
4116
4117 bi->bi_next = conf->retry_read_aligned_list;
4118 conf->retry_read_aligned_list = bi;
4119
4120 spin_unlock_irqrestore(&conf->device_lock, flags);
4121 md_wakeup_thread(conf->mddev->thread);
4122}
4123
4124
d1688a6d 4125static struct bio *remove_bio_from_retry(struct r5conf *conf)
46031f9a
RBJ
4126{
4127 struct bio *bi;
4128
4129 bi = conf->retry_read_aligned;
4130 if (bi) {
4131 conf->retry_read_aligned = NULL;
4132 return bi;
4133 }
4134 bi = conf->retry_read_aligned_list;
4135 if(bi) {
387bb173 4136 conf->retry_read_aligned_list = bi->bi_next;
46031f9a 4137 bi->bi_next = NULL;
960e739d
JA
4138 /*
4139 * this sets the active strip count to 1 and the processed
4140 * strip count to zero (upper 8 bits)
4141 */
e7836bd6 4142 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
46031f9a
RBJ
4143 }
4144
4145 return bi;
4146}
4147
4148
f679623f
RBJ
4149/*
4150 * The "raid5_align_endio" should check if the read succeeded and if it
4151 * did, call bio_endio on the original bio (having bio_put the new bio
4152 * first).
4153 * If the read failed..
4154 */
6712ecf8 4155static void raid5_align_endio(struct bio *bi, int error)
f679623f
RBJ
4156{
4157 struct bio* raid_bi = bi->bi_private;
fd01b88c 4158 struct mddev *mddev;
d1688a6d 4159 struct r5conf *conf;
46031f9a 4160 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3cb03002 4161 struct md_rdev *rdev;
46031f9a 4162
f679623f 4163 bio_put(bi);
46031f9a 4164
46031f9a
RBJ
4165 rdev = (void*)raid_bi->bi_next;
4166 raid_bi->bi_next = NULL;
2b7f2228
N
4167 mddev = rdev->mddev;
4168 conf = mddev->private;
46031f9a
RBJ
4169
4170 rdev_dec_pending(rdev, conf->mddev);
4171
4172 if (!error && uptodate) {
0a82a8d1
LT
4173 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
4174 raid_bi, 0);
6712ecf8 4175 bio_endio(raid_bi, 0);
46031f9a
RBJ
4176 if (atomic_dec_and_test(&conf->active_aligned_reads))
4177 wake_up(&conf->wait_for_stripe);
6712ecf8 4178 return;
46031f9a
RBJ
4179 }
4180
4181
45b4233c 4182 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
46031f9a
RBJ
4183
4184 add_bio_to_retry(raid_bi, conf);
f679623f
RBJ
4185}
4186
387bb173
NB
4187static int bio_fits_rdev(struct bio *bi)
4188{
165125e1 4189 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
387bb173 4190
aa8b57aa 4191 if (bio_sectors(bi) > queue_max_sectors(q))
387bb173
NB
4192 return 0;
4193 blk_recount_segments(q, bi);
8a78362c 4194 if (bi->bi_phys_segments > queue_max_segments(q))
387bb173
NB
4195 return 0;
4196
4197 if (q->merge_bvec_fn)
4198 /* it's too hard to apply the merge_bvec_fn at this stage,
4199 * just just give up
4200 */
4201 return 0;
4202
4203 return 1;
4204}
4205
4206
fd01b88c 4207static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
f679623f 4208{
d1688a6d 4209 struct r5conf *conf = mddev->private;
8553fe7e 4210 int dd_idx;
f679623f 4211 struct bio* align_bi;
3cb03002 4212 struct md_rdev *rdev;
671488cc 4213 sector_t end_sector;
f679623f
RBJ
4214
4215 if (!in_chunk_boundary(mddev, raid_bio)) {
45b4233c 4216 pr_debug("chunk_aligned_read : non aligned\n");
f679623f
RBJ
4217 return 0;
4218 }
4219 /*
a167f663 4220 * use bio_clone_mddev to make a copy of the bio
f679623f 4221 */
a167f663 4222 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
f679623f
RBJ
4223 if (!align_bi)
4224 return 0;
4225 /*
4226 * set bi_end_io to a new function, and set bi_private to the
4227 * original bio.
4228 */
4229 align_bi->bi_end_io = raid5_align_endio;
4230 align_bi->bi_private = raid_bio;
4231 /*
4232 * compute position
4233 */
112bf897
N
4234 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
4235 0,
911d4ee8 4236 &dd_idx, NULL);
f679623f 4237
f73a1c7d 4238 end_sector = bio_end_sector(align_bi);
f679623f 4239 rcu_read_lock();
671488cc
N
4240 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4241 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4242 rdev->recovery_offset < end_sector) {
4243 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4244 if (rdev &&
4245 (test_bit(Faulty, &rdev->flags) ||
4246 !(test_bit(In_sync, &rdev->flags) ||
4247 rdev->recovery_offset >= end_sector)))
4248 rdev = NULL;
4249 }
4250 if (rdev) {
31c176ec
N
4251 sector_t first_bad;
4252 int bad_sectors;
4253
f679623f
RBJ
4254 atomic_inc(&rdev->nr_pending);
4255 rcu_read_unlock();
46031f9a
RBJ
4256 raid_bio->bi_next = (void*)rdev;
4257 align_bi->bi_bdev = rdev->bdev;
4258 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
46031f9a 4259
31c176ec 4260 if (!bio_fits_rdev(align_bi) ||
aa8b57aa 4261 is_badblock(rdev, align_bi->bi_sector, bio_sectors(align_bi),
31c176ec
N
4262 &first_bad, &bad_sectors)) {
4263 /* too big in some way, or has a known bad block */
387bb173
NB
4264 bio_put(align_bi);
4265 rdev_dec_pending(rdev, mddev);
4266 return 0;
4267 }
4268
6c0544e2 4269 /* No reshape active, so we can trust rdev->data_offset */
4270 align_bi->bi_sector += rdev->data_offset;
4271
46031f9a
RBJ
4272 spin_lock_irq(&conf->device_lock);
4273 wait_event_lock_irq(conf->wait_for_stripe,
4274 conf->quiesce == 0,
eed8c02e 4275 conf->device_lock);
46031f9a
RBJ
4276 atomic_inc(&conf->active_aligned_reads);
4277 spin_unlock_irq(&conf->device_lock);
4278
e3620a3a
JB
4279 if (mddev->gendisk)
4280 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4281 align_bi, disk_devt(mddev->gendisk),
4282 raid_bio->bi_sector);
f679623f
RBJ
4283 generic_make_request(align_bi);
4284 return 1;
4285 } else {
4286 rcu_read_unlock();
46031f9a 4287 bio_put(align_bi);
f679623f
RBJ
4288 return 0;
4289 }
4290}
4291
8b3e6cdc
DW
4292/* __get_priority_stripe - get the next stripe to process
4293 *
4294 * Full stripe writes are allowed to pass preread active stripes up until
4295 * the bypass_threshold is exceeded. In general the bypass_count
4296 * increments when the handle_list is handled before the hold_list; however, it
4297 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4298 * stripe with in flight i/o. The bypass_count will be reset when the
4299 * head of the hold_list has changed, i.e. the head was promoted to the
4300 * handle_list.
4301 */
851c30c9 4302static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
8b3e6cdc 4303{
851c30c9
SL
4304 struct stripe_head *sh = NULL, *tmp;
4305 struct list_head *handle_list = NULL;
bfc90cb0 4306 struct r5worker_group *wg = NULL;
851c30c9
SL
4307
4308 if (conf->worker_cnt_per_group == 0) {
4309 handle_list = &conf->handle_list;
4310 } else if (group != ANY_GROUP) {
4311 handle_list = &conf->worker_groups[group].handle_list;
bfc90cb0 4312 wg = &conf->worker_groups[group];
851c30c9
SL
4313 } else {
4314 int i;
4315 for (i = 0; i < conf->group_cnt; i++) {
4316 handle_list = &conf->worker_groups[i].handle_list;
bfc90cb0 4317 wg = &conf->worker_groups[i];
851c30c9
SL
4318 if (!list_empty(handle_list))
4319 break;
4320 }
4321 }
8b3e6cdc
DW
4322
4323 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4324 __func__,
851c30c9 4325 list_empty(handle_list) ? "empty" : "busy",
8b3e6cdc
DW
4326 list_empty(&conf->hold_list) ? "empty" : "busy",
4327 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4328
851c30c9
SL
4329 if (!list_empty(handle_list)) {
4330 sh = list_entry(handle_list->next, typeof(*sh), lru);
8b3e6cdc
DW
4331
4332 if (list_empty(&conf->hold_list))
4333 conf->bypass_count = 0;
4334 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4335 if (conf->hold_list.next == conf->last_hold)
4336 conf->bypass_count++;
4337 else {
4338 conf->last_hold = conf->hold_list.next;
4339 conf->bypass_count -= conf->bypass_threshold;
4340 if (conf->bypass_count < 0)
4341 conf->bypass_count = 0;
4342 }
4343 }
4344 } else if (!list_empty(&conf->hold_list) &&
4345 ((conf->bypass_threshold &&
4346 conf->bypass_count > conf->bypass_threshold) ||
4347 atomic_read(&conf->pending_full_writes) == 0)) {
851c30c9
SL
4348
4349 list_for_each_entry(tmp, &conf->hold_list, lru) {
4350 if (conf->worker_cnt_per_group == 0 ||
4351 group == ANY_GROUP ||
4352 !cpu_online(tmp->cpu) ||
4353 cpu_to_group(tmp->cpu) == group) {
4354 sh = tmp;
4355 break;
4356 }
4357 }
4358
4359 if (sh) {
4360 conf->bypass_count -= conf->bypass_threshold;
4361 if (conf->bypass_count < 0)
4362 conf->bypass_count = 0;
4363 }
bfc90cb0 4364 wg = NULL;
851c30c9
SL
4365 }
4366
4367 if (!sh)
8b3e6cdc
DW
4368 return NULL;
4369
bfc90cb0
SL
4370 if (wg) {
4371 wg->stripes_cnt--;
4372 sh->group = NULL;
4373 }
8b3e6cdc
DW
4374 list_del_init(&sh->lru);
4375 atomic_inc(&sh->count);
4376 BUG_ON(atomic_read(&sh->count) != 1);
4377 return sh;
4378}
f679623f 4379
8811b596
SL
4380struct raid5_plug_cb {
4381 struct blk_plug_cb cb;
4382 struct list_head list;
566c09c5 4383 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
8811b596
SL
4384};
4385
4386static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4387{
4388 struct raid5_plug_cb *cb = container_of(
4389 blk_cb, struct raid5_plug_cb, cb);
4390 struct stripe_head *sh;
4391 struct mddev *mddev = cb->cb.data;
4392 struct r5conf *conf = mddev->private;
a9add5d9 4393 int cnt = 0;
566c09c5 4394 int hash;
8811b596
SL
4395
4396 if (cb->list.next && !list_empty(&cb->list)) {
4397 spin_lock_irq(&conf->device_lock);
4398 while (!list_empty(&cb->list)) {
4399 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4400 list_del_init(&sh->lru);
4401 /*
4402 * avoid race release_stripe_plug() sees
4403 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4404 * is still in our list
4405 */
4406 smp_mb__before_clear_bit();
4407 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
773ca82f
SL
4408 /*
4409 * STRIPE_ON_RELEASE_LIST could be set here. In that
4410 * case, the count is always > 1 here
4411 */
566c09c5
SL
4412 hash = sh->hash_lock_index;
4413 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
a9add5d9 4414 cnt++;
8811b596
SL
4415 }
4416 spin_unlock_irq(&conf->device_lock);
4417 }
566c09c5
SL
4418 release_inactive_stripe_list(conf, cb->temp_inactive_list,
4419 NR_STRIPE_HASH_LOCKS);
e3620a3a
JB
4420 if (mddev->queue)
4421 trace_block_unplug(mddev->queue, cnt, !from_schedule);
8811b596
SL
4422 kfree(cb);
4423}
4424
4425static void release_stripe_plug(struct mddev *mddev,
4426 struct stripe_head *sh)
4427{
4428 struct blk_plug_cb *blk_cb = blk_check_plugged(
4429 raid5_unplug, mddev,
4430 sizeof(struct raid5_plug_cb));
4431 struct raid5_plug_cb *cb;
4432
4433 if (!blk_cb) {
4434 release_stripe(sh);
4435 return;
4436 }
4437
4438 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4439
566c09c5
SL
4440 if (cb->list.next == NULL) {
4441 int i;
8811b596 4442 INIT_LIST_HEAD(&cb->list);
566c09c5
SL
4443 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
4444 INIT_LIST_HEAD(cb->temp_inactive_list + i);
4445 }
8811b596
SL
4446
4447 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4448 list_add_tail(&sh->lru, &cb->list);
4449 else
4450 release_stripe(sh);
4451}
4452
620125f2
SL
4453static void make_discard_request(struct mddev *mddev, struct bio *bi)
4454{
4455 struct r5conf *conf = mddev->private;
4456 sector_t logical_sector, last_sector;
4457 struct stripe_head *sh;
4458 int remaining;
4459 int stripe_sectors;
4460
4461 if (mddev->reshape_position != MaxSector)
4462 /* Skip discard while reshape is happening */
4463 return;
4464
4465 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4466 last_sector = bi->bi_sector + (bi->bi_size>>9);
4467
4468 bi->bi_next = NULL;
4469 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4470
4471 stripe_sectors = conf->chunk_sectors *
4472 (conf->raid_disks - conf->max_degraded);
4473 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4474 stripe_sectors);
4475 sector_div(last_sector, stripe_sectors);
4476
4477 logical_sector *= conf->chunk_sectors;
4478 last_sector *= conf->chunk_sectors;
4479
4480 for (; logical_sector < last_sector;
4481 logical_sector += STRIPE_SECTORS) {
4482 DEFINE_WAIT(w);
4483 int d;
4484 again:
4485 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4486 prepare_to_wait(&conf->wait_for_overlap, &w,
4487 TASK_UNINTERRUPTIBLE);
f8dfcffd
N
4488 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4489 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4490 release_stripe(sh);
4491 schedule();
4492 goto again;
4493 }
4494 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
620125f2
SL
4495 spin_lock_irq(&sh->stripe_lock);
4496 for (d = 0; d < conf->raid_disks; d++) {
4497 if (d == sh->pd_idx || d == sh->qd_idx)
4498 continue;
4499 if (sh->dev[d].towrite || sh->dev[d].toread) {
4500 set_bit(R5_Overlap, &sh->dev[d].flags);
4501 spin_unlock_irq(&sh->stripe_lock);
4502 release_stripe(sh);
4503 schedule();
4504 goto again;
4505 }
4506 }
f8dfcffd 4507 set_bit(STRIPE_DISCARD, &sh->state);
620125f2
SL
4508 finish_wait(&conf->wait_for_overlap, &w);
4509 for (d = 0; d < conf->raid_disks; d++) {
4510 if (d == sh->pd_idx || d == sh->qd_idx)
4511 continue;
4512 sh->dev[d].towrite = bi;
4513 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4514 raid5_inc_bi_active_stripes(bi);
4515 }
4516 spin_unlock_irq(&sh->stripe_lock);
4517 if (conf->mddev->bitmap) {
4518 for (d = 0;
4519 d < conf->raid_disks - conf->max_degraded;
4520 d++)
4521 bitmap_startwrite(mddev->bitmap,
4522 sh->sector,
4523 STRIPE_SECTORS,
4524 0);
4525 sh->bm_seq = conf->seq_flush + 1;
4526 set_bit(STRIPE_BIT_DELAY, &sh->state);
4527 }
4528
4529 set_bit(STRIPE_HANDLE, &sh->state);
4530 clear_bit(STRIPE_DELAYED, &sh->state);
4531 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4532 atomic_inc(&conf->preread_active_stripes);
4533 release_stripe_plug(mddev, sh);
4534 }
4535
4536 remaining = raid5_dec_bi_active_stripes(bi);
4537 if (remaining == 0) {
4538 md_write_end(mddev);
4539 bio_endio(bi, 0);
4540 }
4541}
4542
b4fdcb02 4543static void make_request(struct mddev *mddev, struct bio * bi)
1da177e4 4544{
d1688a6d 4545 struct r5conf *conf = mddev->private;
911d4ee8 4546 int dd_idx;
1da177e4
LT
4547 sector_t new_sector;
4548 sector_t logical_sector, last_sector;
4549 struct stripe_head *sh;
a362357b 4550 const int rw = bio_data_dir(bi);
49077326 4551 int remaining;
1da177e4 4552
e9c7469b
TH
4553 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4554 md_flush_request(mddev, bi);
5a7bbad2 4555 return;
e5dcdd80
N
4556 }
4557
3d310eb7 4558 md_write_start(mddev, bi);
06d91a5f 4559
802ba064 4560 if (rw == READ &&
52488615 4561 mddev->reshape_position == MaxSector &&
21a52c6d 4562 chunk_aligned_read(mddev,bi))
5a7bbad2 4563 return;
52488615 4564
620125f2
SL
4565 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4566 make_discard_request(mddev, bi);
4567 return;
4568 }
4569
1da177e4 4570 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
f73a1c7d 4571 last_sector = bio_end_sector(bi);
1da177e4
LT
4572 bi->bi_next = NULL;
4573 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
06d91a5f 4574
1da177e4
LT
4575 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4576 DEFINE_WAIT(w);
b5663ba4 4577 int previous;
c46501b2 4578 int seq;
b578d55f 4579
7ecaa1e6 4580 retry:
c46501b2 4581 seq = read_seqcount_begin(&conf->gen_lock);
b5663ba4 4582 previous = 0;
b578d55f 4583 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
b0f9ec04 4584 if (unlikely(conf->reshape_progress != MaxSector)) {
fef9c61f 4585 /* spinlock is needed as reshape_progress may be
df8e7f76
N
4586 * 64bit on a 32bit platform, and so it might be
4587 * possible to see a half-updated value
aeb878b0 4588 * Of course reshape_progress could change after
df8e7f76
N
4589 * the lock is dropped, so once we get a reference
4590 * to the stripe that we think it is, we will have
4591 * to check again.
4592 */
7ecaa1e6 4593 spin_lock_irq(&conf->device_lock);
2c810cdd 4594 if (mddev->reshape_backwards
fef9c61f
N
4595 ? logical_sector < conf->reshape_progress
4596 : logical_sector >= conf->reshape_progress) {
b5663ba4
N
4597 previous = 1;
4598 } else {
2c810cdd 4599 if (mddev->reshape_backwards
fef9c61f
N
4600 ? logical_sector < conf->reshape_safe
4601 : logical_sector >= conf->reshape_safe) {
b578d55f
N
4602 spin_unlock_irq(&conf->device_lock);
4603 schedule();
4604 goto retry;
4605 }
4606 }
7ecaa1e6
N
4607 spin_unlock_irq(&conf->device_lock);
4608 }
16a53ecc 4609
112bf897
N
4610 new_sector = raid5_compute_sector(conf, logical_sector,
4611 previous,
911d4ee8 4612 &dd_idx, NULL);
0c55e022 4613 pr_debug("raid456: make_request, sector %llu logical %llu\n",
c46501b2 4614 (unsigned long long)new_sector,
1da177e4
LT
4615 (unsigned long long)logical_sector);
4616
b5663ba4 4617 sh = get_active_stripe(conf, new_sector, previous,
a8c906ca 4618 (bi->bi_rw&RWA_MASK), 0);
1da177e4 4619 if (sh) {
b0f9ec04 4620 if (unlikely(previous)) {
7ecaa1e6 4621 /* expansion might have moved on while waiting for a
df8e7f76
N
4622 * stripe, so we must do the range check again.
4623 * Expansion could still move past after this
4624 * test, but as we are holding a reference to
4625 * 'sh', we know that if that happens,
4626 * STRIPE_EXPANDING will get set and the expansion
4627 * won't proceed until we finish with the stripe.
7ecaa1e6
N
4628 */
4629 int must_retry = 0;
4630 spin_lock_irq(&conf->device_lock);
2c810cdd 4631 if (mddev->reshape_backwards
b0f9ec04
N
4632 ? logical_sector >= conf->reshape_progress
4633 : logical_sector < conf->reshape_progress)
7ecaa1e6
N
4634 /* mismatch, need to try again */
4635 must_retry = 1;
4636 spin_unlock_irq(&conf->device_lock);
4637 if (must_retry) {
4638 release_stripe(sh);
7a3ab908 4639 schedule();
7ecaa1e6
N
4640 goto retry;
4641 }
4642 }
c46501b2
N
4643 if (read_seqcount_retry(&conf->gen_lock, seq)) {
4644 /* Might have got the wrong stripe_head
4645 * by accident
4646 */
4647 release_stripe(sh);
4648 goto retry;
4649 }
e62e58a5 4650
ffd96e35 4651 if (rw == WRITE &&
a5c308d4 4652 logical_sector >= mddev->suspend_lo &&
e464eafd
N
4653 logical_sector < mddev->suspend_hi) {
4654 release_stripe(sh);
e62e58a5
N
4655 /* As the suspend_* range is controlled by
4656 * userspace, we want an interruptible
4657 * wait.
4658 */
4659 flush_signals(current);
4660 prepare_to_wait(&conf->wait_for_overlap,
4661 &w, TASK_INTERRUPTIBLE);
4662 if (logical_sector >= mddev->suspend_lo &&
4663 logical_sector < mddev->suspend_hi)
4664 schedule();
e464eafd
N
4665 goto retry;
4666 }
7ecaa1e6
N
4667
4668 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
ffd96e35 4669 !add_stripe_bio(sh, bi, dd_idx, rw)) {
7ecaa1e6
N
4670 /* Stripe is busy expanding or
4671 * add failed due to overlap. Flush everything
1da177e4
LT
4672 * and wait a while
4673 */
482c0834 4674 md_wakeup_thread(mddev->thread);
1da177e4
LT
4675 release_stripe(sh);
4676 schedule();
4677 goto retry;
4678 }
4679 finish_wait(&conf->wait_for_overlap, &w);
6ed3003c
N
4680 set_bit(STRIPE_HANDLE, &sh->state);
4681 clear_bit(STRIPE_DELAYED, &sh->state);
a852d7b8 4682 if ((bi->bi_rw & REQ_SYNC) &&
729a1866
N
4683 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4684 atomic_inc(&conf->preread_active_stripes);
8811b596 4685 release_stripe_plug(mddev, sh);
1da177e4
LT
4686 } else {
4687 /* cannot get stripe for read-ahead, just give-up */
4688 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4689 finish_wait(&conf->wait_for_overlap, &w);
4690 break;
4691 }
1da177e4 4692 }
7c13edc8 4693
e7836bd6 4694 remaining = raid5_dec_bi_active_stripes(bi);
f6344757 4695 if (remaining == 0) {
1da177e4 4696
16a53ecc 4697 if ( rw == WRITE )
1da177e4 4698 md_write_end(mddev);
6712ecf8 4699
0a82a8d1
LT
4700 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4701 bi, 0);
0e13fe23 4702 bio_endio(bi, 0);
1da177e4 4703 }
1da177e4
LT
4704}
4705
fd01b88c 4706static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
b522adcd 4707
fd01b88c 4708static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
1da177e4 4709{
52c03291
N
4710 /* reshaping is quite different to recovery/resync so it is
4711 * handled quite separately ... here.
4712 *
4713 * On each call to sync_request, we gather one chunk worth of
4714 * destination stripes and flag them as expanding.
4715 * Then we find all the source stripes and request reads.
4716 * As the reads complete, handle_stripe will copy the data
4717 * into the destination stripe and release that stripe.
4718 */
d1688a6d 4719 struct r5conf *conf = mddev->private;
1da177e4 4720 struct stripe_head *sh;
ccfcc3c1 4721 sector_t first_sector, last_sector;
f416885e
N
4722 int raid_disks = conf->previous_raid_disks;
4723 int data_disks = raid_disks - conf->max_degraded;
4724 int new_data_disks = conf->raid_disks - conf->max_degraded;
52c03291
N
4725 int i;
4726 int dd_idx;
c8f517c4 4727 sector_t writepos, readpos, safepos;
ec32a2bd 4728 sector_t stripe_addr;
7a661381 4729 int reshape_sectors;
ab69ae12 4730 struct list_head stripes;
52c03291 4731
fef9c61f
N
4732 if (sector_nr == 0) {
4733 /* If restarting in the middle, skip the initial sectors */
2c810cdd 4734 if (mddev->reshape_backwards &&
fef9c61f
N
4735 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4736 sector_nr = raid5_size(mddev, 0, 0)
4737 - conf->reshape_progress;
2c810cdd 4738 } else if (!mddev->reshape_backwards &&
fef9c61f
N
4739 conf->reshape_progress > 0)
4740 sector_nr = conf->reshape_progress;
f416885e 4741 sector_div(sector_nr, new_data_disks);
fef9c61f 4742 if (sector_nr) {
8dee7211
N
4743 mddev->curr_resync_completed = sector_nr;
4744 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
fef9c61f
N
4745 *skipped = 1;
4746 return sector_nr;
4747 }
52c03291
N
4748 }
4749
7a661381
N
4750 /* We need to process a full chunk at a time.
4751 * If old and new chunk sizes differ, we need to process the
4752 * largest of these
4753 */
664e7c41
AN
4754 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4755 reshape_sectors = mddev->new_chunk_sectors;
7a661381 4756 else
9d8f0363 4757 reshape_sectors = mddev->chunk_sectors;
7a661381 4758
b5254dd5
N
4759 /* We update the metadata at least every 10 seconds, or when
4760 * the data about to be copied would over-write the source of
4761 * the data at the front of the range. i.e. one new_stripe
4762 * along from reshape_progress new_maps to after where
4763 * reshape_safe old_maps to
52c03291 4764 */
fef9c61f 4765 writepos = conf->reshape_progress;
f416885e 4766 sector_div(writepos, new_data_disks);
c8f517c4
N
4767 readpos = conf->reshape_progress;
4768 sector_div(readpos, data_disks);
fef9c61f 4769 safepos = conf->reshape_safe;
f416885e 4770 sector_div(safepos, data_disks);
2c810cdd 4771 if (mddev->reshape_backwards) {
ed37d83e 4772 writepos -= min_t(sector_t, reshape_sectors, writepos);
c8f517c4 4773 readpos += reshape_sectors;
7a661381 4774 safepos += reshape_sectors;
fef9c61f 4775 } else {
7a661381 4776 writepos += reshape_sectors;
ed37d83e
N
4777 readpos -= min_t(sector_t, reshape_sectors, readpos);
4778 safepos -= min_t(sector_t, reshape_sectors, safepos);
fef9c61f 4779 }
52c03291 4780
b5254dd5
N
4781 /* Having calculated the 'writepos' possibly use it
4782 * to set 'stripe_addr' which is where we will write to.
4783 */
4784 if (mddev->reshape_backwards) {
4785 BUG_ON(conf->reshape_progress == 0);
4786 stripe_addr = writepos;
4787 BUG_ON((mddev->dev_sectors &
4788 ~((sector_t)reshape_sectors - 1))
4789 - reshape_sectors - stripe_addr
4790 != sector_nr);
4791 } else {
4792 BUG_ON(writepos != sector_nr + reshape_sectors);
4793 stripe_addr = sector_nr;
4794 }
4795
c8f517c4
N
4796 /* 'writepos' is the most advanced device address we might write.
4797 * 'readpos' is the least advanced device address we might read.
4798 * 'safepos' is the least address recorded in the metadata as having
4799 * been reshaped.
b5254dd5
N
4800 * If there is a min_offset_diff, these are adjusted either by
4801 * increasing the safepos/readpos if diff is negative, or
4802 * increasing writepos if diff is positive.
4803 * If 'readpos' is then behind 'writepos', there is no way that we can
c8f517c4
N
4804 * ensure safety in the face of a crash - that must be done by userspace
4805 * making a backup of the data. So in that case there is no particular
4806 * rush to update metadata.
4807 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4808 * update the metadata to advance 'safepos' to match 'readpos' so that
4809 * we can be safe in the event of a crash.
4810 * So we insist on updating metadata if safepos is behind writepos and
4811 * readpos is beyond writepos.
4812 * In any case, update the metadata every 10 seconds.
4813 * Maybe that number should be configurable, but I'm not sure it is
4814 * worth it.... maybe it could be a multiple of safemode_delay???
4815 */
b5254dd5
N
4816 if (conf->min_offset_diff < 0) {
4817 safepos += -conf->min_offset_diff;
4818 readpos += -conf->min_offset_diff;
4819 } else
4820 writepos += conf->min_offset_diff;
4821
2c810cdd 4822 if ((mddev->reshape_backwards
c8f517c4
N
4823 ? (safepos > writepos && readpos < writepos)
4824 : (safepos < writepos && readpos > writepos)) ||
4825 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
52c03291
N
4826 /* Cannot proceed until we've updated the superblock... */
4827 wait_event(conf->wait_for_overlap,
c91abf5a
N
4828 atomic_read(&conf->reshape_stripes)==0
4829 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4830 if (atomic_read(&conf->reshape_stripes) != 0)
4831 return 0;
fef9c61f 4832 mddev->reshape_position = conf->reshape_progress;
75d3da43 4833 mddev->curr_resync_completed = sector_nr;
c8f517c4 4834 conf->reshape_checkpoint = jiffies;
850b2b42 4835 set_bit(MD_CHANGE_DEVS, &mddev->flags);
52c03291 4836 md_wakeup_thread(mddev->thread);
850b2b42 4837 wait_event(mddev->sb_wait, mddev->flags == 0 ||
c91abf5a
N
4838 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4839 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4840 return 0;
52c03291 4841 spin_lock_irq(&conf->device_lock);
fef9c61f 4842 conf->reshape_safe = mddev->reshape_position;
52c03291
N
4843 spin_unlock_irq(&conf->device_lock);
4844 wake_up(&conf->wait_for_overlap);
acb180b0 4845 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
52c03291
N
4846 }
4847
ab69ae12 4848 INIT_LIST_HEAD(&stripes);
7a661381 4849 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
52c03291 4850 int j;
a9f326eb 4851 int skipped_disk = 0;
a8c906ca 4852 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
52c03291
N
4853 set_bit(STRIPE_EXPANDING, &sh->state);
4854 atomic_inc(&conf->reshape_stripes);
4855 /* If any of this stripe is beyond the end of the old
4856 * array, then we need to zero those blocks
4857 */
4858 for (j=sh->disks; j--;) {
4859 sector_t s;
4860 if (j == sh->pd_idx)
4861 continue;
f416885e 4862 if (conf->level == 6 &&
d0dabf7e 4863 j == sh->qd_idx)
f416885e 4864 continue;
784052ec 4865 s = compute_blocknr(sh, j, 0);
b522adcd 4866 if (s < raid5_size(mddev, 0, 0)) {
a9f326eb 4867 skipped_disk = 1;
52c03291
N
4868 continue;
4869 }
4870 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4871 set_bit(R5_Expanded, &sh->dev[j].flags);
4872 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4873 }
a9f326eb 4874 if (!skipped_disk) {
52c03291
N
4875 set_bit(STRIPE_EXPAND_READY, &sh->state);
4876 set_bit(STRIPE_HANDLE, &sh->state);
4877 }
ab69ae12 4878 list_add(&sh->lru, &stripes);
52c03291
N
4879 }
4880 spin_lock_irq(&conf->device_lock);
2c810cdd 4881 if (mddev->reshape_backwards)
7a661381 4882 conf->reshape_progress -= reshape_sectors * new_data_disks;
fef9c61f 4883 else
7a661381 4884 conf->reshape_progress += reshape_sectors * new_data_disks;
52c03291
N
4885 spin_unlock_irq(&conf->device_lock);
4886 /* Ok, those stripe are ready. We can start scheduling
4887 * reads on the source stripes.
4888 * The source stripes are determined by mapping the first and last
4889 * block on the destination stripes.
4890 */
52c03291 4891 first_sector =
ec32a2bd 4892 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
911d4ee8 4893 1, &dd_idx, NULL);
52c03291 4894 last_sector =
0e6e0271 4895 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
09c9e5fa 4896 * new_data_disks - 1),
911d4ee8 4897 1, &dd_idx, NULL);
58c0fed4
AN
4898 if (last_sector >= mddev->dev_sectors)
4899 last_sector = mddev->dev_sectors - 1;
52c03291 4900 while (first_sector <= last_sector) {
a8c906ca 4901 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
52c03291
N
4902 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4903 set_bit(STRIPE_HANDLE, &sh->state);
4904 release_stripe(sh);
4905 first_sector += STRIPE_SECTORS;
4906 }
ab69ae12
N
4907 /* Now that the sources are clearly marked, we can release
4908 * the destination stripes
4909 */
4910 while (!list_empty(&stripes)) {
4911 sh = list_entry(stripes.next, struct stripe_head, lru);
4912 list_del_init(&sh->lru);
4913 release_stripe(sh);
4914 }
c6207277
N
4915 /* If this takes us to the resync_max point where we have to pause,
4916 * then we need to write out the superblock.
4917 */
7a661381 4918 sector_nr += reshape_sectors;
c03f6a19
N
4919 if ((sector_nr - mddev->curr_resync_completed) * 2
4920 >= mddev->resync_max - mddev->curr_resync_completed) {
c6207277
N
4921 /* Cannot proceed until we've updated the superblock... */
4922 wait_event(conf->wait_for_overlap,
c91abf5a
N
4923 atomic_read(&conf->reshape_stripes) == 0
4924 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4925 if (atomic_read(&conf->reshape_stripes) != 0)
4926 goto ret;
fef9c61f 4927 mddev->reshape_position = conf->reshape_progress;
75d3da43 4928 mddev->curr_resync_completed = sector_nr;
c8f517c4 4929 conf->reshape_checkpoint = jiffies;
c6207277
N
4930 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4931 md_wakeup_thread(mddev->thread);
4932 wait_event(mddev->sb_wait,
4933 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
c91abf5a
N
4934 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4935 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4936 goto ret;
c6207277 4937 spin_lock_irq(&conf->device_lock);
fef9c61f 4938 conf->reshape_safe = mddev->reshape_position;
c6207277
N
4939 spin_unlock_irq(&conf->device_lock);
4940 wake_up(&conf->wait_for_overlap);
acb180b0 4941 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
c6207277 4942 }
c91abf5a 4943ret:
7a661381 4944 return reshape_sectors;
52c03291
N
4945}
4946
4947/* FIXME go_faster isn't used */
fd01b88c 4948static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
52c03291 4949{
d1688a6d 4950 struct r5conf *conf = mddev->private;
52c03291 4951 struct stripe_head *sh;
58c0fed4 4952 sector_t max_sector = mddev->dev_sectors;
57dab0bd 4953 sector_t sync_blocks;
16a53ecc
N
4954 int still_degraded = 0;
4955 int i;
1da177e4 4956
72626685 4957 if (sector_nr >= max_sector) {
1da177e4 4958 /* just being told to finish up .. nothing much to do */
cea9c228 4959
29269553
N
4960 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4961 end_reshape(conf);
4962 return 0;
4963 }
72626685
N
4964
4965 if (mddev->curr_resync < max_sector) /* aborted */
4966 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4967 &sync_blocks, 1);
16a53ecc 4968 else /* completed sync */
72626685
N
4969 conf->fullsync = 0;
4970 bitmap_close_sync(mddev->bitmap);
4971
1da177e4
LT
4972 return 0;
4973 }
ccfcc3c1 4974
64bd660b
N
4975 /* Allow raid5_quiesce to complete */
4976 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4977
52c03291
N
4978 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4979 return reshape_request(mddev, sector_nr, skipped);
f6705578 4980
c6207277
N
4981 /* No need to check resync_max as we never do more than one
4982 * stripe, and as resync_max will always be on a chunk boundary,
4983 * if the check in md_do_sync didn't fire, there is no chance
4984 * of overstepping resync_max here
4985 */
4986
16a53ecc 4987 /* if there is too many failed drives and we are trying
1da177e4
LT
4988 * to resync, then assert that we are finished, because there is
4989 * nothing we can do.
4990 */
3285edf1 4991 if (mddev->degraded >= conf->max_degraded &&
16a53ecc 4992 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
58c0fed4 4993 sector_t rv = mddev->dev_sectors - sector_nr;
57afd89f 4994 *skipped = 1;
1da177e4
LT
4995 return rv;
4996 }
6f608040 4997 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
4998 !conf->fullsync &&
4999 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
5000 sync_blocks >= STRIPE_SECTORS) {
72626685
N
5001 /* we can skip this block, and probably more */
5002 sync_blocks /= STRIPE_SECTORS;
5003 *skipped = 1;
5004 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
5005 }
1da177e4 5006
b47490c9
N
5007 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
5008
a8c906ca 5009 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
1da177e4 5010 if (sh == NULL) {
a8c906ca 5011 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
1da177e4 5012 /* make sure we don't swamp the stripe cache if someone else
16a53ecc 5013 * is trying to get access
1da177e4 5014 */
66c006a5 5015 schedule_timeout_uninterruptible(1);
1da177e4 5016 }
16a53ecc
N
5017 /* Need to check if array will still be degraded after recovery/resync
5018 * We don't need to check the 'failed' flag as when that gets set,
5019 * recovery aborts.
5020 */
f001a70c 5021 for (i = 0; i < conf->raid_disks; i++)
16a53ecc
N
5022 if (conf->disks[i].rdev == NULL)
5023 still_degraded = 1;
5024
5025 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
5026
83206d66 5027 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
1da177e4 5028
1442577b 5029 handle_stripe(sh);
1da177e4
LT
5030 release_stripe(sh);
5031
5032 return STRIPE_SECTORS;
5033}
5034
d1688a6d 5035static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
46031f9a
RBJ
5036{
5037 /* We may not be able to submit a whole bio at once as there
5038 * may not be enough stripe_heads available.
5039 * We cannot pre-allocate enough stripe_heads as we may need
5040 * more than exist in the cache (if we allow ever large chunks).
5041 * So we do one stripe head at a time and record in
5042 * ->bi_hw_segments how many have been done.
5043 *
5044 * We *know* that this entire raid_bio is in one chunk, so
5045 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5046 */
5047 struct stripe_head *sh;
911d4ee8 5048 int dd_idx;
46031f9a
RBJ
5049 sector_t sector, logical_sector, last_sector;
5050 int scnt = 0;
5051 int remaining;
5052 int handled = 0;
5053
5054 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
112bf897 5055 sector = raid5_compute_sector(conf, logical_sector,
911d4ee8 5056 0, &dd_idx, NULL);
f73a1c7d 5057 last_sector = bio_end_sector(raid_bio);
46031f9a
RBJ
5058
5059 for (; logical_sector < last_sector;
387bb173
NB
5060 logical_sector += STRIPE_SECTORS,
5061 sector += STRIPE_SECTORS,
5062 scnt++) {
46031f9a 5063
e7836bd6 5064 if (scnt < raid5_bi_processed_stripes(raid_bio))
46031f9a
RBJ
5065 /* already done this stripe */
5066 continue;
5067
a8c906ca 5068 sh = get_active_stripe(conf, sector, 0, 1, 0);
46031f9a
RBJ
5069
5070 if (!sh) {
5071 /* failed to get a stripe - must wait */
e7836bd6 5072 raid5_set_bi_processed_stripes(raid_bio, scnt);
46031f9a
RBJ
5073 conf->retry_read_aligned = raid_bio;
5074 return handled;
5075 }
5076
387bb173
NB
5077 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
5078 release_stripe(sh);
e7836bd6 5079 raid5_set_bi_processed_stripes(raid_bio, scnt);
387bb173
NB
5080 conf->retry_read_aligned = raid_bio;
5081 return handled;
5082 }
5083
3f9e7c14 5084 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
36d1c647 5085 handle_stripe(sh);
46031f9a
RBJ
5086 release_stripe(sh);
5087 handled++;
5088 }
e7836bd6 5089 remaining = raid5_dec_bi_active_stripes(raid_bio);
0a82a8d1
LT
5090 if (remaining == 0) {
5091 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
5092 raid_bio, 0);
0e13fe23 5093 bio_endio(raid_bio, 0);
0a82a8d1 5094 }
46031f9a
RBJ
5095 if (atomic_dec_and_test(&conf->active_aligned_reads))
5096 wake_up(&conf->wait_for_stripe);
5097 return handled;
5098}
5099
bfc90cb0 5100static int handle_active_stripes(struct r5conf *conf, int group,
566c09c5
SL
5101 struct r5worker *worker,
5102 struct list_head *temp_inactive_list)
46a06401
SL
5103{
5104 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
566c09c5
SL
5105 int i, batch_size = 0, hash;
5106 bool release_inactive = false;
46a06401
SL
5107
5108 while (batch_size < MAX_STRIPE_BATCH &&
851c30c9 5109 (sh = __get_priority_stripe(conf, group)) != NULL)
46a06401
SL
5110 batch[batch_size++] = sh;
5111
566c09c5
SL
5112 if (batch_size == 0) {
5113 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5114 if (!list_empty(temp_inactive_list + i))
5115 break;
5116 if (i == NR_STRIPE_HASH_LOCKS)
5117 return batch_size;
5118 release_inactive = true;
5119 }
46a06401
SL
5120 spin_unlock_irq(&conf->device_lock);
5121
566c09c5
SL
5122 release_inactive_stripe_list(conf, temp_inactive_list,
5123 NR_STRIPE_HASH_LOCKS);
5124
5125 if (release_inactive) {
5126 spin_lock_irq(&conf->device_lock);
5127 return 0;
5128 }
5129
46a06401
SL
5130 for (i = 0; i < batch_size; i++)
5131 handle_stripe(batch[i]);
5132
5133 cond_resched();
5134
5135 spin_lock_irq(&conf->device_lock);
566c09c5
SL
5136 for (i = 0; i < batch_size; i++) {
5137 hash = batch[i]->hash_lock_index;
5138 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
5139 }
46a06401
SL
5140 return batch_size;
5141}
46031f9a 5142
851c30c9
SL
5143static void raid5_do_work(struct work_struct *work)
5144{
5145 struct r5worker *worker = container_of(work, struct r5worker, work);
5146 struct r5worker_group *group = worker->group;
5147 struct r5conf *conf = group->conf;
5148 int group_id = group - conf->worker_groups;
5149 int handled;
5150 struct blk_plug plug;
5151
5152 pr_debug("+++ raid5worker active\n");
5153
5154 blk_start_plug(&plug);
5155 handled = 0;
5156 spin_lock_irq(&conf->device_lock);
5157 while (1) {
5158 int batch_size, released;
5159
566c09c5 5160 released = release_stripe_list(conf, worker->temp_inactive_list);
851c30c9 5161
566c09c5
SL
5162 batch_size = handle_active_stripes(conf, group_id, worker,
5163 worker->temp_inactive_list);
bfc90cb0 5164 worker->working = false;
851c30c9
SL
5165 if (!batch_size && !released)
5166 break;
5167 handled += batch_size;
5168 }
5169 pr_debug("%d stripes handled\n", handled);
5170
5171 spin_unlock_irq(&conf->device_lock);
5172 blk_finish_plug(&plug);
5173
5174 pr_debug("--- raid5worker inactive\n");
5175}
5176
1da177e4
LT
5177/*
5178 * This is our raid5 kernel thread.
5179 *
5180 * We scan the hash table for stripes which can be handled now.
5181 * During the scan, completed stripes are saved for us by the interrupt
5182 * handler, so that they will not have to wait for our next wakeup.
5183 */
4ed8731d 5184static void raid5d(struct md_thread *thread)
1da177e4 5185{
4ed8731d 5186 struct mddev *mddev = thread->mddev;
d1688a6d 5187 struct r5conf *conf = mddev->private;
1da177e4 5188 int handled;
e1dfa0a2 5189 struct blk_plug plug;
1da177e4 5190
45b4233c 5191 pr_debug("+++ raid5d active\n");
1da177e4
LT
5192
5193 md_check_recovery(mddev);
1da177e4 5194
e1dfa0a2 5195 blk_start_plug(&plug);
1da177e4
LT
5196 handled = 0;
5197 spin_lock_irq(&conf->device_lock);
5198 while (1) {
46031f9a 5199 struct bio *bio;
773ca82f
SL
5200 int batch_size, released;
5201
566c09c5 5202 released = release_stripe_list(conf, conf->temp_inactive_list);
1da177e4 5203
0021b7bc 5204 if (
7c13edc8
N
5205 !list_empty(&conf->bitmap_list)) {
5206 /* Now is a good time to flush some bitmap updates */
5207 conf->seq_flush++;
700e432d 5208 spin_unlock_irq(&conf->device_lock);
72626685 5209 bitmap_unplug(mddev->bitmap);
700e432d 5210 spin_lock_irq(&conf->device_lock);
7c13edc8 5211 conf->seq_write = conf->seq_flush;
566c09c5 5212 activate_bit_delay(conf, conf->temp_inactive_list);
72626685 5213 }
0021b7bc 5214 raid5_activate_delayed(conf);
72626685 5215
46031f9a
RBJ
5216 while ((bio = remove_bio_from_retry(conf))) {
5217 int ok;
5218 spin_unlock_irq(&conf->device_lock);
5219 ok = retry_aligned_read(conf, bio);
5220 spin_lock_irq(&conf->device_lock);
5221 if (!ok)
5222 break;
5223 handled++;
5224 }
5225
566c09c5
SL
5226 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
5227 conf->temp_inactive_list);
773ca82f 5228 if (!batch_size && !released)
1da177e4 5229 break;
46a06401 5230 handled += batch_size;
1da177e4 5231
46a06401
SL
5232 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
5233 spin_unlock_irq(&conf->device_lock);
de393cde 5234 md_check_recovery(mddev);
46a06401
SL
5235 spin_lock_irq(&conf->device_lock);
5236 }
1da177e4 5237 }
45b4233c 5238 pr_debug("%d stripes handled\n", handled);
1da177e4
LT
5239
5240 spin_unlock_irq(&conf->device_lock);
5241
c9f21aaf 5242 async_tx_issue_pending_all();
e1dfa0a2 5243 blk_finish_plug(&plug);
1da177e4 5244
45b4233c 5245 pr_debug("--- raid5d inactive\n");
1da177e4
LT
5246}
5247
3f294f4f 5248static ssize_t
fd01b88c 5249raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
3f294f4f 5250{
d1688a6d 5251 struct r5conf *conf = mddev->private;
96de1e66
N
5252 if (conf)
5253 return sprintf(page, "%d\n", conf->max_nr_stripes);
5254 else
5255 return 0;
3f294f4f
N
5256}
5257
c41d4ac4 5258int
fd01b88c 5259raid5_set_cache_size(struct mddev *mddev, int size)
3f294f4f 5260{
d1688a6d 5261 struct r5conf *conf = mddev->private;
b5470dc5 5262 int err;
566c09c5 5263 int hash;
b5470dc5 5264
c41d4ac4 5265 if (size <= 16 || size > 32768)
3f294f4f 5266 return -EINVAL;
566c09c5 5267 hash = (conf->max_nr_stripes - 1) % NR_STRIPE_HASH_LOCKS;
c41d4ac4 5268 while (size < conf->max_nr_stripes) {
566c09c5 5269 if (drop_one_stripe(conf, hash))
3f294f4f
N
5270 conf->max_nr_stripes--;
5271 else
5272 break;
566c09c5
SL
5273 hash--;
5274 if (hash < 0)
5275 hash = NR_STRIPE_HASH_LOCKS - 1;
3f294f4f 5276 }
b5470dc5
DW
5277 err = md_allow_write(mddev);
5278 if (err)
5279 return err;
566c09c5 5280 hash = conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
c41d4ac4 5281 while (size > conf->max_nr_stripes) {
566c09c5 5282 if (grow_one_stripe(conf, hash))
3f294f4f
N
5283 conf->max_nr_stripes++;
5284 else break;
566c09c5 5285 hash = (hash + 1) % NR_STRIPE_HASH_LOCKS;
3f294f4f 5286 }
c41d4ac4
N
5287 return 0;
5288}
5289EXPORT_SYMBOL(raid5_set_cache_size);
5290
5291static ssize_t
fd01b88c 5292raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
c41d4ac4 5293{
d1688a6d 5294 struct r5conf *conf = mddev->private;
c41d4ac4
N
5295 unsigned long new;
5296 int err;
5297
5298 if (len >= PAGE_SIZE)
5299 return -EINVAL;
5300 if (!conf)
5301 return -ENODEV;
5302
b29bebd6 5303 if (kstrtoul(page, 10, &new))
c41d4ac4
N
5304 return -EINVAL;
5305 err = raid5_set_cache_size(mddev, new);
5306 if (err)
5307 return err;
3f294f4f
N
5308 return len;
5309}
007583c9 5310
96de1e66
N
5311static struct md_sysfs_entry
5312raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
5313 raid5_show_stripe_cache_size,
5314 raid5_store_stripe_cache_size);
3f294f4f 5315
8b3e6cdc 5316static ssize_t
fd01b88c 5317raid5_show_preread_threshold(struct mddev *mddev, char *page)
8b3e6cdc 5318{
d1688a6d 5319 struct r5conf *conf = mddev->private;
8b3e6cdc
DW
5320 if (conf)
5321 return sprintf(page, "%d\n", conf->bypass_threshold);
5322 else
5323 return 0;
5324}
5325
5326static ssize_t
fd01b88c 5327raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
8b3e6cdc 5328{
d1688a6d 5329 struct r5conf *conf = mddev->private;
4ef197d8 5330 unsigned long new;
8b3e6cdc
DW
5331 if (len >= PAGE_SIZE)
5332 return -EINVAL;
5333 if (!conf)
5334 return -ENODEV;
5335
b29bebd6 5336 if (kstrtoul(page, 10, &new))
8b3e6cdc 5337 return -EINVAL;
4ef197d8 5338 if (new > conf->max_nr_stripes)
8b3e6cdc
DW
5339 return -EINVAL;
5340 conf->bypass_threshold = new;
5341 return len;
5342}
5343
5344static struct md_sysfs_entry
5345raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
5346 S_IRUGO | S_IWUSR,
5347 raid5_show_preread_threshold,
5348 raid5_store_preread_threshold);
5349
3f294f4f 5350static ssize_t
fd01b88c 5351stripe_cache_active_show(struct mddev *mddev, char *page)
3f294f4f 5352{
d1688a6d 5353 struct r5conf *conf = mddev->private;
96de1e66
N
5354 if (conf)
5355 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
5356 else
5357 return 0;
3f294f4f
N
5358}
5359
96de1e66
N
5360static struct md_sysfs_entry
5361raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
3f294f4f 5362
b721420e
SL
5363static ssize_t
5364raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
5365{
5366 struct r5conf *conf = mddev->private;
5367 if (conf)
5368 return sprintf(page, "%d\n", conf->worker_cnt_per_group);
5369 else
5370 return 0;
5371}
5372
60aaf933 5373static int alloc_thread_groups(struct r5conf *conf, int cnt,
5374 int *group_cnt,
5375 int *worker_cnt_per_group,
5376 struct r5worker_group **worker_groups);
b721420e
SL
5377static ssize_t
5378raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
5379{
5380 struct r5conf *conf = mddev->private;
5381 unsigned long new;
5382 int err;
60aaf933 5383 struct r5worker_group *new_groups, *old_groups;
5384 int group_cnt, worker_cnt_per_group;
b721420e
SL
5385
5386 if (len >= PAGE_SIZE)
5387 return -EINVAL;
5388 if (!conf)
5389 return -ENODEV;
5390
5391 if (kstrtoul(page, 10, &new))
5392 return -EINVAL;
5393
5394 if (new == conf->worker_cnt_per_group)
5395 return len;
5396
5397 mddev_suspend(mddev);
5398
5399 old_groups = conf->worker_groups;
d206dcfa 5400 if (old_groups)
5401 flush_workqueue(raid5_wq);
5402
60aaf933 5403 err = alloc_thread_groups(conf, new,
5404 &group_cnt, &worker_cnt_per_group,
5405 &new_groups);
5406 if (!err) {
5407 spin_lock_irq(&conf->device_lock);
5408 conf->group_cnt = group_cnt;
5409 conf->worker_cnt_per_group = worker_cnt_per_group;
5410 conf->worker_groups = new_groups;
5411 spin_unlock_irq(&conf->device_lock);
b721420e 5412
b721420e
SL
5413 if (old_groups)
5414 kfree(old_groups[0].workers);
5415 kfree(old_groups);
5416 }
5417
5418 mddev_resume(mddev);
5419
5420 if (err)
5421 return err;
5422 return len;
5423}
5424
5425static struct md_sysfs_entry
5426raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
5427 raid5_show_group_thread_cnt,
5428 raid5_store_group_thread_cnt);
5429
007583c9 5430static struct attribute *raid5_attrs[] = {
3f294f4f
N
5431 &raid5_stripecache_size.attr,
5432 &raid5_stripecache_active.attr,
8b3e6cdc 5433 &raid5_preread_bypass_threshold.attr,
b721420e 5434 &raid5_group_thread_cnt.attr,
3f294f4f
N
5435 NULL,
5436};
007583c9
N
5437static struct attribute_group raid5_attrs_group = {
5438 .name = NULL,
5439 .attrs = raid5_attrs,
3f294f4f
N
5440};
5441
60aaf933 5442static int alloc_thread_groups(struct r5conf *conf, int cnt,
5443 int *group_cnt,
5444 int *worker_cnt_per_group,
5445 struct r5worker_group **worker_groups)
851c30c9 5446{
566c09c5 5447 int i, j, k;
851c30c9
SL
5448 ssize_t size;
5449 struct r5worker *workers;
5450
60aaf933 5451 *worker_cnt_per_group = cnt;
851c30c9 5452 if (cnt == 0) {
60aaf933 5453 *group_cnt = 0;
5454 *worker_groups = NULL;
851c30c9
SL
5455 return 0;
5456 }
60aaf933 5457 *group_cnt = num_possible_nodes();
851c30c9 5458 size = sizeof(struct r5worker) * cnt;
60aaf933 5459 workers = kzalloc(size * *group_cnt, GFP_NOIO);
5460 *worker_groups = kzalloc(sizeof(struct r5worker_group) *
5461 *group_cnt, GFP_NOIO);
5462 if (!*worker_groups || !workers) {
851c30c9 5463 kfree(workers);
60aaf933 5464 kfree(*worker_groups);
851c30c9
SL
5465 return -ENOMEM;
5466 }
5467
60aaf933 5468 for (i = 0; i < *group_cnt; i++) {
851c30c9
SL
5469 struct r5worker_group *group;
5470
0c775d52 5471 group = &(*worker_groups)[i];
851c30c9
SL
5472 INIT_LIST_HEAD(&group->handle_list);
5473 group->conf = conf;
5474 group->workers = workers + i * cnt;
5475
5476 for (j = 0; j < cnt; j++) {
566c09c5
SL
5477 struct r5worker *worker = group->workers + j;
5478 worker->group = group;
5479 INIT_WORK(&worker->work, raid5_do_work);
5480
5481 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
5482 INIT_LIST_HEAD(worker->temp_inactive_list + k);
851c30c9
SL
5483 }
5484 }
5485
5486 return 0;
5487}
5488
5489static void free_thread_groups(struct r5conf *conf)
5490{
5491 if (conf->worker_groups)
5492 kfree(conf->worker_groups[0].workers);
5493 kfree(conf->worker_groups);
5494 conf->worker_groups = NULL;
5495}
5496
80c3a6ce 5497static sector_t
fd01b88c 5498raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
80c3a6ce 5499{
d1688a6d 5500 struct r5conf *conf = mddev->private;
80c3a6ce
DW
5501
5502 if (!sectors)
5503 sectors = mddev->dev_sectors;
5e5e3e78 5504 if (!raid_disks)
7ec05478 5505 /* size is defined by the smallest of previous and new size */
5e5e3e78 5506 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
80c3a6ce 5507
9d8f0363 5508 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
664e7c41 5509 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
80c3a6ce
DW
5510 return sectors * (raid_disks - conf->max_degraded);
5511}
5512
d1688a6d 5513static void raid5_free_percpu(struct r5conf *conf)
36d1c647
DW
5514{
5515 struct raid5_percpu *percpu;
5516 unsigned long cpu;
5517
5518 if (!conf->percpu)
5519 return;
5520
5521 get_online_cpus();
5522 for_each_possible_cpu(cpu) {
5523 percpu = per_cpu_ptr(conf->percpu, cpu);
5524 safe_put_page(percpu->spare_page);
d6f38f31 5525 kfree(percpu->scribble);
36d1c647
DW
5526 }
5527#ifdef CONFIG_HOTPLUG_CPU
5528 unregister_cpu_notifier(&conf->cpu_notify);
5529#endif
5530 put_online_cpus();
5531
5532 free_percpu(conf->percpu);
5533}
5534
d1688a6d 5535static void free_conf(struct r5conf *conf)
95fc17aa 5536{
851c30c9 5537 free_thread_groups(conf);
95fc17aa 5538 shrink_stripes(conf);
36d1c647 5539 raid5_free_percpu(conf);
95fc17aa
DW
5540 kfree(conf->disks);
5541 kfree(conf->stripe_hashtbl);
5542 kfree(conf);
5543}
5544
36d1c647
DW
5545#ifdef CONFIG_HOTPLUG_CPU
5546static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5547 void *hcpu)
5548{
d1688a6d 5549 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
36d1c647
DW
5550 long cpu = (long)hcpu;
5551 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5552
5553 switch (action) {
5554 case CPU_UP_PREPARE:
5555 case CPU_UP_PREPARE_FROZEN:
d6f38f31 5556 if (conf->level == 6 && !percpu->spare_page)
36d1c647 5557 percpu->spare_page = alloc_page(GFP_KERNEL);
d6f38f31
DW
5558 if (!percpu->scribble)
5559 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5560
5561 if (!percpu->scribble ||
5562 (conf->level == 6 && !percpu->spare_page)) {
5563 safe_put_page(percpu->spare_page);
5564 kfree(percpu->scribble);
36d1c647
DW
5565 pr_err("%s: failed memory allocation for cpu%ld\n",
5566 __func__, cpu);
55af6bb5 5567 return notifier_from_errno(-ENOMEM);
36d1c647
DW
5568 }
5569 break;
5570 case CPU_DEAD:
5571 case CPU_DEAD_FROZEN:
5572 safe_put_page(percpu->spare_page);
d6f38f31 5573 kfree(percpu->scribble);
36d1c647 5574 percpu->spare_page = NULL;
d6f38f31 5575 percpu->scribble = NULL;
36d1c647
DW
5576 break;
5577 default:
5578 break;
5579 }
5580 return NOTIFY_OK;
5581}
5582#endif
5583
d1688a6d 5584static int raid5_alloc_percpu(struct r5conf *conf)
36d1c647
DW
5585{
5586 unsigned long cpu;
5587 struct page *spare_page;
a29d8b8e 5588 struct raid5_percpu __percpu *allcpus;
d6f38f31 5589 void *scribble;
36d1c647
DW
5590 int err;
5591
36d1c647
DW
5592 allcpus = alloc_percpu(struct raid5_percpu);
5593 if (!allcpus)
5594 return -ENOMEM;
5595 conf->percpu = allcpus;
5596
5597 get_online_cpus();
5598 err = 0;
5599 for_each_present_cpu(cpu) {
d6f38f31
DW
5600 if (conf->level == 6) {
5601 spare_page = alloc_page(GFP_KERNEL);
5602 if (!spare_page) {
5603 err = -ENOMEM;
5604 break;
5605 }
5606 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5607 }
5e5e3e78 5608 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
d6f38f31 5609 if (!scribble) {
36d1c647
DW
5610 err = -ENOMEM;
5611 break;
5612 }
d6f38f31 5613 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
36d1c647
DW
5614 }
5615#ifdef CONFIG_HOTPLUG_CPU
5616 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5617 conf->cpu_notify.priority = 0;
5618 if (err == 0)
5619 err = register_cpu_notifier(&conf->cpu_notify);
5620#endif
5621 put_online_cpus();
5622
5623 return err;
5624}
5625
d1688a6d 5626static struct r5conf *setup_conf(struct mddev *mddev)
1da177e4 5627{
d1688a6d 5628 struct r5conf *conf;
5e5e3e78 5629 int raid_disk, memory, max_disks;
3cb03002 5630 struct md_rdev *rdev;
1da177e4 5631 struct disk_info *disk;
0232605d 5632 char pers_name[6];
566c09c5 5633 int i;
60aaf933 5634 int group_cnt, worker_cnt_per_group;
5635 struct r5worker_group *new_group;
1da177e4 5636
91adb564
N
5637 if (mddev->new_level != 5
5638 && mddev->new_level != 4
5639 && mddev->new_level != 6) {
0c55e022 5640 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
91adb564
N
5641 mdname(mddev), mddev->new_level);
5642 return ERR_PTR(-EIO);
1da177e4 5643 }
91adb564
N
5644 if ((mddev->new_level == 5
5645 && !algorithm_valid_raid5(mddev->new_layout)) ||
5646 (mddev->new_level == 6
5647 && !algorithm_valid_raid6(mddev->new_layout))) {
0c55e022 5648 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
91adb564
N
5649 mdname(mddev), mddev->new_layout);
5650 return ERR_PTR(-EIO);
99c0fb5f 5651 }
91adb564 5652 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
0c55e022 5653 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
91adb564
N
5654 mdname(mddev), mddev->raid_disks);
5655 return ERR_PTR(-EINVAL);
4bbf3771
N
5656 }
5657
664e7c41
AN
5658 if (!mddev->new_chunk_sectors ||
5659 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5660 !is_power_of_2(mddev->new_chunk_sectors)) {
0c55e022
N
5661 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5662 mdname(mddev), mddev->new_chunk_sectors << 9);
91adb564 5663 return ERR_PTR(-EINVAL);
f6705578
N
5664 }
5665
d1688a6d 5666 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
91adb564 5667 if (conf == NULL)
1da177e4 5668 goto abort;
851c30c9 5669 /* Don't enable multi-threading by default*/
60aaf933 5670 if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group,
5671 &new_group)) {
5672 conf->group_cnt = group_cnt;
5673 conf->worker_cnt_per_group = worker_cnt_per_group;
5674 conf->worker_groups = new_group;
5675 } else
851c30c9 5676 goto abort;
f5efd45a 5677 spin_lock_init(&conf->device_lock);
c46501b2 5678 seqcount_init(&conf->gen_lock);
f5efd45a
DW
5679 init_waitqueue_head(&conf->wait_for_stripe);
5680 init_waitqueue_head(&conf->wait_for_overlap);
5681 INIT_LIST_HEAD(&conf->handle_list);
5682 INIT_LIST_HEAD(&conf->hold_list);
5683 INIT_LIST_HEAD(&conf->delayed_list);
5684 INIT_LIST_HEAD(&conf->bitmap_list);
773ca82f 5685 init_llist_head(&conf->released_stripes);
f5efd45a
DW
5686 atomic_set(&conf->active_stripes, 0);
5687 atomic_set(&conf->preread_active_stripes, 0);
5688 atomic_set(&conf->active_aligned_reads, 0);
5689 conf->bypass_threshold = BYPASS_THRESHOLD;
d890fa2b 5690 conf->recovery_disabled = mddev->recovery_disabled - 1;
91adb564
N
5691
5692 conf->raid_disks = mddev->raid_disks;
5693 if (mddev->reshape_position == MaxSector)
5694 conf->previous_raid_disks = mddev->raid_disks;
5695 else
f6705578 5696 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
5e5e3e78
N
5697 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5698 conf->scribble_len = scribble_len(max_disks);
f6705578 5699
5e5e3e78 5700 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
b55e6bfc
N
5701 GFP_KERNEL);
5702 if (!conf->disks)
5703 goto abort;
9ffae0cf 5704
1da177e4
LT
5705 conf->mddev = mddev;
5706
fccddba0 5707 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
1da177e4 5708 goto abort;
1da177e4 5709
566c09c5
SL
5710 /* We init hash_locks[0] separately to that it can be used
5711 * as the reference lock in the spin_lock_nest_lock() call
5712 * in lock_all_device_hash_locks_irq in order to convince
5713 * lockdep that we know what we are doing.
5714 */
5715 spin_lock_init(conf->hash_locks);
5716 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
5717 spin_lock_init(conf->hash_locks + i);
5718
5719 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5720 INIT_LIST_HEAD(conf->inactive_list + i);
5721
5722 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5723 INIT_LIST_HEAD(conf->temp_inactive_list + i);
5724
36d1c647
DW
5725 conf->level = mddev->new_level;
5726 if (raid5_alloc_percpu(conf) != 0)
5727 goto abort;
5728
0c55e022 5729 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
1da177e4 5730
dafb20fa 5731 rdev_for_each(rdev, mddev) {
1da177e4 5732 raid_disk = rdev->raid_disk;
5e5e3e78 5733 if (raid_disk >= max_disks
1da177e4
LT
5734 || raid_disk < 0)
5735 continue;
5736 disk = conf->disks + raid_disk;
5737
17045f52
N
5738 if (test_bit(Replacement, &rdev->flags)) {
5739 if (disk->replacement)
5740 goto abort;
5741 disk->replacement = rdev;
5742 } else {
5743 if (disk->rdev)
5744 goto abort;
5745 disk->rdev = rdev;
5746 }
1da177e4 5747
b2d444d7 5748 if (test_bit(In_sync, &rdev->flags)) {
1da177e4 5749 char b[BDEVNAME_SIZE];
0c55e022
N
5750 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5751 " disk %d\n",
5752 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
d6b212f4 5753 } else if (rdev->saved_raid_disk != raid_disk)
8c2e870a
NB
5754 /* Cannot rely on bitmap to complete recovery */
5755 conf->fullsync = 1;
1da177e4
LT
5756 }
5757
09c9e5fa 5758 conf->chunk_sectors = mddev->new_chunk_sectors;
91adb564 5759 conf->level = mddev->new_level;
16a53ecc
N
5760 if (conf->level == 6)
5761 conf->max_degraded = 2;
5762 else
5763 conf->max_degraded = 1;
91adb564 5764 conf->algorithm = mddev->new_layout;
fef9c61f 5765 conf->reshape_progress = mddev->reshape_position;
e183eaed 5766 if (conf->reshape_progress != MaxSector) {
09c9e5fa 5767 conf->prev_chunk_sectors = mddev->chunk_sectors;
e183eaed
N
5768 conf->prev_algo = mddev->layout;
5769 }
1da177e4 5770
91adb564 5771 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
5e5e3e78 5772 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4bda556a 5773 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
566c09c5 5774 if (grow_stripes(conf, NR_STRIPES)) {
91adb564 5775 printk(KERN_ERR
0c55e022
N
5776 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5777 mdname(mddev), memory);
91adb564
N
5778 goto abort;
5779 } else
0c55e022
N
5780 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5781 mdname(mddev), memory);
1da177e4 5782
0232605d
N
5783 sprintf(pers_name, "raid%d", mddev->new_level);
5784 conf->thread = md_register_thread(raid5d, mddev, pers_name);
91adb564
N
5785 if (!conf->thread) {
5786 printk(KERN_ERR
0c55e022 5787 "md/raid:%s: couldn't allocate thread.\n",
91adb564 5788 mdname(mddev));
16a53ecc
N
5789 goto abort;
5790 }
91adb564
N
5791
5792 return conf;
5793
5794 abort:
5795 if (conf) {
95fc17aa 5796 free_conf(conf);
91adb564
N
5797 return ERR_PTR(-EIO);
5798 } else
5799 return ERR_PTR(-ENOMEM);
5800}
5801
c148ffdc
N
5802
5803static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5804{
5805 switch (algo) {
5806 case ALGORITHM_PARITY_0:
5807 if (raid_disk < max_degraded)
5808 return 1;
5809 break;
5810 case ALGORITHM_PARITY_N:
5811 if (raid_disk >= raid_disks - max_degraded)
5812 return 1;
5813 break;
5814 case ALGORITHM_PARITY_0_6:
5815 if (raid_disk == 0 ||
5816 raid_disk == raid_disks - 1)
5817 return 1;
5818 break;
5819 case ALGORITHM_LEFT_ASYMMETRIC_6:
5820 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5821 case ALGORITHM_LEFT_SYMMETRIC_6:
5822 case ALGORITHM_RIGHT_SYMMETRIC_6:
5823 if (raid_disk == raid_disks - 1)
5824 return 1;
5825 }
5826 return 0;
5827}
5828
fd01b88c 5829static int run(struct mddev *mddev)
91adb564 5830{
d1688a6d 5831 struct r5conf *conf;
9f7c2220 5832 int working_disks = 0;
c148ffdc 5833 int dirty_parity_disks = 0;
3cb03002 5834 struct md_rdev *rdev;
c148ffdc 5835 sector_t reshape_offset = 0;
17045f52 5836 int i;
b5254dd5
N
5837 long long min_offset_diff = 0;
5838 int first = 1;
91adb564 5839
8c6ac868 5840 if (mddev->recovery_cp != MaxSector)
0c55e022 5841 printk(KERN_NOTICE "md/raid:%s: not clean"
8c6ac868
AN
5842 " -- starting background reconstruction\n",
5843 mdname(mddev));
b5254dd5
N
5844
5845 rdev_for_each(rdev, mddev) {
5846 long long diff;
5847 if (rdev->raid_disk < 0)
5848 continue;
5849 diff = (rdev->new_data_offset - rdev->data_offset);
5850 if (first) {
5851 min_offset_diff = diff;
5852 first = 0;
5853 } else if (mddev->reshape_backwards &&
5854 diff < min_offset_diff)
5855 min_offset_diff = diff;
5856 else if (!mddev->reshape_backwards &&
5857 diff > min_offset_diff)
5858 min_offset_diff = diff;
5859 }
5860
91adb564
N
5861 if (mddev->reshape_position != MaxSector) {
5862 /* Check that we can continue the reshape.
b5254dd5
N
5863 * Difficulties arise if the stripe we would write to
5864 * next is at or after the stripe we would read from next.
5865 * For a reshape that changes the number of devices, this
5866 * is only possible for a very short time, and mdadm makes
5867 * sure that time appears to have past before assembling
5868 * the array. So we fail if that time hasn't passed.
5869 * For a reshape that keeps the number of devices the same
5870 * mdadm must be monitoring the reshape can keeping the
5871 * critical areas read-only and backed up. It will start
5872 * the array in read-only mode, so we check for that.
91adb564
N
5873 */
5874 sector_t here_new, here_old;
5875 int old_disks;
18b00334 5876 int max_degraded = (mddev->level == 6 ? 2 : 1);
91adb564 5877
88ce4930 5878 if (mddev->new_level != mddev->level) {
0c55e022 5879 printk(KERN_ERR "md/raid:%s: unsupported reshape "
91adb564
N
5880 "required - aborting.\n",
5881 mdname(mddev));
5882 return -EINVAL;
5883 }
91adb564
N
5884 old_disks = mddev->raid_disks - mddev->delta_disks;
5885 /* reshape_position must be on a new-stripe boundary, and one
5886 * further up in new geometry must map after here in old
5887 * geometry.
5888 */
5889 here_new = mddev->reshape_position;
664e7c41 5890 if (sector_div(here_new, mddev->new_chunk_sectors *
91adb564 5891 (mddev->raid_disks - max_degraded))) {
0c55e022
N
5892 printk(KERN_ERR "md/raid:%s: reshape_position not "
5893 "on a stripe boundary\n", mdname(mddev));
91adb564
N
5894 return -EINVAL;
5895 }
c148ffdc 5896 reshape_offset = here_new * mddev->new_chunk_sectors;
91adb564
N
5897 /* here_new is the stripe we will write to */
5898 here_old = mddev->reshape_position;
9d8f0363 5899 sector_div(here_old, mddev->chunk_sectors *
91adb564
N
5900 (old_disks-max_degraded));
5901 /* here_old is the first stripe that we might need to read
5902 * from */
67ac6011 5903 if (mddev->delta_disks == 0) {
b5254dd5
N
5904 if ((here_new * mddev->new_chunk_sectors !=
5905 here_old * mddev->chunk_sectors)) {
5906 printk(KERN_ERR "md/raid:%s: reshape position is"
5907 " confused - aborting\n", mdname(mddev));
5908 return -EINVAL;
5909 }
67ac6011 5910 /* We cannot be sure it is safe to start an in-place
b5254dd5 5911 * reshape. It is only safe if user-space is monitoring
67ac6011
N
5912 * and taking constant backups.
5913 * mdadm always starts a situation like this in
5914 * readonly mode so it can take control before
5915 * allowing any writes. So just check for that.
5916 */
b5254dd5
N
5917 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5918 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5919 /* not really in-place - so OK */;
5920 else if (mddev->ro == 0) {
5921 printk(KERN_ERR "md/raid:%s: in-place reshape "
5922 "must be started in read-only mode "
5923 "- aborting\n",
0c55e022 5924 mdname(mddev));
67ac6011
N
5925 return -EINVAL;
5926 }
2c810cdd 5927 } else if (mddev->reshape_backwards
b5254dd5 5928 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
67ac6011
N
5929 here_old * mddev->chunk_sectors)
5930 : (here_new * mddev->new_chunk_sectors >=
b5254dd5 5931 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
91adb564 5932 /* Reading from the same stripe as writing to - bad */
0c55e022
N
5933 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5934 "auto-recovery - aborting.\n",
5935 mdname(mddev));
91adb564
N
5936 return -EINVAL;
5937 }
0c55e022
N
5938 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5939 mdname(mddev));
91adb564
N
5940 /* OK, we should be able to continue; */
5941 } else {
5942 BUG_ON(mddev->level != mddev->new_level);
5943 BUG_ON(mddev->layout != mddev->new_layout);
664e7c41 5944 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
91adb564 5945 BUG_ON(mddev->delta_disks != 0);
1da177e4 5946 }
91adb564 5947
245f46c2
N
5948 if (mddev->private == NULL)
5949 conf = setup_conf(mddev);
5950 else
5951 conf = mddev->private;
5952
91adb564
N
5953 if (IS_ERR(conf))
5954 return PTR_ERR(conf);
5955
b5254dd5 5956 conf->min_offset_diff = min_offset_diff;
91adb564
N
5957 mddev->thread = conf->thread;
5958 conf->thread = NULL;
5959 mddev->private = conf;
5960
17045f52
N
5961 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5962 i++) {
5963 rdev = conf->disks[i].rdev;
5964 if (!rdev && conf->disks[i].replacement) {
5965 /* The replacement is all we have yet */
5966 rdev = conf->disks[i].replacement;
5967 conf->disks[i].replacement = NULL;
5968 clear_bit(Replacement, &rdev->flags);
5969 conf->disks[i].rdev = rdev;
5970 }
5971 if (!rdev)
c148ffdc 5972 continue;
17045f52
N
5973 if (conf->disks[i].replacement &&
5974 conf->reshape_progress != MaxSector) {
5975 /* replacements and reshape simply do not mix. */
5976 printk(KERN_ERR "md: cannot handle concurrent "
5977 "replacement and reshape.\n");
5978 goto abort;
5979 }
2f115882 5980 if (test_bit(In_sync, &rdev->flags)) {
91adb564 5981 working_disks++;
2f115882
N
5982 continue;
5983 }
c148ffdc
N
5984 /* This disc is not fully in-sync. However if it
5985 * just stored parity (beyond the recovery_offset),
5986 * when we don't need to be concerned about the
5987 * array being dirty.
5988 * When reshape goes 'backwards', we never have
5989 * partially completed devices, so we only need
5990 * to worry about reshape going forwards.
5991 */
5992 /* Hack because v0.91 doesn't store recovery_offset properly. */
5993 if (mddev->major_version == 0 &&
5994 mddev->minor_version > 90)
5995 rdev->recovery_offset = reshape_offset;
5026d7a9 5996
c148ffdc
N
5997 if (rdev->recovery_offset < reshape_offset) {
5998 /* We need to check old and new layout */
5999 if (!only_parity(rdev->raid_disk,
6000 conf->algorithm,
6001 conf->raid_disks,
6002 conf->max_degraded))
6003 continue;
6004 }
6005 if (!only_parity(rdev->raid_disk,
6006 conf->prev_algo,
6007 conf->previous_raid_disks,
6008 conf->max_degraded))
6009 continue;
6010 dirty_parity_disks++;
6011 }
91adb564 6012
17045f52
N
6013 /*
6014 * 0 for a fully functional array, 1 or 2 for a degraded array.
6015 */
908f4fbd 6016 mddev->degraded = calc_degraded(conf);
91adb564 6017
674806d6 6018 if (has_failed(conf)) {
0c55e022 6019 printk(KERN_ERR "md/raid:%s: not enough operational devices"
1da177e4 6020 " (%d/%d failed)\n",
02c2de8c 6021 mdname(mddev), mddev->degraded, conf->raid_disks);
1da177e4
LT
6022 goto abort;
6023 }
6024
91adb564 6025 /* device size must be a multiple of chunk size */
9d8f0363 6026 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
91adb564
N
6027 mddev->resync_max_sectors = mddev->dev_sectors;
6028
c148ffdc 6029 if (mddev->degraded > dirty_parity_disks &&
1da177e4 6030 mddev->recovery_cp != MaxSector) {
6ff8d8ec
N
6031 if (mddev->ok_start_degraded)
6032 printk(KERN_WARNING
0c55e022
N
6033 "md/raid:%s: starting dirty degraded array"
6034 " - data corruption possible.\n",
6ff8d8ec
N
6035 mdname(mddev));
6036 else {
6037 printk(KERN_ERR
0c55e022 6038 "md/raid:%s: cannot start dirty degraded array.\n",
6ff8d8ec
N
6039 mdname(mddev));
6040 goto abort;
6041 }
1da177e4
LT
6042 }
6043
1da177e4 6044 if (mddev->degraded == 0)
0c55e022
N
6045 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
6046 " devices, algorithm %d\n", mdname(mddev), conf->level,
e183eaed
N
6047 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
6048 mddev->new_layout);
1da177e4 6049 else
0c55e022
N
6050 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
6051 " out of %d devices, algorithm %d\n",
6052 mdname(mddev), conf->level,
6053 mddev->raid_disks - mddev->degraded,
6054 mddev->raid_disks, mddev->new_layout);
1da177e4
LT
6055
6056 print_raid5_conf(conf);
6057
fef9c61f 6058 if (conf->reshape_progress != MaxSector) {
fef9c61f 6059 conf->reshape_safe = conf->reshape_progress;
f6705578
N
6060 atomic_set(&conf->reshape_stripes, 0);
6061 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6062 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6063 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6064 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6065 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
0da3c619 6066 "reshape");
f6705578
N
6067 }
6068
1da177e4
LT
6069
6070 /* Ok, everything is just fine now */
a64c876f
N
6071 if (mddev->to_remove == &raid5_attrs_group)
6072 mddev->to_remove = NULL;
00bcb4ac
N
6073 else if (mddev->kobj.sd &&
6074 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
5e55e2f5 6075 printk(KERN_WARNING
4a5add49 6076 "raid5: failed to create sysfs attributes for %s\n",
5e55e2f5 6077 mdname(mddev));
4a5add49 6078 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
7a5febe9 6079
4a5add49 6080 if (mddev->queue) {
9f7c2220 6081 int chunk_size;
620125f2 6082 bool discard_supported = true;
4a5add49
N
6083 /* read-ahead size must cover two whole stripes, which
6084 * is 2 * (datadisks) * chunksize where 'n' is the
6085 * number of raid devices
6086 */
6087 int data_disks = conf->previous_raid_disks - conf->max_degraded;
6088 int stripe = data_disks *
6089 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
6090 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6091 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
91adb564 6092
4a5add49 6093 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
f022b2fd 6094
11d8a6e3
N
6095 mddev->queue->backing_dev_info.congested_data = mddev;
6096 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
7a5febe9 6097
9f7c2220
N
6098 chunk_size = mddev->chunk_sectors << 9;
6099 blk_queue_io_min(mddev->queue, chunk_size);
6100 blk_queue_io_opt(mddev->queue, chunk_size *
6101 (conf->raid_disks - conf->max_degraded));
620125f2
SL
6102 /*
6103 * We can only discard a whole stripe. It doesn't make sense to
6104 * discard data disk but write parity disk
6105 */
6106 stripe = stripe * PAGE_SIZE;
4ac6875e
N
6107 /* Round up to power of 2, as discard handling
6108 * currently assumes that */
6109 while ((stripe-1) & stripe)
6110 stripe = (stripe | (stripe-1)) + 1;
620125f2
SL
6111 mddev->queue->limits.discard_alignment = stripe;
6112 mddev->queue->limits.discard_granularity = stripe;
6113 /*
6114 * unaligned part of discard request will be ignored, so can't
6115 * guarantee discard_zerors_data
6116 */
6117 mddev->queue->limits.discard_zeroes_data = 0;
8f6c2e4b 6118
5026d7a9
PA
6119 blk_queue_max_write_same_sectors(mddev->queue, 0);
6120
05616be5 6121 rdev_for_each(rdev, mddev) {
9f7c2220
N
6122 disk_stack_limits(mddev->gendisk, rdev->bdev,
6123 rdev->data_offset << 9);
05616be5
N
6124 disk_stack_limits(mddev->gendisk, rdev->bdev,
6125 rdev->new_data_offset << 9);
620125f2
SL
6126 /*
6127 * discard_zeroes_data is required, otherwise data
6128 * could be lost. Consider a scenario: discard a stripe
6129 * (the stripe could be inconsistent if
6130 * discard_zeroes_data is 0); write one disk of the
6131 * stripe (the stripe could be inconsistent again
6132 * depending on which disks are used to calculate
6133 * parity); the disk is broken; The stripe data of this
6134 * disk is lost.
6135 */
6136 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
6137 !bdev_get_queue(rdev->bdev)->
6138 limits.discard_zeroes_data)
6139 discard_supported = false;
05616be5 6140 }
620125f2
SL
6141
6142 if (discard_supported &&
6143 mddev->queue->limits.max_discard_sectors >= stripe &&
6144 mddev->queue->limits.discard_granularity >= stripe)
6145 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
6146 mddev->queue);
6147 else
6148 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
6149 mddev->queue);
9f7c2220 6150 }
23032a0e 6151
1da177e4
LT
6152 return 0;
6153abort:
01f96c0a 6154 md_unregister_thread(&mddev->thread);
e4f869d9
N
6155 print_raid5_conf(conf);
6156 free_conf(conf);
1da177e4 6157 mddev->private = NULL;
0c55e022 6158 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
1da177e4
LT
6159 return -EIO;
6160}
6161
fd01b88c 6162static int stop(struct mddev *mddev)
1da177e4 6163{
d1688a6d 6164 struct r5conf *conf = mddev->private;
1da177e4 6165
01f96c0a 6166 md_unregister_thread(&mddev->thread);
11d8a6e3
N
6167 if (mddev->queue)
6168 mddev->queue->backing_dev_info.congested_fn = NULL;
95fc17aa 6169 free_conf(conf);
a64c876f
N
6170 mddev->private = NULL;
6171 mddev->to_remove = &raid5_attrs_group;
1da177e4
LT
6172 return 0;
6173}
6174
fd01b88c 6175static void status(struct seq_file *seq, struct mddev *mddev)
1da177e4 6176{
d1688a6d 6177 struct r5conf *conf = mddev->private;
1da177e4
LT
6178 int i;
6179
9d8f0363
AN
6180 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
6181 mddev->chunk_sectors / 2, mddev->layout);
02c2de8c 6182 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
1da177e4
LT
6183 for (i = 0; i < conf->raid_disks; i++)
6184 seq_printf (seq, "%s",
6185 conf->disks[i].rdev &&
b2d444d7 6186 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
1da177e4 6187 seq_printf (seq, "]");
1da177e4
LT
6188}
6189
d1688a6d 6190static void print_raid5_conf (struct r5conf *conf)
1da177e4
LT
6191{
6192 int i;
6193 struct disk_info *tmp;
6194
0c55e022 6195 printk(KERN_DEBUG "RAID conf printout:\n");
1da177e4
LT
6196 if (!conf) {
6197 printk("(conf==NULL)\n");
6198 return;
6199 }
0c55e022
N
6200 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
6201 conf->raid_disks,
6202 conf->raid_disks - conf->mddev->degraded);
1da177e4
LT
6203
6204 for (i = 0; i < conf->raid_disks; i++) {
6205 char b[BDEVNAME_SIZE];
6206 tmp = conf->disks + i;
6207 if (tmp->rdev)
0c55e022
N
6208 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
6209 i, !test_bit(Faulty, &tmp->rdev->flags),
6210 bdevname(tmp->rdev->bdev, b));
1da177e4
LT
6211 }
6212}
6213
fd01b88c 6214static int raid5_spare_active(struct mddev *mddev)
1da177e4
LT
6215{
6216 int i;
d1688a6d 6217 struct r5conf *conf = mddev->private;
1da177e4 6218 struct disk_info *tmp;
6b965620
N
6219 int count = 0;
6220 unsigned long flags;
1da177e4
LT
6221
6222 for (i = 0; i < conf->raid_disks; i++) {
6223 tmp = conf->disks + i;
dd054fce
N
6224 if (tmp->replacement
6225 && tmp->replacement->recovery_offset == MaxSector
6226 && !test_bit(Faulty, &tmp->replacement->flags)
6227 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
6228 /* Replacement has just become active. */
6229 if (!tmp->rdev
6230 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
6231 count++;
6232 if (tmp->rdev) {
6233 /* Replaced device not technically faulty,
6234 * but we need to be sure it gets removed
6235 * and never re-added.
6236 */
6237 set_bit(Faulty, &tmp->rdev->flags);
6238 sysfs_notify_dirent_safe(
6239 tmp->rdev->sysfs_state);
6240 }
6241 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
6242 } else if (tmp->rdev
70fffd0b 6243 && tmp->rdev->recovery_offset == MaxSector
b2d444d7 6244 && !test_bit(Faulty, &tmp->rdev->flags)
c04be0aa 6245 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
6b965620 6246 count++;
43c73ca4 6247 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
1da177e4
LT
6248 }
6249 }
6b965620 6250 spin_lock_irqsave(&conf->device_lock, flags);
908f4fbd 6251 mddev->degraded = calc_degraded(conf);
6b965620 6252 spin_unlock_irqrestore(&conf->device_lock, flags);
1da177e4 6253 print_raid5_conf(conf);
6b965620 6254 return count;
1da177e4
LT
6255}
6256
b8321b68 6257static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 6258{
d1688a6d 6259 struct r5conf *conf = mddev->private;
1da177e4 6260 int err = 0;
b8321b68 6261 int number = rdev->raid_disk;
657e3e4d 6262 struct md_rdev **rdevp;
1da177e4
LT
6263 struct disk_info *p = conf->disks + number;
6264
6265 print_raid5_conf(conf);
657e3e4d
N
6266 if (rdev == p->rdev)
6267 rdevp = &p->rdev;
6268 else if (rdev == p->replacement)
6269 rdevp = &p->replacement;
6270 else
6271 return 0;
6272
6273 if (number >= conf->raid_disks &&
6274 conf->reshape_progress == MaxSector)
6275 clear_bit(In_sync, &rdev->flags);
6276
6277 if (test_bit(In_sync, &rdev->flags) ||
6278 atomic_read(&rdev->nr_pending)) {
6279 err = -EBUSY;
6280 goto abort;
6281 }
6282 /* Only remove non-faulty devices if recovery
6283 * isn't possible.
6284 */
6285 if (!test_bit(Faulty, &rdev->flags) &&
6286 mddev->recovery_disabled != conf->recovery_disabled &&
6287 !has_failed(conf) &&
dd054fce 6288 (!p->replacement || p->replacement == rdev) &&
657e3e4d
N
6289 number < conf->raid_disks) {
6290 err = -EBUSY;
6291 goto abort;
6292 }
6293 *rdevp = NULL;
6294 synchronize_rcu();
6295 if (atomic_read(&rdev->nr_pending)) {
6296 /* lost the race, try later */
6297 err = -EBUSY;
6298 *rdevp = rdev;
dd054fce
N
6299 } else if (p->replacement) {
6300 /* We must have just cleared 'rdev' */
6301 p->rdev = p->replacement;
6302 clear_bit(Replacement, &p->replacement->flags);
6303 smp_mb(); /* Make sure other CPUs may see both as identical
6304 * but will never see neither - if they are careful
6305 */
6306 p->replacement = NULL;
6307 clear_bit(WantReplacement, &rdev->flags);
6308 } else
6309 /* We might have just removed the Replacement as faulty-
6310 * clear the bit just in case
6311 */
6312 clear_bit(WantReplacement, &rdev->flags);
1da177e4
LT
6313abort:
6314
6315 print_raid5_conf(conf);
6316 return err;
6317}
6318
fd01b88c 6319static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 6320{
d1688a6d 6321 struct r5conf *conf = mddev->private;
199050ea 6322 int err = -EEXIST;
1da177e4
LT
6323 int disk;
6324 struct disk_info *p;
6c2fce2e
NB
6325 int first = 0;
6326 int last = conf->raid_disks - 1;
1da177e4 6327
7f0da59b
N
6328 if (mddev->recovery_disabled == conf->recovery_disabled)
6329 return -EBUSY;
6330
dc10c643 6331 if (rdev->saved_raid_disk < 0 && has_failed(conf))
1da177e4 6332 /* no point adding a device */
199050ea 6333 return -EINVAL;
1da177e4 6334
6c2fce2e
NB
6335 if (rdev->raid_disk >= 0)
6336 first = last = rdev->raid_disk;
1da177e4
LT
6337
6338 /*
16a53ecc
N
6339 * find the disk ... but prefer rdev->saved_raid_disk
6340 * if possible.
1da177e4 6341 */
16a53ecc 6342 if (rdev->saved_raid_disk >= 0 &&
6c2fce2e 6343 rdev->saved_raid_disk >= first &&
16a53ecc 6344 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5cfb22a1
N
6345 first = rdev->saved_raid_disk;
6346
6347 for (disk = first; disk <= last; disk++) {
7bfec5f3
N
6348 p = conf->disks + disk;
6349 if (p->rdev == NULL) {
b2d444d7 6350 clear_bit(In_sync, &rdev->flags);
1da177e4 6351 rdev->raid_disk = disk;
199050ea 6352 err = 0;
72626685
N
6353 if (rdev->saved_raid_disk != disk)
6354 conf->fullsync = 1;
d6065f7b 6355 rcu_assign_pointer(p->rdev, rdev);
5cfb22a1 6356 goto out;
1da177e4 6357 }
5cfb22a1
N
6358 }
6359 for (disk = first; disk <= last; disk++) {
6360 p = conf->disks + disk;
7bfec5f3
N
6361 if (test_bit(WantReplacement, &p->rdev->flags) &&
6362 p->replacement == NULL) {
6363 clear_bit(In_sync, &rdev->flags);
6364 set_bit(Replacement, &rdev->flags);
6365 rdev->raid_disk = disk;
6366 err = 0;
6367 conf->fullsync = 1;
6368 rcu_assign_pointer(p->replacement, rdev);
6369 break;
6370 }
6371 }
5cfb22a1 6372out:
1da177e4 6373 print_raid5_conf(conf);
199050ea 6374 return err;
1da177e4
LT
6375}
6376
fd01b88c 6377static int raid5_resize(struct mddev *mddev, sector_t sectors)
1da177e4
LT
6378{
6379 /* no resync is happening, and there is enough space
6380 * on all devices, so we can resize.
6381 * We need to make sure resync covers any new space.
6382 * If the array is shrinking we should possibly wait until
6383 * any io in the removed space completes, but it hardly seems
6384 * worth it.
6385 */
a4a6125a 6386 sector_t newsize;
9d8f0363 6387 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
a4a6125a
N
6388 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
6389 if (mddev->external_size &&
6390 mddev->array_sectors > newsize)
b522adcd 6391 return -EINVAL;
a4a6125a
N
6392 if (mddev->bitmap) {
6393 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
6394 if (ret)
6395 return ret;
6396 }
6397 md_set_array_sectors(mddev, newsize);
f233ea5c 6398 set_capacity(mddev->gendisk, mddev->array_sectors);
449aad3e 6399 revalidate_disk(mddev->gendisk);
b098636c
N
6400 if (sectors > mddev->dev_sectors &&
6401 mddev->recovery_cp > mddev->dev_sectors) {
58c0fed4 6402 mddev->recovery_cp = mddev->dev_sectors;
1da177e4
LT
6403 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6404 }
58c0fed4 6405 mddev->dev_sectors = sectors;
4b5c7ae8 6406 mddev->resync_max_sectors = sectors;
1da177e4
LT
6407 return 0;
6408}
6409
fd01b88c 6410static int check_stripe_cache(struct mddev *mddev)
01ee22b4
N
6411{
6412 /* Can only proceed if there are plenty of stripe_heads.
6413 * We need a minimum of one full stripe,, and for sensible progress
6414 * it is best to have about 4 times that.
6415 * If we require 4 times, then the default 256 4K stripe_heads will
6416 * allow for chunk sizes up to 256K, which is probably OK.
6417 * If the chunk size is greater, user-space should request more
6418 * stripe_heads first.
6419 */
d1688a6d 6420 struct r5conf *conf = mddev->private;
01ee22b4
N
6421 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
6422 > conf->max_nr_stripes ||
6423 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
6424 > conf->max_nr_stripes) {
0c55e022
N
6425 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
6426 mdname(mddev),
01ee22b4
N
6427 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
6428 / STRIPE_SIZE)*4);
6429 return 0;
6430 }
6431 return 1;
6432}
6433
fd01b88c 6434static int check_reshape(struct mddev *mddev)
29269553 6435{
d1688a6d 6436 struct r5conf *conf = mddev->private;
29269553 6437
88ce4930
N
6438 if (mddev->delta_disks == 0 &&
6439 mddev->new_layout == mddev->layout &&
664e7c41 6440 mddev->new_chunk_sectors == mddev->chunk_sectors)
50ac168a 6441 return 0; /* nothing to do */
674806d6 6442 if (has_failed(conf))
ec32a2bd 6443 return -EINVAL;
fdcfbbb6 6444 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
ec32a2bd
N
6445 /* We might be able to shrink, but the devices must
6446 * be made bigger first.
6447 * For raid6, 4 is the minimum size.
6448 * Otherwise 2 is the minimum
6449 */
6450 int min = 2;
6451 if (mddev->level == 6)
6452 min = 4;
6453 if (mddev->raid_disks + mddev->delta_disks < min)
6454 return -EINVAL;
6455 }
29269553 6456
01ee22b4 6457 if (!check_stripe_cache(mddev))
29269553 6458 return -ENOSPC;
29269553 6459
e56108d6
N
6460 return resize_stripes(conf, (conf->previous_raid_disks
6461 + mddev->delta_disks));
63c70c4f
N
6462}
6463
fd01b88c 6464static int raid5_start_reshape(struct mddev *mddev)
63c70c4f 6465{
d1688a6d 6466 struct r5conf *conf = mddev->private;
3cb03002 6467 struct md_rdev *rdev;
63c70c4f 6468 int spares = 0;
c04be0aa 6469 unsigned long flags;
63c70c4f 6470
f416885e 6471 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
63c70c4f
N
6472 return -EBUSY;
6473
01ee22b4
N
6474 if (!check_stripe_cache(mddev))
6475 return -ENOSPC;
6476
30b67645
N
6477 if (has_failed(conf))
6478 return -EINVAL;
6479
c6563a8c 6480 rdev_for_each(rdev, mddev) {
469518a3
N
6481 if (!test_bit(In_sync, &rdev->flags)
6482 && !test_bit(Faulty, &rdev->flags))
29269553 6483 spares++;
c6563a8c 6484 }
63c70c4f 6485
f416885e 6486 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
29269553
N
6487 /* Not enough devices even to make a degraded array
6488 * of that size
6489 */
6490 return -EINVAL;
6491
ec32a2bd
N
6492 /* Refuse to reduce size of the array. Any reductions in
6493 * array size must be through explicit setting of array_size
6494 * attribute.
6495 */
6496 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
6497 < mddev->array_sectors) {
0c55e022 6498 printk(KERN_ERR "md/raid:%s: array size must be reduced "
ec32a2bd
N
6499 "before number of disks\n", mdname(mddev));
6500 return -EINVAL;
6501 }
6502
f6705578 6503 atomic_set(&conf->reshape_stripes, 0);
29269553 6504 spin_lock_irq(&conf->device_lock);
c46501b2 6505 write_seqcount_begin(&conf->gen_lock);
29269553 6506 conf->previous_raid_disks = conf->raid_disks;
63c70c4f 6507 conf->raid_disks += mddev->delta_disks;
09c9e5fa
AN
6508 conf->prev_chunk_sectors = conf->chunk_sectors;
6509 conf->chunk_sectors = mddev->new_chunk_sectors;
88ce4930
N
6510 conf->prev_algo = conf->algorithm;
6511 conf->algorithm = mddev->new_layout;
05616be5
N
6512 conf->generation++;
6513 /* Code that selects data_offset needs to see the generation update
6514 * if reshape_progress has been set - so a memory barrier needed.
6515 */
6516 smp_mb();
2c810cdd 6517 if (mddev->reshape_backwards)
fef9c61f
N
6518 conf->reshape_progress = raid5_size(mddev, 0, 0);
6519 else
6520 conf->reshape_progress = 0;
6521 conf->reshape_safe = conf->reshape_progress;
c46501b2 6522 write_seqcount_end(&conf->gen_lock);
29269553
N
6523 spin_unlock_irq(&conf->device_lock);
6524
4d77e3ba
N
6525 /* Now make sure any requests that proceeded on the assumption
6526 * the reshape wasn't running - like Discard or Read - have
6527 * completed.
6528 */
6529 mddev_suspend(mddev);
6530 mddev_resume(mddev);
6531
29269553
N
6532 /* Add some new drives, as many as will fit.
6533 * We know there are enough to make the newly sized array work.
3424bf6a
N
6534 * Don't add devices if we are reducing the number of
6535 * devices in the array. This is because it is not possible
6536 * to correctly record the "partially reconstructed" state of
6537 * such devices during the reshape and confusion could result.
29269553 6538 */
87a8dec9 6539 if (mddev->delta_disks >= 0) {
dafb20fa 6540 rdev_for_each(rdev, mddev)
87a8dec9
N
6541 if (rdev->raid_disk < 0 &&
6542 !test_bit(Faulty, &rdev->flags)) {
6543 if (raid5_add_disk(mddev, rdev) == 0) {
87a8dec9 6544 if (rdev->raid_disk
9d4c7d87 6545 >= conf->previous_raid_disks)
87a8dec9 6546 set_bit(In_sync, &rdev->flags);
9d4c7d87 6547 else
87a8dec9 6548 rdev->recovery_offset = 0;
36fad858
NK
6549
6550 if (sysfs_link_rdev(mddev, rdev))
87a8dec9 6551 /* Failure here is OK */;
50da0840 6552 }
87a8dec9
N
6553 } else if (rdev->raid_disk >= conf->previous_raid_disks
6554 && !test_bit(Faulty, &rdev->flags)) {
6555 /* This is a spare that was manually added */
6556 set_bit(In_sync, &rdev->flags);
87a8dec9 6557 }
29269553 6558
87a8dec9
N
6559 /* When a reshape changes the number of devices,
6560 * ->degraded is measured against the larger of the
6561 * pre and post number of devices.
6562 */
ec32a2bd 6563 spin_lock_irqsave(&conf->device_lock, flags);
908f4fbd 6564 mddev->degraded = calc_degraded(conf);
ec32a2bd
N
6565 spin_unlock_irqrestore(&conf->device_lock, flags);
6566 }
63c70c4f 6567 mddev->raid_disks = conf->raid_disks;
e516402c 6568 mddev->reshape_position = conf->reshape_progress;
850b2b42 6569 set_bit(MD_CHANGE_DEVS, &mddev->flags);
f6705578 6570
29269553
N
6571 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6572 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6573 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6574 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6575 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
0da3c619 6576 "reshape");
29269553
N
6577 if (!mddev->sync_thread) {
6578 mddev->recovery = 0;
6579 spin_lock_irq(&conf->device_lock);
ba8805b9 6580 write_seqcount_begin(&conf->gen_lock);
29269553 6581 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
ba8805b9
N
6582 mddev->new_chunk_sectors =
6583 conf->chunk_sectors = conf->prev_chunk_sectors;
6584 mddev->new_layout = conf->algorithm = conf->prev_algo;
05616be5
N
6585 rdev_for_each(rdev, mddev)
6586 rdev->new_data_offset = rdev->data_offset;
6587 smp_wmb();
ba8805b9 6588 conf->generation --;
fef9c61f 6589 conf->reshape_progress = MaxSector;
1e3fa9bd 6590 mddev->reshape_position = MaxSector;
ba8805b9 6591 write_seqcount_end(&conf->gen_lock);
29269553
N
6592 spin_unlock_irq(&conf->device_lock);
6593 return -EAGAIN;
6594 }
c8f517c4 6595 conf->reshape_checkpoint = jiffies;
29269553
N
6596 md_wakeup_thread(mddev->sync_thread);
6597 md_new_event(mddev);
6598 return 0;
6599}
29269553 6600
ec32a2bd
N
6601/* This is called from the reshape thread and should make any
6602 * changes needed in 'conf'
6603 */
d1688a6d 6604static void end_reshape(struct r5conf *conf)
29269553 6605{
29269553 6606
f6705578 6607 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
05616be5 6608 struct md_rdev *rdev;
f6705578 6609
f6705578 6610 spin_lock_irq(&conf->device_lock);
cea9c228 6611 conf->previous_raid_disks = conf->raid_disks;
05616be5
N
6612 rdev_for_each(rdev, conf->mddev)
6613 rdev->data_offset = rdev->new_data_offset;
6614 smp_wmb();
fef9c61f 6615 conf->reshape_progress = MaxSector;
f6705578 6616 spin_unlock_irq(&conf->device_lock);
b0f9ec04 6617 wake_up(&conf->wait_for_overlap);
16a53ecc
N
6618
6619 /* read-ahead size must cover two whole stripes, which is
6620 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6621 */
4a5add49 6622 if (conf->mddev->queue) {
cea9c228 6623 int data_disks = conf->raid_disks - conf->max_degraded;
09c9e5fa 6624 int stripe = data_disks * ((conf->chunk_sectors << 9)
cea9c228 6625 / PAGE_SIZE);
16a53ecc
N
6626 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6627 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6628 }
29269553 6629 }
29269553
N
6630}
6631
ec32a2bd
N
6632/* This is called from the raid5d thread with mddev_lock held.
6633 * It makes config changes to the device.
6634 */
fd01b88c 6635static void raid5_finish_reshape(struct mddev *mddev)
cea9c228 6636{
d1688a6d 6637 struct r5conf *conf = mddev->private;
cea9c228
N
6638
6639 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6640
ec32a2bd
N
6641 if (mddev->delta_disks > 0) {
6642 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6643 set_capacity(mddev->gendisk, mddev->array_sectors);
449aad3e 6644 revalidate_disk(mddev->gendisk);
ec32a2bd
N
6645 } else {
6646 int d;
908f4fbd
N
6647 spin_lock_irq(&conf->device_lock);
6648 mddev->degraded = calc_degraded(conf);
6649 spin_unlock_irq(&conf->device_lock);
ec32a2bd
N
6650 for (d = conf->raid_disks ;
6651 d < conf->raid_disks - mddev->delta_disks;
1a67dde0 6652 d++) {
3cb03002 6653 struct md_rdev *rdev = conf->disks[d].rdev;
da7613b8
N
6654 if (rdev)
6655 clear_bit(In_sync, &rdev->flags);
6656 rdev = conf->disks[d].replacement;
6657 if (rdev)
6658 clear_bit(In_sync, &rdev->flags);
1a67dde0 6659 }
cea9c228 6660 }
88ce4930 6661 mddev->layout = conf->algorithm;
09c9e5fa 6662 mddev->chunk_sectors = conf->chunk_sectors;
ec32a2bd
N
6663 mddev->reshape_position = MaxSector;
6664 mddev->delta_disks = 0;
2c810cdd 6665 mddev->reshape_backwards = 0;
cea9c228
N
6666 }
6667}
6668
fd01b88c 6669static void raid5_quiesce(struct mddev *mddev, int state)
72626685 6670{
d1688a6d 6671 struct r5conf *conf = mddev->private;
72626685
N
6672
6673 switch(state) {
e464eafd
N
6674 case 2: /* resume for a suspend */
6675 wake_up(&conf->wait_for_overlap);
6676 break;
6677
72626685 6678 case 1: /* stop all writes */
566c09c5 6679 lock_all_device_hash_locks_irq(conf);
64bd660b
N
6680 /* '2' tells resync/reshape to pause so that all
6681 * active stripes can drain
6682 */
6683 conf->quiesce = 2;
566c09c5 6684 wait_event_cmd(conf->wait_for_stripe,
46031f9a
RBJ
6685 atomic_read(&conf->active_stripes) == 0 &&
6686 atomic_read(&conf->active_aligned_reads) == 0,
566c09c5
SL
6687 unlock_all_device_hash_locks_irq(conf),
6688 lock_all_device_hash_locks_irq(conf));
64bd660b 6689 conf->quiesce = 1;
566c09c5 6690 unlock_all_device_hash_locks_irq(conf);
64bd660b
N
6691 /* allow reshape to continue */
6692 wake_up(&conf->wait_for_overlap);
72626685
N
6693 break;
6694
6695 case 0: /* re-enable writes */
566c09c5 6696 lock_all_device_hash_locks_irq(conf);
72626685
N
6697 conf->quiesce = 0;
6698 wake_up(&conf->wait_for_stripe);
e464eafd 6699 wake_up(&conf->wait_for_overlap);
566c09c5 6700 unlock_all_device_hash_locks_irq(conf);
72626685
N
6701 break;
6702 }
72626685 6703}
b15c2e57 6704
d562b0c4 6705
fd01b88c 6706static void *raid45_takeover_raid0(struct mddev *mddev, int level)
54071b38 6707{
e373ab10 6708 struct r0conf *raid0_conf = mddev->private;
d76c8420 6709 sector_t sectors;
54071b38 6710
f1b29bca 6711 /* for raid0 takeover only one zone is supported */
e373ab10 6712 if (raid0_conf->nr_strip_zones > 1) {
0c55e022
N
6713 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6714 mdname(mddev));
f1b29bca
DW
6715 return ERR_PTR(-EINVAL);
6716 }
6717
e373ab10
N
6718 sectors = raid0_conf->strip_zone[0].zone_end;
6719 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
3b71bd93 6720 mddev->dev_sectors = sectors;
f1b29bca 6721 mddev->new_level = level;
54071b38
TM
6722 mddev->new_layout = ALGORITHM_PARITY_N;
6723 mddev->new_chunk_sectors = mddev->chunk_sectors;
6724 mddev->raid_disks += 1;
6725 mddev->delta_disks = 1;
6726 /* make sure it will be not marked as dirty */
6727 mddev->recovery_cp = MaxSector;
6728
6729 return setup_conf(mddev);
6730}
6731
6732
fd01b88c 6733static void *raid5_takeover_raid1(struct mddev *mddev)
d562b0c4
N
6734{
6735 int chunksect;
6736
6737 if (mddev->raid_disks != 2 ||
6738 mddev->degraded > 1)
6739 return ERR_PTR(-EINVAL);
6740
6741 /* Should check if there are write-behind devices? */
6742
6743 chunksect = 64*2; /* 64K by default */
6744
6745 /* The array must be an exact multiple of chunksize */
6746 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6747 chunksect >>= 1;
6748
6749 if ((chunksect<<9) < STRIPE_SIZE)
6750 /* array size does not allow a suitable chunk size */
6751 return ERR_PTR(-EINVAL);
6752
6753 mddev->new_level = 5;
6754 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
664e7c41 6755 mddev->new_chunk_sectors = chunksect;
d562b0c4
N
6756
6757 return setup_conf(mddev);
6758}
6759
fd01b88c 6760static void *raid5_takeover_raid6(struct mddev *mddev)
fc9739c6
N
6761{
6762 int new_layout;
6763
6764 switch (mddev->layout) {
6765 case ALGORITHM_LEFT_ASYMMETRIC_6:
6766 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6767 break;
6768 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6769 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6770 break;
6771 case ALGORITHM_LEFT_SYMMETRIC_6:
6772 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6773 break;
6774 case ALGORITHM_RIGHT_SYMMETRIC_6:
6775 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6776 break;
6777 case ALGORITHM_PARITY_0_6:
6778 new_layout = ALGORITHM_PARITY_0;
6779 break;
6780 case ALGORITHM_PARITY_N:
6781 new_layout = ALGORITHM_PARITY_N;
6782 break;
6783 default:
6784 return ERR_PTR(-EINVAL);
6785 }
6786 mddev->new_level = 5;
6787 mddev->new_layout = new_layout;
6788 mddev->delta_disks = -1;
6789 mddev->raid_disks -= 1;
6790 return setup_conf(mddev);
6791}
6792
d562b0c4 6793
fd01b88c 6794static int raid5_check_reshape(struct mddev *mddev)
b3546035 6795{
88ce4930
N
6796 /* For a 2-drive array, the layout and chunk size can be changed
6797 * immediately as not restriping is needed.
6798 * For larger arrays we record the new value - after validation
6799 * to be used by a reshape pass.
b3546035 6800 */
d1688a6d 6801 struct r5conf *conf = mddev->private;
597a711b 6802 int new_chunk = mddev->new_chunk_sectors;
b3546035 6803
597a711b 6804 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
b3546035
N
6805 return -EINVAL;
6806 if (new_chunk > 0) {
0ba459d2 6807 if (!is_power_of_2(new_chunk))
b3546035 6808 return -EINVAL;
597a711b 6809 if (new_chunk < (PAGE_SIZE>>9))
b3546035 6810 return -EINVAL;
597a711b 6811 if (mddev->array_sectors & (new_chunk-1))
b3546035
N
6812 /* not factor of array size */
6813 return -EINVAL;
6814 }
6815
6816 /* They look valid */
6817
88ce4930 6818 if (mddev->raid_disks == 2) {
597a711b
N
6819 /* can make the change immediately */
6820 if (mddev->new_layout >= 0) {
6821 conf->algorithm = mddev->new_layout;
6822 mddev->layout = mddev->new_layout;
88ce4930
N
6823 }
6824 if (new_chunk > 0) {
597a711b
N
6825 conf->chunk_sectors = new_chunk ;
6826 mddev->chunk_sectors = new_chunk;
88ce4930
N
6827 }
6828 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6829 md_wakeup_thread(mddev->thread);
b3546035 6830 }
50ac168a 6831 return check_reshape(mddev);
88ce4930
N
6832}
6833
fd01b88c 6834static int raid6_check_reshape(struct mddev *mddev)
88ce4930 6835{
597a711b 6836 int new_chunk = mddev->new_chunk_sectors;
50ac168a 6837
597a711b 6838 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
88ce4930 6839 return -EINVAL;
b3546035 6840 if (new_chunk > 0) {
0ba459d2 6841 if (!is_power_of_2(new_chunk))
88ce4930 6842 return -EINVAL;
597a711b 6843 if (new_chunk < (PAGE_SIZE >> 9))
88ce4930 6844 return -EINVAL;
597a711b 6845 if (mddev->array_sectors & (new_chunk-1))
88ce4930
N
6846 /* not factor of array size */
6847 return -EINVAL;
b3546035 6848 }
88ce4930
N
6849
6850 /* They look valid */
50ac168a 6851 return check_reshape(mddev);
b3546035
N
6852}
6853
fd01b88c 6854static void *raid5_takeover(struct mddev *mddev)
d562b0c4
N
6855{
6856 /* raid5 can take over:
f1b29bca 6857 * raid0 - if there is only one strip zone - make it a raid4 layout
d562b0c4
N
6858 * raid1 - if there are two drives. We need to know the chunk size
6859 * raid4 - trivial - just use a raid4 layout.
6860 * raid6 - Providing it is a *_6 layout
d562b0c4 6861 */
f1b29bca
DW
6862 if (mddev->level == 0)
6863 return raid45_takeover_raid0(mddev, 5);
d562b0c4
N
6864 if (mddev->level == 1)
6865 return raid5_takeover_raid1(mddev);
e9d4758f
N
6866 if (mddev->level == 4) {
6867 mddev->new_layout = ALGORITHM_PARITY_N;
6868 mddev->new_level = 5;
6869 return setup_conf(mddev);
6870 }
fc9739c6
N
6871 if (mddev->level == 6)
6872 return raid5_takeover_raid6(mddev);
d562b0c4
N
6873
6874 return ERR_PTR(-EINVAL);
6875}
6876
fd01b88c 6877static void *raid4_takeover(struct mddev *mddev)
a78d38a1 6878{
f1b29bca
DW
6879 /* raid4 can take over:
6880 * raid0 - if there is only one strip zone
6881 * raid5 - if layout is right
a78d38a1 6882 */
f1b29bca
DW
6883 if (mddev->level == 0)
6884 return raid45_takeover_raid0(mddev, 4);
a78d38a1
N
6885 if (mddev->level == 5 &&
6886 mddev->layout == ALGORITHM_PARITY_N) {
6887 mddev->new_layout = 0;
6888 mddev->new_level = 4;
6889 return setup_conf(mddev);
6890 }
6891 return ERR_PTR(-EINVAL);
6892}
d562b0c4 6893
84fc4b56 6894static struct md_personality raid5_personality;
245f46c2 6895
fd01b88c 6896static void *raid6_takeover(struct mddev *mddev)
245f46c2
N
6897{
6898 /* Currently can only take over a raid5. We map the
6899 * personality to an equivalent raid6 personality
6900 * with the Q block at the end.
6901 */
6902 int new_layout;
6903
6904 if (mddev->pers != &raid5_personality)
6905 return ERR_PTR(-EINVAL);
6906 if (mddev->degraded > 1)
6907 return ERR_PTR(-EINVAL);
6908 if (mddev->raid_disks > 253)
6909 return ERR_PTR(-EINVAL);
6910 if (mddev->raid_disks < 3)
6911 return ERR_PTR(-EINVAL);
6912
6913 switch (mddev->layout) {
6914 case ALGORITHM_LEFT_ASYMMETRIC:
6915 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6916 break;
6917 case ALGORITHM_RIGHT_ASYMMETRIC:
6918 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6919 break;
6920 case ALGORITHM_LEFT_SYMMETRIC:
6921 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6922 break;
6923 case ALGORITHM_RIGHT_SYMMETRIC:
6924 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6925 break;
6926 case ALGORITHM_PARITY_0:
6927 new_layout = ALGORITHM_PARITY_0_6;
6928 break;
6929 case ALGORITHM_PARITY_N:
6930 new_layout = ALGORITHM_PARITY_N;
6931 break;
6932 default:
6933 return ERR_PTR(-EINVAL);
6934 }
6935 mddev->new_level = 6;
6936 mddev->new_layout = new_layout;
6937 mddev->delta_disks = 1;
6938 mddev->raid_disks += 1;
6939 return setup_conf(mddev);
6940}
6941
6942
84fc4b56 6943static struct md_personality raid6_personality =
16a53ecc
N
6944{
6945 .name = "raid6",
6946 .level = 6,
6947 .owner = THIS_MODULE,
6948 .make_request = make_request,
6949 .run = run,
6950 .stop = stop,
6951 .status = status,
6952 .error_handler = error,
6953 .hot_add_disk = raid5_add_disk,
6954 .hot_remove_disk= raid5_remove_disk,
6955 .spare_active = raid5_spare_active,
6956 .sync_request = sync_request,
6957 .resize = raid5_resize,
80c3a6ce 6958 .size = raid5_size,
50ac168a 6959 .check_reshape = raid6_check_reshape,
f416885e 6960 .start_reshape = raid5_start_reshape,
cea9c228 6961 .finish_reshape = raid5_finish_reshape,
16a53ecc 6962 .quiesce = raid5_quiesce,
245f46c2 6963 .takeover = raid6_takeover,
16a53ecc 6964};
84fc4b56 6965static struct md_personality raid5_personality =
1da177e4
LT
6966{
6967 .name = "raid5",
2604b703 6968 .level = 5,
1da177e4
LT
6969 .owner = THIS_MODULE,
6970 .make_request = make_request,
6971 .run = run,
6972 .stop = stop,
6973 .status = status,
6974 .error_handler = error,
6975 .hot_add_disk = raid5_add_disk,
6976 .hot_remove_disk= raid5_remove_disk,
6977 .spare_active = raid5_spare_active,
6978 .sync_request = sync_request,
6979 .resize = raid5_resize,
80c3a6ce 6980 .size = raid5_size,
63c70c4f
N
6981 .check_reshape = raid5_check_reshape,
6982 .start_reshape = raid5_start_reshape,
cea9c228 6983 .finish_reshape = raid5_finish_reshape,
72626685 6984 .quiesce = raid5_quiesce,
d562b0c4 6985 .takeover = raid5_takeover,
1da177e4
LT
6986};
6987
84fc4b56 6988static struct md_personality raid4_personality =
1da177e4 6989{
2604b703
N
6990 .name = "raid4",
6991 .level = 4,
6992 .owner = THIS_MODULE,
6993 .make_request = make_request,
6994 .run = run,
6995 .stop = stop,
6996 .status = status,
6997 .error_handler = error,
6998 .hot_add_disk = raid5_add_disk,
6999 .hot_remove_disk= raid5_remove_disk,
7000 .spare_active = raid5_spare_active,
7001 .sync_request = sync_request,
7002 .resize = raid5_resize,
80c3a6ce 7003 .size = raid5_size,
3d37890b
N
7004 .check_reshape = raid5_check_reshape,
7005 .start_reshape = raid5_start_reshape,
cea9c228 7006 .finish_reshape = raid5_finish_reshape,
2604b703 7007 .quiesce = raid5_quiesce,
a78d38a1 7008 .takeover = raid4_takeover,
2604b703
N
7009};
7010
7011static int __init raid5_init(void)
7012{
851c30c9
SL
7013 raid5_wq = alloc_workqueue("raid5wq",
7014 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
7015 if (!raid5_wq)
7016 return -ENOMEM;
16a53ecc 7017 register_md_personality(&raid6_personality);
2604b703
N
7018 register_md_personality(&raid5_personality);
7019 register_md_personality(&raid4_personality);
7020 return 0;
1da177e4
LT
7021}
7022
2604b703 7023static void raid5_exit(void)
1da177e4 7024{
16a53ecc 7025 unregister_md_personality(&raid6_personality);
2604b703
N
7026 unregister_md_personality(&raid5_personality);
7027 unregister_md_personality(&raid4_personality);
851c30c9 7028 destroy_workqueue(raid5_wq);
1da177e4
LT
7029}
7030
7031module_init(raid5_init);
7032module_exit(raid5_exit);
7033MODULE_LICENSE("GPL");
0efb9e61 7034MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
1da177e4 7035MODULE_ALIAS("md-personality-4"); /* RAID5 */
d9d166c2
N
7036MODULE_ALIAS("md-raid5");
7037MODULE_ALIAS("md-raid4");
2604b703
N
7038MODULE_ALIAS("md-level-5");
7039MODULE_ALIAS("md-level-4");
16a53ecc
N
7040MODULE_ALIAS("md-personality-8"); /* RAID6 */
7041MODULE_ALIAS("md-raid6");
7042MODULE_ALIAS("md-level-6");
7043
7044/* This used to be two separate modules, they were: */
7045MODULE_ALIAS("raid5");
7046MODULE_ALIAS("raid6");