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