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