]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/md/raid5.c
md: Don't set mddev private to NULL in raid0 pers->free
[mirror_ubuntu-jammy-kernel.git] / drivers / md / raid5.c
CommitLineData
af1a8899 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * raid5.c : Multiple Devices driver for Linux
4 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
5 * Copyright (C) 1999, 2000 Ingo Molnar
16a53ecc 6 * Copyright (C) 2002, 2003 H. Peter Anvin
1da177e4 7 *
16a53ecc
N
8 * RAID-4/5/6 management functions.
9 * Thanks to Penguin Computing for making the RAID-6 development possible
10 * by donating a test server!
1da177e4
LT
11 */
12
ae3c20cc
N
13/*
14 * BITMAP UNPLUGGING:
15 *
16 * The sequencing for updating the bitmap reliably is a little
17 * subtle (and I got it wrong the first time) so it deserves some
18 * explanation.
19 *
20 * We group bitmap updates into batches. Each batch has a number.
21 * We may write out several batches at once, but that isn't very important.
7c13edc8
N
22 * conf->seq_write is the number of the last batch successfully written.
23 * conf->seq_flush is the number of the last batch that was closed to
ae3c20cc
N
24 * new additions.
25 * When we discover that we will need to write to any block in a stripe
26 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
7c13edc8 27 * the number of the batch it will be in. This is seq_flush+1.
ae3c20cc
N
28 * When we are ready to do a write, if that batch hasn't been written yet,
29 * we plug the array and queue the stripe for later.
30 * When an unplug happens, we increment bm_flush, thus closing the current
31 * batch.
32 * When we notice that bm_flush > bm_write, we write out all pending updates
33 * to the bitmap, and advance bm_write to where bm_flush was.
34 * This may occasionally write a bit out twice, but is sure never to
35 * miss any bits.
36 */
1da177e4 37
bff61975 38#include <linux/blkdev.h>
f6705578 39#include <linux/kthread.h>
f701d589 40#include <linux/raid/pq.h>
91c00924 41#include <linux/async_tx.h>
056075c7 42#include <linux/module.h>
07a3b417 43#include <linux/async.h>
bff61975 44#include <linux/seq_file.h>
36d1c647 45#include <linux/cpu.h>
5a0e3ad6 46#include <linux/slab.h>
8bda470e 47#include <linux/ratelimit.h>
851c30c9 48#include <linux/nodemask.h>
3f07c014 49
a9add5d9 50#include <trace/events/block.h>
aaf9f12e 51#include <linux/list_sort.h>
a9add5d9 52
43b2e5d8 53#include "md.h"
bff61975 54#include "raid5.h"
54071b38 55#include "raid0.h"
935fe098 56#include "md-bitmap.h"
ff875738 57#include "raid5-log.h"
72626685 58
394ed8e4
SL
59#define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED)
60
851c30c9
SL
61#define cpu_to_group(cpu) cpu_to_node(cpu)
62#define ANY_GROUP NUMA_NO_NODE
63
8e0e99ba
N
64static bool devices_handle_discard_safely = false;
65module_param(devices_handle_discard_safely, bool, 0644);
66MODULE_PARM_DESC(devices_handle_discard_safely,
67 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
851c30c9 68static struct workqueue_struct *raid5_wq;
1da177e4 69
d1688a6d 70static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
db298e19 71{
c911c46c 72 int hash = (sect >> RAID5_STRIPE_SHIFT(conf)) & HASH_MASK;
db298e19
N
73 return &conf->stripe_hashtbl[hash];
74}
1da177e4 75
c911c46c 76static inline int stripe_hash_locks_hash(struct r5conf *conf, sector_t sect)
566c09c5 77{
c911c46c 78 return (sect >> RAID5_STRIPE_SHIFT(conf)) & STRIPE_HASH_LOCKS_MASK;
566c09c5
SL
79}
80
81static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
82{
83 spin_lock_irq(conf->hash_locks + hash);
84 spin_lock(&conf->device_lock);
85}
86
87static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
88{
89 spin_unlock(&conf->device_lock);
90 spin_unlock_irq(conf->hash_locks + hash);
91}
92
93static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
94{
95 int i;
3d05f3ae 96 spin_lock_irq(conf->hash_locks);
566c09c5
SL
97 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
98 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
99 spin_lock(&conf->device_lock);
100}
101
102static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
103{
104 int i;
105 spin_unlock(&conf->device_lock);
3d05f3ae
JC
106 for (i = NR_STRIPE_HASH_LOCKS - 1; i; i--)
107 spin_unlock(conf->hash_locks + i);
108 spin_unlock_irq(conf->hash_locks);
566c09c5
SL
109}
110
d0dabf7e
N
111/* Find first data disk in a raid6 stripe */
112static inline int raid6_d0(struct stripe_head *sh)
113{
67cc2b81
N
114 if (sh->ddf_layout)
115 /* ddf always start from first device */
116 return 0;
117 /* md starts just after Q block */
d0dabf7e
N
118 if (sh->qd_idx == sh->disks - 1)
119 return 0;
120 else
121 return sh->qd_idx + 1;
122}
16a53ecc
N
123static inline int raid6_next_disk(int disk, int raid_disks)
124{
125 disk++;
126 return (disk < raid_disks) ? disk : 0;
127}
a4456856 128
d0dabf7e
N
129/* When walking through the disks in a raid5, starting at raid6_d0,
130 * We need to map each disk to a 'slot', where the data disks are slot
131 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
132 * is raid_disks-1. This help does that mapping.
133 */
67cc2b81
N
134static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
135 int *count, int syndrome_disks)
d0dabf7e 136{
6629542e 137 int slot = *count;
67cc2b81 138
e4424fee 139 if (sh->ddf_layout)
6629542e 140 (*count)++;
d0dabf7e 141 if (idx == sh->pd_idx)
67cc2b81 142 return syndrome_disks;
d0dabf7e 143 if (idx == sh->qd_idx)
67cc2b81 144 return syndrome_disks + 1;
e4424fee 145 if (!sh->ddf_layout)
6629542e 146 (*count)++;
d0dabf7e
N
147 return slot;
148}
149
d1688a6d 150static void print_raid5_conf (struct r5conf *conf);
1da177e4 151
600aa109
DW
152static int stripe_operations_active(struct stripe_head *sh)
153{
154 return sh->check_state || sh->reconstruct_state ||
155 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
156 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
157}
158
535ae4eb
SL
159static bool stripe_is_lowprio(struct stripe_head *sh)
160{
161 return (test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) ||
162 test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) &&
163 !test_bit(STRIPE_R5C_CACHING, &sh->state);
164}
165
851c30c9
SL
166static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
167{
168 struct r5conf *conf = sh->raid_conf;
169 struct r5worker_group *group;
bfc90cb0 170 int thread_cnt;
851c30c9
SL
171 int i, cpu = sh->cpu;
172
173 if (!cpu_online(cpu)) {
174 cpu = cpumask_any(cpu_online_mask);
175 sh->cpu = cpu;
176 }
177
178 if (list_empty(&sh->lru)) {
179 struct r5worker_group *group;
180 group = conf->worker_groups + cpu_to_group(cpu);
535ae4eb
SL
181 if (stripe_is_lowprio(sh))
182 list_add_tail(&sh->lru, &group->loprio_list);
183 else
184 list_add_tail(&sh->lru, &group->handle_list);
bfc90cb0
SL
185 group->stripes_cnt++;
186 sh->group = group;
851c30c9
SL
187 }
188
189 if (conf->worker_cnt_per_group == 0) {
190 md_wakeup_thread(conf->mddev->thread);
191 return;
192 }
193
194 group = conf->worker_groups + cpu_to_group(sh->cpu);
195
bfc90cb0
SL
196 group->workers[0].working = true;
197 /* at least one worker should run to avoid race */
198 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
199
200 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
201 /* wakeup more workers */
202 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
203 if (group->workers[i].working == false) {
204 group->workers[i].working = true;
205 queue_work_on(sh->cpu, raid5_wq,
206 &group->workers[i].work);
207 thread_cnt--;
208 }
209 }
851c30c9
SL
210}
211
566c09c5
SL
212static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
213 struct list_head *temp_inactive_list)
1da177e4 214{
1e6d690b
SL
215 int i;
216 int injournal = 0; /* number of date pages with R5_InJournal */
217
4eb788df
SL
218 BUG_ON(!list_empty(&sh->lru));
219 BUG_ON(atomic_read(&conf->active_stripes)==0);
1e6d690b
SL
220
221 if (r5c_is_writeback(conf->log))
222 for (i = sh->disks; i--; )
223 if (test_bit(R5_InJournal, &sh->dev[i].flags))
224 injournal++;
a39f7afd 225 /*
5ddf0440
SL
226 * In the following cases, the stripe cannot be released to cached
227 * lists. Therefore, we make the stripe write out and set
228 * STRIPE_HANDLE:
229 * 1. when quiesce in r5c write back;
230 * 2. when resync is requested fot the stripe.
a39f7afd 231 */
5ddf0440
SL
232 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) ||
233 (conf->quiesce && r5c_is_writeback(conf->log) &&
234 !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0)) {
a39f7afd
SL
235 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
236 r5c_make_stripe_write_out(sh);
237 set_bit(STRIPE_HANDLE, &sh->state);
238 }
1e6d690b 239
4eb788df
SL
240 if (test_bit(STRIPE_HANDLE, &sh->state)) {
241 if (test_bit(STRIPE_DELAYED, &sh->state) &&
ad3ab8b6 242 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4eb788df 243 list_add_tail(&sh->lru, &conf->delayed_list);
ad3ab8b6 244 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
4eb788df
SL
245 sh->bm_seq - conf->seq_write > 0)
246 list_add_tail(&sh->lru, &conf->bitmap_list);
247 else {
248 clear_bit(STRIPE_DELAYED, &sh->state);
249 clear_bit(STRIPE_BIT_DELAY, &sh->state);
851c30c9 250 if (conf->worker_cnt_per_group == 0) {
535ae4eb
SL
251 if (stripe_is_lowprio(sh))
252 list_add_tail(&sh->lru,
253 &conf->loprio_list);
254 else
255 list_add_tail(&sh->lru,
256 &conf->handle_list);
851c30c9
SL
257 } else {
258 raid5_wakeup_stripe_thread(sh);
259 return;
260 }
4eb788df
SL
261 }
262 md_wakeup_thread(conf->mddev->thread);
263 } else {
264 BUG_ON(stripe_operations_active(sh));
265 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
266 if (atomic_dec_return(&conf->preread_active_stripes)
267 < IO_THRESHOLD)
268 md_wakeup_thread(conf->mddev->thread);
269 atomic_dec(&conf->active_stripes);
1e6d690b
SL
270 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
271 if (!r5c_is_writeback(conf->log))
272 list_add_tail(&sh->lru, temp_inactive_list);
273 else {
274 WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags));
275 if (injournal == 0)
276 list_add_tail(&sh->lru, temp_inactive_list);
277 else if (injournal == conf->raid_disks - conf->max_degraded) {
278 /* full stripe */
279 if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state))
280 atomic_inc(&conf->r5c_cached_full_stripes);
281 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state))
282 atomic_dec(&conf->r5c_cached_partial_stripes);
283 list_add_tail(&sh->lru, &conf->r5c_full_stripe_list);
a39f7afd 284 r5c_check_cached_full_stripe(conf);
03b047f4
SL
285 } else
286 /*
287 * STRIPE_R5C_PARTIAL_STRIPE is set in
288 * r5c_try_caching_write(). No need to
289 * set it again.
290 */
1e6d690b 291 list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list);
1e6d690b
SL
292 }
293 }
1da177e4
LT
294 }
295}
d0dabf7e 296
566c09c5
SL
297static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
298 struct list_head *temp_inactive_list)
4eb788df
SL
299{
300 if (atomic_dec_and_test(&sh->count))
566c09c5
SL
301 do_release_stripe(conf, sh, temp_inactive_list);
302}
303
304/*
305 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
306 *
307 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
308 * given time. Adding stripes only takes device lock, while deleting stripes
309 * only takes hash lock.
310 */
311static void release_inactive_stripe_list(struct r5conf *conf,
312 struct list_head *temp_inactive_list,
313 int hash)
314{
315 int size;
6ab2a4b8 316 bool do_wakeup = false;
566c09c5
SL
317 unsigned long flags;
318
319 if (hash == NR_STRIPE_HASH_LOCKS) {
320 size = NR_STRIPE_HASH_LOCKS;
321 hash = NR_STRIPE_HASH_LOCKS - 1;
322 } else
323 size = 1;
324 while (size) {
325 struct list_head *list = &temp_inactive_list[size - 1];
326
327 /*
6d036f7d 328 * We don't hold any lock here yet, raid5_get_active_stripe() might
566c09c5
SL
329 * remove stripes from the list
330 */
331 if (!list_empty_careful(list)) {
332 spin_lock_irqsave(conf->hash_locks + hash, flags);
4bda556a
SL
333 if (list_empty(conf->inactive_list + hash) &&
334 !list_empty(list))
335 atomic_dec(&conf->empty_inactive_list_nr);
566c09c5 336 list_splice_tail_init(list, conf->inactive_list + hash);
6ab2a4b8 337 do_wakeup = true;
566c09c5
SL
338 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
339 }
340 size--;
341 hash--;
342 }
343
344 if (do_wakeup) {
6ab2a4b8 345 wake_up(&conf->wait_for_stripe);
b1b46486
YL
346 if (atomic_read(&conf->active_stripes) == 0)
347 wake_up(&conf->wait_for_quiescent);
566c09c5
SL
348 if (conf->retry_read_aligned)
349 md_wakeup_thread(conf->mddev->thread);
350 }
4eb788df
SL
351}
352
773ca82f 353/* should hold conf->device_lock already */
566c09c5
SL
354static int release_stripe_list(struct r5conf *conf,
355 struct list_head *temp_inactive_list)
773ca82f 356{
eae8263f 357 struct stripe_head *sh, *t;
773ca82f
SL
358 int count = 0;
359 struct llist_node *head;
360
361 head = llist_del_all(&conf->released_stripes);
d265d9dc 362 head = llist_reverse_order(head);
eae8263f 363 llist_for_each_entry_safe(sh, t, head, release_list) {
566c09c5
SL
364 int hash;
365
773ca82f
SL
366 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
367 smp_mb();
368 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
369 /*
370 * Don't worry the bit is set here, because if the bit is set
371 * again, the count is always > 1. This is true for
372 * STRIPE_ON_UNPLUG_LIST bit too.
373 */
566c09c5
SL
374 hash = sh->hash_lock_index;
375 __release_stripe(conf, sh, &temp_inactive_list[hash]);
773ca82f
SL
376 count++;
377 }
378
379 return count;
380}
381
6d036f7d 382void raid5_release_stripe(struct stripe_head *sh)
1da177e4 383{
d1688a6d 384 struct r5conf *conf = sh->raid_conf;
1da177e4 385 unsigned long flags;
566c09c5
SL
386 struct list_head list;
387 int hash;
773ca82f 388 bool wakeup;
16a53ecc 389
cf170f3f
ES
390 /* Avoid release_list until the last reference.
391 */
392 if (atomic_add_unless(&sh->count, -1, 1))
393 return;
394
ad4068de 395 if (unlikely(!conf->mddev->thread) ||
396 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
773ca82f
SL
397 goto slow_path;
398 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
399 if (wakeup)
400 md_wakeup_thread(conf->mddev->thread);
401 return;
402slow_path:
773ca82f 403 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
685dbcaa 404 if (atomic_dec_and_lock_irqsave(&sh->count, &conf->device_lock, flags)) {
566c09c5
SL
405 INIT_LIST_HEAD(&list);
406 hash = sh->hash_lock_index;
407 do_release_stripe(conf, sh, &list);
08edaaa6 408 spin_unlock_irqrestore(&conf->device_lock, flags);
566c09c5 409 release_inactive_stripe_list(conf, &list, hash);
4eb788df 410 }
1da177e4
LT
411}
412
fccddba0 413static inline void remove_hash(struct stripe_head *sh)
1da177e4 414{
45b4233c
DW
415 pr_debug("remove_hash(), stripe %llu\n",
416 (unsigned long long)sh->sector);
1da177e4 417
fccddba0 418 hlist_del_init(&sh->hash);
1da177e4
LT
419}
420
d1688a6d 421static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
1da177e4 422{
fccddba0 423 struct hlist_head *hp = stripe_hash(conf, sh->sector);
1da177e4 424
45b4233c
DW
425 pr_debug("insert_hash(), stripe %llu\n",
426 (unsigned long long)sh->sector);
1da177e4 427
fccddba0 428 hlist_add_head(&sh->hash, hp);
1da177e4
LT
429}
430
1da177e4 431/* find an idle stripe, make sure it is unhashed, and return it. */
566c09c5 432static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
1da177e4
LT
433{
434 struct stripe_head *sh = NULL;
435 struct list_head *first;
436
566c09c5 437 if (list_empty(conf->inactive_list + hash))
1da177e4 438 goto out;
566c09c5 439 first = (conf->inactive_list + hash)->next;
1da177e4
LT
440 sh = list_entry(first, struct stripe_head, lru);
441 list_del_init(first);
442 remove_hash(sh);
443 atomic_inc(&conf->active_stripes);
566c09c5 444 BUG_ON(hash != sh->hash_lock_index);
4bda556a
SL
445 if (list_empty(conf->inactive_list + hash))
446 atomic_inc(&conf->empty_inactive_list_nr);
1da177e4
LT
447out:
448 return sh;
449}
450
046169f0
YY
451#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
452static void free_stripe_pages(struct stripe_head *sh)
1da177e4 453{
046169f0 454 int i;
1da177e4 455 struct page *p;
046169f0
YY
456
457 /* Have not allocate page pool */
458 if (!sh->pages)
459 return;
460
461 for (i = 0; i < sh->nr_pages; i++) {
462 p = sh->pages[i];
463 if (p)
464 put_page(p);
465 sh->pages[i] = NULL;
466 }
467}
468
469static int alloc_stripe_pages(struct stripe_head *sh, gfp_t gfp)
470{
471 int i;
472 struct page *p;
473
474 for (i = 0; i < sh->nr_pages; i++) {
475 /* The page have allocated. */
476 if (sh->pages[i])
477 continue;
478
479 p = alloc_page(gfp);
480 if (!p) {
481 free_stripe_pages(sh);
482 return -ENOMEM;
483 }
484 sh->pages[i] = p;
485 }
486 return 0;
487}
488
489static int
490init_stripe_shared_pages(struct stripe_head *sh, struct r5conf *conf, int disks)
491{
492 int nr_pages, cnt;
493
494 if (sh->pages)
495 return 0;
496
497 /* Each of the sh->dev[i] need one conf->stripe_size */
498 cnt = PAGE_SIZE / conf->stripe_size;
499 nr_pages = (disks + cnt - 1) / cnt;
500
501 sh->pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
502 if (!sh->pages)
503 return -ENOMEM;
504 sh->nr_pages = nr_pages;
505 sh->stripes_per_page = cnt;
506 return 0;
507}
508#endif
509
510static void shrink_buffers(struct stripe_head *sh)
511{
1da177e4 512 int i;
e4e11e38 513 int num = sh->raid_conf->pool_size;
1da177e4 514
046169f0 515#if PAGE_SIZE == DEFAULT_STRIPE_SIZE
e4e11e38 516 for (i = 0; i < num ; i++) {
046169f0
YY
517 struct page *p;
518
d592a996 519 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
1da177e4
LT
520 p = sh->dev[i].page;
521 if (!p)
522 continue;
523 sh->dev[i].page = NULL;
2d1f3b5d 524 put_page(p);
1da177e4 525 }
046169f0
YY
526#else
527 for (i = 0; i < num; i++)
528 sh->dev[i].page = NULL;
529 free_stripe_pages(sh); /* Free pages */
530#endif
1da177e4
LT
531}
532
a9683a79 533static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
1da177e4
LT
534{
535 int i;
e4e11e38 536 int num = sh->raid_conf->pool_size;
1da177e4 537
046169f0 538#if PAGE_SIZE == DEFAULT_STRIPE_SIZE
e4e11e38 539 for (i = 0; i < num; i++) {
1da177e4
LT
540 struct page *page;
541
a9683a79 542 if (!(page = alloc_page(gfp))) {
1da177e4
LT
543 return 1;
544 }
545 sh->dev[i].page = page;
d592a996 546 sh->dev[i].orig_page = page;
7aba13b7 547 sh->dev[i].offset = 0;
1da177e4 548 }
046169f0
YY
549#else
550 if (alloc_stripe_pages(sh, gfp))
551 return -ENOMEM;
3418d036 552
046169f0
YY
553 for (i = 0; i < num; i++) {
554 sh->dev[i].page = raid5_get_dev_page(sh, i);
555 sh->dev[i].orig_page = sh->dev[i].page;
556 sh->dev[i].offset = raid5_get_page_offset(sh, i);
557 }
558#endif
1da177e4
LT
559 return 0;
560}
561
d1688a6d 562static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
911d4ee8 563 struct stripe_head *sh);
1da177e4 564
b5663ba4 565static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
1da177e4 566{
d1688a6d 567 struct r5conf *conf = sh->raid_conf;
566c09c5 568 int i, seq;
1da177e4 569
78bafebd
ES
570 BUG_ON(atomic_read(&sh->count) != 0);
571 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
600aa109 572 BUG_ON(stripe_operations_active(sh));
59fc630b 573 BUG_ON(sh->batch_head);
d84e0f10 574
45b4233c 575 pr_debug("init_stripe called, stripe %llu\n",
b8e6a15a 576 (unsigned long long)sector);
566c09c5
SL
577retry:
578 seq = read_seqcount_begin(&conf->gen_lock);
86b42c71 579 sh->generation = conf->generation - previous;
b5663ba4 580 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
1da177e4 581 sh->sector = sector;
911d4ee8 582 stripe_set_idx(sector, conf, previous, sh);
1da177e4
LT
583 sh->state = 0;
584
7ecaa1e6 585 for (i = sh->disks; i--; ) {
1da177e4
LT
586 struct r5dev *dev = &sh->dev[i];
587
d84e0f10 588 if (dev->toread || dev->read || dev->towrite || dev->written ||
1da177e4 589 test_bit(R5_LOCKED, &dev->flags)) {
cc6167b4 590 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
1da177e4 591 (unsigned long long)sh->sector, i, dev->toread,
d84e0f10 592 dev->read, dev->towrite, dev->written,
1da177e4 593 test_bit(R5_LOCKED, &dev->flags));
8cfa7b0f 594 WARN_ON(1);
1da177e4
LT
595 }
596 dev->flags = 0;
27a4ff8f 597 dev->sector = raid5_compute_blocknr(sh, i, previous);
1da177e4 598 }
566c09c5
SL
599 if (read_seqcount_retry(&conf->gen_lock, seq))
600 goto retry;
7a87f434 601 sh->overwrite_disks = 0;
1da177e4 602 insert_hash(conf, sh);
851c30c9 603 sh->cpu = smp_processor_id();
da41ba65 604 set_bit(STRIPE_BATCH_READY, &sh->state);
1da177e4
LT
605}
606
d1688a6d 607static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
86b42c71 608 short generation)
1da177e4
LT
609{
610 struct stripe_head *sh;
611
45b4233c 612 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
b67bfe0d 613 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
86b42c71 614 if (sh->sector == sector && sh->generation == generation)
1da177e4 615 return sh;
45b4233c 616 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
1da177e4
LT
617 return NULL;
618}
619
674806d6
N
620/*
621 * Need to check if array has failed when deciding whether to:
622 * - start an array
623 * - remove non-faulty devices
624 * - add a spare
625 * - allow a reshape
626 * This determination is simple when no reshape is happening.
627 * However if there is a reshape, we need to carefully check
628 * both the before and after sections.
629 * This is because some failed devices may only affect one
630 * of the two sections, and some non-in_sync devices may
631 * be insync in the section most affected by failed devices.
632 */
2e38a37f 633int raid5_calc_degraded(struct r5conf *conf)
674806d6 634{
908f4fbd 635 int degraded, degraded2;
674806d6 636 int i;
674806d6
N
637
638 rcu_read_lock();
639 degraded = 0;
640 for (i = 0; i < conf->previous_raid_disks; i++) {
3cb03002 641 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
e5c86471
N
642 if (rdev && test_bit(Faulty, &rdev->flags))
643 rdev = rcu_dereference(conf->disks[i].replacement);
674806d6
N
644 if (!rdev || test_bit(Faulty, &rdev->flags))
645 degraded++;
646 else if (test_bit(In_sync, &rdev->flags))
647 ;
648 else
649 /* not in-sync or faulty.
650 * If the reshape increases the number of devices,
651 * this is being recovered by the reshape, so
652 * this 'previous' section is not in_sync.
653 * If the number of devices is being reduced however,
654 * the device can only be part of the array if
655 * we are reverting a reshape, so this section will
656 * be in-sync.
657 */
658 if (conf->raid_disks >= conf->previous_raid_disks)
659 degraded++;
660 }
661 rcu_read_unlock();
908f4fbd
N
662 if (conf->raid_disks == conf->previous_raid_disks)
663 return degraded;
674806d6 664 rcu_read_lock();
908f4fbd 665 degraded2 = 0;
674806d6 666 for (i = 0; i < conf->raid_disks; i++) {
3cb03002 667 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
e5c86471
N
668 if (rdev && test_bit(Faulty, &rdev->flags))
669 rdev = rcu_dereference(conf->disks[i].replacement);
674806d6 670 if (!rdev || test_bit(Faulty, &rdev->flags))
908f4fbd 671 degraded2++;
674806d6
N
672 else if (test_bit(In_sync, &rdev->flags))
673 ;
674 else
675 /* not in-sync or faulty.
676 * If reshape increases the number of devices, this
677 * section has already been recovered, else it
678 * almost certainly hasn't.
679 */
680 if (conf->raid_disks <= conf->previous_raid_disks)
908f4fbd 681 degraded2++;
674806d6
N
682 }
683 rcu_read_unlock();
908f4fbd
N
684 if (degraded2 > degraded)
685 return degraded2;
686 return degraded;
687}
688
ba97a054 689static bool has_failed(struct r5conf *conf)
908f4fbd 690{
ba97a054 691 int degraded = conf->mddev->degraded;
908f4fbd 692
ba97a054
MT
693 if (test_bit(MD_BROKEN, &conf->mddev->flags))
694 return true;
908f4fbd 695
ba97a054
MT
696 if (conf->mddev->reshape_position != MaxSector)
697 degraded = raid5_calc_degraded(conf);
698
699 return degraded > conf->max_degraded;
674806d6
N
700}
701
6d036f7d
SL
702struct stripe_head *
703raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
704 int previous, int noblock, int noquiesce)
1da177e4
LT
705{
706 struct stripe_head *sh;
c911c46c 707 int hash = stripe_hash_locks_hash(conf, sector);
ff00d3b4 708 int inc_empty_inactive_list_flag;
1da177e4 709
45b4233c 710 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
1da177e4 711
566c09c5 712 spin_lock_irq(conf->hash_locks + hash);
1da177e4
LT
713
714 do {
b1b46486 715 wait_event_lock_irq(conf->wait_for_quiescent,
a8c906ca 716 conf->quiesce == 0 || noquiesce,
566c09c5 717 *(conf->hash_locks + hash));
86b42c71 718 sh = __find_stripe(conf, sector, conf->generation - previous);
1da177e4 719 if (!sh) {
edbe83ab 720 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
566c09c5 721 sh = get_free_stripe(conf, hash);
713bc5c2
SL
722 if (!sh && !test_bit(R5_DID_ALLOC,
723 &conf->cache_state))
edbe83ab
N
724 set_bit(R5_ALLOC_MORE,
725 &conf->cache_state);
726 }
1da177e4
LT
727 if (noblock && sh == NULL)
728 break;
a39f7afd
SL
729
730 r5c_check_stripe_cache_usage(conf);
1da177e4 731 if (!sh) {
5423399a
N
732 set_bit(R5_INACTIVE_BLOCKED,
733 &conf->cache_state);
a39f7afd 734 r5l_wake_reclaim(conf->log, 0);
6ab2a4b8
SL
735 wait_event_lock_irq(
736 conf->wait_for_stripe,
566c09c5
SL
737 !list_empty(conf->inactive_list + hash) &&
738 (atomic_read(&conf->active_stripes)
739 < (conf->max_nr_stripes * 3 / 4)
5423399a
N
740 || !test_bit(R5_INACTIVE_BLOCKED,
741 &conf->cache_state)),
6ab2a4b8 742 *(conf->hash_locks + hash));
5423399a
N
743 clear_bit(R5_INACTIVE_BLOCKED,
744 &conf->cache_state);
7da9d450 745 } else {
b5663ba4 746 init_stripe(sh, sector, previous);
7da9d450
N
747 atomic_inc(&sh->count);
748 }
e240c183 749 } else if (!atomic_inc_not_zero(&sh->count)) {
6d183de4 750 spin_lock(&conf->device_lock);
e240c183 751 if (!atomic_read(&sh->count)) {
1da177e4
LT
752 if (!test_bit(STRIPE_HANDLE, &sh->state))
753 atomic_inc(&conf->active_stripes);
5af9bef7
N
754 BUG_ON(list_empty(&sh->lru) &&
755 !test_bit(STRIPE_EXPANDING, &sh->state));
ff00d3b4
ZL
756 inc_empty_inactive_list_flag = 0;
757 if (!list_empty(conf->inactive_list + hash))
758 inc_empty_inactive_list_flag = 1;
16a53ecc 759 list_del_init(&sh->lru);
ff00d3b4
ZL
760 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
761 atomic_inc(&conf->empty_inactive_list_nr);
bfc90cb0
SL
762 if (sh->group) {
763 sh->group->stripes_cnt--;
764 sh->group = NULL;
765 }
1da177e4 766 }
7da9d450 767 atomic_inc(&sh->count);
6d183de4 768 spin_unlock(&conf->device_lock);
1da177e4
LT
769 }
770 } while (sh == NULL);
771
566c09c5 772 spin_unlock_irq(conf->hash_locks + hash);
1da177e4
LT
773 return sh;
774}
775
7a87f434 776static bool is_full_stripe_write(struct stripe_head *sh)
777{
778 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
779 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
780}
781
59fc630b 782static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
368ecade
CH
783 __acquires(&sh1->stripe_lock)
784 __acquires(&sh2->stripe_lock)
59fc630b 785{
59fc630b 786 if (sh1 > sh2) {
3d05f3ae 787 spin_lock_irq(&sh2->stripe_lock);
59fc630b 788 spin_lock_nested(&sh1->stripe_lock, 1);
789 } else {
3d05f3ae 790 spin_lock_irq(&sh1->stripe_lock);
59fc630b 791 spin_lock_nested(&sh2->stripe_lock, 1);
792 }
793}
794
795static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
368ecade
CH
796 __releases(&sh1->stripe_lock)
797 __releases(&sh2->stripe_lock)
59fc630b 798{
799 spin_unlock(&sh1->stripe_lock);
3d05f3ae 800 spin_unlock_irq(&sh2->stripe_lock);
59fc630b 801}
802
803/* Only freshly new full stripe normal write stripe can be added to a batch list */
804static bool stripe_can_batch(struct stripe_head *sh)
805{
9c3e333d
SL
806 struct r5conf *conf = sh->raid_conf;
807
e254de6b 808 if (raid5_has_log(conf) || raid5_has_ppl(conf))
9c3e333d 809 return false;
59fc630b 810 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
d0852df5 811 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
59fc630b 812 is_full_stripe_write(sh);
813}
814
815/* we only do back search */
816static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
817{
818 struct stripe_head *head;
819 sector_t head_sector, tmp_sec;
820 int hash;
821 int dd_idx;
ff00d3b4 822 int inc_empty_inactive_list_flag;
59fc630b 823
59fc630b 824 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
825 tmp_sec = sh->sector;
826 if (!sector_div(tmp_sec, conf->chunk_sectors))
827 return;
c911c46c 828 head_sector = sh->sector - RAID5_STRIPE_SECTORS(conf);
59fc630b 829
c911c46c 830 hash = stripe_hash_locks_hash(conf, head_sector);
59fc630b 831 spin_lock_irq(conf->hash_locks + hash);
832 head = __find_stripe(conf, head_sector, conf->generation);
833 if (head && !atomic_inc_not_zero(&head->count)) {
834 spin_lock(&conf->device_lock);
835 if (!atomic_read(&head->count)) {
836 if (!test_bit(STRIPE_HANDLE, &head->state))
837 atomic_inc(&conf->active_stripes);
838 BUG_ON(list_empty(&head->lru) &&
839 !test_bit(STRIPE_EXPANDING, &head->state));
ff00d3b4
ZL
840 inc_empty_inactive_list_flag = 0;
841 if (!list_empty(conf->inactive_list + hash))
842 inc_empty_inactive_list_flag = 1;
59fc630b 843 list_del_init(&head->lru);
ff00d3b4
ZL
844 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
845 atomic_inc(&conf->empty_inactive_list_nr);
59fc630b 846 if (head->group) {
847 head->group->stripes_cnt--;
848 head->group = NULL;
849 }
850 }
851 atomic_inc(&head->count);
852 spin_unlock(&conf->device_lock);
853 }
854 spin_unlock_irq(conf->hash_locks + hash);
855
856 if (!head)
857 return;
858 if (!stripe_can_batch(head))
859 goto out;
860
861 lock_two_stripes(head, sh);
862 /* clear_batch_ready clear the flag */
863 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
864 goto unlock_out;
865
866 if (sh->batch_head)
867 goto unlock_out;
868
869 dd_idx = 0;
870 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
871 dd_idx++;
1eff9d32 872 if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
796a5cf0 873 bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
59fc630b 874 goto unlock_out;
875
876 if (head->batch_head) {
877 spin_lock(&head->batch_head->batch_lock);
878 /* This batch list is already running */
879 if (!stripe_can_batch(head)) {
880 spin_unlock(&head->batch_head->batch_lock);
881 goto unlock_out;
882 }
3664847d
SL
883 /*
884 * We must assign batch_head of this stripe within the
885 * batch_lock, otherwise clear_batch_ready of batch head
886 * stripe could clear BATCH_READY bit of this stripe and
887 * this stripe->batch_head doesn't get assigned, which
888 * could confuse clear_batch_ready for this stripe
889 */
890 sh->batch_head = head->batch_head;
59fc630b 891
892 /*
893 * at this point, head's BATCH_READY could be cleared, but we
894 * can still add the stripe to batch list
895 */
896 list_add(&sh->batch_list, &head->batch_list);
897 spin_unlock(&head->batch_head->batch_lock);
59fc630b 898 } else {
899 head->batch_head = head;
900 sh->batch_head = head->batch_head;
901 spin_lock(&head->batch_lock);
902 list_add_tail(&sh->batch_list, &head->batch_list);
903 spin_unlock(&head->batch_lock);
904 }
905
906 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
907 if (atomic_dec_return(&conf->preread_active_stripes)
908 < IO_THRESHOLD)
909 md_wakeup_thread(conf->mddev->thread);
910
2b6b2457
N
911 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
912 int seq = sh->bm_seq;
913 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
914 sh->batch_head->bm_seq > seq)
915 seq = sh->batch_head->bm_seq;
916 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
917 sh->batch_head->bm_seq = seq;
918 }
919
59fc630b 920 atomic_inc(&sh->count);
921unlock_out:
922 unlock_two_stripes(head, sh);
923out:
6d036f7d 924 raid5_release_stripe(head);
59fc630b 925}
926
05616be5
N
927/* Determine if 'data_offset' or 'new_data_offset' should be used
928 * in this stripe_head.
929 */
930static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
931{
932 sector_t progress = conf->reshape_progress;
933 /* Need a memory barrier to make sure we see the value
934 * of conf->generation, or ->data_offset that was set before
935 * reshape_progress was updated.
936 */
937 smp_rmb();
938 if (progress == MaxSector)
939 return 0;
940 if (sh->generation == conf->generation - 1)
941 return 0;
942 /* We are in a reshape, and this is a new-generation stripe,
943 * so use new_data_offset.
944 */
945 return 1;
946}
947
aaf9f12e 948static void dispatch_bio_list(struct bio_list *tmp)
765d704d 949{
765d704d
SL
950 struct bio *bio;
951
aaf9f12e 952 while ((bio = bio_list_pop(tmp)))
ed00aabd 953 submit_bio_noacct(bio);
aaf9f12e
SL
954}
955
4f0f586b
ST
956static int cmp_stripe(void *priv, const struct list_head *a,
957 const struct list_head *b)
aaf9f12e
SL
958{
959 const struct r5pending_data *da = list_entry(a,
960 struct r5pending_data, sibling);
961 const struct r5pending_data *db = list_entry(b,
962 struct r5pending_data, sibling);
963 if (da->sector > db->sector)
964 return 1;
965 if (da->sector < db->sector)
966 return -1;
967 return 0;
968}
969
970static void dispatch_defer_bios(struct r5conf *conf, int target,
971 struct bio_list *list)
972{
973 struct r5pending_data *data;
974 struct list_head *first, *next = NULL;
975 int cnt = 0;
976
977 if (conf->pending_data_cnt == 0)
978 return;
979
980 list_sort(NULL, &conf->pending_list, cmp_stripe);
981
982 first = conf->pending_list.next;
983
984 /* temporarily move the head */
985 if (conf->next_pending_data)
986 list_move_tail(&conf->pending_list,
987 &conf->next_pending_data->sibling);
988
989 while (!list_empty(&conf->pending_list)) {
990 data = list_first_entry(&conf->pending_list,
991 struct r5pending_data, sibling);
992 if (&data->sibling == first)
993 first = data->sibling.next;
994 next = data->sibling.next;
995
996 bio_list_merge(list, &data->bios);
997 list_move(&data->sibling, &conf->free_list);
998 cnt++;
999 if (cnt >= target)
1000 break;
1001 }
1002 conf->pending_data_cnt -= cnt;
1003 BUG_ON(conf->pending_data_cnt < 0 || cnt < target);
1004
1005 if (next != &conf->pending_list)
1006 conf->next_pending_data = list_entry(next,
1007 struct r5pending_data, sibling);
1008 else
1009 conf->next_pending_data = NULL;
1010 /* list isn't empty */
1011 if (first != &conf->pending_list)
1012 list_move_tail(&conf->pending_list, first);
1013}
1014
1015static void flush_deferred_bios(struct r5conf *conf)
1016{
1017 struct bio_list tmp = BIO_EMPTY_LIST;
1018
1019 if (conf->pending_data_cnt == 0)
765d704d
SL
1020 return;
1021
765d704d 1022 spin_lock(&conf->pending_bios_lock);
aaf9f12e
SL
1023 dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp);
1024 BUG_ON(conf->pending_data_cnt != 0);
765d704d
SL
1025 spin_unlock(&conf->pending_bios_lock);
1026
aaf9f12e 1027 dispatch_bio_list(&tmp);
765d704d
SL
1028}
1029
aaf9f12e
SL
1030static void defer_issue_bios(struct r5conf *conf, sector_t sector,
1031 struct bio_list *bios)
765d704d 1032{
aaf9f12e
SL
1033 struct bio_list tmp = BIO_EMPTY_LIST;
1034 struct r5pending_data *ent;
1035
765d704d 1036 spin_lock(&conf->pending_bios_lock);
aaf9f12e
SL
1037 ent = list_first_entry(&conf->free_list, struct r5pending_data,
1038 sibling);
1039 list_move_tail(&ent->sibling, &conf->pending_list);
1040 ent->sector = sector;
1041 bio_list_init(&ent->bios);
1042 bio_list_merge(&ent->bios, bios);
1043 conf->pending_data_cnt++;
1044 if (conf->pending_data_cnt >= PENDING_IO_MAX)
1045 dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp);
1046
765d704d 1047 spin_unlock(&conf->pending_bios_lock);
aaf9f12e
SL
1048
1049 dispatch_bio_list(&tmp);
765d704d
SL
1050}
1051
6712ecf8 1052static void
4246a0b6 1053raid5_end_read_request(struct bio *bi);
6712ecf8 1054static void
4246a0b6 1055raid5_end_write_request(struct bio *bi);
91c00924 1056
c4e5ac0a 1057static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
91c00924 1058{
d1688a6d 1059 struct r5conf *conf = sh->raid_conf;
91c00924 1060 int i, disks = sh->disks;
59fc630b 1061 struct stripe_head *head_sh = sh;
aaf9f12e
SL
1062 struct bio_list pending_bios = BIO_EMPTY_LIST;
1063 bool should_defer;
91c00924
DW
1064
1065 might_sleep();
1066
ff875738
AP
1067 if (log_stripe(sh, s) == 0)
1068 return;
1e6d690b 1069
aaf9f12e 1070 should_defer = conf->batch_bio_dispatch && conf->group_cnt;
1e6d690b 1071
91c00924 1072 for (i = disks; i--; ) {
796a5cf0 1073 int op, op_flags = 0;
9a3e1101 1074 int replace_only = 0;
977df362
N
1075 struct bio *bi, *rbi;
1076 struct md_rdev *rdev, *rrdev = NULL;
59fc630b 1077
1078 sh = head_sh;
e9c7469b 1079 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
796a5cf0 1080 op = REQ_OP_WRITE;
e9c7469b 1081 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
70fd7614 1082 op_flags = REQ_FUA;
9e444768 1083 if (test_bit(R5_Discard, &sh->dev[i].flags))
796a5cf0 1084 op = REQ_OP_DISCARD;
e9c7469b 1085 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
796a5cf0 1086 op = REQ_OP_READ;
9a3e1101
N
1087 else if (test_and_clear_bit(R5_WantReplace,
1088 &sh->dev[i].flags)) {
796a5cf0 1089 op = REQ_OP_WRITE;
9a3e1101
N
1090 replace_only = 1;
1091 } else
91c00924 1092 continue;
bc0934f0 1093 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
796a5cf0 1094 op_flags |= REQ_SYNC;
91c00924 1095
59fc630b 1096again:
91c00924 1097 bi = &sh->dev[i].req;
977df362 1098 rbi = &sh->dev[i].rreq; /* For writing to replacement */
91c00924 1099
91c00924 1100 rcu_read_lock();
9a3e1101 1101 rrdev = rcu_dereference(conf->disks[i].replacement);
dd054fce
N
1102 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
1103 rdev = rcu_dereference(conf->disks[i].rdev);
1104 if (!rdev) {
1105 rdev = rrdev;
1106 rrdev = NULL;
1107 }
796a5cf0 1108 if (op_is_write(op)) {
9a3e1101
N
1109 if (replace_only)
1110 rdev = NULL;
dd054fce
N
1111 if (rdev == rrdev)
1112 /* We raced and saw duplicates */
1113 rrdev = NULL;
9a3e1101 1114 } else {
59fc630b 1115 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
9a3e1101
N
1116 rdev = rrdev;
1117 rrdev = NULL;
1118 }
977df362 1119
91c00924
DW
1120 if (rdev && test_bit(Faulty, &rdev->flags))
1121 rdev = NULL;
1122 if (rdev)
1123 atomic_inc(&rdev->nr_pending);
977df362
N
1124 if (rrdev && test_bit(Faulty, &rrdev->flags))
1125 rrdev = NULL;
1126 if (rrdev)
1127 atomic_inc(&rrdev->nr_pending);
91c00924
DW
1128 rcu_read_unlock();
1129
73e92e51 1130 /* We have already checked bad blocks for reads. Now
977df362
N
1131 * need to check for writes. We never accept write errors
1132 * on the replacement, so we don't to check rrdev.
73e92e51 1133 */
796a5cf0 1134 while (op_is_write(op) && rdev &&
73e92e51
N
1135 test_bit(WriteErrorSeen, &rdev->flags)) {
1136 sector_t first_bad;
1137 int bad_sectors;
c911c46c 1138 int bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
73e92e51
N
1139 &first_bad, &bad_sectors);
1140 if (!bad)
1141 break;
1142
1143 if (bad < 0) {
1144 set_bit(BlockedBadBlocks, &rdev->flags);
1145 if (!conf->mddev->external &&
2953079c 1146 conf->mddev->sb_flags) {
73e92e51
N
1147 /* It is very unlikely, but we might
1148 * still need to write out the
1149 * bad block log - better give it
1150 * a chance*/
1151 md_check_recovery(conf->mddev);
1152 }
1850753d 1153 /*
1154 * Because md_wait_for_blocked_rdev
1155 * will dec nr_pending, we must
1156 * increment it first.
1157 */
1158 atomic_inc(&rdev->nr_pending);
73e92e51
N
1159 md_wait_for_blocked_rdev(rdev, conf->mddev);
1160 } else {
1161 /* Acknowledged bad block - skip the write */
1162 rdev_dec_pending(rdev, conf->mddev);
1163 rdev = NULL;
1164 }
1165 }
1166
91c00924 1167 if (rdev) {
9a3e1101
N
1168 if (s->syncing || s->expanding || s->expanded
1169 || s->replacing)
c911c46c 1170 md_sync_acct(rdev->bdev, RAID5_STRIPE_SECTORS(conf));
91c00924 1171
2b7497f0
DW
1172 set_bit(STRIPE_IO_STARTED, &sh->state);
1173
74d46992 1174 bio_set_dev(bi, rdev->bdev);
796a5cf0
MC
1175 bio_set_op_attrs(bi, op, op_flags);
1176 bi->bi_end_io = op_is_write(op)
2f6db2a7
KO
1177 ? raid5_end_write_request
1178 : raid5_end_read_request;
1179 bi->bi_private = sh;
1180
6296b960 1181 pr_debug("%s: for %llu schedule op %d on disc %d\n",
e46b272b 1182 __func__, (unsigned long long)sh->sector,
1eff9d32 1183 bi->bi_opf, i);
91c00924 1184 atomic_inc(&sh->count);
59fc630b 1185 if (sh != head_sh)
1186 atomic_inc(&head_sh->count);
05616be5 1187 if (use_new_offset(conf, sh))
4f024f37 1188 bi->bi_iter.bi_sector = (sh->sector
05616be5
N
1189 + rdev->new_data_offset);
1190 else
4f024f37 1191 bi->bi_iter.bi_sector = (sh->sector
05616be5 1192 + rdev->data_offset);
59fc630b 1193 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
1eff9d32 1194 bi->bi_opf |= REQ_NOMERGE;
3f9e7c14 1195
d592a996
SL
1196 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1197 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
86aa1397
SL
1198
1199 if (!op_is_write(op) &&
1200 test_bit(R5_InJournal, &sh->dev[i].flags))
1201 /*
1202 * issuing read for a page in journal, this
1203 * must be preparing for prexor in rmw; read
1204 * the data into orig_page
1205 */
1206 sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
1207 else
1208 sh->dev[i].vec.bv_page = sh->dev[i].page;
4997b72e 1209 bi->bi_vcnt = 1;
c911c46c 1210 bi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf);
7aba13b7 1211 bi->bi_io_vec[0].bv_offset = sh->dev[i].offset;
c911c46c 1212 bi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf);
2cd259a7
MD
1213 bi->bi_write_hint = sh->dev[i].write_hint;
1214 if (!rrdev)
f1934892 1215 sh->dev[i].write_hint = RWH_WRITE_LIFE_NOT_SET;
37c61ff3
SL
1216 /*
1217 * If this is discard request, set bi_vcnt 0. We don't
1218 * want to confuse SCSI because SCSI will replace payload
1219 */
796a5cf0 1220 if (op == REQ_OP_DISCARD)
37c61ff3 1221 bi->bi_vcnt = 0;
977df362
N
1222 if (rrdev)
1223 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
e3620a3a
JB
1224
1225 if (conf->mddev->gendisk)
1c02fca6
CH
1226 trace_block_bio_remap(bi,
1227 disk_devt(conf->mddev->gendisk),
1228 sh->dev[i].sector);
aaf9f12e
SL
1229 if (should_defer && op_is_write(op))
1230 bio_list_add(&pending_bios, bi);
1231 else
ed00aabd 1232 submit_bio_noacct(bi);
977df362
N
1233 }
1234 if (rrdev) {
9a3e1101
N
1235 if (s->syncing || s->expanding || s->expanded
1236 || s->replacing)
c911c46c 1237 md_sync_acct(rrdev->bdev, RAID5_STRIPE_SECTORS(conf));
977df362
N
1238
1239 set_bit(STRIPE_IO_STARTED, &sh->state);
1240
74d46992 1241 bio_set_dev(rbi, rrdev->bdev);
796a5cf0
MC
1242 bio_set_op_attrs(rbi, op, op_flags);
1243 BUG_ON(!op_is_write(op));
2f6db2a7
KO
1244 rbi->bi_end_io = raid5_end_write_request;
1245 rbi->bi_private = sh;
1246
6296b960 1247 pr_debug("%s: for %llu schedule op %d on "
977df362
N
1248 "replacement disc %d\n",
1249 __func__, (unsigned long long)sh->sector,
1eff9d32 1250 rbi->bi_opf, i);
977df362 1251 atomic_inc(&sh->count);
59fc630b 1252 if (sh != head_sh)
1253 atomic_inc(&head_sh->count);
05616be5 1254 if (use_new_offset(conf, sh))
4f024f37 1255 rbi->bi_iter.bi_sector = (sh->sector
05616be5
N
1256 + rrdev->new_data_offset);
1257 else
4f024f37 1258 rbi->bi_iter.bi_sector = (sh->sector
05616be5 1259 + rrdev->data_offset);
d592a996
SL
1260 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1261 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1262 sh->dev[i].rvec.bv_page = sh->dev[i].page;
4997b72e 1263 rbi->bi_vcnt = 1;
c911c46c 1264 rbi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf);
7aba13b7 1265 rbi->bi_io_vec[0].bv_offset = sh->dev[i].offset;
c911c46c 1266 rbi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf);
2cd259a7 1267 rbi->bi_write_hint = sh->dev[i].write_hint;
f1934892 1268 sh->dev[i].write_hint = RWH_WRITE_LIFE_NOT_SET;
37c61ff3
SL
1269 /*
1270 * If this is discard request, set bi_vcnt 0. We don't
1271 * want to confuse SCSI because SCSI will replace payload
1272 */
796a5cf0 1273 if (op == REQ_OP_DISCARD)
37c61ff3 1274 rbi->bi_vcnt = 0;
e3620a3a 1275 if (conf->mddev->gendisk)
1c02fca6
CH
1276 trace_block_bio_remap(rbi,
1277 disk_devt(conf->mddev->gendisk),
1278 sh->dev[i].sector);
aaf9f12e
SL
1279 if (should_defer && op_is_write(op))
1280 bio_list_add(&pending_bios, rbi);
1281 else
ed00aabd 1282 submit_bio_noacct(rbi);
977df362
N
1283 }
1284 if (!rdev && !rrdev) {
796a5cf0 1285 if (op_is_write(op))
91c00924 1286 set_bit(STRIPE_DEGRADED, &sh->state);
6296b960 1287 pr_debug("skip op %d on disc %d for sector %llu\n",
1eff9d32 1288 bi->bi_opf, i, (unsigned long long)sh->sector);
91c00924
DW
1289 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1290 set_bit(STRIPE_HANDLE, &sh->state);
1291 }
59fc630b 1292
1293 if (!head_sh->batch_head)
1294 continue;
1295 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1296 batch_list);
1297 if (sh != head_sh)
1298 goto again;
91c00924 1299 }
aaf9f12e
SL
1300
1301 if (should_defer && !bio_list_empty(&pending_bios))
1302 defer_issue_bios(conf, head_sh->sector, &pending_bios);
91c00924
DW
1303}
1304
1305static struct dma_async_tx_descriptor *
d592a996 1306async_copy_data(int frombio, struct bio *bio, struct page **page,
248728dd 1307 unsigned int poff, sector_t sector, struct dma_async_tx_descriptor *tx,
1e6d690b 1308 struct stripe_head *sh, int no_skipcopy)
91c00924 1309{
7988613b
KO
1310 struct bio_vec bvl;
1311 struct bvec_iter iter;
91c00924 1312 struct page *bio_page;
91c00924 1313 int page_offset;
a08abd8c 1314 struct async_submit_ctl submit;
0403e382 1315 enum async_tx_flags flags = 0;
c911c46c 1316 struct r5conf *conf = sh->raid_conf;
91c00924 1317
4f024f37
KO
1318 if (bio->bi_iter.bi_sector >= sector)
1319 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
91c00924 1320 else
4f024f37 1321 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
a08abd8c 1322
0403e382
DW
1323 if (frombio)
1324 flags |= ASYNC_TX_FENCE;
1325 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1326
7988613b
KO
1327 bio_for_each_segment(bvl, bio, iter) {
1328 int len = bvl.bv_len;
91c00924
DW
1329 int clen;
1330 int b_offset = 0;
1331
1332 if (page_offset < 0) {
1333 b_offset = -page_offset;
1334 page_offset += b_offset;
1335 len -= b_offset;
1336 }
1337
c911c46c
YY
1338 if (len > 0 && page_offset + len > RAID5_STRIPE_SIZE(conf))
1339 clen = RAID5_STRIPE_SIZE(conf) - page_offset;
91c00924
DW
1340 else
1341 clen = len;
1342
1343 if (clen > 0) {
7988613b
KO
1344 b_offset += bvl.bv_offset;
1345 bio_page = bvl.bv_page;
d592a996 1346 if (frombio) {
c911c46c 1347 if (conf->skip_copy &&
d592a996 1348 b_offset == 0 && page_offset == 0 &&
c911c46c 1349 clen == RAID5_STRIPE_SIZE(conf) &&
1e6d690b 1350 !no_skipcopy)
d592a996
SL
1351 *page = bio_page;
1352 else
248728dd 1353 tx = async_memcpy(*page, bio_page, page_offset + poff,
a08abd8c 1354 b_offset, clen, &submit);
d592a996
SL
1355 } else
1356 tx = async_memcpy(bio_page, *page, b_offset,
248728dd 1357 page_offset + poff, clen, &submit);
91c00924 1358 }
a08abd8c
DW
1359 /* chain the operations */
1360 submit.depend_tx = tx;
1361
91c00924
DW
1362 if (clen < len) /* hit end of page */
1363 break;
1364 page_offset += len;
1365 }
1366
1367 return tx;
1368}
1369
1370static void ops_complete_biofill(void *stripe_head_ref)
1371{
1372 struct stripe_head *sh = stripe_head_ref;
e4d84909 1373 int i;
c911c46c 1374 struct r5conf *conf = sh->raid_conf;
91c00924 1375
e46b272b 1376 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1377 (unsigned long long)sh->sector);
1378
1379 /* clear completed biofills */
1380 for (i = sh->disks; i--; ) {
1381 struct r5dev *dev = &sh->dev[i];
91c00924
DW
1382
1383 /* acknowledge completion of a biofill operation */
e4d84909
DW
1384 /* and check if we need to reply to a read request,
1385 * new R5_Wantfill requests are held off until
83de75cc 1386 * !STRIPE_BIOFILL_RUN
e4d84909
DW
1387 */
1388 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
91c00924 1389 struct bio *rbi, *rbi2;
91c00924 1390
91c00924
DW
1391 BUG_ON(!dev->read);
1392 rbi = dev->read;
1393 dev->read = NULL;
4f024f37 1394 while (rbi && rbi->bi_iter.bi_sector <
c911c46c
YY
1395 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
1396 rbi2 = r5_next_bio(conf, rbi, dev->sector);
016c76ac 1397 bio_endio(rbi);
91c00924
DW
1398 rbi = rbi2;
1399 }
1400 }
1401 }
83de75cc 1402 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
91c00924 1403
e4d84909 1404 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 1405 raid5_release_stripe(sh);
91c00924
DW
1406}
1407
1408static void ops_run_biofill(struct stripe_head *sh)
1409{
1410 struct dma_async_tx_descriptor *tx = NULL;
a08abd8c 1411 struct async_submit_ctl submit;
91c00924 1412 int i;
c911c46c 1413 struct r5conf *conf = sh->raid_conf;
91c00924 1414
59fc630b 1415 BUG_ON(sh->batch_head);
e46b272b 1416 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1417 (unsigned long long)sh->sector);
1418
1419 for (i = sh->disks; i--; ) {
1420 struct r5dev *dev = &sh->dev[i];
1421 if (test_bit(R5_Wantfill, &dev->flags)) {
1422 struct bio *rbi;
b17459c0 1423 spin_lock_irq(&sh->stripe_lock);
91c00924
DW
1424 dev->read = rbi = dev->toread;
1425 dev->toread = NULL;
b17459c0 1426 spin_unlock_irq(&sh->stripe_lock);
4f024f37 1427 while (rbi && rbi->bi_iter.bi_sector <
c911c46c 1428 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
d592a996 1429 tx = async_copy_data(0, rbi, &dev->page,
248728dd 1430 dev->offset,
1e6d690b 1431 dev->sector, tx, sh, 0);
c911c46c 1432 rbi = r5_next_bio(conf, rbi, dev->sector);
91c00924
DW
1433 }
1434 }
1435 }
1436
1437 atomic_inc(&sh->count);
a08abd8c
DW
1438 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1439 async_trigger_callback(&submit);
91c00924
DW
1440}
1441
4e7d2c0a 1442static void mark_target_uptodate(struct stripe_head *sh, int target)
91c00924 1443{
4e7d2c0a 1444 struct r5dev *tgt;
91c00924 1445
4e7d2c0a
DW
1446 if (target < 0)
1447 return;
91c00924 1448
4e7d2c0a 1449 tgt = &sh->dev[target];
91c00924
DW
1450 set_bit(R5_UPTODATE, &tgt->flags);
1451 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1452 clear_bit(R5_Wantcompute, &tgt->flags);
4e7d2c0a
DW
1453}
1454
ac6b53b6 1455static void ops_complete_compute(void *stripe_head_ref)
91c00924
DW
1456{
1457 struct stripe_head *sh = stripe_head_ref;
91c00924 1458
e46b272b 1459 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1460 (unsigned long long)sh->sector);
1461
ac6b53b6 1462 /* mark the computed target(s) as uptodate */
4e7d2c0a 1463 mark_target_uptodate(sh, sh->ops.target);
ac6b53b6 1464 mark_target_uptodate(sh, sh->ops.target2);
4e7d2c0a 1465
ecc65c9b
DW
1466 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1467 if (sh->check_state == check_state_compute_run)
1468 sh->check_state = check_state_compute_result;
91c00924 1469 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 1470 raid5_release_stripe(sh);
91c00924
DW
1471}
1472
d6f38f31 1473/* return a pointer to the address conversion region of the scribble buffer */
b330e6a4 1474static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
d6f38f31 1475{
b330e6a4 1476 return percpu->scribble + i * percpu->scribble_obj_size;
46d5b785 1477}
1478
1479/* return a pointer to the address conversion region of the scribble buffer */
b330e6a4
KO
1480static addr_conv_t *to_addr_conv(struct stripe_head *sh,
1481 struct raid5_percpu *percpu, int i)
46d5b785 1482{
b330e6a4 1483 return (void *) (to_addr_page(percpu, i) + sh->disks + 2);
d6f38f31
DW
1484}
1485
7aba13b7
YY
1486/*
1487 * Return a pointer to record offset address.
1488 */
1489static unsigned int *
1490to_addr_offs(struct stripe_head *sh, struct raid5_percpu *percpu)
1491{
1492 return (unsigned int *) (to_addr_conv(sh, percpu, 0) + sh->disks + 2);
1493}
1494
d6f38f31
DW
1495static struct dma_async_tx_descriptor *
1496ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
91c00924 1497{
91c00924 1498 int disks = sh->disks;
46d5b785 1499 struct page **xor_srcs = to_addr_page(percpu, 0);
7aba13b7 1500 unsigned int *off_srcs = to_addr_offs(sh, percpu);
91c00924
DW
1501 int target = sh->ops.target;
1502 struct r5dev *tgt = &sh->dev[target];
1503 struct page *xor_dest = tgt->page;
7aba13b7 1504 unsigned int off_dest = tgt->offset;
91c00924
DW
1505 int count = 0;
1506 struct dma_async_tx_descriptor *tx;
a08abd8c 1507 struct async_submit_ctl submit;
91c00924
DW
1508 int i;
1509
59fc630b 1510 BUG_ON(sh->batch_head);
1511
91c00924 1512 pr_debug("%s: stripe %llu block: %d\n",
e46b272b 1513 __func__, (unsigned long long)sh->sector, target);
91c00924
DW
1514 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1515
7aba13b7
YY
1516 for (i = disks; i--; ) {
1517 if (i != target) {
1518 off_srcs[count] = sh->dev[i].offset;
91c00924 1519 xor_srcs[count++] = sh->dev[i].page;
7aba13b7
YY
1520 }
1521 }
91c00924
DW
1522
1523 atomic_inc(&sh->count);
1524
0403e382 1525 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
46d5b785 1526 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
91c00924 1527 if (unlikely(count == 1))
7aba13b7 1528 tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0],
c911c46c 1529 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
91c00924 1530 else
a7c224a8 1531 tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
c911c46c 1532 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
91c00924 1533
91c00924
DW
1534 return tx;
1535}
1536
ac6b53b6
DW
1537/* set_syndrome_sources - populate source buffers for gen_syndrome
1538 * @srcs - (struct page *) array of size sh->disks
d69454bc 1539 * @offs - (unsigned int) array of offset for each page
ac6b53b6
DW
1540 * @sh - stripe_head to parse
1541 *
1542 * Populates srcs in proper layout order for the stripe and returns the
1543 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1544 * destination buffer is recorded in srcs[count] and the Q destination
1545 * is recorded in srcs[count+1]].
1546 */
584acdd4 1547static int set_syndrome_sources(struct page **srcs,
d69454bc 1548 unsigned int *offs,
584acdd4
MS
1549 struct stripe_head *sh,
1550 int srctype)
ac6b53b6
DW
1551{
1552 int disks = sh->disks;
1553 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1554 int d0_idx = raid6_d0(sh);
1555 int count;
1556 int i;
1557
1558 for (i = 0; i < disks; i++)
5dd33c9a 1559 srcs[i] = NULL;
ac6b53b6
DW
1560
1561 count = 0;
1562 i = d0_idx;
1563 do {
1564 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
584acdd4 1565 struct r5dev *dev = &sh->dev[i];
ac6b53b6 1566
584acdd4
MS
1567 if (i == sh->qd_idx || i == sh->pd_idx ||
1568 (srctype == SYNDROME_SRC_ALL) ||
1569 (srctype == SYNDROME_SRC_WANT_DRAIN &&
1e6d690b
SL
1570 (test_bit(R5_Wantdrain, &dev->flags) ||
1571 test_bit(R5_InJournal, &dev->flags))) ||
584acdd4 1572 (srctype == SYNDROME_SRC_WRITTEN &&
0977762f
SL
1573 (dev->written ||
1574 test_bit(R5_InJournal, &dev->flags)))) {
1e6d690b
SL
1575 if (test_bit(R5_InJournal, &dev->flags))
1576 srcs[slot] = sh->dev[i].orig_page;
1577 else
1578 srcs[slot] = sh->dev[i].page;
d69454bc
YY
1579 /*
1580 * For R5_InJournal, PAGE_SIZE must be 4KB and will
1581 * not shared page. In that case, dev[i].offset
1582 * is 0.
1583 */
1584 offs[slot] = sh->dev[i].offset;
1e6d690b 1585 }
ac6b53b6
DW
1586 i = raid6_next_disk(i, disks);
1587 } while (i != d0_idx);
ac6b53b6 1588
e4424fee 1589 return syndrome_disks;
ac6b53b6
DW
1590}
1591
1592static struct dma_async_tx_descriptor *
1593ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1594{
1595 int disks = sh->disks;
46d5b785 1596 struct page **blocks = to_addr_page(percpu, 0);
a7c224a8 1597 unsigned int *offs = to_addr_offs(sh, percpu);
ac6b53b6
DW
1598 int target;
1599 int qd_idx = sh->qd_idx;
1600 struct dma_async_tx_descriptor *tx;
1601 struct async_submit_ctl submit;
1602 struct r5dev *tgt;
1603 struct page *dest;
a7c224a8 1604 unsigned int dest_off;
ac6b53b6
DW
1605 int i;
1606 int count;
1607
59fc630b 1608 BUG_ON(sh->batch_head);
ac6b53b6
DW
1609 if (sh->ops.target < 0)
1610 target = sh->ops.target2;
1611 else if (sh->ops.target2 < 0)
1612 target = sh->ops.target;
91c00924 1613 else
ac6b53b6
DW
1614 /* we should only have one valid target */
1615 BUG();
1616 BUG_ON(target < 0);
1617 pr_debug("%s: stripe %llu block: %d\n",
1618 __func__, (unsigned long long)sh->sector, target);
1619
1620 tgt = &sh->dev[target];
1621 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1622 dest = tgt->page;
a7c224a8 1623 dest_off = tgt->offset;
ac6b53b6
DW
1624
1625 atomic_inc(&sh->count);
1626
1627 if (target == qd_idx) {
d69454bc 1628 count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL);
ac6b53b6
DW
1629 blocks[count] = NULL; /* regenerating p is not necessary */
1630 BUG_ON(blocks[count+1] != dest); /* q should already be set */
0403e382
DW
1631 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1632 ops_complete_compute, sh,
46d5b785 1633 to_addr_conv(sh, percpu, 0));
d69454bc 1634 tx = async_gen_syndrome(blocks, offs, count+2,
c911c46c 1635 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
ac6b53b6
DW
1636 } else {
1637 /* Compute any data- or p-drive using XOR */
1638 count = 0;
1639 for (i = disks; i-- ; ) {
1640 if (i == target || i == qd_idx)
1641 continue;
a7c224a8 1642 offs[count] = sh->dev[i].offset;
ac6b53b6
DW
1643 blocks[count++] = sh->dev[i].page;
1644 }
1645
0403e382
DW
1646 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1647 NULL, ops_complete_compute, sh,
46d5b785 1648 to_addr_conv(sh, percpu, 0));
a7c224a8 1649 tx = async_xor_offs(dest, dest_off, blocks, offs, count,
c911c46c 1650 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
ac6b53b6 1651 }
91c00924 1652
91c00924
DW
1653 return tx;
1654}
1655
ac6b53b6
DW
1656static struct dma_async_tx_descriptor *
1657ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1658{
1659 int i, count, disks = sh->disks;
1660 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1661 int d0_idx = raid6_d0(sh);
1662 int faila = -1, failb = -1;
1663 int target = sh->ops.target;
1664 int target2 = sh->ops.target2;
1665 struct r5dev *tgt = &sh->dev[target];
1666 struct r5dev *tgt2 = &sh->dev[target2];
1667 struct dma_async_tx_descriptor *tx;
46d5b785 1668 struct page **blocks = to_addr_page(percpu, 0);
a7c224a8 1669 unsigned int *offs = to_addr_offs(sh, percpu);
ac6b53b6
DW
1670 struct async_submit_ctl submit;
1671
59fc630b 1672 BUG_ON(sh->batch_head);
ac6b53b6
DW
1673 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1674 __func__, (unsigned long long)sh->sector, target, target2);
1675 BUG_ON(target < 0 || target2 < 0);
1676 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1677 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1678
6c910a78 1679 /* we need to open-code set_syndrome_sources to handle the
ac6b53b6
DW
1680 * slot number conversion for 'faila' and 'failb'
1681 */
a7c224a8
YY
1682 for (i = 0; i < disks ; i++) {
1683 offs[i] = 0;
5dd33c9a 1684 blocks[i] = NULL;
a7c224a8 1685 }
ac6b53b6
DW
1686 count = 0;
1687 i = d0_idx;
1688 do {
1689 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1690
a7c224a8 1691 offs[slot] = sh->dev[i].offset;
ac6b53b6
DW
1692 blocks[slot] = sh->dev[i].page;
1693
1694 if (i == target)
1695 faila = slot;
1696 if (i == target2)
1697 failb = slot;
1698 i = raid6_next_disk(i, disks);
1699 } while (i != d0_idx);
ac6b53b6
DW
1700
1701 BUG_ON(faila == failb);
1702 if (failb < faila)
1703 swap(faila, failb);
1704 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1705 __func__, (unsigned long long)sh->sector, faila, failb);
1706
1707 atomic_inc(&sh->count);
1708
1709 if (failb == syndrome_disks+1) {
1710 /* Q disk is one of the missing disks */
1711 if (faila == syndrome_disks) {
1712 /* Missing P+Q, just recompute */
0403e382
DW
1713 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1714 ops_complete_compute, sh,
46d5b785 1715 to_addr_conv(sh, percpu, 0));
d69454bc 1716 return async_gen_syndrome(blocks, offs, syndrome_disks+2,
c911c46c
YY
1717 RAID5_STRIPE_SIZE(sh->raid_conf),
1718 &submit);
ac6b53b6
DW
1719 } else {
1720 struct page *dest;
a7c224a8 1721 unsigned int dest_off;
ac6b53b6
DW
1722 int data_target;
1723 int qd_idx = sh->qd_idx;
1724
1725 /* Missing D+Q: recompute D from P, then recompute Q */
1726 if (target == qd_idx)
1727 data_target = target2;
1728 else
1729 data_target = target;
1730
1731 count = 0;
1732 for (i = disks; i-- ; ) {
1733 if (i == data_target || i == qd_idx)
1734 continue;
a7c224a8 1735 offs[count] = sh->dev[i].offset;
ac6b53b6
DW
1736 blocks[count++] = sh->dev[i].page;
1737 }
1738 dest = sh->dev[data_target].page;
a7c224a8 1739 dest_off = sh->dev[data_target].offset;
0403e382
DW
1740 init_async_submit(&submit,
1741 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1742 NULL, NULL, NULL,
46d5b785 1743 to_addr_conv(sh, percpu, 0));
a7c224a8 1744 tx = async_xor_offs(dest, dest_off, blocks, offs, count,
c911c46c 1745 RAID5_STRIPE_SIZE(sh->raid_conf),
ac6b53b6
DW
1746 &submit);
1747
d69454bc 1748 count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL);
0403e382
DW
1749 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1750 ops_complete_compute, sh,
46d5b785 1751 to_addr_conv(sh, percpu, 0));
d69454bc 1752 return async_gen_syndrome(blocks, offs, count+2,
c911c46c
YY
1753 RAID5_STRIPE_SIZE(sh->raid_conf),
1754 &submit);
ac6b53b6 1755 }
ac6b53b6 1756 } else {
6c910a78
DW
1757 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1758 ops_complete_compute, sh,
46d5b785 1759 to_addr_conv(sh, percpu, 0));
6c910a78
DW
1760 if (failb == syndrome_disks) {
1761 /* We're missing D+P. */
1762 return async_raid6_datap_recov(syndrome_disks+2,
c911c46c
YY
1763 RAID5_STRIPE_SIZE(sh->raid_conf),
1764 faila,
4f86ff55 1765 blocks, offs, &submit);
6c910a78
DW
1766 } else {
1767 /* We're missing D+D. */
1768 return async_raid6_2data_recov(syndrome_disks+2,
c911c46c
YY
1769 RAID5_STRIPE_SIZE(sh->raid_conf),
1770 faila, failb,
4f86ff55 1771 blocks, offs, &submit);
6c910a78 1772 }
ac6b53b6
DW
1773 }
1774}
1775
91c00924
DW
1776static void ops_complete_prexor(void *stripe_head_ref)
1777{
1778 struct stripe_head *sh = stripe_head_ref;
1779
e46b272b 1780 pr_debug("%s: stripe %llu\n", __func__,
91c00924 1781 (unsigned long long)sh->sector);
1e6d690b
SL
1782
1783 if (r5c_is_writeback(sh->raid_conf->log))
1784 /*
1785 * raid5-cache write back uses orig_page during prexor.
1786 * After prexor, it is time to free orig_page
1787 */
1788 r5c_release_extra_page(sh);
91c00924
DW
1789}
1790
1791static struct dma_async_tx_descriptor *
584acdd4
MS
1792ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1793 struct dma_async_tx_descriptor *tx)
91c00924 1794{
91c00924 1795 int disks = sh->disks;
46d5b785 1796 struct page **xor_srcs = to_addr_page(percpu, 0);
a7c224a8 1797 unsigned int *off_srcs = to_addr_offs(sh, percpu);
91c00924 1798 int count = 0, pd_idx = sh->pd_idx, i;
a08abd8c 1799 struct async_submit_ctl submit;
91c00924
DW
1800
1801 /* existing parity data subtracted */
a7c224a8 1802 unsigned int off_dest = off_srcs[count] = sh->dev[pd_idx].offset;
91c00924
DW
1803 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1804
59fc630b 1805 BUG_ON(sh->batch_head);
e46b272b 1806 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1807 (unsigned long long)sh->sector);
1808
1809 for (i = disks; i--; ) {
1810 struct r5dev *dev = &sh->dev[i];
1811 /* Only process blocks that are known to be uptodate */
a7c224a8
YY
1812 if (test_bit(R5_InJournal, &dev->flags)) {
1813 /*
1814 * For this case, PAGE_SIZE must be equal to 4KB and
1815 * page offset is zero.
1816 */
1817 off_srcs[count] = dev->offset;
1e6d690b 1818 xor_srcs[count++] = dev->orig_page;
a7c224a8
YY
1819 } else if (test_bit(R5_Wantdrain, &dev->flags)) {
1820 off_srcs[count] = dev->offset;
91c00924 1821 xor_srcs[count++] = dev->page;
a7c224a8 1822 }
91c00924
DW
1823 }
1824
0403e382 1825 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
46d5b785 1826 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
a7c224a8 1827 tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
c911c46c 1828 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
91c00924
DW
1829
1830 return tx;
1831}
1832
584acdd4
MS
1833static struct dma_async_tx_descriptor *
1834ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1835 struct dma_async_tx_descriptor *tx)
1836{
1837 struct page **blocks = to_addr_page(percpu, 0);
d69454bc 1838 unsigned int *offs = to_addr_offs(sh, percpu);
584acdd4
MS
1839 int count;
1840 struct async_submit_ctl submit;
1841
1842 pr_debug("%s: stripe %llu\n", __func__,
1843 (unsigned long long)sh->sector);
1844
d69454bc 1845 count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_WANT_DRAIN);
584acdd4
MS
1846
1847 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1848 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
d69454bc 1849 tx = async_gen_syndrome(blocks, offs, count+2,
c911c46c 1850 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
584acdd4
MS
1851
1852 return tx;
1853}
1854
91c00924 1855static struct dma_async_tx_descriptor *
d8ee0728 1856ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
91c00924 1857{
1e6d690b 1858 struct r5conf *conf = sh->raid_conf;
91c00924 1859 int disks = sh->disks;
d8ee0728 1860 int i;
59fc630b 1861 struct stripe_head *head_sh = sh;
91c00924 1862
e46b272b 1863 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1864 (unsigned long long)sh->sector);
1865
1866 for (i = disks; i--; ) {
59fc630b 1867 struct r5dev *dev;
91c00924 1868 struct bio *chosen;
91c00924 1869
59fc630b 1870 sh = head_sh;
1871 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
91c00924
DW
1872 struct bio *wbi;
1873
59fc630b 1874again:
1875 dev = &sh->dev[i];
1e6d690b
SL
1876 /*
1877 * clear R5_InJournal, so when rewriting a page in
1878 * journal, it is not skipped by r5l_log_stripe()
1879 */
1880 clear_bit(R5_InJournal, &dev->flags);
b17459c0 1881 spin_lock_irq(&sh->stripe_lock);
91c00924
DW
1882 chosen = dev->towrite;
1883 dev->towrite = NULL;
7a87f434 1884 sh->overwrite_disks = 0;
91c00924
DW
1885 BUG_ON(dev->written);
1886 wbi = dev->written = chosen;
b17459c0 1887 spin_unlock_irq(&sh->stripe_lock);
d592a996 1888 WARN_ON(dev->page != dev->orig_page);
91c00924 1889
4f024f37 1890 while (wbi && wbi->bi_iter.bi_sector <
c911c46c 1891 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
1eff9d32 1892 if (wbi->bi_opf & REQ_FUA)
e9c7469b 1893 set_bit(R5_WantFUA, &dev->flags);
1eff9d32 1894 if (wbi->bi_opf & REQ_SYNC)
bc0934f0 1895 set_bit(R5_SyncIO, &dev->flags);
796a5cf0 1896 if (bio_op(wbi) == REQ_OP_DISCARD)
620125f2 1897 set_bit(R5_Discard, &dev->flags);
d592a996
SL
1898 else {
1899 tx = async_copy_data(1, wbi, &dev->page,
248728dd 1900 dev->offset,
1e6d690b
SL
1901 dev->sector, tx, sh,
1902 r5c_is_writeback(conf->log));
1903 if (dev->page != dev->orig_page &&
1904 !r5c_is_writeback(conf->log)) {
d592a996
SL
1905 set_bit(R5_SkipCopy, &dev->flags);
1906 clear_bit(R5_UPTODATE, &dev->flags);
1907 clear_bit(R5_OVERWRITE, &dev->flags);
1908 }
1909 }
c911c46c 1910 wbi = r5_next_bio(conf, wbi, dev->sector);
91c00924 1911 }
59fc630b 1912
1913 if (head_sh->batch_head) {
1914 sh = list_first_entry(&sh->batch_list,
1915 struct stripe_head,
1916 batch_list);
1917 if (sh == head_sh)
1918 continue;
1919 goto again;
1920 }
91c00924
DW
1921 }
1922 }
1923
1924 return tx;
1925}
1926
ac6b53b6 1927static void ops_complete_reconstruct(void *stripe_head_ref)
91c00924
DW
1928{
1929 struct stripe_head *sh = stripe_head_ref;
ac6b53b6
DW
1930 int disks = sh->disks;
1931 int pd_idx = sh->pd_idx;
1932 int qd_idx = sh->qd_idx;
1933 int i;
9e444768 1934 bool fua = false, sync = false, discard = false;
91c00924 1935
e46b272b 1936 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1937 (unsigned long long)sh->sector);
1938
bc0934f0 1939 for (i = disks; i--; ) {
e9c7469b 1940 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
bc0934f0 1941 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
9e444768 1942 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
bc0934f0 1943 }
e9c7469b 1944
91c00924
DW
1945 for (i = disks; i--; ) {
1946 struct r5dev *dev = &sh->dev[i];
ac6b53b6 1947
e9c7469b 1948 if (dev->written || i == pd_idx || i == qd_idx) {
235b6003 1949 if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) {
9e444768 1950 set_bit(R5_UPTODATE, &dev->flags);
235b6003
N
1951 if (test_bit(STRIPE_EXPAND_READY, &sh->state))
1952 set_bit(R5_Expanded, &dev->flags);
1953 }
e9c7469b
TH
1954 if (fua)
1955 set_bit(R5_WantFUA, &dev->flags);
bc0934f0
SL
1956 if (sync)
1957 set_bit(R5_SyncIO, &dev->flags);
e9c7469b 1958 }
91c00924
DW
1959 }
1960
d8ee0728
DW
1961 if (sh->reconstruct_state == reconstruct_state_drain_run)
1962 sh->reconstruct_state = reconstruct_state_drain_result;
1963 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1964 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1965 else {
1966 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1967 sh->reconstruct_state = reconstruct_state_result;
1968 }
91c00924
DW
1969
1970 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 1971 raid5_release_stripe(sh);
91c00924
DW
1972}
1973
1974static void
ac6b53b6
DW
1975ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1976 struct dma_async_tx_descriptor *tx)
91c00924 1977{
91c00924 1978 int disks = sh->disks;
59fc630b 1979 struct page **xor_srcs;
7aba13b7 1980 unsigned int *off_srcs;
a08abd8c 1981 struct async_submit_ctl submit;
59fc630b 1982 int count, pd_idx = sh->pd_idx, i;
91c00924 1983 struct page *xor_dest;
7aba13b7 1984 unsigned int off_dest;
d8ee0728 1985 int prexor = 0;
91c00924 1986 unsigned long flags;
59fc630b 1987 int j = 0;
1988 struct stripe_head *head_sh = sh;
1989 int last_stripe;
91c00924 1990
e46b272b 1991 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1992 (unsigned long long)sh->sector);
1993
620125f2
SL
1994 for (i = 0; i < sh->disks; i++) {
1995 if (pd_idx == i)
1996 continue;
1997 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1998 break;
1999 }
2000 if (i >= sh->disks) {
2001 atomic_inc(&sh->count);
620125f2
SL
2002 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
2003 ops_complete_reconstruct(sh);
2004 return;
2005 }
59fc630b 2006again:
2007 count = 0;
2008 xor_srcs = to_addr_page(percpu, j);
7aba13b7 2009 off_srcs = to_addr_offs(sh, percpu);
91c00924
DW
2010 /* check if prexor is active which means only process blocks
2011 * that are part of a read-modify-write (written)
2012 */
59fc630b 2013 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
d8ee0728 2014 prexor = 1;
7aba13b7 2015 off_dest = off_srcs[count] = sh->dev[pd_idx].offset;
91c00924
DW
2016 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
2017 for (i = disks; i--; ) {
2018 struct r5dev *dev = &sh->dev[i];
1e6d690b 2019 if (head_sh->dev[i].written ||
7aba13b7
YY
2020 test_bit(R5_InJournal, &head_sh->dev[i].flags)) {
2021 off_srcs[count] = dev->offset;
91c00924 2022 xor_srcs[count++] = dev->page;
7aba13b7 2023 }
91c00924
DW
2024 }
2025 } else {
2026 xor_dest = sh->dev[pd_idx].page;
7aba13b7 2027 off_dest = sh->dev[pd_idx].offset;
91c00924
DW
2028 for (i = disks; i--; ) {
2029 struct r5dev *dev = &sh->dev[i];
7aba13b7
YY
2030 if (i != pd_idx) {
2031 off_srcs[count] = dev->offset;
91c00924 2032 xor_srcs[count++] = dev->page;
7aba13b7 2033 }
91c00924
DW
2034 }
2035 }
2036
91c00924
DW
2037 /* 1/ if we prexor'd then the dest is reused as a source
2038 * 2/ if we did not prexor then we are redoing the parity
2039 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
2040 * for the synchronous xor case
2041 */
59fc630b 2042 last_stripe = !head_sh->batch_head ||
2043 list_first_entry(&sh->batch_list,
2044 struct stripe_head, batch_list) == head_sh;
2045 if (last_stripe) {
2046 flags = ASYNC_TX_ACK |
2047 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
2048
2049 atomic_inc(&head_sh->count);
2050 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
2051 to_addr_conv(sh, percpu, j));
2052 } else {
2053 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
2054 init_async_submit(&submit, flags, tx, NULL, NULL,
2055 to_addr_conv(sh, percpu, j));
2056 }
91c00924 2057
a08abd8c 2058 if (unlikely(count == 1))
7aba13b7 2059 tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0],
c911c46c 2060 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
a08abd8c 2061 else
a7c224a8 2062 tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
c911c46c 2063 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
59fc630b 2064 if (!last_stripe) {
2065 j++;
2066 sh = list_first_entry(&sh->batch_list, struct stripe_head,
2067 batch_list);
2068 goto again;
2069 }
91c00924
DW
2070}
2071
ac6b53b6
DW
2072static void
2073ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
2074 struct dma_async_tx_descriptor *tx)
2075{
2076 struct async_submit_ctl submit;
59fc630b 2077 struct page **blocks;
d69454bc 2078 unsigned int *offs;
59fc630b 2079 int count, i, j = 0;
2080 struct stripe_head *head_sh = sh;
2081 int last_stripe;
584acdd4
MS
2082 int synflags;
2083 unsigned long txflags;
ac6b53b6
DW
2084
2085 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
2086
620125f2
SL
2087 for (i = 0; i < sh->disks; i++) {
2088 if (sh->pd_idx == i || sh->qd_idx == i)
2089 continue;
2090 if (!test_bit(R5_Discard, &sh->dev[i].flags))
2091 break;
2092 }
2093 if (i >= sh->disks) {
2094 atomic_inc(&sh->count);
620125f2
SL
2095 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
2096 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
2097 ops_complete_reconstruct(sh);
2098 return;
2099 }
2100
59fc630b 2101again:
2102 blocks = to_addr_page(percpu, j);
d69454bc 2103 offs = to_addr_offs(sh, percpu);
584acdd4
MS
2104
2105 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
2106 synflags = SYNDROME_SRC_WRITTEN;
2107 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
2108 } else {
2109 synflags = SYNDROME_SRC_ALL;
2110 txflags = ASYNC_TX_ACK;
2111 }
2112
d69454bc 2113 count = set_syndrome_sources(blocks, offs, sh, synflags);
59fc630b 2114 last_stripe = !head_sh->batch_head ||
2115 list_first_entry(&sh->batch_list,
2116 struct stripe_head, batch_list) == head_sh;
2117
2118 if (last_stripe) {
2119 atomic_inc(&head_sh->count);
584acdd4 2120 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
59fc630b 2121 head_sh, to_addr_conv(sh, percpu, j));
2122 } else
2123 init_async_submit(&submit, 0, tx, NULL, NULL,
2124 to_addr_conv(sh, percpu, j));
d69454bc 2125 tx = async_gen_syndrome(blocks, offs, count+2,
c911c46c 2126 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
59fc630b 2127 if (!last_stripe) {
2128 j++;
2129 sh = list_first_entry(&sh->batch_list, struct stripe_head,
2130 batch_list);
2131 goto again;
2132 }
91c00924
DW
2133}
2134
2135static void ops_complete_check(void *stripe_head_ref)
2136{
2137 struct stripe_head *sh = stripe_head_ref;
91c00924 2138
e46b272b 2139 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
2140 (unsigned long long)sh->sector);
2141
ecc65c9b 2142 sh->check_state = check_state_check_result;
91c00924 2143 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 2144 raid5_release_stripe(sh);
91c00924
DW
2145}
2146
ac6b53b6 2147static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
91c00924 2148{
91c00924 2149 int disks = sh->disks;
ac6b53b6
DW
2150 int pd_idx = sh->pd_idx;
2151 int qd_idx = sh->qd_idx;
2152 struct page *xor_dest;
a7c224a8 2153 unsigned int off_dest;
46d5b785 2154 struct page **xor_srcs = to_addr_page(percpu, 0);
a7c224a8 2155 unsigned int *off_srcs = to_addr_offs(sh, percpu);
91c00924 2156 struct dma_async_tx_descriptor *tx;
a08abd8c 2157 struct async_submit_ctl submit;
ac6b53b6
DW
2158 int count;
2159 int i;
91c00924 2160
e46b272b 2161 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
2162 (unsigned long long)sh->sector);
2163
59fc630b 2164 BUG_ON(sh->batch_head);
ac6b53b6
DW
2165 count = 0;
2166 xor_dest = sh->dev[pd_idx].page;
a7c224a8
YY
2167 off_dest = sh->dev[pd_idx].offset;
2168 off_srcs[count] = off_dest;
ac6b53b6 2169 xor_srcs[count++] = xor_dest;
91c00924 2170 for (i = disks; i--; ) {
ac6b53b6
DW
2171 if (i == pd_idx || i == qd_idx)
2172 continue;
a7c224a8 2173 off_srcs[count] = sh->dev[i].offset;
ac6b53b6 2174 xor_srcs[count++] = sh->dev[i].page;
91c00924
DW
2175 }
2176
d6f38f31 2177 init_async_submit(&submit, 0, NULL, NULL, NULL,
46d5b785 2178 to_addr_conv(sh, percpu, 0));
a7c224a8 2179 tx = async_xor_val_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
c911c46c 2180 RAID5_STRIPE_SIZE(sh->raid_conf),
a08abd8c 2181 &sh->ops.zero_sum_result, &submit);
91c00924 2182
91c00924 2183 atomic_inc(&sh->count);
a08abd8c
DW
2184 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
2185 tx = async_trigger_callback(&submit);
91c00924
DW
2186}
2187
ac6b53b6
DW
2188static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
2189{
46d5b785 2190 struct page **srcs = to_addr_page(percpu, 0);
d69454bc 2191 unsigned int *offs = to_addr_offs(sh, percpu);
ac6b53b6
DW
2192 struct async_submit_ctl submit;
2193 int count;
2194
2195 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
2196 (unsigned long long)sh->sector, checkp);
2197
59fc630b 2198 BUG_ON(sh->batch_head);
d69454bc 2199 count = set_syndrome_sources(srcs, offs, sh, SYNDROME_SRC_ALL);
ac6b53b6
DW
2200 if (!checkp)
2201 srcs[count] = NULL;
91c00924 2202
91c00924 2203 atomic_inc(&sh->count);
ac6b53b6 2204 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
46d5b785 2205 sh, to_addr_conv(sh, percpu, 0));
d69454bc 2206 async_syndrome_val(srcs, offs, count+2,
c911c46c 2207 RAID5_STRIPE_SIZE(sh->raid_conf),
d69454bc 2208 &sh->ops.zero_sum_result, percpu->spare_page, 0, &submit);
91c00924
DW
2209}
2210
51acbcec 2211static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
91c00924
DW
2212{
2213 int overlap_clear = 0, i, disks = sh->disks;
2214 struct dma_async_tx_descriptor *tx = NULL;
d1688a6d 2215 struct r5conf *conf = sh->raid_conf;
ac6b53b6 2216 int level = conf->level;
d6f38f31
DW
2217 struct raid5_percpu *percpu;
2218 unsigned long cpu;
91c00924 2219
d6f38f31
DW
2220 cpu = get_cpu();
2221 percpu = per_cpu_ptr(conf->percpu, cpu);
83de75cc 2222 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
91c00924
DW
2223 ops_run_biofill(sh);
2224 overlap_clear++;
2225 }
2226
7b3a871e 2227 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
ac6b53b6
DW
2228 if (level < 6)
2229 tx = ops_run_compute5(sh, percpu);
2230 else {
2231 if (sh->ops.target2 < 0 || sh->ops.target < 0)
2232 tx = ops_run_compute6_1(sh, percpu);
2233 else
2234 tx = ops_run_compute6_2(sh, percpu);
2235 }
2236 /* terminate the chain if reconstruct is not set to be run */
2237 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
7b3a871e
DW
2238 async_tx_ack(tx);
2239 }
91c00924 2240
584acdd4
MS
2241 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
2242 if (level < 6)
2243 tx = ops_run_prexor5(sh, percpu, tx);
2244 else
2245 tx = ops_run_prexor6(sh, percpu, tx);
2246 }
91c00924 2247
ae1713e2
AP
2248 if (test_bit(STRIPE_OP_PARTIAL_PARITY, &ops_request))
2249 tx = ops_run_partial_parity(sh, percpu, tx);
2250
600aa109 2251 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
d8ee0728 2252 tx = ops_run_biodrain(sh, tx);
91c00924
DW
2253 overlap_clear++;
2254 }
2255
ac6b53b6
DW
2256 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
2257 if (level < 6)
2258 ops_run_reconstruct5(sh, percpu, tx);
2259 else
2260 ops_run_reconstruct6(sh, percpu, tx);
2261 }
91c00924 2262
ac6b53b6
DW
2263 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
2264 if (sh->check_state == check_state_run)
2265 ops_run_check_p(sh, percpu);
2266 else if (sh->check_state == check_state_run_q)
2267 ops_run_check_pq(sh, percpu, 0);
2268 else if (sh->check_state == check_state_run_pq)
2269 ops_run_check_pq(sh, percpu, 1);
2270 else
2271 BUG();
2272 }
91c00924 2273
59fc630b 2274 if (overlap_clear && !sh->batch_head)
91c00924
DW
2275 for (i = disks; i--; ) {
2276 struct r5dev *dev = &sh->dev[i];
2277 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2278 wake_up(&sh->raid_conf->wait_for_overlap);
2279 }
d6f38f31 2280 put_cpu();
91c00924
DW
2281}
2282
845b9e22
AP
2283static void free_stripe(struct kmem_cache *sc, struct stripe_head *sh)
2284{
046169f0
YY
2285#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2286 kfree(sh->pages);
2287#endif
845b9e22
AP
2288 if (sh->ppl_page)
2289 __free_page(sh->ppl_page);
2290 kmem_cache_free(sc, sh);
2291}
2292
5f9d1fde 2293static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
845b9e22 2294 int disks, struct r5conf *conf)
f18c1a35
N
2295{
2296 struct stripe_head *sh;
5f9d1fde 2297 int i;
f18c1a35
N
2298
2299 sh = kmem_cache_zalloc(sc, gfp);
2300 if (sh) {
2301 spin_lock_init(&sh->stripe_lock);
2302 spin_lock_init(&sh->batch_lock);
2303 INIT_LIST_HEAD(&sh->batch_list);
2304 INIT_LIST_HEAD(&sh->lru);
a39f7afd 2305 INIT_LIST_HEAD(&sh->r5c);
d7bd398e 2306 INIT_LIST_HEAD(&sh->log_list);
f18c1a35 2307 atomic_set(&sh->count, 1);
845b9e22 2308 sh->raid_conf = conf;
a39f7afd 2309 sh->log_start = MaxSector;
5f9d1fde
SL
2310 for (i = 0; i < disks; i++) {
2311 struct r5dev *dev = &sh->dev[i];
2312
3a83f467
ML
2313 bio_init(&dev->req, &dev->vec, 1);
2314 bio_init(&dev->rreq, &dev->rvec, 1);
5f9d1fde 2315 }
845b9e22
AP
2316
2317 if (raid5_has_ppl(conf)) {
2318 sh->ppl_page = alloc_page(gfp);
2319 if (!sh->ppl_page) {
2320 free_stripe(sc, sh);
046169f0 2321 return NULL;
845b9e22
AP
2322 }
2323 }
046169f0
YY
2324#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2325 if (init_stripe_shared_pages(sh, conf, disks)) {
2326 free_stripe(sc, sh);
2327 return NULL;
2328 }
2329#endif
f18c1a35
N
2330 }
2331 return sh;
2332}
486f0644 2333static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
1da177e4
LT
2334{
2335 struct stripe_head *sh;
f18c1a35 2336
845b9e22 2337 sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size, conf);
3f294f4f
N
2338 if (!sh)
2339 return 0;
6ce32846 2340
a9683a79 2341 if (grow_buffers(sh, gfp)) {
e4e11e38 2342 shrink_buffers(sh);
845b9e22 2343 free_stripe(conf->slab_cache, sh);
3f294f4f
N
2344 return 0;
2345 }
486f0644
N
2346 sh->hash_lock_index =
2347 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
3f294f4f 2348 /* we just created an active stripe so... */
3f294f4f 2349 atomic_inc(&conf->active_stripes);
59fc630b 2350
6d036f7d 2351 raid5_release_stripe(sh);
486f0644 2352 conf->max_nr_stripes++;
3f294f4f
N
2353 return 1;
2354}
2355
d1688a6d 2356static int grow_stripes(struct r5conf *conf, int num)
3f294f4f 2357{
e18b890b 2358 struct kmem_cache *sc;
53b8d89d 2359 size_t namelen = sizeof(conf->cache_name[0]);
5e5e3e78 2360 int devs = max(conf->raid_disks, conf->previous_raid_disks);
1da177e4 2361
f4be6b43 2362 if (conf->mddev->gendisk)
53b8d89d 2363 snprintf(conf->cache_name[0], namelen,
f4be6b43
N
2364 "raid%d-%s", conf->level, mdname(conf->mddev));
2365 else
53b8d89d 2366 snprintf(conf->cache_name[0], namelen,
f4be6b43 2367 "raid%d-%p", conf->level, conf->mddev);
53b8d89d 2368 snprintf(conf->cache_name[1], namelen, "%.27s-alt", conf->cache_name[0]);
f4be6b43 2369
ad01c9e3
N
2370 conf->active_name = 0;
2371 sc = kmem_cache_create(conf->cache_name[conf->active_name],
1da177e4 2372 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
20c2df83 2373 0, 0, NULL);
1da177e4
LT
2374 if (!sc)
2375 return 1;
2376 conf->slab_cache = sc;
ad01c9e3 2377 conf->pool_size = devs;
486f0644
N
2378 while (num--)
2379 if (!grow_one_stripe(conf, GFP_KERNEL))
1da177e4 2380 return 1;
486f0644 2381
1da177e4
LT
2382 return 0;
2383}
29269553 2384
d6f38f31 2385/**
7f8a30e5
CL
2386 * scribble_alloc - allocate percpu scribble buffer for required size
2387 * of the scribble region
2aada5b1
DLM
2388 * @percpu: from for_each_present_cpu() of the caller
2389 * @num: total number of disks in the array
2390 * @cnt: scribble objs count for required size of the scribble region
d6f38f31 2391 *
7f8a30e5 2392 * The scribble buffer size must be enough to contain:
d6f38f31
DW
2393 * 1/ a struct page pointer for each device in the array +2
2394 * 2/ room to convert each entry in (1) to its corresponding dma
2395 * (dma_map_page()) or page (page_address()) address.
2396 *
2397 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2398 * calculate over all devices (not just the data blocks), using zeros in place
2399 * of the P and Q blocks.
2400 */
b330e6a4 2401static int scribble_alloc(struct raid5_percpu *percpu,
ba54d4d4 2402 int num, int cnt)
d6f38f31 2403{
b330e6a4 2404 size_t obj_size =
7aba13b7
YY
2405 sizeof(struct page *) * (num + 2) +
2406 sizeof(addr_conv_t) * (num + 2) +
2407 sizeof(unsigned int) * (num + 2);
b330e6a4 2408 void *scribble;
d6f38f31 2409
ba54d4d4
CL
2410 /*
2411 * If here is in raid array suspend context, it is in memalloc noio
2412 * context as well, there is no potential recursive memory reclaim
2413 * I/Os with the GFP_KERNEL flag.
2414 */
2415 scribble = kvmalloc_array(cnt, obj_size, GFP_KERNEL);
b330e6a4
KO
2416 if (!scribble)
2417 return -ENOMEM;
2418
2419 kvfree(percpu->scribble);
2420
2421 percpu->scribble = scribble;
2422 percpu->scribble_obj_size = obj_size;
2423 return 0;
d6f38f31
DW
2424}
2425
738a2738
N
2426static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2427{
2428 unsigned long cpu;
2429 int err = 0;
2430
27a353c0
SL
2431 /*
2432 * Never shrink. And mddev_suspend() could deadlock if this is called
2433 * from raid5d. In that case, scribble_disks and scribble_sectors
2434 * should equal to new_disks and new_sectors
2435 */
2436 if (conf->scribble_disks >= new_disks &&
2437 conf->scribble_sectors >= new_sectors)
2438 return 0;
738a2738 2439 mddev_suspend(conf->mddev);
252034e0 2440 cpus_read_lock();
b330e6a4 2441
738a2738
N
2442 for_each_present_cpu(cpu) {
2443 struct raid5_percpu *percpu;
738a2738
N
2444
2445 percpu = per_cpu_ptr(conf->percpu, cpu);
b330e6a4 2446 err = scribble_alloc(percpu, new_disks,
c911c46c 2447 new_sectors / RAID5_STRIPE_SECTORS(conf));
b330e6a4 2448 if (err)
738a2738 2449 break;
738a2738 2450 }
b330e6a4 2451
252034e0 2452 cpus_read_unlock();
738a2738 2453 mddev_resume(conf->mddev);
27a353c0
SL
2454 if (!err) {
2455 conf->scribble_disks = new_disks;
2456 conf->scribble_sectors = new_sectors;
2457 }
738a2738
N
2458 return err;
2459}
2460
d1688a6d 2461static int resize_stripes(struct r5conf *conf, int newsize)
ad01c9e3
N
2462{
2463 /* Make all the stripes able to hold 'newsize' devices.
2464 * New slots in each stripe get 'page' set to a new page.
2465 *
2466 * This happens in stages:
2467 * 1/ create a new kmem_cache and allocate the required number of
2468 * stripe_heads.
83f0d77a 2469 * 2/ gather all the old stripe_heads and transfer the pages across
ad01c9e3
N
2470 * to the new stripe_heads. This will have the side effect of
2471 * freezing the array as once all stripe_heads have been collected,
2472 * no IO will be possible. Old stripe heads are freed once their
2473 * pages have been transferred over, and the old kmem_cache is
2474 * freed when all stripes are done.
2475 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
3560741e 2476 * we simple return a failure status - no need to clean anything up.
ad01c9e3
N
2477 * 4/ allocate new pages for the new slots in the new stripe_heads.
2478 * If this fails, we don't bother trying the shrink the
2479 * stripe_heads down again, we just leave them as they are.
2480 * As each stripe_head is processed the new one is released into
2481 * active service.
2482 *
2483 * Once step2 is started, we cannot afford to wait for a write,
2484 * so we use GFP_NOIO allocations.
2485 */
2486 struct stripe_head *osh, *nsh;
2487 LIST_HEAD(newstripes);
2488 struct disk_info *ndisks;
2214c260 2489 int err = 0;
e18b890b 2490 struct kmem_cache *sc;
ad01c9e3 2491 int i;
566c09c5 2492 int hash, cnt;
ad01c9e3 2493
2214c260 2494 md_allow_write(conf->mddev);
2a2275d6 2495
ad01c9e3
N
2496 /* Step 1 */
2497 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2498 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
20c2df83 2499 0, 0, NULL);
ad01c9e3
N
2500 if (!sc)
2501 return -ENOMEM;
2502
2d5b569b
N
2503 /* Need to ensure auto-resizing doesn't interfere */
2504 mutex_lock(&conf->cache_size_mutex);
2505
ad01c9e3 2506 for (i = conf->max_nr_stripes; i; i--) {
845b9e22 2507 nsh = alloc_stripe(sc, GFP_KERNEL, newsize, conf);
ad01c9e3
N
2508 if (!nsh)
2509 break;
2510
ad01c9e3
N
2511 list_add(&nsh->lru, &newstripes);
2512 }
2513 if (i) {
2514 /* didn't get enough, give up */
2515 while (!list_empty(&newstripes)) {
2516 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2517 list_del(&nsh->lru);
845b9e22 2518 free_stripe(sc, nsh);
ad01c9e3
N
2519 }
2520 kmem_cache_destroy(sc);
2d5b569b 2521 mutex_unlock(&conf->cache_size_mutex);
ad01c9e3
N
2522 return -ENOMEM;
2523 }
2524 /* Step 2 - Must use GFP_NOIO now.
2525 * OK, we have enough stripes, start collecting inactive
2526 * stripes and copying them over
2527 */
566c09c5
SL
2528 hash = 0;
2529 cnt = 0;
ad01c9e3 2530 list_for_each_entry(nsh, &newstripes, lru) {
566c09c5 2531 lock_device_hash_lock(conf, hash);
6ab2a4b8 2532 wait_event_cmd(conf->wait_for_stripe,
566c09c5
SL
2533 !list_empty(conf->inactive_list + hash),
2534 unlock_device_hash_lock(conf, hash),
2535 lock_device_hash_lock(conf, hash));
2536 osh = get_free_stripe(conf, hash);
2537 unlock_device_hash_lock(conf, hash);
f18c1a35 2538
f16acaf3
YY
2539#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2540 for (i = 0; i < osh->nr_pages; i++) {
2541 nsh->pages[i] = osh->pages[i];
2542 osh->pages[i] = NULL;
2543 }
2544#endif
d592a996 2545 for(i=0; i<conf->pool_size; i++) {
ad01c9e3 2546 nsh->dev[i].page = osh->dev[i].page;
d592a996 2547 nsh->dev[i].orig_page = osh->dev[i].page;
7aba13b7 2548 nsh->dev[i].offset = osh->dev[i].offset;
d592a996 2549 }
566c09c5 2550 nsh->hash_lock_index = hash;
845b9e22 2551 free_stripe(conf->slab_cache, osh);
566c09c5
SL
2552 cnt++;
2553 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2554 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2555 hash++;
2556 cnt = 0;
2557 }
ad01c9e3
N
2558 }
2559 kmem_cache_destroy(conf->slab_cache);
2560
2561 /* Step 3.
2562 * At this point, we are holding all the stripes so the array
2563 * is completely stalled, so now is a good time to resize
d6f38f31 2564 * conf->disks and the scribble region
ad01c9e3 2565 */
6396bb22 2566 ndisks = kcalloc(newsize, sizeof(struct disk_info), GFP_NOIO);
ad01c9e3 2567 if (ndisks) {
d7bd398e 2568 for (i = 0; i < conf->pool_size; i++)
ad01c9e3 2569 ndisks[i] = conf->disks[i];
d7bd398e
SL
2570
2571 for (i = conf->pool_size; i < newsize; i++) {
2572 ndisks[i].extra_page = alloc_page(GFP_NOIO);
2573 if (!ndisks[i].extra_page)
2574 err = -ENOMEM;
2575 }
2576
2577 if (err) {
2578 for (i = conf->pool_size; i < newsize; i++)
2579 if (ndisks[i].extra_page)
2580 put_page(ndisks[i].extra_page);
2581 kfree(ndisks);
2582 } else {
2583 kfree(conf->disks);
2584 conf->disks = ndisks;
2585 }
ad01c9e3
N
2586 } else
2587 err = -ENOMEM;
2588
583da48e
DY
2589 conf->slab_cache = sc;
2590 conf->active_name = 1-conf->active_name;
2591
ad01c9e3
N
2592 /* Step 4, return new stripes to service */
2593 while(!list_empty(&newstripes)) {
2594 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2595 list_del_init(&nsh->lru);
d6f38f31 2596
f16acaf3
YY
2597#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2598 for (i = 0; i < nsh->nr_pages; i++) {
2599 if (nsh->pages[i])
2600 continue;
2601 nsh->pages[i] = alloc_page(GFP_NOIO);
2602 if (!nsh->pages[i])
2603 err = -ENOMEM;
2604 }
2605
2606 for (i = conf->raid_disks; i < newsize; i++) {
2607 if (nsh->dev[i].page)
2608 continue;
2609 nsh->dev[i].page = raid5_get_dev_page(nsh, i);
2610 nsh->dev[i].orig_page = nsh->dev[i].page;
2611 nsh->dev[i].offset = raid5_get_page_offset(nsh, i);
2612 }
2613#else
ad01c9e3
N
2614 for (i=conf->raid_disks; i < newsize; i++)
2615 if (nsh->dev[i].page == NULL) {
2616 struct page *p = alloc_page(GFP_NOIO);
2617 nsh->dev[i].page = p;
d592a996 2618 nsh->dev[i].orig_page = p;
7aba13b7 2619 nsh->dev[i].offset = 0;
ad01c9e3
N
2620 if (!p)
2621 err = -ENOMEM;
2622 }
f16acaf3 2623#endif
6d036f7d 2624 raid5_release_stripe(nsh);
ad01c9e3
N
2625 }
2626 /* critical section pass, GFP_NOIO no longer needed */
2627
6e9eac2d
N
2628 if (!err)
2629 conf->pool_size = newsize;
b44c018c
SL
2630 mutex_unlock(&conf->cache_size_mutex);
2631
ad01c9e3
N
2632 return err;
2633}
1da177e4 2634
486f0644 2635static int drop_one_stripe(struct r5conf *conf)
1da177e4
LT
2636{
2637 struct stripe_head *sh;
49895bcc 2638 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
1da177e4 2639
566c09c5
SL
2640 spin_lock_irq(conf->hash_locks + hash);
2641 sh = get_free_stripe(conf, hash);
2642 spin_unlock_irq(conf->hash_locks + hash);
3f294f4f
N
2643 if (!sh)
2644 return 0;
78bafebd 2645 BUG_ON(atomic_read(&sh->count));
e4e11e38 2646 shrink_buffers(sh);
845b9e22 2647 free_stripe(conf->slab_cache, sh);
3f294f4f 2648 atomic_dec(&conf->active_stripes);
486f0644 2649 conf->max_nr_stripes--;
3f294f4f
N
2650 return 1;
2651}
2652
d1688a6d 2653static void shrink_stripes(struct r5conf *conf)
3f294f4f 2654{
486f0644
N
2655 while (conf->max_nr_stripes &&
2656 drop_one_stripe(conf))
2657 ;
3f294f4f 2658
644df1a8 2659 kmem_cache_destroy(conf->slab_cache);
1da177e4
LT
2660 conf->slab_cache = NULL;
2661}
2662
4246a0b6 2663static void raid5_end_read_request(struct bio * bi)
1da177e4 2664{
99c0fb5f 2665 struct stripe_head *sh = bi->bi_private;
d1688a6d 2666 struct r5conf *conf = sh->raid_conf;
7ecaa1e6 2667 int disks = sh->disks, i;
d6950432 2668 char b[BDEVNAME_SIZE];
dd054fce 2669 struct md_rdev *rdev = NULL;
05616be5 2670 sector_t s;
1da177e4
LT
2671
2672 for (i=0 ; i<disks; i++)
2673 if (bi == &sh->dev[i].req)
2674 break;
2675
4246a0b6 2676 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
45b4233c 2677 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
4e4cbee9 2678 bi->bi_status);
1da177e4 2679 if (i == disks) {
5f9d1fde 2680 bio_reset(bi);
1da177e4 2681 BUG();
6712ecf8 2682 return;
1da177e4 2683 }
14a75d3e 2684 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
dd054fce
N
2685 /* If replacement finished while this request was outstanding,
2686 * 'replacement' might be NULL already.
2687 * In that case it moved down to 'rdev'.
2688 * rdev is not removed until all requests are finished.
2689 */
14a75d3e 2690 rdev = conf->disks[i].replacement;
dd054fce 2691 if (!rdev)
14a75d3e 2692 rdev = conf->disks[i].rdev;
1da177e4 2693
05616be5
N
2694 if (use_new_offset(conf, sh))
2695 s = sh->sector + rdev->new_data_offset;
2696 else
2697 s = sh->sector + rdev->data_offset;
4e4cbee9 2698 if (!bi->bi_status) {
1da177e4 2699 set_bit(R5_UPTODATE, &sh->dev[i].flags);
4e5314b5 2700 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
14a75d3e
N
2701 /* Note that this cannot happen on a
2702 * replacement device. We just fail those on
2703 * any error
2704 */
cc6167b4
N
2705 pr_info_ratelimited(
2706 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
c911c46c 2707 mdname(conf->mddev), RAID5_STRIPE_SECTORS(conf),
05616be5 2708 (unsigned long long)s,
8bda470e 2709 bdevname(rdev->bdev, b));
c911c46c 2710 atomic_add(RAID5_STRIPE_SECTORS(conf), &rdev->corrected_errors);
4e5314b5
N
2711 clear_bit(R5_ReadError, &sh->dev[i].flags);
2712 clear_bit(R5_ReWrite, &sh->dev[i].flags);
3f9e7c14 2713 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2714 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2715
86aa1397
SL
2716 if (test_bit(R5_InJournal, &sh->dev[i].flags))
2717 /*
2718 * end read for a page in journal, this
2719 * must be preparing for prexor in rmw
2720 */
2721 set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
2722
14a75d3e
N
2723 if (atomic_read(&rdev->read_errors))
2724 atomic_set(&rdev->read_errors, 0);
1da177e4 2725 } else {
14a75d3e 2726 const char *bdn = bdevname(rdev->bdev, b);
ba22dcbf 2727 int retry = 0;
2e8ac303 2728 int set_bad = 0;
d6950432 2729
1da177e4 2730 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
b76b4715
NC
2731 if (!(bi->bi_status == BLK_STS_PROTECTION))
2732 atomic_inc(&rdev->read_errors);
14a75d3e 2733 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
cc6167b4
N
2734 pr_warn_ratelimited(
2735 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
14a75d3e 2736 mdname(conf->mddev),
05616be5 2737 (unsigned long long)s,
14a75d3e 2738 bdn);
2e8ac303 2739 else if (conf->mddev->degraded >= conf->max_degraded) {
2740 set_bad = 1;
cc6167b4
N
2741 pr_warn_ratelimited(
2742 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
8bda470e 2743 mdname(conf->mddev),
05616be5 2744 (unsigned long long)s,
8bda470e 2745 bdn);
2e8ac303 2746 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
4e5314b5 2747 /* Oh, no!!! */
2e8ac303 2748 set_bad = 1;
cc6167b4
N
2749 pr_warn_ratelimited(
2750 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
8bda470e 2751 mdname(conf->mddev),
05616be5 2752 (unsigned long long)s,
8bda470e 2753 bdn);
2e8ac303 2754 } else if (atomic_read(&rdev->read_errors)
0009fad0
NC
2755 > conf->max_nr_stripes) {
2756 if (!test_bit(Faulty, &rdev->flags)) {
2757 pr_warn("md/raid:%s: %d read_errors > %d stripes\n",
2758 mdname(conf->mddev),
2759 atomic_read(&rdev->read_errors),
2760 conf->max_nr_stripes);
2761 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
2762 mdname(conf->mddev), bdn);
2763 }
2764 } else
ba22dcbf 2765 retry = 1;
edfa1f65
BY
2766 if (set_bad && test_bit(In_sync, &rdev->flags)
2767 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2768 retry = 1;
ba22dcbf 2769 if (retry)
143f6e73
XN
2770 if (sh->qd_idx >= 0 && sh->pd_idx == i)
2771 set_bit(R5_ReadError, &sh->dev[i].flags);
2772 else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
3f9e7c14 2773 set_bit(R5_ReadError, &sh->dev[i].flags);
2774 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2775 } else
2776 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
ba22dcbf 2777 else {
4e5314b5
N
2778 clear_bit(R5_ReadError, &sh->dev[i].flags);
2779 clear_bit(R5_ReWrite, &sh->dev[i].flags);
2e8ac303 2780 if (!(set_bad
2781 && test_bit(In_sync, &rdev->flags)
2782 && rdev_set_badblocks(
c911c46c 2783 rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), 0)))
2e8ac303 2784 md_error(conf->mddev, rdev);
ba22dcbf 2785 }
1da177e4 2786 }
14a75d3e 2787 rdev_dec_pending(rdev, conf->mddev);
c9445555 2788 bio_reset(bi);
1da177e4
LT
2789 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2790 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 2791 raid5_release_stripe(sh);
1da177e4
LT
2792}
2793
4246a0b6 2794static void raid5_end_write_request(struct bio *bi)
1da177e4 2795{
99c0fb5f 2796 struct stripe_head *sh = bi->bi_private;
d1688a6d 2797 struct r5conf *conf = sh->raid_conf;
7ecaa1e6 2798 int disks = sh->disks, i;
3f649ab7 2799 struct md_rdev *rdev;
b84db560
N
2800 sector_t first_bad;
2801 int bad_sectors;
977df362 2802 int replacement = 0;
1da177e4 2803
977df362
N
2804 for (i = 0 ; i < disks; i++) {
2805 if (bi == &sh->dev[i].req) {
2806 rdev = conf->disks[i].rdev;
1da177e4 2807 break;
977df362
N
2808 }
2809 if (bi == &sh->dev[i].rreq) {
2810 rdev = conf->disks[i].replacement;
dd054fce
N
2811 if (rdev)
2812 replacement = 1;
2813 else
2814 /* rdev was removed and 'replacement'
2815 * replaced it. rdev is not removed
2816 * until all requests are finished.
2817 */
2818 rdev = conf->disks[i].rdev;
977df362
N
2819 break;
2820 }
2821 }
4246a0b6 2822 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
1da177e4 2823 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
4e4cbee9 2824 bi->bi_status);
1da177e4 2825 if (i == disks) {
5f9d1fde 2826 bio_reset(bi);
1da177e4 2827 BUG();
6712ecf8 2828 return;
1da177e4
LT
2829 }
2830
977df362 2831 if (replacement) {
4e4cbee9 2832 if (bi->bi_status)
977df362
N
2833 md_error(conf->mddev, rdev);
2834 else if (is_badblock(rdev, sh->sector,
c911c46c 2835 RAID5_STRIPE_SECTORS(conf),
977df362
N
2836 &first_bad, &bad_sectors))
2837 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2838 } else {
4e4cbee9 2839 if (bi->bi_status) {
9f97e4b1 2840 set_bit(STRIPE_DEGRADED, &sh->state);
977df362
N
2841 set_bit(WriteErrorSeen, &rdev->flags);
2842 set_bit(R5_WriteError, &sh->dev[i].flags);
3a6de292
N
2843 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2844 set_bit(MD_RECOVERY_NEEDED,
2845 &rdev->mddev->recovery);
977df362 2846 } else if (is_badblock(rdev, sh->sector,
c911c46c 2847 RAID5_STRIPE_SECTORS(conf),
c0b32972 2848 &first_bad, &bad_sectors)) {
977df362 2849 set_bit(R5_MadeGood, &sh->dev[i].flags);
c0b32972
N
2850 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2851 /* That was a successful write so make
2852 * sure it looks like we already did
2853 * a re-write.
2854 */
2855 set_bit(R5_ReWrite, &sh->dev[i].flags);
2856 }
977df362
N
2857 }
2858 rdev_dec_pending(rdev, conf->mddev);
1da177e4 2859
4e4cbee9 2860 if (sh->batch_head && bi->bi_status && !replacement)
72ac7330 2861 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2862
c9445555 2863 bio_reset(bi);
977df362
N
2864 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2865 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1da177e4 2866 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 2867 raid5_release_stripe(sh);
59fc630b 2868
2869 if (sh->batch_head && sh != sh->batch_head)
6d036f7d 2870 raid5_release_stripe(sh->batch_head);
1da177e4
LT
2871}
2872
849674e4 2873static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
1da177e4
LT
2874{
2875 char b[BDEVNAME_SIZE];
d1688a6d 2876 struct r5conf *conf = mddev->private;
908f4fbd 2877 unsigned long flags;
0c55e022 2878 pr_debug("raid456: error called\n");
1da177e4 2879
ba97a054
MT
2880 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n",
2881 mdname(mddev), bdevname(rdev->bdev, b));
2882
908f4fbd 2883 spin_lock_irqsave(&conf->device_lock, flags);
ba97a054
MT
2884 set_bit(Faulty, &rdev->flags);
2885 clear_bit(In_sync, &rdev->flags);
2886 mddev->degraded = raid5_calc_degraded(conf);
fb73b357 2887
ba97a054
MT
2888 if (has_failed(conf)) {
2889 set_bit(MD_BROKEN, &conf->mddev->flags);
fb73b357 2890 conf->recovery_disabled = mddev->recovery_disabled;
ba97a054
MT
2891
2892 pr_crit("md/raid:%s: Cannot continue operation (%d/%d failed).\n",
2893 mdname(mddev), mddev->degraded, conf->raid_disks);
2894 } else {
2895 pr_crit("md/raid:%s: Operation continuing on %d devices.\n",
2896 mdname(mddev), conf->raid_disks - mddev->degraded);
fb73b357
MT
2897 }
2898
908f4fbd
N
2899 spin_unlock_irqrestore(&conf->device_lock, flags);
2900 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2901
de393cde 2902 set_bit(Blocked, &rdev->flags);
2953079c
SL
2903 set_mask_bits(&mddev->sb_flags, 0,
2904 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
70d466f7 2905 r5c_update_on_rdev_error(mddev, rdev);
16a53ecc 2906}
1da177e4
LT
2907
2908/*
2909 * Input: a 'big' sector number,
2910 * Output: index of the data and parity disk, and the sector # in them.
2911 */
6d036f7d
SL
2912sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2913 int previous, int *dd_idx,
2914 struct stripe_head *sh)
1da177e4 2915{
6e3b96ed 2916 sector_t stripe, stripe2;
35f2a591 2917 sector_t chunk_number;
1da177e4 2918 unsigned int chunk_offset;
911d4ee8 2919 int pd_idx, qd_idx;
67cc2b81 2920 int ddf_layout = 0;
1da177e4 2921 sector_t new_sector;
e183eaed
N
2922 int algorithm = previous ? conf->prev_algo
2923 : conf->algorithm;
09c9e5fa
AN
2924 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2925 : conf->chunk_sectors;
112bf897
N
2926 int raid_disks = previous ? conf->previous_raid_disks
2927 : conf->raid_disks;
2928 int data_disks = raid_disks - conf->max_degraded;
1da177e4
LT
2929
2930 /* First compute the information on this sector */
2931
2932 /*
2933 * Compute the chunk number and the sector offset inside the chunk
2934 */
2935 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2936 chunk_number = r_sector;
1da177e4
LT
2937
2938 /*
2939 * Compute the stripe number
2940 */
35f2a591
N
2941 stripe = chunk_number;
2942 *dd_idx = sector_div(stripe, data_disks);
6e3b96ed 2943 stripe2 = stripe;
1da177e4
LT
2944 /*
2945 * Select the parity disk based on the user selected algorithm.
2946 */
84789554 2947 pd_idx = qd_idx = -1;
16a53ecc
N
2948 switch(conf->level) {
2949 case 4:
911d4ee8 2950 pd_idx = data_disks;
16a53ecc
N
2951 break;
2952 case 5:
e183eaed 2953 switch (algorithm) {
1da177e4 2954 case ALGORITHM_LEFT_ASYMMETRIC:
6e3b96ed 2955 pd_idx = data_disks - sector_div(stripe2, raid_disks);
911d4ee8 2956 if (*dd_idx >= pd_idx)
1da177e4
LT
2957 (*dd_idx)++;
2958 break;
2959 case ALGORITHM_RIGHT_ASYMMETRIC:
6e3b96ed 2960 pd_idx = sector_div(stripe2, raid_disks);
911d4ee8 2961 if (*dd_idx >= pd_idx)
1da177e4
LT
2962 (*dd_idx)++;
2963 break;
2964 case ALGORITHM_LEFT_SYMMETRIC:
6e3b96ed 2965 pd_idx = data_disks - sector_div(stripe2, raid_disks);
911d4ee8 2966 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
1da177e4
LT
2967 break;
2968 case ALGORITHM_RIGHT_SYMMETRIC:
6e3b96ed 2969 pd_idx = sector_div(stripe2, raid_disks);
911d4ee8 2970 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
1da177e4 2971 break;
99c0fb5f
N
2972 case ALGORITHM_PARITY_0:
2973 pd_idx = 0;
2974 (*dd_idx)++;
2975 break;
2976 case ALGORITHM_PARITY_N:
2977 pd_idx = data_disks;
2978 break;
1da177e4 2979 default:
99c0fb5f 2980 BUG();
16a53ecc
N
2981 }
2982 break;
2983 case 6:
2984
e183eaed 2985 switch (algorithm) {
16a53ecc 2986 case ALGORITHM_LEFT_ASYMMETRIC:
6e3b96ed 2987 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
911d4ee8
N
2988 qd_idx = pd_idx + 1;
2989 if (pd_idx == raid_disks-1) {
99c0fb5f 2990 (*dd_idx)++; /* Q D D D P */
911d4ee8
N
2991 qd_idx = 0;
2992 } else if (*dd_idx >= pd_idx)
16a53ecc
N
2993 (*dd_idx) += 2; /* D D P Q D */
2994 break;
2995 case ALGORITHM_RIGHT_ASYMMETRIC:
6e3b96ed 2996 pd_idx = sector_div(stripe2, raid_disks);
911d4ee8
N
2997 qd_idx = pd_idx + 1;
2998 if (pd_idx == raid_disks-1) {
99c0fb5f 2999 (*dd_idx)++; /* Q D D D P */
911d4ee8
N
3000 qd_idx = 0;
3001 } else if (*dd_idx >= pd_idx)
16a53ecc
N
3002 (*dd_idx) += 2; /* D D P Q D */
3003 break;
3004 case ALGORITHM_LEFT_SYMMETRIC:
6e3b96ed 3005 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
911d4ee8
N
3006 qd_idx = (pd_idx + 1) % raid_disks;
3007 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
16a53ecc
N
3008 break;
3009 case ALGORITHM_RIGHT_SYMMETRIC:
6e3b96ed 3010 pd_idx = sector_div(stripe2, raid_disks);
911d4ee8
N
3011 qd_idx = (pd_idx + 1) % raid_disks;
3012 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
16a53ecc 3013 break;
99c0fb5f
N
3014
3015 case ALGORITHM_PARITY_0:
3016 pd_idx = 0;
3017 qd_idx = 1;
3018 (*dd_idx) += 2;
3019 break;
3020 case ALGORITHM_PARITY_N:
3021 pd_idx = data_disks;
3022 qd_idx = data_disks + 1;
3023 break;
3024
3025 case ALGORITHM_ROTATING_ZERO_RESTART:
3026 /* Exactly the same as RIGHT_ASYMMETRIC, but or
3027 * of blocks for computing Q is different.
3028 */
6e3b96ed 3029 pd_idx = sector_div(stripe2, raid_disks);
99c0fb5f
N
3030 qd_idx = pd_idx + 1;
3031 if (pd_idx == raid_disks-1) {
3032 (*dd_idx)++; /* Q D D D P */
3033 qd_idx = 0;
3034 } else if (*dd_idx >= pd_idx)
3035 (*dd_idx) += 2; /* D D P Q D */
67cc2b81 3036 ddf_layout = 1;
99c0fb5f
N
3037 break;
3038
3039 case ALGORITHM_ROTATING_N_RESTART:
3040 /* Same a left_asymmetric, by first stripe is
3041 * D D D P Q rather than
3042 * Q D D D P
3043 */
6e3b96ed
N
3044 stripe2 += 1;
3045 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
99c0fb5f
N
3046 qd_idx = pd_idx + 1;
3047 if (pd_idx == raid_disks-1) {
3048 (*dd_idx)++; /* Q D D D P */
3049 qd_idx = 0;
3050 } else if (*dd_idx >= pd_idx)
3051 (*dd_idx) += 2; /* D D P Q D */
67cc2b81 3052 ddf_layout = 1;
99c0fb5f
N
3053 break;
3054
3055 case ALGORITHM_ROTATING_N_CONTINUE:
3056 /* Same as left_symmetric but Q is before P */
6e3b96ed 3057 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
99c0fb5f
N
3058 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
3059 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
67cc2b81 3060 ddf_layout = 1;
99c0fb5f
N
3061 break;
3062
3063 case ALGORITHM_LEFT_ASYMMETRIC_6:
3064 /* RAID5 left_asymmetric, with Q on last device */
6e3b96ed 3065 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
99c0fb5f
N
3066 if (*dd_idx >= pd_idx)
3067 (*dd_idx)++;
3068 qd_idx = raid_disks - 1;
3069 break;
3070
3071 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6e3b96ed 3072 pd_idx = sector_div(stripe2, raid_disks-1);
99c0fb5f
N
3073 if (*dd_idx >= pd_idx)
3074 (*dd_idx)++;
3075 qd_idx = raid_disks - 1;
3076 break;
3077
3078 case ALGORITHM_LEFT_SYMMETRIC_6:
6e3b96ed 3079 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
99c0fb5f
N
3080 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
3081 qd_idx = raid_disks - 1;
3082 break;
3083
3084 case ALGORITHM_RIGHT_SYMMETRIC_6:
6e3b96ed 3085 pd_idx = sector_div(stripe2, raid_disks-1);
99c0fb5f
N
3086 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
3087 qd_idx = raid_disks - 1;
3088 break;
3089
3090 case ALGORITHM_PARITY_0_6:
3091 pd_idx = 0;
3092 (*dd_idx)++;
3093 qd_idx = raid_disks - 1;
3094 break;
3095
16a53ecc 3096 default:
99c0fb5f 3097 BUG();
16a53ecc
N
3098 }
3099 break;
1da177e4
LT
3100 }
3101
911d4ee8
N
3102 if (sh) {
3103 sh->pd_idx = pd_idx;
3104 sh->qd_idx = qd_idx;
67cc2b81 3105 sh->ddf_layout = ddf_layout;
911d4ee8 3106 }
1da177e4
LT
3107 /*
3108 * Finally, compute the new sector number
3109 */
3110 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
3111 return new_sector;
3112}
3113
6d036f7d 3114sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
1da177e4 3115{
d1688a6d 3116 struct r5conf *conf = sh->raid_conf;
b875e531
N
3117 int raid_disks = sh->disks;
3118 int data_disks = raid_disks - conf->max_degraded;
1da177e4 3119 sector_t new_sector = sh->sector, check;
09c9e5fa
AN
3120 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
3121 : conf->chunk_sectors;
e183eaed
N
3122 int algorithm = previous ? conf->prev_algo
3123 : conf->algorithm;
1da177e4
LT
3124 sector_t stripe;
3125 int chunk_offset;
35f2a591
N
3126 sector_t chunk_number;
3127 int dummy1, dd_idx = i;
1da177e4 3128 sector_t r_sector;
911d4ee8 3129 struct stripe_head sh2;
1da177e4
LT
3130
3131 chunk_offset = sector_div(new_sector, sectors_per_chunk);
3132 stripe = new_sector;
1da177e4 3133
16a53ecc
N
3134 if (i == sh->pd_idx)
3135 return 0;
3136 switch(conf->level) {
3137 case 4: break;
3138 case 5:
e183eaed 3139 switch (algorithm) {
1da177e4
LT
3140 case ALGORITHM_LEFT_ASYMMETRIC:
3141 case ALGORITHM_RIGHT_ASYMMETRIC:
3142 if (i > sh->pd_idx)
3143 i--;
3144 break;
3145 case ALGORITHM_LEFT_SYMMETRIC:
3146 case ALGORITHM_RIGHT_SYMMETRIC:
3147 if (i < sh->pd_idx)
3148 i += raid_disks;
3149 i -= (sh->pd_idx + 1);
3150 break;
99c0fb5f
N
3151 case ALGORITHM_PARITY_0:
3152 i -= 1;
3153 break;
3154 case ALGORITHM_PARITY_N:
3155 break;
1da177e4 3156 default:
99c0fb5f 3157 BUG();
16a53ecc
N
3158 }
3159 break;
3160 case 6:
d0dabf7e 3161 if (i == sh->qd_idx)
16a53ecc 3162 return 0; /* It is the Q disk */
e183eaed 3163 switch (algorithm) {
16a53ecc
N
3164 case ALGORITHM_LEFT_ASYMMETRIC:
3165 case ALGORITHM_RIGHT_ASYMMETRIC:
99c0fb5f
N
3166 case ALGORITHM_ROTATING_ZERO_RESTART:
3167 case ALGORITHM_ROTATING_N_RESTART:
3168 if (sh->pd_idx == raid_disks-1)
3169 i--; /* Q D D D P */
16a53ecc
N
3170 else if (i > sh->pd_idx)
3171 i -= 2; /* D D P Q D */
3172 break;
3173 case ALGORITHM_LEFT_SYMMETRIC:
3174 case ALGORITHM_RIGHT_SYMMETRIC:
3175 if (sh->pd_idx == raid_disks-1)
3176 i--; /* Q D D D P */
3177 else {
3178 /* D D P Q D */
3179 if (i < sh->pd_idx)
3180 i += raid_disks;
3181 i -= (sh->pd_idx + 2);
3182 }
3183 break;
99c0fb5f
N
3184 case ALGORITHM_PARITY_0:
3185 i -= 2;
3186 break;
3187 case ALGORITHM_PARITY_N:
3188 break;
3189 case ALGORITHM_ROTATING_N_CONTINUE:
e4424fee 3190 /* Like left_symmetric, but P is before Q */
99c0fb5f
N
3191 if (sh->pd_idx == 0)
3192 i--; /* P D D D Q */
e4424fee
N
3193 else {
3194 /* D D Q P D */
3195 if (i < sh->pd_idx)
3196 i += raid_disks;
3197 i -= (sh->pd_idx + 1);
3198 }
99c0fb5f
N
3199 break;
3200 case ALGORITHM_LEFT_ASYMMETRIC_6:
3201 case ALGORITHM_RIGHT_ASYMMETRIC_6:
3202 if (i > sh->pd_idx)
3203 i--;
3204 break;
3205 case ALGORITHM_LEFT_SYMMETRIC_6:
3206 case ALGORITHM_RIGHT_SYMMETRIC_6:
3207 if (i < sh->pd_idx)
3208 i += data_disks + 1;
3209 i -= (sh->pd_idx + 1);
3210 break;
3211 case ALGORITHM_PARITY_0_6:
3212 i -= 1;
3213 break;
16a53ecc 3214 default:
99c0fb5f 3215 BUG();
16a53ecc
N
3216 }
3217 break;
1da177e4
LT
3218 }
3219
3220 chunk_number = stripe * data_disks + i;
35f2a591 3221 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
1da177e4 3222
112bf897 3223 check = raid5_compute_sector(conf, r_sector,
784052ec 3224 previous, &dummy1, &sh2);
911d4ee8
N
3225 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
3226 || sh2.qd_idx != sh->qd_idx) {
cc6167b4
N
3227 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
3228 mdname(conf->mddev));
1da177e4
LT
3229 return 0;
3230 }
3231 return r_sector;
3232}
3233
07e83364
SL
3234/*
3235 * There are cases where we want handle_stripe_dirtying() and
3236 * schedule_reconstruction() to delay towrite to some dev of a stripe.
3237 *
3238 * This function checks whether we want to delay the towrite. Specifically,
3239 * we delay the towrite when:
3240 *
3241 * 1. degraded stripe has a non-overwrite to the missing dev, AND this
3242 * stripe has data in journal (for other devices).
3243 *
3244 * In this case, when reading data for the non-overwrite dev, it is
3245 * necessary to handle complex rmw of write back cache (prexor with
3246 * orig_page, and xor with page). To keep read path simple, we would
3247 * like to flush data in journal to RAID disks first, so complex rmw
3248 * is handled in the write patch (handle_stripe_dirtying).
3249 *
39b99586
SL
3250 * 2. when journal space is critical (R5C_LOG_CRITICAL=1)
3251 *
3252 * It is important to be able to flush all stripes in raid5-cache.
3253 * Therefore, we need reserve some space on the journal device for
3254 * these flushes. If flush operation includes pending writes to the
3255 * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe
3256 * for the flush out. If we exclude these pending writes from flush
3257 * operation, we only need (conf->max_degraded + 1) pages per stripe.
3258 * Therefore, excluding pending writes in these cases enables more
3259 * efficient use of the journal device.
3260 *
3261 * Note: To make sure the stripe makes progress, we only delay
3262 * towrite for stripes with data already in journal (injournal > 0).
3263 * When LOG_CRITICAL, stripes with injournal == 0 will be sent to
3264 * no_space_stripes list.
3265 *
70d466f7
SL
3266 * 3. during journal failure
3267 * In journal failure, we try to flush all cached data to raid disks
3268 * based on data in stripe cache. The array is read-only to upper
3269 * layers, so we would skip all pending writes.
3270 *
07e83364 3271 */
39b99586
SL
3272static inline bool delay_towrite(struct r5conf *conf,
3273 struct r5dev *dev,
3274 struct stripe_head_state *s)
07e83364 3275{
39b99586
SL
3276 /* case 1 above */
3277 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3278 !test_bit(R5_Insync, &dev->flags) && s->injournal)
3279 return true;
3280 /* case 2 above */
3281 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
3282 s->injournal > 0)
3283 return true;
70d466f7
SL
3284 /* case 3 above */
3285 if (s->log_failed && s->injournal)
3286 return true;
39b99586 3287 return false;
07e83364
SL
3288}
3289
600aa109 3290static void
c0f7bddb 3291schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
600aa109 3292 int rcw, int expand)
e33129d8 3293{
584acdd4 3294 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
d1688a6d 3295 struct r5conf *conf = sh->raid_conf;
c0f7bddb 3296 int level = conf->level;
e33129d8
DW
3297
3298 if (rcw) {
1e6d690b
SL
3299 /*
3300 * In some cases, handle_stripe_dirtying initially decided to
3301 * run rmw and allocates extra page for prexor. However, rcw is
3302 * cheaper later on. We need to free the extra page now,
3303 * because we won't be able to do that in ops_complete_prexor().
3304 */
3305 r5c_release_extra_page(sh);
e33129d8
DW
3306
3307 for (i = disks; i--; ) {
3308 struct r5dev *dev = &sh->dev[i];
3309
39b99586 3310 if (dev->towrite && !delay_towrite(conf, dev, s)) {
e33129d8 3311 set_bit(R5_LOCKED, &dev->flags);
d8ee0728 3312 set_bit(R5_Wantdrain, &dev->flags);
e33129d8
DW
3313 if (!expand)
3314 clear_bit(R5_UPTODATE, &dev->flags);
600aa109 3315 s->locked++;
1e6d690b
SL
3316 } else if (test_bit(R5_InJournal, &dev->flags)) {
3317 set_bit(R5_LOCKED, &dev->flags);
3318 s->locked++;
e33129d8
DW
3319 }
3320 }
ce7d363a
N
3321 /* if we are not expanding this is a proper write request, and
3322 * there will be bios with new data to be drained into the
3323 * stripe cache
3324 */
3325 if (!expand) {
3326 if (!s->locked)
3327 /* False alarm, nothing to do */
3328 return;
3329 sh->reconstruct_state = reconstruct_state_drain_run;
3330 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3331 } else
3332 sh->reconstruct_state = reconstruct_state_run;
3333
3334 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3335
c0f7bddb 3336 if (s->locked + conf->max_degraded == disks)
8b3e6cdc 3337 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
c0f7bddb 3338 atomic_inc(&conf->pending_full_writes);
e33129d8
DW
3339 } else {
3340 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
3341 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
584acdd4
MS
3342 BUG_ON(level == 6 &&
3343 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
3344 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
e33129d8 3345
e33129d8
DW
3346 for (i = disks; i--; ) {
3347 struct r5dev *dev = &sh->dev[i];
584acdd4 3348 if (i == pd_idx || i == qd_idx)
e33129d8
DW
3349 continue;
3350
e33129d8
DW
3351 if (dev->towrite &&
3352 (test_bit(R5_UPTODATE, &dev->flags) ||
d8ee0728
DW
3353 test_bit(R5_Wantcompute, &dev->flags))) {
3354 set_bit(R5_Wantdrain, &dev->flags);
e33129d8
DW
3355 set_bit(R5_LOCKED, &dev->flags);
3356 clear_bit(R5_UPTODATE, &dev->flags);
600aa109 3357 s->locked++;
1e6d690b
SL
3358 } else if (test_bit(R5_InJournal, &dev->flags)) {
3359 set_bit(R5_LOCKED, &dev->flags);
3360 s->locked++;
e33129d8
DW
3361 }
3362 }
ce7d363a
N
3363 if (!s->locked)
3364 /* False alarm - nothing to do */
3365 return;
3366 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
3367 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
3368 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3369 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
e33129d8
DW
3370 }
3371
c0f7bddb 3372 /* keep the parity disk(s) locked while asynchronous operations
e33129d8
DW
3373 * are in flight
3374 */
3375 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
3376 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
600aa109 3377 s->locked++;
e33129d8 3378
c0f7bddb
YT
3379 if (level == 6) {
3380 int qd_idx = sh->qd_idx;
3381 struct r5dev *dev = &sh->dev[qd_idx];
3382
3383 set_bit(R5_LOCKED, &dev->flags);
3384 clear_bit(R5_UPTODATE, &dev->flags);
3385 s->locked++;
3386 }
3387
845b9e22 3388 if (raid5_has_ppl(sh->raid_conf) && sh->ppl_page &&
3418d036
AP
3389 test_bit(STRIPE_OP_BIODRAIN, &s->ops_request) &&
3390 !test_bit(STRIPE_FULL_WRITE, &sh->state) &&
3391 test_bit(R5_Insync, &sh->dev[pd_idx].flags))
3392 set_bit(STRIPE_OP_PARTIAL_PARITY, &s->ops_request);
3393
600aa109 3394 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
e46b272b 3395 __func__, (unsigned long long)sh->sector,
600aa109 3396 s->locked, s->ops_request);
e33129d8 3397}
16a53ecc 3398
1da177e4
LT
3399/*
3400 * Each stripe/dev can have one or more bion attached.
16a53ecc 3401 * toread/towrite point to the first in a chain.
1da177e4
LT
3402 * The bi_next chain must be in order.
3403 */
da41ba65 3404static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
3405 int forwrite, int previous)
1da177e4
LT
3406{
3407 struct bio **bip;
d1688a6d 3408 struct r5conf *conf = sh->raid_conf;
72626685 3409 int firstwrite=0;
1da177e4 3410
cbe47ec5 3411 pr_debug("adding bi b#%llu to stripe s#%llu\n",
4f024f37 3412 (unsigned long long)bi->bi_iter.bi_sector,
1da177e4
LT
3413 (unsigned long long)sh->sector);
3414
b17459c0 3415 spin_lock_irq(&sh->stripe_lock);
2cd259a7 3416 sh->dev[dd_idx].write_hint = bi->bi_write_hint;
59fc630b 3417 /* Don't allow new IO added to stripes in batch list */
3418 if (sh->batch_head)
3419 goto overlap;
72626685 3420 if (forwrite) {
1da177e4 3421 bip = &sh->dev[dd_idx].towrite;
7eaf7e8e 3422 if (*bip == NULL)
72626685
N
3423 firstwrite = 1;
3424 } else
1da177e4 3425 bip = &sh->dev[dd_idx].toread;
4f024f37
KO
3426 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
3427 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
1da177e4
LT
3428 goto overlap;
3429 bip = & (*bip)->bi_next;
3430 }
4f024f37 3431 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
1da177e4
LT
3432 goto overlap;
3433
3418d036
AP
3434 if (forwrite && raid5_has_ppl(conf)) {
3435 /*
3436 * With PPL only writes to consecutive data chunks within a
3437 * stripe are allowed because for a single stripe_head we can
3438 * only have one PPL entry at a time, which describes one data
3439 * range. Not really an overlap, but wait_for_overlap can be
3440 * used to handle this.
3441 */
3442 sector_t sector;
3443 sector_t first = 0;
3444 sector_t last = 0;
3445 int count = 0;
3446 int i;
3447
3448 for (i = 0; i < sh->disks; i++) {
3449 if (i != sh->pd_idx &&
3450 (i == dd_idx || sh->dev[i].towrite)) {
3451 sector = sh->dev[i].sector;
3452 if (count == 0 || sector < first)
3453 first = sector;
3454 if (sector > last)
3455 last = sector;
3456 count++;
3457 }
3458 }
3459
3460 if (first + conf->chunk_sectors * (count - 1) != last)
3461 goto overlap;
3462 }
3463
da41ba65 3464 if (!forwrite || previous)
3465 clear_bit(STRIPE_BATCH_READY, &sh->state);
3466
78bafebd 3467 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
1da177e4
LT
3468 if (*bip)
3469 bi->bi_next = *bip;
3470 *bip = bi;
016c76ac 3471 bio_inc_remaining(bi);
49728050 3472 md_write_inc(conf->mddev, bi);
72626685 3473
1da177e4
LT
3474 if (forwrite) {
3475 /* check if page is covered */
3476 sector_t sector = sh->dev[dd_idx].sector;
3477 for (bi=sh->dev[dd_idx].towrite;
c911c46c 3478 sector < sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf) &&
4f024f37 3479 bi && bi->bi_iter.bi_sector <= sector;
c911c46c 3480 bi = r5_next_bio(conf, bi, sh->dev[dd_idx].sector)) {
f73a1c7d
KO
3481 if (bio_end_sector(bi) >= sector)
3482 sector = bio_end_sector(bi);
1da177e4 3483 }
c911c46c 3484 if (sector >= sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf))
7a87f434 3485 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
3486 sh->overwrite_disks++;
1da177e4 3487 }
cbe47ec5
N
3488
3489 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
4f024f37 3490 (unsigned long long)(*bip)->bi_iter.bi_sector,
cbe47ec5
N
3491 (unsigned long long)sh->sector, dd_idx);
3492
3493 if (conf->mddev->bitmap && firstwrite) {
d0852df5
N
3494 /* Cannot hold spinlock over bitmap_startwrite,
3495 * but must ensure this isn't added to a batch until
3496 * we have added to the bitmap and set bm_seq.
3497 * So set STRIPE_BITMAP_PENDING to prevent
3498 * batching.
3499 * If multiple add_stripe_bio() calls race here they
3500 * much all set STRIPE_BITMAP_PENDING. So only the first one
3501 * to complete "bitmap_startwrite" gets to set
3502 * STRIPE_BIT_DELAY. This is important as once a stripe
3503 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3504 * any more.
3505 */
3506 set_bit(STRIPE_BITMAP_PENDING, &sh->state);
3507 spin_unlock_irq(&sh->stripe_lock);
e64e4018 3508 md_bitmap_startwrite(conf->mddev->bitmap, sh->sector,
c911c46c 3509 RAID5_STRIPE_SECTORS(conf), 0);
d0852df5
N
3510 spin_lock_irq(&sh->stripe_lock);
3511 clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
3512 if (!sh->batch_head) {
3513 sh->bm_seq = conf->seq_flush+1;
3514 set_bit(STRIPE_BIT_DELAY, &sh->state);
3515 }
cbe47ec5 3516 }
d0852df5 3517 spin_unlock_irq(&sh->stripe_lock);
59fc630b 3518
3519 if (stripe_can_batch(sh))
3520 stripe_add_to_batch_list(conf, sh);
1da177e4
LT
3521 return 1;
3522
3523 overlap:
3524 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
b17459c0 3525 spin_unlock_irq(&sh->stripe_lock);
1da177e4
LT
3526 return 0;
3527}
3528
d1688a6d 3529static void end_reshape(struct r5conf *conf);
29269553 3530
d1688a6d 3531static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
911d4ee8 3532 struct stripe_head *sh)
ccfcc3c1 3533{
784052ec 3534 int sectors_per_chunk =
09c9e5fa 3535 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
911d4ee8 3536 int dd_idx;
2d2063ce 3537 int chunk_offset = sector_div(stripe, sectors_per_chunk);
112bf897 3538 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
2d2063ce 3539
112bf897
N
3540 raid5_compute_sector(conf,
3541 stripe * (disks - conf->max_degraded)
b875e531 3542 *sectors_per_chunk + chunk_offset,
112bf897 3543 previous,
911d4ee8 3544 &dd_idx, sh);
ccfcc3c1
N
3545}
3546
a4456856 3547static void
d1688a6d 3548handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
bd83d0a2 3549 struct stripe_head_state *s, int disks)
a4456856
DW
3550{
3551 int i;
59fc630b 3552 BUG_ON(sh->batch_head);
a4456856
DW
3553 for (i = disks; i--; ) {
3554 struct bio *bi;
3555 int bitmap_end = 0;
3556
3557 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
3cb03002 3558 struct md_rdev *rdev;
a4456856
DW
3559 rcu_read_lock();
3560 rdev = rcu_dereference(conf->disks[i].rdev);
f5b67ae8
N
3561 if (rdev && test_bit(In_sync, &rdev->flags) &&
3562 !test_bit(Faulty, &rdev->flags))
7f0da59b
N
3563 atomic_inc(&rdev->nr_pending);
3564 else
3565 rdev = NULL;
a4456856 3566 rcu_read_unlock();
7f0da59b
N
3567 if (rdev) {
3568 if (!rdev_set_badblocks(
3569 rdev,
3570 sh->sector,
c911c46c 3571 RAID5_STRIPE_SECTORS(conf), 0))
7f0da59b
N
3572 md_error(conf->mddev, rdev);
3573 rdev_dec_pending(rdev, conf->mddev);
3574 }
a4456856 3575 }
b17459c0 3576 spin_lock_irq(&sh->stripe_lock);
a4456856
DW
3577 /* fail all writes first */
3578 bi = sh->dev[i].towrite;
3579 sh->dev[i].towrite = NULL;
7a87f434 3580 sh->overwrite_disks = 0;
b17459c0 3581 spin_unlock_irq(&sh->stripe_lock);
1ed850f3 3582 if (bi)
a4456856 3583 bitmap_end = 1;
a4456856 3584
ff875738 3585 log_stripe_write_finished(sh);
0576b1c6 3586
a4456856
DW
3587 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3588 wake_up(&conf->wait_for_overlap);
3589
4f024f37 3590 while (bi && bi->bi_iter.bi_sector <
c911c46c
YY
3591 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3592 struct bio *nextbi = r5_next_bio(conf, bi, sh->dev[i].sector);
4246a0b6 3593
49728050 3594 md_write_end(conf->mddev);
6308d8e3 3595 bio_io_error(bi);
a4456856
DW
3596 bi = nextbi;
3597 }
7eaf7e8e 3598 if (bitmap_end)
e64e4018 3599 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
c911c46c 3600 RAID5_STRIPE_SECTORS(conf), 0, 0);
7eaf7e8e 3601 bitmap_end = 0;
a4456856
DW
3602 /* and fail all 'written' */
3603 bi = sh->dev[i].written;
3604 sh->dev[i].written = NULL;
d592a996
SL
3605 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3606 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3607 sh->dev[i].page = sh->dev[i].orig_page;
3608 }
3609
a4456856 3610 if (bi) bitmap_end = 1;
4f024f37 3611 while (bi && bi->bi_iter.bi_sector <
c911c46c
YY
3612 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3613 struct bio *bi2 = r5_next_bio(conf, bi, sh->dev[i].sector);
4246a0b6 3614
49728050 3615 md_write_end(conf->mddev);
6308d8e3 3616 bio_io_error(bi);
a4456856
DW
3617 bi = bi2;
3618 }
3619
b5e98d65
DW
3620 /* fail any reads if this device is non-operational and
3621 * the data has not reached the cache yet.
3622 */
3623 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
6e74a9cf 3624 s->failed > conf->max_degraded &&
b5e98d65
DW
3625 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3626 test_bit(R5_ReadError, &sh->dev[i].flags))) {
143c4d05 3627 spin_lock_irq(&sh->stripe_lock);
a4456856
DW
3628 bi = sh->dev[i].toread;
3629 sh->dev[i].toread = NULL;
143c4d05 3630 spin_unlock_irq(&sh->stripe_lock);
a4456856
DW
3631 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3632 wake_up(&conf->wait_for_overlap);
ebda780b
SL
3633 if (bi)
3634 s->to_read--;
4f024f37 3635 while (bi && bi->bi_iter.bi_sector <
c911c46c 3636 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
a4456856 3637 struct bio *nextbi =
c911c46c 3638 r5_next_bio(conf, bi, sh->dev[i].sector);
4246a0b6 3639
6308d8e3 3640 bio_io_error(bi);
a4456856
DW
3641 bi = nextbi;
3642 }
3643 }
a4456856 3644 if (bitmap_end)
e64e4018 3645 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
c911c46c 3646 RAID5_STRIPE_SECTORS(conf), 0, 0);
8cfa7b0f
N
3647 /* If we were in the middle of a write the parity block might
3648 * still be locked - so just clear all R5_LOCKED flags
3649 */
3650 clear_bit(R5_LOCKED, &sh->dev[i].flags);
a4456856 3651 }
ebda780b
SL
3652 s->to_write = 0;
3653 s->written = 0;
a4456856 3654
8b3e6cdc
DW
3655 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3656 if (atomic_dec_and_test(&conf->pending_full_writes))
3657 md_wakeup_thread(conf->mddev->thread);
a4456856
DW
3658}
3659
7f0da59b 3660static void
d1688a6d 3661handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
7f0da59b
N
3662 struct stripe_head_state *s)
3663{
3664 int abort = 0;
3665 int i;
3666
59fc630b 3667 BUG_ON(sh->batch_head);
7f0da59b 3668 clear_bit(STRIPE_SYNCING, &sh->state);
f8dfcffd
N
3669 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3670 wake_up(&conf->wait_for_overlap);
7f0da59b 3671 s->syncing = 0;
9a3e1101 3672 s->replacing = 0;
7f0da59b 3673 /* There is nothing more to do for sync/check/repair.
18b9837e
N
3674 * Don't even need to abort as that is handled elsewhere
3675 * if needed, and not always wanted e.g. if there is a known
3676 * bad block here.
9a3e1101 3677 * For recover/replace we need to record a bad block on all
7f0da59b
N
3678 * non-sync devices, or abort the recovery
3679 */
18b9837e
N
3680 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3681 /* During recovery devices cannot be removed, so
3682 * locking and refcounting of rdevs is not needed
3683 */
e50d3992 3684 rcu_read_lock();
18b9837e 3685 for (i = 0; i < conf->raid_disks; i++) {
e50d3992 3686 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
18b9837e
N
3687 if (rdev
3688 && !test_bit(Faulty, &rdev->flags)
3689 && !test_bit(In_sync, &rdev->flags)
3690 && !rdev_set_badblocks(rdev, sh->sector,
c911c46c 3691 RAID5_STRIPE_SECTORS(conf), 0))
18b9837e 3692 abort = 1;
e50d3992 3693 rdev = rcu_dereference(conf->disks[i].replacement);
18b9837e
N
3694 if (rdev
3695 && !test_bit(Faulty, &rdev->flags)
3696 && !test_bit(In_sync, &rdev->flags)
3697 && !rdev_set_badblocks(rdev, sh->sector,
c911c46c 3698 RAID5_STRIPE_SECTORS(conf), 0))
18b9837e
N
3699 abort = 1;
3700 }
e50d3992 3701 rcu_read_unlock();
18b9837e
N
3702 if (abort)
3703 conf->recovery_disabled =
3704 conf->mddev->recovery_disabled;
7f0da59b 3705 }
c911c46c 3706 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), !abort);
7f0da59b
N
3707}
3708
9a3e1101
N
3709static int want_replace(struct stripe_head *sh, int disk_idx)
3710{
3711 struct md_rdev *rdev;
3712 int rv = 0;
3f232d6a
N
3713
3714 rcu_read_lock();
3715 rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
9a3e1101
N
3716 if (rdev
3717 && !test_bit(Faulty, &rdev->flags)
3718 && !test_bit(In_sync, &rdev->flags)
3719 && (rdev->recovery_offset <= sh->sector
3720 || rdev->mddev->recovery_cp <= sh->sector))
3721 rv = 1;
3f232d6a 3722 rcu_read_unlock();
9a3e1101
N
3723 return rv;
3724}
3725
2c58f06e
N
3726static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3727 int disk_idx, int disks)
a4456856 3728{
5599becc 3729 struct r5dev *dev = &sh->dev[disk_idx];
f2b3b44d
N
3730 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3731 &sh->dev[s->failed_num[1]] };
ea664c82 3732 int i;
45a4d8fd 3733 bool force_rcw = (sh->raid_conf->rmw_level == PARITY_DISABLE_RMW);
5599becc 3734
a79cfe12
N
3735
3736 if (test_bit(R5_LOCKED, &dev->flags) ||
3737 test_bit(R5_UPTODATE, &dev->flags))
3738 /* No point reading this as we already have it or have
3739 * decided to get it.
3740 */
3741 return 0;
3742
3743 if (dev->toread ||
3744 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3745 /* We need this block to directly satisfy a request */
3746 return 1;
3747
3748 if (s->syncing || s->expanding ||
3749 (s->replacing && want_replace(sh, disk_idx)))
3750 /* When syncing, or expanding we read everything.
3751 * When replacing, we need the replaced block.
3752 */
3753 return 1;
3754
3755 if ((s->failed >= 1 && fdev[0]->toread) ||
3756 (s->failed >= 2 && fdev[1]->toread))
3757 /* If we want to read from a failed device, then
3758 * we need to actually read every other device.
3759 */
3760 return 1;
3761
a9d56950
N
3762 /* Sometimes neither read-modify-write nor reconstruct-write
3763 * cycles can work. In those cases we read every block we
3764 * can. Then the parity-update is certain to have enough to
3765 * work with.
3766 * This can only be a problem when we need to write something,
3767 * and some device has failed. If either of those tests
3768 * fail we need look no further.
3769 */
3770 if (!s->failed || !s->to_write)
3771 return 0;
3772
3773 if (test_bit(R5_Insync, &dev->flags) &&
3774 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3775 /* Pre-reads at not permitted until after short delay
3776 * to gather multiple requests. However if this
3560741e 3777 * device is no Insync, the block could only be computed
a9d56950
N
3778 * and there is no need to delay that.
3779 */
3780 return 0;
ea664c82 3781
36707bb2 3782 for (i = 0; i < s->failed && i < 2; i++) {
ea664c82
N
3783 if (fdev[i]->towrite &&
3784 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3785 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3786 /* If we have a partial write to a failed
3787 * device, then we will need to reconstruct
3788 * the content of that device, so all other
3789 * devices must be read.
3790 */
3791 return 1;
45a4d8fd
CP
3792
3793 if (s->failed >= 2 &&
3794 (fdev[i]->towrite ||
3795 s->failed_num[i] == sh->pd_idx ||
3796 s->failed_num[i] == sh->qd_idx) &&
3797 !test_bit(R5_UPTODATE, &fdev[i]->flags))
3798 /* In max degraded raid6, If the failed disk is P, Q,
3799 * or we want to read the failed disk, we need to do
3800 * reconstruct-write.
3801 */
3802 force_rcw = true;
ea664c82
N
3803 }
3804
45a4d8fd
CP
3805 /* If we are forced to do a reconstruct-write, because parity
3806 * cannot be trusted and we are currently recovering it, there
3807 * is extra need to be careful.
ea664c82
N
3808 * If one of the devices that we would need to read, because
3809 * it is not being overwritten (and maybe not written at all)
3810 * is missing/faulty, then we need to read everything we can.
3811 */
45a4d8fd 3812 if (!force_rcw &&
ea664c82
N
3813 sh->sector < sh->raid_conf->mddev->recovery_cp)
3814 /* reconstruct-write isn't being forced */
3815 return 0;
36707bb2 3816 for (i = 0; i < s->failed && i < 2; i++) {
10d82c5f
N
3817 if (s->failed_num[i] != sh->pd_idx &&
3818 s->failed_num[i] != sh->qd_idx &&
3819 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
ea664c82
N
3820 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3821 return 1;
3822 }
3823
2c58f06e
N
3824 return 0;
3825}
3826
ba02684d
SL
3827/* fetch_block - checks the given member device to see if its data needs
3828 * to be read or computed to satisfy a request.
3829 *
3830 * Returns 1 when no more member devices need to be checked, otherwise returns
3831 * 0 to tell the loop in handle_stripe_fill to continue
3832 */
2c58f06e
N
3833static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3834 int disk_idx, int disks)
3835{
3836 struct r5dev *dev = &sh->dev[disk_idx];
3837
3838 /* is the data in this block needed, and can we get it? */
3839 if (need_this_block(sh, s, disk_idx, disks)) {
5599becc
YT
3840 /* we would like to get this block, possibly by computing it,
3841 * otherwise read it if the backing disk is insync
3842 */
3843 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3844 BUG_ON(test_bit(R5_Wantread, &dev->flags));
b0c783b3 3845 BUG_ON(sh->batch_head);
7471fb77
N
3846
3847 /*
3848 * In the raid6 case if the only non-uptodate disk is P
3849 * then we already trusted P to compute the other failed
3850 * drives. It is safe to compute rather than re-read P.
3851 * In other cases we only compute blocks from failed
3852 * devices, otherwise check/repair might fail to detect
3853 * a real inconsistency.
3854 */
3855
5599becc 3856 if ((s->uptodate == disks - 1) &&
7471fb77 3857 ((sh->qd_idx >= 0 && sh->pd_idx == disk_idx) ||
f2b3b44d 3858 (s->failed && (disk_idx == s->failed_num[0] ||
7471fb77 3859 disk_idx == s->failed_num[1])))) {
5599becc
YT
3860 /* have disk failed, and we're requested to fetch it;
3861 * do compute it
a4456856 3862 */
5599becc
YT
3863 pr_debug("Computing stripe %llu block %d\n",
3864 (unsigned long long)sh->sector, disk_idx);
3865 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3866 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3867 set_bit(R5_Wantcompute, &dev->flags);
3868 sh->ops.target = disk_idx;
3869 sh->ops.target2 = -1; /* no 2nd target */
3870 s->req_compute = 1;
93b3dbce
N
3871 /* Careful: from this point on 'uptodate' is in the eye
3872 * of raid_run_ops which services 'compute' operations
3873 * before writes. R5_Wantcompute flags a block that will
3874 * be R5_UPTODATE by the time it is needed for a
3875 * subsequent operation.
3876 */
5599becc
YT
3877 s->uptodate++;
3878 return 1;
3879 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3880 /* Computing 2-failure is *very* expensive; only
3881 * do it if failed >= 2
3882 */
3883 int other;
3884 for (other = disks; other--; ) {
3885 if (other == disk_idx)
3886 continue;
3887 if (!test_bit(R5_UPTODATE,
3888 &sh->dev[other].flags))
3889 break;
a4456856 3890 }
5599becc
YT
3891 BUG_ON(other < 0);
3892 pr_debug("Computing stripe %llu blocks %d,%d\n",
3893 (unsigned long long)sh->sector,
3894 disk_idx, other);
3895 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3896 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3897 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3898 set_bit(R5_Wantcompute, &sh->dev[other].flags);
3899 sh->ops.target = disk_idx;
3900 sh->ops.target2 = other;
3901 s->uptodate += 2;
3902 s->req_compute = 1;
3903 return 1;
3904 } else if (test_bit(R5_Insync, &dev->flags)) {
3905 set_bit(R5_LOCKED, &dev->flags);
3906 set_bit(R5_Wantread, &dev->flags);
3907 s->locked++;
3908 pr_debug("Reading block %d (sync=%d)\n",
3909 disk_idx, s->syncing);
a4456856
DW
3910 }
3911 }
5599becc
YT
3912
3913 return 0;
3914}
3915
2aada5b1 3916/*
93b3dbce 3917 * handle_stripe_fill - read or compute data to satisfy pending requests.
5599becc 3918 */
93b3dbce
N
3919static void handle_stripe_fill(struct stripe_head *sh,
3920 struct stripe_head_state *s,
3921 int disks)
5599becc
YT
3922{
3923 int i;
3924
3925 /* look for blocks to read/compute, skip this if a compute
3926 * is already in flight, or if the stripe contents are in the
3927 * midst of changing due to a write
3928 */
3929 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
07e83364
SL
3930 !sh->reconstruct_state) {
3931
3932 /*
3933 * For degraded stripe with data in journal, do not handle
3934 * read requests yet, instead, flush the stripe to raid
3935 * disks first, this avoids handling complex rmw of write
3936 * back cache (prexor with orig_page, and then xor with
3937 * page) in the read path
3938 */
3939 if (s->injournal && s->failed) {
3940 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
3941 r5c_make_stripe_write_out(sh);
3942 goto out;
3943 }
3944
5599becc 3945 for (i = disks; i--; )
93b3dbce 3946 if (fetch_block(sh, s, i, disks))
5599becc 3947 break;
07e83364
SL
3948 }
3949out:
a4456856
DW
3950 set_bit(STRIPE_HANDLE, &sh->state);
3951}
3952
787b76fa
N
3953static void break_stripe_batch_list(struct stripe_head *head_sh,
3954 unsigned long handle_flags);
1fe797e6 3955/* handle_stripe_clean_event
a4456856
DW
3956 * any written block on an uptodate or failed drive can be returned.
3957 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3958 * never LOCKED, so we don't need to test 'failed' directly.
3959 */
d1688a6d 3960static void handle_stripe_clean_event(struct r5conf *conf,
bd83d0a2 3961 struct stripe_head *sh, int disks)
a4456856
DW
3962{
3963 int i;
3964 struct r5dev *dev;
f8dfcffd 3965 int discard_pending = 0;
59fc630b 3966 struct stripe_head *head_sh = sh;
3967 bool do_endio = false;
a4456856
DW
3968
3969 for (i = disks; i--; )
3970 if (sh->dev[i].written) {
3971 dev = &sh->dev[i];
3972 if (!test_bit(R5_LOCKED, &dev->flags) &&
9e444768 3973 (test_bit(R5_UPTODATE, &dev->flags) ||
d592a996
SL
3974 test_bit(R5_Discard, &dev->flags) ||
3975 test_bit(R5_SkipCopy, &dev->flags))) {
a4456856
DW
3976 /* We can return any write requests */
3977 struct bio *wbi, *wbi2;
45b4233c 3978 pr_debug("Return write for disc %d\n", i);
ca64cae9
N
3979 if (test_and_clear_bit(R5_Discard, &dev->flags))
3980 clear_bit(R5_UPTODATE, &dev->flags);
d592a996
SL
3981 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3982 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
d592a996 3983 }
59fc630b 3984 do_endio = true;
3985
3986returnbi:
3987 dev->page = dev->orig_page;
a4456856
DW
3988 wbi = dev->written;
3989 dev->written = NULL;
4f024f37 3990 while (wbi && wbi->bi_iter.bi_sector <
c911c46c
YY
3991 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
3992 wbi2 = r5_next_bio(conf, wbi, dev->sector);
49728050 3993 md_write_end(conf->mddev);
016c76ac 3994 bio_endio(wbi);
a4456856
DW
3995 wbi = wbi2;
3996 }
e64e4018 3997 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
c911c46c 3998 RAID5_STRIPE_SECTORS(conf),
e64e4018
AS
3999 !test_bit(STRIPE_DEGRADED, &sh->state),
4000 0);
59fc630b 4001 if (head_sh->batch_head) {
4002 sh = list_first_entry(&sh->batch_list,
4003 struct stripe_head,
4004 batch_list);
4005 if (sh != head_sh) {
4006 dev = &sh->dev[i];
4007 goto returnbi;
4008 }
4009 }
4010 sh = head_sh;
4011 dev = &sh->dev[i];
f8dfcffd
N
4012 } else if (test_bit(R5_Discard, &dev->flags))
4013 discard_pending = 1;
4014 }
f6bed0ef 4015
ff875738 4016 log_stripe_write_finished(sh);
0576b1c6 4017
f8dfcffd
N
4018 if (!discard_pending &&
4019 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
b8a9d66d 4020 int hash;
f8dfcffd
N
4021 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
4022 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
4023 if (sh->qd_idx >= 0) {
4024 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
4025 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
4026 }
4027 /* now that discard is done we can proceed with any sync */
4028 clear_bit(STRIPE_DISCARD, &sh->state);
d47648fc
SL
4029 /*
4030 * SCSI discard will change some bio fields and the stripe has
4031 * no updated data, so remove it from hash list and the stripe
4032 * will be reinitialized
4033 */
59fc630b 4034unhash:
b8a9d66d
RG
4035 hash = sh->hash_lock_index;
4036 spin_lock_irq(conf->hash_locks + hash);
d47648fc 4037 remove_hash(sh);
b8a9d66d 4038 spin_unlock_irq(conf->hash_locks + hash);
59fc630b 4039 if (head_sh->batch_head) {
4040 sh = list_first_entry(&sh->batch_list,
4041 struct stripe_head, batch_list);
4042 if (sh != head_sh)
4043 goto unhash;
4044 }
59fc630b 4045 sh = head_sh;
4046
f8dfcffd
N
4047 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
4048 set_bit(STRIPE_HANDLE, &sh->state);
4049
4050 }
8b3e6cdc
DW
4051
4052 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
4053 if (atomic_dec_and_test(&conf->pending_full_writes))
4054 md_wakeup_thread(conf->mddev->thread);
59fc630b 4055
787b76fa
N
4056 if (head_sh->batch_head && do_endio)
4057 break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
a4456856
DW
4058}
4059
86aa1397
SL
4060/*
4061 * For RMW in write back cache, we need extra page in prexor to store the
4062 * old data. This page is stored in dev->orig_page.
4063 *
4064 * This function checks whether we have data for prexor. The exact logic
4065 * is:
4066 * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
4067 */
4068static inline bool uptodate_for_rmw(struct r5dev *dev)
4069{
4070 return (test_bit(R5_UPTODATE, &dev->flags)) &&
4071 (!test_bit(R5_InJournal, &dev->flags) ||
4072 test_bit(R5_OrigPageUPTDODATE, &dev->flags));
4073}
4074
d7bd398e
SL
4075static int handle_stripe_dirtying(struct r5conf *conf,
4076 struct stripe_head *sh,
4077 struct stripe_head_state *s,
4078 int disks)
a4456856
DW
4079{
4080 int rmw = 0, rcw = 0, i;
a7854487
AL
4081 sector_t recovery_cp = conf->mddev->recovery_cp;
4082
584acdd4 4083 /* Check whether resync is now happening or should start.
a7854487
AL
4084 * If yes, then the array is dirty (after unclean shutdown or
4085 * initial creation), so parity in some stripes might be inconsistent.
4086 * In this case, we need to always do reconstruct-write, to ensure
4087 * that in case of drive failure or read-error correction, we
4088 * generate correct data from the parity.
4089 */
584acdd4 4090 if (conf->rmw_level == PARITY_DISABLE_RMW ||
26ac1073
N
4091 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
4092 s->failed == 0)) {
a7854487 4093 /* Calculate the real rcw later - for now make it
c8ac1803
N
4094 * look like rcw is cheaper
4095 */
4096 rcw = 1; rmw = 2;
584acdd4
MS
4097 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
4098 conf->rmw_level, (unsigned long long)recovery_cp,
a7854487 4099 (unsigned long long)sh->sector);
c8ac1803 4100 } else for (i = disks; i--; ) {
a4456856
DW
4101 /* would I have to read this buffer for read_modify_write */
4102 struct r5dev *dev = &sh->dev[i];
39b99586 4103 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
07e83364 4104 i == sh->pd_idx || i == sh->qd_idx ||
1e6d690b 4105 test_bit(R5_InJournal, &dev->flags)) &&
a4456856 4106 !test_bit(R5_LOCKED, &dev->flags) &&
86aa1397 4107 !(uptodate_for_rmw(dev) ||
f38e1219 4108 test_bit(R5_Wantcompute, &dev->flags))) {
a4456856
DW
4109 if (test_bit(R5_Insync, &dev->flags))
4110 rmw++;
4111 else
4112 rmw += 2*disks; /* cannot read it */
4113 }
4114 /* Would I have to read this buffer for reconstruct_write */
584acdd4
MS
4115 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
4116 i != sh->pd_idx && i != sh->qd_idx &&
a4456856 4117 !test_bit(R5_LOCKED, &dev->flags) &&
f38e1219 4118 !(test_bit(R5_UPTODATE, &dev->flags) ||
1e6d690b 4119 test_bit(R5_Wantcompute, &dev->flags))) {
67f45548
N
4120 if (test_bit(R5_Insync, &dev->flags))
4121 rcw++;
a4456856
DW
4122 else
4123 rcw += 2*disks;
4124 }
4125 }
1e6d690b 4126
39b99586
SL
4127 pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n",
4128 (unsigned long long)sh->sector, sh->state, rmw, rcw);
a4456856 4129 set_bit(STRIPE_HANDLE, &sh->state);
41257580 4130 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
a4456856 4131 /* prefer read-modify-write, but need to get some data */
e3620a3a
JB
4132 if (conf->mddev->queue)
4133 blk_add_trace_msg(conf->mddev->queue,
4134 "raid5 rmw %llu %d",
4135 (unsigned long long)sh->sector, rmw);
a4456856
DW
4136 for (i = disks; i--; ) {
4137 struct r5dev *dev = &sh->dev[i];
1e6d690b
SL
4138 if (test_bit(R5_InJournal, &dev->flags) &&
4139 dev->page == dev->orig_page &&
4140 !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) {
4141 /* alloc page for prexor */
d7bd398e
SL
4142 struct page *p = alloc_page(GFP_NOIO);
4143
4144 if (p) {
4145 dev->orig_page = p;
4146 continue;
4147 }
4148
4149 /*
4150 * alloc_page() failed, try use
4151 * disk_info->extra_page
4152 */
4153 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE,
4154 &conf->cache_state)) {
4155 r5c_use_extra_page(sh);
4156 break;
4157 }
1e6d690b 4158
d7bd398e
SL
4159 /* extra_page in use, add to delayed_list */
4160 set_bit(STRIPE_DELAYED, &sh->state);
4161 s->waiting_extra_page = 1;
4162 return -EAGAIN;
1e6d690b 4163 }
d7bd398e 4164 }
1e6d690b 4165
d7bd398e
SL
4166 for (i = disks; i--; ) {
4167 struct r5dev *dev = &sh->dev[i];
39b99586 4168 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
1e6d690b
SL
4169 i == sh->pd_idx || i == sh->qd_idx ||
4170 test_bit(R5_InJournal, &dev->flags)) &&
a4456856 4171 !test_bit(R5_LOCKED, &dev->flags) &&
86aa1397 4172 !(uptodate_for_rmw(dev) ||
1e6d690b 4173 test_bit(R5_Wantcompute, &dev->flags)) &&
a4456856 4174 test_bit(R5_Insync, &dev->flags)) {
67f45548
N
4175 if (test_bit(STRIPE_PREREAD_ACTIVE,
4176 &sh->state)) {
4177 pr_debug("Read_old block %d for r-m-w\n",
4178 i);
a4456856
DW
4179 set_bit(R5_LOCKED, &dev->flags);
4180 set_bit(R5_Wantread, &dev->flags);
4181 s->locked++;
e3914d59 4182 } else
a4456856 4183 set_bit(STRIPE_DELAYED, &sh->state);
a4456856
DW
4184 }
4185 }
a9add5d9 4186 }
41257580 4187 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
a4456856 4188 /* want reconstruct write, but need to get some data */
a9add5d9 4189 int qread =0;
c8ac1803 4190 rcw = 0;
a4456856
DW
4191 for (i = disks; i--; ) {
4192 struct r5dev *dev = &sh->dev[i];
4193 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
c8ac1803 4194 i != sh->pd_idx && i != sh->qd_idx &&
a4456856 4195 !test_bit(R5_LOCKED, &dev->flags) &&
f38e1219 4196 !(test_bit(R5_UPTODATE, &dev->flags) ||
c8ac1803
N
4197 test_bit(R5_Wantcompute, &dev->flags))) {
4198 rcw++;
67f45548
N
4199 if (test_bit(R5_Insync, &dev->flags) &&
4200 test_bit(STRIPE_PREREAD_ACTIVE,
4201 &sh->state)) {
45b4233c 4202 pr_debug("Read_old block "
a4456856
DW
4203 "%d for Reconstruct\n", i);
4204 set_bit(R5_LOCKED, &dev->flags);
4205 set_bit(R5_Wantread, &dev->flags);
4206 s->locked++;
a9add5d9 4207 qread++;
e3914d59 4208 } else
a4456856 4209 set_bit(STRIPE_DELAYED, &sh->state);
a4456856
DW
4210 }
4211 }
e3620a3a 4212 if (rcw && conf->mddev->queue)
a9add5d9
N
4213 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
4214 (unsigned long long)sh->sector,
4215 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
c8ac1803 4216 }
b1b02fe9
N
4217
4218 if (rcw > disks && rmw > disks &&
4219 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4220 set_bit(STRIPE_DELAYED, &sh->state);
4221
a4456856
DW
4222 /* now if nothing is locked, and if we have enough data,
4223 * we can start a write request
4224 */
f38e1219
DW
4225 /* since handle_stripe can be called at any time we need to handle the
4226 * case where a compute block operation has been submitted and then a
ac6b53b6
DW
4227 * subsequent call wants to start a write request. raid_run_ops only
4228 * handles the case where compute block and reconstruct are requested
f38e1219
DW
4229 * simultaneously. If this is not the case then new writes need to be
4230 * held off until the compute completes.
4231 */
976ea8d4
DW
4232 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
4233 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
1e6d690b 4234 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
c0f7bddb 4235 schedule_reconstruction(sh, s, rcw == 0, 0);
d7bd398e 4236 return 0;
a4456856
DW
4237}
4238
d1688a6d 4239static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
a4456856
DW
4240 struct stripe_head_state *s, int disks)
4241{
ecc65c9b 4242 struct r5dev *dev = NULL;
bd2ab670 4243
59fc630b 4244 BUG_ON(sh->batch_head);
a4456856 4245 set_bit(STRIPE_HANDLE, &sh->state);
e89f8962 4246
ecc65c9b
DW
4247 switch (sh->check_state) {
4248 case check_state_idle:
4249 /* start a new check operation if there are no failures */
bd2ab670 4250 if (s->failed == 0) {
bd2ab670 4251 BUG_ON(s->uptodate != disks);
ecc65c9b
DW
4252 sh->check_state = check_state_run;
4253 set_bit(STRIPE_OP_CHECK, &s->ops_request);
bd2ab670 4254 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
bd2ab670 4255 s->uptodate--;
ecc65c9b 4256 break;
bd2ab670 4257 }
f2b3b44d 4258 dev = &sh->dev[s->failed_num[0]];
df561f66 4259 fallthrough;
ecc65c9b
DW
4260 case check_state_compute_result:
4261 sh->check_state = check_state_idle;
4262 if (!dev)
4263 dev = &sh->dev[sh->pd_idx];
4264
4265 /* check that a write has not made the stripe insync */
4266 if (test_bit(STRIPE_INSYNC, &sh->state))
4267 break;
c8894419 4268
a4456856 4269 /* either failed parity check, or recovery is happening */
a4456856
DW
4270 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
4271 BUG_ON(s->uptodate != disks);
4272
4273 set_bit(R5_LOCKED, &dev->flags);
ecc65c9b 4274 s->locked++;
a4456856 4275 set_bit(R5_Wantwrite, &dev->flags);
830ea016 4276
a4456856 4277 clear_bit(STRIPE_DEGRADED, &sh->state);
a4456856 4278 set_bit(STRIPE_INSYNC, &sh->state);
ecc65c9b
DW
4279 break;
4280 case check_state_run:
4281 break; /* we will be called again upon completion */
4282 case check_state_check_result:
4283 sh->check_state = check_state_idle;
4284
4285 /* if a failure occurred during the check operation, leave
4286 * STRIPE_INSYNC not set and let the stripe be handled again
4287 */
4288 if (s->failed)
4289 break;
4290
4291 /* handle a successful check operation, if parity is correct
4292 * we are done. Otherwise update the mismatch count and repair
4293 * parity if !MD_RECOVERY_CHECK
4294 */
ad283ea4 4295 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
ecc65c9b
DW
4296 /* parity is correct (on disc,
4297 * not in buffer any more)
4298 */
4299 set_bit(STRIPE_INSYNC, &sh->state);
4300 else {
c911c46c 4301 atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches);
e1539036 4302 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
ecc65c9b
DW
4303 /* don't try to repair!! */
4304 set_bit(STRIPE_INSYNC, &sh->state);
e1539036
N
4305 pr_warn_ratelimited("%s: mismatch sector in range "
4306 "%llu-%llu\n", mdname(conf->mddev),
4307 (unsigned long long) sh->sector,
4308 (unsigned long long) sh->sector +
c911c46c 4309 RAID5_STRIPE_SECTORS(conf));
e1539036 4310 } else {
ecc65c9b 4311 sh->check_state = check_state_compute_run;
976ea8d4 4312 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
ecc65c9b
DW
4313 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4314 set_bit(R5_Wantcompute,
4315 &sh->dev[sh->pd_idx].flags);
4316 sh->ops.target = sh->pd_idx;
ac6b53b6 4317 sh->ops.target2 = -1;
ecc65c9b
DW
4318 s->uptodate++;
4319 }
4320 }
4321 break;
4322 case check_state_compute_run:
4323 break;
4324 default:
cc6167b4 4325 pr_err("%s: unknown check_state: %d sector: %llu\n",
ecc65c9b
DW
4326 __func__, sh->check_state,
4327 (unsigned long long) sh->sector);
4328 BUG();
a4456856
DW
4329 }
4330}
4331
d1688a6d 4332static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
36d1c647 4333 struct stripe_head_state *s,
f2b3b44d 4334 int disks)
a4456856 4335{
a4456856 4336 int pd_idx = sh->pd_idx;
34e04e87 4337 int qd_idx = sh->qd_idx;
d82dfee0 4338 struct r5dev *dev;
a4456856 4339
59fc630b 4340 BUG_ON(sh->batch_head);
a4456856
DW
4341 set_bit(STRIPE_HANDLE, &sh->state);
4342
4343 BUG_ON(s->failed > 2);
d82dfee0 4344
a4456856
DW
4345 /* Want to check and possibly repair P and Q.
4346 * However there could be one 'failed' device, in which
4347 * case we can only check one of them, possibly using the
4348 * other to generate missing data
4349 */
4350
d82dfee0
DW
4351 switch (sh->check_state) {
4352 case check_state_idle:
4353 /* start a new check operation if there are < 2 failures */
f2b3b44d 4354 if (s->failed == s->q_failed) {
d82dfee0 4355 /* The only possible failed device holds Q, so it
a4456856
DW
4356 * makes sense to check P (If anything else were failed,
4357 * we would have used P to recreate it).
4358 */
d82dfee0 4359 sh->check_state = check_state_run;
a4456856 4360 }
f2b3b44d 4361 if (!s->q_failed && s->failed < 2) {
d82dfee0 4362 /* Q is not failed, and we didn't use it to generate
a4456856
DW
4363 * anything, so it makes sense to check it
4364 */
d82dfee0
DW
4365 if (sh->check_state == check_state_run)
4366 sh->check_state = check_state_run_pq;
4367 else
4368 sh->check_state = check_state_run_q;
a4456856 4369 }
a4456856 4370
d82dfee0
DW
4371 /* discard potentially stale zero_sum_result */
4372 sh->ops.zero_sum_result = 0;
a4456856 4373
d82dfee0
DW
4374 if (sh->check_state == check_state_run) {
4375 /* async_xor_zero_sum destroys the contents of P */
4376 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
4377 s->uptodate--;
a4456856 4378 }
d82dfee0
DW
4379 if (sh->check_state >= check_state_run &&
4380 sh->check_state <= check_state_run_pq) {
4381 /* async_syndrome_zero_sum preserves P and Q, so
4382 * no need to mark them !uptodate here
4383 */
4384 set_bit(STRIPE_OP_CHECK, &s->ops_request);
4385 break;
a4456856
DW
4386 }
4387
d82dfee0
DW
4388 /* we have 2-disk failure */
4389 BUG_ON(s->failed != 2);
df561f66 4390 fallthrough;
d82dfee0
DW
4391 case check_state_compute_result:
4392 sh->check_state = check_state_idle;
a4456856 4393
d82dfee0
DW
4394 /* check that a write has not made the stripe insync */
4395 if (test_bit(STRIPE_INSYNC, &sh->state))
4396 break;
a4456856
DW
4397
4398 /* now write out any block on a failed drive,
d82dfee0 4399 * or P or Q if they were recomputed
a4456856 4400 */
b2176a1d 4401 dev = NULL;
a4456856 4402 if (s->failed == 2) {
f2b3b44d 4403 dev = &sh->dev[s->failed_num[1]];
a4456856
DW
4404 s->locked++;
4405 set_bit(R5_LOCKED, &dev->flags);
4406 set_bit(R5_Wantwrite, &dev->flags);
4407 }
4408 if (s->failed >= 1) {
f2b3b44d 4409 dev = &sh->dev[s->failed_num[0]];
a4456856
DW
4410 s->locked++;
4411 set_bit(R5_LOCKED, &dev->flags);
4412 set_bit(R5_Wantwrite, &dev->flags);
4413 }
d82dfee0 4414 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
a4456856
DW
4415 dev = &sh->dev[pd_idx];
4416 s->locked++;
4417 set_bit(R5_LOCKED, &dev->flags);
4418 set_bit(R5_Wantwrite, &dev->flags);
4419 }
d82dfee0 4420 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
a4456856
DW
4421 dev = &sh->dev[qd_idx];
4422 s->locked++;
4423 set_bit(R5_LOCKED, &dev->flags);
4424 set_bit(R5_Wantwrite, &dev->flags);
4425 }
b2176a1d
NC
4426 if (WARN_ONCE(dev && !test_bit(R5_UPTODATE, &dev->flags),
4427 "%s: disk%td not up to date\n",
4428 mdname(conf->mddev),
4429 dev - (struct r5dev *) &sh->dev)) {
4430 clear_bit(R5_LOCKED, &dev->flags);
4431 clear_bit(R5_Wantwrite, &dev->flags);
4432 s->locked--;
4433 }
a4456856
DW
4434 clear_bit(STRIPE_DEGRADED, &sh->state);
4435
4436 set_bit(STRIPE_INSYNC, &sh->state);
d82dfee0
DW
4437 break;
4438 case check_state_run:
4439 case check_state_run_q:
4440 case check_state_run_pq:
4441 break; /* we will be called again upon completion */
4442 case check_state_check_result:
4443 sh->check_state = check_state_idle;
4444
4445 /* handle a successful check operation, if parity is correct
4446 * we are done. Otherwise update the mismatch count and repair
4447 * parity if !MD_RECOVERY_CHECK
4448 */
4449 if (sh->ops.zero_sum_result == 0) {
a25d8c32
SL
4450 /* both parities are correct */
4451 if (!s->failed)
4452 set_bit(STRIPE_INSYNC, &sh->state);
4453 else {
4454 /* in contrast to the raid5 case we can validate
4455 * parity, but still have a failure to write
4456 * back
4457 */
4458 sh->check_state = check_state_compute_result;
4459 /* Returning at this point means that we may go
4460 * off and bring p and/or q uptodate again so
4461 * we make sure to check zero_sum_result again
4462 * to verify if p or q need writeback
4463 */
4464 }
d82dfee0 4465 } else {
c911c46c 4466 atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches);
e1539036 4467 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
d82dfee0
DW
4468 /* don't try to repair!! */
4469 set_bit(STRIPE_INSYNC, &sh->state);
e1539036
N
4470 pr_warn_ratelimited("%s: mismatch sector in range "
4471 "%llu-%llu\n", mdname(conf->mddev),
4472 (unsigned long long) sh->sector,
4473 (unsigned long long) sh->sector +
c911c46c 4474 RAID5_STRIPE_SECTORS(conf));
e1539036 4475 } else {
d82dfee0
DW
4476 int *target = &sh->ops.target;
4477
4478 sh->ops.target = -1;
4479 sh->ops.target2 = -1;
4480 sh->check_state = check_state_compute_run;
4481 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4482 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4483 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4484 set_bit(R5_Wantcompute,
4485 &sh->dev[pd_idx].flags);
4486 *target = pd_idx;
4487 target = &sh->ops.target2;
4488 s->uptodate++;
4489 }
4490 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4491 set_bit(R5_Wantcompute,
4492 &sh->dev[qd_idx].flags);
4493 *target = qd_idx;
4494 s->uptodate++;
4495 }
4496 }
4497 }
4498 break;
4499 case check_state_compute_run:
4500 break;
4501 default:
cc6167b4
N
4502 pr_warn("%s: unknown check_state: %d sector: %llu\n",
4503 __func__, sh->check_state,
4504 (unsigned long long) sh->sector);
d82dfee0 4505 BUG();
a4456856
DW
4506 }
4507}
4508
d1688a6d 4509static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
a4456856
DW
4510{
4511 int i;
4512
4513 /* We have read all the blocks in this stripe and now we need to
4514 * copy some of them into a target stripe for expand.
4515 */
f0a50d37 4516 struct dma_async_tx_descriptor *tx = NULL;
59fc630b 4517 BUG_ON(sh->batch_head);
a4456856
DW
4518 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4519 for (i = 0; i < sh->disks; i++)
34e04e87 4520 if (i != sh->pd_idx && i != sh->qd_idx) {
911d4ee8 4521 int dd_idx, j;
a4456856 4522 struct stripe_head *sh2;
a08abd8c 4523 struct async_submit_ctl submit;
a4456856 4524
6d036f7d 4525 sector_t bn = raid5_compute_blocknr(sh, i, 1);
911d4ee8
N
4526 sector_t s = raid5_compute_sector(conf, bn, 0,
4527 &dd_idx, NULL);
6d036f7d 4528 sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
a4456856
DW
4529 if (sh2 == NULL)
4530 /* so far only the early blocks of this stripe
4531 * have been requested. When later blocks
4532 * get requested, we will try again
4533 */
4534 continue;
4535 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
4536 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
4537 /* must have already done this block */
6d036f7d 4538 raid5_release_stripe(sh2);
a4456856
DW
4539 continue;
4540 }
f0a50d37
DW
4541
4542 /* place all the copies on one channel */
a08abd8c 4543 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
f0a50d37 4544 tx = async_memcpy(sh2->dev[dd_idx].page,
7aba13b7
YY
4545 sh->dev[i].page, sh2->dev[dd_idx].offset,
4546 sh->dev[i].offset, RAID5_STRIPE_SIZE(conf),
a08abd8c 4547 &submit);
f0a50d37 4548
a4456856
DW
4549 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
4550 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
4551 for (j = 0; j < conf->raid_disks; j++)
4552 if (j != sh2->pd_idx &&
86c374ba 4553 j != sh2->qd_idx &&
a4456856
DW
4554 !test_bit(R5_Expanded, &sh2->dev[j].flags))
4555 break;
4556 if (j == conf->raid_disks) {
4557 set_bit(STRIPE_EXPAND_READY, &sh2->state);
4558 set_bit(STRIPE_HANDLE, &sh2->state);
4559 }
6d036f7d 4560 raid5_release_stripe(sh2);
f0a50d37 4561
a4456856 4562 }
a2e08551 4563 /* done submitting copies, wait for them to complete */
749586b7 4564 async_tx_quiesce(&tx);
a4456856 4565}
1da177e4
LT
4566
4567/*
4568 * handle_stripe - do things to a stripe.
4569 *
9a3e1101
N
4570 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4571 * state of various bits to see what needs to be done.
1da177e4 4572 * Possible results:
9a3e1101
N
4573 * return some read requests which now have data
4574 * return some write requests which are safely on storage
1da177e4
LT
4575 * schedule a read on some buffers
4576 * schedule a write of some buffers
4577 * return confirmation of parity correctness
4578 *
1da177e4 4579 */
a4456856 4580
acfe726b 4581static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
1da177e4 4582{
d1688a6d 4583 struct r5conf *conf = sh->raid_conf;
f416885e 4584 int disks = sh->disks;
474af965
N
4585 struct r5dev *dev;
4586 int i;
9a3e1101 4587 int do_recovery = 0;
1da177e4 4588
acfe726b
N
4589 memset(s, 0, sizeof(*s));
4590
dabc4ec6 4591 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
4592 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
acfe726b
N
4593 s->failed_num[0] = -1;
4594 s->failed_num[1] = -1;
6e74a9cf 4595 s->log_failed = r5l_log_disk_error(conf);
1da177e4 4596
acfe726b 4597 /* Now to look around and see what can be done */
1da177e4 4598 rcu_read_lock();
16a53ecc 4599 for (i=disks; i--; ) {
3cb03002 4600 struct md_rdev *rdev;
31c176ec
N
4601 sector_t first_bad;
4602 int bad_sectors;
4603 int is_bad = 0;
acfe726b 4604
16a53ecc 4605 dev = &sh->dev[i];
1da177e4 4606
45b4233c 4607 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
9a3e1101
N
4608 i, dev->flags,
4609 dev->toread, dev->towrite, dev->written);
6c0069c0
YT
4610 /* maybe we can reply to a read
4611 *
4612 * new wantfill requests are only permitted while
4613 * ops_complete_biofill is guaranteed to be inactive
4614 */
4615 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4616 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4617 set_bit(R5_Wantfill, &dev->flags);
1da177e4 4618
16a53ecc 4619 /* now count some things */
cc94015a
N
4620 if (test_bit(R5_LOCKED, &dev->flags))
4621 s->locked++;
4622 if (test_bit(R5_UPTODATE, &dev->flags))
4623 s->uptodate++;
2d6e4ecc 4624 if (test_bit(R5_Wantcompute, &dev->flags)) {
cc94015a
N
4625 s->compute++;
4626 BUG_ON(s->compute > 2);
2d6e4ecc 4627 }
1da177e4 4628
acfe726b 4629 if (test_bit(R5_Wantfill, &dev->flags))
cc94015a 4630 s->to_fill++;
acfe726b 4631 else if (dev->toread)
cc94015a 4632 s->to_read++;
16a53ecc 4633 if (dev->towrite) {
cc94015a 4634 s->to_write++;
16a53ecc 4635 if (!test_bit(R5_OVERWRITE, &dev->flags))
cc94015a 4636 s->non_overwrite++;
16a53ecc 4637 }
a4456856 4638 if (dev->written)
cc94015a 4639 s->written++;
14a75d3e
N
4640 /* Prefer to use the replacement for reads, but only
4641 * if it is recovered enough and has no bad blocks.
4642 */
4643 rdev = rcu_dereference(conf->disks[i].replacement);
4644 if (rdev && !test_bit(Faulty, &rdev->flags) &&
c911c46c
YY
4645 rdev->recovery_offset >= sh->sector + RAID5_STRIPE_SECTORS(conf) &&
4646 !is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
14a75d3e
N
4647 &first_bad, &bad_sectors))
4648 set_bit(R5_ReadRepl, &dev->flags);
4649 else {
e6030cb0 4650 if (rdev && !test_bit(Faulty, &rdev->flags))
9a3e1101 4651 set_bit(R5_NeedReplace, &dev->flags);
e6030cb0
N
4652 else
4653 clear_bit(R5_NeedReplace, &dev->flags);
14a75d3e
N
4654 rdev = rcu_dereference(conf->disks[i].rdev);
4655 clear_bit(R5_ReadRepl, &dev->flags);
4656 }
9283d8c5
N
4657 if (rdev && test_bit(Faulty, &rdev->flags))
4658 rdev = NULL;
31c176ec 4659 if (rdev) {
c911c46c 4660 is_bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
31c176ec
N
4661 &first_bad, &bad_sectors);
4662 if (s->blocked_rdev == NULL
4663 && (test_bit(Blocked, &rdev->flags)
4664 || is_bad < 0)) {
4665 if (is_bad < 0)
4666 set_bit(BlockedBadBlocks,
4667 &rdev->flags);
4668 s->blocked_rdev = rdev;
4669 atomic_inc(&rdev->nr_pending);
4670 }
6bfe0b49 4671 }
415e72d0
N
4672 clear_bit(R5_Insync, &dev->flags);
4673 if (!rdev)
4674 /* Not in-sync */;
31c176ec
N
4675 else if (is_bad) {
4676 /* also not in-sync */
18b9837e
N
4677 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4678 test_bit(R5_UPTODATE, &dev->flags)) {
31c176ec
N
4679 /* treat as in-sync, but with a read error
4680 * which we can now try to correct
4681 */
4682 set_bit(R5_Insync, &dev->flags);
4683 set_bit(R5_ReadError, &dev->flags);
4684 }
4685 } else if (test_bit(In_sync, &rdev->flags))
415e72d0 4686 set_bit(R5_Insync, &dev->flags);
c911c46c 4687 else if (sh->sector + RAID5_STRIPE_SECTORS(conf) <= rdev->recovery_offset)
415e72d0 4688 /* in sync if before recovery_offset */
30d7a483
N
4689 set_bit(R5_Insync, &dev->flags);
4690 else if (test_bit(R5_UPTODATE, &dev->flags) &&
4691 test_bit(R5_Expanded, &dev->flags))
4692 /* If we've reshaped into here, we assume it is Insync.
4693 * We will shortly update recovery_offset to make
4694 * it official.
4695 */
4696 set_bit(R5_Insync, &dev->flags);
4697
1cc03eb9 4698 if (test_bit(R5_WriteError, &dev->flags)) {
14a75d3e
N
4699 /* This flag does not apply to '.replacement'
4700 * only to .rdev, so make sure to check that*/
4701 struct md_rdev *rdev2 = rcu_dereference(
4702 conf->disks[i].rdev);
4703 if (rdev2 == rdev)
4704 clear_bit(R5_Insync, &dev->flags);
4705 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
bc2607f3 4706 s->handle_bad_blocks = 1;
14a75d3e 4707 atomic_inc(&rdev2->nr_pending);
bc2607f3
N
4708 } else
4709 clear_bit(R5_WriteError, &dev->flags);
4710 }
1cc03eb9 4711 if (test_bit(R5_MadeGood, &dev->flags)) {
14a75d3e
N
4712 /* This flag does not apply to '.replacement'
4713 * only to .rdev, so make sure to check that*/
4714 struct md_rdev *rdev2 = rcu_dereference(
4715 conf->disks[i].rdev);
4716 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
b84db560 4717 s->handle_bad_blocks = 1;
14a75d3e 4718 atomic_inc(&rdev2->nr_pending);
b84db560
N
4719 } else
4720 clear_bit(R5_MadeGood, &dev->flags);
4721 }
977df362
N
4722 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4723 struct md_rdev *rdev2 = rcu_dereference(
4724 conf->disks[i].replacement);
4725 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4726 s->handle_bad_blocks = 1;
4727 atomic_inc(&rdev2->nr_pending);
4728 } else
4729 clear_bit(R5_MadeGoodRepl, &dev->flags);
4730 }
415e72d0 4731 if (!test_bit(R5_Insync, &dev->flags)) {
16a53ecc
N
4732 /* The ReadError flag will just be confusing now */
4733 clear_bit(R5_ReadError, &dev->flags);
4734 clear_bit(R5_ReWrite, &dev->flags);
1da177e4 4735 }
415e72d0
N
4736 if (test_bit(R5_ReadError, &dev->flags))
4737 clear_bit(R5_Insync, &dev->flags);
4738 if (!test_bit(R5_Insync, &dev->flags)) {
cc94015a
N
4739 if (s->failed < 2)
4740 s->failed_num[s->failed] = i;
4741 s->failed++;
9a3e1101
N
4742 if (rdev && !test_bit(Faulty, &rdev->flags))
4743 do_recovery = 1;
d63e2fc8
BC
4744 else if (!rdev) {
4745 rdev = rcu_dereference(
4746 conf->disks[i].replacement);
4747 if (rdev && !test_bit(Faulty, &rdev->flags))
4748 do_recovery = 1;
4749 }
415e72d0 4750 }
2ded3703
SL
4751
4752 if (test_bit(R5_InJournal, &dev->flags))
4753 s->injournal++;
1e6d690b
SL
4754 if (test_bit(R5_InJournal, &dev->flags) && dev->written)
4755 s->just_cached++;
1da177e4 4756 }
9a3e1101
N
4757 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4758 /* If there is a failed device being replaced,
4759 * we must be recovering.
4760 * else if we are after recovery_cp, we must be syncing
c6d2e084 4761 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
9a3e1101
N
4762 * else we can only be replacing
4763 * sync and recovery both need to read all devices, and so
4764 * use the same flag.
4765 */
4766 if (do_recovery ||
c6d2e084 4767 sh->sector >= conf->mddev->recovery_cp ||
4768 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
9a3e1101
N
4769 s->syncing = 1;
4770 else
4771 s->replacing = 1;
4772 }
1da177e4 4773 rcu_read_unlock();
cc94015a
N
4774}
4775
cb9902db
GJ
4776/*
4777 * Return '1' if this is a member of batch, or '0' if it is a lone stripe or
4778 * a head which can now be handled.
4779 */
59fc630b 4780static int clear_batch_ready(struct stripe_head *sh)
4781{
4782 struct stripe_head *tmp;
4783 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
b15a9dbd 4784 return (sh->batch_head && sh->batch_head != sh);
59fc630b 4785 spin_lock(&sh->stripe_lock);
4786 if (!sh->batch_head) {
4787 spin_unlock(&sh->stripe_lock);
4788 return 0;
4789 }
4790
4791 /*
4792 * this stripe could be added to a batch list before we check
4793 * BATCH_READY, skips it
4794 */
4795 if (sh->batch_head != sh) {
4796 spin_unlock(&sh->stripe_lock);
4797 return 1;
4798 }
4799 spin_lock(&sh->batch_lock);
4800 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4801 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4802 spin_unlock(&sh->batch_lock);
4803 spin_unlock(&sh->stripe_lock);
4804
4805 /*
4806 * BATCH_READY is cleared, no new stripes can be added.
4807 * batch_list can be accessed without lock
4808 */
4809 return 0;
4810}
4811
3960ce79
N
4812static void break_stripe_batch_list(struct stripe_head *head_sh,
4813 unsigned long handle_flags)
72ac7330 4814{
4e3d62ff 4815 struct stripe_head *sh, *next;
72ac7330 4816 int i;
fb642b92 4817 int do_wakeup = 0;
72ac7330 4818
bb27051f
N
4819 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4820
72ac7330 4821 list_del_init(&sh->batch_list);
4822
fb3229d5 4823 WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
1b956f7a
N
4824 (1 << STRIPE_SYNCING) |
4825 (1 << STRIPE_REPLACED) |
1b956f7a
N
4826 (1 << STRIPE_DELAYED) |
4827 (1 << STRIPE_BIT_DELAY) |
4828 (1 << STRIPE_FULL_WRITE) |
4829 (1 << STRIPE_BIOFILL_RUN) |
4830 (1 << STRIPE_COMPUTE_RUN) |
1b956f7a
N
4831 (1 << STRIPE_DISCARD) |
4832 (1 << STRIPE_BATCH_READY) |
4833 (1 << STRIPE_BATCH_ERR) |
fb3229d5
SL
4834 (1 << STRIPE_BITMAP_PENDING)),
4835 "stripe state: %lx\n", sh->state);
4836 WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4837 (1 << STRIPE_REPLACED)),
4838 "head stripe state: %lx\n", head_sh->state);
1b956f7a
N
4839
4840 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
550da24f 4841 (1 << STRIPE_PREREAD_ACTIVE) |
184a09eb
DY
4842 (1 << STRIPE_DEGRADED) |
4843 (1 << STRIPE_ON_UNPLUG_LIST)),
1b956f7a
N
4844 head_sh->state & (1 << STRIPE_INSYNC));
4845
72ac7330 4846 sh->check_state = head_sh->check_state;
4847 sh->reconstruct_state = head_sh->reconstruct_state;
448ec638
AC
4848 spin_lock_irq(&sh->stripe_lock);
4849 sh->batch_head = NULL;
4850 spin_unlock_irq(&sh->stripe_lock);
fb642b92
N
4851 for (i = 0; i < sh->disks; i++) {
4852 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4853 do_wakeup = 1;
72ac7330 4854 sh->dev[i].flags = head_sh->dev[i].flags &
4855 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
fb642b92 4856 }
3960ce79
N
4857 if (handle_flags == 0 ||
4858 sh->state & handle_flags)
4859 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 4860 raid5_release_stripe(sh);
72ac7330 4861 }
fb642b92
N
4862 spin_lock_irq(&head_sh->stripe_lock);
4863 head_sh->batch_head = NULL;
4864 spin_unlock_irq(&head_sh->stripe_lock);
4865 for (i = 0; i < head_sh->disks; i++)
4866 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4867 do_wakeup = 1;
3960ce79
N
4868 if (head_sh->state & handle_flags)
4869 set_bit(STRIPE_HANDLE, &head_sh->state);
fb642b92
N
4870
4871 if (do_wakeup)
4872 wake_up(&head_sh->raid_conf->wait_for_overlap);
72ac7330 4873}
4874
cc94015a
N
4875static void handle_stripe(struct stripe_head *sh)
4876{
4877 struct stripe_head_state s;
d1688a6d 4878 struct r5conf *conf = sh->raid_conf;
3687c061 4879 int i;
84789554
N
4880 int prexor;
4881 int disks = sh->disks;
474af965 4882 struct r5dev *pdev, *qdev;
cc94015a
N
4883
4884 clear_bit(STRIPE_HANDLE, &sh->state);
a377a472
GJ
4885
4886 /*
4887 * handle_stripe should not continue handle the batched stripe, only
4888 * the head of batch list or lone stripe can continue. Otherwise we
4889 * could see break_stripe_batch_list warns about the STRIPE_ACTIVE
4890 * is set for the batched stripe.
4891 */
4892 if (clear_batch_ready(sh))
4893 return;
4894
257a4b42 4895 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
cc94015a
N
4896 /* already being handled, ensure it gets handled
4897 * again when current action finishes */
4898 set_bit(STRIPE_HANDLE, &sh->state);
4899 return;
4900 }
4901
4e3d62ff 4902 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
3960ce79 4903 break_stripe_batch_list(sh, 0);
72ac7330 4904
dabc4ec6 4905 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
f8dfcffd 4906 spin_lock(&sh->stripe_lock);
5ddf0440
SL
4907 /*
4908 * Cannot process 'sync' concurrently with 'discard'.
4909 * Flush data in r5cache before 'sync'.
4910 */
4911 if (!test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) &&
4912 !test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) &&
4913 !test_bit(STRIPE_DISCARD, &sh->state) &&
f8dfcffd
N
4914 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4915 set_bit(STRIPE_SYNCING, &sh->state);
4916 clear_bit(STRIPE_INSYNC, &sh->state);
f94c0b66 4917 clear_bit(STRIPE_REPLACED, &sh->state);
f8dfcffd
N
4918 }
4919 spin_unlock(&sh->stripe_lock);
cc94015a
N
4920 }
4921 clear_bit(STRIPE_DELAYED, &sh->state);
4922
4923 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4924 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4925 (unsigned long long)sh->sector, sh->state,
4926 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4927 sh->check_state, sh->reconstruct_state);
3687c061 4928
acfe726b 4929 analyse_stripe(sh, &s);
c5a31000 4930
b70abcb2
SL
4931 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
4932 goto finish;
4933
16d997b7
N
4934 if (s.handle_bad_blocks ||
4935 test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) {
bc2607f3
N
4936 set_bit(STRIPE_HANDLE, &sh->state);
4937 goto finish;
4938 }
4939
474af965
N
4940 if (unlikely(s.blocked_rdev)) {
4941 if (s.syncing || s.expanding || s.expanded ||
9a3e1101 4942 s.replacing || s.to_write || s.written) {
474af965
N
4943 set_bit(STRIPE_HANDLE, &sh->state);
4944 goto finish;
4945 }
4946 /* There is nothing for the blocked_rdev to block */
4947 rdev_dec_pending(s.blocked_rdev, conf->mddev);
4948 s.blocked_rdev = NULL;
4949 }
4950
4951 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4952 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4953 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4954 }
4955
4956 pr_debug("locked=%d uptodate=%d to_read=%d"
4957 " to_write=%d failed=%d failed_num=%d,%d\n",
4958 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4959 s.failed_num[0], s.failed_num[1]);
70d466f7
SL
4960 /*
4961 * check if the array has lost more than max_degraded devices and,
474af965 4962 * if so, some requests might need to be failed.
70d466f7
SL
4963 *
4964 * When journal device failed (log_failed), we will only process
4965 * the stripe if there is data need write to raid disks
474af965 4966 */
70d466f7
SL
4967 if (s.failed > conf->max_degraded ||
4968 (s.log_failed && s.injournal == 0)) {
9a3f530f
N
4969 sh->check_state = 0;
4970 sh->reconstruct_state = 0;
626f2092 4971 break_stripe_batch_list(sh, 0);
9a3f530f 4972 if (s.to_read+s.to_write+s.written)
bd83d0a2 4973 handle_failed_stripe(conf, sh, &s, disks);
9a3e1101 4974 if (s.syncing + s.replacing)
9a3f530f
N
4975 handle_failed_sync(conf, sh, &s);
4976 }
474af965 4977
84789554
N
4978 /* Now we check to see if any write operations have recently
4979 * completed
4980 */
4981 prexor = 0;
4982 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4983 prexor = 1;
4984 if (sh->reconstruct_state == reconstruct_state_drain_result ||
4985 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
4986 sh->reconstruct_state = reconstruct_state_idle;
4987
4988 /* All the 'written' buffers and the parity block are ready to
4989 * be written back to disk
4990 */
9e444768
SL
4991 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
4992 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
84789554 4993 BUG_ON(sh->qd_idx >= 0 &&
9e444768
SL
4994 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
4995 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
84789554
N
4996 for (i = disks; i--; ) {
4997 struct r5dev *dev = &sh->dev[i];
4998 if (test_bit(R5_LOCKED, &dev->flags) &&
4999 (i == sh->pd_idx || i == sh->qd_idx ||
1e6d690b
SL
5000 dev->written || test_bit(R5_InJournal,
5001 &dev->flags))) {
84789554
N
5002 pr_debug("Writing block %d\n", i);
5003 set_bit(R5_Wantwrite, &dev->flags);
5004 if (prexor)
5005 continue;
9c4bdf69
N
5006 if (s.failed > 1)
5007 continue;
84789554
N
5008 if (!test_bit(R5_Insync, &dev->flags) ||
5009 ((i == sh->pd_idx || i == sh->qd_idx) &&
5010 s.failed == 0))
5011 set_bit(STRIPE_INSYNC, &sh->state);
5012 }
5013 }
5014 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5015 s.dec_preread_active = 1;
5016 }
5017
ef5b7c69
N
5018 /*
5019 * might be able to return some write requests if the parity blocks
5020 * are safe, or on a failed drive
5021 */
5022 pdev = &sh->dev[sh->pd_idx];
5023 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
5024 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
5025 qdev = &sh->dev[sh->qd_idx];
5026 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
5027 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
5028 || conf->level < 6;
5029
5030 if (s.written &&
5031 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
5032 && !test_bit(R5_LOCKED, &pdev->flags)
5033 && (test_bit(R5_UPTODATE, &pdev->flags) ||
5034 test_bit(R5_Discard, &pdev->flags))))) &&
5035 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
5036 && !test_bit(R5_LOCKED, &qdev->flags)
5037 && (test_bit(R5_UPTODATE, &qdev->flags) ||
5038 test_bit(R5_Discard, &qdev->flags))))))
bd83d0a2 5039 handle_stripe_clean_event(conf, sh, disks);
ef5b7c69 5040
1e6d690b 5041 if (s.just_cached)
bd83d0a2 5042 r5c_handle_cached_data_endio(conf, sh, disks);
ff875738 5043 log_stripe_write_finished(sh);
1e6d690b 5044
ef5b7c69
N
5045 /* Now we might consider reading some blocks, either to check/generate
5046 * parity, or to satisfy requests
5047 * or to load a block that is being partially written.
5048 */
5049 if (s.to_read || s.non_overwrite
a1c6ae3d 5050 || (s.to_write && s.failed)
ef5b7c69
N
5051 || (s.syncing && (s.uptodate + s.compute < disks))
5052 || s.replacing
5053 || s.expanding)
5054 handle_stripe_fill(sh, &s, disks);
5055
2ded3703
SL
5056 /*
5057 * When the stripe finishes full journal write cycle (write to journal
5058 * and raid disk), this is the clean up procedure so it is ready for
5059 * next operation.
5060 */
5061 r5c_finish_stripe_write_out(conf, sh, &s);
5062
5063 /*
5064 * Now to consider new write requests, cache write back and what else,
5065 * if anything should be read. We do not handle new writes when:
84789554
N
5066 * 1/ A 'write' operation (copy+xor) is already in flight.
5067 * 2/ A 'check' operation is in flight, as it may clobber the parity
5068 * block.
2ded3703 5069 * 3/ A r5c cache log write is in flight.
84789554 5070 */
2ded3703
SL
5071
5072 if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
5073 if (!r5c_is_writeback(conf->log)) {
5074 if (s.to_write)
5075 handle_stripe_dirtying(conf, sh, &s, disks);
5076 } else { /* write back cache */
5077 int ret = 0;
5078
5079 /* First, try handle writes in caching phase */
5080 if (s.to_write)
5081 ret = r5c_try_caching_write(conf, sh, &s,
5082 disks);
5083 /*
5084 * If caching phase failed: ret == -EAGAIN
5085 * OR
5086 * stripe under reclaim: !caching && injournal
5087 *
5088 * fall back to handle_stripe_dirtying()
5089 */
5090 if (ret == -EAGAIN ||
5091 /* stripe under reclaim: !caching && injournal */
5092 (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
d7bd398e
SL
5093 s.injournal > 0)) {
5094 ret = handle_stripe_dirtying(conf, sh, &s,
5095 disks);
5096 if (ret == -EAGAIN)
5097 goto finish;
5098 }
2ded3703
SL
5099 }
5100 }
84789554
N
5101
5102 /* maybe we need to check and possibly fix the parity for this stripe
5103 * Any reads will already have been scheduled, so we just see if enough
5104 * data is available. The parity check is held off while parity
5105 * dependent operations are in flight.
5106 */
5107 if (sh->check_state ||
5108 (s.syncing && s.locked == 0 &&
5109 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
5110 !test_bit(STRIPE_INSYNC, &sh->state))) {
5111 if (conf->level == 6)
5112 handle_parity_checks6(conf, sh, &s, disks);
5113 else
5114 handle_parity_checks5(conf, sh, &s, disks);
5115 }
c5a31000 5116
f94c0b66
N
5117 if ((s.replacing || s.syncing) && s.locked == 0
5118 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
5119 && !test_bit(STRIPE_REPLACED, &sh->state)) {
9a3e1101
N
5120 /* Write out to replacement devices where possible */
5121 for (i = 0; i < conf->raid_disks; i++)
f94c0b66
N
5122 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
5123 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
9a3e1101
N
5124 set_bit(R5_WantReplace, &sh->dev[i].flags);
5125 set_bit(R5_LOCKED, &sh->dev[i].flags);
5126 s.locked++;
5127 }
f94c0b66
N
5128 if (s.replacing)
5129 set_bit(STRIPE_INSYNC, &sh->state);
5130 set_bit(STRIPE_REPLACED, &sh->state);
9a3e1101
N
5131 }
5132 if ((s.syncing || s.replacing) && s.locked == 0 &&
f94c0b66 5133 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
9a3e1101 5134 test_bit(STRIPE_INSYNC, &sh->state)) {
c911c46c 5135 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1);
c5a31000 5136 clear_bit(STRIPE_SYNCING, &sh->state);
f8dfcffd
N
5137 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
5138 wake_up(&conf->wait_for_overlap);
c5a31000
N
5139 }
5140
5141 /* If the failed drives are just a ReadError, then we might need
5142 * to progress the repair/check process
5143 */
5144 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
5145 for (i = 0; i < s.failed; i++) {
5146 struct r5dev *dev = &sh->dev[s.failed_num[i]];
5147 if (test_bit(R5_ReadError, &dev->flags)
5148 && !test_bit(R5_LOCKED, &dev->flags)
5149 && test_bit(R5_UPTODATE, &dev->flags)
5150 ) {
5151 if (!test_bit(R5_ReWrite, &dev->flags)) {
5152 set_bit(R5_Wantwrite, &dev->flags);
5153 set_bit(R5_ReWrite, &dev->flags);
3a31cf3d 5154 } else
c5a31000
N
5155 /* let's read it back */
5156 set_bit(R5_Wantread, &dev->flags);
3a31cf3d
GJ
5157 set_bit(R5_LOCKED, &dev->flags);
5158 s.locked++;
c5a31000
N
5159 }
5160 }
5161
3687c061
N
5162 /* Finish reconstruct operations initiated by the expansion process */
5163 if (sh->reconstruct_state == reconstruct_state_result) {
5164 struct stripe_head *sh_src
6d036f7d 5165 = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
3687c061
N
5166 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
5167 /* sh cannot be written until sh_src has been read.
5168 * so arrange for sh to be delayed a little
5169 */
5170 set_bit(STRIPE_DELAYED, &sh->state);
5171 set_bit(STRIPE_HANDLE, &sh->state);
5172 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
5173 &sh_src->state))
5174 atomic_inc(&conf->preread_active_stripes);
6d036f7d 5175 raid5_release_stripe(sh_src);
3687c061
N
5176 goto finish;
5177 }
5178 if (sh_src)
6d036f7d 5179 raid5_release_stripe(sh_src);
3687c061
N
5180
5181 sh->reconstruct_state = reconstruct_state_idle;
5182 clear_bit(STRIPE_EXPANDING, &sh->state);
5183 for (i = conf->raid_disks; i--; ) {
5184 set_bit(R5_Wantwrite, &sh->dev[i].flags);
5185 set_bit(R5_LOCKED, &sh->dev[i].flags);
5186 s.locked++;
5187 }
5188 }
f416885e 5189
3687c061
N
5190 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
5191 !sh->reconstruct_state) {
5192 /* Need to write out all blocks after computing parity */
5193 sh->disks = conf->raid_disks;
5194 stripe_set_idx(sh->sector, conf, 0, sh);
5195 schedule_reconstruction(sh, &s, 1, 1);
5196 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
5197 clear_bit(STRIPE_EXPAND_READY, &sh->state);
5198 atomic_dec(&conf->reshape_stripes);
5199 wake_up(&conf->wait_for_overlap);
c911c46c 5200 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1);
3687c061
N
5201 }
5202
5203 if (s.expanding && s.locked == 0 &&
5204 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
5205 handle_stripe_expansion(conf, sh);
16a53ecc 5206
3687c061 5207finish:
6bfe0b49 5208 /* wait for this device to become unblocked */
5f066c63
N
5209 if (unlikely(s.blocked_rdev)) {
5210 if (conf->mddev->external)
5211 md_wait_for_blocked_rdev(s.blocked_rdev,
5212 conf->mddev);
5213 else
5214 /* Internal metadata will immediately
5215 * be written by raid5d, so we don't
5216 * need to wait here.
5217 */
5218 rdev_dec_pending(s.blocked_rdev,
5219 conf->mddev);
5220 }
6bfe0b49 5221
bc2607f3
N
5222 if (s.handle_bad_blocks)
5223 for (i = disks; i--; ) {
3cb03002 5224 struct md_rdev *rdev;
bc2607f3
N
5225 struct r5dev *dev = &sh->dev[i];
5226 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
5227 /* We own a safe reference to the rdev */
5228 rdev = conf->disks[i].rdev;
5229 if (!rdev_set_badblocks(rdev, sh->sector,
c911c46c 5230 RAID5_STRIPE_SECTORS(conf), 0))
bc2607f3
N
5231 md_error(conf->mddev, rdev);
5232 rdev_dec_pending(rdev, conf->mddev);
5233 }
b84db560
N
5234 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
5235 rdev = conf->disks[i].rdev;
5236 rdev_clear_badblocks(rdev, sh->sector,
c911c46c 5237 RAID5_STRIPE_SECTORS(conf), 0);
b84db560
N
5238 rdev_dec_pending(rdev, conf->mddev);
5239 }
977df362
N
5240 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
5241 rdev = conf->disks[i].replacement;
dd054fce
N
5242 if (!rdev)
5243 /* rdev have been moved down */
5244 rdev = conf->disks[i].rdev;
977df362 5245 rdev_clear_badblocks(rdev, sh->sector,
c911c46c 5246 RAID5_STRIPE_SECTORS(conf), 0);
977df362
N
5247 rdev_dec_pending(rdev, conf->mddev);
5248 }
bc2607f3
N
5249 }
5250
6c0069c0
YT
5251 if (s.ops_request)
5252 raid_run_ops(sh, s.ops_request);
5253
f0e43bcd 5254 ops_run_io(sh, &s);
16a53ecc 5255
c5709ef6 5256 if (s.dec_preread_active) {
729a1866 5257 /* We delay this until after ops_run_io so that if make_request
e9c7469b 5258 * is waiting on a flush, it won't continue until the writes
729a1866
N
5259 * have actually been submitted.
5260 */
5261 atomic_dec(&conf->preread_active_stripes);
5262 if (atomic_read(&conf->preread_active_stripes) <
5263 IO_THRESHOLD)
5264 md_wakeup_thread(conf->mddev->thread);
5265 }
5266
257a4b42 5267 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
16a53ecc
N
5268}
5269
d1688a6d 5270static void raid5_activate_delayed(struct r5conf *conf)
16a53ecc
N
5271{
5272 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
5273 while (!list_empty(&conf->delayed_list)) {
5274 struct list_head *l = conf->delayed_list.next;
5275 struct stripe_head *sh;
5276 sh = list_entry(l, struct stripe_head, lru);
5277 list_del_init(l);
5278 clear_bit(STRIPE_DELAYED, &sh->state);
5279 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5280 atomic_inc(&conf->preread_active_stripes);
8b3e6cdc 5281 list_add_tail(&sh->lru, &conf->hold_list);
851c30c9 5282 raid5_wakeup_stripe_thread(sh);
16a53ecc 5283 }
482c0834 5284 }
16a53ecc
N
5285}
5286
566c09c5
SL
5287static void activate_bit_delay(struct r5conf *conf,
5288 struct list_head *temp_inactive_list)
16a53ecc
N
5289{
5290 /* device_lock is held */
5291 struct list_head head;
5292 list_add(&head, &conf->bitmap_list);
5293 list_del_init(&conf->bitmap_list);
5294 while (!list_empty(&head)) {
5295 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
566c09c5 5296 int hash;
16a53ecc
N
5297 list_del_init(&sh->lru);
5298 atomic_inc(&sh->count);
566c09c5
SL
5299 hash = sh->hash_lock_index;
5300 __release_stripe(conf, sh, &temp_inactive_list[hash]);
16a53ecc
N
5301 }
5302}
5303
fd01b88c 5304static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
f679623f 5305{
3cb5edf4 5306 struct r5conf *conf = mddev->private;
10433d04 5307 sector_t sector = bio->bi_iter.bi_sector;
3cb5edf4 5308 unsigned int chunk_sectors;
aa8b57aa 5309 unsigned int bio_sectors = bio_sectors(bio);
f679623f 5310
3cb5edf4 5311 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
f679623f
RBJ
5312 return chunk_sectors >=
5313 ((sector & (chunk_sectors - 1)) + bio_sectors);
5314}
5315
46031f9a
RBJ
5316/*
5317 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
5318 * later sampled by raid5d.
5319 */
d1688a6d 5320static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
46031f9a
RBJ
5321{
5322 unsigned long flags;
5323
5324 spin_lock_irqsave(&conf->device_lock, flags);
5325
5326 bi->bi_next = conf->retry_read_aligned_list;
5327 conf->retry_read_aligned_list = bi;
5328
5329 spin_unlock_irqrestore(&conf->device_lock, flags);
5330 md_wakeup_thread(conf->mddev->thread);
5331}
5332
0472a42b
N
5333static struct bio *remove_bio_from_retry(struct r5conf *conf,
5334 unsigned int *offset)
46031f9a
RBJ
5335{
5336 struct bio *bi;
5337
5338 bi = conf->retry_read_aligned;
5339 if (bi) {
0472a42b 5340 *offset = conf->retry_read_offset;
46031f9a
RBJ
5341 conf->retry_read_aligned = NULL;
5342 return bi;
5343 }
5344 bi = conf->retry_read_aligned_list;
5345 if(bi) {
387bb173 5346 conf->retry_read_aligned_list = bi->bi_next;
46031f9a 5347 bi->bi_next = NULL;
0472a42b 5348 *offset = 0;
46031f9a
RBJ
5349 }
5350
5351 return bi;
5352}
5353
f679623f
RBJ
5354/*
5355 * The "raid5_align_endio" should check if the read succeeded and if it
5356 * did, call bio_endio on the original bio (having bio_put the new bio
5357 * first).
5358 * If the read failed..
5359 */
4246a0b6 5360static void raid5_align_endio(struct bio *bi)
f679623f 5361{
1147f58e
GJ
5362 struct md_io_acct *md_io_acct = bi->bi_private;
5363 struct bio *raid_bi = md_io_acct->orig_bio;
fd01b88c 5364 struct mddev *mddev;
d1688a6d 5365 struct r5conf *conf;
3cb03002 5366 struct md_rdev *rdev;
4e4cbee9 5367 blk_status_t error = bi->bi_status;
1147f58e 5368 unsigned long start_time = md_io_acct->start_time;
46031f9a 5369
f679623f 5370 bio_put(bi);
46031f9a 5371
46031f9a
RBJ
5372 rdev = (void*)raid_bi->bi_next;
5373 raid_bi->bi_next = NULL;
2b7f2228
N
5374 mddev = rdev->mddev;
5375 conf = mddev->private;
46031f9a
RBJ
5376
5377 rdev_dec_pending(rdev, conf->mddev);
5378
9b81c842 5379 if (!error) {
1147f58e
GJ
5380 if (blk_queue_io_stat(raid_bi->bi_bdev->bd_disk->queue))
5381 bio_end_io_acct(raid_bi, start_time);
4246a0b6 5382 bio_endio(raid_bi);
46031f9a 5383 if (atomic_dec_and_test(&conf->active_aligned_reads))
b1b46486 5384 wake_up(&conf->wait_for_quiescent);
6712ecf8 5385 return;
46031f9a
RBJ
5386 }
5387
45b4233c 5388 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
46031f9a
RBJ
5389
5390 add_bio_to_retry(raid_bi, conf);
f679623f
RBJ
5391}
5392
7ef6b12a 5393static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
f679623f 5394{
d1688a6d 5395 struct r5conf *conf = mddev->private;
e82ed3a4 5396 struct bio *align_bio;
3cb03002 5397 struct md_rdev *rdev;
e82ed3a4
CH
5398 sector_t sector, end_sector, first_bad;
5399 int bad_sectors, dd_idx;
1147f58e 5400 struct md_io_acct *md_io_acct;
97ae2725 5401 bool did_inc;
f679623f
RBJ
5402
5403 if (!in_chunk_boundary(mddev, raid_bio)) {
7ef6b12a 5404 pr_debug("%s: non aligned\n", __func__);
f679623f
RBJ
5405 return 0;
5406 }
f679623f 5407
e82ed3a4
CH
5408 sector = raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector, 0,
5409 &dd_idx, NULL);
5410 end_sector = bio_end_sector(raid_bio);
5411
f679623f 5412 rcu_read_lock();
e82ed3a4
CH
5413 if (r5c_big_stripe_cached(conf, sector))
5414 goto out_rcu_unlock;
5415
671488cc
N
5416 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
5417 if (!rdev || test_bit(Faulty, &rdev->flags) ||
5418 rdev->recovery_offset < end_sector) {
5419 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
e82ed3a4
CH
5420 if (!rdev)
5421 goto out_rcu_unlock;
5422 if (test_bit(Faulty, &rdev->flags) ||
671488cc 5423 !(test_bit(In_sync, &rdev->flags) ||
e82ed3a4
CH
5424 rdev->recovery_offset >= end_sector))
5425 goto out_rcu_unlock;
671488cc 5426 }
03b047f4 5427
e82ed3a4
CH
5428 atomic_inc(&rdev->nr_pending);
5429 rcu_read_unlock();
5430
c82aa1b7 5431 if (is_badblock(rdev, sector, bio_sectors(raid_bio), &first_bad,
e82ed3a4 5432 &bad_sectors)) {
c82aa1b7 5433 bio_put(raid_bio);
e82ed3a4 5434 rdev_dec_pending(rdev, mddev);
03b047f4
SL
5435 return 0;
5436 }
5437
1147f58e
GJ
5438 align_bio = bio_clone_fast(raid_bio, GFP_NOIO, &mddev->io_acct_set);
5439 md_io_acct = container_of(align_bio, struct md_io_acct, bio_clone);
5440 raid_bio->bi_next = (void *)rdev;
5441 if (blk_queue_io_stat(raid_bio->bi_bdev->bd_disk->queue))
5442 md_io_acct->start_time = bio_start_io_acct(raid_bio);
5443 md_io_acct->orig_bio = raid_bio;
5444
e82ed3a4
CH
5445 bio_set_dev(align_bio, rdev->bdev);
5446 align_bio->bi_end_io = raid5_align_endio;
1147f58e 5447 align_bio->bi_private = md_io_acct;
e82ed3a4
CH
5448 align_bio->bi_iter.bi_sector = sector;
5449
e82ed3a4
CH
5450 /* No reshape active, so we can trust rdev->data_offset */
5451 align_bio->bi_iter.bi_sector += rdev->data_offset;
31c176ec 5452
97ae2725
GO
5453 did_inc = false;
5454 if (conf->quiesce == 0) {
5455 atomic_inc(&conf->active_aligned_reads);
5456 did_inc = true;
5457 }
5458 /* need a memory barrier to detect the race with raid5_quiesce() */
5459 if (!did_inc || smp_load_acquire(&conf->quiesce) != 0) {
5460 /* quiesce is in progress, so we need to undo io activation and wait
5461 * for it to finish
5462 */
5463 if (did_inc && atomic_dec_and_test(&conf->active_aligned_reads))
5464 wake_up(&conf->wait_for_quiescent);
5465 spin_lock_irq(&conf->device_lock);
5466 wait_event_lock_irq(conf->wait_for_quiescent, conf->quiesce == 0,
5467 conf->device_lock);
5468 atomic_inc(&conf->active_aligned_reads);
5469 spin_unlock_irq(&conf->device_lock);
5470 }
387bb173 5471
e82ed3a4
CH
5472 if (mddev->gendisk)
5473 trace_block_bio_remap(align_bio, disk_devt(mddev->gendisk),
5474 raid_bio->bi_iter.bi_sector);
5475 submit_bio_noacct(align_bio);
5476 return 1;
6c0544e2 5477
e82ed3a4
CH
5478out_rcu_unlock:
5479 rcu_read_unlock();
5480 return 0;
f679623f
RBJ
5481}
5482
7ef6b12a
ML
5483static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
5484{
5485 struct bio *split;
dd7a8f5d
N
5486 sector_t sector = raid_bio->bi_iter.bi_sector;
5487 unsigned chunk_sects = mddev->chunk_sectors;
5488 unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
7ef6b12a 5489
dd7a8f5d
N
5490 if (sectors < bio_sectors(raid_bio)) {
5491 struct r5conf *conf = mddev->private;
afeee514 5492 split = bio_split(raid_bio, sectors, GFP_NOIO, &conf->bio_split);
dd7a8f5d 5493 bio_chain(split, raid_bio);
ed00aabd 5494 submit_bio_noacct(raid_bio);
dd7a8f5d
N
5495 raid_bio = split;
5496 }
7ef6b12a 5497
dd7a8f5d
N
5498 if (!raid5_read_one_chunk(mddev, raid_bio))
5499 return raid_bio;
7ef6b12a
ML
5500
5501 return NULL;
5502}
5503
8b3e6cdc
DW
5504/* __get_priority_stripe - get the next stripe to process
5505 *
5506 * Full stripe writes are allowed to pass preread active stripes up until
5507 * the bypass_threshold is exceeded. In general the bypass_count
5508 * increments when the handle_list is handled before the hold_list; however, it
5509 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
5510 * stripe with in flight i/o. The bypass_count will be reset when the
5511 * head of the hold_list has changed, i.e. the head was promoted to the
5512 * handle_list.
5513 */
851c30c9 5514static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
8b3e6cdc 5515{
535ae4eb 5516 struct stripe_head *sh, *tmp;
851c30c9 5517 struct list_head *handle_list = NULL;
535ae4eb 5518 struct r5worker_group *wg;
70d466f7
SL
5519 bool second_try = !r5c_is_writeback(conf->log) &&
5520 !r5l_log_disk_error(conf);
5521 bool try_loprio = test_bit(R5C_LOG_TIGHT, &conf->cache_state) ||
5522 r5l_log_disk_error(conf);
851c30c9 5523
535ae4eb
SL
5524again:
5525 wg = NULL;
5526 sh = NULL;
851c30c9 5527 if (conf->worker_cnt_per_group == 0) {
535ae4eb
SL
5528 handle_list = try_loprio ? &conf->loprio_list :
5529 &conf->handle_list;
851c30c9 5530 } else if (group != ANY_GROUP) {
535ae4eb
SL
5531 handle_list = try_loprio ? &conf->worker_groups[group].loprio_list :
5532 &conf->worker_groups[group].handle_list;
bfc90cb0 5533 wg = &conf->worker_groups[group];
851c30c9
SL
5534 } else {
5535 int i;
5536 for (i = 0; i < conf->group_cnt; i++) {
535ae4eb
SL
5537 handle_list = try_loprio ? &conf->worker_groups[i].loprio_list :
5538 &conf->worker_groups[i].handle_list;
bfc90cb0 5539 wg = &conf->worker_groups[i];
851c30c9
SL
5540 if (!list_empty(handle_list))
5541 break;
5542 }
5543 }
8b3e6cdc
DW
5544
5545 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5546 __func__,
851c30c9 5547 list_empty(handle_list) ? "empty" : "busy",
8b3e6cdc
DW
5548 list_empty(&conf->hold_list) ? "empty" : "busy",
5549 atomic_read(&conf->pending_full_writes), conf->bypass_count);
5550
851c30c9
SL
5551 if (!list_empty(handle_list)) {
5552 sh = list_entry(handle_list->next, typeof(*sh), lru);
8b3e6cdc
DW
5553
5554 if (list_empty(&conf->hold_list))
5555 conf->bypass_count = 0;
5556 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
5557 if (conf->hold_list.next == conf->last_hold)
5558 conf->bypass_count++;
5559 else {
5560 conf->last_hold = conf->hold_list.next;
5561 conf->bypass_count -= conf->bypass_threshold;
5562 if (conf->bypass_count < 0)
5563 conf->bypass_count = 0;
5564 }
5565 }
5566 } else if (!list_empty(&conf->hold_list) &&
5567 ((conf->bypass_threshold &&
5568 conf->bypass_count > conf->bypass_threshold) ||
5569 atomic_read(&conf->pending_full_writes) == 0)) {
851c30c9
SL
5570
5571 list_for_each_entry(tmp, &conf->hold_list, lru) {
5572 if (conf->worker_cnt_per_group == 0 ||
5573 group == ANY_GROUP ||
5574 !cpu_online(tmp->cpu) ||
5575 cpu_to_group(tmp->cpu) == group) {
5576 sh = tmp;
5577 break;
5578 }
5579 }
5580
5581 if (sh) {
5582 conf->bypass_count -= conf->bypass_threshold;
5583 if (conf->bypass_count < 0)
5584 conf->bypass_count = 0;
5585 }
bfc90cb0 5586 wg = NULL;
851c30c9
SL
5587 }
5588
535ae4eb
SL
5589 if (!sh) {
5590 if (second_try)
5591 return NULL;
5592 second_try = true;
5593 try_loprio = !try_loprio;
5594 goto again;
5595 }
8b3e6cdc 5596
bfc90cb0
SL
5597 if (wg) {
5598 wg->stripes_cnt--;
5599 sh->group = NULL;
5600 }
8b3e6cdc 5601 list_del_init(&sh->lru);
c7a6d35e 5602 BUG_ON(atomic_inc_return(&sh->count) != 1);
8b3e6cdc
DW
5603 return sh;
5604}
f679623f 5605
8811b596
SL
5606struct raid5_plug_cb {
5607 struct blk_plug_cb cb;
5608 struct list_head list;
566c09c5 5609 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
8811b596
SL
5610};
5611
5612static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
5613{
5614 struct raid5_plug_cb *cb = container_of(
5615 blk_cb, struct raid5_plug_cb, cb);
5616 struct stripe_head *sh;
5617 struct mddev *mddev = cb->cb.data;
5618 struct r5conf *conf = mddev->private;
a9add5d9 5619 int cnt = 0;
566c09c5 5620 int hash;
8811b596
SL
5621
5622 if (cb->list.next && !list_empty(&cb->list)) {
5623 spin_lock_irq(&conf->device_lock);
5624 while (!list_empty(&cb->list)) {
5625 sh = list_first_entry(&cb->list, struct stripe_head, lru);
5626 list_del_init(&sh->lru);
5627 /*
5628 * avoid race release_stripe_plug() sees
5629 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5630 * is still in our list
5631 */
4e857c58 5632 smp_mb__before_atomic();
8811b596 5633 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
773ca82f
SL
5634 /*
5635 * STRIPE_ON_RELEASE_LIST could be set here. In that
5636 * case, the count is always > 1 here
5637 */
566c09c5
SL
5638 hash = sh->hash_lock_index;
5639 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
a9add5d9 5640 cnt++;
8811b596
SL
5641 }
5642 spin_unlock_irq(&conf->device_lock);
5643 }
566c09c5
SL
5644 release_inactive_stripe_list(conf, cb->temp_inactive_list,
5645 NR_STRIPE_HASH_LOCKS);
e3620a3a
JB
5646 if (mddev->queue)
5647 trace_block_unplug(mddev->queue, cnt, !from_schedule);
8811b596
SL
5648 kfree(cb);
5649}
5650
5651static void release_stripe_plug(struct mddev *mddev,
5652 struct stripe_head *sh)
5653{
5654 struct blk_plug_cb *blk_cb = blk_check_plugged(
5655 raid5_unplug, mddev,
5656 sizeof(struct raid5_plug_cb));
5657 struct raid5_plug_cb *cb;
5658
5659 if (!blk_cb) {
6d036f7d 5660 raid5_release_stripe(sh);
8811b596
SL
5661 return;
5662 }
5663
5664 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
5665
566c09c5
SL
5666 if (cb->list.next == NULL) {
5667 int i;
8811b596 5668 INIT_LIST_HEAD(&cb->list);
566c09c5
SL
5669 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5670 INIT_LIST_HEAD(cb->temp_inactive_list + i);
5671 }
8811b596
SL
5672
5673 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
5674 list_add_tail(&sh->lru, &cb->list);
5675 else
6d036f7d 5676 raid5_release_stripe(sh);
8811b596
SL
5677}
5678
620125f2
SL
5679static void make_discard_request(struct mddev *mddev, struct bio *bi)
5680{
5681 struct r5conf *conf = mddev->private;
5682 sector_t logical_sector, last_sector;
5683 struct stripe_head *sh;
620125f2
SL
5684 int stripe_sectors;
5685
5686 if (mddev->reshape_position != MaxSector)
5687 /* Skip discard while reshape is happening */
5688 return;
5689
c911c46c 5690 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
b0f01ecf 5691 last_sector = bio_end_sector(bi);
620125f2
SL
5692
5693 bi->bi_next = NULL;
620125f2
SL
5694
5695 stripe_sectors = conf->chunk_sectors *
5696 (conf->raid_disks - conf->max_degraded);
5697 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5698 stripe_sectors);
5699 sector_div(last_sector, stripe_sectors);
5700
5701 logical_sector *= conf->chunk_sectors;
5702 last_sector *= conf->chunk_sectors;
5703
5704 for (; logical_sector < last_sector;
c911c46c 5705 logical_sector += RAID5_STRIPE_SECTORS(conf)) {
620125f2
SL
5706 DEFINE_WAIT(w);
5707 int d;
5708 again:
6d036f7d 5709 sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
620125f2
SL
5710 prepare_to_wait(&conf->wait_for_overlap, &w,
5711 TASK_UNINTERRUPTIBLE);
f8dfcffd
N
5712 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5713 if (test_bit(STRIPE_SYNCING, &sh->state)) {
6d036f7d 5714 raid5_release_stripe(sh);
f8dfcffd
N
5715 schedule();
5716 goto again;
5717 }
5718 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
620125f2
SL
5719 spin_lock_irq(&sh->stripe_lock);
5720 for (d = 0; d < conf->raid_disks; d++) {
5721 if (d == sh->pd_idx || d == sh->qd_idx)
5722 continue;
5723 if (sh->dev[d].towrite || sh->dev[d].toread) {
5724 set_bit(R5_Overlap, &sh->dev[d].flags);
5725 spin_unlock_irq(&sh->stripe_lock);
6d036f7d 5726 raid5_release_stripe(sh);
620125f2
SL
5727 schedule();
5728 goto again;
5729 }
5730 }
f8dfcffd 5731 set_bit(STRIPE_DISCARD, &sh->state);
620125f2 5732 finish_wait(&conf->wait_for_overlap, &w);
7a87f434 5733 sh->overwrite_disks = 0;
620125f2
SL
5734 for (d = 0; d < conf->raid_disks; d++) {
5735 if (d == sh->pd_idx || d == sh->qd_idx)
5736 continue;
5737 sh->dev[d].towrite = bi;
5738 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
016c76ac 5739 bio_inc_remaining(bi);
49728050 5740 md_write_inc(mddev, bi);
7a87f434 5741 sh->overwrite_disks++;
620125f2
SL
5742 }
5743 spin_unlock_irq(&sh->stripe_lock);
5744 if (conf->mddev->bitmap) {
5745 for (d = 0;
5746 d < conf->raid_disks - conf->max_degraded;
5747 d++)
e64e4018
AS
5748 md_bitmap_startwrite(mddev->bitmap,
5749 sh->sector,
c911c46c 5750 RAID5_STRIPE_SECTORS(conf),
e64e4018 5751 0);
620125f2
SL
5752 sh->bm_seq = conf->seq_flush + 1;
5753 set_bit(STRIPE_BIT_DELAY, &sh->state);
5754 }
5755
5756 set_bit(STRIPE_HANDLE, &sh->state);
5757 clear_bit(STRIPE_DELAYED, &sh->state);
5758 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5759 atomic_inc(&conf->preread_active_stripes);
5760 release_stripe_plug(mddev, sh);
5761 }
5762
016c76ac 5763 bio_endio(bi);
620125f2
SL
5764}
5765
cc27b0c7 5766static bool raid5_make_request(struct mddev *mddev, struct bio * bi)
1da177e4 5767{
d1688a6d 5768 struct r5conf *conf = mddev->private;
911d4ee8 5769 int dd_idx;
1da177e4
LT
5770 sector_t new_sector;
5771 sector_t logical_sector, last_sector;
5772 struct stripe_head *sh;
a362357b 5773 const int rw = bio_data_dir(bi);
27c0f68f
SL
5774 DEFINE_WAIT(w);
5775 bool do_prepare;
3bddb7f8 5776 bool do_flush = false;
1da177e4 5777
1eff9d32 5778 if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
1532d9e8 5779 int ret = log_handle_flush_request(conf, bi);
828cbe98
SL
5780
5781 if (ret == 0)
cc27b0c7 5782 return true;
828cbe98 5783 if (ret == -ENODEV) {
775d7831
DJ
5784 if (md_flush_request(mddev, bi))
5785 return true;
828cbe98
SL
5786 }
5787 /* ret == -EAGAIN, fallback */
3bddb7f8
SL
5788 /*
5789 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
5790 * we need to flush journal device
5791 */
5792 do_flush = bi->bi_opf & REQ_PREFLUSH;
e5dcdd80
N
5793 }
5794
cc27b0c7
N
5795 if (!md_write_start(mddev, bi))
5796 return false;
9ffc8f7c
EM
5797 /*
5798 * If array is degraded, better not do chunk aligned read because
5799 * later we might have to read it again in order to reconstruct
5800 * data on failed drives.
5801 */
5802 if (rw == READ && mddev->degraded == 0 &&
7ef6b12a
ML
5803 mddev->reshape_position == MaxSector) {
5804 bi = chunk_aligned_read(mddev, bi);
5805 if (!bi)
cc27b0c7 5806 return true;
7ef6b12a 5807 }
52488615 5808
796a5cf0 5809 if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
620125f2 5810 make_discard_request(mddev, bi);
cc27b0c7
N
5811 md_write_end(mddev);
5812 return true;
620125f2
SL
5813 }
5814
c911c46c 5815 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
f73a1c7d 5816 last_sector = bio_end_sector(bi);
1da177e4 5817 bi->bi_next = NULL;
06d91a5f 5818
1147f58e 5819 md_account_bio(mddev, &bi);
27c0f68f 5820 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
c911c46c 5821 for (; logical_sector < last_sector; logical_sector += RAID5_STRIPE_SECTORS(conf)) {
b5663ba4 5822 int previous;
c46501b2 5823 int seq;
b578d55f 5824
27c0f68f 5825 do_prepare = false;
7ecaa1e6 5826 retry:
c46501b2 5827 seq = read_seqcount_begin(&conf->gen_lock);
b5663ba4 5828 previous = 0;
27c0f68f
SL
5829 if (do_prepare)
5830 prepare_to_wait(&conf->wait_for_overlap, &w,
5831 TASK_UNINTERRUPTIBLE);
b0f9ec04 5832 if (unlikely(conf->reshape_progress != MaxSector)) {
fef9c61f 5833 /* spinlock is needed as reshape_progress may be
df8e7f76
N
5834 * 64bit on a 32bit platform, and so it might be
5835 * possible to see a half-updated value
aeb878b0 5836 * Of course reshape_progress could change after
df8e7f76
N
5837 * the lock is dropped, so once we get a reference
5838 * to the stripe that we think it is, we will have
5839 * to check again.
5840 */
7ecaa1e6 5841 spin_lock_irq(&conf->device_lock);
2c810cdd 5842 if (mddev->reshape_backwards
fef9c61f
N
5843 ? logical_sector < conf->reshape_progress
5844 : logical_sector >= conf->reshape_progress) {
b5663ba4
N
5845 previous = 1;
5846 } else {
2c810cdd 5847 if (mddev->reshape_backwards
fef9c61f
N
5848 ? logical_sector < conf->reshape_safe
5849 : logical_sector >= conf->reshape_safe) {
b578d55f
N
5850 spin_unlock_irq(&conf->device_lock);
5851 schedule();
27c0f68f 5852 do_prepare = true;
b578d55f
N
5853 goto retry;
5854 }
5855 }
7ecaa1e6
N
5856 spin_unlock_irq(&conf->device_lock);
5857 }
16a53ecc 5858
112bf897
N
5859 new_sector = raid5_compute_sector(conf, logical_sector,
5860 previous,
911d4ee8 5861 &dd_idx, NULL);
849674e4 5862 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
c46501b2 5863 (unsigned long long)new_sector,
1da177e4
LT
5864 (unsigned long long)logical_sector);
5865
6d036f7d 5866 sh = raid5_get_active_stripe(conf, new_sector, previous,
1eff9d32 5867 (bi->bi_opf & REQ_RAHEAD), 0);
1da177e4 5868 if (sh) {
b0f9ec04 5869 if (unlikely(previous)) {
7ecaa1e6 5870 /* expansion might have moved on while waiting for a
df8e7f76
N
5871 * stripe, so we must do the range check again.
5872 * Expansion could still move past after this
5873 * test, but as we are holding a reference to
5874 * 'sh', we know that if that happens,
5875 * STRIPE_EXPANDING will get set and the expansion
5876 * won't proceed until we finish with the stripe.
7ecaa1e6
N
5877 */
5878 int must_retry = 0;
5879 spin_lock_irq(&conf->device_lock);
2c810cdd 5880 if (mddev->reshape_backwards
b0f9ec04
N
5881 ? logical_sector >= conf->reshape_progress
5882 : logical_sector < conf->reshape_progress)
7ecaa1e6
N
5883 /* mismatch, need to try again */
5884 must_retry = 1;
5885 spin_unlock_irq(&conf->device_lock);
5886 if (must_retry) {
6d036f7d 5887 raid5_release_stripe(sh);
7a3ab908 5888 schedule();
27c0f68f 5889 do_prepare = true;
7ecaa1e6
N
5890 goto retry;
5891 }
5892 }
c46501b2
N
5893 if (read_seqcount_retry(&conf->gen_lock, seq)) {
5894 /* Might have got the wrong stripe_head
5895 * by accident
5896 */
6d036f7d 5897 raid5_release_stripe(sh);
c46501b2
N
5898 goto retry;
5899 }
e62e58a5 5900
7ecaa1e6 5901 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
da41ba65 5902 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
7ecaa1e6
N
5903 /* Stripe is busy expanding or
5904 * add failed due to overlap. Flush everything
1da177e4
LT
5905 * and wait a while
5906 */
482c0834 5907 md_wakeup_thread(mddev->thread);
6d036f7d 5908 raid5_release_stripe(sh);
1da177e4 5909 schedule();
27c0f68f 5910 do_prepare = true;
1da177e4
LT
5911 goto retry;
5912 }
3bddb7f8
SL
5913 if (do_flush) {
5914 set_bit(STRIPE_R5C_PREFLUSH, &sh->state);
5915 /* we only need flush for one stripe */
5916 do_flush = false;
5917 }
5918
1684e975 5919 set_bit(STRIPE_HANDLE, &sh->state);
6ed3003c 5920 clear_bit(STRIPE_DELAYED, &sh->state);
59fc630b 5921 if ((!sh->batch_head || sh == sh->batch_head) &&
1eff9d32 5922 (bi->bi_opf & REQ_SYNC) &&
729a1866
N
5923 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5924 atomic_inc(&conf->preread_active_stripes);
8811b596 5925 release_stripe_plug(mddev, sh);
1da177e4
LT
5926 } else {
5927 /* cannot get stripe for read-ahead, just give-up */
4e4cbee9 5928 bi->bi_status = BLK_STS_IOERR;
1da177e4
LT
5929 break;
5930 }
1da177e4 5931 }
27c0f68f 5932 finish_wait(&conf->wait_for_overlap, &w);
7c13edc8 5933
49728050
N
5934 if (rw == WRITE)
5935 md_write_end(mddev);
016c76ac 5936 bio_endio(bi);
cc27b0c7 5937 return true;
1da177e4
LT
5938}
5939
fd01b88c 5940static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
b522adcd 5941
fd01b88c 5942static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
1da177e4 5943{
52c03291
N
5944 /* reshaping is quite different to recovery/resync so it is
5945 * handled quite separately ... here.
5946 *
5947 * On each call to sync_request, we gather one chunk worth of
5948 * destination stripes and flag them as expanding.
5949 * Then we find all the source stripes and request reads.
5950 * As the reads complete, handle_stripe will copy the data
5951 * into the destination stripe and release that stripe.
5952 */
d1688a6d 5953 struct r5conf *conf = mddev->private;
1da177e4 5954 struct stripe_head *sh;
db0505d3 5955 struct md_rdev *rdev;
ccfcc3c1 5956 sector_t first_sector, last_sector;
f416885e
N
5957 int raid_disks = conf->previous_raid_disks;
5958 int data_disks = raid_disks - conf->max_degraded;
5959 int new_data_disks = conf->raid_disks - conf->max_degraded;
52c03291
N
5960 int i;
5961 int dd_idx;
c8f517c4 5962 sector_t writepos, readpos, safepos;
ec32a2bd 5963 sector_t stripe_addr;
7a661381 5964 int reshape_sectors;
ab69ae12 5965 struct list_head stripes;
92140480 5966 sector_t retn;
52c03291 5967
fef9c61f
N
5968 if (sector_nr == 0) {
5969 /* If restarting in the middle, skip the initial sectors */
2c810cdd 5970 if (mddev->reshape_backwards &&
fef9c61f
N
5971 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
5972 sector_nr = raid5_size(mddev, 0, 0)
5973 - conf->reshape_progress;
6cbd8148
N
5974 } else if (mddev->reshape_backwards &&
5975 conf->reshape_progress == MaxSector) {
5976 /* shouldn't happen, but just in case, finish up.*/
5977 sector_nr = MaxSector;
2c810cdd 5978 } else if (!mddev->reshape_backwards &&
fef9c61f
N
5979 conf->reshape_progress > 0)
5980 sector_nr = conf->reshape_progress;
f416885e 5981 sector_div(sector_nr, new_data_disks);
fef9c61f 5982 if (sector_nr) {
8dee7211 5983 mddev->curr_resync_completed = sector_nr;
e1a86dbb 5984 sysfs_notify_dirent_safe(mddev->sysfs_completed);
fef9c61f 5985 *skipped = 1;
92140480
N
5986 retn = sector_nr;
5987 goto finish;
fef9c61f 5988 }
52c03291
N
5989 }
5990
7a661381
N
5991 /* We need to process a full chunk at a time.
5992 * If old and new chunk sizes differ, we need to process the
5993 * largest of these
5994 */
3cb5edf4
N
5995
5996 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
7a661381 5997
b5254dd5
N
5998 /* We update the metadata at least every 10 seconds, or when
5999 * the data about to be copied would over-write the source of
6000 * the data at the front of the range. i.e. one new_stripe
6001 * along from reshape_progress new_maps to after where
6002 * reshape_safe old_maps to
52c03291 6003 */
fef9c61f 6004 writepos = conf->reshape_progress;
f416885e 6005 sector_div(writepos, new_data_disks);
c8f517c4
N
6006 readpos = conf->reshape_progress;
6007 sector_div(readpos, data_disks);
fef9c61f 6008 safepos = conf->reshape_safe;
f416885e 6009 sector_div(safepos, data_disks);
2c810cdd 6010 if (mddev->reshape_backwards) {
c74c0d76
N
6011 BUG_ON(writepos < reshape_sectors);
6012 writepos -= reshape_sectors;
c8f517c4 6013 readpos += reshape_sectors;
7a661381 6014 safepos += reshape_sectors;
fef9c61f 6015 } else {
7a661381 6016 writepos += reshape_sectors;
c74c0d76
N
6017 /* readpos and safepos are worst-case calculations.
6018 * A negative number is overly pessimistic, and causes
6019 * obvious problems for unsigned storage. So clip to 0.
6020 */
ed37d83e
N
6021 readpos -= min_t(sector_t, reshape_sectors, readpos);
6022 safepos -= min_t(sector_t, reshape_sectors, safepos);
fef9c61f 6023 }
52c03291 6024
b5254dd5
N
6025 /* Having calculated the 'writepos' possibly use it
6026 * to set 'stripe_addr' which is where we will write to.
6027 */
6028 if (mddev->reshape_backwards) {
6029 BUG_ON(conf->reshape_progress == 0);
6030 stripe_addr = writepos;
6031 BUG_ON((mddev->dev_sectors &
6032 ~((sector_t)reshape_sectors - 1))
6033 - reshape_sectors - stripe_addr
6034 != sector_nr);
6035 } else {
6036 BUG_ON(writepos != sector_nr + reshape_sectors);
6037 stripe_addr = sector_nr;
6038 }
6039
c8f517c4
N
6040 /* 'writepos' is the most advanced device address we might write.
6041 * 'readpos' is the least advanced device address we might read.
6042 * 'safepos' is the least address recorded in the metadata as having
6043 * been reshaped.
b5254dd5
N
6044 * If there is a min_offset_diff, these are adjusted either by
6045 * increasing the safepos/readpos if diff is negative, or
6046 * increasing writepos if diff is positive.
6047 * If 'readpos' is then behind 'writepos', there is no way that we can
c8f517c4
N
6048 * ensure safety in the face of a crash - that must be done by userspace
6049 * making a backup of the data. So in that case there is no particular
6050 * rush to update metadata.
6051 * Otherwise if 'safepos' is behind 'writepos', then we really need to
6052 * update the metadata to advance 'safepos' to match 'readpos' so that
6053 * we can be safe in the event of a crash.
6054 * So we insist on updating metadata if safepos is behind writepos and
6055 * readpos is beyond writepos.
6056 * In any case, update the metadata every 10 seconds.
6057 * Maybe that number should be configurable, but I'm not sure it is
6058 * worth it.... maybe it could be a multiple of safemode_delay???
6059 */
b5254dd5
N
6060 if (conf->min_offset_diff < 0) {
6061 safepos += -conf->min_offset_diff;
6062 readpos += -conf->min_offset_diff;
6063 } else
6064 writepos += conf->min_offset_diff;
6065
2c810cdd 6066 if ((mddev->reshape_backwards
c8f517c4
N
6067 ? (safepos > writepos && readpos < writepos)
6068 : (safepos < writepos && readpos > writepos)) ||
6069 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
52c03291
N
6070 /* Cannot proceed until we've updated the superblock... */
6071 wait_event(conf->wait_for_overlap,
c91abf5a
N
6072 atomic_read(&conf->reshape_stripes)==0
6073 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6074 if (atomic_read(&conf->reshape_stripes) != 0)
6075 return 0;
fef9c61f 6076 mddev->reshape_position = conf->reshape_progress;
75d3da43 6077 mddev->curr_resync_completed = sector_nr;
db0505d3
N
6078 if (!mddev->reshape_backwards)
6079 /* Can update recovery_offset */
6080 rdev_for_each(rdev, mddev)
6081 if (rdev->raid_disk >= 0 &&
6082 !test_bit(Journal, &rdev->flags) &&
6083 !test_bit(In_sync, &rdev->flags) &&
6084 rdev->recovery_offset < sector_nr)
6085 rdev->recovery_offset = sector_nr;
6086
c8f517c4 6087 conf->reshape_checkpoint = jiffies;
2953079c 6088 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
52c03291 6089 md_wakeup_thread(mddev->thread);
2953079c 6090 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
c91abf5a
N
6091 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6092 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6093 return 0;
52c03291 6094 spin_lock_irq(&conf->device_lock);
fef9c61f 6095 conf->reshape_safe = mddev->reshape_position;
52c03291
N
6096 spin_unlock_irq(&conf->device_lock);
6097 wake_up(&conf->wait_for_overlap);
e1a86dbb 6098 sysfs_notify_dirent_safe(mddev->sysfs_completed);
52c03291
N
6099 }
6100
ab69ae12 6101 INIT_LIST_HEAD(&stripes);
c911c46c 6102 for (i = 0; i < reshape_sectors; i += RAID5_STRIPE_SECTORS(conf)) {
52c03291 6103 int j;
a9f326eb 6104 int skipped_disk = 0;
6d036f7d 6105 sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
52c03291
N
6106 set_bit(STRIPE_EXPANDING, &sh->state);
6107 atomic_inc(&conf->reshape_stripes);
6108 /* If any of this stripe is beyond the end of the old
6109 * array, then we need to zero those blocks
6110 */
6111 for (j=sh->disks; j--;) {
6112 sector_t s;
6113 if (j == sh->pd_idx)
6114 continue;
f416885e 6115 if (conf->level == 6 &&
d0dabf7e 6116 j == sh->qd_idx)
f416885e 6117 continue;
6d036f7d 6118 s = raid5_compute_blocknr(sh, j, 0);
b522adcd 6119 if (s < raid5_size(mddev, 0, 0)) {
a9f326eb 6120 skipped_disk = 1;
52c03291
N
6121 continue;
6122 }
c911c46c 6123 memset(page_address(sh->dev[j].page), 0, RAID5_STRIPE_SIZE(conf));
52c03291
N
6124 set_bit(R5_Expanded, &sh->dev[j].flags);
6125 set_bit(R5_UPTODATE, &sh->dev[j].flags);
6126 }
a9f326eb 6127 if (!skipped_disk) {
52c03291
N
6128 set_bit(STRIPE_EXPAND_READY, &sh->state);
6129 set_bit(STRIPE_HANDLE, &sh->state);
6130 }
ab69ae12 6131 list_add(&sh->lru, &stripes);
52c03291
N
6132 }
6133 spin_lock_irq(&conf->device_lock);
2c810cdd 6134 if (mddev->reshape_backwards)
7a661381 6135 conf->reshape_progress -= reshape_sectors * new_data_disks;
fef9c61f 6136 else
7a661381 6137 conf->reshape_progress += reshape_sectors * new_data_disks;
52c03291
N
6138 spin_unlock_irq(&conf->device_lock);
6139 /* Ok, those stripe are ready. We can start scheduling
6140 * reads on the source stripes.
6141 * The source stripes are determined by mapping the first and last
6142 * block on the destination stripes.
6143 */
52c03291 6144 first_sector =
ec32a2bd 6145 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
911d4ee8 6146 1, &dd_idx, NULL);
52c03291 6147 last_sector =
0e6e0271 6148 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
09c9e5fa 6149 * new_data_disks - 1),
911d4ee8 6150 1, &dd_idx, NULL);
58c0fed4
AN
6151 if (last_sector >= mddev->dev_sectors)
6152 last_sector = mddev->dev_sectors - 1;
52c03291 6153 while (first_sector <= last_sector) {
6d036f7d 6154 sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
52c03291
N
6155 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
6156 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 6157 raid5_release_stripe(sh);
c911c46c 6158 first_sector += RAID5_STRIPE_SECTORS(conf);
52c03291 6159 }
ab69ae12
N
6160 /* Now that the sources are clearly marked, we can release
6161 * the destination stripes
6162 */
6163 while (!list_empty(&stripes)) {
6164 sh = list_entry(stripes.next, struct stripe_head, lru);
6165 list_del_init(&sh->lru);
6d036f7d 6166 raid5_release_stripe(sh);
ab69ae12 6167 }
c6207277
N
6168 /* If this takes us to the resync_max point where we have to pause,
6169 * then we need to write out the superblock.
6170 */
7a661381 6171 sector_nr += reshape_sectors;
92140480
N
6172 retn = reshape_sectors;
6173finish:
c5e19d90
N
6174 if (mddev->curr_resync_completed > mddev->resync_max ||
6175 (sector_nr - mddev->curr_resync_completed) * 2
c03f6a19 6176 >= mddev->resync_max - mddev->curr_resync_completed) {
c6207277
N
6177 /* Cannot proceed until we've updated the superblock... */
6178 wait_event(conf->wait_for_overlap,
c91abf5a
N
6179 atomic_read(&conf->reshape_stripes) == 0
6180 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6181 if (atomic_read(&conf->reshape_stripes) != 0)
6182 goto ret;
fef9c61f 6183 mddev->reshape_position = conf->reshape_progress;
75d3da43 6184 mddev->curr_resync_completed = sector_nr;
db0505d3
N
6185 if (!mddev->reshape_backwards)
6186 /* Can update recovery_offset */
6187 rdev_for_each(rdev, mddev)
6188 if (rdev->raid_disk >= 0 &&
6189 !test_bit(Journal, &rdev->flags) &&
6190 !test_bit(In_sync, &rdev->flags) &&
6191 rdev->recovery_offset < sector_nr)
6192 rdev->recovery_offset = sector_nr;
c8f517c4 6193 conf->reshape_checkpoint = jiffies;
2953079c 6194 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
c6207277
N
6195 md_wakeup_thread(mddev->thread);
6196 wait_event(mddev->sb_wait,
2953079c 6197 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)
c91abf5a
N
6198 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6199 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6200 goto ret;
c6207277 6201 spin_lock_irq(&conf->device_lock);
fef9c61f 6202 conf->reshape_safe = mddev->reshape_position;
c6207277
N
6203 spin_unlock_irq(&conf->device_lock);
6204 wake_up(&conf->wait_for_overlap);
e1a86dbb 6205 sysfs_notify_dirent_safe(mddev->sysfs_completed);
c6207277 6206 }
c91abf5a 6207ret:
92140480 6208 return retn;
52c03291
N
6209}
6210
849674e4
SL
6211static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
6212 int *skipped)
52c03291 6213{
d1688a6d 6214 struct r5conf *conf = mddev->private;
52c03291 6215 struct stripe_head *sh;
58c0fed4 6216 sector_t max_sector = mddev->dev_sectors;
57dab0bd 6217 sector_t sync_blocks;
16a53ecc
N
6218 int still_degraded = 0;
6219 int i;
1da177e4 6220
72626685 6221 if (sector_nr >= max_sector) {
1da177e4 6222 /* just being told to finish up .. nothing much to do */
cea9c228 6223
29269553
N
6224 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
6225 end_reshape(conf);
6226 return 0;
6227 }
72626685
N
6228
6229 if (mddev->curr_resync < max_sector) /* aborted */
e64e4018
AS
6230 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
6231 &sync_blocks, 1);
16a53ecc 6232 else /* completed sync */
72626685 6233 conf->fullsync = 0;
e64e4018 6234 md_bitmap_close_sync(mddev->bitmap);
72626685 6235
1da177e4
LT
6236 return 0;
6237 }
ccfcc3c1 6238
64bd660b
N
6239 /* Allow raid5_quiesce to complete */
6240 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
6241
52c03291
N
6242 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6243 return reshape_request(mddev, sector_nr, skipped);
f6705578 6244
c6207277
N
6245 /* No need to check resync_max as we never do more than one
6246 * stripe, and as resync_max will always be on a chunk boundary,
6247 * if the check in md_do_sync didn't fire, there is no chance
6248 * of overstepping resync_max here
6249 */
6250
16a53ecc 6251 /* if there is too many failed drives and we are trying
1da177e4
LT
6252 * to resync, then assert that we are finished, because there is
6253 * nothing we can do.
6254 */
3285edf1 6255 if (mddev->degraded >= conf->max_degraded &&
16a53ecc 6256 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
58c0fed4 6257 sector_t rv = mddev->dev_sectors - sector_nr;
57afd89f 6258 *skipped = 1;
1da177e4
LT
6259 return rv;
6260 }
6f608040 6261 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
6262 !conf->fullsync &&
e64e4018 6263 !md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
c911c46c 6264 sync_blocks >= RAID5_STRIPE_SECTORS(conf)) {
72626685 6265 /* we can skip this block, and probably more */
83c3e5e1 6266 do_div(sync_blocks, RAID5_STRIPE_SECTORS(conf));
72626685 6267 *skipped = 1;
c911c46c
YY
6268 /* keep things rounded to whole stripes */
6269 return sync_blocks * RAID5_STRIPE_SECTORS(conf);
72626685 6270 }
1da177e4 6271
e64e4018 6272 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
b47490c9 6273
6d036f7d 6274 sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
1da177e4 6275 if (sh == NULL) {
6d036f7d 6276 sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
1da177e4 6277 /* make sure we don't swamp the stripe cache if someone else
16a53ecc 6278 * is trying to get access
1da177e4 6279 */
66c006a5 6280 schedule_timeout_uninterruptible(1);
1da177e4 6281 }
16a53ecc 6282 /* Need to check if array will still be degraded after recovery/resync
16d9cfab
EM
6283 * Note in case of > 1 drive failures it's possible we're rebuilding
6284 * one drive while leaving another faulty drive in array.
16a53ecc 6285 */
16d9cfab
EM
6286 rcu_read_lock();
6287 for (i = 0; i < conf->raid_disks; i++) {
6aa7de05 6288 struct md_rdev *rdev = READ_ONCE(conf->disks[i].rdev);
16d9cfab
EM
6289
6290 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
16a53ecc 6291 still_degraded = 1;
16d9cfab
EM
6292 }
6293 rcu_read_unlock();
16a53ecc 6294
e64e4018 6295 md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
16a53ecc 6296
83206d66 6297 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
053f5b65 6298 set_bit(STRIPE_HANDLE, &sh->state);
1da177e4 6299
6d036f7d 6300 raid5_release_stripe(sh);
1da177e4 6301
c911c46c 6302 return RAID5_STRIPE_SECTORS(conf);
1da177e4
LT
6303}
6304
0472a42b
N
6305static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio,
6306 unsigned int offset)
46031f9a
RBJ
6307{
6308 /* We may not be able to submit a whole bio at once as there
6309 * may not be enough stripe_heads available.
6310 * We cannot pre-allocate enough stripe_heads as we may need
6311 * more than exist in the cache (if we allow ever large chunks).
6312 * So we do one stripe head at a time and record in
6313 * ->bi_hw_segments how many have been done.
6314 *
6315 * We *know* that this entire raid_bio is in one chunk, so
6316 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
6317 */
6318 struct stripe_head *sh;
911d4ee8 6319 int dd_idx;
46031f9a
RBJ
6320 sector_t sector, logical_sector, last_sector;
6321 int scnt = 0;
46031f9a
RBJ
6322 int handled = 0;
6323
4f024f37 6324 logical_sector = raid_bio->bi_iter.bi_sector &
c911c46c 6325 ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
112bf897 6326 sector = raid5_compute_sector(conf, logical_sector,
911d4ee8 6327 0, &dd_idx, NULL);
f73a1c7d 6328 last_sector = bio_end_sector(raid_bio);
46031f9a
RBJ
6329
6330 for (; logical_sector < last_sector;
c911c46c
YY
6331 logical_sector += RAID5_STRIPE_SECTORS(conf),
6332 sector += RAID5_STRIPE_SECTORS(conf),
387bb173 6333 scnt++) {
46031f9a 6334
0472a42b 6335 if (scnt < offset)
46031f9a
RBJ
6336 /* already done this stripe */
6337 continue;
6338
6d036f7d 6339 sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
46031f9a
RBJ
6340
6341 if (!sh) {
6342 /* failed to get a stripe - must wait */
46031f9a 6343 conf->retry_read_aligned = raid_bio;
0472a42b 6344 conf->retry_read_offset = scnt;
46031f9a
RBJ
6345 return handled;
6346 }
6347
da41ba65 6348 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
6d036f7d 6349 raid5_release_stripe(sh);
387bb173 6350 conf->retry_read_aligned = raid_bio;
0472a42b 6351 conf->retry_read_offset = scnt;
387bb173
NB
6352 return handled;
6353 }
6354
3f9e7c14 6355 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
36d1c647 6356 handle_stripe(sh);
6d036f7d 6357 raid5_release_stripe(sh);
46031f9a
RBJ
6358 handled++;
6359 }
016c76ac
N
6360
6361 bio_endio(raid_bio);
6362
46031f9a 6363 if (atomic_dec_and_test(&conf->active_aligned_reads))
b1b46486 6364 wake_up(&conf->wait_for_quiescent);
46031f9a
RBJ
6365 return handled;
6366}
6367
bfc90cb0 6368static int handle_active_stripes(struct r5conf *conf, int group,
566c09c5
SL
6369 struct r5worker *worker,
6370 struct list_head *temp_inactive_list)
efcd487c
CH
6371 __releases(&conf->device_lock)
6372 __acquires(&conf->device_lock)
46a06401
SL
6373{
6374 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
566c09c5
SL
6375 int i, batch_size = 0, hash;
6376 bool release_inactive = false;
46a06401
SL
6377
6378 while (batch_size < MAX_STRIPE_BATCH &&
851c30c9 6379 (sh = __get_priority_stripe(conf, group)) != NULL)
46a06401
SL
6380 batch[batch_size++] = sh;
6381
566c09c5
SL
6382 if (batch_size == 0) {
6383 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6384 if (!list_empty(temp_inactive_list + i))
6385 break;
a8c34f91
SL
6386 if (i == NR_STRIPE_HASH_LOCKS) {
6387 spin_unlock_irq(&conf->device_lock);
1532d9e8 6388 log_flush_stripe_to_raid(conf);
a8c34f91 6389 spin_lock_irq(&conf->device_lock);
566c09c5 6390 return batch_size;
a8c34f91 6391 }
566c09c5
SL
6392 release_inactive = true;
6393 }
46a06401
SL
6394 spin_unlock_irq(&conf->device_lock);
6395
566c09c5
SL
6396 release_inactive_stripe_list(conf, temp_inactive_list,
6397 NR_STRIPE_HASH_LOCKS);
6398
a8c34f91 6399 r5l_flush_stripe_to_raid(conf->log);
566c09c5
SL
6400 if (release_inactive) {
6401 spin_lock_irq(&conf->device_lock);
6402 return 0;
6403 }
6404
46a06401
SL
6405 for (i = 0; i < batch_size; i++)
6406 handle_stripe(batch[i]);
ff875738 6407 log_write_stripe_run(conf);
46a06401
SL
6408
6409 cond_resched();
6410
6411 spin_lock_irq(&conf->device_lock);
566c09c5
SL
6412 for (i = 0; i < batch_size; i++) {
6413 hash = batch[i]->hash_lock_index;
6414 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
6415 }
46a06401
SL
6416 return batch_size;
6417}
46031f9a 6418
851c30c9
SL
6419static void raid5_do_work(struct work_struct *work)
6420{
6421 struct r5worker *worker = container_of(work, struct r5worker, work);
6422 struct r5worker_group *group = worker->group;
6423 struct r5conf *conf = group->conf;
16d997b7 6424 struct mddev *mddev = conf->mddev;
851c30c9
SL
6425 int group_id = group - conf->worker_groups;
6426 int handled;
6427 struct blk_plug plug;
6428
6429 pr_debug("+++ raid5worker active\n");
6430
6431 blk_start_plug(&plug);
6432 handled = 0;
6433 spin_lock_irq(&conf->device_lock);
6434 while (1) {
6435 int batch_size, released;
6436
566c09c5 6437 released = release_stripe_list(conf, worker->temp_inactive_list);
851c30c9 6438
566c09c5
SL
6439 batch_size = handle_active_stripes(conf, group_id, worker,
6440 worker->temp_inactive_list);
bfc90cb0 6441 worker->working = false;
851c30c9
SL
6442 if (!batch_size && !released)
6443 break;
6444 handled += batch_size;
16d997b7
N
6445 wait_event_lock_irq(mddev->sb_wait,
6446 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags),
6447 conf->device_lock);
851c30c9
SL
6448 }
6449 pr_debug("%d stripes handled\n", handled);
6450
6451 spin_unlock_irq(&conf->device_lock);
7e96d559 6452
9c72a18e
SL
6453 flush_deferred_bios(conf);
6454
6455 r5l_flush_stripe_to_raid(conf->log);
6456
7e96d559 6457 async_tx_issue_pending_all();
851c30c9
SL
6458 blk_finish_plug(&plug);
6459
6460 pr_debug("--- raid5worker inactive\n");
6461}
6462
1da177e4
LT
6463/*
6464 * This is our raid5 kernel thread.
6465 *
6466 * We scan the hash table for stripes which can be handled now.
6467 * During the scan, completed stripes are saved for us by the interrupt
6468 * handler, so that they will not have to wait for our next wakeup.
6469 */
4ed8731d 6470static void raid5d(struct md_thread *thread)
1da177e4 6471{
4ed8731d 6472 struct mddev *mddev = thread->mddev;
d1688a6d 6473 struct r5conf *conf = mddev->private;
1da177e4 6474 int handled;
e1dfa0a2 6475 struct blk_plug plug;
1da177e4 6476
45b4233c 6477 pr_debug("+++ raid5d active\n");
1da177e4
LT
6478
6479 md_check_recovery(mddev);
1da177e4 6480
e1dfa0a2 6481 blk_start_plug(&plug);
1da177e4
LT
6482 handled = 0;
6483 spin_lock_irq(&conf->device_lock);
6484 while (1) {
46031f9a 6485 struct bio *bio;
773ca82f 6486 int batch_size, released;
0472a42b 6487 unsigned int offset;
773ca82f 6488
566c09c5 6489 released = release_stripe_list(conf, conf->temp_inactive_list);
edbe83ab
N
6490 if (released)
6491 clear_bit(R5_DID_ALLOC, &conf->cache_state);
1da177e4 6492
0021b7bc 6493 if (
7c13edc8
N
6494 !list_empty(&conf->bitmap_list)) {
6495 /* Now is a good time to flush some bitmap updates */
6496 conf->seq_flush++;
700e432d 6497 spin_unlock_irq(&conf->device_lock);
e64e4018 6498 md_bitmap_unplug(mddev->bitmap);
700e432d 6499 spin_lock_irq(&conf->device_lock);
7c13edc8 6500 conf->seq_write = conf->seq_flush;
566c09c5 6501 activate_bit_delay(conf, conf->temp_inactive_list);
72626685 6502 }
0021b7bc 6503 raid5_activate_delayed(conf);
72626685 6504
0472a42b 6505 while ((bio = remove_bio_from_retry(conf, &offset))) {
46031f9a
RBJ
6506 int ok;
6507 spin_unlock_irq(&conf->device_lock);
0472a42b 6508 ok = retry_aligned_read(conf, bio, offset);
46031f9a
RBJ
6509 spin_lock_irq(&conf->device_lock);
6510 if (!ok)
6511 break;
6512 handled++;
6513 }
6514
566c09c5
SL
6515 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
6516 conf->temp_inactive_list);
773ca82f 6517 if (!batch_size && !released)
1da177e4 6518 break;
46a06401 6519 handled += batch_size;
1da177e4 6520
2953079c 6521 if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) {
46a06401 6522 spin_unlock_irq(&conf->device_lock);
de393cde 6523 md_check_recovery(mddev);
46a06401
SL
6524 spin_lock_irq(&conf->device_lock);
6525 }
1da177e4 6526 }
45b4233c 6527 pr_debug("%d stripes handled\n", handled);
1da177e4
LT
6528
6529 spin_unlock_irq(&conf->device_lock);
2d5b569b
N
6530 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
6531 mutex_trylock(&conf->cache_size_mutex)) {
edbe83ab
N
6532 grow_one_stripe(conf, __GFP_NOWARN);
6533 /* Set flag even if allocation failed. This helps
6534 * slow down allocation requests when mem is short
6535 */
6536 set_bit(R5_DID_ALLOC, &conf->cache_state);
2d5b569b 6537 mutex_unlock(&conf->cache_size_mutex);
edbe83ab 6538 }
1da177e4 6539
765d704d
SL
6540 flush_deferred_bios(conf);
6541
0576b1c6
SL
6542 r5l_flush_stripe_to_raid(conf->log);
6543
c9f21aaf 6544 async_tx_issue_pending_all();
e1dfa0a2 6545 blk_finish_plug(&plug);
1da177e4 6546
45b4233c 6547 pr_debug("--- raid5d inactive\n");
1da177e4
LT
6548}
6549
3f294f4f 6550static ssize_t
fd01b88c 6551raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
3f294f4f 6552{
7b1485ba
N
6553 struct r5conf *conf;
6554 int ret = 0;
6555 spin_lock(&mddev->lock);
6556 conf = mddev->private;
96de1e66 6557 if (conf)
edbe83ab 6558 ret = sprintf(page, "%d\n", conf->min_nr_stripes);
7b1485ba
N
6559 spin_unlock(&mddev->lock);
6560 return ret;
3f294f4f
N
6561}
6562
c41d4ac4 6563int
fd01b88c 6564raid5_set_cache_size(struct mddev *mddev, int size)
3f294f4f 6565{
483cbbed 6566 int result = 0;
d1688a6d 6567 struct r5conf *conf = mddev->private;
b5470dc5 6568
c41d4ac4 6569 if (size <= 16 || size > 32768)
3f294f4f 6570 return -EINVAL;
486f0644 6571
edbe83ab 6572 conf->min_nr_stripes = size;
2d5b569b 6573 mutex_lock(&conf->cache_size_mutex);
486f0644
N
6574 while (size < conf->max_nr_stripes &&
6575 drop_one_stripe(conf))
6576 ;
2d5b569b 6577 mutex_unlock(&conf->cache_size_mutex);
486f0644 6578
2214c260 6579 md_allow_write(mddev);
486f0644 6580
2d5b569b 6581 mutex_lock(&conf->cache_size_mutex);
486f0644 6582 while (size > conf->max_nr_stripes)
483cbbed
AN
6583 if (!grow_one_stripe(conf, GFP_KERNEL)) {
6584 conf->min_nr_stripes = conf->max_nr_stripes;
6585 result = -ENOMEM;
486f0644 6586 break;
483cbbed 6587 }
2d5b569b 6588 mutex_unlock(&conf->cache_size_mutex);
486f0644 6589
483cbbed 6590 return result;
c41d4ac4
N
6591}
6592EXPORT_SYMBOL(raid5_set_cache_size);
6593
6594static ssize_t
fd01b88c 6595raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
c41d4ac4 6596{
6791875e 6597 struct r5conf *conf;
c41d4ac4
N
6598 unsigned long new;
6599 int err;
6600
6601 if (len >= PAGE_SIZE)
6602 return -EINVAL;
b29bebd6 6603 if (kstrtoul(page, 10, &new))
c41d4ac4 6604 return -EINVAL;
6791875e 6605 err = mddev_lock(mddev);
c41d4ac4
N
6606 if (err)
6607 return err;
6791875e
N
6608 conf = mddev->private;
6609 if (!conf)
6610 err = -ENODEV;
6611 else
6612 err = raid5_set_cache_size(mddev, new);
6613 mddev_unlock(mddev);
6614
6615 return err ?: len;
3f294f4f 6616}
007583c9 6617
96de1e66
N
6618static struct md_sysfs_entry
6619raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
6620 raid5_show_stripe_cache_size,
6621 raid5_store_stripe_cache_size);
3f294f4f 6622
d06f191f
MS
6623static ssize_t
6624raid5_show_rmw_level(struct mddev *mddev, char *page)
6625{
6626 struct r5conf *conf = mddev->private;
6627 if (conf)
6628 return sprintf(page, "%d\n", conf->rmw_level);
6629 else
6630 return 0;
6631}
6632
6633static ssize_t
6634raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
6635{
6636 struct r5conf *conf = mddev->private;
6637 unsigned long new;
6638
6639 if (!conf)
6640 return -ENODEV;
6641
6642 if (len >= PAGE_SIZE)
6643 return -EINVAL;
6644
6645 if (kstrtoul(page, 10, &new))
6646 return -EINVAL;
6647
6648 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
6649 return -EINVAL;
6650
6651 if (new != PARITY_DISABLE_RMW &&
6652 new != PARITY_ENABLE_RMW &&
6653 new != PARITY_PREFER_RMW)
6654 return -EINVAL;
6655
6656 conf->rmw_level = new;
6657 return len;
6658}
6659
6660static struct md_sysfs_entry
6661raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
6662 raid5_show_rmw_level,
6663 raid5_store_rmw_level);
6664
3b5408b9
YY
6665static ssize_t
6666raid5_show_stripe_size(struct mddev *mddev, char *page)
6667{
6668 struct r5conf *conf;
6669 int ret = 0;
6670
6671 spin_lock(&mddev->lock);
6672 conf = mddev->private;
6673 if (conf)
6674 ret = sprintf(page, "%lu\n", RAID5_STRIPE_SIZE(conf));
6675 spin_unlock(&mddev->lock);
6676 return ret;
6677}
6678
6679#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
6680static ssize_t
6681raid5_store_stripe_size(struct mddev *mddev, const char *page, size_t len)
6682{
6683 struct r5conf *conf;
6684 unsigned long new;
6685 int err;
38912584 6686 int size;
3b5408b9
YY
6687
6688 if (len >= PAGE_SIZE)
6689 return -EINVAL;
6690 if (kstrtoul(page, 10, &new))
6691 return -EINVAL;
6692
6693 /*
6694 * The value should not be bigger than PAGE_SIZE. It requires to
6af10a33
YY
6695 * be multiple of DEFAULT_STRIPE_SIZE and the value should be power
6696 * of two.
3b5408b9 6697 */
6af10a33
YY
6698 if (new % DEFAULT_STRIPE_SIZE != 0 ||
6699 new > PAGE_SIZE || new == 0 ||
6700 new != roundup_pow_of_two(new))
3b5408b9
YY
6701 return -EINVAL;
6702
6703 err = mddev_lock(mddev);
6704 if (err)
6705 return err;
6706
6707 conf = mddev->private;
6708 if (!conf) {
6709 err = -ENODEV;
6710 goto out_unlock;
6711 }
6712
6713 if (new == conf->stripe_size)
6714 goto out_unlock;
6715
6716 pr_debug("md/raid: change stripe_size from %lu to %lu\n",
6717 conf->stripe_size, new);
6718
38912584
YY
6719 if (mddev->sync_thread ||
6720 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6721 mddev->reshape_position != MaxSector ||
6722 mddev->sysfs_active) {
6723 err = -EBUSY;
6724 goto out_unlock;
6725 }
6726
3b5408b9 6727 mddev_suspend(mddev);
38912584
YY
6728 mutex_lock(&conf->cache_size_mutex);
6729 size = conf->max_nr_stripes;
6730
6731 shrink_stripes(conf);
6732
3b5408b9
YY
6733 conf->stripe_size = new;
6734 conf->stripe_shift = ilog2(new) - 9;
6735 conf->stripe_sectors = new >> 9;
38912584
YY
6736 if (grow_stripes(conf, size)) {
6737 pr_warn("md/raid:%s: couldn't allocate buffers\n",
6738 mdname(mddev));
6739 err = -ENOMEM;
6740 }
6741 mutex_unlock(&conf->cache_size_mutex);
3b5408b9
YY
6742 mddev_resume(mddev);
6743
6744out_unlock:
6745 mddev_unlock(mddev);
6746 return err ?: len;
6747}
6748
6749static struct md_sysfs_entry
6750raid5_stripe_size = __ATTR(stripe_size, 0644,
6751 raid5_show_stripe_size,
6752 raid5_store_stripe_size);
6753#else
6754static struct md_sysfs_entry
6755raid5_stripe_size = __ATTR(stripe_size, 0444,
6756 raid5_show_stripe_size,
6757 NULL);
6758#endif
d06f191f 6759
8b3e6cdc 6760static ssize_t
fd01b88c 6761raid5_show_preread_threshold(struct mddev *mddev, char *page)
8b3e6cdc 6762{
7b1485ba
N
6763 struct r5conf *conf;
6764 int ret = 0;
6765 spin_lock(&mddev->lock);
6766 conf = mddev->private;
8b3e6cdc 6767 if (conf)
7b1485ba
N
6768 ret = sprintf(page, "%d\n", conf->bypass_threshold);
6769 spin_unlock(&mddev->lock);
6770 return ret;
8b3e6cdc
DW
6771}
6772
6773static ssize_t
fd01b88c 6774raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
8b3e6cdc 6775{
6791875e 6776 struct r5conf *conf;
4ef197d8 6777 unsigned long new;
6791875e
N
6778 int err;
6779
8b3e6cdc
DW
6780 if (len >= PAGE_SIZE)
6781 return -EINVAL;
b29bebd6 6782 if (kstrtoul(page, 10, &new))
8b3e6cdc 6783 return -EINVAL;
6791875e
N
6784
6785 err = mddev_lock(mddev);
6786 if (err)
6787 return err;
6788 conf = mddev->private;
6789 if (!conf)
6790 err = -ENODEV;
edbe83ab 6791 else if (new > conf->min_nr_stripes)
6791875e
N
6792 err = -EINVAL;
6793 else
6794 conf->bypass_threshold = new;
6795 mddev_unlock(mddev);
6796 return err ?: len;
8b3e6cdc
DW
6797}
6798
6799static struct md_sysfs_entry
6800raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
6801 S_IRUGO | S_IWUSR,
6802 raid5_show_preread_threshold,
6803 raid5_store_preread_threshold);
6804
d592a996
SL
6805static ssize_t
6806raid5_show_skip_copy(struct mddev *mddev, char *page)
6807{
7b1485ba
N
6808 struct r5conf *conf;
6809 int ret = 0;
6810 spin_lock(&mddev->lock);
6811 conf = mddev->private;
d592a996 6812 if (conf)
7b1485ba
N
6813 ret = sprintf(page, "%d\n", conf->skip_copy);
6814 spin_unlock(&mddev->lock);
6815 return ret;
d592a996
SL
6816}
6817
6818static ssize_t
6819raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
6820{
6791875e 6821 struct r5conf *conf;
d592a996 6822 unsigned long new;
6791875e
N
6823 int err;
6824
d592a996
SL
6825 if (len >= PAGE_SIZE)
6826 return -EINVAL;
d592a996
SL
6827 if (kstrtoul(page, 10, &new))
6828 return -EINVAL;
6829 new = !!new;
6791875e
N
6830
6831 err = mddev_lock(mddev);
6832 if (err)
6833 return err;
6834 conf = mddev->private;
6835 if (!conf)
6836 err = -ENODEV;
6837 else if (new != conf->skip_copy) {
1cb039f3
CH
6838 struct request_queue *q = mddev->queue;
6839
6791875e
N
6840 mddev_suspend(mddev);
6841 conf->skip_copy = new;
6842 if (new)
1cb039f3 6843 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, q);
6791875e 6844 else
1cb039f3 6845 blk_queue_flag_clear(QUEUE_FLAG_STABLE_WRITES, q);
6791875e
N
6846 mddev_resume(mddev);
6847 }
6848 mddev_unlock(mddev);
6849 return err ?: len;
d592a996
SL
6850}
6851
6852static struct md_sysfs_entry
6853raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
6854 raid5_show_skip_copy,
6855 raid5_store_skip_copy);
6856
3f294f4f 6857static ssize_t
fd01b88c 6858stripe_cache_active_show(struct mddev *mddev, char *page)
3f294f4f 6859{
d1688a6d 6860 struct r5conf *conf = mddev->private;
96de1e66
N
6861 if (conf)
6862 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
6863 else
6864 return 0;
3f294f4f
N
6865}
6866
96de1e66
N
6867static struct md_sysfs_entry
6868raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
3f294f4f 6869
b721420e
SL
6870static ssize_t
6871raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
6872{
7b1485ba
N
6873 struct r5conf *conf;
6874 int ret = 0;
6875 spin_lock(&mddev->lock);
6876 conf = mddev->private;
b721420e 6877 if (conf)
7b1485ba
N
6878 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
6879 spin_unlock(&mddev->lock);
6880 return ret;
b721420e
SL
6881}
6882
60aaf933 6883static int alloc_thread_groups(struct r5conf *conf, int cnt,
6884 int *group_cnt,
60aaf933 6885 struct r5worker_group **worker_groups);
b721420e
SL
6886static ssize_t
6887raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
6888{
6791875e 6889 struct r5conf *conf;
7d5d7b50 6890 unsigned int new;
b721420e 6891 int err;
60aaf933 6892 struct r5worker_group *new_groups, *old_groups;
d2c9ad41 6893 int group_cnt;
b721420e
SL
6894
6895 if (len >= PAGE_SIZE)
6896 return -EINVAL;
7d5d7b50
SL
6897 if (kstrtouint(page, 10, &new))
6898 return -EINVAL;
6899 /* 8192 should be big enough */
6900 if (new > 8192)
b721420e
SL
6901 return -EINVAL;
6902
6791875e
N
6903 err = mddev_lock(mddev);
6904 if (err)
6905 return err;
6906 conf = mddev->private;
6907 if (!conf)
6908 err = -ENODEV;
6909 else if (new != conf->worker_cnt_per_group) {
6910 mddev_suspend(mddev);
b721420e 6911
6791875e
N
6912 old_groups = conf->worker_groups;
6913 if (old_groups)
6914 flush_workqueue(raid5_wq);
d206dcfa 6915
d2c9ad41 6916 err = alloc_thread_groups(conf, new, &group_cnt, &new_groups);
6791875e
N
6917 if (!err) {
6918 spin_lock_irq(&conf->device_lock);
6919 conf->group_cnt = group_cnt;
d2c9ad41 6920 conf->worker_cnt_per_group = new;
6791875e
N
6921 conf->worker_groups = new_groups;
6922 spin_unlock_irq(&conf->device_lock);
b721420e 6923
6791875e
N
6924 if (old_groups)
6925 kfree(old_groups[0].workers);
6926 kfree(old_groups);
6927 }
6928 mddev_resume(mddev);
b721420e 6929 }
6791875e 6930 mddev_unlock(mddev);
b721420e 6931
6791875e 6932 return err ?: len;
b721420e
SL
6933}
6934
6935static struct md_sysfs_entry
6936raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
6937 raid5_show_group_thread_cnt,
6938 raid5_store_group_thread_cnt);
6939
007583c9 6940static struct attribute *raid5_attrs[] = {
3f294f4f
N
6941 &raid5_stripecache_size.attr,
6942 &raid5_stripecache_active.attr,
8b3e6cdc 6943 &raid5_preread_bypass_threshold.attr,
b721420e 6944 &raid5_group_thread_cnt.attr,
d592a996 6945 &raid5_skip_copy.attr,
d06f191f 6946 &raid5_rmw_level.attr,
3b5408b9 6947 &raid5_stripe_size.attr,
2c7da14b 6948 &r5c_journal_mode.attr,
a596d086 6949 &ppl_write_hint.attr,
3f294f4f
N
6950 NULL,
6951};
c32dc040 6952static const struct attribute_group raid5_attrs_group = {
007583c9
N
6953 .name = NULL,
6954 .attrs = raid5_attrs,
3f294f4f
N
6955};
6956
d2c9ad41 6957static int alloc_thread_groups(struct r5conf *conf, int cnt, int *group_cnt,
60aaf933 6958 struct r5worker_group **worker_groups)
851c30c9 6959{
566c09c5 6960 int i, j, k;
851c30c9
SL
6961 ssize_t size;
6962 struct r5worker *workers;
6963
851c30c9 6964 if (cnt == 0) {
60aaf933 6965 *group_cnt = 0;
6966 *worker_groups = NULL;
851c30c9
SL
6967 return 0;
6968 }
60aaf933 6969 *group_cnt = num_possible_nodes();
851c30c9 6970 size = sizeof(struct r5worker) * cnt;
6396bb22
KC
6971 workers = kcalloc(size, *group_cnt, GFP_NOIO);
6972 *worker_groups = kcalloc(*group_cnt, sizeof(struct r5worker_group),
6973 GFP_NOIO);
60aaf933 6974 if (!*worker_groups || !workers) {
851c30c9 6975 kfree(workers);
60aaf933 6976 kfree(*worker_groups);
851c30c9
SL
6977 return -ENOMEM;
6978 }
6979
60aaf933 6980 for (i = 0; i < *group_cnt; i++) {
851c30c9
SL
6981 struct r5worker_group *group;
6982
0c775d52 6983 group = &(*worker_groups)[i];
851c30c9 6984 INIT_LIST_HEAD(&group->handle_list);
535ae4eb 6985 INIT_LIST_HEAD(&group->loprio_list);
851c30c9
SL
6986 group->conf = conf;
6987 group->workers = workers + i * cnt;
6988
6989 for (j = 0; j < cnt; j++) {
566c09c5
SL
6990 struct r5worker *worker = group->workers + j;
6991 worker->group = group;
6992 INIT_WORK(&worker->work, raid5_do_work);
6993
6994 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
6995 INIT_LIST_HEAD(worker->temp_inactive_list + k);
851c30c9
SL
6996 }
6997 }
6998
6999 return 0;
7000}
7001
7002static void free_thread_groups(struct r5conf *conf)
7003{
7004 if (conf->worker_groups)
7005 kfree(conf->worker_groups[0].workers);
7006 kfree(conf->worker_groups);
7007 conf->worker_groups = NULL;
7008}
7009
80c3a6ce 7010static sector_t
fd01b88c 7011raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
80c3a6ce 7012{
d1688a6d 7013 struct r5conf *conf = mddev->private;
80c3a6ce
DW
7014
7015 if (!sectors)
7016 sectors = mddev->dev_sectors;
5e5e3e78 7017 if (!raid_disks)
7ec05478 7018 /* size is defined by the smallest of previous and new size */
5e5e3e78 7019 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
80c3a6ce 7020
3cb5edf4
N
7021 sectors &= ~((sector_t)conf->chunk_sectors - 1);
7022 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
80c3a6ce
DW
7023 return sectors * (raid_disks - conf->max_degraded);
7024}
7025
789b5e03
ON
7026static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
7027{
7028 safe_put_page(percpu->spare_page);
789b5e03 7029 percpu->spare_page = NULL;
b330e6a4 7030 kvfree(percpu->scribble);
789b5e03
ON
7031 percpu->scribble = NULL;
7032}
7033
7034static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
7035{
b330e6a4 7036 if (conf->level == 6 && !percpu->spare_page) {
789b5e03 7037 percpu->spare_page = alloc_page(GFP_KERNEL);
b330e6a4
KO
7038 if (!percpu->spare_page)
7039 return -ENOMEM;
7040 }
7041
7042 if (scribble_alloc(percpu,
7043 max(conf->raid_disks,
7044 conf->previous_raid_disks),
7045 max(conf->chunk_sectors,
7046 conf->prev_chunk_sectors)
c911c46c 7047 / RAID5_STRIPE_SECTORS(conf))) {
789b5e03
ON
7048 free_scratch_buffer(conf, percpu);
7049 return -ENOMEM;
7050 }
7051
7052 return 0;
7053}
7054
29c6d1bb 7055static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
36d1c647 7056{
29c6d1bb
SAS
7057 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
7058
7059 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
7060 return 0;
7061}
36d1c647 7062
29c6d1bb
SAS
7063static void raid5_free_percpu(struct r5conf *conf)
7064{
36d1c647
DW
7065 if (!conf->percpu)
7066 return;
7067
29c6d1bb 7068 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
36d1c647
DW
7069 free_percpu(conf->percpu);
7070}
7071
d1688a6d 7072static void free_conf(struct r5conf *conf)
95fc17aa 7073{
d7bd398e
SL
7074 int i;
7075
ff875738
AP
7076 log_exit(conf);
7077
565e0450 7078 unregister_shrinker(&conf->shrinker);
851c30c9 7079 free_thread_groups(conf);
95fc17aa 7080 shrink_stripes(conf);
36d1c647 7081 raid5_free_percpu(conf);
d7bd398e
SL
7082 for (i = 0; i < conf->pool_size; i++)
7083 if (conf->disks[i].extra_page)
7084 put_page(conf->disks[i].extra_page);
95fc17aa 7085 kfree(conf->disks);
afeee514 7086 bioset_exit(&conf->bio_split);
95fc17aa 7087 kfree(conf->stripe_hashtbl);
aaf9f12e 7088 kfree(conf->pending_data);
95fc17aa
DW
7089 kfree(conf);
7090}
7091
29c6d1bb 7092static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
36d1c647 7093{
29c6d1bb 7094 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
36d1c647
DW
7095 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
7096
29c6d1bb 7097 if (alloc_scratch_buffer(conf, percpu)) {
cc6167b4
N
7098 pr_warn("%s: failed memory allocation for cpu%u\n",
7099 __func__, cpu);
29c6d1bb 7100 return -ENOMEM;
36d1c647 7101 }
29c6d1bb 7102 return 0;
36d1c647 7103}
36d1c647 7104
d1688a6d 7105static int raid5_alloc_percpu(struct r5conf *conf)
36d1c647 7106{
789b5e03 7107 int err = 0;
36d1c647 7108
789b5e03
ON
7109 conf->percpu = alloc_percpu(struct raid5_percpu);
7110 if (!conf->percpu)
36d1c647 7111 return -ENOMEM;
789b5e03 7112
29c6d1bb 7113 err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
27a353c0
SL
7114 if (!err) {
7115 conf->scribble_disks = max(conf->raid_disks,
7116 conf->previous_raid_disks);
7117 conf->scribble_sectors = max(conf->chunk_sectors,
7118 conf->prev_chunk_sectors);
7119 }
36d1c647
DW
7120 return err;
7121}
7122
edbe83ab
N
7123static unsigned long raid5_cache_scan(struct shrinker *shrink,
7124 struct shrink_control *sc)
7125{
7126 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
2d5b569b
N
7127 unsigned long ret = SHRINK_STOP;
7128
7129 if (mutex_trylock(&conf->cache_size_mutex)) {
7130 ret= 0;
49895bcc
N
7131 while (ret < sc->nr_to_scan &&
7132 conf->max_nr_stripes > conf->min_nr_stripes) {
2d5b569b
N
7133 if (drop_one_stripe(conf) == 0) {
7134 ret = SHRINK_STOP;
7135 break;
7136 }
7137 ret++;
7138 }
7139 mutex_unlock(&conf->cache_size_mutex);
edbe83ab
N
7140 }
7141 return ret;
7142}
7143
7144static unsigned long raid5_cache_count(struct shrinker *shrink,
7145 struct shrink_control *sc)
7146{
7147 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
7148
7149 if (conf->max_nr_stripes < conf->min_nr_stripes)
7150 /* unlikely, but not impossible */
7151 return 0;
7152 return conf->max_nr_stripes - conf->min_nr_stripes;
7153}
7154
d1688a6d 7155static struct r5conf *setup_conf(struct mddev *mddev)
1da177e4 7156{
d1688a6d 7157 struct r5conf *conf;
5e5e3e78 7158 int raid_disk, memory, max_disks;
3cb03002 7159 struct md_rdev *rdev;
1da177e4 7160 struct disk_info *disk;
0232605d 7161 char pers_name[6];
566c09c5 7162 int i;
d2c9ad41 7163 int group_cnt;
60aaf933 7164 struct r5worker_group *new_group;
afeee514 7165 int ret;
1da177e4 7166
91adb564
N
7167 if (mddev->new_level != 5
7168 && mddev->new_level != 4
7169 && mddev->new_level != 6) {
cc6167b4
N
7170 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
7171 mdname(mddev), mddev->new_level);
91adb564 7172 return ERR_PTR(-EIO);
1da177e4 7173 }
91adb564
N
7174 if ((mddev->new_level == 5
7175 && !algorithm_valid_raid5(mddev->new_layout)) ||
7176 (mddev->new_level == 6
7177 && !algorithm_valid_raid6(mddev->new_layout))) {
cc6167b4
N
7178 pr_warn("md/raid:%s: layout %d not supported\n",
7179 mdname(mddev), mddev->new_layout);
91adb564 7180 return ERR_PTR(-EIO);
99c0fb5f 7181 }
91adb564 7182 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
cc6167b4
N
7183 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
7184 mdname(mddev), mddev->raid_disks);
91adb564 7185 return ERR_PTR(-EINVAL);
4bbf3771
N
7186 }
7187
664e7c41
AN
7188 if (!mddev->new_chunk_sectors ||
7189 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
7190 !is_power_of_2(mddev->new_chunk_sectors)) {
cc6167b4
N
7191 pr_warn("md/raid:%s: invalid chunk size %d\n",
7192 mdname(mddev), mddev->new_chunk_sectors << 9);
91adb564 7193 return ERR_PTR(-EINVAL);
f6705578
N
7194 }
7195
d1688a6d 7196 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
91adb564 7197 if (conf == NULL)
1da177e4 7198 goto abort;
c911c46c 7199
e2368582
YY
7200#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
7201 conf->stripe_size = DEFAULT_STRIPE_SIZE;
7202 conf->stripe_shift = ilog2(DEFAULT_STRIPE_SIZE) - 9;
7203 conf->stripe_sectors = DEFAULT_STRIPE_SIZE >> 9;
7204#endif
aaf9f12e
SL
7205 INIT_LIST_HEAD(&conf->free_list);
7206 INIT_LIST_HEAD(&conf->pending_list);
6396bb22
KC
7207 conf->pending_data = kcalloc(PENDING_IO_MAX,
7208 sizeof(struct r5pending_data),
7209 GFP_KERNEL);
aaf9f12e
SL
7210 if (!conf->pending_data)
7211 goto abort;
7212 for (i = 0; i < PENDING_IO_MAX; i++)
7213 list_add(&conf->pending_data[i].sibling, &conf->free_list);
851c30c9 7214 /* Don't enable multi-threading by default*/
d2c9ad41 7215 if (!alloc_thread_groups(conf, 0, &group_cnt, &new_group)) {
60aaf933 7216 conf->group_cnt = group_cnt;
d2c9ad41 7217 conf->worker_cnt_per_group = 0;
60aaf933 7218 conf->worker_groups = new_group;
7219 } else
851c30c9 7220 goto abort;
f5efd45a 7221 spin_lock_init(&conf->device_lock);
0a87b25f 7222 seqcount_spinlock_init(&conf->gen_lock, &conf->device_lock);
2d5b569b 7223 mutex_init(&conf->cache_size_mutex);
b1b46486 7224 init_waitqueue_head(&conf->wait_for_quiescent);
6ab2a4b8 7225 init_waitqueue_head(&conf->wait_for_stripe);
f5efd45a
DW
7226 init_waitqueue_head(&conf->wait_for_overlap);
7227 INIT_LIST_HEAD(&conf->handle_list);
535ae4eb 7228 INIT_LIST_HEAD(&conf->loprio_list);
f5efd45a
DW
7229 INIT_LIST_HEAD(&conf->hold_list);
7230 INIT_LIST_HEAD(&conf->delayed_list);
7231 INIT_LIST_HEAD(&conf->bitmap_list);
773ca82f 7232 init_llist_head(&conf->released_stripes);
f5efd45a
DW
7233 atomic_set(&conf->active_stripes, 0);
7234 atomic_set(&conf->preread_active_stripes, 0);
7235 atomic_set(&conf->active_aligned_reads, 0);
765d704d
SL
7236 spin_lock_init(&conf->pending_bios_lock);
7237 conf->batch_bio_dispatch = true;
7238 rdev_for_each(rdev, mddev) {
7239 if (test_bit(Journal, &rdev->flags))
7240 continue;
7241 if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
7242 conf->batch_bio_dispatch = false;
7243 break;
7244 }
7245 }
7246
f5efd45a 7247 conf->bypass_threshold = BYPASS_THRESHOLD;
d890fa2b 7248 conf->recovery_disabled = mddev->recovery_disabled - 1;
91adb564
N
7249
7250 conf->raid_disks = mddev->raid_disks;
7251 if (mddev->reshape_position == MaxSector)
7252 conf->previous_raid_disks = mddev->raid_disks;
7253 else
f6705578 7254 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
5e5e3e78 7255 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
f6705578 7256
6396bb22 7257 conf->disks = kcalloc(max_disks, sizeof(struct disk_info),
b55e6bfc 7258 GFP_KERNEL);
d7bd398e 7259
b55e6bfc
N
7260 if (!conf->disks)
7261 goto abort;
9ffae0cf 7262
d7bd398e
SL
7263 for (i = 0; i < max_disks; i++) {
7264 conf->disks[i].extra_page = alloc_page(GFP_KERNEL);
7265 if (!conf->disks[i].extra_page)
7266 goto abort;
7267 }
7268
afeee514
KO
7269 ret = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
7270 if (ret)
dd7a8f5d 7271 goto abort;
1da177e4
LT
7272 conf->mddev = mddev;
7273
fccddba0 7274 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
1da177e4 7275 goto abort;
1da177e4 7276
566c09c5
SL
7277 /* We init hash_locks[0] separately to that it can be used
7278 * as the reference lock in the spin_lock_nest_lock() call
7279 * in lock_all_device_hash_locks_irq in order to convince
7280 * lockdep that we know what we are doing.
7281 */
7282 spin_lock_init(conf->hash_locks);
7283 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
7284 spin_lock_init(conf->hash_locks + i);
7285
7286 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
7287 INIT_LIST_HEAD(conf->inactive_list + i);
7288
7289 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
7290 INIT_LIST_HEAD(conf->temp_inactive_list + i);
7291
1e6d690b
SL
7292 atomic_set(&conf->r5c_cached_full_stripes, 0);
7293 INIT_LIST_HEAD(&conf->r5c_full_stripe_list);
7294 atomic_set(&conf->r5c_cached_partial_stripes, 0);
7295 INIT_LIST_HEAD(&conf->r5c_partial_stripe_list);
e33fbb9c
SL
7296 atomic_set(&conf->r5c_flushing_full_stripes, 0);
7297 atomic_set(&conf->r5c_flushing_partial_stripes, 0);
1e6d690b 7298
36d1c647 7299 conf->level = mddev->new_level;
46d5b785 7300 conf->chunk_sectors = mddev->new_chunk_sectors;
36d1c647
DW
7301 if (raid5_alloc_percpu(conf) != 0)
7302 goto abort;
7303
0c55e022 7304 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
1da177e4 7305
dafb20fa 7306 rdev_for_each(rdev, mddev) {
1da177e4 7307 raid_disk = rdev->raid_disk;
5e5e3e78 7308 if (raid_disk >= max_disks
f2076e7d 7309 || raid_disk < 0 || test_bit(Journal, &rdev->flags))
1da177e4
LT
7310 continue;
7311 disk = conf->disks + raid_disk;
7312
17045f52
N
7313 if (test_bit(Replacement, &rdev->flags)) {
7314 if (disk->replacement)
7315 goto abort;
7316 disk->replacement = rdev;
7317 } else {
7318 if (disk->rdev)
7319 goto abort;
7320 disk->rdev = rdev;
7321 }
1da177e4 7322
b2d444d7 7323 if (test_bit(In_sync, &rdev->flags)) {
1da177e4 7324 char b[BDEVNAME_SIZE];
cc6167b4
N
7325 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
7326 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
d6b212f4 7327 } else if (rdev->saved_raid_disk != raid_disk)
8c2e870a
NB
7328 /* Cannot rely on bitmap to complete recovery */
7329 conf->fullsync = 1;
1da177e4
LT
7330 }
7331
91adb564 7332 conf->level = mddev->new_level;
584acdd4 7333 if (conf->level == 6) {
16a53ecc 7334 conf->max_degraded = 2;
584acdd4
MS
7335 if (raid6_call.xor_syndrome)
7336 conf->rmw_level = PARITY_ENABLE_RMW;
7337 else
7338 conf->rmw_level = PARITY_DISABLE_RMW;
7339 } else {
16a53ecc 7340 conf->max_degraded = 1;
584acdd4
MS
7341 conf->rmw_level = PARITY_ENABLE_RMW;
7342 }
91adb564 7343 conf->algorithm = mddev->new_layout;
fef9c61f 7344 conf->reshape_progress = mddev->reshape_position;
e183eaed 7345 if (conf->reshape_progress != MaxSector) {
09c9e5fa 7346 conf->prev_chunk_sectors = mddev->chunk_sectors;
e183eaed 7347 conf->prev_algo = mddev->layout;
5cac6bcb
N
7348 } else {
7349 conf->prev_chunk_sectors = conf->chunk_sectors;
7350 conf->prev_algo = conf->algorithm;
e183eaed 7351 }
1da177e4 7352
edbe83ab 7353 conf->min_nr_stripes = NR_STRIPES;
ad5b0f76
SL
7354 if (mddev->reshape_position != MaxSector) {
7355 int stripes = max_t(int,
c911c46c
YY
7356 ((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4,
7357 ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4);
ad5b0f76
SL
7358 conf->min_nr_stripes = max(NR_STRIPES, stripes);
7359 if (conf->min_nr_stripes != NR_STRIPES)
cc6167b4 7360 pr_info("md/raid:%s: force stripe size %d for reshape\n",
ad5b0f76
SL
7361 mdname(mddev), conf->min_nr_stripes);
7362 }
edbe83ab 7363 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
5e5e3e78 7364 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4bda556a 7365 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
edbe83ab 7366 if (grow_stripes(conf, conf->min_nr_stripes)) {
cc6167b4
N
7367 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
7368 mdname(mddev), memory);
91adb564
N
7369 goto abort;
7370 } else
cc6167b4 7371 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory);
edbe83ab
N
7372 /*
7373 * Losing a stripe head costs more than the time to refill it,
7374 * it reduces the queue depth and so can hurt throughput.
7375 * So set it rather large, scaled by number of devices.
7376 */
7377 conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
7378 conf->shrinker.scan_objects = raid5_cache_scan;
7379 conf->shrinker.count_objects = raid5_cache_count;
7380 conf->shrinker.batch = 128;
7381 conf->shrinker.flags = 0;
6a0f53ff 7382 if (register_shrinker(&conf->shrinker)) {
cc6167b4
N
7383 pr_warn("md/raid:%s: couldn't register shrinker.\n",
7384 mdname(mddev));
6a0f53ff
CY
7385 goto abort;
7386 }
1da177e4 7387
0232605d
N
7388 sprintf(pers_name, "raid%d", mddev->new_level);
7389 conf->thread = md_register_thread(raid5d, mddev, pers_name);
91adb564 7390 if (!conf->thread) {
cc6167b4
N
7391 pr_warn("md/raid:%s: couldn't allocate thread.\n",
7392 mdname(mddev));
16a53ecc
N
7393 goto abort;
7394 }
91adb564
N
7395
7396 return conf;
7397
7398 abort:
7399 if (conf) {
95fc17aa 7400 free_conf(conf);
91adb564
N
7401 return ERR_PTR(-EIO);
7402 } else
7403 return ERR_PTR(-ENOMEM);
7404}
7405
c148ffdc
N
7406static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
7407{
7408 switch (algo) {
7409 case ALGORITHM_PARITY_0:
7410 if (raid_disk < max_degraded)
7411 return 1;
7412 break;
7413 case ALGORITHM_PARITY_N:
7414 if (raid_disk >= raid_disks - max_degraded)
7415 return 1;
7416 break;
7417 case ALGORITHM_PARITY_0_6:
f72ffdd6 7418 if (raid_disk == 0 ||
c148ffdc
N
7419 raid_disk == raid_disks - 1)
7420 return 1;
7421 break;
7422 case ALGORITHM_LEFT_ASYMMETRIC_6:
7423 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7424 case ALGORITHM_LEFT_SYMMETRIC_6:
7425 case ALGORITHM_RIGHT_SYMMETRIC_6:
7426 if (raid_disk == raid_disks - 1)
7427 return 1;
7428 }
7429 return 0;
7430}
7431
16ef5101
CH
7432static void raid5_set_io_opt(struct r5conf *conf)
7433{
7434 blk_queue_io_opt(conf->mddev->queue, (conf->chunk_sectors << 9) *
7435 (conf->raid_disks - conf->max_degraded));
7436}
7437
849674e4 7438static int raid5_run(struct mddev *mddev)
91adb564 7439{
d1688a6d 7440 struct r5conf *conf;
9f7c2220 7441 int working_disks = 0;
c148ffdc 7442 int dirty_parity_disks = 0;
3cb03002 7443 struct md_rdev *rdev;
713cf5a6 7444 struct md_rdev *journal_dev = NULL;
c148ffdc 7445 sector_t reshape_offset = 0;
306d8015 7446 int i, ret = 0;
b5254dd5
N
7447 long long min_offset_diff = 0;
7448 int first = 1;
91adb564 7449
306d8015
XN
7450 if (acct_bioset_init(mddev)) {
7451 pr_err("md/raid456:%s: alloc acct bioset failed.\n", mdname(mddev));
a415c0f1 7452 return -ENOMEM;
306d8015
XN
7453 }
7454
7455 if (mddev_init_writes_pending(mddev) < 0) {
7456 ret = -ENOMEM;
7457 goto exit_acct_set;
7458 }
a415c0f1 7459
8c6ac868 7460 if (mddev->recovery_cp != MaxSector)
cc6167b4
N
7461 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
7462 mdname(mddev));
b5254dd5
N
7463
7464 rdev_for_each(rdev, mddev) {
7465 long long diff;
713cf5a6 7466
f2076e7d 7467 if (test_bit(Journal, &rdev->flags)) {
713cf5a6 7468 journal_dev = rdev;
f2076e7d
SL
7469 continue;
7470 }
b5254dd5
N
7471 if (rdev->raid_disk < 0)
7472 continue;
7473 diff = (rdev->new_data_offset - rdev->data_offset);
7474 if (first) {
7475 min_offset_diff = diff;
7476 first = 0;
7477 } else if (mddev->reshape_backwards &&
7478 diff < min_offset_diff)
7479 min_offset_diff = diff;
7480 else if (!mddev->reshape_backwards &&
7481 diff > min_offset_diff)
7482 min_offset_diff = diff;
7483 }
7484
230b55fa
N
7485 if ((test_bit(MD_HAS_JOURNAL, &mddev->flags) || journal_dev) &&
7486 (mddev->bitmap_info.offset || mddev->bitmap_info.file)) {
7487 pr_notice("md/raid:%s: array cannot have both journal and bitmap\n",
7488 mdname(mddev));
306d8015
XN
7489 ret = -EINVAL;
7490 goto exit_acct_set;
230b55fa
N
7491 }
7492
91adb564
N
7493 if (mddev->reshape_position != MaxSector) {
7494 /* Check that we can continue the reshape.
b5254dd5
N
7495 * Difficulties arise if the stripe we would write to
7496 * next is at or after the stripe we would read from next.
7497 * For a reshape that changes the number of devices, this
7498 * is only possible for a very short time, and mdadm makes
7499 * sure that time appears to have past before assembling
7500 * the array. So we fail if that time hasn't passed.
7501 * For a reshape that keeps the number of devices the same
7502 * mdadm must be monitoring the reshape can keeping the
7503 * critical areas read-only and backed up. It will start
7504 * the array in read-only mode, so we check for that.
91adb564
N
7505 */
7506 sector_t here_new, here_old;
7507 int old_disks;
18b00334 7508 int max_degraded = (mddev->level == 6 ? 2 : 1);
05256d98
N
7509 int chunk_sectors;
7510 int new_data_disks;
91adb564 7511
713cf5a6 7512 if (journal_dev) {
cc6167b4
N
7513 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
7514 mdname(mddev));
306d8015
XN
7515 ret = -EINVAL;
7516 goto exit_acct_set;
713cf5a6
SL
7517 }
7518
88ce4930 7519 if (mddev->new_level != mddev->level) {
cc6167b4
N
7520 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
7521 mdname(mddev));
306d8015
XN
7522 ret = -EINVAL;
7523 goto exit_acct_set;
91adb564 7524 }
91adb564
N
7525 old_disks = mddev->raid_disks - mddev->delta_disks;
7526 /* reshape_position must be on a new-stripe boundary, and one
7527 * further up in new geometry must map after here in old
7528 * geometry.
05256d98
N
7529 * If the chunk sizes are different, then as we perform reshape
7530 * in units of the largest of the two, reshape_position needs
7531 * be a multiple of the largest chunk size times new data disks.
91adb564
N
7532 */
7533 here_new = mddev->reshape_position;
05256d98
N
7534 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
7535 new_data_disks = mddev->raid_disks - max_degraded;
7536 if (sector_div(here_new, chunk_sectors * new_data_disks)) {
cc6167b4
N
7537 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
7538 mdname(mddev));
306d8015
XN
7539 ret = -EINVAL;
7540 goto exit_acct_set;
91adb564 7541 }
05256d98 7542 reshape_offset = here_new * chunk_sectors;
91adb564
N
7543 /* here_new is the stripe we will write to */
7544 here_old = mddev->reshape_position;
05256d98 7545 sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
91adb564
N
7546 /* here_old is the first stripe that we might need to read
7547 * from */
67ac6011
N
7548 if (mddev->delta_disks == 0) {
7549 /* We cannot be sure it is safe to start an in-place
b5254dd5 7550 * reshape. It is only safe if user-space is monitoring
67ac6011
N
7551 * and taking constant backups.
7552 * mdadm always starts a situation like this in
7553 * readonly mode so it can take control before
7554 * allowing any writes. So just check for that.
7555 */
b5254dd5
N
7556 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
7557 abs(min_offset_diff) >= mddev->new_chunk_sectors)
7558 /* not really in-place - so OK */;
7559 else if (mddev->ro == 0) {
cc6167b4
N
7560 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
7561 mdname(mddev));
306d8015
XN
7562 ret = -EINVAL;
7563 goto exit_acct_set;
67ac6011 7564 }
2c810cdd 7565 } else if (mddev->reshape_backwards
05256d98
N
7566 ? (here_new * chunk_sectors + min_offset_diff <=
7567 here_old * chunk_sectors)
7568 : (here_new * chunk_sectors >=
7569 here_old * chunk_sectors + (-min_offset_diff))) {
91adb564 7570 /* Reading from the same stripe as writing to - bad */
cc6167b4
N
7571 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
7572 mdname(mddev));
306d8015
XN
7573 ret = -EINVAL;
7574 goto exit_acct_set;
91adb564 7575 }
cc6167b4 7576 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev));
91adb564
N
7577 /* OK, we should be able to continue; */
7578 } else {
7579 BUG_ON(mddev->level != mddev->new_level);
7580 BUG_ON(mddev->layout != mddev->new_layout);
664e7c41 7581 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
91adb564 7582 BUG_ON(mddev->delta_disks != 0);
1da177e4 7583 }
91adb564 7584
3418d036
AP
7585 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) &&
7586 test_bit(MD_HAS_PPL, &mddev->flags)) {
7587 pr_warn("md/raid:%s: using journal device and PPL not allowed - disabling PPL\n",
7588 mdname(mddev));
7589 clear_bit(MD_HAS_PPL, &mddev->flags);
ddc08823 7590 clear_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags);
3418d036
AP
7591 }
7592
245f46c2
N
7593 if (mddev->private == NULL)
7594 conf = setup_conf(mddev);
7595 else
7596 conf = mddev->private;
7597
306d8015
XN
7598 if (IS_ERR(conf)) {
7599 ret = PTR_ERR(conf);
7600 goto exit_acct_set;
7601 }
91adb564 7602
486b0f7b
SL
7603 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
7604 if (!journal_dev) {
cc6167b4
N
7605 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
7606 mdname(mddev));
486b0f7b
SL
7607 mddev->ro = 1;
7608 set_disk_ro(mddev->gendisk, 1);
7609 } else if (mddev->recovery_cp == MaxSector)
7610 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
7dde2ad3
SL
7611 }
7612
b5254dd5 7613 conf->min_offset_diff = min_offset_diff;
91adb564
N
7614 mddev->thread = conf->thread;
7615 conf->thread = NULL;
7616 mddev->private = conf;
7617
17045f52
N
7618 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
7619 i++) {
7620 rdev = conf->disks[i].rdev;
7621 if (!rdev && conf->disks[i].replacement) {
7622 /* The replacement is all we have yet */
7623 rdev = conf->disks[i].replacement;
7624 conf->disks[i].replacement = NULL;
7625 clear_bit(Replacement, &rdev->flags);
7626 conf->disks[i].rdev = rdev;
7627 }
7628 if (!rdev)
c148ffdc 7629 continue;
17045f52
N
7630 if (conf->disks[i].replacement &&
7631 conf->reshape_progress != MaxSector) {
7632 /* replacements and reshape simply do not mix. */
cc6167b4 7633 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
17045f52
N
7634 goto abort;
7635 }
2f115882 7636 if (test_bit(In_sync, &rdev->flags)) {
91adb564 7637 working_disks++;
2f115882
N
7638 continue;
7639 }
c148ffdc
N
7640 /* This disc is not fully in-sync. However if it
7641 * just stored parity (beyond the recovery_offset),
7642 * when we don't need to be concerned about the
7643 * array being dirty.
7644 * When reshape goes 'backwards', we never have
7645 * partially completed devices, so we only need
7646 * to worry about reshape going forwards.
7647 */
7648 /* Hack because v0.91 doesn't store recovery_offset properly. */
7649 if (mddev->major_version == 0 &&
7650 mddev->minor_version > 90)
7651 rdev->recovery_offset = reshape_offset;
5026d7a9 7652
c148ffdc
N
7653 if (rdev->recovery_offset < reshape_offset) {
7654 /* We need to check old and new layout */
7655 if (!only_parity(rdev->raid_disk,
7656 conf->algorithm,
7657 conf->raid_disks,
7658 conf->max_degraded))
7659 continue;
7660 }
7661 if (!only_parity(rdev->raid_disk,
7662 conf->prev_algo,
7663 conf->previous_raid_disks,
7664 conf->max_degraded))
7665 continue;
7666 dirty_parity_disks++;
7667 }
91adb564 7668
17045f52
N
7669 /*
7670 * 0 for a fully functional array, 1 or 2 for a degraded array.
7671 */
2e38a37f 7672 mddev->degraded = raid5_calc_degraded(conf);
91adb564 7673
674806d6 7674 if (has_failed(conf)) {
cc6167b4 7675 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
02c2de8c 7676 mdname(mddev), mddev->degraded, conf->raid_disks);
1da177e4
LT
7677 goto abort;
7678 }
7679
91adb564 7680 /* device size must be a multiple of chunk size */
c5eec74f 7681 mddev->dev_sectors &= ~((sector_t)mddev->chunk_sectors - 1);
91adb564
N
7682 mddev->resync_max_sectors = mddev->dev_sectors;
7683
c148ffdc 7684 if (mddev->degraded > dirty_parity_disks &&
1da177e4 7685 mddev->recovery_cp != MaxSector) {
4536bf9b
AP
7686 if (test_bit(MD_HAS_PPL, &mddev->flags))
7687 pr_crit("md/raid:%s: starting dirty degraded array with PPL.\n",
7688 mdname(mddev));
7689 else if (mddev->ok_start_degraded)
cc6167b4
N
7690 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
7691 mdname(mddev));
6ff8d8ec 7692 else {
cc6167b4
N
7693 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
7694 mdname(mddev));
6ff8d8ec
N
7695 goto abort;
7696 }
1da177e4
LT
7697 }
7698
cc6167b4
N
7699 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
7700 mdname(mddev), conf->level,
7701 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
7702 mddev->new_layout);
1da177e4
LT
7703
7704 print_raid5_conf(conf);
7705
fef9c61f 7706 if (conf->reshape_progress != MaxSector) {
fef9c61f 7707 conf->reshape_safe = conf->reshape_progress;
f6705578
N
7708 atomic_set(&conf->reshape_stripes, 0);
7709 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7710 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7711 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7712 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7713 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
0da3c619 7714 "reshape");
e406f12d
AP
7715 if (!mddev->sync_thread)
7716 goto abort;
f6705578
N
7717 }
7718
1da177e4 7719 /* Ok, everything is just fine now */
a64c876f
N
7720 if (mddev->to_remove == &raid5_attrs_group)
7721 mddev->to_remove = NULL;
00bcb4ac
N
7722 else if (mddev->kobj.sd &&
7723 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
cc6167b4
N
7724 pr_warn("raid5: failed to create sysfs attributes for %s\n",
7725 mdname(mddev));
4a5add49 7726 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
7a5febe9 7727
4a5add49 7728 if (mddev->queue) {
9f7c2220 7729 int chunk_size;
4a5add49
N
7730 /* read-ahead size must cover two whole stripes, which
7731 * is 2 * (datadisks) * chunksize where 'n' is the
7732 * number of raid devices
7733 */
7734 int data_disks = conf->previous_raid_disks - conf->max_degraded;
7735 int stripe = data_disks *
7736 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
91adb564 7737
9f7c2220
N
7738 chunk_size = mddev->chunk_sectors << 9;
7739 blk_queue_io_min(mddev->queue, chunk_size);
16ef5101 7740 raid5_set_io_opt(conf);
c78afc62 7741 mddev->queue->limits.raid_partial_stripes_expensive = 1;
620125f2
SL
7742 /*
7743 * We can only discard a whole stripe. It doesn't make sense to
7744 * discard data disk but write parity disk
7745 */
7746 stripe = stripe * PAGE_SIZE;
4ac6875e
N
7747 /* Round up to power of 2, as discard handling
7748 * currently assumes that */
7749 while ((stripe-1) & stripe)
7750 stripe = (stripe | (stripe-1)) + 1;
620125f2
SL
7751 mddev->queue->limits.discard_alignment = stripe;
7752 mddev->queue->limits.discard_granularity = stripe;
e8d7c332 7753
5026d7a9 7754 blk_queue_max_write_same_sectors(mddev->queue, 0);
3deff1a7 7755 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
5026d7a9 7756
05616be5 7757 rdev_for_each(rdev, mddev) {
9f7c2220
N
7758 disk_stack_limits(mddev->gendisk, rdev->bdev,
7759 rdev->data_offset << 9);
05616be5
N
7760 disk_stack_limits(mddev->gendisk, rdev->bdev,
7761 rdev->new_data_offset << 9);
7762 }
620125f2 7763
48920ff2
CH
7764 /*
7765 * zeroing is required, otherwise data
7766 * could be lost. Consider a scenario: discard a stripe
7767 * (the stripe could be inconsistent if
7768 * discard_zeroes_data is 0); write one disk of the
7769 * stripe (the stripe could be inconsistent again
7770 * depending on which disks are used to calculate
7771 * parity); the disk is broken; The stripe data of this
7772 * disk is lost.
7773 *
7774 * We only allow DISCARD if the sysadmin has confirmed that
7775 * only safe devices are in use by setting a module parameter.
7776 * A better idea might be to turn DISCARD into WRITE_ZEROES
7777 * requests, as that is required to be safe.
7778 */
7779 if (devices_handle_discard_safely &&
e7597e69
JS
7780 mddev->queue->limits.max_discard_sectors >= (stripe >> 9) &&
7781 mddev->queue->limits.discard_granularity >= stripe)
8b904b5b 7782 blk_queue_flag_set(QUEUE_FLAG_DISCARD,
620125f2
SL
7783 mddev->queue);
7784 else
8b904b5b 7785 blk_queue_flag_clear(QUEUE_FLAG_DISCARD,
620125f2 7786 mddev->queue);
1dffdddd
SL
7787
7788 blk_queue_max_hw_sectors(mddev->queue, UINT_MAX);
9f7c2220 7789 }
23032a0e 7790
845b9e22 7791 if (log_init(conf, journal_dev, raid5_has_ppl(conf)))
ff875738 7792 goto abort;
5c7e81c3 7793
1da177e4
LT
7794 return 0;
7795abort:
01f96c0a 7796 md_unregister_thread(&mddev->thread);
e4f869d9
N
7797 print_raid5_conf(conf);
7798 free_conf(conf);
1da177e4 7799 mddev->private = NULL;
cc6167b4 7800 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
306d8015
XN
7801 ret = -EIO;
7802exit_acct_set:
7803 acct_bioset_exit(mddev);
7804 return ret;
1da177e4
LT
7805}
7806
afa0f557 7807static void raid5_free(struct mddev *mddev, void *priv)
1da177e4 7808{
afa0f557 7809 struct r5conf *conf = priv;
1da177e4 7810
95fc17aa 7811 free_conf(conf);
306d8015 7812 acct_bioset_exit(mddev);
a64c876f 7813 mddev->to_remove = &raid5_attrs_group;
1da177e4
LT
7814}
7815
849674e4 7816static void raid5_status(struct seq_file *seq, struct mddev *mddev)
1da177e4 7817{
d1688a6d 7818 struct r5conf *conf = mddev->private;
1da177e4
LT
7819 int i;
7820
9d8f0363 7821 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
3cb5edf4 7822 conf->chunk_sectors / 2, mddev->layout);
02c2de8c 7823 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
5fd13351
N
7824 rcu_read_lock();
7825 for (i = 0; i < conf->raid_disks; i++) {
7826 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
7827 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
7828 }
7829 rcu_read_unlock();
1da177e4 7830 seq_printf (seq, "]");
1da177e4
LT
7831}
7832
d1688a6d 7833static void print_raid5_conf (struct r5conf *conf)
1da177e4
LT
7834{
7835 int i;
7836 struct disk_info *tmp;
7837
cc6167b4 7838 pr_debug("RAID conf printout:\n");
1da177e4 7839 if (!conf) {
cc6167b4 7840 pr_debug("(conf==NULL)\n");
1da177e4
LT
7841 return;
7842 }
cc6167b4 7843 pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level,
0c55e022
N
7844 conf->raid_disks,
7845 conf->raid_disks - conf->mddev->degraded);
1da177e4
LT
7846
7847 for (i = 0; i < conf->raid_disks; i++) {
7848 char b[BDEVNAME_SIZE];
7849 tmp = conf->disks + i;
7850 if (tmp->rdev)
cc6167b4 7851 pr_debug(" disk %d, o:%d, dev:%s\n",
0c55e022
N
7852 i, !test_bit(Faulty, &tmp->rdev->flags),
7853 bdevname(tmp->rdev->bdev, b));
1da177e4
LT
7854 }
7855}
7856
fd01b88c 7857static int raid5_spare_active(struct mddev *mddev)
1da177e4
LT
7858{
7859 int i;
d1688a6d 7860 struct r5conf *conf = mddev->private;
1da177e4 7861 struct disk_info *tmp;
6b965620
N
7862 int count = 0;
7863 unsigned long flags;
1da177e4
LT
7864
7865 for (i = 0; i < conf->raid_disks; i++) {
7866 tmp = conf->disks + i;
dd054fce
N
7867 if (tmp->replacement
7868 && tmp->replacement->recovery_offset == MaxSector
7869 && !test_bit(Faulty, &tmp->replacement->flags)
7870 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
7871 /* Replacement has just become active. */
7872 if (!tmp->rdev
7873 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
7874 count++;
7875 if (tmp->rdev) {
7876 /* Replaced device not technically faulty,
7877 * but we need to be sure it gets removed
7878 * and never re-added.
7879 */
7880 set_bit(Faulty, &tmp->rdev->flags);
7881 sysfs_notify_dirent_safe(
7882 tmp->rdev->sysfs_state);
7883 }
7884 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
7885 } else if (tmp->rdev
70fffd0b 7886 && tmp->rdev->recovery_offset == MaxSector
b2d444d7 7887 && !test_bit(Faulty, &tmp->rdev->flags)
c04be0aa 7888 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
6b965620 7889 count++;
43c73ca4 7890 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
1da177e4
LT
7891 }
7892 }
6b965620 7893 spin_lock_irqsave(&conf->device_lock, flags);
2e38a37f 7894 mddev->degraded = raid5_calc_degraded(conf);
6b965620 7895 spin_unlock_irqrestore(&conf->device_lock, flags);
1da177e4 7896 print_raid5_conf(conf);
6b965620 7897 return count;
1da177e4
LT
7898}
7899
b8321b68 7900static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 7901{
d1688a6d 7902 struct r5conf *conf = mddev->private;
1da177e4 7903 int err = 0;
b8321b68 7904 int number = rdev->raid_disk;
657e3e4d 7905 struct md_rdev **rdevp;
1da177e4
LT
7906 struct disk_info *p = conf->disks + number;
7907
7908 print_raid5_conf(conf);
f6b6ec5c 7909 if (test_bit(Journal, &rdev->flags) && conf->log) {
c2bb6242 7910 /*
f6b6ec5c
SL
7911 * we can't wait pending write here, as this is called in
7912 * raid5d, wait will deadlock.
84dd97a6
N
7913 * neilb: there is no locking about new writes here,
7914 * so this cannot be safe.
c2bb6242 7915 */
70d466f7
SL
7916 if (atomic_read(&conf->active_stripes) ||
7917 atomic_read(&conf->r5c_cached_full_stripes) ||
7918 atomic_read(&conf->r5c_cached_partial_stripes)) {
f6b6ec5c 7919 return -EBUSY;
84dd97a6 7920 }
ff875738 7921 log_exit(conf);
f6b6ec5c 7922 return 0;
c2bb6242 7923 }
657e3e4d
N
7924 if (rdev == p->rdev)
7925 rdevp = &p->rdev;
7926 else if (rdev == p->replacement)
7927 rdevp = &p->replacement;
7928 else
7929 return 0;
7930
7931 if (number >= conf->raid_disks &&
7932 conf->reshape_progress == MaxSector)
7933 clear_bit(In_sync, &rdev->flags);
7934
7935 if (test_bit(In_sync, &rdev->flags) ||
7936 atomic_read(&rdev->nr_pending)) {
7937 err = -EBUSY;
7938 goto abort;
7939 }
7940 /* Only remove non-faulty devices if recovery
7941 * isn't possible.
7942 */
7943 if (!test_bit(Faulty, &rdev->flags) &&
7944 mddev->recovery_disabled != conf->recovery_disabled &&
7945 !has_failed(conf) &&
dd054fce 7946 (!p->replacement || p->replacement == rdev) &&
657e3e4d
N
7947 number < conf->raid_disks) {
7948 err = -EBUSY;
7949 goto abort;
7950 }
7951 *rdevp = NULL;
d787be40
N
7952 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
7953 synchronize_rcu();
7954 if (atomic_read(&rdev->nr_pending)) {
7955 /* lost the race, try later */
7956 err = -EBUSY;
7957 *rdevp = rdev;
7958 }
7959 }
6358c239
AP
7960 if (!err) {
7961 err = log_modify(conf, rdev, false);
7962 if (err)
7963 goto abort;
7964 }
d787be40 7965 if (p->replacement) {
dd054fce
N
7966 /* We must have just cleared 'rdev' */
7967 p->rdev = p->replacement;
7968 clear_bit(Replacement, &p->replacement->flags);
7969 smp_mb(); /* Make sure other CPUs may see both as identical
7970 * but will never see neither - if they are careful
7971 */
7972 p->replacement = NULL;
6358c239
AP
7973
7974 if (!err)
7975 err = log_modify(conf, p->rdev, true);
e5bc9c3c
GJ
7976 }
7977
7978 clear_bit(WantReplacement, &rdev->flags);
1da177e4
LT
7979abort:
7980
7981 print_raid5_conf(conf);
7982 return err;
7983}
7984
fd01b88c 7985static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 7986{
d1688a6d 7987 struct r5conf *conf = mddev->private;
d9771f5e 7988 int ret, err = -EEXIST;
1da177e4
LT
7989 int disk;
7990 struct disk_info *p;
6c2fce2e
NB
7991 int first = 0;
7992 int last = conf->raid_disks - 1;
1da177e4 7993
f6b6ec5c 7994 if (test_bit(Journal, &rdev->flags)) {
f6b6ec5c
SL
7995 if (conf->log)
7996 return -EBUSY;
7997
7998 rdev->raid_disk = 0;
7999 /*
8000 * The array is in readonly mode if journal is missing, so no
8001 * write requests running. We should be safe
8002 */
d9771f5e
XN
8003 ret = log_init(conf, rdev, false);
8004 if (ret)
8005 return ret;
8006
8007 ret = r5l_start(conf->log);
8008 if (ret)
8009 return ret;
8010
f6b6ec5c
SL
8011 return 0;
8012 }
7f0da59b
N
8013 if (mddev->recovery_disabled == conf->recovery_disabled)
8014 return -EBUSY;
8015
dc10c643 8016 if (rdev->saved_raid_disk < 0 && has_failed(conf))
1da177e4 8017 /* no point adding a device */
199050ea 8018 return -EINVAL;
1da177e4 8019
6c2fce2e
NB
8020 if (rdev->raid_disk >= 0)
8021 first = last = rdev->raid_disk;
1da177e4
LT
8022
8023 /*
16a53ecc
N
8024 * find the disk ... but prefer rdev->saved_raid_disk
8025 * if possible.
1da177e4 8026 */
16a53ecc 8027 if (rdev->saved_raid_disk >= 0 &&
6c2fce2e 8028 rdev->saved_raid_disk >= first &&
16a53ecc 8029 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5cfb22a1
N
8030 first = rdev->saved_raid_disk;
8031
8032 for (disk = first; disk <= last; disk++) {
7bfec5f3
N
8033 p = conf->disks + disk;
8034 if (p->rdev == NULL) {
b2d444d7 8035 clear_bit(In_sync, &rdev->flags);
1da177e4 8036 rdev->raid_disk = disk;
72626685
N
8037 if (rdev->saved_raid_disk != disk)
8038 conf->fullsync = 1;
d6065f7b 8039 rcu_assign_pointer(p->rdev, rdev);
6358c239
AP
8040
8041 err = log_modify(conf, rdev, true);
8042
5cfb22a1 8043 goto out;
1da177e4 8044 }
5cfb22a1
N
8045 }
8046 for (disk = first; disk <= last; disk++) {
8047 p = conf->disks + disk;
7bfec5f3
N
8048 if (test_bit(WantReplacement, &p->rdev->flags) &&
8049 p->replacement == NULL) {
8050 clear_bit(In_sync, &rdev->flags);
8051 set_bit(Replacement, &rdev->flags);
8052 rdev->raid_disk = disk;
8053 err = 0;
8054 conf->fullsync = 1;
8055 rcu_assign_pointer(p->replacement, rdev);
8056 break;
8057 }
8058 }
5cfb22a1 8059out:
1da177e4 8060 print_raid5_conf(conf);
199050ea 8061 return err;
1da177e4
LT
8062}
8063
fd01b88c 8064static int raid5_resize(struct mddev *mddev, sector_t sectors)
1da177e4
LT
8065{
8066 /* no resync is happening, and there is enough space
8067 * on all devices, so we can resize.
8068 * We need to make sure resync covers any new space.
8069 * If the array is shrinking we should possibly wait until
8070 * any io in the removed space completes, but it hardly seems
8071 * worth it.
8072 */
a4a6125a 8073 sector_t newsize;
3cb5edf4
N
8074 struct r5conf *conf = mddev->private;
8075
e254de6b 8076 if (raid5_has_log(conf) || raid5_has_ppl(conf))
713cf5a6 8077 return -EINVAL;
3cb5edf4 8078 sectors &= ~((sector_t)conf->chunk_sectors - 1);
a4a6125a
N
8079 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
8080 if (mddev->external_size &&
8081 mddev->array_sectors > newsize)
b522adcd 8082 return -EINVAL;
a4a6125a 8083 if (mddev->bitmap) {
e64e4018 8084 int ret = md_bitmap_resize(mddev->bitmap, sectors, 0, 0);
a4a6125a
N
8085 if (ret)
8086 return ret;
8087 }
8088 md_set_array_sectors(mddev, newsize);
b098636c
N
8089 if (sectors > mddev->dev_sectors &&
8090 mddev->recovery_cp > mddev->dev_sectors) {
58c0fed4 8091 mddev->recovery_cp = mddev->dev_sectors;
1da177e4
LT
8092 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8093 }
58c0fed4 8094 mddev->dev_sectors = sectors;
4b5c7ae8 8095 mddev->resync_max_sectors = sectors;
1da177e4
LT
8096 return 0;
8097}
8098
fd01b88c 8099static int check_stripe_cache(struct mddev *mddev)
01ee22b4
N
8100{
8101 /* Can only proceed if there are plenty of stripe_heads.
8102 * We need a minimum of one full stripe,, and for sensible progress
8103 * it is best to have about 4 times that.
8104 * If we require 4 times, then the default 256 4K stripe_heads will
8105 * allow for chunk sizes up to 256K, which is probably OK.
8106 * If the chunk size is greater, user-space should request more
8107 * stripe_heads first.
8108 */
d1688a6d 8109 struct r5conf *conf = mddev->private;
c911c46c 8110 if (((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4
edbe83ab 8111 > conf->min_nr_stripes ||
c911c46c 8112 ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4
edbe83ab 8113 > conf->min_nr_stripes) {
cc6167b4
N
8114 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
8115 mdname(mddev),
8116 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
c911c46c 8117 / RAID5_STRIPE_SIZE(conf))*4);
01ee22b4
N
8118 return 0;
8119 }
8120 return 1;
8121}
8122
fd01b88c 8123static int check_reshape(struct mddev *mddev)
29269553 8124{
d1688a6d 8125 struct r5conf *conf = mddev->private;
29269553 8126
e254de6b 8127 if (raid5_has_log(conf) || raid5_has_ppl(conf))
713cf5a6 8128 return -EINVAL;
88ce4930
N
8129 if (mddev->delta_disks == 0 &&
8130 mddev->new_layout == mddev->layout &&
664e7c41 8131 mddev->new_chunk_sectors == mddev->chunk_sectors)
50ac168a 8132 return 0; /* nothing to do */
674806d6 8133 if (has_failed(conf))
ec32a2bd 8134 return -EINVAL;
fdcfbbb6 8135 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
ec32a2bd
N
8136 /* We might be able to shrink, but the devices must
8137 * be made bigger first.
8138 * For raid6, 4 is the minimum size.
8139 * Otherwise 2 is the minimum
8140 */
8141 int min = 2;
8142 if (mddev->level == 6)
8143 min = 4;
8144 if (mddev->raid_disks + mddev->delta_disks < min)
8145 return -EINVAL;
8146 }
29269553 8147
01ee22b4 8148 if (!check_stripe_cache(mddev))
29269553 8149 return -ENOSPC;
29269553 8150
738a2738
N
8151 if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
8152 mddev->delta_disks > 0)
8153 if (resize_chunks(conf,
8154 conf->previous_raid_disks
8155 + max(0, mddev->delta_disks),
8156 max(mddev->new_chunk_sectors,
8157 mddev->chunk_sectors)
8158 ) < 0)
8159 return -ENOMEM;
845b9e22
AP
8160
8161 if (conf->previous_raid_disks + mddev->delta_disks <= conf->pool_size)
8162 return 0; /* never bother to shrink */
e56108d6
N
8163 return resize_stripes(conf, (conf->previous_raid_disks
8164 + mddev->delta_disks));
63c70c4f
N
8165}
8166
fd01b88c 8167static int raid5_start_reshape(struct mddev *mddev)
63c70c4f 8168{
d1688a6d 8169 struct r5conf *conf = mddev->private;
3cb03002 8170 struct md_rdev *rdev;
63c70c4f 8171 int spares = 0;
c04be0aa 8172 unsigned long flags;
63c70c4f 8173
f416885e 8174 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
63c70c4f
N
8175 return -EBUSY;
8176
01ee22b4
N
8177 if (!check_stripe_cache(mddev))
8178 return -ENOSPC;
8179
30b67645
N
8180 if (has_failed(conf))
8181 return -EINVAL;
8182
c6563a8c 8183 rdev_for_each(rdev, mddev) {
469518a3
N
8184 if (!test_bit(In_sync, &rdev->flags)
8185 && !test_bit(Faulty, &rdev->flags))
29269553 8186 spares++;
c6563a8c 8187 }
63c70c4f 8188
f416885e 8189 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
29269553
N
8190 /* Not enough devices even to make a degraded array
8191 * of that size
8192 */
8193 return -EINVAL;
8194
ec32a2bd
N
8195 /* Refuse to reduce size of the array. Any reductions in
8196 * array size must be through explicit setting of array_size
8197 * attribute.
8198 */
8199 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
8200 < mddev->array_sectors) {
cc6167b4
N
8201 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
8202 mdname(mddev));
ec32a2bd
N
8203 return -EINVAL;
8204 }
8205
f6705578 8206 atomic_set(&conf->reshape_stripes, 0);
29269553 8207 spin_lock_irq(&conf->device_lock);
c46501b2 8208 write_seqcount_begin(&conf->gen_lock);
29269553 8209 conf->previous_raid_disks = conf->raid_disks;
63c70c4f 8210 conf->raid_disks += mddev->delta_disks;
09c9e5fa
AN
8211 conf->prev_chunk_sectors = conf->chunk_sectors;
8212 conf->chunk_sectors = mddev->new_chunk_sectors;
88ce4930
N
8213 conf->prev_algo = conf->algorithm;
8214 conf->algorithm = mddev->new_layout;
05616be5
N
8215 conf->generation++;
8216 /* Code that selects data_offset needs to see the generation update
8217 * if reshape_progress has been set - so a memory barrier needed.
8218 */
8219 smp_mb();
2c810cdd 8220 if (mddev->reshape_backwards)
fef9c61f
N
8221 conf->reshape_progress = raid5_size(mddev, 0, 0);
8222 else
8223 conf->reshape_progress = 0;
8224 conf->reshape_safe = conf->reshape_progress;
c46501b2 8225 write_seqcount_end(&conf->gen_lock);
29269553
N
8226 spin_unlock_irq(&conf->device_lock);
8227
4d77e3ba
N
8228 /* Now make sure any requests that proceeded on the assumption
8229 * the reshape wasn't running - like Discard or Read - have
8230 * completed.
8231 */
8232 mddev_suspend(mddev);
8233 mddev_resume(mddev);
8234
29269553
N
8235 /* Add some new drives, as many as will fit.
8236 * We know there are enough to make the newly sized array work.
3424bf6a
N
8237 * Don't add devices if we are reducing the number of
8238 * devices in the array. This is because it is not possible
8239 * to correctly record the "partially reconstructed" state of
8240 * such devices during the reshape and confusion could result.
29269553 8241 */
87a8dec9 8242 if (mddev->delta_disks >= 0) {
dafb20fa 8243 rdev_for_each(rdev, mddev)
87a8dec9
N
8244 if (rdev->raid_disk < 0 &&
8245 !test_bit(Faulty, &rdev->flags)) {
8246 if (raid5_add_disk(mddev, rdev) == 0) {
87a8dec9 8247 if (rdev->raid_disk
9d4c7d87 8248 >= conf->previous_raid_disks)
87a8dec9 8249 set_bit(In_sync, &rdev->flags);
9d4c7d87 8250 else
87a8dec9 8251 rdev->recovery_offset = 0;
36fad858 8252
2aada5b1
DLM
8253 /* Failure here is OK */
8254 sysfs_link_rdev(mddev, rdev);
50da0840 8255 }
87a8dec9
N
8256 } else if (rdev->raid_disk >= conf->previous_raid_disks
8257 && !test_bit(Faulty, &rdev->flags)) {
8258 /* This is a spare that was manually added */
8259 set_bit(In_sync, &rdev->flags);
87a8dec9 8260 }
29269553 8261
87a8dec9
N
8262 /* When a reshape changes the number of devices,
8263 * ->degraded is measured against the larger of the
8264 * pre and post number of devices.
8265 */
ec32a2bd 8266 spin_lock_irqsave(&conf->device_lock, flags);
2e38a37f 8267 mddev->degraded = raid5_calc_degraded(conf);
ec32a2bd
N
8268 spin_unlock_irqrestore(&conf->device_lock, flags);
8269 }
63c70c4f 8270 mddev->raid_disks = conf->raid_disks;
e516402c 8271 mddev->reshape_position = conf->reshape_progress;
2953079c 8272 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
f6705578 8273
29269553
N
8274 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8275 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
ea358cd0 8276 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
29269553
N
8277 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8278 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8279 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
0da3c619 8280 "reshape");
29269553
N
8281 if (!mddev->sync_thread) {
8282 mddev->recovery = 0;
8283 spin_lock_irq(&conf->device_lock);
ba8805b9 8284 write_seqcount_begin(&conf->gen_lock);
29269553 8285 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
ba8805b9
N
8286 mddev->new_chunk_sectors =
8287 conf->chunk_sectors = conf->prev_chunk_sectors;
8288 mddev->new_layout = conf->algorithm = conf->prev_algo;
05616be5
N
8289 rdev_for_each(rdev, mddev)
8290 rdev->new_data_offset = rdev->data_offset;
8291 smp_wmb();
ba8805b9 8292 conf->generation --;
fef9c61f 8293 conf->reshape_progress = MaxSector;
1e3fa9bd 8294 mddev->reshape_position = MaxSector;
ba8805b9 8295 write_seqcount_end(&conf->gen_lock);
29269553
N
8296 spin_unlock_irq(&conf->device_lock);
8297 return -EAGAIN;
8298 }
c8f517c4 8299 conf->reshape_checkpoint = jiffies;
29269553
N
8300 md_wakeup_thread(mddev->sync_thread);
8301 md_new_event(mddev);
8302 return 0;
8303}
29269553 8304
ec32a2bd
N
8305/* This is called from the reshape thread and should make any
8306 * changes needed in 'conf'
8307 */
d1688a6d 8308static void end_reshape(struct r5conf *conf)
29269553 8309{
29269553 8310
f6705578 8311 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
db0505d3 8312 struct md_rdev *rdev;
f6705578 8313
f6705578 8314 spin_lock_irq(&conf->device_lock);
cea9c228 8315 conf->previous_raid_disks = conf->raid_disks;
b5d27718 8316 md_finish_reshape(conf->mddev);
05616be5 8317 smp_wmb();
fef9c61f 8318 conf->reshape_progress = MaxSector;
6cbd8148 8319 conf->mddev->reshape_position = MaxSector;
db0505d3
N
8320 rdev_for_each(rdev, conf->mddev)
8321 if (rdev->raid_disk >= 0 &&
8322 !test_bit(Journal, &rdev->flags) &&
8323 !test_bit(In_sync, &rdev->flags))
8324 rdev->recovery_offset = MaxSector;
f6705578 8325 spin_unlock_irq(&conf->device_lock);
b0f9ec04 8326 wake_up(&conf->wait_for_overlap);
16a53ecc 8327
c2e4cd57 8328 if (conf->mddev->queue)
16ef5101 8329 raid5_set_io_opt(conf);
29269553 8330 }
29269553
N
8331}
8332
ec32a2bd
N
8333/* This is called from the raid5d thread with mddev_lock held.
8334 * It makes config changes to the device.
8335 */
fd01b88c 8336static void raid5_finish_reshape(struct mddev *mddev)
cea9c228 8337{
d1688a6d 8338 struct r5conf *conf = mddev->private;
cea9c228
N
8339
8340 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8341
8876391e 8342 if (mddev->delta_disks <= 0) {
ec32a2bd 8343 int d;
908f4fbd 8344 spin_lock_irq(&conf->device_lock);
2e38a37f 8345 mddev->degraded = raid5_calc_degraded(conf);
908f4fbd 8346 spin_unlock_irq(&conf->device_lock);
ec32a2bd
N
8347 for (d = conf->raid_disks ;
8348 d < conf->raid_disks - mddev->delta_disks;
1a67dde0 8349 d++) {
3cb03002 8350 struct md_rdev *rdev = conf->disks[d].rdev;
da7613b8
N
8351 if (rdev)
8352 clear_bit(In_sync, &rdev->flags);
8353 rdev = conf->disks[d].replacement;
8354 if (rdev)
8355 clear_bit(In_sync, &rdev->flags);
1a67dde0 8356 }
cea9c228 8357 }
88ce4930 8358 mddev->layout = conf->algorithm;
09c9e5fa 8359 mddev->chunk_sectors = conf->chunk_sectors;
ec32a2bd
N
8360 mddev->reshape_position = MaxSector;
8361 mddev->delta_disks = 0;
2c810cdd 8362 mddev->reshape_backwards = 0;
cea9c228
N
8363 }
8364}
8365
b03e0ccb 8366static void raid5_quiesce(struct mddev *mddev, int quiesce)
72626685 8367{
d1688a6d 8368 struct r5conf *conf = mddev->private;
72626685 8369
b03e0ccb
N
8370 if (quiesce) {
8371 /* stop all writes */
566c09c5 8372 lock_all_device_hash_locks_irq(conf);
64bd660b
N
8373 /* '2' tells resync/reshape to pause so that all
8374 * active stripes can drain
8375 */
a39f7afd 8376 r5c_flush_cache(conf, INT_MAX);
97ae2725
GO
8377 /* need a memory barrier to make sure read_one_chunk() sees
8378 * quiesce started and reverts to slow (locked) path.
8379 */
8380 smp_store_release(&conf->quiesce, 2);
b1b46486 8381 wait_event_cmd(conf->wait_for_quiescent,
46031f9a
RBJ
8382 atomic_read(&conf->active_stripes) == 0 &&
8383 atomic_read(&conf->active_aligned_reads) == 0,
566c09c5
SL
8384 unlock_all_device_hash_locks_irq(conf),
8385 lock_all_device_hash_locks_irq(conf));
64bd660b 8386 conf->quiesce = 1;
566c09c5 8387 unlock_all_device_hash_locks_irq(conf);
64bd660b
N
8388 /* allow reshape to continue */
8389 wake_up(&conf->wait_for_overlap);
b03e0ccb
N
8390 } else {
8391 /* re-enable writes */
566c09c5 8392 lock_all_device_hash_locks_irq(conf);
72626685 8393 conf->quiesce = 0;
b1b46486 8394 wake_up(&conf->wait_for_quiescent);
e464eafd 8395 wake_up(&conf->wait_for_overlap);
566c09c5 8396 unlock_all_device_hash_locks_irq(conf);
72626685 8397 }
1532d9e8 8398 log_quiesce(conf, quiesce);
72626685 8399}
b15c2e57 8400
fd01b88c 8401static void *raid45_takeover_raid0(struct mddev *mddev, int level)
54071b38 8402{
e373ab10 8403 struct r0conf *raid0_conf = mddev->private;
d76c8420 8404 sector_t sectors;
54071b38 8405
f1b29bca 8406 /* for raid0 takeover only one zone is supported */
e373ab10 8407 if (raid0_conf->nr_strip_zones > 1) {
cc6167b4
N
8408 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
8409 mdname(mddev));
f1b29bca
DW
8410 return ERR_PTR(-EINVAL);
8411 }
8412
e373ab10
N
8413 sectors = raid0_conf->strip_zone[0].zone_end;
8414 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
3b71bd93 8415 mddev->dev_sectors = sectors;
f1b29bca 8416 mddev->new_level = level;
54071b38
TM
8417 mddev->new_layout = ALGORITHM_PARITY_N;
8418 mddev->new_chunk_sectors = mddev->chunk_sectors;
8419 mddev->raid_disks += 1;
8420 mddev->delta_disks = 1;
8421 /* make sure it will be not marked as dirty */
8422 mddev->recovery_cp = MaxSector;
8423
8424 return setup_conf(mddev);
8425}
8426
fd01b88c 8427static void *raid5_takeover_raid1(struct mddev *mddev)
d562b0c4
N
8428{
8429 int chunksect;
6995f0b2 8430 void *ret;
d562b0c4
N
8431
8432 if (mddev->raid_disks != 2 ||
8433 mddev->degraded > 1)
8434 return ERR_PTR(-EINVAL);
8435
8436 /* Should check if there are write-behind devices? */
8437
8438 chunksect = 64*2; /* 64K by default */
8439
8440 /* The array must be an exact multiple of chunksize */
8441 while (chunksect && (mddev->array_sectors & (chunksect-1)))
8442 chunksect >>= 1;
8443
c911c46c 8444 if ((chunksect<<9) < RAID5_STRIPE_SIZE((struct r5conf *)mddev->private))
d562b0c4
N
8445 /* array size does not allow a suitable chunk size */
8446 return ERR_PTR(-EINVAL);
8447
8448 mddev->new_level = 5;
8449 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
664e7c41 8450 mddev->new_chunk_sectors = chunksect;
d562b0c4 8451
6995f0b2 8452 ret = setup_conf(mddev);
32cd7cbb 8453 if (!IS_ERR(ret))
394ed8e4
SL
8454 mddev_clear_unsupported_flags(mddev,
8455 UNSUPPORTED_MDDEV_FLAGS);
6995f0b2 8456 return ret;
d562b0c4
N
8457}
8458
fd01b88c 8459static void *raid5_takeover_raid6(struct mddev *mddev)
fc9739c6
N
8460{
8461 int new_layout;
8462
8463 switch (mddev->layout) {
8464 case ALGORITHM_LEFT_ASYMMETRIC_6:
8465 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
8466 break;
8467 case ALGORITHM_RIGHT_ASYMMETRIC_6:
8468 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
8469 break;
8470 case ALGORITHM_LEFT_SYMMETRIC_6:
8471 new_layout = ALGORITHM_LEFT_SYMMETRIC;
8472 break;
8473 case ALGORITHM_RIGHT_SYMMETRIC_6:
8474 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
8475 break;
8476 case ALGORITHM_PARITY_0_6:
8477 new_layout = ALGORITHM_PARITY_0;
8478 break;
8479 case ALGORITHM_PARITY_N:
8480 new_layout = ALGORITHM_PARITY_N;
8481 break;
8482 default:
8483 return ERR_PTR(-EINVAL);
8484 }
8485 mddev->new_level = 5;
8486 mddev->new_layout = new_layout;
8487 mddev->delta_disks = -1;
8488 mddev->raid_disks -= 1;
8489 return setup_conf(mddev);
8490}
8491
fd01b88c 8492static int raid5_check_reshape(struct mddev *mddev)
b3546035 8493{
88ce4930
N
8494 /* For a 2-drive array, the layout and chunk size can be changed
8495 * immediately as not restriping is needed.
8496 * For larger arrays we record the new value - after validation
8497 * to be used by a reshape pass.
b3546035 8498 */
d1688a6d 8499 struct r5conf *conf = mddev->private;
597a711b 8500 int new_chunk = mddev->new_chunk_sectors;
b3546035 8501
597a711b 8502 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
b3546035
N
8503 return -EINVAL;
8504 if (new_chunk > 0) {
0ba459d2 8505 if (!is_power_of_2(new_chunk))
b3546035 8506 return -EINVAL;
597a711b 8507 if (new_chunk < (PAGE_SIZE>>9))
b3546035 8508 return -EINVAL;
597a711b 8509 if (mddev->array_sectors & (new_chunk-1))
b3546035
N
8510 /* not factor of array size */
8511 return -EINVAL;
8512 }
8513
8514 /* They look valid */
8515
88ce4930 8516 if (mddev->raid_disks == 2) {
597a711b
N
8517 /* can make the change immediately */
8518 if (mddev->new_layout >= 0) {
8519 conf->algorithm = mddev->new_layout;
8520 mddev->layout = mddev->new_layout;
88ce4930
N
8521 }
8522 if (new_chunk > 0) {
597a711b
N
8523 conf->chunk_sectors = new_chunk ;
8524 mddev->chunk_sectors = new_chunk;
88ce4930 8525 }
2953079c 8526 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
88ce4930 8527 md_wakeup_thread(mddev->thread);
b3546035 8528 }
50ac168a 8529 return check_reshape(mddev);
88ce4930
N
8530}
8531
fd01b88c 8532static int raid6_check_reshape(struct mddev *mddev)
88ce4930 8533{
597a711b 8534 int new_chunk = mddev->new_chunk_sectors;
50ac168a 8535
597a711b 8536 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
88ce4930 8537 return -EINVAL;
b3546035 8538 if (new_chunk > 0) {
0ba459d2 8539 if (!is_power_of_2(new_chunk))
88ce4930 8540 return -EINVAL;
597a711b 8541 if (new_chunk < (PAGE_SIZE >> 9))
88ce4930 8542 return -EINVAL;
597a711b 8543 if (mddev->array_sectors & (new_chunk-1))
88ce4930
N
8544 /* not factor of array size */
8545 return -EINVAL;
b3546035 8546 }
88ce4930
N
8547
8548 /* They look valid */
50ac168a 8549 return check_reshape(mddev);
b3546035
N
8550}
8551
fd01b88c 8552static void *raid5_takeover(struct mddev *mddev)
d562b0c4
N
8553{
8554 /* raid5 can take over:
f1b29bca 8555 * raid0 - if there is only one strip zone - make it a raid4 layout
d562b0c4
N
8556 * raid1 - if there are two drives. We need to know the chunk size
8557 * raid4 - trivial - just use a raid4 layout.
8558 * raid6 - Providing it is a *_6 layout
d562b0c4 8559 */
f1b29bca
DW
8560 if (mddev->level == 0)
8561 return raid45_takeover_raid0(mddev, 5);
d562b0c4
N
8562 if (mddev->level == 1)
8563 return raid5_takeover_raid1(mddev);
e9d4758f
N
8564 if (mddev->level == 4) {
8565 mddev->new_layout = ALGORITHM_PARITY_N;
8566 mddev->new_level = 5;
8567 return setup_conf(mddev);
8568 }
fc9739c6
N
8569 if (mddev->level == 6)
8570 return raid5_takeover_raid6(mddev);
d562b0c4
N
8571
8572 return ERR_PTR(-EINVAL);
8573}
8574
fd01b88c 8575static void *raid4_takeover(struct mddev *mddev)
a78d38a1 8576{
f1b29bca
DW
8577 /* raid4 can take over:
8578 * raid0 - if there is only one strip zone
8579 * raid5 - if layout is right
a78d38a1 8580 */
f1b29bca
DW
8581 if (mddev->level == 0)
8582 return raid45_takeover_raid0(mddev, 4);
a78d38a1
N
8583 if (mddev->level == 5 &&
8584 mddev->layout == ALGORITHM_PARITY_N) {
8585 mddev->new_layout = 0;
8586 mddev->new_level = 4;
8587 return setup_conf(mddev);
8588 }
8589 return ERR_PTR(-EINVAL);
8590}
d562b0c4 8591
84fc4b56 8592static struct md_personality raid5_personality;
245f46c2 8593
fd01b88c 8594static void *raid6_takeover(struct mddev *mddev)
245f46c2
N
8595{
8596 /* Currently can only take over a raid5. We map the
8597 * personality to an equivalent raid6 personality
8598 * with the Q block at the end.
8599 */
8600 int new_layout;
8601
8602 if (mddev->pers != &raid5_personality)
8603 return ERR_PTR(-EINVAL);
8604 if (mddev->degraded > 1)
8605 return ERR_PTR(-EINVAL);
8606 if (mddev->raid_disks > 253)
8607 return ERR_PTR(-EINVAL);
8608 if (mddev->raid_disks < 3)
8609 return ERR_PTR(-EINVAL);
8610
8611 switch (mddev->layout) {
8612 case ALGORITHM_LEFT_ASYMMETRIC:
8613 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
8614 break;
8615 case ALGORITHM_RIGHT_ASYMMETRIC:
8616 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
8617 break;
8618 case ALGORITHM_LEFT_SYMMETRIC:
8619 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
8620 break;
8621 case ALGORITHM_RIGHT_SYMMETRIC:
8622 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
8623 break;
8624 case ALGORITHM_PARITY_0:
8625 new_layout = ALGORITHM_PARITY_0_6;
8626 break;
8627 case ALGORITHM_PARITY_N:
8628 new_layout = ALGORITHM_PARITY_N;
8629 break;
8630 default:
8631 return ERR_PTR(-EINVAL);
8632 }
8633 mddev->new_level = 6;
8634 mddev->new_layout = new_layout;
8635 mddev->delta_disks = 1;
8636 mddev->raid_disks += 1;
8637 return setup_conf(mddev);
8638}
8639
ba903a3e
AP
8640static int raid5_change_consistency_policy(struct mddev *mddev, const char *buf)
8641{
8642 struct r5conf *conf;
8643 int err;
8644
8645 err = mddev_lock(mddev);
8646 if (err)
8647 return err;
8648 conf = mddev->private;
8649 if (!conf) {
8650 mddev_unlock(mddev);
8651 return -ENODEV;
8652 }
8653
845b9e22 8654 if (strncmp(buf, "ppl", 3) == 0) {
0bb0c105 8655 /* ppl only works with RAID 5 */
845b9e22
AP
8656 if (!raid5_has_ppl(conf) && conf->level == 5) {
8657 err = log_init(conf, NULL, true);
8658 if (!err) {
8659 err = resize_stripes(conf, conf->pool_size);
8660 if (err)
8661 log_exit(conf);
8662 }
0bb0c105
SL
8663 } else
8664 err = -EINVAL;
8665 } else if (strncmp(buf, "resync", 6) == 0) {
8666 if (raid5_has_ppl(conf)) {
8667 mddev_suspend(mddev);
8668 log_exit(conf);
0bb0c105 8669 mddev_resume(mddev);
845b9e22 8670 err = resize_stripes(conf, conf->pool_size);
0bb0c105
SL
8671 } else if (test_bit(MD_HAS_JOURNAL, &conf->mddev->flags) &&
8672 r5l_log_disk_error(conf)) {
8673 bool journal_dev_exists = false;
8674 struct md_rdev *rdev;
8675
8676 rdev_for_each(rdev, mddev)
8677 if (test_bit(Journal, &rdev->flags)) {
8678 journal_dev_exists = true;
8679 break;
8680 }
8681
8682 if (!journal_dev_exists) {
8683 mddev_suspend(mddev);
8684 clear_bit(MD_HAS_JOURNAL, &mddev->flags);
8685 mddev_resume(mddev);
8686 } else /* need remove journal device first */
8687 err = -EBUSY;
8688 } else
8689 err = -EINVAL;
ba903a3e
AP
8690 } else {
8691 err = -EINVAL;
8692 }
8693
8694 if (!err)
8695 md_update_sb(mddev, 1);
8696
8697 mddev_unlock(mddev);
8698
8699 return err;
8700}
8701
d5d885fd
SL
8702static int raid5_start(struct mddev *mddev)
8703{
8704 struct r5conf *conf = mddev->private;
8705
8706 return r5l_start(conf->log);
8707}
8708
84fc4b56 8709static struct md_personality raid6_personality =
16a53ecc
N
8710{
8711 .name = "raid6",
8712 .level = 6,
8713 .owner = THIS_MODULE,
849674e4
SL
8714 .make_request = raid5_make_request,
8715 .run = raid5_run,
d5d885fd 8716 .start = raid5_start,
afa0f557 8717 .free = raid5_free,
849674e4
SL
8718 .status = raid5_status,
8719 .error_handler = raid5_error,
16a53ecc
N
8720 .hot_add_disk = raid5_add_disk,
8721 .hot_remove_disk= raid5_remove_disk,
8722 .spare_active = raid5_spare_active,
849674e4 8723 .sync_request = raid5_sync_request,
16a53ecc 8724 .resize = raid5_resize,
80c3a6ce 8725 .size = raid5_size,
50ac168a 8726 .check_reshape = raid6_check_reshape,
f416885e 8727 .start_reshape = raid5_start_reshape,
cea9c228 8728 .finish_reshape = raid5_finish_reshape,
16a53ecc 8729 .quiesce = raid5_quiesce,
245f46c2 8730 .takeover = raid6_takeover,
0bb0c105 8731 .change_consistency_policy = raid5_change_consistency_policy,
16a53ecc 8732};
84fc4b56 8733static struct md_personality raid5_personality =
1da177e4
LT
8734{
8735 .name = "raid5",
2604b703 8736 .level = 5,
1da177e4 8737 .owner = THIS_MODULE,
849674e4
SL
8738 .make_request = raid5_make_request,
8739 .run = raid5_run,
d5d885fd 8740 .start = raid5_start,
afa0f557 8741 .free = raid5_free,
849674e4
SL
8742 .status = raid5_status,
8743 .error_handler = raid5_error,
1da177e4
LT
8744 .hot_add_disk = raid5_add_disk,
8745 .hot_remove_disk= raid5_remove_disk,
8746 .spare_active = raid5_spare_active,
849674e4 8747 .sync_request = raid5_sync_request,
1da177e4 8748 .resize = raid5_resize,
80c3a6ce 8749 .size = raid5_size,
63c70c4f
N
8750 .check_reshape = raid5_check_reshape,
8751 .start_reshape = raid5_start_reshape,
cea9c228 8752 .finish_reshape = raid5_finish_reshape,
72626685 8753 .quiesce = raid5_quiesce,
d562b0c4 8754 .takeover = raid5_takeover,
ba903a3e 8755 .change_consistency_policy = raid5_change_consistency_policy,
1da177e4
LT
8756};
8757
84fc4b56 8758static struct md_personality raid4_personality =
1da177e4 8759{
2604b703
N
8760 .name = "raid4",
8761 .level = 4,
8762 .owner = THIS_MODULE,
849674e4
SL
8763 .make_request = raid5_make_request,
8764 .run = raid5_run,
d5d885fd 8765 .start = raid5_start,
afa0f557 8766 .free = raid5_free,
849674e4
SL
8767 .status = raid5_status,
8768 .error_handler = raid5_error,
2604b703
N
8769 .hot_add_disk = raid5_add_disk,
8770 .hot_remove_disk= raid5_remove_disk,
8771 .spare_active = raid5_spare_active,
849674e4 8772 .sync_request = raid5_sync_request,
2604b703 8773 .resize = raid5_resize,
80c3a6ce 8774 .size = raid5_size,
3d37890b
N
8775 .check_reshape = raid5_check_reshape,
8776 .start_reshape = raid5_start_reshape,
cea9c228 8777 .finish_reshape = raid5_finish_reshape,
2604b703 8778 .quiesce = raid5_quiesce,
a78d38a1 8779 .takeover = raid4_takeover,
0bb0c105 8780 .change_consistency_policy = raid5_change_consistency_policy,
2604b703
N
8781};
8782
8783static int __init raid5_init(void)
8784{
29c6d1bb
SAS
8785 int ret;
8786
851c30c9
SL
8787 raid5_wq = alloc_workqueue("raid5wq",
8788 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
8789 if (!raid5_wq)
8790 return -ENOMEM;
29c6d1bb
SAS
8791
8792 ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
8793 "md/raid5:prepare",
8794 raid456_cpu_up_prepare,
8795 raid456_cpu_dead);
8796 if (ret) {
8797 destroy_workqueue(raid5_wq);
8798 return ret;
8799 }
16a53ecc 8800 register_md_personality(&raid6_personality);
2604b703
N
8801 register_md_personality(&raid5_personality);
8802 register_md_personality(&raid4_personality);
8803 return 0;
1da177e4
LT
8804}
8805
2604b703 8806static void raid5_exit(void)
1da177e4 8807{
16a53ecc 8808 unregister_md_personality(&raid6_personality);
2604b703
N
8809 unregister_md_personality(&raid5_personality);
8810 unregister_md_personality(&raid4_personality);
29c6d1bb 8811 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
851c30c9 8812 destroy_workqueue(raid5_wq);
1da177e4
LT
8813}
8814
8815module_init(raid5_init);
8816module_exit(raid5_exit);
8817MODULE_LICENSE("GPL");
0efb9e61 8818MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
1da177e4 8819MODULE_ALIAS("md-personality-4"); /* RAID5 */
d9d166c2
N
8820MODULE_ALIAS("md-raid5");
8821MODULE_ALIAS("md-raid4");
2604b703
N
8822MODULE_ALIAS("md-level-5");
8823MODULE_ALIAS("md-level-4");
16a53ecc
N
8824MODULE_ALIAS("md-personality-8"); /* RAID6 */
8825MODULE_ALIAS("md-raid6");
8826MODULE_ALIAS("md-level-6");
8827
8828/* This used to be two separate modules, they were: */
8829MODULE_ALIAS("raid5");
8830MODULE_ALIAS("raid6");