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