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