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