md/r5cache: State machine for raid5-cache write back mode
[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 }
2ded3703
SL
4110
4111 if (test_bit(R5_InJournal, &dev->flags))
4112 s->injournal++;
1da177e4 4113 }
9a3e1101
N
4114 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4115 /* If there is a failed device being replaced,
4116 * we must be recovering.
4117 * else if we are after recovery_cp, we must be syncing
c6d2e084 4118 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
9a3e1101
N
4119 * else we can only be replacing
4120 * sync and recovery both need to read all devices, and so
4121 * use the same flag.
4122 */
4123 if (do_recovery ||
c6d2e084 4124 sh->sector >= conf->mddev->recovery_cp ||
4125 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
9a3e1101
N
4126 s->syncing = 1;
4127 else
4128 s->replacing = 1;
4129 }
1da177e4 4130 rcu_read_unlock();
cc94015a
N
4131}
4132
59fc630b 4133static int clear_batch_ready(struct stripe_head *sh)
4134{
b15a9dbd
N
4135 /* Return '1' if this is a member of batch, or
4136 * '0' if it is a lone stripe or a head which can now be
4137 * handled.
4138 */
59fc630b 4139 struct stripe_head *tmp;
4140 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
b15a9dbd 4141 return (sh->batch_head && sh->batch_head != sh);
59fc630b 4142 spin_lock(&sh->stripe_lock);
4143 if (!sh->batch_head) {
4144 spin_unlock(&sh->stripe_lock);
4145 return 0;
4146 }
4147
4148 /*
4149 * this stripe could be added to a batch list before we check
4150 * BATCH_READY, skips it
4151 */
4152 if (sh->batch_head != sh) {
4153 spin_unlock(&sh->stripe_lock);
4154 return 1;
4155 }
4156 spin_lock(&sh->batch_lock);
4157 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4158 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4159 spin_unlock(&sh->batch_lock);
4160 spin_unlock(&sh->stripe_lock);
4161
4162 /*
4163 * BATCH_READY is cleared, no new stripes can be added.
4164 * batch_list can be accessed without lock
4165 */
4166 return 0;
4167}
4168
3960ce79
N
4169static void break_stripe_batch_list(struct stripe_head *head_sh,
4170 unsigned long handle_flags)
72ac7330 4171{
4e3d62ff 4172 struct stripe_head *sh, *next;
72ac7330 4173 int i;
fb642b92 4174 int do_wakeup = 0;
72ac7330 4175
bb27051f
N
4176 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4177
72ac7330 4178 list_del_init(&sh->batch_list);
4179
fb3229d5 4180 WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
1b956f7a
N
4181 (1 << STRIPE_SYNCING) |
4182 (1 << STRIPE_REPLACED) |
1b956f7a
N
4183 (1 << STRIPE_DELAYED) |
4184 (1 << STRIPE_BIT_DELAY) |
4185 (1 << STRIPE_FULL_WRITE) |
4186 (1 << STRIPE_BIOFILL_RUN) |
4187 (1 << STRIPE_COMPUTE_RUN) |
4188 (1 << STRIPE_OPS_REQ_PENDING) |
4189 (1 << STRIPE_DISCARD) |
4190 (1 << STRIPE_BATCH_READY) |
4191 (1 << STRIPE_BATCH_ERR) |
fb3229d5
SL
4192 (1 << STRIPE_BITMAP_PENDING)),
4193 "stripe state: %lx\n", sh->state);
4194 WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4195 (1 << STRIPE_REPLACED)),
4196 "head stripe state: %lx\n", head_sh->state);
1b956f7a
N
4197
4198 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
550da24f 4199 (1 << STRIPE_PREREAD_ACTIVE) |
1b956f7a
N
4200 (1 << STRIPE_DEGRADED)),
4201 head_sh->state & (1 << STRIPE_INSYNC));
4202
72ac7330 4203 sh->check_state = head_sh->check_state;
4204 sh->reconstruct_state = head_sh->reconstruct_state;
fb642b92
N
4205 for (i = 0; i < sh->disks; i++) {
4206 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4207 do_wakeup = 1;
72ac7330 4208 sh->dev[i].flags = head_sh->dev[i].flags &
4209 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
fb642b92 4210 }
72ac7330 4211 spin_lock_irq(&sh->stripe_lock);
4212 sh->batch_head = NULL;
4213 spin_unlock_irq(&sh->stripe_lock);
3960ce79
N
4214 if (handle_flags == 0 ||
4215 sh->state & handle_flags)
4216 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 4217 raid5_release_stripe(sh);
72ac7330 4218 }
fb642b92
N
4219 spin_lock_irq(&head_sh->stripe_lock);
4220 head_sh->batch_head = NULL;
4221 spin_unlock_irq(&head_sh->stripe_lock);
4222 for (i = 0; i < head_sh->disks; i++)
4223 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4224 do_wakeup = 1;
3960ce79
N
4225 if (head_sh->state & handle_flags)
4226 set_bit(STRIPE_HANDLE, &head_sh->state);
fb642b92
N
4227
4228 if (do_wakeup)
4229 wake_up(&head_sh->raid_conf->wait_for_overlap);
72ac7330 4230}
4231
cc94015a
N
4232static void handle_stripe(struct stripe_head *sh)
4233{
4234 struct stripe_head_state s;
d1688a6d 4235 struct r5conf *conf = sh->raid_conf;
3687c061 4236 int i;
84789554
N
4237 int prexor;
4238 int disks = sh->disks;
474af965 4239 struct r5dev *pdev, *qdev;
cc94015a
N
4240
4241 clear_bit(STRIPE_HANDLE, &sh->state);
257a4b42 4242 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
cc94015a
N
4243 /* already being handled, ensure it gets handled
4244 * again when current action finishes */
4245 set_bit(STRIPE_HANDLE, &sh->state);
4246 return;
4247 }
4248
59fc630b 4249 if (clear_batch_ready(sh) ) {
4250 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
4251 return;
4252 }
4253
4e3d62ff 4254 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
3960ce79 4255 break_stripe_batch_list(sh, 0);
72ac7330 4256
dabc4ec6 4257 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
f8dfcffd
N
4258 spin_lock(&sh->stripe_lock);
4259 /* Cannot process 'sync' concurrently with 'discard' */
4260 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
4261 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4262 set_bit(STRIPE_SYNCING, &sh->state);
4263 clear_bit(STRIPE_INSYNC, &sh->state);
f94c0b66 4264 clear_bit(STRIPE_REPLACED, &sh->state);
f8dfcffd
N
4265 }
4266 spin_unlock(&sh->stripe_lock);
cc94015a
N
4267 }
4268 clear_bit(STRIPE_DELAYED, &sh->state);
4269
4270 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4271 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4272 (unsigned long long)sh->sector, sh->state,
4273 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4274 sh->check_state, sh->reconstruct_state);
3687c061 4275
acfe726b 4276 analyse_stripe(sh, &s);
c5a31000 4277
b70abcb2
SL
4278 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
4279 goto finish;
4280
bc2607f3
N
4281 if (s.handle_bad_blocks) {
4282 set_bit(STRIPE_HANDLE, &sh->state);
4283 goto finish;
4284 }
4285
474af965
N
4286 if (unlikely(s.blocked_rdev)) {
4287 if (s.syncing || s.expanding || s.expanded ||
9a3e1101 4288 s.replacing || s.to_write || s.written) {
474af965
N
4289 set_bit(STRIPE_HANDLE, &sh->state);
4290 goto finish;
4291 }
4292 /* There is nothing for the blocked_rdev to block */
4293 rdev_dec_pending(s.blocked_rdev, conf->mddev);
4294 s.blocked_rdev = NULL;
4295 }
4296
4297 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4298 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4299 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4300 }
4301
4302 pr_debug("locked=%d uptodate=%d to_read=%d"
4303 " to_write=%d failed=%d failed_num=%d,%d\n",
4304 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4305 s.failed_num[0], s.failed_num[1]);
4306 /* check if the array has lost more than max_degraded devices and,
4307 * if so, some requests might need to be failed.
4308 */
6e74a9cf 4309 if (s.failed > conf->max_degraded || s.log_failed) {
9a3f530f
N
4310 sh->check_state = 0;
4311 sh->reconstruct_state = 0;
626f2092 4312 break_stripe_batch_list(sh, 0);
9a3f530f
N
4313 if (s.to_read+s.to_write+s.written)
4314 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
9a3e1101 4315 if (s.syncing + s.replacing)
9a3f530f
N
4316 handle_failed_sync(conf, sh, &s);
4317 }
474af965 4318
84789554
N
4319 /* Now we check to see if any write operations have recently
4320 * completed
4321 */
4322 prexor = 0;
4323 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4324 prexor = 1;
4325 if (sh->reconstruct_state == reconstruct_state_drain_result ||
4326 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
4327 sh->reconstruct_state = reconstruct_state_idle;
4328
4329 /* All the 'written' buffers and the parity block are ready to
4330 * be written back to disk
4331 */
9e444768
SL
4332 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
4333 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
84789554 4334 BUG_ON(sh->qd_idx >= 0 &&
9e444768
SL
4335 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
4336 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
84789554
N
4337 for (i = disks; i--; ) {
4338 struct r5dev *dev = &sh->dev[i];
4339 if (test_bit(R5_LOCKED, &dev->flags) &&
4340 (i == sh->pd_idx || i == sh->qd_idx ||
4341 dev->written)) {
4342 pr_debug("Writing block %d\n", i);
4343 set_bit(R5_Wantwrite, &dev->flags);
4344 if (prexor)
4345 continue;
9c4bdf69
N
4346 if (s.failed > 1)
4347 continue;
84789554
N
4348 if (!test_bit(R5_Insync, &dev->flags) ||
4349 ((i == sh->pd_idx || i == sh->qd_idx) &&
4350 s.failed == 0))
4351 set_bit(STRIPE_INSYNC, &sh->state);
4352 }
4353 }
4354 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4355 s.dec_preread_active = 1;
4356 }
4357
ef5b7c69
N
4358 /*
4359 * might be able to return some write requests if the parity blocks
4360 * are safe, or on a failed drive
4361 */
4362 pdev = &sh->dev[sh->pd_idx];
4363 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
4364 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
4365 qdev = &sh->dev[sh->qd_idx];
4366 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
4367 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
4368 || conf->level < 6;
4369
4370 if (s.written &&
4371 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
4372 && !test_bit(R5_LOCKED, &pdev->flags)
4373 && (test_bit(R5_UPTODATE, &pdev->flags) ||
4374 test_bit(R5_Discard, &pdev->flags))))) &&
4375 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
4376 && !test_bit(R5_LOCKED, &qdev->flags)
4377 && (test_bit(R5_UPTODATE, &qdev->flags) ||
4378 test_bit(R5_Discard, &qdev->flags))))))
4379 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
4380
4381 /* Now we might consider reading some blocks, either to check/generate
4382 * parity, or to satisfy requests
4383 * or to load a block that is being partially written.
4384 */
4385 if (s.to_read || s.non_overwrite
4386 || (conf->level == 6 && s.to_write && s.failed)
4387 || (s.syncing && (s.uptodate + s.compute < disks))
4388 || s.replacing
4389 || s.expanding)
4390 handle_stripe_fill(sh, &s, disks);
4391
2ded3703
SL
4392 /*
4393 * When the stripe finishes full journal write cycle (write to journal
4394 * and raid disk), this is the clean up procedure so it is ready for
4395 * next operation.
4396 */
4397 r5c_finish_stripe_write_out(conf, sh, &s);
4398
4399 /*
4400 * Now to consider new write requests, cache write back and what else,
4401 * if anything should be read. We do not handle new writes when:
84789554
N
4402 * 1/ A 'write' operation (copy+xor) is already in flight.
4403 * 2/ A 'check' operation is in flight, as it may clobber the parity
4404 * block.
2ded3703 4405 * 3/ A r5c cache log write is in flight.
84789554 4406 */
2ded3703
SL
4407
4408 if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
4409 if (!r5c_is_writeback(conf->log)) {
4410 if (s.to_write)
4411 handle_stripe_dirtying(conf, sh, &s, disks);
4412 } else { /* write back cache */
4413 int ret = 0;
4414
4415 /* First, try handle writes in caching phase */
4416 if (s.to_write)
4417 ret = r5c_try_caching_write(conf, sh, &s,
4418 disks);
4419 /*
4420 * If caching phase failed: ret == -EAGAIN
4421 * OR
4422 * stripe under reclaim: !caching && injournal
4423 *
4424 * fall back to handle_stripe_dirtying()
4425 */
4426 if (ret == -EAGAIN ||
4427 /* stripe under reclaim: !caching && injournal */
4428 (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
4429 s.injournal > 0))
4430 handle_stripe_dirtying(conf, sh, &s, disks);
4431 }
4432 }
84789554
N
4433
4434 /* maybe we need to check and possibly fix the parity for this stripe
4435 * Any reads will already have been scheduled, so we just see if enough
4436 * data is available. The parity check is held off while parity
4437 * dependent operations are in flight.
4438 */
4439 if (sh->check_state ||
4440 (s.syncing && s.locked == 0 &&
4441 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
4442 !test_bit(STRIPE_INSYNC, &sh->state))) {
4443 if (conf->level == 6)
4444 handle_parity_checks6(conf, sh, &s, disks);
4445 else
4446 handle_parity_checks5(conf, sh, &s, disks);
4447 }
c5a31000 4448
f94c0b66
N
4449 if ((s.replacing || s.syncing) && s.locked == 0
4450 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
4451 && !test_bit(STRIPE_REPLACED, &sh->state)) {
9a3e1101
N
4452 /* Write out to replacement devices where possible */
4453 for (i = 0; i < conf->raid_disks; i++)
f94c0b66
N
4454 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
4455 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
9a3e1101
N
4456 set_bit(R5_WantReplace, &sh->dev[i].flags);
4457 set_bit(R5_LOCKED, &sh->dev[i].flags);
4458 s.locked++;
4459 }
f94c0b66
N
4460 if (s.replacing)
4461 set_bit(STRIPE_INSYNC, &sh->state);
4462 set_bit(STRIPE_REPLACED, &sh->state);
9a3e1101
N
4463 }
4464 if ((s.syncing || s.replacing) && s.locked == 0 &&
f94c0b66 4465 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
9a3e1101 4466 test_bit(STRIPE_INSYNC, &sh->state)) {
c5a31000
N
4467 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4468 clear_bit(STRIPE_SYNCING, &sh->state);
f8dfcffd
N
4469 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
4470 wake_up(&conf->wait_for_overlap);
c5a31000
N
4471 }
4472
4473 /* If the failed drives are just a ReadError, then we might need
4474 * to progress the repair/check process
4475 */
4476 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
4477 for (i = 0; i < s.failed; i++) {
4478 struct r5dev *dev = &sh->dev[s.failed_num[i]];
4479 if (test_bit(R5_ReadError, &dev->flags)
4480 && !test_bit(R5_LOCKED, &dev->flags)
4481 && test_bit(R5_UPTODATE, &dev->flags)
4482 ) {
4483 if (!test_bit(R5_ReWrite, &dev->flags)) {
4484 set_bit(R5_Wantwrite, &dev->flags);
4485 set_bit(R5_ReWrite, &dev->flags);
4486 set_bit(R5_LOCKED, &dev->flags);
4487 s.locked++;
4488 } else {
4489 /* let's read it back */
4490 set_bit(R5_Wantread, &dev->flags);
4491 set_bit(R5_LOCKED, &dev->flags);
4492 s.locked++;
4493 }
4494 }
4495 }
4496
3687c061
N
4497 /* Finish reconstruct operations initiated by the expansion process */
4498 if (sh->reconstruct_state == reconstruct_state_result) {
4499 struct stripe_head *sh_src
6d036f7d 4500 = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
3687c061
N
4501 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
4502 /* sh cannot be written until sh_src has been read.
4503 * so arrange for sh to be delayed a little
4504 */
4505 set_bit(STRIPE_DELAYED, &sh->state);
4506 set_bit(STRIPE_HANDLE, &sh->state);
4507 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
4508 &sh_src->state))
4509 atomic_inc(&conf->preread_active_stripes);
6d036f7d 4510 raid5_release_stripe(sh_src);
3687c061
N
4511 goto finish;
4512 }
4513 if (sh_src)
6d036f7d 4514 raid5_release_stripe(sh_src);
3687c061
N
4515
4516 sh->reconstruct_state = reconstruct_state_idle;
4517 clear_bit(STRIPE_EXPANDING, &sh->state);
4518 for (i = conf->raid_disks; i--; ) {
4519 set_bit(R5_Wantwrite, &sh->dev[i].flags);
4520 set_bit(R5_LOCKED, &sh->dev[i].flags);
4521 s.locked++;
4522 }
4523 }
f416885e 4524
3687c061
N
4525 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
4526 !sh->reconstruct_state) {
4527 /* Need to write out all blocks after computing parity */
4528 sh->disks = conf->raid_disks;
4529 stripe_set_idx(sh->sector, conf, 0, sh);
4530 schedule_reconstruction(sh, &s, 1, 1);
4531 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
4532 clear_bit(STRIPE_EXPAND_READY, &sh->state);
4533 atomic_dec(&conf->reshape_stripes);
4534 wake_up(&conf->wait_for_overlap);
4535 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4536 }
4537
4538 if (s.expanding && s.locked == 0 &&
4539 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
4540 handle_stripe_expansion(conf, sh);
16a53ecc 4541
3687c061 4542finish:
6bfe0b49 4543 /* wait for this device to become unblocked */
5f066c63
N
4544 if (unlikely(s.blocked_rdev)) {
4545 if (conf->mddev->external)
4546 md_wait_for_blocked_rdev(s.blocked_rdev,
4547 conf->mddev);
4548 else
4549 /* Internal metadata will immediately
4550 * be written by raid5d, so we don't
4551 * need to wait here.
4552 */
4553 rdev_dec_pending(s.blocked_rdev,
4554 conf->mddev);
4555 }
6bfe0b49 4556
bc2607f3
N
4557 if (s.handle_bad_blocks)
4558 for (i = disks; i--; ) {
3cb03002 4559 struct md_rdev *rdev;
bc2607f3
N
4560 struct r5dev *dev = &sh->dev[i];
4561 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
4562 /* We own a safe reference to the rdev */
4563 rdev = conf->disks[i].rdev;
4564 if (!rdev_set_badblocks(rdev, sh->sector,
4565 STRIPE_SECTORS, 0))
4566 md_error(conf->mddev, rdev);
4567 rdev_dec_pending(rdev, conf->mddev);
4568 }
b84db560
N
4569 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
4570 rdev = conf->disks[i].rdev;
4571 rdev_clear_badblocks(rdev, sh->sector,
c6563a8c 4572 STRIPE_SECTORS, 0);
b84db560
N
4573 rdev_dec_pending(rdev, conf->mddev);
4574 }
977df362
N
4575 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
4576 rdev = conf->disks[i].replacement;
dd054fce
N
4577 if (!rdev)
4578 /* rdev have been moved down */
4579 rdev = conf->disks[i].rdev;
977df362 4580 rdev_clear_badblocks(rdev, sh->sector,
c6563a8c 4581 STRIPE_SECTORS, 0);
977df362
N
4582 rdev_dec_pending(rdev, conf->mddev);
4583 }
bc2607f3
N
4584 }
4585
6c0069c0
YT
4586 if (s.ops_request)
4587 raid_run_ops(sh, s.ops_request);
4588
f0e43bcd 4589 ops_run_io(sh, &s);
16a53ecc 4590
c5709ef6 4591 if (s.dec_preread_active) {
729a1866 4592 /* We delay this until after ops_run_io so that if make_request
e9c7469b 4593 * is waiting on a flush, it won't continue until the writes
729a1866
N
4594 * have actually been submitted.
4595 */
4596 atomic_dec(&conf->preread_active_stripes);
4597 if (atomic_read(&conf->preread_active_stripes) <
4598 IO_THRESHOLD)
4599 md_wakeup_thread(conf->mddev->thread);
4600 }
4601
c3cce6cd 4602 if (!bio_list_empty(&s.return_bi)) {
7adb072c 4603 if (test_bit(MD_CHANGE_PENDING, &conf->mddev->flags)) {
c3cce6cd
N
4604 spin_lock_irq(&conf->device_lock);
4605 bio_list_merge(&conf->return_bi, &s.return_bi);
4606 spin_unlock_irq(&conf->device_lock);
4607 md_wakeup_thread(conf->mddev->thread);
4608 } else
4609 return_io(&s.return_bi);
4610 }
16a53ecc 4611
257a4b42 4612 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
16a53ecc
N
4613}
4614
d1688a6d 4615static void raid5_activate_delayed(struct r5conf *conf)
16a53ecc
N
4616{
4617 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
4618 while (!list_empty(&conf->delayed_list)) {
4619 struct list_head *l = conf->delayed_list.next;
4620 struct stripe_head *sh;
4621 sh = list_entry(l, struct stripe_head, lru);
4622 list_del_init(l);
4623 clear_bit(STRIPE_DELAYED, &sh->state);
4624 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4625 atomic_inc(&conf->preread_active_stripes);
8b3e6cdc 4626 list_add_tail(&sh->lru, &conf->hold_list);
851c30c9 4627 raid5_wakeup_stripe_thread(sh);
16a53ecc 4628 }
482c0834 4629 }
16a53ecc
N
4630}
4631
566c09c5
SL
4632static void activate_bit_delay(struct r5conf *conf,
4633 struct list_head *temp_inactive_list)
16a53ecc
N
4634{
4635 /* device_lock is held */
4636 struct list_head head;
4637 list_add(&head, &conf->bitmap_list);
4638 list_del_init(&conf->bitmap_list);
4639 while (!list_empty(&head)) {
4640 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
566c09c5 4641 int hash;
16a53ecc
N
4642 list_del_init(&sh->lru);
4643 atomic_inc(&sh->count);
566c09c5
SL
4644 hash = sh->hash_lock_index;
4645 __release_stripe(conf, sh, &temp_inactive_list[hash]);
16a53ecc
N
4646 }
4647}
4648
5c675f83 4649static int raid5_congested(struct mddev *mddev, int bits)
f022b2fd 4650{
d1688a6d 4651 struct r5conf *conf = mddev->private;
f022b2fd
N
4652
4653 /* No difference between reads and writes. Just check
4654 * how busy the stripe_cache is
4655 */
3fa841d7 4656
5423399a 4657 if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state))
f022b2fd
N
4658 return 1;
4659 if (conf->quiesce)
4660 return 1;
4bda556a 4661 if (atomic_read(&conf->empty_inactive_list_nr))
f022b2fd
N
4662 return 1;
4663
4664 return 0;
4665}
4666
fd01b88c 4667static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
f679623f 4668{
3cb5edf4 4669 struct r5conf *conf = mddev->private;
4f024f37 4670 sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev);
3cb5edf4 4671 unsigned int chunk_sectors;
aa8b57aa 4672 unsigned int bio_sectors = bio_sectors(bio);
f679623f 4673
3cb5edf4 4674 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
f679623f
RBJ
4675 return chunk_sectors >=
4676 ((sector & (chunk_sectors - 1)) + bio_sectors);
4677}
4678
46031f9a
RBJ
4679/*
4680 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4681 * later sampled by raid5d.
4682 */
d1688a6d 4683static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
46031f9a
RBJ
4684{
4685 unsigned long flags;
4686
4687 spin_lock_irqsave(&conf->device_lock, flags);
4688
4689 bi->bi_next = conf->retry_read_aligned_list;
4690 conf->retry_read_aligned_list = bi;
4691
4692 spin_unlock_irqrestore(&conf->device_lock, flags);
4693 md_wakeup_thread(conf->mddev->thread);
4694}
4695
d1688a6d 4696static struct bio *remove_bio_from_retry(struct r5conf *conf)
46031f9a
RBJ
4697{
4698 struct bio *bi;
4699
4700 bi = conf->retry_read_aligned;
4701 if (bi) {
4702 conf->retry_read_aligned = NULL;
4703 return bi;
4704 }
4705 bi = conf->retry_read_aligned_list;
4706 if(bi) {
387bb173 4707 conf->retry_read_aligned_list = bi->bi_next;
46031f9a 4708 bi->bi_next = NULL;
960e739d
JA
4709 /*
4710 * this sets the active strip count to 1 and the processed
4711 * strip count to zero (upper 8 bits)
4712 */
e7836bd6 4713 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
46031f9a
RBJ
4714 }
4715
4716 return bi;
4717}
4718
f679623f
RBJ
4719/*
4720 * The "raid5_align_endio" should check if the read succeeded and if it
4721 * did, call bio_endio on the original bio (having bio_put the new bio
4722 * first).
4723 * If the read failed..
4724 */
4246a0b6 4725static void raid5_align_endio(struct bio *bi)
f679623f
RBJ
4726{
4727 struct bio* raid_bi = bi->bi_private;
fd01b88c 4728 struct mddev *mddev;
d1688a6d 4729 struct r5conf *conf;
3cb03002 4730 struct md_rdev *rdev;
9b81c842 4731 int error = bi->bi_error;
46031f9a 4732
f679623f 4733 bio_put(bi);
46031f9a 4734
46031f9a
RBJ
4735 rdev = (void*)raid_bi->bi_next;
4736 raid_bi->bi_next = NULL;
2b7f2228
N
4737 mddev = rdev->mddev;
4738 conf = mddev->private;
46031f9a
RBJ
4739
4740 rdev_dec_pending(rdev, conf->mddev);
4741
9b81c842 4742 if (!error) {
0a82a8d1
LT
4743 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
4744 raid_bi, 0);
4246a0b6 4745 bio_endio(raid_bi);
46031f9a 4746 if (atomic_dec_and_test(&conf->active_aligned_reads))
b1b46486 4747 wake_up(&conf->wait_for_quiescent);
6712ecf8 4748 return;
46031f9a
RBJ
4749 }
4750
45b4233c 4751 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
46031f9a
RBJ
4752
4753 add_bio_to_retry(raid_bi, conf);
f679623f
RBJ
4754}
4755
7ef6b12a 4756static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
f679623f 4757{
d1688a6d 4758 struct r5conf *conf = mddev->private;
8553fe7e 4759 int dd_idx;
f679623f 4760 struct bio* align_bi;
3cb03002 4761 struct md_rdev *rdev;
671488cc 4762 sector_t end_sector;
f679623f
RBJ
4763
4764 if (!in_chunk_boundary(mddev, raid_bio)) {
7ef6b12a 4765 pr_debug("%s: non aligned\n", __func__);
f679623f
RBJ
4766 return 0;
4767 }
4768 /*
a167f663 4769 * use bio_clone_mddev to make a copy of the bio
f679623f 4770 */
a167f663 4771 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
f679623f
RBJ
4772 if (!align_bi)
4773 return 0;
4774 /*
4775 * set bi_end_io to a new function, and set bi_private to the
4776 * original bio.
4777 */
4778 align_bi->bi_end_io = raid5_align_endio;
4779 align_bi->bi_private = raid_bio;
4780 /*
4781 * compute position
4782 */
4f024f37
KO
4783 align_bi->bi_iter.bi_sector =
4784 raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
4785 0, &dd_idx, NULL);
f679623f 4786
f73a1c7d 4787 end_sector = bio_end_sector(align_bi);
f679623f 4788 rcu_read_lock();
671488cc
N
4789 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4790 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4791 rdev->recovery_offset < end_sector) {
4792 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4793 if (rdev &&
4794 (test_bit(Faulty, &rdev->flags) ||
4795 !(test_bit(In_sync, &rdev->flags) ||
4796 rdev->recovery_offset >= end_sector)))
4797 rdev = NULL;
4798 }
4799 if (rdev) {
31c176ec
N
4800 sector_t first_bad;
4801 int bad_sectors;
4802
f679623f
RBJ
4803 atomic_inc(&rdev->nr_pending);
4804 rcu_read_unlock();
46031f9a
RBJ
4805 raid_bio->bi_next = (void*)rdev;
4806 align_bi->bi_bdev = rdev->bdev;
b7c44ed9 4807 bio_clear_flag(align_bi, BIO_SEG_VALID);
46031f9a 4808
7140aafc 4809 if (is_badblock(rdev, align_bi->bi_iter.bi_sector,
4f024f37 4810 bio_sectors(align_bi),
31c176ec 4811 &first_bad, &bad_sectors)) {
387bb173
NB
4812 bio_put(align_bi);
4813 rdev_dec_pending(rdev, mddev);
4814 return 0;
4815 }
4816
6c0544e2 4817 /* No reshape active, so we can trust rdev->data_offset */
4f024f37 4818 align_bi->bi_iter.bi_sector += rdev->data_offset;
6c0544e2 4819
46031f9a 4820 spin_lock_irq(&conf->device_lock);
b1b46486 4821 wait_event_lock_irq(conf->wait_for_quiescent,
46031f9a 4822 conf->quiesce == 0,
eed8c02e 4823 conf->device_lock);
46031f9a
RBJ
4824 atomic_inc(&conf->active_aligned_reads);
4825 spin_unlock_irq(&conf->device_lock);
4826
e3620a3a
JB
4827 if (mddev->gendisk)
4828 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4829 align_bi, disk_devt(mddev->gendisk),
4f024f37 4830 raid_bio->bi_iter.bi_sector);
f679623f
RBJ
4831 generic_make_request(align_bi);
4832 return 1;
4833 } else {
4834 rcu_read_unlock();
46031f9a 4835 bio_put(align_bi);
f679623f
RBJ
4836 return 0;
4837 }
4838}
4839
7ef6b12a
ML
4840static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
4841{
4842 struct bio *split;
4843
4844 do {
4845 sector_t sector = raid_bio->bi_iter.bi_sector;
4846 unsigned chunk_sects = mddev->chunk_sectors;
4847 unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
4848
4849 if (sectors < bio_sectors(raid_bio)) {
4850 split = bio_split(raid_bio, sectors, GFP_NOIO, fs_bio_set);
4851 bio_chain(split, raid_bio);
4852 } else
4853 split = raid_bio;
4854
4855 if (!raid5_read_one_chunk(mddev, split)) {
4856 if (split != raid_bio)
4857 generic_make_request(raid_bio);
4858 return split;
4859 }
4860 } while (split != raid_bio);
4861
4862 return NULL;
4863}
4864
8b3e6cdc
DW
4865/* __get_priority_stripe - get the next stripe to process
4866 *
4867 * Full stripe writes are allowed to pass preread active stripes up until
4868 * the bypass_threshold is exceeded. In general the bypass_count
4869 * increments when the handle_list is handled before the hold_list; however, it
4870 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4871 * stripe with in flight i/o. The bypass_count will be reset when the
4872 * head of the hold_list has changed, i.e. the head was promoted to the
4873 * handle_list.
4874 */
851c30c9 4875static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
8b3e6cdc 4876{
851c30c9
SL
4877 struct stripe_head *sh = NULL, *tmp;
4878 struct list_head *handle_list = NULL;
bfc90cb0 4879 struct r5worker_group *wg = NULL;
851c30c9
SL
4880
4881 if (conf->worker_cnt_per_group == 0) {
4882 handle_list = &conf->handle_list;
4883 } else if (group != ANY_GROUP) {
4884 handle_list = &conf->worker_groups[group].handle_list;
bfc90cb0 4885 wg = &conf->worker_groups[group];
851c30c9
SL
4886 } else {
4887 int i;
4888 for (i = 0; i < conf->group_cnt; i++) {
4889 handle_list = &conf->worker_groups[i].handle_list;
bfc90cb0 4890 wg = &conf->worker_groups[i];
851c30c9
SL
4891 if (!list_empty(handle_list))
4892 break;
4893 }
4894 }
8b3e6cdc
DW
4895
4896 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4897 __func__,
851c30c9 4898 list_empty(handle_list) ? "empty" : "busy",
8b3e6cdc
DW
4899 list_empty(&conf->hold_list) ? "empty" : "busy",
4900 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4901
851c30c9
SL
4902 if (!list_empty(handle_list)) {
4903 sh = list_entry(handle_list->next, typeof(*sh), lru);
8b3e6cdc
DW
4904
4905 if (list_empty(&conf->hold_list))
4906 conf->bypass_count = 0;
4907 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4908 if (conf->hold_list.next == conf->last_hold)
4909 conf->bypass_count++;
4910 else {
4911 conf->last_hold = conf->hold_list.next;
4912 conf->bypass_count -= conf->bypass_threshold;
4913 if (conf->bypass_count < 0)
4914 conf->bypass_count = 0;
4915 }
4916 }
4917 } else if (!list_empty(&conf->hold_list) &&
4918 ((conf->bypass_threshold &&
4919 conf->bypass_count > conf->bypass_threshold) ||
4920 atomic_read(&conf->pending_full_writes) == 0)) {
851c30c9
SL
4921
4922 list_for_each_entry(tmp, &conf->hold_list, lru) {
4923 if (conf->worker_cnt_per_group == 0 ||
4924 group == ANY_GROUP ||
4925 !cpu_online(tmp->cpu) ||
4926 cpu_to_group(tmp->cpu) == group) {
4927 sh = tmp;
4928 break;
4929 }
4930 }
4931
4932 if (sh) {
4933 conf->bypass_count -= conf->bypass_threshold;
4934 if (conf->bypass_count < 0)
4935 conf->bypass_count = 0;
4936 }
bfc90cb0 4937 wg = NULL;
851c30c9
SL
4938 }
4939
4940 if (!sh)
8b3e6cdc
DW
4941 return NULL;
4942
bfc90cb0
SL
4943 if (wg) {
4944 wg->stripes_cnt--;
4945 sh->group = NULL;
4946 }
8b3e6cdc 4947 list_del_init(&sh->lru);
c7a6d35e 4948 BUG_ON(atomic_inc_return(&sh->count) != 1);
8b3e6cdc
DW
4949 return sh;
4950}
f679623f 4951
8811b596
SL
4952struct raid5_plug_cb {
4953 struct blk_plug_cb cb;
4954 struct list_head list;
566c09c5 4955 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
8811b596
SL
4956};
4957
4958static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4959{
4960 struct raid5_plug_cb *cb = container_of(
4961 blk_cb, struct raid5_plug_cb, cb);
4962 struct stripe_head *sh;
4963 struct mddev *mddev = cb->cb.data;
4964 struct r5conf *conf = mddev->private;
a9add5d9 4965 int cnt = 0;
566c09c5 4966 int hash;
8811b596
SL
4967
4968 if (cb->list.next && !list_empty(&cb->list)) {
4969 spin_lock_irq(&conf->device_lock);
4970 while (!list_empty(&cb->list)) {
4971 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4972 list_del_init(&sh->lru);
4973 /*
4974 * avoid race release_stripe_plug() sees
4975 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4976 * is still in our list
4977 */
4e857c58 4978 smp_mb__before_atomic();
8811b596 4979 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
773ca82f
SL
4980 /*
4981 * STRIPE_ON_RELEASE_LIST could be set here. In that
4982 * case, the count is always > 1 here
4983 */
566c09c5
SL
4984 hash = sh->hash_lock_index;
4985 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
a9add5d9 4986 cnt++;
8811b596
SL
4987 }
4988 spin_unlock_irq(&conf->device_lock);
4989 }
566c09c5
SL
4990 release_inactive_stripe_list(conf, cb->temp_inactive_list,
4991 NR_STRIPE_HASH_LOCKS);
e3620a3a
JB
4992 if (mddev->queue)
4993 trace_block_unplug(mddev->queue, cnt, !from_schedule);
8811b596
SL
4994 kfree(cb);
4995}
4996
4997static void release_stripe_plug(struct mddev *mddev,
4998 struct stripe_head *sh)
4999{
5000 struct blk_plug_cb *blk_cb = blk_check_plugged(
5001 raid5_unplug, mddev,
5002 sizeof(struct raid5_plug_cb));
5003 struct raid5_plug_cb *cb;
5004
5005 if (!blk_cb) {
6d036f7d 5006 raid5_release_stripe(sh);
8811b596
SL
5007 return;
5008 }
5009
5010 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
5011
566c09c5
SL
5012 if (cb->list.next == NULL) {
5013 int i;
8811b596 5014 INIT_LIST_HEAD(&cb->list);
566c09c5
SL
5015 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5016 INIT_LIST_HEAD(cb->temp_inactive_list + i);
5017 }
8811b596
SL
5018
5019 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
5020 list_add_tail(&sh->lru, &cb->list);
5021 else
6d036f7d 5022 raid5_release_stripe(sh);
8811b596
SL
5023}
5024
620125f2
SL
5025static void make_discard_request(struct mddev *mddev, struct bio *bi)
5026{
5027 struct r5conf *conf = mddev->private;
5028 sector_t logical_sector, last_sector;
5029 struct stripe_head *sh;
5030 int remaining;
5031 int stripe_sectors;
5032
5033 if (mddev->reshape_position != MaxSector)
5034 /* Skip discard while reshape is happening */
5035 return;
5036
4f024f37
KO
5037 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
5038 last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9);
620125f2
SL
5039
5040 bi->bi_next = NULL;
5041 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
5042
5043 stripe_sectors = conf->chunk_sectors *
5044 (conf->raid_disks - conf->max_degraded);
5045 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5046 stripe_sectors);
5047 sector_div(last_sector, stripe_sectors);
5048
5049 logical_sector *= conf->chunk_sectors;
5050 last_sector *= conf->chunk_sectors;
5051
5052 for (; logical_sector < last_sector;
5053 logical_sector += STRIPE_SECTORS) {
5054 DEFINE_WAIT(w);
5055 int d;
5056 again:
6d036f7d 5057 sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
620125f2
SL
5058 prepare_to_wait(&conf->wait_for_overlap, &w,
5059 TASK_UNINTERRUPTIBLE);
f8dfcffd
N
5060 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5061 if (test_bit(STRIPE_SYNCING, &sh->state)) {
6d036f7d 5062 raid5_release_stripe(sh);
f8dfcffd
N
5063 schedule();
5064 goto again;
5065 }
5066 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
620125f2
SL
5067 spin_lock_irq(&sh->stripe_lock);
5068 for (d = 0; d < conf->raid_disks; d++) {
5069 if (d == sh->pd_idx || d == sh->qd_idx)
5070 continue;
5071 if (sh->dev[d].towrite || sh->dev[d].toread) {
5072 set_bit(R5_Overlap, &sh->dev[d].flags);
5073 spin_unlock_irq(&sh->stripe_lock);
6d036f7d 5074 raid5_release_stripe(sh);
620125f2
SL
5075 schedule();
5076 goto again;
5077 }
5078 }
f8dfcffd 5079 set_bit(STRIPE_DISCARD, &sh->state);
620125f2 5080 finish_wait(&conf->wait_for_overlap, &w);
7a87f434 5081 sh->overwrite_disks = 0;
620125f2
SL
5082 for (d = 0; d < conf->raid_disks; d++) {
5083 if (d == sh->pd_idx || d == sh->qd_idx)
5084 continue;
5085 sh->dev[d].towrite = bi;
5086 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
5087 raid5_inc_bi_active_stripes(bi);
7a87f434 5088 sh->overwrite_disks++;
620125f2
SL
5089 }
5090 spin_unlock_irq(&sh->stripe_lock);
5091 if (conf->mddev->bitmap) {
5092 for (d = 0;
5093 d < conf->raid_disks - conf->max_degraded;
5094 d++)
5095 bitmap_startwrite(mddev->bitmap,
5096 sh->sector,
5097 STRIPE_SECTORS,
5098 0);
5099 sh->bm_seq = conf->seq_flush + 1;
5100 set_bit(STRIPE_BIT_DELAY, &sh->state);
5101 }
5102
5103 set_bit(STRIPE_HANDLE, &sh->state);
5104 clear_bit(STRIPE_DELAYED, &sh->state);
5105 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5106 atomic_inc(&conf->preread_active_stripes);
5107 release_stripe_plug(mddev, sh);
5108 }
5109
5110 remaining = raid5_dec_bi_active_stripes(bi);
5111 if (remaining == 0) {
5112 md_write_end(mddev);
4246a0b6 5113 bio_endio(bi);
620125f2
SL
5114 }
5115}
5116
849674e4 5117static void raid5_make_request(struct mddev *mddev, struct bio * bi)
1da177e4 5118{
d1688a6d 5119 struct r5conf *conf = mddev->private;
911d4ee8 5120 int dd_idx;
1da177e4
LT
5121 sector_t new_sector;
5122 sector_t logical_sector, last_sector;
5123 struct stripe_head *sh;
a362357b 5124 const int rw = bio_data_dir(bi);
49077326 5125 int remaining;
27c0f68f
SL
5126 DEFINE_WAIT(w);
5127 bool do_prepare;
1da177e4 5128
1eff9d32 5129 if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
828cbe98
SL
5130 int ret = r5l_handle_flush_request(conf->log, bi);
5131
5132 if (ret == 0)
5133 return;
5134 if (ret == -ENODEV) {
5135 md_flush_request(mddev, bi);
5136 return;
5137 }
5138 /* ret == -EAGAIN, fallback */
e5dcdd80
N
5139 }
5140
3d310eb7 5141 md_write_start(mddev, bi);
06d91a5f 5142
9ffc8f7c
EM
5143 /*
5144 * If array is degraded, better not do chunk aligned read because
5145 * later we might have to read it again in order to reconstruct
5146 * data on failed drives.
5147 */
5148 if (rw == READ && mddev->degraded == 0 &&
2ded3703 5149 !r5c_is_writeback(conf->log) &&
7ef6b12a
ML
5150 mddev->reshape_position == MaxSector) {
5151 bi = chunk_aligned_read(mddev, bi);
5152 if (!bi)
5153 return;
5154 }
52488615 5155
796a5cf0 5156 if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
620125f2
SL
5157 make_discard_request(mddev, bi);
5158 return;
5159 }
5160
4f024f37 5161 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
f73a1c7d 5162 last_sector = bio_end_sector(bi);
1da177e4
LT
5163 bi->bi_next = NULL;
5164 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
06d91a5f 5165
27c0f68f 5166 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
1da177e4 5167 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
b5663ba4 5168 int previous;
c46501b2 5169 int seq;
b578d55f 5170
27c0f68f 5171 do_prepare = false;
7ecaa1e6 5172 retry:
c46501b2 5173 seq = read_seqcount_begin(&conf->gen_lock);
b5663ba4 5174 previous = 0;
27c0f68f
SL
5175 if (do_prepare)
5176 prepare_to_wait(&conf->wait_for_overlap, &w,
5177 TASK_UNINTERRUPTIBLE);
b0f9ec04 5178 if (unlikely(conf->reshape_progress != MaxSector)) {
fef9c61f 5179 /* spinlock is needed as reshape_progress may be
df8e7f76
N
5180 * 64bit on a 32bit platform, and so it might be
5181 * possible to see a half-updated value
aeb878b0 5182 * Of course reshape_progress could change after
df8e7f76
N
5183 * the lock is dropped, so once we get a reference
5184 * to the stripe that we think it is, we will have
5185 * to check again.
5186 */
7ecaa1e6 5187 spin_lock_irq(&conf->device_lock);
2c810cdd 5188 if (mddev->reshape_backwards
fef9c61f
N
5189 ? logical_sector < conf->reshape_progress
5190 : logical_sector >= conf->reshape_progress) {
b5663ba4
N
5191 previous = 1;
5192 } else {
2c810cdd 5193 if (mddev->reshape_backwards
fef9c61f
N
5194 ? logical_sector < conf->reshape_safe
5195 : logical_sector >= conf->reshape_safe) {
b578d55f
N
5196 spin_unlock_irq(&conf->device_lock);
5197 schedule();
27c0f68f 5198 do_prepare = true;
b578d55f
N
5199 goto retry;
5200 }
5201 }
7ecaa1e6
N
5202 spin_unlock_irq(&conf->device_lock);
5203 }
16a53ecc 5204
112bf897
N
5205 new_sector = raid5_compute_sector(conf, logical_sector,
5206 previous,
911d4ee8 5207 &dd_idx, NULL);
849674e4 5208 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
c46501b2 5209 (unsigned long long)new_sector,
1da177e4
LT
5210 (unsigned long long)logical_sector);
5211
6d036f7d 5212 sh = raid5_get_active_stripe(conf, new_sector, previous,
1eff9d32 5213 (bi->bi_opf & REQ_RAHEAD), 0);
1da177e4 5214 if (sh) {
b0f9ec04 5215 if (unlikely(previous)) {
7ecaa1e6 5216 /* expansion might have moved on while waiting for a
df8e7f76
N
5217 * stripe, so we must do the range check again.
5218 * Expansion could still move past after this
5219 * test, but as we are holding a reference to
5220 * 'sh', we know that if that happens,
5221 * STRIPE_EXPANDING will get set and the expansion
5222 * won't proceed until we finish with the stripe.
7ecaa1e6
N
5223 */
5224 int must_retry = 0;
5225 spin_lock_irq(&conf->device_lock);
2c810cdd 5226 if (mddev->reshape_backwards
b0f9ec04
N
5227 ? logical_sector >= conf->reshape_progress
5228 : logical_sector < conf->reshape_progress)
7ecaa1e6
N
5229 /* mismatch, need to try again */
5230 must_retry = 1;
5231 spin_unlock_irq(&conf->device_lock);
5232 if (must_retry) {
6d036f7d 5233 raid5_release_stripe(sh);
7a3ab908 5234 schedule();
27c0f68f 5235 do_prepare = true;
7ecaa1e6
N
5236 goto retry;
5237 }
5238 }
c46501b2
N
5239 if (read_seqcount_retry(&conf->gen_lock, seq)) {
5240 /* Might have got the wrong stripe_head
5241 * by accident
5242 */
6d036f7d 5243 raid5_release_stripe(sh);
c46501b2
N
5244 goto retry;
5245 }
e62e58a5 5246
ffd96e35 5247 if (rw == WRITE &&
a5c308d4 5248 logical_sector >= mddev->suspend_lo &&
e464eafd 5249 logical_sector < mddev->suspend_hi) {
6d036f7d 5250 raid5_release_stripe(sh);
e62e58a5
N
5251 /* As the suspend_* range is controlled by
5252 * userspace, we want an interruptible
5253 * wait.
5254 */
5255 flush_signals(current);
5256 prepare_to_wait(&conf->wait_for_overlap,
5257 &w, TASK_INTERRUPTIBLE);
5258 if (logical_sector >= mddev->suspend_lo &&
27c0f68f 5259 logical_sector < mddev->suspend_hi) {
e62e58a5 5260 schedule();
27c0f68f
SL
5261 do_prepare = true;
5262 }
e464eafd
N
5263 goto retry;
5264 }
7ecaa1e6
N
5265
5266 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
da41ba65 5267 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
7ecaa1e6
N
5268 /* Stripe is busy expanding or
5269 * add failed due to overlap. Flush everything
1da177e4
LT
5270 * and wait a while
5271 */
482c0834 5272 md_wakeup_thread(mddev->thread);
6d036f7d 5273 raid5_release_stripe(sh);
1da177e4 5274 schedule();
27c0f68f 5275 do_prepare = true;
1da177e4
LT
5276 goto retry;
5277 }
6ed3003c
N
5278 set_bit(STRIPE_HANDLE, &sh->state);
5279 clear_bit(STRIPE_DELAYED, &sh->state);
59fc630b 5280 if ((!sh->batch_head || sh == sh->batch_head) &&
1eff9d32 5281 (bi->bi_opf & REQ_SYNC) &&
729a1866
N
5282 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5283 atomic_inc(&conf->preread_active_stripes);
8811b596 5284 release_stripe_plug(mddev, sh);
1da177e4
LT
5285 } else {
5286 /* cannot get stripe for read-ahead, just give-up */
4246a0b6 5287 bi->bi_error = -EIO;
1da177e4
LT
5288 break;
5289 }
1da177e4 5290 }
27c0f68f 5291 finish_wait(&conf->wait_for_overlap, &w);
7c13edc8 5292
e7836bd6 5293 remaining = raid5_dec_bi_active_stripes(bi);
f6344757 5294 if (remaining == 0) {
1da177e4 5295
16a53ecc 5296 if ( rw == WRITE )
1da177e4 5297 md_write_end(mddev);
6712ecf8 5298
0a82a8d1
LT
5299 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
5300 bi, 0);
4246a0b6 5301 bio_endio(bi);
1da177e4 5302 }
1da177e4
LT
5303}
5304
fd01b88c 5305static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
b522adcd 5306
fd01b88c 5307static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
1da177e4 5308{
52c03291
N
5309 /* reshaping is quite different to recovery/resync so it is
5310 * handled quite separately ... here.
5311 *
5312 * On each call to sync_request, we gather one chunk worth of
5313 * destination stripes and flag them as expanding.
5314 * Then we find all the source stripes and request reads.
5315 * As the reads complete, handle_stripe will copy the data
5316 * into the destination stripe and release that stripe.
5317 */
d1688a6d 5318 struct r5conf *conf = mddev->private;
1da177e4 5319 struct stripe_head *sh;
ccfcc3c1 5320 sector_t first_sector, last_sector;
f416885e
N
5321 int raid_disks = conf->previous_raid_disks;
5322 int data_disks = raid_disks - conf->max_degraded;
5323 int new_data_disks = conf->raid_disks - conf->max_degraded;
52c03291
N
5324 int i;
5325 int dd_idx;
c8f517c4 5326 sector_t writepos, readpos, safepos;
ec32a2bd 5327 sector_t stripe_addr;
7a661381 5328 int reshape_sectors;
ab69ae12 5329 struct list_head stripes;
92140480 5330 sector_t retn;
52c03291 5331
fef9c61f
N
5332 if (sector_nr == 0) {
5333 /* If restarting in the middle, skip the initial sectors */
2c810cdd 5334 if (mddev->reshape_backwards &&
fef9c61f
N
5335 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
5336 sector_nr = raid5_size(mddev, 0, 0)
5337 - conf->reshape_progress;
6cbd8148
N
5338 } else if (mddev->reshape_backwards &&
5339 conf->reshape_progress == MaxSector) {
5340 /* shouldn't happen, but just in case, finish up.*/
5341 sector_nr = MaxSector;
2c810cdd 5342 } else if (!mddev->reshape_backwards &&
fef9c61f
N
5343 conf->reshape_progress > 0)
5344 sector_nr = conf->reshape_progress;
f416885e 5345 sector_div(sector_nr, new_data_disks);
fef9c61f 5346 if (sector_nr) {
8dee7211
N
5347 mddev->curr_resync_completed = sector_nr;
5348 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
fef9c61f 5349 *skipped = 1;
92140480
N
5350 retn = sector_nr;
5351 goto finish;
fef9c61f 5352 }
52c03291
N
5353 }
5354
7a661381
N
5355 /* We need to process a full chunk at a time.
5356 * If old and new chunk sizes differ, we need to process the
5357 * largest of these
5358 */
3cb5edf4
N
5359
5360 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
7a661381 5361
b5254dd5
N
5362 /* We update the metadata at least every 10 seconds, or when
5363 * the data about to be copied would over-write the source of
5364 * the data at the front of the range. i.e. one new_stripe
5365 * along from reshape_progress new_maps to after where
5366 * reshape_safe old_maps to
52c03291 5367 */
fef9c61f 5368 writepos = conf->reshape_progress;
f416885e 5369 sector_div(writepos, new_data_disks);
c8f517c4
N
5370 readpos = conf->reshape_progress;
5371 sector_div(readpos, data_disks);
fef9c61f 5372 safepos = conf->reshape_safe;
f416885e 5373 sector_div(safepos, data_disks);
2c810cdd 5374 if (mddev->reshape_backwards) {
c74c0d76
N
5375 BUG_ON(writepos < reshape_sectors);
5376 writepos -= reshape_sectors;
c8f517c4 5377 readpos += reshape_sectors;
7a661381 5378 safepos += reshape_sectors;
fef9c61f 5379 } else {
7a661381 5380 writepos += reshape_sectors;
c74c0d76
N
5381 /* readpos and safepos are worst-case calculations.
5382 * A negative number is overly pessimistic, and causes
5383 * obvious problems for unsigned storage. So clip to 0.
5384 */
ed37d83e
N
5385 readpos -= min_t(sector_t, reshape_sectors, readpos);
5386 safepos -= min_t(sector_t, reshape_sectors, safepos);
fef9c61f 5387 }
52c03291 5388
b5254dd5
N
5389 /* Having calculated the 'writepos' possibly use it
5390 * to set 'stripe_addr' which is where we will write to.
5391 */
5392 if (mddev->reshape_backwards) {
5393 BUG_ON(conf->reshape_progress == 0);
5394 stripe_addr = writepos;
5395 BUG_ON((mddev->dev_sectors &
5396 ~((sector_t)reshape_sectors - 1))
5397 - reshape_sectors - stripe_addr
5398 != sector_nr);
5399 } else {
5400 BUG_ON(writepos != sector_nr + reshape_sectors);
5401 stripe_addr = sector_nr;
5402 }
5403
c8f517c4
N
5404 /* 'writepos' is the most advanced device address we might write.
5405 * 'readpos' is the least advanced device address we might read.
5406 * 'safepos' is the least address recorded in the metadata as having
5407 * been reshaped.
b5254dd5
N
5408 * If there is a min_offset_diff, these are adjusted either by
5409 * increasing the safepos/readpos if diff is negative, or
5410 * increasing writepos if diff is positive.
5411 * If 'readpos' is then behind 'writepos', there is no way that we can
c8f517c4
N
5412 * ensure safety in the face of a crash - that must be done by userspace
5413 * making a backup of the data. So in that case there is no particular
5414 * rush to update metadata.
5415 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5416 * update the metadata to advance 'safepos' to match 'readpos' so that
5417 * we can be safe in the event of a crash.
5418 * So we insist on updating metadata if safepos is behind writepos and
5419 * readpos is beyond writepos.
5420 * In any case, update the metadata every 10 seconds.
5421 * Maybe that number should be configurable, but I'm not sure it is
5422 * worth it.... maybe it could be a multiple of safemode_delay???
5423 */
b5254dd5
N
5424 if (conf->min_offset_diff < 0) {
5425 safepos += -conf->min_offset_diff;
5426 readpos += -conf->min_offset_diff;
5427 } else
5428 writepos += conf->min_offset_diff;
5429
2c810cdd 5430 if ((mddev->reshape_backwards
c8f517c4
N
5431 ? (safepos > writepos && readpos < writepos)
5432 : (safepos < writepos && readpos > writepos)) ||
5433 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
52c03291
N
5434 /* Cannot proceed until we've updated the superblock... */
5435 wait_event(conf->wait_for_overlap,
c91abf5a
N
5436 atomic_read(&conf->reshape_stripes)==0
5437 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5438 if (atomic_read(&conf->reshape_stripes) != 0)
5439 return 0;
fef9c61f 5440 mddev->reshape_position = conf->reshape_progress;
75d3da43 5441 mddev->curr_resync_completed = sector_nr;
c8f517c4 5442 conf->reshape_checkpoint = jiffies;
850b2b42 5443 set_bit(MD_CHANGE_DEVS, &mddev->flags);
52c03291 5444 md_wakeup_thread(mddev->thread);
850b2b42 5445 wait_event(mddev->sb_wait, mddev->flags == 0 ||
c91abf5a
N
5446 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5447 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5448 return 0;
52c03291 5449 spin_lock_irq(&conf->device_lock);
fef9c61f 5450 conf->reshape_safe = mddev->reshape_position;
52c03291
N
5451 spin_unlock_irq(&conf->device_lock);
5452 wake_up(&conf->wait_for_overlap);
acb180b0 5453 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
52c03291
N
5454 }
5455
ab69ae12 5456 INIT_LIST_HEAD(&stripes);
7a661381 5457 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
52c03291 5458 int j;
a9f326eb 5459 int skipped_disk = 0;
6d036f7d 5460 sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
52c03291
N
5461 set_bit(STRIPE_EXPANDING, &sh->state);
5462 atomic_inc(&conf->reshape_stripes);
5463 /* If any of this stripe is beyond the end of the old
5464 * array, then we need to zero those blocks
5465 */
5466 for (j=sh->disks; j--;) {
5467 sector_t s;
5468 if (j == sh->pd_idx)
5469 continue;
f416885e 5470 if (conf->level == 6 &&
d0dabf7e 5471 j == sh->qd_idx)
f416885e 5472 continue;
6d036f7d 5473 s = raid5_compute_blocknr(sh, j, 0);
b522adcd 5474 if (s < raid5_size(mddev, 0, 0)) {
a9f326eb 5475 skipped_disk = 1;
52c03291
N
5476 continue;
5477 }
5478 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
5479 set_bit(R5_Expanded, &sh->dev[j].flags);
5480 set_bit(R5_UPTODATE, &sh->dev[j].flags);
5481 }
a9f326eb 5482 if (!skipped_disk) {
52c03291
N
5483 set_bit(STRIPE_EXPAND_READY, &sh->state);
5484 set_bit(STRIPE_HANDLE, &sh->state);
5485 }
ab69ae12 5486 list_add(&sh->lru, &stripes);
52c03291
N
5487 }
5488 spin_lock_irq(&conf->device_lock);
2c810cdd 5489 if (mddev->reshape_backwards)
7a661381 5490 conf->reshape_progress -= reshape_sectors * new_data_disks;
fef9c61f 5491 else
7a661381 5492 conf->reshape_progress += reshape_sectors * new_data_disks;
52c03291
N
5493 spin_unlock_irq(&conf->device_lock);
5494 /* Ok, those stripe are ready. We can start scheduling
5495 * reads on the source stripes.
5496 * The source stripes are determined by mapping the first and last
5497 * block on the destination stripes.
5498 */
52c03291 5499 first_sector =
ec32a2bd 5500 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
911d4ee8 5501 1, &dd_idx, NULL);
52c03291 5502 last_sector =
0e6e0271 5503 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
09c9e5fa 5504 * new_data_disks - 1),
911d4ee8 5505 1, &dd_idx, NULL);
58c0fed4
AN
5506 if (last_sector >= mddev->dev_sectors)
5507 last_sector = mddev->dev_sectors - 1;
52c03291 5508 while (first_sector <= last_sector) {
6d036f7d 5509 sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
52c03291
N
5510 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
5511 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 5512 raid5_release_stripe(sh);
52c03291
N
5513 first_sector += STRIPE_SECTORS;
5514 }
ab69ae12
N
5515 /* Now that the sources are clearly marked, we can release
5516 * the destination stripes
5517 */
5518 while (!list_empty(&stripes)) {
5519 sh = list_entry(stripes.next, struct stripe_head, lru);
5520 list_del_init(&sh->lru);
6d036f7d 5521 raid5_release_stripe(sh);
ab69ae12 5522 }
c6207277
N
5523 /* If this takes us to the resync_max point where we have to pause,
5524 * then we need to write out the superblock.
5525 */
7a661381 5526 sector_nr += reshape_sectors;
92140480
N
5527 retn = reshape_sectors;
5528finish:
c5e19d90
N
5529 if (mddev->curr_resync_completed > mddev->resync_max ||
5530 (sector_nr - mddev->curr_resync_completed) * 2
c03f6a19 5531 >= mddev->resync_max - mddev->curr_resync_completed) {
c6207277
N
5532 /* Cannot proceed until we've updated the superblock... */
5533 wait_event(conf->wait_for_overlap,
c91abf5a
N
5534 atomic_read(&conf->reshape_stripes) == 0
5535 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5536 if (atomic_read(&conf->reshape_stripes) != 0)
5537 goto ret;
fef9c61f 5538 mddev->reshape_position = conf->reshape_progress;
75d3da43 5539 mddev->curr_resync_completed = sector_nr;
c8f517c4 5540 conf->reshape_checkpoint = jiffies;
c6207277
N
5541 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5542 md_wakeup_thread(mddev->thread);
5543 wait_event(mddev->sb_wait,
5544 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
c91abf5a
N
5545 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5546 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5547 goto ret;
c6207277 5548 spin_lock_irq(&conf->device_lock);
fef9c61f 5549 conf->reshape_safe = mddev->reshape_position;
c6207277
N
5550 spin_unlock_irq(&conf->device_lock);
5551 wake_up(&conf->wait_for_overlap);
acb180b0 5552 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
c6207277 5553 }
c91abf5a 5554ret:
92140480 5555 return retn;
52c03291
N
5556}
5557
849674e4
SL
5558static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
5559 int *skipped)
52c03291 5560{
d1688a6d 5561 struct r5conf *conf = mddev->private;
52c03291 5562 struct stripe_head *sh;
58c0fed4 5563 sector_t max_sector = mddev->dev_sectors;
57dab0bd 5564 sector_t sync_blocks;
16a53ecc
N
5565 int still_degraded = 0;
5566 int i;
1da177e4 5567
72626685 5568 if (sector_nr >= max_sector) {
1da177e4 5569 /* just being told to finish up .. nothing much to do */
cea9c228 5570
29269553
N
5571 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
5572 end_reshape(conf);
5573 return 0;
5574 }
72626685
N
5575
5576 if (mddev->curr_resync < max_sector) /* aborted */
5577 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
5578 &sync_blocks, 1);
16a53ecc 5579 else /* completed sync */
72626685
N
5580 conf->fullsync = 0;
5581 bitmap_close_sync(mddev->bitmap);
5582
1da177e4
LT
5583 return 0;
5584 }
ccfcc3c1 5585
64bd660b
N
5586 /* Allow raid5_quiesce to complete */
5587 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
5588
52c03291
N
5589 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5590 return reshape_request(mddev, sector_nr, skipped);
f6705578 5591
c6207277
N
5592 /* No need to check resync_max as we never do more than one
5593 * stripe, and as resync_max will always be on a chunk boundary,
5594 * if the check in md_do_sync didn't fire, there is no chance
5595 * of overstepping resync_max here
5596 */
5597
16a53ecc 5598 /* if there is too many failed drives and we are trying
1da177e4
LT
5599 * to resync, then assert that we are finished, because there is
5600 * nothing we can do.
5601 */
3285edf1 5602 if (mddev->degraded >= conf->max_degraded &&
16a53ecc 5603 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
58c0fed4 5604 sector_t rv = mddev->dev_sectors - sector_nr;
57afd89f 5605 *skipped = 1;
1da177e4
LT
5606 return rv;
5607 }
6f608040 5608 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
5609 !conf->fullsync &&
5610 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
5611 sync_blocks >= STRIPE_SECTORS) {
72626685
N
5612 /* we can skip this block, and probably more */
5613 sync_blocks /= STRIPE_SECTORS;
5614 *skipped = 1;
5615 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
5616 }
1da177e4 5617
c40f341f 5618 bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
b47490c9 5619
6d036f7d 5620 sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
1da177e4 5621 if (sh == NULL) {
6d036f7d 5622 sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
1da177e4 5623 /* make sure we don't swamp the stripe cache if someone else
16a53ecc 5624 * is trying to get access
1da177e4 5625 */
66c006a5 5626 schedule_timeout_uninterruptible(1);
1da177e4 5627 }
16a53ecc 5628 /* Need to check if array will still be degraded after recovery/resync
16d9cfab
EM
5629 * Note in case of > 1 drive failures it's possible we're rebuilding
5630 * one drive while leaving another faulty drive in array.
16a53ecc 5631 */
16d9cfab
EM
5632 rcu_read_lock();
5633 for (i = 0; i < conf->raid_disks; i++) {
5634 struct md_rdev *rdev = ACCESS_ONCE(conf->disks[i].rdev);
5635
5636 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
16a53ecc 5637 still_degraded = 1;
16d9cfab
EM
5638 }
5639 rcu_read_unlock();
16a53ecc
N
5640
5641 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
5642
83206d66 5643 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
053f5b65 5644 set_bit(STRIPE_HANDLE, &sh->state);
1da177e4 5645
6d036f7d 5646 raid5_release_stripe(sh);
1da177e4
LT
5647
5648 return STRIPE_SECTORS;
5649}
5650
d1688a6d 5651static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
46031f9a
RBJ
5652{
5653 /* We may not be able to submit a whole bio at once as there
5654 * may not be enough stripe_heads available.
5655 * We cannot pre-allocate enough stripe_heads as we may need
5656 * more than exist in the cache (if we allow ever large chunks).
5657 * So we do one stripe head at a time and record in
5658 * ->bi_hw_segments how many have been done.
5659 *
5660 * We *know* that this entire raid_bio is in one chunk, so
5661 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5662 */
5663 struct stripe_head *sh;
911d4ee8 5664 int dd_idx;
46031f9a
RBJ
5665 sector_t sector, logical_sector, last_sector;
5666 int scnt = 0;
5667 int remaining;
5668 int handled = 0;
5669
4f024f37
KO
5670 logical_sector = raid_bio->bi_iter.bi_sector &
5671 ~((sector_t)STRIPE_SECTORS-1);
112bf897 5672 sector = raid5_compute_sector(conf, logical_sector,
911d4ee8 5673 0, &dd_idx, NULL);
f73a1c7d 5674 last_sector = bio_end_sector(raid_bio);
46031f9a
RBJ
5675
5676 for (; logical_sector < last_sector;
387bb173
NB
5677 logical_sector += STRIPE_SECTORS,
5678 sector += STRIPE_SECTORS,
5679 scnt++) {
46031f9a 5680
e7836bd6 5681 if (scnt < raid5_bi_processed_stripes(raid_bio))
46031f9a
RBJ
5682 /* already done this stripe */
5683 continue;
5684
6d036f7d 5685 sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
46031f9a
RBJ
5686
5687 if (!sh) {
5688 /* failed to get a stripe - must wait */
e7836bd6 5689 raid5_set_bi_processed_stripes(raid_bio, scnt);
46031f9a
RBJ
5690 conf->retry_read_aligned = raid_bio;
5691 return handled;
5692 }
5693
da41ba65 5694 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
6d036f7d 5695 raid5_release_stripe(sh);
e7836bd6 5696 raid5_set_bi_processed_stripes(raid_bio, scnt);
387bb173
NB
5697 conf->retry_read_aligned = raid_bio;
5698 return handled;
5699 }
5700
3f9e7c14 5701 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
36d1c647 5702 handle_stripe(sh);
6d036f7d 5703 raid5_release_stripe(sh);
46031f9a
RBJ
5704 handled++;
5705 }
e7836bd6 5706 remaining = raid5_dec_bi_active_stripes(raid_bio);
0a82a8d1
LT
5707 if (remaining == 0) {
5708 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
5709 raid_bio, 0);
4246a0b6 5710 bio_endio(raid_bio);
0a82a8d1 5711 }
46031f9a 5712 if (atomic_dec_and_test(&conf->active_aligned_reads))
b1b46486 5713 wake_up(&conf->wait_for_quiescent);
46031f9a
RBJ
5714 return handled;
5715}
5716
bfc90cb0 5717static int handle_active_stripes(struct r5conf *conf, int group,
566c09c5
SL
5718 struct r5worker *worker,
5719 struct list_head *temp_inactive_list)
46a06401
SL
5720{
5721 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
566c09c5
SL
5722 int i, batch_size = 0, hash;
5723 bool release_inactive = false;
46a06401
SL
5724
5725 while (batch_size < MAX_STRIPE_BATCH &&
851c30c9 5726 (sh = __get_priority_stripe(conf, group)) != NULL)
46a06401
SL
5727 batch[batch_size++] = sh;
5728
566c09c5
SL
5729 if (batch_size == 0) {
5730 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5731 if (!list_empty(temp_inactive_list + i))
5732 break;
a8c34f91
SL
5733 if (i == NR_STRIPE_HASH_LOCKS) {
5734 spin_unlock_irq(&conf->device_lock);
5735 r5l_flush_stripe_to_raid(conf->log);
5736 spin_lock_irq(&conf->device_lock);
566c09c5 5737 return batch_size;
a8c34f91 5738 }
566c09c5
SL
5739 release_inactive = true;
5740 }
46a06401
SL
5741 spin_unlock_irq(&conf->device_lock);
5742
566c09c5
SL
5743 release_inactive_stripe_list(conf, temp_inactive_list,
5744 NR_STRIPE_HASH_LOCKS);
5745
a8c34f91 5746 r5l_flush_stripe_to_raid(conf->log);
566c09c5
SL
5747 if (release_inactive) {
5748 spin_lock_irq(&conf->device_lock);
5749 return 0;
5750 }
5751
46a06401
SL
5752 for (i = 0; i < batch_size; i++)
5753 handle_stripe(batch[i]);
f6bed0ef 5754 r5l_write_stripe_run(conf->log);
46a06401
SL
5755
5756 cond_resched();
5757
5758 spin_lock_irq(&conf->device_lock);
566c09c5
SL
5759 for (i = 0; i < batch_size; i++) {
5760 hash = batch[i]->hash_lock_index;
5761 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
5762 }
46a06401
SL
5763 return batch_size;
5764}
46031f9a 5765
851c30c9
SL
5766static void raid5_do_work(struct work_struct *work)
5767{
5768 struct r5worker *worker = container_of(work, struct r5worker, work);
5769 struct r5worker_group *group = worker->group;
5770 struct r5conf *conf = group->conf;
5771 int group_id = group - conf->worker_groups;
5772 int handled;
5773 struct blk_plug plug;
5774
5775 pr_debug("+++ raid5worker active\n");
5776
5777 blk_start_plug(&plug);
5778 handled = 0;
5779 spin_lock_irq(&conf->device_lock);
5780 while (1) {
5781 int batch_size, released;
5782
566c09c5 5783 released = release_stripe_list(conf, worker->temp_inactive_list);
851c30c9 5784
566c09c5
SL
5785 batch_size = handle_active_stripes(conf, group_id, worker,
5786 worker->temp_inactive_list);
bfc90cb0 5787 worker->working = false;
851c30c9
SL
5788 if (!batch_size && !released)
5789 break;
5790 handled += batch_size;
5791 }
5792 pr_debug("%d stripes handled\n", handled);
5793
5794 spin_unlock_irq(&conf->device_lock);
5795 blk_finish_plug(&plug);
5796
5797 pr_debug("--- raid5worker inactive\n");
5798}
5799
1da177e4
LT
5800/*
5801 * This is our raid5 kernel thread.
5802 *
5803 * We scan the hash table for stripes which can be handled now.
5804 * During the scan, completed stripes are saved for us by the interrupt
5805 * handler, so that they will not have to wait for our next wakeup.
5806 */
4ed8731d 5807static void raid5d(struct md_thread *thread)
1da177e4 5808{
4ed8731d 5809 struct mddev *mddev = thread->mddev;
d1688a6d 5810 struct r5conf *conf = mddev->private;
1da177e4 5811 int handled;
e1dfa0a2 5812 struct blk_plug plug;
1da177e4 5813
45b4233c 5814 pr_debug("+++ raid5d active\n");
1da177e4
LT
5815
5816 md_check_recovery(mddev);
1da177e4 5817
c3cce6cd
N
5818 if (!bio_list_empty(&conf->return_bi) &&
5819 !test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
5820 struct bio_list tmp = BIO_EMPTY_LIST;
5821 spin_lock_irq(&conf->device_lock);
5822 if (!test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
5823 bio_list_merge(&tmp, &conf->return_bi);
5824 bio_list_init(&conf->return_bi);
5825 }
5826 spin_unlock_irq(&conf->device_lock);
5827 return_io(&tmp);
5828 }
5829
e1dfa0a2 5830 blk_start_plug(&plug);
1da177e4
LT
5831 handled = 0;
5832 spin_lock_irq(&conf->device_lock);
5833 while (1) {
46031f9a 5834 struct bio *bio;
773ca82f
SL
5835 int batch_size, released;
5836
566c09c5 5837 released = release_stripe_list(conf, conf->temp_inactive_list);
edbe83ab
N
5838 if (released)
5839 clear_bit(R5_DID_ALLOC, &conf->cache_state);
1da177e4 5840
0021b7bc 5841 if (
7c13edc8
N
5842 !list_empty(&conf->bitmap_list)) {
5843 /* Now is a good time to flush some bitmap updates */
5844 conf->seq_flush++;
700e432d 5845 spin_unlock_irq(&conf->device_lock);
72626685 5846 bitmap_unplug(mddev->bitmap);
700e432d 5847 spin_lock_irq(&conf->device_lock);
7c13edc8 5848 conf->seq_write = conf->seq_flush;
566c09c5 5849 activate_bit_delay(conf, conf->temp_inactive_list);
72626685 5850 }
0021b7bc 5851 raid5_activate_delayed(conf);
72626685 5852
46031f9a
RBJ
5853 while ((bio = remove_bio_from_retry(conf))) {
5854 int ok;
5855 spin_unlock_irq(&conf->device_lock);
5856 ok = retry_aligned_read(conf, bio);
5857 spin_lock_irq(&conf->device_lock);
5858 if (!ok)
5859 break;
5860 handled++;
5861 }
5862
566c09c5
SL
5863 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
5864 conf->temp_inactive_list);
773ca82f 5865 if (!batch_size && !released)
1da177e4 5866 break;
46a06401 5867 handled += batch_size;
1da177e4 5868
46a06401
SL
5869 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
5870 spin_unlock_irq(&conf->device_lock);
de393cde 5871 md_check_recovery(mddev);
46a06401
SL
5872 spin_lock_irq(&conf->device_lock);
5873 }
1da177e4 5874 }
45b4233c 5875 pr_debug("%d stripes handled\n", handled);
1da177e4
LT
5876
5877 spin_unlock_irq(&conf->device_lock);
2d5b569b
N
5878 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
5879 mutex_trylock(&conf->cache_size_mutex)) {
edbe83ab
N
5880 grow_one_stripe(conf, __GFP_NOWARN);
5881 /* Set flag even if allocation failed. This helps
5882 * slow down allocation requests when mem is short
5883 */
5884 set_bit(R5_DID_ALLOC, &conf->cache_state);
2d5b569b 5885 mutex_unlock(&conf->cache_size_mutex);
edbe83ab 5886 }
1da177e4 5887
0576b1c6
SL
5888 r5l_flush_stripe_to_raid(conf->log);
5889
c9f21aaf 5890 async_tx_issue_pending_all();
e1dfa0a2 5891 blk_finish_plug(&plug);
1da177e4 5892
45b4233c 5893 pr_debug("--- raid5d inactive\n");
1da177e4
LT
5894}
5895
3f294f4f 5896static ssize_t
fd01b88c 5897raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
3f294f4f 5898{
7b1485ba
N
5899 struct r5conf *conf;
5900 int ret = 0;
5901 spin_lock(&mddev->lock);
5902 conf = mddev->private;
96de1e66 5903 if (conf)
edbe83ab 5904 ret = sprintf(page, "%d\n", conf->min_nr_stripes);
7b1485ba
N
5905 spin_unlock(&mddev->lock);
5906 return ret;
3f294f4f
N
5907}
5908
c41d4ac4 5909int
fd01b88c 5910raid5_set_cache_size(struct mddev *mddev, int size)
3f294f4f 5911{
d1688a6d 5912 struct r5conf *conf = mddev->private;
b5470dc5
DW
5913 int err;
5914
c41d4ac4 5915 if (size <= 16 || size > 32768)
3f294f4f 5916 return -EINVAL;
486f0644 5917
edbe83ab 5918 conf->min_nr_stripes = size;
2d5b569b 5919 mutex_lock(&conf->cache_size_mutex);
486f0644
N
5920 while (size < conf->max_nr_stripes &&
5921 drop_one_stripe(conf))
5922 ;
2d5b569b 5923 mutex_unlock(&conf->cache_size_mutex);
486f0644 5924
edbe83ab 5925
b5470dc5
DW
5926 err = md_allow_write(mddev);
5927 if (err)
5928 return err;
486f0644 5929
2d5b569b 5930 mutex_lock(&conf->cache_size_mutex);
486f0644
N
5931 while (size > conf->max_nr_stripes)
5932 if (!grow_one_stripe(conf, GFP_KERNEL))
5933 break;
2d5b569b 5934 mutex_unlock(&conf->cache_size_mutex);
486f0644 5935
c41d4ac4
N
5936 return 0;
5937}
5938EXPORT_SYMBOL(raid5_set_cache_size);
5939
5940static ssize_t
fd01b88c 5941raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
c41d4ac4 5942{
6791875e 5943 struct r5conf *conf;
c41d4ac4
N
5944 unsigned long new;
5945 int err;
5946
5947 if (len >= PAGE_SIZE)
5948 return -EINVAL;
b29bebd6 5949 if (kstrtoul(page, 10, &new))
c41d4ac4 5950 return -EINVAL;
6791875e 5951 err = mddev_lock(mddev);
c41d4ac4
N
5952 if (err)
5953 return err;
6791875e
N
5954 conf = mddev->private;
5955 if (!conf)
5956 err = -ENODEV;
5957 else
5958 err = raid5_set_cache_size(mddev, new);
5959 mddev_unlock(mddev);
5960
5961 return err ?: len;
3f294f4f 5962}
007583c9 5963
96de1e66
N
5964static struct md_sysfs_entry
5965raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
5966 raid5_show_stripe_cache_size,
5967 raid5_store_stripe_cache_size);
3f294f4f 5968
d06f191f
MS
5969static ssize_t
5970raid5_show_rmw_level(struct mddev *mddev, char *page)
5971{
5972 struct r5conf *conf = mddev->private;
5973 if (conf)
5974 return sprintf(page, "%d\n", conf->rmw_level);
5975 else
5976 return 0;
5977}
5978
5979static ssize_t
5980raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
5981{
5982 struct r5conf *conf = mddev->private;
5983 unsigned long new;
5984
5985 if (!conf)
5986 return -ENODEV;
5987
5988 if (len >= PAGE_SIZE)
5989 return -EINVAL;
5990
5991 if (kstrtoul(page, 10, &new))
5992 return -EINVAL;
5993
5994 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
5995 return -EINVAL;
5996
5997 if (new != PARITY_DISABLE_RMW &&
5998 new != PARITY_ENABLE_RMW &&
5999 new != PARITY_PREFER_RMW)
6000 return -EINVAL;
6001
6002 conf->rmw_level = new;
6003 return len;
6004}
6005
6006static struct md_sysfs_entry
6007raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
6008 raid5_show_rmw_level,
6009 raid5_store_rmw_level);
6010
6011
8b3e6cdc 6012static ssize_t
fd01b88c 6013raid5_show_preread_threshold(struct mddev *mddev, char *page)
8b3e6cdc 6014{
7b1485ba
N
6015 struct r5conf *conf;
6016 int ret = 0;
6017 spin_lock(&mddev->lock);
6018 conf = mddev->private;
8b3e6cdc 6019 if (conf)
7b1485ba
N
6020 ret = sprintf(page, "%d\n", conf->bypass_threshold);
6021 spin_unlock(&mddev->lock);
6022 return ret;
8b3e6cdc
DW
6023}
6024
6025static ssize_t
fd01b88c 6026raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
8b3e6cdc 6027{
6791875e 6028 struct r5conf *conf;
4ef197d8 6029 unsigned long new;
6791875e
N
6030 int err;
6031
8b3e6cdc
DW
6032 if (len >= PAGE_SIZE)
6033 return -EINVAL;
b29bebd6 6034 if (kstrtoul(page, 10, &new))
8b3e6cdc 6035 return -EINVAL;
6791875e
N
6036
6037 err = mddev_lock(mddev);
6038 if (err)
6039 return err;
6040 conf = mddev->private;
6041 if (!conf)
6042 err = -ENODEV;
edbe83ab 6043 else if (new > conf->min_nr_stripes)
6791875e
N
6044 err = -EINVAL;
6045 else
6046 conf->bypass_threshold = new;
6047 mddev_unlock(mddev);
6048 return err ?: len;
8b3e6cdc
DW
6049}
6050
6051static struct md_sysfs_entry
6052raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
6053 S_IRUGO | S_IWUSR,
6054 raid5_show_preread_threshold,
6055 raid5_store_preread_threshold);
6056
d592a996
SL
6057static ssize_t
6058raid5_show_skip_copy(struct mddev *mddev, char *page)
6059{
7b1485ba
N
6060 struct r5conf *conf;
6061 int ret = 0;
6062 spin_lock(&mddev->lock);
6063 conf = mddev->private;
d592a996 6064 if (conf)
7b1485ba
N
6065 ret = sprintf(page, "%d\n", conf->skip_copy);
6066 spin_unlock(&mddev->lock);
6067 return ret;
d592a996
SL
6068}
6069
6070static ssize_t
6071raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
6072{
6791875e 6073 struct r5conf *conf;
d592a996 6074 unsigned long new;
6791875e
N
6075 int err;
6076
d592a996
SL
6077 if (len >= PAGE_SIZE)
6078 return -EINVAL;
d592a996
SL
6079 if (kstrtoul(page, 10, &new))
6080 return -EINVAL;
6081 new = !!new;
6791875e
N
6082
6083 err = mddev_lock(mddev);
6084 if (err)
6085 return err;
6086 conf = mddev->private;
6087 if (!conf)
6088 err = -ENODEV;
6089 else if (new != conf->skip_copy) {
6090 mddev_suspend(mddev);
6091 conf->skip_copy = new;
6092 if (new)
6093 mddev->queue->backing_dev_info.capabilities |=
6094 BDI_CAP_STABLE_WRITES;
6095 else
6096 mddev->queue->backing_dev_info.capabilities &=
6097 ~BDI_CAP_STABLE_WRITES;
6098 mddev_resume(mddev);
6099 }
6100 mddev_unlock(mddev);
6101 return err ?: len;
d592a996
SL
6102}
6103
6104static struct md_sysfs_entry
6105raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
6106 raid5_show_skip_copy,
6107 raid5_store_skip_copy);
6108
3f294f4f 6109static ssize_t
fd01b88c 6110stripe_cache_active_show(struct mddev *mddev, char *page)
3f294f4f 6111{
d1688a6d 6112 struct r5conf *conf = mddev->private;
96de1e66
N
6113 if (conf)
6114 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
6115 else
6116 return 0;
3f294f4f
N
6117}
6118
96de1e66
N
6119static struct md_sysfs_entry
6120raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
3f294f4f 6121
b721420e
SL
6122static ssize_t
6123raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
6124{
7b1485ba
N
6125 struct r5conf *conf;
6126 int ret = 0;
6127 spin_lock(&mddev->lock);
6128 conf = mddev->private;
b721420e 6129 if (conf)
7b1485ba
N
6130 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
6131 spin_unlock(&mddev->lock);
6132 return ret;
b721420e
SL
6133}
6134
60aaf933 6135static int alloc_thread_groups(struct r5conf *conf, int cnt,
6136 int *group_cnt,
6137 int *worker_cnt_per_group,
6138 struct r5worker_group **worker_groups);
b721420e
SL
6139static ssize_t
6140raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
6141{
6791875e 6142 struct r5conf *conf;
b721420e
SL
6143 unsigned long new;
6144 int err;
60aaf933 6145 struct r5worker_group *new_groups, *old_groups;
6146 int group_cnt, worker_cnt_per_group;
b721420e
SL
6147
6148 if (len >= PAGE_SIZE)
6149 return -EINVAL;
b721420e
SL
6150 if (kstrtoul(page, 10, &new))
6151 return -EINVAL;
6152
6791875e
N
6153 err = mddev_lock(mddev);
6154 if (err)
6155 return err;
6156 conf = mddev->private;
6157 if (!conf)
6158 err = -ENODEV;
6159 else if (new != conf->worker_cnt_per_group) {
6160 mddev_suspend(mddev);
b721420e 6161
6791875e
N
6162 old_groups = conf->worker_groups;
6163 if (old_groups)
6164 flush_workqueue(raid5_wq);
d206dcfa 6165
6791875e
N
6166 err = alloc_thread_groups(conf, new,
6167 &group_cnt, &worker_cnt_per_group,
6168 &new_groups);
6169 if (!err) {
6170 spin_lock_irq(&conf->device_lock);
6171 conf->group_cnt = group_cnt;
6172 conf->worker_cnt_per_group = worker_cnt_per_group;
6173 conf->worker_groups = new_groups;
6174 spin_unlock_irq(&conf->device_lock);
b721420e 6175
6791875e
N
6176 if (old_groups)
6177 kfree(old_groups[0].workers);
6178 kfree(old_groups);
6179 }
6180 mddev_resume(mddev);
b721420e 6181 }
6791875e 6182 mddev_unlock(mddev);
b721420e 6183
6791875e 6184 return err ?: len;
b721420e
SL
6185}
6186
6187static struct md_sysfs_entry
6188raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
6189 raid5_show_group_thread_cnt,
6190 raid5_store_group_thread_cnt);
6191
007583c9 6192static struct attribute *raid5_attrs[] = {
3f294f4f
N
6193 &raid5_stripecache_size.attr,
6194 &raid5_stripecache_active.attr,
8b3e6cdc 6195 &raid5_preread_bypass_threshold.attr,
b721420e 6196 &raid5_group_thread_cnt.attr,
d592a996 6197 &raid5_skip_copy.attr,
d06f191f 6198 &raid5_rmw_level.attr,
3f294f4f
N
6199 NULL,
6200};
007583c9
N
6201static struct attribute_group raid5_attrs_group = {
6202 .name = NULL,
6203 .attrs = raid5_attrs,
3f294f4f
N
6204};
6205
60aaf933 6206static int alloc_thread_groups(struct r5conf *conf, int cnt,
6207 int *group_cnt,
6208 int *worker_cnt_per_group,
6209 struct r5worker_group **worker_groups)
851c30c9 6210{
566c09c5 6211 int i, j, k;
851c30c9
SL
6212 ssize_t size;
6213 struct r5worker *workers;
6214
60aaf933 6215 *worker_cnt_per_group = cnt;
851c30c9 6216 if (cnt == 0) {
60aaf933 6217 *group_cnt = 0;
6218 *worker_groups = NULL;
851c30c9
SL
6219 return 0;
6220 }
60aaf933 6221 *group_cnt = num_possible_nodes();
851c30c9 6222 size = sizeof(struct r5worker) * cnt;
60aaf933 6223 workers = kzalloc(size * *group_cnt, GFP_NOIO);
6224 *worker_groups = kzalloc(sizeof(struct r5worker_group) *
6225 *group_cnt, GFP_NOIO);
6226 if (!*worker_groups || !workers) {
851c30c9 6227 kfree(workers);
60aaf933 6228 kfree(*worker_groups);
851c30c9
SL
6229 return -ENOMEM;
6230 }
6231
60aaf933 6232 for (i = 0; i < *group_cnt; i++) {
851c30c9
SL
6233 struct r5worker_group *group;
6234
0c775d52 6235 group = &(*worker_groups)[i];
851c30c9
SL
6236 INIT_LIST_HEAD(&group->handle_list);
6237 group->conf = conf;
6238 group->workers = workers + i * cnt;
6239
6240 for (j = 0; j < cnt; j++) {
566c09c5
SL
6241 struct r5worker *worker = group->workers + j;
6242 worker->group = group;
6243 INIT_WORK(&worker->work, raid5_do_work);
6244
6245 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
6246 INIT_LIST_HEAD(worker->temp_inactive_list + k);
851c30c9
SL
6247 }
6248 }
6249
6250 return 0;
6251}
6252
6253static void free_thread_groups(struct r5conf *conf)
6254{
6255 if (conf->worker_groups)
6256 kfree(conf->worker_groups[0].workers);
6257 kfree(conf->worker_groups);
6258 conf->worker_groups = NULL;
6259}
6260
80c3a6ce 6261static sector_t
fd01b88c 6262raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
80c3a6ce 6263{
d1688a6d 6264 struct r5conf *conf = mddev->private;
80c3a6ce
DW
6265
6266 if (!sectors)
6267 sectors = mddev->dev_sectors;
5e5e3e78 6268 if (!raid_disks)
7ec05478 6269 /* size is defined by the smallest of previous and new size */
5e5e3e78 6270 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
80c3a6ce 6271
3cb5edf4
N
6272 sectors &= ~((sector_t)conf->chunk_sectors - 1);
6273 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
80c3a6ce
DW
6274 return sectors * (raid_disks - conf->max_degraded);
6275}
6276
789b5e03
ON
6277static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6278{
6279 safe_put_page(percpu->spare_page);
46d5b785 6280 if (percpu->scribble)
6281 flex_array_free(percpu->scribble);
789b5e03
ON
6282 percpu->spare_page = NULL;
6283 percpu->scribble = NULL;
6284}
6285
6286static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6287{
6288 if (conf->level == 6 && !percpu->spare_page)
6289 percpu->spare_page = alloc_page(GFP_KERNEL);
6290 if (!percpu->scribble)
46d5b785 6291 percpu->scribble = scribble_alloc(max(conf->raid_disks,
738a2738
N
6292 conf->previous_raid_disks),
6293 max(conf->chunk_sectors,
6294 conf->prev_chunk_sectors)
6295 / STRIPE_SECTORS,
6296 GFP_KERNEL);
789b5e03
ON
6297
6298 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
6299 free_scratch_buffer(conf, percpu);
6300 return -ENOMEM;
6301 }
6302
6303 return 0;
6304}
6305
29c6d1bb 6306static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
36d1c647 6307{
29c6d1bb
SAS
6308 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
6309
6310 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
6311 return 0;
6312}
36d1c647 6313
29c6d1bb
SAS
6314static void raid5_free_percpu(struct r5conf *conf)
6315{
36d1c647
DW
6316 if (!conf->percpu)
6317 return;
6318
29c6d1bb 6319 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
36d1c647
DW
6320 free_percpu(conf->percpu);
6321}
6322
d1688a6d 6323static void free_conf(struct r5conf *conf)
95fc17aa 6324{
5c7e81c3
SL
6325 if (conf->log)
6326 r5l_exit_log(conf->log);
30c89465 6327 if (conf->shrinker.nr_deferred)
edbe83ab 6328 unregister_shrinker(&conf->shrinker);
5c7e81c3 6329
851c30c9 6330 free_thread_groups(conf);
95fc17aa 6331 shrink_stripes(conf);
36d1c647 6332 raid5_free_percpu(conf);
95fc17aa
DW
6333 kfree(conf->disks);
6334 kfree(conf->stripe_hashtbl);
6335 kfree(conf);
6336}
6337
29c6d1bb 6338static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
36d1c647 6339{
29c6d1bb 6340 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
36d1c647
DW
6341 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
6342
29c6d1bb 6343 if (alloc_scratch_buffer(conf, percpu)) {
cc6167b4
N
6344 pr_warn("%s: failed memory allocation for cpu%u\n",
6345 __func__, cpu);
29c6d1bb 6346 return -ENOMEM;
36d1c647 6347 }
29c6d1bb 6348 return 0;
36d1c647 6349}
36d1c647 6350
d1688a6d 6351static int raid5_alloc_percpu(struct r5conf *conf)
36d1c647 6352{
789b5e03 6353 int err = 0;
36d1c647 6354
789b5e03
ON
6355 conf->percpu = alloc_percpu(struct raid5_percpu);
6356 if (!conf->percpu)
36d1c647 6357 return -ENOMEM;
789b5e03 6358
29c6d1bb 6359 err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
27a353c0
SL
6360 if (!err) {
6361 conf->scribble_disks = max(conf->raid_disks,
6362 conf->previous_raid_disks);
6363 conf->scribble_sectors = max(conf->chunk_sectors,
6364 conf->prev_chunk_sectors);
6365 }
36d1c647
DW
6366 return err;
6367}
6368
edbe83ab
N
6369static unsigned long raid5_cache_scan(struct shrinker *shrink,
6370 struct shrink_control *sc)
6371{
6372 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
2d5b569b
N
6373 unsigned long ret = SHRINK_STOP;
6374
6375 if (mutex_trylock(&conf->cache_size_mutex)) {
6376 ret= 0;
49895bcc
N
6377 while (ret < sc->nr_to_scan &&
6378 conf->max_nr_stripes > conf->min_nr_stripes) {
2d5b569b
N
6379 if (drop_one_stripe(conf) == 0) {
6380 ret = SHRINK_STOP;
6381 break;
6382 }
6383 ret++;
6384 }
6385 mutex_unlock(&conf->cache_size_mutex);
edbe83ab
N
6386 }
6387 return ret;
6388}
6389
6390static unsigned long raid5_cache_count(struct shrinker *shrink,
6391 struct shrink_control *sc)
6392{
6393 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
6394
6395 if (conf->max_nr_stripes < conf->min_nr_stripes)
6396 /* unlikely, but not impossible */
6397 return 0;
6398 return conf->max_nr_stripes - conf->min_nr_stripes;
6399}
6400
d1688a6d 6401static struct r5conf *setup_conf(struct mddev *mddev)
1da177e4 6402{
d1688a6d 6403 struct r5conf *conf;
5e5e3e78 6404 int raid_disk, memory, max_disks;
3cb03002 6405 struct md_rdev *rdev;
1da177e4 6406 struct disk_info *disk;
0232605d 6407 char pers_name[6];
566c09c5 6408 int i;
60aaf933 6409 int group_cnt, worker_cnt_per_group;
6410 struct r5worker_group *new_group;
1da177e4 6411
91adb564
N
6412 if (mddev->new_level != 5
6413 && mddev->new_level != 4
6414 && mddev->new_level != 6) {
cc6167b4
N
6415 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
6416 mdname(mddev), mddev->new_level);
91adb564 6417 return ERR_PTR(-EIO);
1da177e4 6418 }
91adb564
N
6419 if ((mddev->new_level == 5
6420 && !algorithm_valid_raid5(mddev->new_layout)) ||
6421 (mddev->new_level == 6
6422 && !algorithm_valid_raid6(mddev->new_layout))) {
cc6167b4
N
6423 pr_warn("md/raid:%s: layout %d not supported\n",
6424 mdname(mddev), mddev->new_layout);
91adb564 6425 return ERR_PTR(-EIO);
99c0fb5f 6426 }
91adb564 6427 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
cc6167b4
N
6428 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
6429 mdname(mddev), mddev->raid_disks);
91adb564 6430 return ERR_PTR(-EINVAL);
4bbf3771
N
6431 }
6432
664e7c41
AN
6433 if (!mddev->new_chunk_sectors ||
6434 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
6435 !is_power_of_2(mddev->new_chunk_sectors)) {
cc6167b4
N
6436 pr_warn("md/raid:%s: invalid chunk size %d\n",
6437 mdname(mddev), mddev->new_chunk_sectors << 9);
91adb564 6438 return ERR_PTR(-EINVAL);
f6705578
N
6439 }
6440
d1688a6d 6441 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
91adb564 6442 if (conf == NULL)
1da177e4 6443 goto abort;
851c30c9 6444 /* Don't enable multi-threading by default*/
60aaf933 6445 if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group,
6446 &new_group)) {
6447 conf->group_cnt = group_cnt;
6448 conf->worker_cnt_per_group = worker_cnt_per_group;
6449 conf->worker_groups = new_group;
6450 } else
851c30c9 6451 goto abort;
f5efd45a 6452 spin_lock_init(&conf->device_lock);
c46501b2 6453 seqcount_init(&conf->gen_lock);
2d5b569b 6454 mutex_init(&conf->cache_size_mutex);
b1b46486 6455 init_waitqueue_head(&conf->wait_for_quiescent);
6ab2a4b8 6456 init_waitqueue_head(&conf->wait_for_stripe);
f5efd45a
DW
6457 init_waitqueue_head(&conf->wait_for_overlap);
6458 INIT_LIST_HEAD(&conf->handle_list);
6459 INIT_LIST_HEAD(&conf->hold_list);
6460 INIT_LIST_HEAD(&conf->delayed_list);
6461 INIT_LIST_HEAD(&conf->bitmap_list);
c3cce6cd 6462 bio_list_init(&conf->return_bi);
773ca82f 6463 init_llist_head(&conf->released_stripes);
f5efd45a
DW
6464 atomic_set(&conf->active_stripes, 0);
6465 atomic_set(&conf->preread_active_stripes, 0);
6466 atomic_set(&conf->active_aligned_reads, 0);
6467 conf->bypass_threshold = BYPASS_THRESHOLD;
d890fa2b 6468 conf->recovery_disabled = mddev->recovery_disabled - 1;
91adb564
N
6469
6470 conf->raid_disks = mddev->raid_disks;
6471 if (mddev->reshape_position == MaxSector)
6472 conf->previous_raid_disks = mddev->raid_disks;
6473 else
f6705578 6474 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
5e5e3e78 6475 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
f6705578 6476
5e5e3e78 6477 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
b55e6bfc
N
6478 GFP_KERNEL);
6479 if (!conf->disks)
6480 goto abort;
9ffae0cf 6481
1da177e4
LT
6482 conf->mddev = mddev;
6483
fccddba0 6484 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
1da177e4 6485 goto abort;
1da177e4 6486
566c09c5
SL
6487 /* We init hash_locks[0] separately to that it can be used
6488 * as the reference lock in the spin_lock_nest_lock() call
6489 * in lock_all_device_hash_locks_irq in order to convince
6490 * lockdep that we know what we are doing.
6491 */
6492 spin_lock_init(conf->hash_locks);
6493 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
6494 spin_lock_init(conf->hash_locks + i);
6495
6496 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6497 INIT_LIST_HEAD(conf->inactive_list + i);
6498
6499 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6500 INIT_LIST_HEAD(conf->temp_inactive_list + i);
6501
36d1c647 6502 conf->level = mddev->new_level;
46d5b785 6503 conf->chunk_sectors = mddev->new_chunk_sectors;
36d1c647
DW
6504 if (raid5_alloc_percpu(conf) != 0)
6505 goto abort;
6506
0c55e022 6507 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
1da177e4 6508
dafb20fa 6509 rdev_for_each(rdev, mddev) {
1da177e4 6510 raid_disk = rdev->raid_disk;
5e5e3e78 6511 if (raid_disk >= max_disks
f2076e7d 6512 || raid_disk < 0 || test_bit(Journal, &rdev->flags))
1da177e4
LT
6513 continue;
6514 disk = conf->disks + raid_disk;
6515
17045f52
N
6516 if (test_bit(Replacement, &rdev->flags)) {
6517 if (disk->replacement)
6518 goto abort;
6519 disk->replacement = rdev;
6520 } else {
6521 if (disk->rdev)
6522 goto abort;
6523 disk->rdev = rdev;
6524 }
1da177e4 6525
b2d444d7 6526 if (test_bit(In_sync, &rdev->flags)) {
1da177e4 6527 char b[BDEVNAME_SIZE];
cc6167b4
N
6528 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
6529 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
d6b212f4 6530 } else if (rdev->saved_raid_disk != raid_disk)
8c2e870a
NB
6531 /* Cannot rely on bitmap to complete recovery */
6532 conf->fullsync = 1;
1da177e4
LT
6533 }
6534
91adb564 6535 conf->level = mddev->new_level;
584acdd4 6536 if (conf->level == 6) {
16a53ecc 6537 conf->max_degraded = 2;
584acdd4
MS
6538 if (raid6_call.xor_syndrome)
6539 conf->rmw_level = PARITY_ENABLE_RMW;
6540 else
6541 conf->rmw_level = PARITY_DISABLE_RMW;
6542 } else {
16a53ecc 6543 conf->max_degraded = 1;
584acdd4
MS
6544 conf->rmw_level = PARITY_ENABLE_RMW;
6545 }
91adb564 6546 conf->algorithm = mddev->new_layout;
fef9c61f 6547 conf->reshape_progress = mddev->reshape_position;
e183eaed 6548 if (conf->reshape_progress != MaxSector) {
09c9e5fa 6549 conf->prev_chunk_sectors = mddev->chunk_sectors;
e183eaed 6550 conf->prev_algo = mddev->layout;
5cac6bcb
N
6551 } else {
6552 conf->prev_chunk_sectors = conf->chunk_sectors;
6553 conf->prev_algo = conf->algorithm;
e183eaed 6554 }
1da177e4 6555
edbe83ab 6556 conf->min_nr_stripes = NR_STRIPES;
ad5b0f76
SL
6557 if (mddev->reshape_position != MaxSector) {
6558 int stripes = max_t(int,
6559 ((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4,
6560 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4);
6561 conf->min_nr_stripes = max(NR_STRIPES, stripes);
6562 if (conf->min_nr_stripes != NR_STRIPES)
cc6167b4 6563 pr_info("md/raid:%s: force stripe size %d for reshape\n",
ad5b0f76
SL
6564 mdname(mddev), conf->min_nr_stripes);
6565 }
edbe83ab 6566 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
5e5e3e78 6567 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4bda556a 6568 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
edbe83ab 6569 if (grow_stripes(conf, conf->min_nr_stripes)) {
cc6167b4
N
6570 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
6571 mdname(mddev), memory);
91adb564
N
6572 goto abort;
6573 } else
cc6167b4 6574 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory);
edbe83ab
N
6575 /*
6576 * Losing a stripe head costs more than the time to refill it,
6577 * it reduces the queue depth and so can hurt throughput.
6578 * So set it rather large, scaled by number of devices.
6579 */
6580 conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
6581 conf->shrinker.scan_objects = raid5_cache_scan;
6582 conf->shrinker.count_objects = raid5_cache_count;
6583 conf->shrinker.batch = 128;
6584 conf->shrinker.flags = 0;
6a0f53ff 6585 if (register_shrinker(&conf->shrinker)) {
cc6167b4
N
6586 pr_warn("md/raid:%s: couldn't register shrinker.\n",
6587 mdname(mddev));
6a0f53ff
CY
6588 goto abort;
6589 }
1da177e4 6590
0232605d
N
6591 sprintf(pers_name, "raid%d", mddev->new_level);
6592 conf->thread = md_register_thread(raid5d, mddev, pers_name);
91adb564 6593 if (!conf->thread) {
cc6167b4
N
6594 pr_warn("md/raid:%s: couldn't allocate thread.\n",
6595 mdname(mddev));
16a53ecc
N
6596 goto abort;
6597 }
91adb564
N
6598
6599 return conf;
6600
6601 abort:
6602 if (conf) {
95fc17aa 6603 free_conf(conf);
91adb564
N
6604 return ERR_PTR(-EIO);
6605 } else
6606 return ERR_PTR(-ENOMEM);
6607}
6608
c148ffdc
N
6609static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
6610{
6611 switch (algo) {
6612 case ALGORITHM_PARITY_0:
6613 if (raid_disk < max_degraded)
6614 return 1;
6615 break;
6616 case ALGORITHM_PARITY_N:
6617 if (raid_disk >= raid_disks - max_degraded)
6618 return 1;
6619 break;
6620 case ALGORITHM_PARITY_0_6:
f72ffdd6 6621 if (raid_disk == 0 ||
c148ffdc
N
6622 raid_disk == raid_disks - 1)
6623 return 1;
6624 break;
6625 case ALGORITHM_LEFT_ASYMMETRIC_6:
6626 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6627 case ALGORITHM_LEFT_SYMMETRIC_6:
6628 case ALGORITHM_RIGHT_SYMMETRIC_6:
6629 if (raid_disk == raid_disks - 1)
6630 return 1;
6631 }
6632 return 0;
6633}
6634
849674e4 6635static int raid5_run(struct mddev *mddev)
91adb564 6636{
d1688a6d 6637 struct r5conf *conf;
9f7c2220 6638 int working_disks = 0;
c148ffdc 6639 int dirty_parity_disks = 0;
3cb03002 6640 struct md_rdev *rdev;
713cf5a6 6641 struct md_rdev *journal_dev = NULL;
c148ffdc 6642 sector_t reshape_offset = 0;
17045f52 6643 int i;
b5254dd5
N
6644 long long min_offset_diff = 0;
6645 int first = 1;
91adb564 6646
8c6ac868 6647 if (mddev->recovery_cp != MaxSector)
cc6167b4
N
6648 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
6649 mdname(mddev));
b5254dd5
N
6650
6651 rdev_for_each(rdev, mddev) {
6652 long long diff;
713cf5a6 6653
f2076e7d 6654 if (test_bit(Journal, &rdev->flags)) {
713cf5a6 6655 journal_dev = rdev;
f2076e7d
SL
6656 continue;
6657 }
b5254dd5
N
6658 if (rdev->raid_disk < 0)
6659 continue;
6660 diff = (rdev->new_data_offset - rdev->data_offset);
6661 if (first) {
6662 min_offset_diff = diff;
6663 first = 0;
6664 } else if (mddev->reshape_backwards &&
6665 diff < min_offset_diff)
6666 min_offset_diff = diff;
6667 else if (!mddev->reshape_backwards &&
6668 diff > min_offset_diff)
6669 min_offset_diff = diff;
6670 }
6671
91adb564
N
6672 if (mddev->reshape_position != MaxSector) {
6673 /* Check that we can continue the reshape.
b5254dd5
N
6674 * Difficulties arise if the stripe we would write to
6675 * next is at or after the stripe we would read from next.
6676 * For a reshape that changes the number of devices, this
6677 * is only possible for a very short time, and mdadm makes
6678 * sure that time appears to have past before assembling
6679 * the array. So we fail if that time hasn't passed.
6680 * For a reshape that keeps the number of devices the same
6681 * mdadm must be monitoring the reshape can keeping the
6682 * critical areas read-only and backed up. It will start
6683 * the array in read-only mode, so we check for that.
91adb564
N
6684 */
6685 sector_t here_new, here_old;
6686 int old_disks;
18b00334 6687 int max_degraded = (mddev->level == 6 ? 2 : 1);
05256d98
N
6688 int chunk_sectors;
6689 int new_data_disks;
91adb564 6690
713cf5a6 6691 if (journal_dev) {
cc6167b4
N
6692 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
6693 mdname(mddev));
713cf5a6
SL
6694 return -EINVAL;
6695 }
6696
88ce4930 6697 if (mddev->new_level != mddev->level) {
cc6167b4
N
6698 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
6699 mdname(mddev));
91adb564
N
6700 return -EINVAL;
6701 }
91adb564
N
6702 old_disks = mddev->raid_disks - mddev->delta_disks;
6703 /* reshape_position must be on a new-stripe boundary, and one
6704 * further up in new geometry must map after here in old
6705 * geometry.
05256d98
N
6706 * If the chunk sizes are different, then as we perform reshape
6707 * in units of the largest of the two, reshape_position needs
6708 * be a multiple of the largest chunk size times new data disks.
91adb564
N
6709 */
6710 here_new = mddev->reshape_position;
05256d98
N
6711 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
6712 new_data_disks = mddev->raid_disks - max_degraded;
6713 if (sector_div(here_new, chunk_sectors * new_data_disks)) {
cc6167b4
N
6714 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
6715 mdname(mddev));
91adb564
N
6716 return -EINVAL;
6717 }
05256d98 6718 reshape_offset = here_new * chunk_sectors;
91adb564
N
6719 /* here_new is the stripe we will write to */
6720 here_old = mddev->reshape_position;
05256d98 6721 sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
91adb564
N
6722 /* here_old is the first stripe that we might need to read
6723 * from */
67ac6011
N
6724 if (mddev->delta_disks == 0) {
6725 /* We cannot be sure it is safe to start an in-place
b5254dd5 6726 * reshape. It is only safe if user-space is monitoring
67ac6011
N
6727 * and taking constant backups.
6728 * mdadm always starts a situation like this in
6729 * readonly mode so it can take control before
6730 * allowing any writes. So just check for that.
6731 */
b5254dd5
N
6732 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
6733 abs(min_offset_diff) >= mddev->new_chunk_sectors)
6734 /* not really in-place - so OK */;
6735 else if (mddev->ro == 0) {
cc6167b4
N
6736 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
6737 mdname(mddev));
67ac6011
N
6738 return -EINVAL;
6739 }
2c810cdd 6740 } else if (mddev->reshape_backwards
05256d98
N
6741 ? (here_new * chunk_sectors + min_offset_diff <=
6742 here_old * chunk_sectors)
6743 : (here_new * chunk_sectors >=
6744 here_old * chunk_sectors + (-min_offset_diff))) {
91adb564 6745 /* Reading from the same stripe as writing to - bad */
cc6167b4
N
6746 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
6747 mdname(mddev));
91adb564
N
6748 return -EINVAL;
6749 }
cc6167b4 6750 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev));
91adb564
N
6751 /* OK, we should be able to continue; */
6752 } else {
6753 BUG_ON(mddev->level != mddev->new_level);
6754 BUG_ON(mddev->layout != mddev->new_layout);
664e7c41 6755 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
91adb564 6756 BUG_ON(mddev->delta_disks != 0);
1da177e4 6757 }
91adb564 6758
245f46c2
N
6759 if (mddev->private == NULL)
6760 conf = setup_conf(mddev);
6761 else
6762 conf = mddev->private;
6763
91adb564
N
6764 if (IS_ERR(conf))
6765 return PTR_ERR(conf);
6766
486b0f7b
SL
6767 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
6768 if (!journal_dev) {
cc6167b4
N
6769 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
6770 mdname(mddev));
486b0f7b
SL
6771 mddev->ro = 1;
6772 set_disk_ro(mddev->gendisk, 1);
6773 } else if (mddev->recovery_cp == MaxSector)
6774 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
7dde2ad3
SL
6775 }
6776
b5254dd5 6777 conf->min_offset_diff = min_offset_diff;
91adb564
N
6778 mddev->thread = conf->thread;
6779 conf->thread = NULL;
6780 mddev->private = conf;
6781
17045f52
N
6782 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
6783 i++) {
6784 rdev = conf->disks[i].rdev;
6785 if (!rdev && conf->disks[i].replacement) {
6786 /* The replacement is all we have yet */
6787 rdev = conf->disks[i].replacement;
6788 conf->disks[i].replacement = NULL;
6789 clear_bit(Replacement, &rdev->flags);
6790 conf->disks[i].rdev = rdev;
6791 }
6792 if (!rdev)
c148ffdc 6793 continue;
17045f52
N
6794 if (conf->disks[i].replacement &&
6795 conf->reshape_progress != MaxSector) {
6796 /* replacements and reshape simply do not mix. */
cc6167b4 6797 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
17045f52
N
6798 goto abort;
6799 }
2f115882 6800 if (test_bit(In_sync, &rdev->flags)) {
91adb564 6801 working_disks++;
2f115882
N
6802 continue;
6803 }
c148ffdc
N
6804 /* This disc is not fully in-sync. However if it
6805 * just stored parity (beyond the recovery_offset),
6806 * when we don't need to be concerned about the
6807 * array being dirty.
6808 * When reshape goes 'backwards', we never have
6809 * partially completed devices, so we only need
6810 * to worry about reshape going forwards.
6811 */
6812 /* Hack because v0.91 doesn't store recovery_offset properly. */
6813 if (mddev->major_version == 0 &&
6814 mddev->minor_version > 90)
6815 rdev->recovery_offset = reshape_offset;
5026d7a9 6816
c148ffdc
N
6817 if (rdev->recovery_offset < reshape_offset) {
6818 /* We need to check old and new layout */
6819 if (!only_parity(rdev->raid_disk,
6820 conf->algorithm,
6821 conf->raid_disks,
6822 conf->max_degraded))
6823 continue;
6824 }
6825 if (!only_parity(rdev->raid_disk,
6826 conf->prev_algo,
6827 conf->previous_raid_disks,
6828 conf->max_degraded))
6829 continue;
6830 dirty_parity_disks++;
6831 }
91adb564 6832
17045f52
N
6833 /*
6834 * 0 for a fully functional array, 1 or 2 for a degraded array.
6835 */
908f4fbd 6836 mddev->degraded = calc_degraded(conf);
91adb564 6837
674806d6 6838 if (has_failed(conf)) {
cc6167b4 6839 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
02c2de8c 6840 mdname(mddev), mddev->degraded, conf->raid_disks);
1da177e4
LT
6841 goto abort;
6842 }
6843
91adb564 6844 /* device size must be a multiple of chunk size */
9d8f0363 6845 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
91adb564
N
6846 mddev->resync_max_sectors = mddev->dev_sectors;
6847
c148ffdc 6848 if (mddev->degraded > dirty_parity_disks &&
1da177e4 6849 mddev->recovery_cp != MaxSector) {
6ff8d8ec 6850 if (mddev->ok_start_degraded)
cc6167b4
N
6851 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
6852 mdname(mddev));
6ff8d8ec 6853 else {
cc6167b4
N
6854 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
6855 mdname(mddev));
6ff8d8ec
N
6856 goto abort;
6857 }
1da177e4
LT
6858 }
6859
cc6167b4
N
6860 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
6861 mdname(mddev), conf->level,
6862 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
6863 mddev->new_layout);
1da177e4
LT
6864
6865 print_raid5_conf(conf);
6866
fef9c61f 6867 if (conf->reshape_progress != MaxSector) {
fef9c61f 6868 conf->reshape_safe = conf->reshape_progress;
f6705578
N
6869 atomic_set(&conf->reshape_stripes, 0);
6870 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6871 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6872 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6873 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6874 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
0da3c619 6875 "reshape");
f6705578
N
6876 }
6877
1da177e4 6878 /* Ok, everything is just fine now */
a64c876f
N
6879 if (mddev->to_remove == &raid5_attrs_group)
6880 mddev->to_remove = NULL;
00bcb4ac
N
6881 else if (mddev->kobj.sd &&
6882 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
cc6167b4
N
6883 pr_warn("raid5: failed to create sysfs attributes for %s\n",
6884 mdname(mddev));
4a5add49 6885 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
7a5febe9 6886
4a5add49 6887 if (mddev->queue) {
9f7c2220 6888 int chunk_size;
620125f2 6889 bool discard_supported = true;
4a5add49
N
6890 /* read-ahead size must cover two whole stripes, which
6891 * is 2 * (datadisks) * chunksize where 'n' is the
6892 * number of raid devices
6893 */
6894 int data_disks = conf->previous_raid_disks - conf->max_degraded;
6895 int stripe = data_disks *
6896 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
6897 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6898 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
91adb564 6899
9f7c2220
N
6900 chunk_size = mddev->chunk_sectors << 9;
6901 blk_queue_io_min(mddev->queue, chunk_size);
6902 blk_queue_io_opt(mddev->queue, chunk_size *
6903 (conf->raid_disks - conf->max_degraded));
c78afc62 6904 mddev->queue->limits.raid_partial_stripes_expensive = 1;
620125f2
SL
6905 /*
6906 * We can only discard a whole stripe. It doesn't make sense to
6907 * discard data disk but write parity disk
6908 */
6909 stripe = stripe * PAGE_SIZE;
4ac6875e
N
6910 /* Round up to power of 2, as discard handling
6911 * currently assumes that */
6912 while ((stripe-1) & stripe)
6913 stripe = (stripe | (stripe-1)) + 1;
620125f2
SL
6914 mddev->queue->limits.discard_alignment = stripe;
6915 mddev->queue->limits.discard_granularity = stripe;
6916 /*
6917 * unaligned part of discard request will be ignored, so can't
8e0e99ba 6918 * guarantee discard_zeroes_data
620125f2
SL
6919 */
6920 mddev->queue->limits.discard_zeroes_data = 0;
8f6c2e4b 6921
5026d7a9
PA
6922 blk_queue_max_write_same_sectors(mddev->queue, 0);
6923
05616be5 6924 rdev_for_each(rdev, mddev) {
9f7c2220
N
6925 disk_stack_limits(mddev->gendisk, rdev->bdev,
6926 rdev->data_offset << 9);
05616be5
N
6927 disk_stack_limits(mddev->gendisk, rdev->bdev,
6928 rdev->new_data_offset << 9);
620125f2
SL
6929 /*
6930 * discard_zeroes_data is required, otherwise data
6931 * could be lost. Consider a scenario: discard a stripe
6932 * (the stripe could be inconsistent if
6933 * discard_zeroes_data is 0); write one disk of the
6934 * stripe (the stripe could be inconsistent again
6935 * depending on which disks are used to calculate
6936 * parity); the disk is broken; The stripe data of this
6937 * disk is lost.
6938 */
6939 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
6940 !bdev_get_queue(rdev->bdev)->
6941 limits.discard_zeroes_data)
6942 discard_supported = false;
8e0e99ba
N
6943 /* Unfortunately, discard_zeroes_data is not currently
6944 * a guarantee - just a hint. So we only allow DISCARD
6945 * if the sysadmin has confirmed that only safe devices
6946 * are in use by setting a module parameter.
6947 */
6948 if (!devices_handle_discard_safely) {
6949 if (discard_supported) {
6950 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
6951 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
6952 }
6953 discard_supported = false;
6954 }
05616be5 6955 }
620125f2
SL
6956
6957 if (discard_supported &&
e7597e69
JS
6958 mddev->queue->limits.max_discard_sectors >= (stripe >> 9) &&
6959 mddev->queue->limits.discard_granularity >= stripe)
620125f2
SL
6960 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
6961 mddev->queue);
6962 else
6963 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
6964 mddev->queue);
1dffdddd
SL
6965
6966 blk_queue_max_hw_sectors(mddev->queue, UINT_MAX);
9f7c2220 6967 }
23032a0e 6968
5c7e81c3
SL
6969 if (journal_dev) {
6970 char b[BDEVNAME_SIZE];
6971
cc6167b4
N
6972 pr_debug("md/raid:%s: using device %s as journal\n",
6973 mdname(mddev), bdevname(journal_dev->bdev, b));
5c7e81c3
SL
6974 r5l_init_log(conf, journal_dev);
6975 }
6976
1da177e4
LT
6977 return 0;
6978abort:
01f96c0a 6979 md_unregister_thread(&mddev->thread);
e4f869d9
N
6980 print_raid5_conf(conf);
6981 free_conf(conf);
1da177e4 6982 mddev->private = NULL;
cc6167b4 6983 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
1da177e4
LT
6984 return -EIO;
6985}
6986
afa0f557 6987static void raid5_free(struct mddev *mddev, void *priv)
1da177e4 6988{
afa0f557 6989 struct r5conf *conf = priv;
1da177e4 6990
95fc17aa 6991 free_conf(conf);
a64c876f 6992 mddev->to_remove = &raid5_attrs_group;
1da177e4
LT
6993}
6994
849674e4 6995static void raid5_status(struct seq_file *seq, struct mddev *mddev)
1da177e4 6996{
d1688a6d 6997 struct r5conf *conf = mddev->private;
1da177e4
LT
6998 int i;
6999
9d8f0363 7000 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
3cb5edf4 7001 conf->chunk_sectors / 2, mddev->layout);
02c2de8c 7002 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
5fd13351
N
7003 rcu_read_lock();
7004 for (i = 0; i < conf->raid_disks; i++) {
7005 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
7006 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
7007 }
7008 rcu_read_unlock();
1da177e4 7009 seq_printf (seq, "]");
1da177e4
LT
7010}
7011
d1688a6d 7012static void print_raid5_conf (struct r5conf *conf)
1da177e4
LT
7013{
7014 int i;
7015 struct disk_info *tmp;
7016
cc6167b4 7017 pr_debug("RAID conf printout:\n");
1da177e4 7018 if (!conf) {
cc6167b4 7019 pr_debug("(conf==NULL)\n");
1da177e4
LT
7020 return;
7021 }
cc6167b4 7022 pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level,
0c55e022
N
7023 conf->raid_disks,
7024 conf->raid_disks - conf->mddev->degraded);
1da177e4
LT
7025
7026 for (i = 0; i < conf->raid_disks; i++) {
7027 char b[BDEVNAME_SIZE];
7028 tmp = conf->disks + i;
7029 if (tmp->rdev)
cc6167b4 7030 pr_debug(" disk %d, o:%d, dev:%s\n",
0c55e022
N
7031 i, !test_bit(Faulty, &tmp->rdev->flags),
7032 bdevname(tmp->rdev->bdev, b));
1da177e4
LT
7033 }
7034}
7035
fd01b88c 7036static int raid5_spare_active(struct mddev *mddev)
1da177e4
LT
7037{
7038 int i;
d1688a6d 7039 struct r5conf *conf = mddev->private;
1da177e4 7040 struct disk_info *tmp;
6b965620
N
7041 int count = 0;
7042 unsigned long flags;
1da177e4
LT
7043
7044 for (i = 0; i < conf->raid_disks; i++) {
7045 tmp = conf->disks + i;
dd054fce
N
7046 if (tmp->replacement
7047 && tmp->replacement->recovery_offset == MaxSector
7048 && !test_bit(Faulty, &tmp->replacement->flags)
7049 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
7050 /* Replacement has just become active. */
7051 if (!tmp->rdev
7052 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
7053 count++;
7054 if (tmp->rdev) {
7055 /* Replaced device not technically faulty,
7056 * but we need to be sure it gets removed
7057 * and never re-added.
7058 */
7059 set_bit(Faulty, &tmp->rdev->flags);
7060 sysfs_notify_dirent_safe(
7061 tmp->rdev->sysfs_state);
7062 }
7063 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
7064 } else if (tmp->rdev
70fffd0b 7065 && tmp->rdev->recovery_offset == MaxSector
b2d444d7 7066 && !test_bit(Faulty, &tmp->rdev->flags)
c04be0aa 7067 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
6b965620 7068 count++;
43c73ca4 7069 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
1da177e4
LT
7070 }
7071 }
6b965620 7072 spin_lock_irqsave(&conf->device_lock, flags);
908f4fbd 7073 mddev->degraded = calc_degraded(conf);
6b965620 7074 spin_unlock_irqrestore(&conf->device_lock, flags);
1da177e4 7075 print_raid5_conf(conf);
6b965620 7076 return count;
1da177e4
LT
7077}
7078
b8321b68 7079static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 7080{
d1688a6d 7081 struct r5conf *conf = mddev->private;
1da177e4 7082 int err = 0;
b8321b68 7083 int number = rdev->raid_disk;
657e3e4d 7084 struct md_rdev **rdevp;
1da177e4
LT
7085 struct disk_info *p = conf->disks + number;
7086
7087 print_raid5_conf(conf);
f6b6ec5c
SL
7088 if (test_bit(Journal, &rdev->flags) && conf->log) {
7089 struct r5l_log *log;
c2bb6242 7090 /*
f6b6ec5c
SL
7091 * we can't wait pending write here, as this is called in
7092 * raid5d, wait will deadlock.
c2bb6242 7093 */
f6b6ec5c
SL
7094 if (atomic_read(&mddev->writes_pending))
7095 return -EBUSY;
7096 log = conf->log;
7097 conf->log = NULL;
7098 synchronize_rcu();
7099 r5l_exit_log(log);
7100 return 0;
c2bb6242 7101 }
657e3e4d
N
7102 if (rdev == p->rdev)
7103 rdevp = &p->rdev;
7104 else if (rdev == p->replacement)
7105 rdevp = &p->replacement;
7106 else
7107 return 0;
7108
7109 if (number >= conf->raid_disks &&
7110 conf->reshape_progress == MaxSector)
7111 clear_bit(In_sync, &rdev->flags);
7112
7113 if (test_bit(In_sync, &rdev->flags) ||
7114 atomic_read(&rdev->nr_pending)) {
7115 err = -EBUSY;
7116 goto abort;
7117 }
7118 /* Only remove non-faulty devices if recovery
7119 * isn't possible.
7120 */
7121 if (!test_bit(Faulty, &rdev->flags) &&
7122 mddev->recovery_disabled != conf->recovery_disabled &&
7123 !has_failed(conf) &&
dd054fce 7124 (!p->replacement || p->replacement == rdev) &&
657e3e4d
N
7125 number < conf->raid_disks) {
7126 err = -EBUSY;
7127 goto abort;
7128 }
7129 *rdevp = NULL;
d787be40
N
7130 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
7131 synchronize_rcu();
7132 if (atomic_read(&rdev->nr_pending)) {
7133 /* lost the race, try later */
7134 err = -EBUSY;
7135 *rdevp = rdev;
7136 }
7137 }
7138 if (p->replacement) {
dd054fce
N
7139 /* We must have just cleared 'rdev' */
7140 p->rdev = p->replacement;
7141 clear_bit(Replacement, &p->replacement->flags);
7142 smp_mb(); /* Make sure other CPUs may see both as identical
7143 * but will never see neither - if they are careful
7144 */
7145 p->replacement = NULL;
7146 clear_bit(WantReplacement, &rdev->flags);
7147 } else
7148 /* We might have just removed the Replacement as faulty-
7149 * clear the bit just in case
7150 */
7151 clear_bit(WantReplacement, &rdev->flags);
1da177e4
LT
7152abort:
7153
7154 print_raid5_conf(conf);
7155 return err;
7156}
7157
fd01b88c 7158static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 7159{
d1688a6d 7160 struct r5conf *conf = mddev->private;
199050ea 7161 int err = -EEXIST;
1da177e4
LT
7162 int disk;
7163 struct disk_info *p;
6c2fce2e
NB
7164 int first = 0;
7165 int last = conf->raid_disks - 1;
1da177e4 7166
f6b6ec5c
SL
7167 if (test_bit(Journal, &rdev->flags)) {
7168 char b[BDEVNAME_SIZE];
7169 if (conf->log)
7170 return -EBUSY;
7171
7172 rdev->raid_disk = 0;
7173 /*
7174 * The array is in readonly mode if journal is missing, so no
7175 * write requests running. We should be safe
7176 */
7177 r5l_init_log(conf, rdev);
cc6167b4
N
7178 pr_debug("md/raid:%s: using device %s as journal\n",
7179 mdname(mddev), bdevname(rdev->bdev, b));
f6b6ec5c
SL
7180 return 0;
7181 }
7f0da59b
N
7182 if (mddev->recovery_disabled == conf->recovery_disabled)
7183 return -EBUSY;
7184
dc10c643 7185 if (rdev->saved_raid_disk < 0 && has_failed(conf))
1da177e4 7186 /* no point adding a device */
199050ea 7187 return -EINVAL;
1da177e4 7188
6c2fce2e
NB
7189 if (rdev->raid_disk >= 0)
7190 first = last = rdev->raid_disk;
1da177e4
LT
7191
7192 /*
16a53ecc
N
7193 * find the disk ... but prefer rdev->saved_raid_disk
7194 * if possible.
1da177e4 7195 */
16a53ecc 7196 if (rdev->saved_raid_disk >= 0 &&
6c2fce2e 7197 rdev->saved_raid_disk >= first &&
16a53ecc 7198 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5cfb22a1
N
7199 first = rdev->saved_raid_disk;
7200
7201 for (disk = first; disk <= last; disk++) {
7bfec5f3
N
7202 p = conf->disks + disk;
7203 if (p->rdev == NULL) {
b2d444d7 7204 clear_bit(In_sync, &rdev->flags);
1da177e4 7205 rdev->raid_disk = disk;
199050ea 7206 err = 0;
72626685
N
7207 if (rdev->saved_raid_disk != disk)
7208 conf->fullsync = 1;
d6065f7b 7209 rcu_assign_pointer(p->rdev, rdev);
5cfb22a1 7210 goto out;
1da177e4 7211 }
5cfb22a1
N
7212 }
7213 for (disk = first; disk <= last; disk++) {
7214 p = conf->disks + disk;
7bfec5f3
N
7215 if (test_bit(WantReplacement, &p->rdev->flags) &&
7216 p->replacement == NULL) {
7217 clear_bit(In_sync, &rdev->flags);
7218 set_bit(Replacement, &rdev->flags);
7219 rdev->raid_disk = disk;
7220 err = 0;
7221 conf->fullsync = 1;
7222 rcu_assign_pointer(p->replacement, rdev);
7223 break;
7224 }
7225 }
5cfb22a1 7226out:
1da177e4 7227 print_raid5_conf(conf);
199050ea 7228 return err;
1da177e4
LT
7229}
7230
fd01b88c 7231static int raid5_resize(struct mddev *mddev, sector_t sectors)
1da177e4
LT
7232{
7233 /* no resync is happening, and there is enough space
7234 * on all devices, so we can resize.
7235 * We need to make sure resync covers any new space.
7236 * If the array is shrinking we should possibly wait until
7237 * any io in the removed space completes, but it hardly seems
7238 * worth it.
7239 */
a4a6125a 7240 sector_t newsize;
3cb5edf4
N
7241 struct r5conf *conf = mddev->private;
7242
713cf5a6
SL
7243 if (conf->log)
7244 return -EINVAL;
3cb5edf4 7245 sectors &= ~((sector_t)conf->chunk_sectors - 1);
a4a6125a
N
7246 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
7247 if (mddev->external_size &&
7248 mddev->array_sectors > newsize)
b522adcd 7249 return -EINVAL;
a4a6125a
N
7250 if (mddev->bitmap) {
7251 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
7252 if (ret)
7253 return ret;
7254 }
7255 md_set_array_sectors(mddev, newsize);
f233ea5c 7256 set_capacity(mddev->gendisk, mddev->array_sectors);
449aad3e 7257 revalidate_disk(mddev->gendisk);
b098636c
N
7258 if (sectors > mddev->dev_sectors &&
7259 mddev->recovery_cp > mddev->dev_sectors) {
58c0fed4 7260 mddev->recovery_cp = mddev->dev_sectors;
1da177e4
LT
7261 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7262 }
58c0fed4 7263 mddev->dev_sectors = sectors;
4b5c7ae8 7264 mddev->resync_max_sectors = sectors;
1da177e4
LT
7265 return 0;
7266}
7267
fd01b88c 7268static int check_stripe_cache(struct mddev *mddev)
01ee22b4
N
7269{
7270 /* Can only proceed if there are plenty of stripe_heads.
7271 * We need a minimum of one full stripe,, and for sensible progress
7272 * it is best to have about 4 times that.
7273 * If we require 4 times, then the default 256 4K stripe_heads will
7274 * allow for chunk sizes up to 256K, which is probably OK.
7275 * If the chunk size is greater, user-space should request more
7276 * stripe_heads first.
7277 */
d1688a6d 7278 struct r5conf *conf = mddev->private;
01ee22b4 7279 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
edbe83ab 7280 > conf->min_nr_stripes ||
01ee22b4 7281 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
edbe83ab 7282 > conf->min_nr_stripes) {
cc6167b4
N
7283 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7284 mdname(mddev),
7285 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
7286 / STRIPE_SIZE)*4);
01ee22b4
N
7287 return 0;
7288 }
7289 return 1;
7290}
7291
fd01b88c 7292static int check_reshape(struct mddev *mddev)
29269553 7293{
d1688a6d 7294 struct r5conf *conf = mddev->private;
29269553 7295
713cf5a6
SL
7296 if (conf->log)
7297 return -EINVAL;
88ce4930
N
7298 if (mddev->delta_disks == 0 &&
7299 mddev->new_layout == mddev->layout &&
664e7c41 7300 mddev->new_chunk_sectors == mddev->chunk_sectors)
50ac168a 7301 return 0; /* nothing to do */
674806d6 7302 if (has_failed(conf))
ec32a2bd 7303 return -EINVAL;
fdcfbbb6 7304 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
ec32a2bd
N
7305 /* We might be able to shrink, but the devices must
7306 * be made bigger first.
7307 * For raid6, 4 is the minimum size.
7308 * Otherwise 2 is the minimum
7309 */
7310 int min = 2;
7311 if (mddev->level == 6)
7312 min = 4;
7313 if (mddev->raid_disks + mddev->delta_disks < min)
7314 return -EINVAL;
7315 }
29269553 7316
01ee22b4 7317 if (!check_stripe_cache(mddev))
29269553 7318 return -ENOSPC;
29269553 7319
738a2738
N
7320 if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
7321 mddev->delta_disks > 0)
7322 if (resize_chunks(conf,
7323 conf->previous_raid_disks
7324 + max(0, mddev->delta_disks),
7325 max(mddev->new_chunk_sectors,
7326 mddev->chunk_sectors)
7327 ) < 0)
7328 return -ENOMEM;
e56108d6
N
7329 return resize_stripes(conf, (conf->previous_raid_disks
7330 + mddev->delta_disks));
63c70c4f
N
7331}
7332
fd01b88c 7333static int raid5_start_reshape(struct mddev *mddev)
63c70c4f 7334{
d1688a6d 7335 struct r5conf *conf = mddev->private;
3cb03002 7336 struct md_rdev *rdev;
63c70c4f 7337 int spares = 0;
c04be0aa 7338 unsigned long flags;
63c70c4f 7339
f416885e 7340 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
63c70c4f
N
7341 return -EBUSY;
7342
01ee22b4
N
7343 if (!check_stripe_cache(mddev))
7344 return -ENOSPC;
7345
30b67645
N
7346 if (has_failed(conf))
7347 return -EINVAL;
7348
c6563a8c 7349 rdev_for_each(rdev, mddev) {
469518a3
N
7350 if (!test_bit(In_sync, &rdev->flags)
7351 && !test_bit(Faulty, &rdev->flags))
29269553 7352 spares++;
c6563a8c 7353 }
63c70c4f 7354
f416885e 7355 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
29269553
N
7356 /* Not enough devices even to make a degraded array
7357 * of that size
7358 */
7359 return -EINVAL;
7360
ec32a2bd
N
7361 /* Refuse to reduce size of the array. Any reductions in
7362 * array size must be through explicit setting of array_size
7363 * attribute.
7364 */
7365 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
7366 < mddev->array_sectors) {
cc6167b4
N
7367 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
7368 mdname(mddev));
ec32a2bd
N
7369 return -EINVAL;
7370 }
7371
f6705578 7372 atomic_set(&conf->reshape_stripes, 0);
29269553 7373 spin_lock_irq(&conf->device_lock);
c46501b2 7374 write_seqcount_begin(&conf->gen_lock);
29269553 7375 conf->previous_raid_disks = conf->raid_disks;
63c70c4f 7376 conf->raid_disks += mddev->delta_disks;
09c9e5fa
AN
7377 conf->prev_chunk_sectors = conf->chunk_sectors;
7378 conf->chunk_sectors = mddev->new_chunk_sectors;
88ce4930
N
7379 conf->prev_algo = conf->algorithm;
7380 conf->algorithm = mddev->new_layout;
05616be5
N
7381 conf->generation++;
7382 /* Code that selects data_offset needs to see the generation update
7383 * if reshape_progress has been set - so a memory barrier needed.
7384 */
7385 smp_mb();
2c810cdd 7386 if (mddev->reshape_backwards)
fef9c61f
N
7387 conf->reshape_progress = raid5_size(mddev, 0, 0);
7388 else
7389 conf->reshape_progress = 0;
7390 conf->reshape_safe = conf->reshape_progress;
c46501b2 7391 write_seqcount_end(&conf->gen_lock);
29269553
N
7392 spin_unlock_irq(&conf->device_lock);
7393
4d77e3ba
N
7394 /* Now make sure any requests that proceeded on the assumption
7395 * the reshape wasn't running - like Discard or Read - have
7396 * completed.
7397 */
7398 mddev_suspend(mddev);
7399 mddev_resume(mddev);
7400
29269553
N
7401 /* Add some new drives, as many as will fit.
7402 * We know there are enough to make the newly sized array work.
3424bf6a
N
7403 * Don't add devices if we are reducing the number of
7404 * devices in the array. This is because it is not possible
7405 * to correctly record the "partially reconstructed" state of
7406 * such devices during the reshape and confusion could result.
29269553 7407 */
87a8dec9 7408 if (mddev->delta_disks >= 0) {
dafb20fa 7409 rdev_for_each(rdev, mddev)
87a8dec9
N
7410 if (rdev->raid_disk < 0 &&
7411 !test_bit(Faulty, &rdev->flags)) {
7412 if (raid5_add_disk(mddev, rdev) == 0) {
87a8dec9 7413 if (rdev->raid_disk
9d4c7d87 7414 >= conf->previous_raid_disks)
87a8dec9 7415 set_bit(In_sync, &rdev->flags);
9d4c7d87 7416 else
87a8dec9 7417 rdev->recovery_offset = 0;
36fad858
NK
7418
7419 if (sysfs_link_rdev(mddev, rdev))
87a8dec9 7420 /* Failure here is OK */;
50da0840 7421 }
87a8dec9
N
7422 } else if (rdev->raid_disk >= conf->previous_raid_disks
7423 && !test_bit(Faulty, &rdev->flags)) {
7424 /* This is a spare that was manually added */
7425 set_bit(In_sync, &rdev->flags);
87a8dec9 7426 }
29269553 7427
87a8dec9
N
7428 /* When a reshape changes the number of devices,
7429 * ->degraded is measured against the larger of the
7430 * pre and post number of devices.
7431 */
ec32a2bd 7432 spin_lock_irqsave(&conf->device_lock, flags);
908f4fbd 7433 mddev->degraded = calc_degraded(conf);
ec32a2bd
N
7434 spin_unlock_irqrestore(&conf->device_lock, flags);
7435 }
63c70c4f 7436 mddev->raid_disks = conf->raid_disks;
e516402c 7437 mddev->reshape_position = conf->reshape_progress;
850b2b42 7438 set_bit(MD_CHANGE_DEVS, &mddev->flags);
f6705578 7439
29269553
N
7440 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7441 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
ea358cd0 7442 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
29269553
N
7443 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7444 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7445 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
0da3c619 7446 "reshape");
29269553
N
7447 if (!mddev->sync_thread) {
7448 mddev->recovery = 0;
7449 spin_lock_irq(&conf->device_lock);
ba8805b9 7450 write_seqcount_begin(&conf->gen_lock);
29269553 7451 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
ba8805b9
N
7452 mddev->new_chunk_sectors =
7453 conf->chunk_sectors = conf->prev_chunk_sectors;
7454 mddev->new_layout = conf->algorithm = conf->prev_algo;
05616be5
N
7455 rdev_for_each(rdev, mddev)
7456 rdev->new_data_offset = rdev->data_offset;
7457 smp_wmb();
ba8805b9 7458 conf->generation --;
fef9c61f 7459 conf->reshape_progress = MaxSector;
1e3fa9bd 7460 mddev->reshape_position = MaxSector;
ba8805b9 7461 write_seqcount_end(&conf->gen_lock);
29269553
N
7462 spin_unlock_irq(&conf->device_lock);
7463 return -EAGAIN;
7464 }
c8f517c4 7465 conf->reshape_checkpoint = jiffies;
29269553
N
7466 md_wakeup_thread(mddev->sync_thread);
7467 md_new_event(mddev);
7468 return 0;
7469}
29269553 7470
ec32a2bd
N
7471/* This is called from the reshape thread and should make any
7472 * changes needed in 'conf'
7473 */
d1688a6d 7474static void end_reshape(struct r5conf *conf)
29269553 7475{
29269553 7476
f6705578 7477 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
05616be5 7478 struct md_rdev *rdev;
f6705578 7479
f6705578 7480 spin_lock_irq(&conf->device_lock);
cea9c228 7481 conf->previous_raid_disks = conf->raid_disks;
05616be5
N
7482 rdev_for_each(rdev, conf->mddev)
7483 rdev->data_offset = rdev->new_data_offset;
7484 smp_wmb();
fef9c61f 7485 conf->reshape_progress = MaxSector;
6cbd8148 7486 conf->mddev->reshape_position = MaxSector;
f6705578 7487 spin_unlock_irq(&conf->device_lock);
b0f9ec04 7488 wake_up(&conf->wait_for_overlap);
16a53ecc
N
7489
7490 /* read-ahead size must cover two whole stripes, which is
7491 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
7492 */
4a5add49 7493 if (conf->mddev->queue) {
cea9c228 7494 int data_disks = conf->raid_disks - conf->max_degraded;
09c9e5fa 7495 int stripe = data_disks * ((conf->chunk_sectors << 9)
cea9c228 7496 / PAGE_SIZE);
16a53ecc
N
7497 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
7498 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
7499 }
29269553 7500 }
29269553
N
7501}
7502
ec32a2bd
N
7503/* This is called from the raid5d thread with mddev_lock held.
7504 * It makes config changes to the device.
7505 */
fd01b88c 7506static void raid5_finish_reshape(struct mddev *mddev)
cea9c228 7507{
d1688a6d 7508 struct r5conf *conf = mddev->private;
cea9c228
N
7509
7510 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7511
ec32a2bd
N
7512 if (mddev->delta_disks > 0) {
7513 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
fe67d19a
HM
7514 if (mddev->queue) {
7515 set_capacity(mddev->gendisk, mddev->array_sectors);
7516 revalidate_disk(mddev->gendisk);
7517 }
ec32a2bd
N
7518 } else {
7519 int d;
908f4fbd
N
7520 spin_lock_irq(&conf->device_lock);
7521 mddev->degraded = calc_degraded(conf);
7522 spin_unlock_irq(&conf->device_lock);
ec32a2bd
N
7523 for (d = conf->raid_disks ;
7524 d < conf->raid_disks - mddev->delta_disks;
1a67dde0 7525 d++) {
3cb03002 7526 struct md_rdev *rdev = conf->disks[d].rdev;
da7613b8
N
7527 if (rdev)
7528 clear_bit(In_sync, &rdev->flags);
7529 rdev = conf->disks[d].replacement;
7530 if (rdev)
7531 clear_bit(In_sync, &rdev->flags);
1a67dde0 7532 }
cea9c228 7533 }
88ce4930 7534 mddev->layout = conf->algorithm;
09c9e5fa 7535 mddev->chunk_sectors = conf->chunk_sectors;
ec32a2bd
N
7536 mddev->reshape_position = MaxSector;
7537 mddev->delta_disks = 0;
2c810cdd 7538 mddev->reshape_backwards = 0;
cea9c228
N
7539 }
7540}
7541
fd01b88c 7542static void raid5_quiesce(struct mddev *mddev, int state)
72626685 7543{
d1688a6d 7544 struct r5conf *conf = mddev->private;
72626685
N
7545
7546 switch(state) {
e464eafd
N
7547 case 2: /* resume for a suspend */
7548 wake_up(&conf->wait_for_overlap);
7549 break;
7550
72626685 7551 case 1: /* stop all writes */
566c09c5 7552 lock_all_device_hash_locks_irq(conf);
64bd660b
N
7553 /* '2' tells resync/reshape to pause so that all
7554 * active stripes can drain
7555 */
7556 conf->quiesce = 2;
b1b46486 7557 wait_event_cmd(conf->wait_for_quiescent,
46031f9a
RBJ
7558 atomic_read(&conf->active_stripes) == 0 &&
7559 atomic_read(&conf->active_aligned_reads) == 0,
566c09c5
SL
7560 unlock_all_device_hash_locks_irq(conf),
7561 lock_all_device_hash_locks_irq(conf));
64bd660b 7562 conf->quiesce = 1;
566c09c5 7563 unlock_all_device_hash_locks_irq(conf);
64bd660b
N
7564 /* allow reshape to continue */
7565 wake_up(&conf->wait_for_overlap);
72626685
N
7566 break;
7567
7568 case 0: /* re-enable writes */
566c09c5 7569 lock_all_device_hash_locks_irq(conf);
72626685 7570 conf->quiesce = 0;
b1b46486 7571 wake_up(&conf->wait_for_quiescent);
e464eafd 7572 wake_up(&conf->wait_for_overlap);
566c09c5 7573 unlock_all_device_hash_locks_irq(conf);
72626685
N
7574 break;
7575 }
e6c033f7 7576 r5l_quiesce(conf->log, state);
72626685 7577}
b15c2e57 7578
fd01b88c 7579static void *raid45_takeover_raid0(struct mddev *mddev, int level)
54071b38 7580{
e373ab10 7581 struct r0conf *raid0_conf = mddev->private;
d76c8420 7582 sector_t sectors;
54071b38 7583
f1b29bca 7584 /* for raid0 takeover only one zone is supported */
e373ab10 7585 if (raid0_conf->nr_strip_zones > 1) {
cc6167b4
N
7586 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
7587 mdname(mddev));
f1b29bca
DW
7588 return ERR_PTR(-EINVAL);
7589 }
7590
e373ab10
N
7591 sectors = raid0_conf->strip_zone[0].zone_end;
7592 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
3b71bd93 7593 mddev->dev_sectors = sectors;
f1b29bca 7594 mddev->new_level = level;
54071b38
TM
7595 mddev->new_layout = ALGORITHM_PARITY_N;
7596 mddev->new_chunk_sectors = mddev->chunk_sectors;
7597 mddev->raid_disks += 1;
7598 mddev->delta_disks = 1;
7599 /* make sure it will be not marked as dirty */
7600 mddev->recovery_cp = MaxSector;
7601
7602 return setup_conf(mddev);
7603}
7604
fd01b88c 7605static void *raid5_takeover_raid1(struct mddev *mddev)
d562b0c4
N
7606{
7607 int chunksect;
7608
7609 if (mddev->raid_disks != 2 ||
7610 mddev->degraded > 1)
7611 return ERR_PTR(-EINVAL);
7612
7613 /* Should check if there are write-behind devices? */
7614
7615 chunksect = 64*2; /* 64K by default */
7616
7617 /* The array must be an exact multiple of chunksize */
7618 while (chunksect && (mddev->array_sectors & (chunksect-1)))
7619 chunksect >>= 1;
7620
7621 if ((chunksect<<9) < STRIPE_SIZE)
7622 /* array size does not allow a suitable chunk size */
7623 return ERR_PTR(-EINVAL);
7624
7625 mddev->new_level = 5;
7626 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
664e7c41 7627 mddev->new_chunk_sectors = chunksect;
d562b0c4
N
7628
7629 return setup_conf(mddev);
7630}
7631
fd01b88c 7632static void *raid5_takeover_raid6(struct mddev *mddev)
fc9739c6
N
7633{
7634 int new_layout;
7635
7636 switch (mddev->layout) {
7637 case ALGORITHM_LEFT_ASYMMETRIC_6:
7638 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
7639 break;
7640 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7641 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
7642 break;
7643 case ALGORITHM_LEFT_SYMMETRIC_6:
7644 new_layout = ALGORITHM_LEFT_SYMMETRIC;
7645 break;
7646 case ALGORITHM_RIGHT_SYMMETRIC_6:
7647 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
7648 break;
7649 case ALGORITHM_PARITY_0_6:
7650 new_layout = ALGORITHM_PARITY_0;
7651 break;
7652 case ALGORITHM_PARITY_N:
7653 new_layout = ALGORITHM_PARITY_N;
7654 break;
7655 default:
7656 return ERR_PTR(-EINVAL);
7657 }
7658 mddev->new_level = 5;
7659 mddev->new_layout = new_layout;
7660 mddev->delta_disks = -1;
7661 mddev->raid_disks -= 1;
7662 return setup_conf(mddev);
7663}
7664
fd01b88c 7665static int raid5_check_reshape(struct mddev *mddev)
b3546035 7666{
88ce4930
N
7667 /* For a 2-drive array, the layout and chunk size can be changed
7668 * immediately as not restriping is needed.
7669 * For larger arrays we record the new value - after validation
7670 * to be used by a reshape pass.
b3546035 7671 */
d1688a6d 7672 struct r5conf *conf = mddev->private;
597a711b 7673 int new_chunk = mddev->new_chunk_sectors;
b3546035 7674
597a711b 7675 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
b3546035
N
7676 return -EINVAL;
7677 if (new_chunk > 0) {
0ba459d2 7678 if (!is_power_of_2(new_chunk))
b3546035 7679 return -EINVAL;
597a711b 7680 if (new_chunk < (PAGE_SIZE>>9))
b3546035 7681 return -EINVAL;
597a711b 7682 if (mddev->array_sectors & (new_chunk-1))
b3546035
N
7683 /* not factor of array size */
7684 return -EINVAL;
7685 }
7686
7687 /* They look valid */
7688
88ce4930 7689 if (mddev->raid_disks == 2) {
597a711b
N
7690 /* can make the change immediately */
7691 if (mddev->new_layout >= 0) {
7692 conf->algorithm = mddev->new_layout;
7693 mddev->layout = mddev->new_layout;
88ce4930
N
7694 }
7695 if (new_chunk > 0) {
597a711b
N
7696 conf->chunk_sectors = new_chunk ;
7697 mddev->chunk_sectors = new_chunk;
88ce4930
N
7698 }
7699 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7700 md_wakeup_thread(mddev->thread);
b3546035 7701 }
50ac168a 7702 return check_reshape(mddev);
88ce4930
N
7703}
7704
fd01b88c 7705static int raid6_check_reshape(struct mddev *mddev)
88ce4930 7706{
597a711b 7707 int new_chunk = mddev->new_chunk_sectors;
50ac168a 7708
597a711b 7709 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
88ce4930 7710 return -EINVAL;
b3546035 7711 if (new_chunk > 0) {
0ba459d2 7712 if (!is_power_of_2(new_chunk))
88ce4930 7713 return -EINVAL;
597a711b 7714 if (new_chunk < (PAGE_SIZE >> 9))
88ce4930 7715 return -EINVAL;
597a711b 7716 if (mddev->array_sectors & (new_chunk-1))
88ce4930
N
7717 /* not factor of array size */
7718 return -EINVAL;
b3546035 7719 }
88ce4930
N
7720
7721 /* They look valid */
50ac168a 7722 return check_reshape(mddev);
b3546035
N
7723}
7724
fd01b88c 7725static void *raid5_takeover(struct mddev *mddev)
d562b0c4
N
7726{
7727 /* raid5 can take over:
f1b29bca 7728 * raid0 - if there is only one strip zone - make it a raid4 layout
d562b0c4
N
7729 * raid1 - if there are two drives. We need to know the chunk size
7730 * raid4 - trivial - just use a raid4 layout.
7731 * raid6 - Providing it is a *_6 layout
d562b0c4 7732 */
f1b29bca
DW
7733 if (mddev->level == 0)
7734 return raid45_takeover_raid0(mddev, 5);
d562b0c4
N
7735 if (mddev->level == 1)
7736 return raid5_takeover_raid1(mddev);
e9d4758f
N
7737 if (mddev->level == 4) {
7738 mddev->new_layout = ALGORITHM_PARITY_N;
7739 mddev->new_level = 5;
7740 return setup_conf(mddev);
7741 }
fc9739c6
N
7742 if (mddev->level == 6)
7743 return raid5_takeover_raid6(mddev);
d562b0c4
N
7744
7745 return ERR_PTR(-EINVAL);
7746}
7747
fd01b88c 7748static void *raid4_takeover(struct mddev *mddev)
a78d38a1 7749{
f1b29bca
DW
7750 /* raid4 can take over:
7751 * raid0 - if there is only one strip zone
7752 * raid5 - if layout is right
a78d38a1 7753 */
f1b29bca
DW
7754 if (mddev->level == 0)
7755 return raid45_takeover_raid0(mddev, 4);
a78d38a1
N
7756 if (mddev->level == 5 &&
7757 mddev->layout == ALGORITHM_PARITY_N) {
7758 mddev->new_layout = 0;
7759 mddev->new_level = 4;
7760 return setup_conf(mddev);
7761 }
7762 return ERR_PTR(-EINVAL);
7763}
d562b0c4 7764
84fc4b56 7765static struct md_personality raid5_personality;
245f46c2 7766
fd01b88c 7767static void *raid6_takeover(struct mddev *mddev)
245f46c2
N
7768{
7769 /* Currently can only take over a raid5. We map the
7770 * personality to an equivalent raid6 personality
7771 * with the Q block at the end.
7772 */
7773 int new_layout;
7774
7775 if (mddev->pers != &raid5_personality)
7776 return ERR_PTR(-EINVAL);
7777 if (mddev->degraded > 1)
7778 return ERR_PTR(-EINVAL);
7779 if (mddev->raid_disks > 253)
7780 return ERR_PTR(-EINVAL);
7781 if (mddev->raid_disks < 3)
7782 return ERR_PTR(-EINVAL);
7783
7784 switch (mddev->layout) {
7785 case ALGORITHM_LEFT_ASYMMETRIC:
7786 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
7787 break;
7788 case ALGORITHM_RIGHT_ASYMMETRIC:
7789 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
7790 break;
7791 case ALGORITHM_LEFT_SYMMETRIC:
7792 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
7793 break;
7794 case ALGORITHM_RIGHT_SYMMETRIC:
7795 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
7796 break;
7797 case ALGORITHM_PARITY_0:
7798 new_layout = ALGORITHM_PARITY_0_6;
7799 break;
7800 case ALGORITHM_PARITY_N:
7801 new_layout = ALGORITHM_PARITY_N;
7802 break;
7803 default:
7804 return ERR_PTR(-EINVAL);
7805 }
7806 mddev->new_level = 6;
7807 mddev->new_layout = new_layout;
7808 mddev->delta_disks = 1;
7809 mddev->raid_disks += 1;
7810 return setup_conf(mddev);
7811}
7812
84fc4b56 7813static struct md_personality raid6_personality =
16a53ecc
N
7814{
7815 .name = "raid6",
7816 .level = 6,
7817 .owner = THIS_MODULE,
849674e4
SL
7818 .make_request = raid5_make_request,
7819 .run = raid5_run,
afa0f557 7820 .free = raid5_free,
849674e4
SL
7821 .status = raid5_status,
7822 .error_handler = raid5_error,
16a53ecc
N
7823 .hot_add_disk = raid5_add_disk,
7824 .hot_remove_disk= raid5_remove_disk,
7825 .spare_active = raid5_spare_active,
849674e4 7826 .sync_request = raid5_sync_request,
16a53ecc 7827 .resize = raid5_resize,
80c3a6ce 7828 .size = raid5_size,
50ac168a 7829 .check_reshape = raid6_check_reshape,
f416885e 7830 .start_reshape = raid5_start_reshape,
cea9c228 7831 .finish_reshape = raid5_finish_reshape,
16a53ecc 7832 .quiesce = raid5_quiesce,
245f46c2 7833 .takeover = raid6_takeover,
5c675f83 7834 .congested = raid5_congested,
16a53ecc 7835};
84fc4b56 7836static struct md_personality raid5_personality =
1da177e4
LT
7837{
7838 .name = "raid5",
2604b703 7839 .level = 5,
1da177e4 7840 .owner = THIS_MODULE,
849674e4
SL
7841 .make_request = raid5_make_request,
7842 .run = raid5_run,
afa0f557 7843 .free = raid5_free,
849674e4
SL
7844 .status = raid5_status,
7845 .error_handler = raid5_error,
1da177e4
LT
7846 .hot_add_disk = raid5_add_disk,
7847 .hot_remove_disk= raid5_remove_disk,
7848 .spare_active = raid5_spare_active,
849674e4 7849 .sync_request = raid5_sync_request,
1da177e4 7850 .resize = raid5_resize,
80c3a6ce 7851 .size = raid5_size,
63c70c4f
N
7852 .check_reshape = raid5_check_reshape,
7853 .start_reshape = raid5_start_reshape,
cea9c228 7854 .finish_reshape = raid5_finish_reshape,
72626685 7855 .quiesce = raid5_quiesce,
d562b0c4 7856 .takeover = raid5_takeover,
5c675f83 7857 .congested = raid5_congested,
1da177e4
LT
7858};
7859
84fc4b56 7860static struct md_personality raid4_personality =
1da177e4 7861{
2604b703
N
7862 .name = "raid4",
7863 .level = 4,
7864 .owner = THIS_MODULE,
849674e4
SL
7865 .make_request = raid5_make_request,
7866 .run = raid5_run,
afa0f557 7867 .free = raid5_free,
849674e4
SL
7868 .status = raid5_status,
7869 .error_handler = raid5_error,
2604b703
N
7870 .hot_add_disk = raid5_add_disk,
7871 .hot_remove_disk= raid5_remove_disk,
7872 .spare_active = raid5_spare_active,
849674e4 7873 .sync_request = raid5_sync_request,
2604b703 7874 .resize = raid5_resize,
80c3a6ce 7875 .size = raid5_size,
3d37890b
N
7876 .check_reshape = raid5_check_reshape,
7877 .start_reshape = raid5_start_reshape,
cea9c228 7878 .finish_reshape = raid5_finish_reshape,
2604b703 7879 .quiesce = raid5_quiesce,
a78d38a1 7880 .takeover = raid4_takeover,
5c675f83 7881 .congested = raid5_congested,
2604b703
N
7882};
7883
7884static int __init raid5_init(void)
7885{
29c6d1bb
SAS
7886 int ret;
7887
851c30c9
SL
7888 raid5_wq = alloc_workqueue("raid5wq",
7889 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
7890 if (!raid5_wq)
7891 return -ENOMEM;
29c6d1bb
SAS
7892
7893 ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
7894 "md/raid5:prepare",
7895 raid456_cpu_up_prepare,
7896 raid456_cpu_dead);
7897 if (ret) {
7898 destroy_workqueue(raid5_wq);
7899 return ret;
7900 }
16a53ecc 7901 register_md_personality(&raid6_personality);
2604b703
N
7902 register_md_personality(&raid5_personality);
7903 register_md_personality(&raid4_personality);
7904 return 0;
1da177e4
LT
7905}
7906
2604b703 7907static void raid5_exit(void)
1da177e4 7908{
16a53ecc 7909 unregister_md_personality(&raid6_personality);
2604b703
N
7910 unregister_md_personality(&raid5_personality);
7911 unregister_md_personality(&raid4_personality);
29c6d1bb 7912 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
851c30c9 7913 destroy_workqueue(raid5_wq);
1da177e4
LT
7914}
7915
7916module_init(raid5_init);
7917module_exit(raid5_exit);
7918MODULE_LICENSE("GPL");
0efb9e61 7919MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
1da177e4 7920MODULE_ALIAS("md-personality-4"); /* RAID5 */
d9d166c2
N
7921MODULE_ALIAS("md-raid5");
7922MODULE_ALIAS("md-raid4");
2604b703
N
7923MODULE_ALIAS("md-level-5");
7924MODULE_ALIAS("md-level-4");
16a53ecc
N
7925MODULE_ALIAS("md-personality-8"); /* RAID6 */
7926MODULE_ALIAS("md-raid6");
7927MODULE_ALIAS("md-level-6");
7928
7929/* This used to be two separate modules, they were: */
7930MODULE_ALIAS("raid5");
7931MODULE_ALIAS("raid6");