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