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