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