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