1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * raid5.c : Multiple Devices driver for Linux
4 *	   Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
5 *	   Copyright (C) 1999, 2000 Ingo Molnar
6 *	   Copyright (C) 2002, 2003 H. Peter Anvin
7 *
8 * RAID-4/5/6 management functions.
9 * Thanks to Penguin Computing for making the RAID-6 development possible
10 * by donating a test server!
11 */
12
13/*
14 * BITMAP UNPLUGGING:
15 *
16 * The sequencing for updating the bitmap reliably is a little
17 * subtle (and I got it wrong the first time) so it deserves some
18 * explanation.
19 *
20 * We group bitmap updates into batches.  Each batch has a number.
21 * We may write out several batches at once, but that isn't very important.
22 * conf->seq_write is the number of the last batch successfully written.
23 * conf->seq_flush is the number of the last batch that was closed to
24 *    new additions.
25 * When we discover that we will need to write to any block in a stripe
26 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
27 * the number of the batch it will be in. This is seq_flush+1.
28 * When we are ready to do a write, if that batch hasn't been written yet,
29 *   we plug the array and queue the stripe for later.
30 * When an unplug happens, we increment bm_flush, thus closing the current
31 *   batch.
32 * When we notice that bm_flush > bm_write, we write out all pending updates
33 * to the bitmap, and advance bm_write to where bm_flush was.
34 * This may occasionally write a bit out twice, but is sure never to
35 * miss any bits.
36 */
37
38#include <linux/blkdev.h>
39#include <linux/kthread.h>
40#include <linux/raid/pq.h>
41#include <linux/async_tx.h>
42#include <linux/module.h>
43#include <linux/async.h>
44#include <linux/seq_file.h>
45#include <linux/cpu.h>
46#include <linux/slab.h>
47#include <linux/ratelimit.h>
48#include <linux/nodemask.h>
49
50#include <trace/events/block.h>
51#include <linux/list_sort.h>
52
53#include "md.h"
54#include "raid5.h"
55#include "raid0.h"
56#include "md-bitmap.h"
57#include "raid5-log.h"
58
59#define UNSUPPORTED_MDDEV_FLAGS	(1L << MD_FAILFAST_SUPPORTED)
60
61#define cpu_to_group(cpu) cpu_to_node(cpu)
62#define ANY_GROUP NUMA_NO_NODE
63
64static bool devices_handle_discard_safely = false;
65module_param(devices_handle_discard_safely, bool, 0644);
66MODULE_PARM_DESC(devices_handle_discard_safely,
67		 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
68static struct workqueue_struct *raid5_wq;
69
70static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
71{
72	int hash = (sect >> RAID5_STRIPE_SHIFT(conf)) & HASH_MASK;
73	return &conf->stripe_hashtbl[hash];
74}
75
76static inline int stripe_hash_locks_hash(struct r5conf *conf, sector_t sect)
77{
78	return (sect >> RAID5_STRIPE_SHIFT(conf)) & STRIPE_HASH_LOCKS_MASK;
79}
80
81static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
82	__acquires(&conf->device_lock)
83{
84	spin_lock_irq(conf->hash_locks + hash);
85	spin_lock(&conf->device_lock);
86}
87
88static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
89	__releases(&conf->device_lock)
90{
91	spin_unlock(&conf->device_lock);
92	spin_unlock_irq(conf->hash_locks + hash);
93}
94
95static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
96	__acquires(&conf->device_lock)
97{
98	int i;
99	spin_lock_irq(conf->hash_locks);
100	for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
101		spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
102	spin_lock(&conf->device_lock);
103}
104
105static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
106	__releases(&conf->device_lock)
107{
108	int i;
109	spin_unlock(&conf->device_lock);
110	for (i = NR_STRIPE_HASH_LOCKS - 1; i; i--)
111		spin_unlock(conf->hash_locks + i);
112	spin_unlock_irq(conf->hash_locks);
113}
114
115/* Find first data disk in a raid6 stripe */
116static inline int raid6_d0(struct stripe_head *sh)
117{
118	if (sh->ddf_layout)
119		/* ddf always start from first device */
120		return 0;
121	/* md starts just after Q block */
122	if (sh->qd_idx == sh->disks - 1)
123		return 0;
124	else
125		return sh->qd_idx + 1;
126}
127static inline int raid6_next_disk(int disk, int raid_disks)
128{
129	disk++;
130	return (disk < raid_disks) ? disk : 0;
131}
132
133/* When walking through the disks in a raid5, starting at raid6_d0,
134 * We need to map each disk to a 'slot', where the data disks are slot
135 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
136 * is raid_disks-1.  This help does that mapping.
137 */
138static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
139			     int *count, int syndrome_disks)
140{
141	int slot = *count;
142
143	if (sh->ddf_layout)
144		(*count)++;
145	if (idx == sh->pd_idx)
146		return syndrome_disks;
147	if (idx == sh->qd_idx)
148		return syndrome_disks + 1;
149	if (!sh->ddf_layout)
150		(*count)++;
151	return slot;
152}
153
154static void print_raid5_conf (struct r5conf *conf);
155
156static int stripe_operations_active(struct stripe_head *sh)
157{
158	return sh->check_state || sh->reconstruct_state ||
159	       test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
160	       test_bit(STRIPE_COMPUTE_RUN, &sh->state);
161}
162
163static bool stripe_is_lowprio(struct stripe_head *sh)
164{
165	return (test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) ||
166		test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) &&
167	       !test_bit(STRIPE_R5C_CACHING, &sh->state);
168}
169
170static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
171	__must_hold(&sh->raid_conf->device_lock)
172{
173	struct r5conf *conf = sh->raid_conf;
174	struct r5worker_group *group;
175	int thread_cnt;
176	int i, cpu = sh->cpu;
177
178	if (!cpu_online(cpu)) {
179		cpu = cpumask_any(cpu_online_mask);
180		sh->cpu = cpu;
181	}
182
183	if (list_empty(&sh->lru)) {
184		struct r5worker_group *group;
185		group = conf->worker_groups + cpu_to_group(cpu);
186		if (stripe_is_lowprio(sh))
187			list_add_tail(&sh->lru, &group->loprio_list);
188		else
189			list_add_tail(&sh->lru, &group->handle_list);
190		group->stripes_cnt++;
191		sh->group = group;
192	}
193
194	if (conf->worker_cnt_per_group == 0) {
195		md_wakeup_thread(conf->mddev->thread);
196		return;
197	}
198
199	group = conf->worker_groups + cpu_to_group(sh->cpu);
200
201	group->workers[0].working = true;
202	/* at least one worker should run to avoid race */
203	queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
204
205	thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
206	/* wakeup more workers */
207	for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
208		if (group->workers[i].working == false) {
209			group->workers[i].working = true;
210			queue_work_on(sh->cpu, raid5_wq,
211				      &group->workers[i].work);
212			thread_cnt--;
213		}
214	}
215}
216
217static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
218			      struct list_head *temp_inactive_list)
219	__must_hold(&conf->device_lock)
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	 * In the following cases, the stripe cannot be released to cached
233	 * lists. Therefore, we make the stripe write out and set
234	 * STRIPE_HANDLE:
235	 *   1. when quiesce in r5c write back;
236	 *   2. when resync is requested fot the stripe.
237	 */
238	if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) ||
239	    (conf->quiesce && r5c_is_writeback(conf->log) &&
240	     !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0)) {
241		if (test_bit(STRIPE_R5C_CACHING, &sh->state))
242			r5c_make_stripe_write_out(sh);
243		set_bit(STRIPE_HANDLE, &sh->state);
244	}
245
246	if (test_bit(STRIPE_HANDLE, &sh->state)) {
247		if (test_bit(STRIPE_DELAYED, &sh->state) &&
248		    !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
249			list_add_tail(&sh->lru, &conf->delayed_list);
250		else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
251			   sh->bm_seq - conf->seq_write > 0)
252			list_add_tail(&sh->lru, &conf->bitmap_list);
253		else {
254			clear_bit(STRIPE_DELAYED, &sh->state);
255			clear_bit(STRIPE_BIT_DELAY, &sh->state);
256			if (conf->worker_cnt_per_group == 0) {
257				if (stripe_is_lowprio(sh))
258					list_add_tail(&sh->lru,
259							&conf->loprio_list);
260				else
261					list_add_tail(&sh->lru,
262							&conf->handle_list);
263			} else {
264				raid5_wakeup_stripe_thread(sh);
265				return;
266			}
267		}
268		md_wakeup_thread(conf->mddev->thread);
269	} else {
270		BUG_ON(stripe_operations_active(sh));
271		if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
272			if (atomic_dec_return(&conf->preread_active_stripes)
273			    < IO_THRESHOLD)
274				md_wakeup_thread(conf->mddev->thread);
275		atomic_dec(&conf->active_stripes);
276		if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
277			if (!r5c_is_writeback(conf->log))
278				list_add_tail(&sh->lru, temp_inactive_list);
279			else {
280				WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags));
281				if (injournal == 0)
282					list_add_tail(&sh->lru, temp_inactive_list);
283				else if (injournal == conf->raid_disks - conf->max_degraded) {
284					/* full stripe */
285					if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state))
286						atomic_inc(&conf->r5c_cached_full_stripes);
287					if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state))
288						atomic_dec(&conf->r5c_cached_partial_stripes);
289					list_add_tail(&sh->lru, &conf->r5c_full_stripe_list);
290					r5c_check_cached_full_stripe(conf);
291				} else
292					/*
293					 * STRIPE_R5C_PARTIAL_STRIPE is set in
294					 * r5c_try_caching_write(). No need to
295					 * set it again.
296					 */
297					list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list);
298			}
299		}
300	}
301}
302
303static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
304			     struct list_head *temp_inactive_list)
305	__must_hold(&conf->device_lock)
306{
307	if (atomic_dec_and_test(&sh->count))
308		do_release_stripe(conf, sh, temp_inactive_list);
309}
310
311/*
312 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
313 *
314 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
315 * given time. Adding stripes only takes device lock, while deleting stripes
316 * only takes hash lock.
317 */
318static void release_inactive_stripe_list(struct r5conf *conf,
319					 struct list_head *temp_inactive_list,
320					 int hash)
321{
322	int size;
323	bool do_wakeup = false;
324	unsigned long flags;
325
326	if (hash == NR_STRIPE_HASH_LOCKS) {
327		size = NR_STRIPE_HASH_LOCKS;
328		hash = NR_STRIPE_HASH_LOCKS - 1;
329	} else
330		size = 1;
331	while (size) {
332		struct list_head *list = &temp_inactive_list[size - 1];
333
334		/*
335		 * We don't hold any lock here yet, raid5_get_active_stripe() might
336		 * remove stripes from the list
337		 */
338		if (!list_empty_careful(list)) {
339			spin_lock_irqsave(conf->hash_locks + hash, flags);
340			if (list_empty(conf->inactive_list + hash) &&
341			    !list_empty(list))
342				atomic_dec(&conf->empty_inactive_list_nr);
343			list_splice_tail_init(list, conf->inactive_list + hash);
344			do_wakeup = true;
345			spin_unlock_irqrestore(conf->hash_locks + hash, flags);
346		}
347		size--;
348		hash--;
349	}
350
351	if (do_wakeup) {
352		wake_up(&conf->wait_for_stripe);
353		if (atomic_read(&conf->active_stripes) == 0)
354			wake_up(&conf->wait_for_quiescent);
355		if (conf->retry_read_aligned)
356			md_wakeup_thread(conf->mddev->thread);
357	}
358}
359
360static int release_stripe_list(struct r5conf *conf,
361			       struct list_head *temp_inactive_list)
362	__must_hold(&conf->device_lock)
363{
364	struct stripe_head *sh, *t;
365	int count = 0;
366	struct llist_node *head;
367
368	head = llist_del_all(&conf->released_stripes);
369	head = llist_reverse_order(head);
370	llist_for_each_entry_safe(sh, t, head, release_list) {
371		int hash;
372
373		/* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
374		smp_mb();
375		clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
376		/*
377		 * Don't worry the bit is set here, because if the bit is set
378		 * again, the count is always > 1. This is true for
379		 * STRIPE_ON_UNPLUG_LIST bit too.
380		 */
381		hash = sh->hash_lock_index;
382		__release_stripe(conf, sh, &temp_inactive_list[hash]);
383		count++;
384	}
385
386	return count;
387}
388
389void raid5_release_stripe(struct stripe_head *sh)
390{
391	struct r5conf *conf = sh->raid_conf;
392	unsigned long flags;
393	struct list_head list;
394	int hash;
395	bool wakeup;
396
397	/* Avoid release_list until the last reference.
398	 */
399	if (atomic_add_unless(&sh->count, -1, 1))
400		return;
401
402	if (unlikely(!conf->mddev->thread) ||
403		test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
404		goto slow_path;
405	wakeup = llist_add(&sh->release_list, &conf->released_stripes);
406	if (wakeup)
407		md_wakeup_thread(conf->mddev->thread);
408	return;
409slow_path:
410	/* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
411	if (atomic_dec_and_lock_irqsave(&sh->count, &conf->device_lock, flags)) {
412		INIT_LIST_HEAD(&list);
413		hash = sh->hash_lock_index;
414		do_release_stripe(conf, sh, &list);
415		spin_unlock_irqrestore(&conf->device_lock, flags);
416		release_inactive_stripe_list(conf, &list, hash);
417	}
418}
419
420static inline void remove_hash(struct stripe_head *sh)
421{
422	pr_debug("remove_hash(), stripe %llu\n",
423		(unsigned long long)sh->sector);
424
425	hlist_del_init(&sh->hash);
426}
427
428static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
429{
430	struct hlist_head *hp = stripe_hash(conf, sh->sector);
431
432	pr_debug("insert_hash(), stripe %llu\n",
433		(unsigned long long)sh->sector);
434
435	hlist_add_head(&sh->hash, hp);
436}
437
438/* find an idle stripe, make sure it is unhashed, and return it. */
439static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
440{
441	struct stripe_head *sh = NULL;
442	struct list_head *first;
443
444	if (list_empty(conf->inactive_list + hash))
445		goto out;
446	first = (conf->inactive_list + hash)->next;
447	sh = list_entry(first, struct stripe_head, lru);
448	list_del_init(first);
449	remove_hash(sh);
450	atomic_inc(&conf->active_stripes);
451	BUG_ON(hash != sh->hash_lock_index);
452	if (list_empty(conf->inactive_list + hash))
453		atomic_inc(&conf->empty_inactive_list_nr);
454out:
455	return sh;
456}
457
458#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
459static void free_stripe_pages(struct stripe_head *sh)
460{
461	int i;
462	struct page *p;
463
464	/* Have not allocate page pool */
465	if (!sh->pages)
466		return;
467
468	for (i = 0; i < sh->nr_pages; i++) {
469		p = sh->pages[i];
470		if (p)
471			put_page(p);
472		sh->pages[i] = NULL;
473	}
474}
475
476static int alloc_stripe_pages(struct stripe_head *sh, gfp_t gfp)
477{
478	int i;
479	struct page *p;
480
481	for (i = 0; i < sh->nr_pages; i++) {
482		/* The page have allocated. */
483		if (sh->pages[i])
484			continue;
485
486		p = alloc_page(gfp);
487		if (!p) {
488			free_stripe_pages(sh);
489			return -ENOMEM;
490		}
491		sh->pages[i] = p;
492	}
493	return 0;
494}
495
496static int
497init_stripe_shared_pages(struct stripe_head *sh, struct r5conf *conf, int disks)
498{
499	int nr_pages, cnt;
500
501	if (sh->pages)
502		return 0;
503
504	/* Each of the sh->dev[i] need one conf->stripe_size */
505	cnt = PAGE_SIZE / conf->stripe_size;
506	nr_pages = (disks + cnt - 1) / cnt;
507
508	sh->pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
509	if (!sh->pages)
510		return -ENOMEM;
511	sh->nr_pages = nr_pages;
512	sh->stripes_per_page = cnt;
513	return 0;
514}
515#endif
516
517static void shrink_buffers(struct stripe_head *sh)
518{
519	int i;
520	int num = sh->raid_conf->pool_size;
521
522#if PAGE_SIZE == DEFAULT_STRIPE_SIZE
523	for (i = 0; i < num ; i++) {
524		struct page *p;
525
526		WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
527		p = sh->dev[i].page;
528		if (!p)
529			continue;
530		sh->dev[i].page = NULL;
531		put_page(p);
532	}
533#else
534	for (i = 0; i < num; i++)
535		sh->dev[i].page = NULL;
536	free_stripe_pages(sh); /* Free pages */
537#endif
538}
539
540static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
541{
542	int i;
543	int num = sh->raid_conf->pool_size;
544
545#if PAGE_SIZE == DEFAULT_STRIPE_SIZE
546	for (i = 0; i < num; i++) {
547		struct page *page;
548
549		if (!(page = alloc_page(gfp))) {
550			return 1;
551		}
552		sh->dev[i].page = page;
553		sh->dev[i].orig_page = page;
554		sh->dev[i].offset = 0;
555	}
556#else
557	if (alloc_stripe_pages(sh, gfp))
558		return -ENOMEM;
559
560	for (i = 0; i < num; i++) {
561		sh->dev[i].page = raid5_get_dev_page(sh, i);
562		sh->dev[i].orig_page = sh->dev[i].page;
563		sh->dev[i].offset = raid5_get_page_offset(sh, i);
564	}
565#endif
566	return 0;
567}
568
569static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
570			    struct stripe_head *sh);
571
572static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
573{
574	struct r5conf *conf = sh->raid_conf;
575	int i, seq;
576
577	BUG_ON(atomic_read(&sh->count) != 0);
578	BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
579	BUG_ON(stripe_operations_active(sh));
580	BUG_ON(sh->batch_head);
581
582	pr_debug("init_stripe called, stripe %llu\n",
583		(unsigned long long)sector);
584retry:
585	seq = read_seqcount_begin(&conf->gen_lock);
586	sh->generation = conf->generation - previous;
587	sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
588	sh->sector = sector;
589	stripe_set_idx(sector, conf, previous, sh);
590	sh->state = 0;
591
592	for (i = sh->disks; i--; ) {
593		struct r5dev *dev = &sh->dev[i];
594
595		if (dev->toread || dev->read || dev->towrite || dev->written ||
596		    test_bit(R5_LOCKED, &dev->flags)) {
597			pr_err("sector=%llx i=%d %p %p %p %p %d\n",
598			       (unsigned long long)sh->sector, i, dev->toread,
599			       dev->read, dev->towrite, dev->written,
600			       test_bit(R5_LOCKED, &dev->flags));
601			WARN_ON(1);
602		}
603		dev->flags = 0;
604		dev->sector = raid5_compute_blocknr(sh, i, previous);
605	}
606	if (read_seqcount_retry(&conf->gen_lock, seq))
607		goto retry;
608	sh->overwrite_disks = 0;
609	insert_hash(conf, sh);
610	sh->cpu = smp_processor_id();
611	set_bit(STRIPE_BATCH_READY, &sh->state);
612}
613
614static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
615					 short generation)
616{
617	struct stripe_head *sh;
618
619	pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
620	hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
621		if (sh->sector == sector && sh->generation == generation)
622			return sh;
623	pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
624	return NULL;
625}
626
627/*
628 * Need to check if array has failed when deciding whether to:
629 *  - start an array
630 *  - remove non-faulty devices
631 *  - add a spare
632 *  - allow a reshape
633 * This determination is simple when no reshape is happening.
634 * However if there is a reshape, we need to carefully check
635 * both the before and after sections.
636 * This is because some failed devices may only affect one
637 * of the two sections, and some non-in_sync devices may
638 * be insync in the section most affected by failed devices.
639 *
640 * Most calls to this function hold &conf->device_lock. Calls
641 * in raid5_run() do not require the lock as no other threads
642 * have been started yet.
643 */
644int raid5_calc_degraded(struct r5conf *conf)
645{
646	int degraded, degraded2;
647	int i;
648
649	rcu_read_lock();
650	degraded = 0;
651	for (i = 0; i < conf->previous_raid_disks; i++) {
652		struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
653		if (rdev && test_bit(Faulty, &rdev->flags))
654			rdev = rcu_dereference(conf->disks[i].replacement);
655		if (!rdev || test_bit(Faulty, &rdev->flags))
656			degraded++;
657		else if (test_bit(In_sync, &rdev->flags))
658			;
659		else
660			/* not in-sync or faulty.
661			 * If the reshape increases the number of devices,
662			 * this is being recovered by the reshape, so
663			 * this 'previous' section is not in_sync.
664			 * If the number of devices is being reduced however,
665			 * the device can only be part of the array if
666			 * we are reverting a reshape, so this section will
667			 * be in-sync.
668			 */
669			if (conf->raid_disks >= conf->previous_raid_disks)
670				degraded++;
671	}
672	rcu_read_unlock();
673	if (conf->raid_disks == conf->previous_raid_disks)
674		return degraded;
675	rcu_read_lock();
676	degraded2 = 0;
677	for (i = 0; i < conf->raid_disks; i++) {
678		struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
679		if (rdev && test_bit(Faulty, &rdev->flags))
680			rdev = rcu_dereference(conf->disks[i].replacement);
681		if (!rdev || test_bit(Faulty, &rdev->flags))
682			degraded2++;
683		else if (test_bit(In_sync, &rdev->flags))
684			;
685		else
686			/* not in-sync or faulty.
687			 * If reshape increases the number of devices, this
688			 * section has already been recovered, else it
689			 * almost certainly hasn't.
690			 */
691			if (conf->raid_disks <= conf->previous_raid_disks)
692				degraded2++;
693	}
694	rcu_read_unlock();
695	if (degraded2 > degraded)
696		return degraded2;
697	return degraded;
698}
699
700static bool has_failed(struct r5conf *conf)
701{
702	int degraded = conf->mddev->degraded;
703
704	if (test_bit(MD_BROKEN, &conf->mddev->flags))
705		return true;
706
707	if (conf->mddev->reshape_position != MaxSector)
708		degraded = raid5_calc_degraded(conf);
709
710	return degraded > conf->max_degraded;
711}
712
713struct stripe_head *
714raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
715			int previous, int noblock, int noquiesce)
716{
717	struct stripe_head *sh;
718	int hash = stripe_hash_locks_hash(conf, sector);
719	int inc_empty_inactive_list_flag;
720
721	pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
722
723	spin_lock_irq(conf->hash_locks + hash);
724
725	do {
726		wait_event_lock_irq(conf->wait_for_quiescent,
727				    conf->quiesce == 0 || noquiesce,
728				    *(conf->hash_locks + hash));
729		sh = __find_stripe(conf, sector, conf->generation - previous);
730		if (!sh) {
731			if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
732				sh = get_free_stripe(conf, hash);
733				if (!sh && !test_bit(R5_DID_ALLOC,
734						     &conf->cache_state))
735					set_bit(R5_ALLOC_MORE,
736						&conf->cache_state);
737			}
738			if (noblock && sh == NULL)
739				break;
740
741			r5c_check_stripe_cache_usage(conf);
742			if (!sh) {
743				set_bit(R5_INACTIVE_BLOCKED,
744					&conf->cache_state);
745				r5l_wake_reclaim(conf->log, 0);
746				wait_event_lock_irq(
747					conf->wait_for_stripe,
748					!list_empty(conf->inactive_list + hash) &&
749					(atomic_read(&conf->active_stripes)
750					 < (conf->max_nr_stripes * 3 / 4)
751					 || !test_bit(R5_INACTIVE_BLOCKED,
752						      &conf->cache_state)),
753					*(conf->hash_locks + hash));
754				clear_bit(R5_INACTIVE_BLOCKED,
755					  &conf->cache_state);
756			} else {
757				init_stripe(sh, sector, previous);
758				atomic_inc(&sh->count);
759			}
760		} else if (!atomic_inc_not_zero(&sh->count)) {
761			spin_lock(&conf->device_lock);
762			if (!atomic_read(&sh->count)) {
763				if (!test_bit(STRIPE_HANDLE, &sh->state))
764					atomic_inc(&conf->active_stripes);
765				BUG_ON(list_empty(&sh->lru) &&
766				       !test_bit(STRIPE_EXPANDING, &sh->state));
767				inc_empty_inactive_list_flag = 0;
768				if (!list_empty(conf->inactive_list + hash))
769					inc_empty_inactive_list_flag = 1;
770				list_del_init(&sh->lru);
771				if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
772					atomic_inc(&conf->empty_inactive_list_nr);
773				if (sh->group) {
774					sh->group->stripes_cnt--;
775					sh->group = NULL;
776				}
777			}
778			atomic_inc(&sh->count);
779			spin_unlock(&conf->device_lock);
780		}
781	} while (sh == NULL);
782
783	spin_unlock_irq(conf->hash_locks + hash);
784	return sh;
785}
786
787static bool is_full_stripe_write(struct stripe_head *sh)
788{
789	BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
790	return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
791}
792
793static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
794		__acquires(&sh1->stripe_lock)
795		__acquires(&sh2->stripe_lock)
796{
797	if (sh1 > sh2) {
798		spin_lock_irq(&sh2->stripe_lock);
799		spin_lock_nested(&sh1->stripe_lock, 1);
800	} else {
801		spin_lock_irq(&sh1->stripe_lock);
802		spin_lock_nested(&sh2->stripe_lock, 1);
803	}
804}
805
806static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
807		__releases(&sh1->stripe_lock)
808		__releases(&sh2->stripe_lock)
809{
810	spin_unlock(&sh1->stripe_lock);
811	spin_unlock_irq(&sh2->stripe_lock);
812}
813
814/* Only freshly new full stripe normal write stripe can be added to a batch list */
815static bool stripe_can_batch(struct stripe_head *sh)
816{
817	struct r5conf *conf = sh->raid_conf;
818
819	if (raid5_has_log(conf) || raid5_has_ppl(conf))
820		return false;
821	return test_bit(STRIPE_BATCH_READY, &sh->state) &&
822		!test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
823		is_full_stripe_write(sh);
824}
825
826/* we only do back search */
827static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
828{
829	struct stripe_head *head;
830	sector_t head_sector, tmp_sec;
831	int hash;
832	int dd_idx;
833	int inc_empty_inactive_list_flag;
834
835	/* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
836	tmp_sec = sh->sector;
837	if (!sector_div(tmp_sec, conf->chunk_sectors))
838		return;
839	head_sector = sh->sector - RAID5_STRIPE_SECTORS(conf);
840
841	hash = stripe_hash_locks_hash(conf, head_sector);
842	spin_lock_irq(conf->hash_locks + hash);
843	head = __find_stripe(conf, head_sector, conf->generation);
844	if (head && !atomic_inc_not_zero(&head->count)) {
845		spin_lock(&conf->device_lock);
846		if (!atomic_read(&head->count)) {
847			if (!test_bit(STRIPE_HANDLE, &head->state))
848				atomic_inc(&conf->active_stripes);
849			BUG_ON(list_empty(&head->lru) &&
850			       !test_bit(STRIPE_EXPANDING, &head->state));
851			inc_empty_inactive_list_flag = 0;
852			if (!list_empty(conf->inactive_list + hash))
853				inc_empty_inactive_list_flag = 1;
854			list_del_init(&head->lru);
855			if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
856				atomic_inc(&conf->empty_inactive_list_nr);
857			if (head->group) {
858				head->group->stripes_cnt--;
859				head->group = NULL;
860			}
861		}
862		atomic_inc(&head->count);
863		spin_unlock(&conf->device_lock);
864	}
865	spin_unlock_irq(conf->hash_locks + hash);
866
867	if (!head)
868		return;
869	if (!stripe_can_batch(head))
870		goto out;
871
872	lock_two_stripes(head, sh);
873	/* clear_batch_ready clear the flag */
874	if (!stripe_can_batch(head) || !stripe_can_batch(sh))
875		goto unlock_out;
876
877	if (sh->batch_head)
878		goto unlock_out;
879
880	dd_idx = 0;
881	while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
882		dd_idx++;
883	if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
884	    bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
885		goto unlock_out;
886
887	if (head->batch_head) {
888		spin_lock(&head->batch_head->batch_lock);
889		/* This batch list is already running */
890		if (!stripe_can_batch(head)) {
891			spin_unlock(&head->batch_head->batch_lock);
892			goto unlock_out;
893		}
894		/*
895		 * We must assign batch_head of this stripe within the
896		 * batch_lock, otherwise clear_batch_ready of batch head
897		 * stripe could clear BATCH_READY bit of this stripe and
898		 * this stripe->batch_head doesn't get assigned, which
899		 * could confuse clear_batch_ready for this stripe
900		 */
901		sh->batch_head = head->batch_head;
902
903		/*
904		 * at this point, head's BATCH_READY could be cleared, but we
905		 * can still add the stripe to batch list
906		 */
907		list_add(&sh->batch_list, &head->batch_list);
908		spin_unlock(&head->batch_head->batch_lock);
909	} else {
910		head->batch_head = head;
911		sh->batch_head = head->batch_head;
912		spin_lock(&head->batch_lock);
913		list_add_tail(&sh->batch_list, &head->batch_list);
914		spin_unlock(&head->batch_lock);
915	}
916
917	if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
918		if (atomic_dec_return(&conf->preread_active_stripes)
919		    < IO_THRESHOLD)
920			md_wakeup_thread(conf->mddev->thread);
921
922	if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
923		int seq = sh->bm_seq;
924		if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
925		    sh->batch_head->bm_seq > seq)
926			seq = sh->batch_head->bm_seq;
927		set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
928		sh->batch_head->bm_seq = seq;
929	}
930
931	atomic_inc(&sh->count);
932unlock_out:
933	unlock_two_stripes(head, sh);
934out:
935	raid5_release_stripe(head);
936}
937
938/* Determine if 'data_offset' or 'new_data_offset' should be used
939 * in this stripe_head.
940 */
941static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
942{
943	sector_t progress = conf->reshape_progress;
944	/* Need a memory barrier to make sure we see the value
945	 * of conf->generation, or ->data_offset that was set before
946	 * reshape_progress was updated.
947	 */
948	smp_rmb();
949	if (progress == MaxSector)
950		return 0;
951	if (sh->generation == conf->generation - 1)
952		return 0;
953	/* We are in a reshape, and this is a new-generation stripe,
954	 * so use new_data_offset.
955	 */
956	return 1;
957}
958
959static void dispatch_bio_list(struct bio_list *tmp)
960{
961	struct bio *bio;
962
963	while ((bio = bio_list_pop(tmp)))
964		submit_bio_noacct(bio);
965}
966
967static int cmp_stripe(void *priv, const struct list_head *a,
968		      const struct list_head *b)
969{
970	const struct r5pending_data *da = list_entry(a,
971				struct r5pending_data, sibling);
972	const struct r5pending_data *db = list_entry(b,
973				struct r5pending_data, sibling);
974	if (da->sector > db->sector)
975		return 1;
976	if (da->sector < db->sector)
977		return -1;
978	return 0;
979}
980
981static void dispatch_defer_bios(struct r5conf *conf, int target,
982				struct bio_list *list)
983{
984	struct r5pending_data *data;
985	struct list_head *first, *next = NULL;
986	int cnt = 0;
987
988	if (conf->pending_data_cnt == 0)
989		return;
990
991	list_sort(NULL, &conf->pending_list, cmp_stripe);
992
993	first = conf->pending_list.next;
994
995	/* temporarily move the head */
996	if (conf->next_pending_data)
997		list_move_tail(&conf->pending_list,
998				&conf->next_pending_data->sibling);
999
1000	while (!list_empty(&conf->pending_list)) {
1001		data = list_first_entry(&conf->pending_list,
1002			struct r5pending_data, sibling);
1003		if (&data->sibling == first)
1004			first = data->sibling.next;
1005		next = data->sibling.next;
1006
1007		bio_list_merge(list, &data->bios);
1008		list_move(&data->sibling, &conf->free_list);
1009		cnt++;
1010		if (cnt >= target)
1011			break;
1012	}
1013	conf->pending_data_cnt -= cnt;
1014	BUG_ON(conf->pending_data_cnt < 0 || cnt < target);
1015
1016	if (next != &conf->pending_list)
1017		conf->next_pending_data = list_entry(next,
1018				struct r5pending_data, sibling);
1019	else
1020		conf->next_pending_data = NULL;
1021	/* list isn't empty */
1022	if (first != &conf->pending_list)
1023		list_move_tail(&conf->pending_list, first);
1024}
1025
1026static void flush_deferred_bios(struct r5conf *conf)
1027{
1028	struct bio_list tmp = BIO_EMPTY_LIST;
1029
1030	if (conf->pending_data_cnt == 0)
1031		return;
1032
1033	spin_lock(&conf->pending_bios_lock);
1034	dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp);
1035	BUG_ON(conf->pending_data_cnt != 0);
1036	spin_unlock(&conf->pending_bios_lock);
1037
1038	dispatch_bio_list(&tmp);
1039}
1040
1041static void defer_issue_bios(struct r5conf *conf, sector_t sector,
1042				struct bio_list *bios)
1043{
1044	struct bio_list tmp = BIO_EMPTY_LIST;
1045	struct r5pending_data *ent;
1046
1047	spin_lock(&conf->pending_bios_lock);
1048	ent = list_first_entry(&conf->free_list, struct r5pending_data,
1049							sibling);
1050	list_move_tail(&ent->sibling, &conf->pending_list);
1051	ent->sector = sector;
1052	bio_list_init(&ent->bios);
1053	bio_list_merge(&ent->bios, bios);
1054	conf->pending_data_cnt++;
1055	if (conf->pending_data_cnt >= PENDING_IO_MAX)
1056		dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp);
1057
1058	spin_unlock(&conf->pending_bios_lock);
1059
1060	dispatch_bio_list(&tmp);
1061}
1062
1063static void
1064raid5_end_read_request(struct bio *bi);
1065static void
1066raid5_end_write_request(struct bio *bi);
1067
1068static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
1069{
1070	struct r5conf *conf = sh->raid_conf;
1071	int i, disks = sh->disks;
1072	struct stripe_head *head_sh = sh;
1073	struct bio_list pending_bios = BIO_EMPTY_LIST;
1074	struct r5dev *dev;
1075	bool should_defer;
1076
1077	might_sleep();
1078
1079	if (log_stripe(sh, s) == 0)
1080		return;
1081
1082	should_defer = conf->batch_bio_dispatch && conf->group_cnt;
1083
1084	for (i = disks; i--; ) {
1085		int op, op_flags = 0;
1086		int replace_only = 0;
1087		struct bio *bi, *rbi;
1088		struct md_rdev *rdev, *rrdev = NULL;
1089
1090		sh = head_sh;
1091		if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
1092			op = REQ_OP_WRITE;
1093			if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
1094				op_flags = REQ_FUA;
1095			if (test_bit(R5_Discard, &sh->dev[i].flags))
1096				op = REQ_OP_DISCARD;
1097		} else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1098			op = REQ_OP_READ;
1099		else if (test_and_clear_bit(R5_WantReplace,
1100					    &sh->dev[i].flags)) {
1101			op = REQ_OP_WRITE;
1102			replace_only = 1;
1103		} else
1104			continue;
1105		if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
1106			op_flags |= REQ_SYNC;
1107
1108again:
1109		dev = &sh->dev[i];
1110		bi = &dev->req;
1111		rbi = &dev->rreq; /* For writing to replacement */
1112
1113		rcu_read_lock();
1114		rrdev = rcu_dereference(conf->disks[i].replacement);
1115		smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
1116		rdev = rcu_dereference(conf->disks[i].rdev);
1117		if (!rdev) {
1118			rdev = rrdev;
1119			rrdev = NULL;
1120		}
1121		if (op_is_write(op)) {
1122			if (replace_only)
1123				rdev = NULL;
1124			if (rdev == rrdev)
1125				/* We raced and saw duplicates */
1126				rrdev = NULL;
1127		} else {
1128			if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
1129				rdev = rrdev;
1130			rrdev = NULL;
1131		}
1132
1133		if (rdev && test_bit(Faulty, &rdev->flags))
1134			rdev = NULL;
1135		if (rdev)
1136			atomic_inc(&rdev->nr_pending);
1137		if (rrdev && test_bit(Faulty, &rrdev->flags))
1138			rrdev = NULL;
1139		if (rrdev)
1140			atomic_inc(&rrdev->nr_pending);
1141		rcu_read_unlock();
1142
1143		/* We have already checked bad blocks for reads.  Now
1144		 * need to check for writes.  We never accept write errors
1145		 * on the replacement, so we don't to check rrdev.
1146		 */
1147		while (op_is_write(op) && rdev &&
1148		       test_bit(WriteErrorSeen, &rdev->flags)) {
1149			sector_t first_bad;
1150			int bad_sectors;
1151			int bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
1152					      &first_bad, &bad_sectors);
1153			if (!bad)
1154				break;
1155
1156			if (bad < 0) {
1157				set_bit(BlockedBadBlocks, &rdev->flags);
1158				if (!conf->mddev->external &&
1159				    conf->mddev->sb_flags) {
1160					/* It is very unlikely, but we might
1161					 * still need to write out the
1162					 * bad block log - better give it
1163					 * a chance*/
1164					md_check_recovery(conf->mddev);
1165				}
1166				/*
1167				 * Because md_wait_for_blocked_rdev
1168				 * will dec nr_pending, we must
1169				 * increment it first.
1170				 */
1171				atomic_inc(&rdev->nr_pending);
1172				md_wait_for_blocked_rdev(rdev, conf->mddev);
1173			} else {
1174				/* Acknowledged bad block - skip the write */
1175				rdev_dec_pending(rdev, conf->mddev);
1176				rdev = NULL;
1177			}
1178		}
1179
1180		if (rdev) {
1181			if (s->syncing || s->expanding || s->expanded
1182			    || s->replacing)
1183				md_sync_acct(rdev->bdev, RAID5_STRIPE_SECTORS(conf));
1184
1185			set_bit(STRIPE_IO_STARTED, &sh->state);
1186
1187			bio_init(bi, rdev->bdev, &dev->vec, 1, op | op_flags);
1188			bi->bi_end_io = op_is_write(op)
1189				? raid5_end_write_request
1190				: raid5_end_read_request;
1191			bi->bi_private = sh;
1192
1193			pr_debug("%s: for %llu schedule op %d on disc %d\n",
1194				__func__, (unsigned long long)sh->sector,
1195				bi->bi_opf, i);
1196			atomic_inc(&sh->count);
1197			if (sh != head_sh)
1198				atomic_inc(&head_sh->count);
1199			if (use_new_offset(conf, sh))
1200				bi->bi_iter.bi_sector = (sh->sector
1201						 + rdev->new_data_offset);
1202			else
1203				bi->bi_iter.bi_sector = (sh->sector
1204						 + rdev->data_offset);
1205			if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
1206				bi->bi_opf |= REQ_NOMERGE;
1207
1208			if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1209				WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1210
1211			if (!op_is_write(op) &&
1212			    test_bit(R5_InJournal, &sh->dev[i].flags))
1213				/*
1214				 * issuing read for a page in journal, this
1215				 * must be preparing for prexor in rmw; read
1216				 * the data into orig_page
1217				 */
1218				sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
1219			else
1220				sh->dev[i].vec.bv_page = sh->dev[i].page;
1221			bi->bi_vcnt = 1;
1222			bi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf);
1223			bi->bi_io_vec[0].bv_offset = sh->dev[i].offset;
1224			bi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf);
1225			/*
1226			 * If this is discard request, set bi_vcnt 0. We don't
1227			 * want to confuse SCSI because SCSI will replace payload
1228			 */
1229			if (op == REQ_OP_DISCARD)
1230				bi->bi_vcnt = 0;
1231			if (rrdev)
1232				set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
1233
1234			if (conf->mddev->gendisk)
1235				trace_block_bio_remap(bi,
1236						disk_devt(conf->mddev->gendisk),
1237						sh->dev[i].sector);
1238			if (should_defer && op_is_write(op))
1239				bio_list_add(&pending_bios, bi);
1240			else
1241				submit_bio_noacct(bi);
1242		}
1243		if (rrdev) {
1244			if (s->syncing || s->expanding || s->expanded
1245			    || s->replacing)
1246				md_sync_acct(rrdev->bdev, RAID5_STRIPE_SECTORS(conf));
1247
1248			set_bit(STRIPE_IO_STARTED, &sh->state);
1249
1250			bio_init(rbi, rrdev->bdev, &dev->rvec, 1, op | op_flags);
1251			BUG_ON(!op_is_write(op));
1252			rbi->bi_end_io = raid5_end_write_request;
1253			rbi->bi_private = sh;
1254
1255			pr_debug("%s: for %llu schedule op %d on "
1256				 "replacement disc %d\n",
1257				__func__, (unsigned long long)sh->sector,
1258				rbi->bi_opf, i);
1259			atomic_inc(&sh->count);
1260			if (sh != head_sh)
1261				atomic_inc(&head_sh->count);
1262			if (use_new_offset(conf, sh))
1263				rbi->bi_iter.bi_sector = (sh->sector
1264						  + rrdev->new_data_offset);
1265			else
1266				rbi->bi_iter.bi_sector = (sh->sector
1267						  + rrdev->data_offset);
1268			if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1269				WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1270			sh->dev[i].rvec.bv_page = sh->dev[i].page;
1271			rbi->bi_vcnt = 1;
1272			rbi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf);
1273			rbi->bi_io_vec[0].bv_offset = sh->dev[i].offset;
1274			rbi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf);
1275			/*
1276			 * If this is discard request, set bi_vcnt 0. We don't
1277			 * want to confuse SCSI because SCSI will replace payload
1278			 */
1279			if (op == REQ_OP_DISCARD)
1280				rbi->bi_vcnt = 0;
1281			if (conf->mddev->gendisk)
1282				trace_block_bio_remap(rbi,
1283						disk_devt(conf->mddev->gendisk),
1284						sh->dev[i].sector);
1285			if (should_defer && op_is_write(op))
1286				bio_list_add(&pending_bios, rbi);
1287			else
1288				submit_bio_noacct(rbi);
1289		}
1290		if (!rdev && !rrdev) {
1291			if (op_is_write(op))
1292				set_bit(STRIPE_DEGRADED, &sh->state);
1293			pr_debug("skip op %d on disc %d for sector %llu\n",
1294				bi->bi_opf, i, (unsigned long long)sh->sector);
1295			clear_bit(R5_LOCKED, &sh->dev[i].flags);
1296			set_bit(STRIPE_HANDLE, &sh->state);
1297		}
1298
1299		if (!head_sh->batch_head)
1300			continue;
1301		sh = list_first_entry(&sh->batch_list, struct stripe_head,
1302				      batch_list);
1303		if (sh != head_sh)
1304			goto again;
1305	}
1306
1307	if (should_defer && !bio_list_empty(&pending_bios))
1308		defer_issue_bios(conf, head_sh->sector, &pending_bios);
1309}
1310
1311static struct dma_async_tx_descriptor *
1312async_copy_data(int frombio, struct bio *bio, struct page **page,
1313	unsigned int poff, sector_t sector, struct dma_async_tx_descriptor *tx,
1314	struct stripe_head *sh, int no_skipcopy)
1315{
1316	struct bio_vec bvl;
1317	struct bvec_iter iter;
1318	struct page *bio_page;
1319	int page_offset;
1320	struct async_submit_ctl submit;
1321	enum async_tx_flags flags = 0;
1322	struct r5conf *conf = sh->raid_conf;
1323
1324	if (bio->bi_iter.bi_sector >= sector)
1325		page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
1326	else
1327		page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
1328
1329	if (frombio)
1330		flags |= ASYNC_TX_FENCE;
1331	init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1332
1333	bio_for_each_segment(bvl, bio, iter) {
1334		int len = bvl.bv_len;
1335		int clen;
1336		int b_offset = 0;
1337
1338		if (page_offset < 0) {
1339			b_offset = -page_offset;
1340			page_offset += b_offset;
1341			len -= b_offset;
1342		}
1343
1344		if (len > 0 && page_offset + len > RAID5_STRIPE_SIZE(conf))
1345			clen = RAID5_STRIPE_SIZE(conf) - page_offset;
1346		else
1347			clen = len;
1348
1349		if (clen > 0) {
1350			b_offset += bvl.bv_offset;
1351			bio_page = bvl.bv_page;
1352			if (frombio) {
1353				if (conf->skip_copy &&
1354				    b_offset == 0 && page_offset == 0 &&
1355				    clen == RAID5_STRIPE_SIZE(conf) &&
1356				    !no_skipcopy)
1357					*page = bio_page;
1358				else
1359					tx = async_memcpy(*page, bio_page, page_offset + poff,
1360						  b_offset, clen, &submit);
1361			} else
1362				tx = async_memcpy(bio_page, *page, b_offset,
1363						  page_offset + poff, clen, &submit);
1364		}
1365		/* chain the operations */
1366		submit.depend_tx = tx;
1367
1368		if (clen < len) /* hit end of page */
1369			break;
1370		page_offset +=  len;
1371	}
1372
1373	return tx;
1374}
1375
1376static void ops_complete_biofill(void *stripe_head_ref)
1377{
1378	struct stripe_head *sh = stripe_head_ref;
1379	int i;
1380	struct r5conf *conf = sh->raid_conf;
1381
1382	pr_debug("%s: stripe %llu\n", __func__,
1383		(unsigned long long)sh->sector);
1384
1385	/* clear completed biofills */
1386	for (i = sh->disks; i--; ) {
1387		struct r5dev *dev = &sh->dev[i];
1388
1389		/* acknowledge completion of a biofill operation */
1390		/* and check if we need to reply to a read request,
1391		 * new R5_Wantfill requests are held off until
1392		 * !STRIPE_BIOFILL_RUN
1393		 */
1394		if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
1395			struct bio *rbi, *rbi2;
1396
1397			BUG_ON(!dev->read);
1398			rbi = dev->read;
1399			dev->read = NULL;
1400			while (rbi && rbi->bi_iter.bi_sector <
1401				dev->sector + RAID5_STRIPE_SECTORS(conf)) {
1402				rbi2 = r5_next_bio(conf, rbi, dev->sector);
1403				bio_endio(rbi);
1404				rbi = rbi2;
1405			}
1406		}
1407	}
1408	clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
1409
1410	set_bit(STRIPE_HANDLE, &sh->state);
1411	raid5_release_stripe(sh);
1412}
1413
1414static void ops_run_biofill(struct stripe_head *sh)
1415{
1416	struct dma_async_tx_descriptor *tx = NULL;
1417	struct async_submit_ctl submit;
1418	int i;
1419	struct r5conf *conf = sh->raid_conf;
1420
1421	BUG_ON(sh->batch_head);
1422	pr_debug("%s: stripe %llu\n", __func__,
1423		(unsigned long long)sh->sector);
1424
1425	for (i = sh->disks; i--; ) {
1426		struct r5dev *dev = &sh->dev[i];
1427		if (test_bit(R5_Wantfill, &dev->flags)) {
1428			struct bio *rbi;
1429			spin_lock_irq(&sh->stripe_lock);
1430			dev->read = rbi = dev->toread;
1431			dev->toread = NULL;
1432			spin_unlock_irq(&sh->stripe_lock);
1433			while (rbi && rbi->bi_iter.bi_sector <
1434				dev->sector + RAID5_STRIPE_SECTORS(conf)) {
1435				tx = async_copy_data(0, rbi, &dev->page,
1436						     dev->offset,
1437						     dev->sector, tx, sh, 0);
1438				rbi = r5_next_bio(conf, rbi, dev->sector);
1439			}
1440		}
1441	}
1442
1443	atomic_inc(&sh->count);
1444	init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1445	async_trigger_callback(&submit);
1446}
1447
1448static void mark_target_uptodate(struct stripe_head *sh, int target)
1449{
1450	struct r5dev *tgt;
1451
1452	if (target < 0)
1453		return;
1454
1455	tgt = &sh->dev[target];
1456	set_bit(R5_UPTODATE, &tgt->flags);
1457	BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1458	clear_bit(R5_Wantcompute, &tgt->flags);
1459}
1460
1461static void ops_complete_compute(void *stripe_head_ref)
1462{
1463	struct stripe_head *sh = stripe_head_ref;
1464
1465	pr_debug("%s: stripe %llu\n", __func__,
1466		(unsigned long long)sh->sector);
1467
1468	/* mark the computed target(s) as uptodate */
1469	mark_target_uptodate(sh, sh->ops.target);
1470	mark_target_uptodate(sh, sh->ops.target2);
1471
1472	clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1473	if (sh->check_state == check_state_compute_run)
1474		sh->check_state = check_state_compute_result;
1475	set_bit(STRIPE_HANDLE, &sh->state);
1476	raid5_release_stripe(sh);
1477}
1478
1479/* return a pointer to the address conversion region of the scribble buffer */
1480static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
1481{
1482	return percpu->scribble + i * percpu->scribble_obj_size;
1483}
1484
1485/* return a pointer to the address conversion region of the scribble buffer */
1486static addr_conv_t *to_addr_conv(struct stripe_head *sh,
1487				 struct raid5_percpu *percpu, int i)
1488{
1489	return (void *) (to_addr_page(percpu, i) + sh->disks + 2);
1490}
1491
1492/*
1493 * Return a pointer to record offset address.
1494 */
1495static unsigned int *
1496to_addr_offs(struct stripe_head *sh, struct raid5_percpu *percpu)
1497{
1498	return (unsigned int *) (to_addr_conv(sh, percpu, 0) + sh->disks + 2);
1499}
1500
1501static struct dma_async_tx_descriptor *
1502ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1503{
1504	int disks = sh->disks;
1505	struct page **xor_srcs = to_addr_page(percpu, 0);
1506	unsigned int *off_srcs = to_addr_offs(sh, percpu);
1507	int target = sh->ops.target;
1508	struct r5dev *tgt = &sh->dev[target];
1509	struct page *xor_dest = tgt->page;
1510	unsigned int off_dest = tgt->offset;
1511	int count = 0;
1512	struct dma_async_tx_descriptor *tx;
1513	struct async_submit_ctl submit;
1514	int i;
1515
1516	BUG_ON(sh->batch_head);
1517
1518	pr_debug("%s: stripe %llu block: %d\n",
1519		__func__, (unsigned long long)sh->sector, target);
1520	BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1521
1522	for (i = disks; i--; ) {
1523		if (i != target) {
1524			off_srcs[count] = sh->dev[i].offset;
1525			xor_srcs[count++] = sh->dev[i].page;
1526		}
1527	}
1528
1529	atomic_inc(&sh->count);
1530
1531	init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
1532			  ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
1533	if (unlikely(count == 1))
1534		tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0],
1535				RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1536	else
1537		tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
1538				RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1539
1540	return tx;
1541}
1542
1543/* set_syndrome_sources - populate source buffers for gen_syndrome
1544 * @srcs - (struct page *) array of size sh->disks
1545 * @offs - (unsigned int) array of offset for each page
1546 * @sh - stripe_head to parse
1547 *
1548 * Populates srcs in proper layout order for the stripe and returns the
1549 * 'count' of sources to be used in a call to async_gen_syndrome.  The P
1550 * destination buffer is recorded in srcs[count] and the Q destination
1551 * is recorded in srcs[count+1]].
1552 */
1553static int set_syndrome_sources(struct page **srcs,
1554				unsigned int *offs,
1555				struct stripe_head *sh,
1556				int srctype)
1557{
1558	int disks = sh->disks;
1559	int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1560	int d0_idx = raid6_d0(sh);
1561	int count;
1562	int i;
1563
1564	for (i = 0; i < disks; i++)
1565		srcs[i] = NULL;
1566
1567	count = 0;
1568	i = d0_idx;
1569	do {
1570		int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1571		struct r5dev *dev = &sh->dev[i];
1572
1573		if (i == sh->qd_idx || i == sh->pd_idx ||
1574		    (srctype == SYNDROME_SRC_ALL) ||
1575		    (srctype == SYNDROME_SRC_WANT_DRAIN &&
1576		     (test_bit(R5_Wantdrain, &dev->flags) ||
1577		      test_bit(R5_InJournal, &dev->flags))) ||
1578		    (srctype == SYNDROME_SRC_WRITTEN &&
1579		     (dev->written ||
1580		      test_bit(R5_InJournal, &dev->flags)))) {
1581			if (test_bit(R5_InJournal, &dev->flags))
1582				srcs[slot] = sh->dev[i].orig_page;
1583			else
1584				srcs[slot] = sh->dev[i].page;
1585			/*
1586			 * For R5_InJournal, PAGE_SIZE must be 4KB and will
1587			 * not shared page. In that case, dev[i].offset
1588			 * is 0.
1589			 */
1590			offs[slot] = sh->dev[i].offset;
1591		}
1592		i = raid6_next_disk(i, disks);
1593	} while (i != d0_idx);
1594
1595	return syndrome_disks;
1596}
1597
1598static struct dma_async_tx_descriptor *
1599ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1600{
1601	int disks = sh->disks;
1602	struct page **blocks = to_addr_page(percpu, 0);
1603	unsigned int *offs = to_addr_offs(sh, percpu);
1604	int target;
1605	int qd_idx = sh->qd_idx;
1606	struct dma_async_tx_descriptor *tx;
1607	struct async_submit_ctl submit;
1608	struct r5dev *tgt;
1609	struct page *dest;
1610	unsigned int dest_off;
1611	int i;
1612	int count;
1613
1614	BUG_ON(sh->batch_head);
1615	if (sh->ops.target < 0)
1616		target = sh->ops.target2;
1617	else if (sh->ops.target2 < 0)
1618		target = sh->ops.target;
1619	else
1620		/* we should only have one valid target */
1621		BUG();
1622	BUG_ON(target < 0);
1623	pr_debug("%s: stripe %llu block: %d\n",
1624		__func__, (unsigned long long)sh->sector, target);
1625
1626	tgt = &sh->dev[target];
1627	BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1628	dest = tgt->page;
1629	dest_off = tgt->offset;
1630
1631	atomic_inc(&sh->count);
1632
1633	if (target == qd_idx) {
1634		count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL);
1635		blocks[count] = NULL; /* regenerating p is not necessary */
1636		BUG_ON(blocks[count+1] != dest); /* q should already be set */
1637		init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1638				  ops_complete_compute, sh,
1639				  to_addr_conv(sh, percpu, 0));
1640		tx = async_gen_syndrome(blocks, offs, count+2,
1641				RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1642	} else {
1643		/* Compute any data- or p-drive using XOR */
1644		count = 0;
1645		for (i = disks; i-- ; ) {
1646			if (i == target || i == qd_idx)
1647				continue;
1648			offs[count] = sh->dev[i].offset;
1649			blocks[count++] = sh->dev[i].page;
1650		}
1651
1652		init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1653				  NULL, ops_complete_compute, sh,
1654				  to_addr_conv(sh, percpu, 0));
1655		tx = async_xor_offs(dest, dest_off, blocks, offs, count,
1656				RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1657	}
1658
1659	return tx;
1660}
1661
1662static struct dma_async_tx_descriptor *
1663ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1664{
1665	int i, count, disks = sh->disks;
1666	int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1667	int d0_idx = raid6_d0(sh);
1668	int faila = -1, failb = -1;
1669	int target = sh->ops.target;
1670	int target2 = sh->ops.target2;
1671	struct r5dev *tgt = &sh->dev[target];
1672	struct r5dev *tgt2 = &sh->dev[target2];
1673	struct dma_async_tx_descriptor *tx;
1674	struct page **blocks = to_addr_page(percpu, 0);
1675	unsigned int *offs = to_addr_offs(sh, percpu);
1676	struct async_submit_ctl submit;
1677
1678	BUG_ON(sh->batch_head);
1679	pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1680		 __func__, (unsigned long long)sh->sector, target, target2);
1681	BUG_ON(target < 0 || target2 < 0);
1682	BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1683	BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1684
1685	/* we need to open-code set_syndrome_sources to handle the
1686	 * slot number conversion for 'faila' and 'failb'
1687	 */
1688	for (i = 0; i < disks ; i++) {
1689		offs[i] = 0;
1690		blocks[i] = NULL;
1691	}
1692	count = 0;
1693	i = d0_idx;
1694	do {
1695		int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1696
1697		offs[slot] = sh->dev[i].offset;
1698		blocks[slot] = sh->dev[i].page;
1699
1700		if (i == target)
1701			faila = slot;
1702		if (i == target2)
1703			failb = slot;
1704		i = raid6_next_disk(i, disks);
1705	} while (i != d0_idx);
1706
1707	BUG_ON(faila == failb);
1708	if (failb < faila)
1709		swap(faila, failb);
1710	pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1711		 __func__, (unsigned long long)sh->sector, faila, failb);
1712
1713	atomic_inc(&sh->count);
1714
1715	if (failb == syndrome_disks+1) {
1716		/* Q disk is one of the missing disks */
1717		if (faila == syndrome_disks) {
1718			/* Missing P+Q, just recompute */
1719			init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1720					  ops_complete_compute, sh,
1721					  to_addr_conv(sh, percpu, 0));
1722			return async_gen_syndrome(blocks, offs, syndrome_disks+2,
1723						  RAID5_STRIPE_SIZE(sh->raid_conf),
1724						  &submit);
1725		} else {
1726			struct page *dest;
1727			unsigned int dest_off;
1728			int data_target;
1729			int qd_idx = sh->qd_idx;
1730
1731			/* Missing D+Q: recompute D from P, then recompute Q */
1732			if (target == qd_idx)
1733				data_target = target2;
1734			else
1735				data_target = target;
1736
1737			count = 0;
1738			for (i = disks; i-- ; ) {
1739				if (i == data_target || i == qd_idx)
1740					continue;
1741				offs[count] = sh->dev[i].offset;
1742				blocks[count++] = sh->dev[i].page;
1743			}
1744			dest = sh->dev[data_target].page;
1745			dest_off = sh->dev[data_target].offset;
1746			init_async_submit(&submit,
1747					  ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1748					  NULL, NULL, NULL,
1749					  to_addr_conv(sh, percpu, 0));
1750			tx = async_xor_offs(dest, dest_off, blocks, offs, count,
1751				       RAID5_STRIPE_SIZE(sh->raid_conf),
1752				       &submit);
1753
1754			count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL);
1755			init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1756					  ops_complete_compute, sh,
1757					  to_addr_conv(sh, percpu, 0));
1758			return async_gen_syndrome(blocks, offs, count+2,
1759						  RAID5_STRIPE_SIZE(sh->raid_conf),
1760						  &submit);
1761		}
1762	} else {
1763		init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1764				  ops_complete_compute, sh,
1765				  to_addr_conv(sh, percpu, 0));
1766		if (failb == syndrome_disks) {
1767			/* We're missing D+P. */
1768			return async_raid6_datap_recov(syndrome_disks+2,
1769						RAID5_STRIPE_SIZE(sh->raid_conf),
1770						faila,
1771						blocks, offs, &submit);
1772		} else {
1773			/* We're missing D+D. */
1774			return async_raid6_2data_recov(syndrome_disks+2,
1775						RAID5_STRIPE_SIZE(sh->raid_conf),
1776						faila, failb,
1777						blocks, offs, &submit);
1778		}
1779	}
1780}
1781
1782static void ops_complete_prexor(void *stripe_head_ref)
1783{
1784	struct stripe_head *sh = stripe_head_ref;
1785
1786	pr_debug("%s: stripe %llu\n", __func__,
1787		(unsigned long long)sh->sector);
1788
1789	if (r5c_is_writeback(sh->raid_conf->log))
1790		/*
1791		 * raid5-cache write back uses orig_page during prexor.
1792		 * After prexor, it is time to free orig_page
1793		 */
1794		r5c_release_extra_page(sh);
1795}
1796
1797static struct dma_async_tx_descriptor *
1798ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1799		struct dma_async_tx_descriptor *tx)
1800{
1801	int disks = sh->disks;
1802	struct page **xor_srcs = to_addr_page(percpu, 0);
1803	unsigned int *off_srcs = to_addr_offs(sh, percpu);
1804	int count = 0, pd_idx = sh->pd_idx, i;
1805	struct async_submit_ctl submit;
1806
1807	/* existing parity data subtracted */
1808	unsigned int off_dest = off_srcs[count] = sh->dev[pd_idx].offset;
1809	struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1810
1811	BUG_ON(sh->batch_head);
1812	pr_debug("%s: stripe %llu\n", __func__,
1813		(unsigned long long)sh->sector);
1814
1815	for (i = disks; i--; ) {
1816		struct r5dev *dev = &sh->dev[i];
1817		/* Only process blocks that are known to be uptodate */
1818		if (test_bit(R5_InJournal, &dev->flags)) {
1819			/*
1820			 * For this case, PAGE_SIZE must be equal to 4KB and
1821			 * page offset is zero.
1822			 */
1823			off_srcs[count] = dev->offset;
1824			xor_srcs[count++] = dev->orig_page;
1825		} else if (test_bit(R5_Wantdrain, &dev->flags)) {
1826			off_srcs[count] = dev->offset;
1827			xor_srcs[count++] = dev->page;
1828		}
1829	}
1830
1831	init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
1832			  ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1833	tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
1834			RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1835
1836	return tx;
1837}
1838
1839static struct dma_async_tx_descriptor *
1840ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1841		struct dma_async_tx_descriptor *tx)
1842{
1843	struct page **blocks = to_addr_page(percpu, 0);
1844	unsigned int *offs = to_addr_offs(sh, percpu);
1845	int count;
1846	struct async_submit_ctl submit;
1847
1848	pr_debug("%s: stripe %llu\n", __func__,
1849		(unsigned long long)sh->sector);
1850
1851	count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_WANT_DRAIN);
1852
1853	init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1854			  ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1855	tx = async_gen_syndrome(blocks, offs, count+2,
1856			RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
1857
1858	return tx;
1859}
1860
1861static struct dma_async_tx_descriptor *
1862ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
1863{
1864	struct r5conf *conf = sh->raid_conf;
1865	int disks = sh->disks;
1866	int i;
1867	struct stripe_head *head_sh = sh;
1868
1869	pr_debug("%s: stripe %llu\n", __func__,
1870		(unsigned long long)sh->sector);
1871
1872	for (i = disks; i--; ) {
1873		struct r5dev *dev;
1874		struct bio *chosen;
1875
1876		sh = head_sh;
1877		if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
1878			struct bio *wbi;
1879
1880again:
1881			dev = &sh->dev[i];
1882			/*
1883			 * clear R5_InJournal, so when rewriting a page in
1884			 * journal, it is not skipped by r5l_log_stripe()
1885			 */
1886			clear_bit(R5_InJournal, &dev->flags);
1887			spin_lock_irq(&sh->stripe_lock);
1888			chosen = dev->towrite;
1889			dev->towrite = NULL;
1890			sh->overwrite_disks = 0;
1891			BUG_ON(dev->written);
1892			wbi = dev->written = chosen;
1893			spin_unlock_irq(&sh->stripe_lock);
1894			WARN_ON(dev->page != dev->orig_page);
1895
1896			while (wbi && wbi->bi_iter.bi_sector <
1897				dev->sector + RAID5_STRIPE_SECTORS(conf)) {
1898				if (wbi->bi_opf & REQ_FUA)
1899					set_bit(R5_WantFUA, &dev->flags);
1900				if (wbi->bi_opf & REQ_SYNC)
1901					set_bit(R5_SyncIO, &dev->flags);
1902				if (bio_op(wbi) == REQ_OP_DISCARD)
1903					set_bit(R5_Discard, &dev->flags);
1904				else {
1905					tx = async_copy_data(1, wbi, &dev->page,
1906							     dev->offset,
1907							     dev->sector, tx, sh,
1908							     r5c_is_writeback(conf->log));
1909					if (dev->page != dev->orig_page &&
1910					    !r5c_is_writeback(conf->log)) {
1911						set_bit(R5_SkipCopy, &dev->flags);
1912						clear_bit(R5_UPTODATE, &dev->flags);
1913						clear_bit(R5_OVERWRITE, &dev->flags);
1914					}
1915				}
1916				wbi = r5_next_bio(conf, wbi, dev->sector);
1917			}
1918
1919			if (head_sh->batch_head) {
1920				sh = list_first_entry(&sh->batch_list,
1921						      struct stripe_head,
1922						      batch_list);
1923				if (sh == head_sh)
1924					continue;
1925				goto again;
1926			}
1927		}
1928	}
1929
1930	return tx;
1931}
1932
1933static void ops_complete_reconstruct(void *stripe_head_ref)
1934{
1935	struct stripe_head *sh = stripe_head_ref;
1936	int disks = sh->disks;
1937	int pd_idx = sh->pd_idx;
1938	int qd_idx = sh->qd_idx;
1939	int i;
1940	bool fua = false, sync = false, discard = false;
1941
1942	pr_debug("%s: stripe %llu\n", __func__,
1943		(unsigned long long)sh->sector);
1944
1945	for (i = disks; i--; ) {
1946		fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1947		sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1948		discard |= test_bit(R5_Discard, &sh->dev[i].flags);
1949	}
1950
1951	for (i = disks; i--; ) {
1952		struct r5dev *dev = &sh->dev[i];
1953
1954		if (dev->written || i == pd_idx || i == qd_idx) {
1955			if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) {
1956				set_bit(R5_UPTODATE, &dev->flags);
1957				if (test_bit(STRIPE_EXPAND_READY, &sh->state))
1958					set_bit(R5_Expanded, &dev->flags);
1959			}
1960			if (fua)
1961				set_bit(R5_WantFUA, &dev->flags);
1962			if (sync)
1963				set_bit(R5_SyncIO, &dev->flags);
1964		}
1965	}
1966
1967	if (sh->reconstruct_state == reconstruct_state_drain_run)
1968		sh->reconstruct_state = reconstruct_state_drain_result;
1969	else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1970		sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1971	else {
1972		BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1973		sh->reconstruct_state = reconstruct_state_result;
1974	}
1975
1976	set_bit(STRIPE_HANDLE, &sh->state);
1977	raid5_release_stripe(sh);
1978}
1979
1980static void
1981ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1982		     struct dma_async_tx_descriptor *tx)
1983{
1984	int disks = sh->disks;
1985	struct page **xor_srcs;
1986	unsigned int *off_srcs;
1987	struct async_submit_ctl submit;
1988	int count, pd_idx = sh->pd_idx, i;
1989	struct page *xor_dest;
1990	unsigned int off_dest;
1991	int prexor = 0;
1992	unsigned long flags;
1993	int j = 0;
1994	struct stripe_head *head_sh = sh;
1995	int last_stripe;
1996
1997	pr_debug("%s: stripe %llu\n", __func__,
1998		(unsigned long long)sh->sector);
1999
2000	for (i = 0; i < sh->disks; i++) {
2001		if (pd_idx == i)
2002			continue;
2003		if (!test_bit(R5_Discard, &sh->dev[i].flags))
2004			break;
2005	}
2006	if (i >= sh->disks) {
2007		atomic_inc(&sh->count);
2008		set_bit(R5_Discard, &sh->dev[pd_idx].flags);
2009		ops_complete_reconstruct(sh);
2010		return;
2011	}
2012again:
2013	count = 0;
2014	xor_srcs = to_addr_page(percpu, j);
2015	off_srcs = to_addr_offs(sh, percpu);
2016	/* check if prexor is active which means only process blocks
2017	 * that are part of a read-modify-write (written)
2018	 */
2019	if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
2020		prexor = 1;
2021		off_dest = off_srcs[count] = sh->dev[pd_idx].offset;
2022		xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
2023		for (i = disks; i--; ) {
2024			struct r5dev *dev = &sh->dev[i];
2025			if (head_sh->dev[i].written ||
2026			    test_bit(R5_InJournal, &head_sh->dev[i].flags)) {
2027				off_srcs[count] = dev->offset;
2028				xor_srcs[count++] = dev->page;
2029			}
2030		}
2031	} else {
2032		xor_dest = sh->dev[pd_idx].page;
2033		off_dest = sh->dev[pd_idx].offset;
2034		for (i = disks; i--; ) {
2035			struct r5dev *dev = &sh->dev[i];
2036			if (i != pd_idx) {
2037				off_srcs[count] = dev->offset;
2038				xor_srcs[count++] = dev->page;
2039			}
2040		}
2041	}
2042
2043	/* 1/ if we prexor'd then the dest is reused as a source
2044	 * 2/ if we did not prexor then we are redoing the parity
2045	 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
2046	 * for the synchronous xor case
2047	 */
2048	last_stripe = !head_sh->batch_head ||
2049		list_first_entry(&sh->batch_list,
2050				 struct stripe_head, batch_list) == head_sh;
2051	if (last_stripe) {
2052		flags = ASYNC_TX_ACK |
2053			(prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
2054
2055		atomic_inc(&head_sh->count);
2056		init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
2057				  to_addr_conv(sh, percpu, j));
2058	} else {
2059		flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
2060		init_async_submit(&submit, flags, tx, NULL, NULL,
2061				  to_addr_conv(sh, percpu, j));
2062	}
2063
2064	if (unlikely(count == 1))
2065		tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0],
2066				RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
2067	else
2068		tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
2069				RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
2070	if (!last_stripe) {
2071		j++;
2072		sh = list_first_entry(&sh->batch_list, struct stripe_head,
2073				      batch_list);
2074		goto again;
2075	}
2076}
2077
2078static void
2079ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
2080		     struct dma_async_tx_descriptor *tx)
2081{
2082	struct async_submit_ctl submit;
2083	struct page **blocks;
2084	unsigned int *offs;
2085	int count, i, j = 0;
2086	struct stripe_head *head_sh = sh;
2087	int last_stripe;
2088	int synflags;
2089	unsigned long txflags;
2090
2091	pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
2092
2093	for (i = 0; i < sh->disks; i++) {
2094		if (sh->pd_idx == i || sh->qd_idx == i)
2095			continue;
2096		if (!test_bit(R5_Discard, &sh->dev[i].flags))
2097			break;
2098	}
2099	if (i >= sh->disks) {
2100		atomic_inc(&sh->count);
2101		set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
2102		set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
2103		ops_complete_reconstruct(sh);
2104		return;
2105	}
2106
2107again:
2108	blocks = to_addr_page(percpu, j);
2109	offs = to_addr_offs(sh, percpu);
2110
2111	if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
2112		synflags = SYNDROME_SRC_WRITTEN;
2113		txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
2114	} else {
2115		synflags = SYNDROME_SRC_ALL;
2116		txflags = ASYNC_TX_ACK;
2117	}
2118
2119	count = set_syndrome_sources(blocks, offs, sh, synflags);
2120	last_stripe = !head_sh->batch_head ||
2121		list_first_entry(&sh->batch_list,
2122				 struct stripe_head, batch_list) == head_sh;
2123
2124	if (last_stripe) {
2125		atomic_inc(&head_sh->count);
2126		init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
2127				  head_sh, to_addr_conv(sh, percpu, j));
2128	} else
2129		init_async_submit(&submit, 0, tx, NULL, NULL,
2130				  to_addr_conv(sh, percpu, j));
2131	tx = async_gen_syndrome(blocks, offs, count+2,
2132			RAID5_STRIPE_SIZE(sh->raid_conf),  &submit);
2133	if (!last_stripe) {
2134		j++;
2135		sh = list_first_entry(&sh->batch_list, struct stripe_head,
2136				      batch_list);
2137		goto again;
2138	}
2139}
2140
2141static void ops_complete_check(void *stripe_head_ref)
2142{
2143	struct stripe_head *sh = stripe_head_ref;
2144
2145	pr_debug("%s: stripe %llu\n", __func__,
2146		(unsigned long long)sh->sector);
2147
2148	sh->check_state = check_state_check_result;
2149	set_bit(STRIPE_HANDLE, &sh->state);
2150	raid5_release_stripe(sh);
2151}
2152
2153static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
2154{
2155	int disks = sh->disks;
2156	int pd_idx = sh->pd_idx;
2157	int qd_idx = sh->qd_idx;
2158	struct page *xor_dest;
2159	unsigned int off_dest;
2160	struct page **xor_srcs = to_addr_page(percpu, 0);
2161	unsigned int *off_srcs = to_addr_offs(sh, percpu);
2162	struct dma_async_tx_descriptor *tx;
2163	struct async_submit_ctl submit;
2164	int count;
2165	int i;
2166
2167	pr_debug("%s: stripe %llu\n", __func__,
2168		(unsigned long long)sh->sector);
2169
2170	BUG_ON(sh->batch_head);
2171	count = 0;
2172	xor_dest = sh->dev[pd_idx].page;
2173	off_dest = sh->dev[pd_idx].offset;
2174	off_srcs[count] = off_dest;
2175	xor_srcs[count++] = xor_dest;
2176	for (i = disks; i--; ) {
2177		if (i == pd_idx || i == qd_idx)
2178			continue;
2179		off_srcs[count] = sh->dev[i].offset;
2180		xor_srcs[count++] = sh->dev[i].page;
2181	}
2182
2183	init_async_submit(&submit, 0, NULL, NULL, NULL,
2184			  to_addr_conv(sh, percpu, 0));
2185	tx = async_xor_val_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
2186			   RAID5_STRIPE_SIZE(sh->raid_conf),
2187			   &sh->ops.zero_sum_result, &submit);
2188
2189	atomic_inc(&sh->count);
2190	init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
2191	tx = async_trigger_callback(&submit);
2192}
2193
2194static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
2195{
2196	struct page **srcs = to_addr_page(percpu, 0);
2197	unsigned int *offs = to_addr_offs(sh, percpu);
2198	struct async_submit_ctl submit;
2199	int count;
2200
2201	pr_debug("%s: stripe %llu checkp: %d\n", __func__,
2202		(unsigned long long)sh->sector, checkp);
2203
2204	BUG_ON(sh->batch_head);
2205	count = set_syndrome_sources(srcs, offs, sh, SYNDROME_SRC_ALL);
2206	if (!checkp)
2207		srcs[count] = NULL;
2208
2209	atomic_inc(&sh->count);
2210	init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
2211			  sh, to_addr_conv(sh, percpu, 0));
2212	async_syndrome_val(srcs, offs, count+2,
2213			   RAID5_STRIPE_SIZE(sh->raid_conf),
2214			   &sh->ops.zero_sum_result, percpu->spare_page, 0, &submit);
2215}
2216
2217static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
2218{
2219	int overlap_clear = 0, i, disks = sh->disks;
2220	struct dma_async_tx_descriptor *tx = NULL;
2221	struct r5conf *conf = sh->raid_conf;
2222	int level = conf->level;
2223	struct raid5_percpu *percpu;
2224
2225	local_lock(&conf->percpu->lock);
2226	percpu = this_cpu_ptr(conf->percpu);
2227	if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
2228		ops_run_biofill(sh);
2229		overlap_clear++;
2230	}
2231
2232	if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
2233		if (level < 6)
2234			tx = ops_run_compute5(sh, percpu);
2235		else {
2236			if (sh->ops.target2 < 0 || sh->ops.target < 0)
2237				tx = ops_run_compute6_1(sh, percpu);
2238			else
2239				tx = ops_run_compute6_2(sh, percpu);
2240		}
2241		/* terminate the chain if reconstruct is not set to be run */
2242		if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
2243			async_tx_ack(tx);
2244	}
2245
2246	if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
2247		if (level < 6)
2248			tx = ops_run_prexor5(sh, percpu, tx);
2249		else
2250			tx = ops_run_prexor6(sh, percpu, tx);
2251	}
2252
2253	if (test_bit(STRIPE_OP_PARTIAL_PARITY, &ops_request))
2254		tx = ops_run_partial_parity(sh, percpu, tx);
2255
2256	if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
2257		tx = ops_run_biodrain(sh, tx);
2258		overlap_clear++;
2259	}
2260
2261	if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
2262		if (level < 6)
2263			ops_run_reconstruct5(sh, percpu, tx);
2264		else
2265			ops_run_reconstruct6(sh, percpu, tx);
2266	}
2267
2268	if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
2269		if (sh->check_state == check_state_run)
2270			ops_run_check_p(sh, percpu);
2271		else if (sh->check_state == check_state_run_q)
2272			ops_run_check_pq(sh, percpu, 0);
2273		else if (sh->check_state == check_state_run_pq)
2274			ops_run_check_pq(sh, percpu, 1);
2275		else
2276			BUG();
2277	}
2278
2279	if (overlap_clear && !sh->batch_head) {
2280		for (i = disks; i--; ) {
2281			struct r5dev *dev = &sh->dev[i];
2282			if (test_and_clear_bit(R5_Overlap, &dev->flags))
2283				wake_up(&sh->raid_conf->wait_for_overlap);
2284		}
2285	}
2286	local_unlock(&conf->percpu->lock);
2287}
2288
2289static void free_stripe(struct kmem_cache *sc, struct stripe_head *sh)
2290{
2291#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2292	kfree(sh->pages);
2293#endif
2294	if (sh->ppl_page)
2295		__free_page(sh->ppl_page);
2296	kmem_cache_free(sc, sh);
2297}
2298
2299static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
2300	int disks, struct r5conf *conf)
2301{
2302	struct stripe_head *sh;
2303
2304	sh = kmem_cache_zalloc(sc, gfp);
2305	if (sh) {
2306		spin_lock_init(&sh->stripe_lock);
2307		spin_lock_init(&sh->batch_lock);
2308		INIT_LIST_HEAD(&sh->batch_list);
2309		INIT_LIST_HEAD(&sh->lru);
2310		INIT_LIST_HEAD(&sh->r5c);
2311		INIT_LIST_HEAD(&sh->log_list);
2312		atomic_set(&sh->count, 1);
2313		sh->raid_conf = conf;
2314		sh->log_start = MaxSector;
2315
2316		if (raid5_has_ppl(conf)) {
2317			sh->ppl_page = alloc_page(gfp);
2318			if (!sh->ppl_page) {
2319				free_stripe(sc, sh);
2320				return NULL;
2321			}
2322		}
2323#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2324		if (init_stripe_shared_pages(sh, conf, disks)) {
2325			free_stripe(sc, sh);
2326			return NULL;
2327		}
2328#endif
2329	}
2330	return sh;
2331}
2332static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
2333{
2334	struct stripe_head *sh;
2335
2336	sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size, conf);
2337	if (!sh)
2338		return 0;
2339
2340	if (grow_buffers(sh, gfp)) {
2341		shrink_buffers(sh);
2342		free_stripe(conf->slab_cache, sh);
2343		return 0;
2344	}
2345	sh->hash_lock_index =
2346		conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
2347	/* we just created an active stripe so... */
2348	atomic_inc(&conf->active_stripes);
2349
2350	raid5_release_stripe(sh);
2351	conf->max_nr_stripes++;
2352	return 1;
2353}
2354
2355static int grow_stripes(struct r5conf *conf, int num)
2356{
2357	struct kmem_cache *sc;
2358	size_t namelen = sizeof(conf->cache_name[0]);
2359	int devs = max(conf->raid_disks, conf->previous_raid_disks);
2360
2361	if (conf->mddev->gendisk)
2362		snprintf(conf->cache_name[0], namelen,
2363			"raid%d-%s", conf->level, mdname(conf->mddev));
2364	else
2365		snprintf(conf->cache_name[0], namelen,
2366			"raid%d-%p", conf->level, conf->mddev);
2367	snprintf(conf->cache_name[1], namelen, "%.27s-alt", conf->cache_name[0]);
2368
2369	conf->active_name = 0;
2370	sc = kmem_cache_create(conf->cache_name[conf->active_name],
2371			       sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
2372			       0, 0, NULL);
2373	if (!sc)
2374		return 1;
2375	conf->slab_cache = sc;
2376	conf->pool_size = devs;
2377	while (num--)
2378		if (!grow_one_stripe(conf, GFP_KERNEL))
2379			return 1;
2380
2381	return 0;
2382}
2383
2384/**
2385 * scribble_alloc - allocate percpu scribble buffer for required size
2386 *		    of the scribble region
2387 * @percpu: from for_each_present_cpu() of the caller
2388 * @num: total number of disks in the array
2389 * @cnt: scribble objs count for required size of the scribble region
2390 *
2391 * The scribble buffer size must be enough to contain:
2392 * 1/ a struct page pointer for each device in the array +2
2393 * 2/ room to convert each entry in (1) to its corresponding dma
2394 *    (dma_map_page()) or page (page_address()) address.
2395 *
2396 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2397 * calculate over all devices (not just the data blocks), using zeros in place
2398 * of the P and Q blocks.
2399 */
2400static int scribble_alloc(struct raid5_percpu *percpu,
2401			  int num, int cnt)
2402{
2403	size_t obj_size =
2404		sizeof(struct page *) * (num + 2) +
2405		sizeof(addr_conv_t) * (num + 2) +
2406		sizeof(unsigned int) * (num + 2);
2407	void *scribble;
2408
2409	/*
2410	 * If here is in raid array suspend context, it is in memalloc noio
2411	 * context as well, there is no potential recursive memory reclaim
2412	 * I/Os with the GFP_KERNEL flag.
2413	 */
2414	scribble = kvmalloc_array(cnt, obj_size, GFP_KERNEL);
2415	if (!scribble)
2416		return -ENOMEM;
2417
2418	kvfree(percpu->scribble);
2419
2420	percpu->scribble = scribble;
2421	percpu->scribble_obj_size = obj_size;
2422	return 0;
2423}
2424
2425static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2426{
2427	unsigned long cpu;
2428	int err = 0;
2429
2430	/*
2431	 * Never shrink. And mddev_suspend() could deadlock if this is called
2432	 * from raid5d. In that case, scribble_disks and scribble_sectors
2433	 * should equal to new_disks and new_sectors
2434	 */
2435	if (conf->scribble_disks >= new_disks &&
2436	    conf->scribble_sectors >= new_sectors)
2437		return 0;
2438	mddev_suspend(conf->mddev);
2439	cpus_read_lock();
2440
2441	for_each_present_cpu(cpu) {
2442		struct raid5_percpu *percpu;
2443
2444		percpu = per_cpu_ptr(conf->percpu, cpu);
2445		err = scribble_alloc(percpu, new_disks,
2446				     new_sectors / RAID5_STRIPE_SECTORS(conf));
2447		if (err)
2448			break;
2449	}
2450
2451	cpus_read_unlock();
2452	mddev_resume(conf->mddev);
2453	if (!err) {
2454		conf->scribble_disks = new_disks;
2455		conf->scribble_sectors = new_sectors;
2456	}
2457	return err;
2458}
2459
2460static int resize_stripes(struct r5conf *conf, int newsize)
2461{
2462	/* Make all the stripes able to hold 'newsize' devices.
2463	 * New slots in each stripe get 'page' set to a new page.
2464	 *
2465	 * This happens in stages:
2466	 * 1/ create a new kmem_cache and allocate the required number of
2467	 *    stripe_heads.
2468	 * 2/ gather all the old stripe_heads and transfer the pages across
2469	 *    to the new stripe_heads.  This will have the side effect of
2470	 *    freezing the array as once all stripe_heads have been collected,
2471	 *    no IO will be possible.  Old stripe heads are freed once their
2472	 *    pages have been transferred over, and the old kmem_cache is
2473	 *    freed when all stripes are done.
2474	 * 3/ reallocate conf->disks to be suitable bigger.  If this fails,
2475	 *    we simple return a failure status - no need to clean anything up.
2476	 * 4/ allocate new pages for the new slots in the new stripe_heads.
2477	 *    If this fails, we don't bother trying the shrink the
2478	 *    stripe_heads down again, we just leave them as they are.
2479	 *    As each stripe_head is processed the new one is released into
2480	 *    active service.
2481	 *
2482	 * Once step2 is started, we cannot afford to wait for a write,
2483	 * so we use GFP_NOIO allocations.
2484	 */
2485	struct stripe_head *osh, *nsh;
2486	LIST_HEAD(newstripes);
2487	struct disk_info *ndisks;
2488	int err = 0;
2489	struct kmem_cache *sc;
2490	int i;
2491	int hash, cnt;
2492
2493	md_allow_write(conf->mddev);
2494
2495	/* Step 1 */
2496	sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2497			       sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
2498			       0, 0, NULL);
2499	if (!sc)
2500		return -ENOMEM;
2501
2502	/* Need to ensure auto-resizing doesn't interfere */
2503	mutex_lock(&conf->cache_size_mutex);
2504
2505	for (i = conf->max_nr_stripes; i; i--) {
2506		nsh = alloc_stripe(sc, GFP_KERNEL, newsize, conf);
2507		if (!nsh)
2508			break;
2509
2510		list_add(&nsh->lru, &newstripes);
2511	}
2512	if (i) {
2513		/* didn't get enough, give up */
2514		while (!list_empty(&newstripes)) {
2515			nsh = list_entry(newstripes.next, struct stripe_head, lru);
2516			list_del(&nsh->lru);
2517			free_stripe(sc, nsh);
2518		}
2519		kmem_cache_destroy(sc);
2520		mutex_unlock(&conf->cache_size_mutex);
2521		return -ENOMEM;
2522	}
2523	/* Step 2 - Must use GFP_NOIO now.
2524	 * OK, we have enough stripes, start collecting inactive
2525	 * stripes and copying them over
2526	 */
2527	hash = 0;
2528	cnt = 0;
2529	list_for_each_entry(nsh, &newstripes, lru) {
2530		lock_device_hash_lock(conf, hash);
2531		wait_event_cmd(conf->wait_for_stripe,
2532				    !list_empty(conf->inactive_list + hash),
2533				    unlock_device_hash_lock(conf, hash),
2534				    lock_device_hash_lock(conf, hash));
2535		osh = get_free_stripe(conf, hash);
2536		unlock_device_hash_lock(conf, hash);
2537
2538#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2539	for (i = 0; i < osh->nr_pages; i++) {
2540		nsh->pages[i] = osh->pages[i];
2541		osh->pages[i] = NULL;
2542	}
2543#endif
2544		for(i=0; i<conf->pool_size; i++) {
2545			nsh->dev[i].page = osh->dev[i].page;
2546			nsh->dev[i].orig_page = osh->dev[i].page;
2547			nsh->dev[i].offset = osh->dev[i].offset;
2548		}
2549		nsh->hash_lock_index = hash;
2550		free_stripe(conf->slab_cache, osh);
2551		cnt++;
2552		if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2553		    !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2554			hash++;
2555			cnt = 0;
2556		}
2557	}
2558	kmem_cache_destroy(conf->slab_cache);
2559
2560	/* Step 3.
2561	 * At this point, we are holding all the stripes so the array
2562	 * is completely stalled, so now is a good time to resize
2563	 * conf->disks and the scribble region
2564	 */
2565	ndisks = kcalloc(newsize, sizeof(struct disk_info), GFP_NOIO);
2566	if (ndisks) {
2567		for (i = 0; i < conf->pool_size; i++)
2568			ndisks[i] = conf->disks[i];
2569
2570		for (i = conf->pool_size; i < newsize; i++) {
2571			ndisks[i].extra_page = alloc_page(GFP_NOIO);
2572			if (!ndisks[i].extra_page)
2573				err = -ENOMEM;
2574		}
2575
2576		if (err) {
2577			for (i = conf->pool_size; i < newsize; i++)
2578				if (ndisks[i].extra_page)
2579					put_page(ndisks[i].extra_page);
2580			kfree(ndisks);
2581		} else {
2582			kfree(conf->disks);
2583			conf->disks = ndisks;
2584		}
2585	} else
2586		err = -ENOMEM;
2587
2588	conf->slab_cache = sc;
2589	conf->active_name = 1-conf->active_name;
2590
2591	/* Step 4, return new stripes to service */
2592	while(!list_empty(&newstripes)) {
2593		nsh = list_entry(newstripes.next, struct stripe_head, lru);
2594		list_del_init(&nsh->lru);
2595
2596#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2597		for (i = 0; i < nsh->nr_pages; i++) {
2598			if (nsh->pages[i])
2599				continue;
2600			nsh->pages[i] = alloc_page(GFP_NOIO);
2601			if (!nsh->pages[i])
2602				err = -ENOMEM;
2603		}
2604
2605		for (i = conf->raid_disks; i < newsize; i++) {
2606			if (nsh->dev[i].page)
2607				continue;
2608			nsh->dev[i].page = raid5_get_dev_page(nsh, i);
2609			nsh->dev[i].orig_page = nsh->dev[i].page;
2610			nsh->dev[i].offset = raid5_get_page_offset(nsh, i);
2611		}
2612#else
2613		for (i=conf->raid_disks; i < newsize; i++)
2614			if (nsh->dev[i].page == NULL) {
2615				struct page *p = alloc_page(GFP_NOIO);
2616				nsh->dev[i].page = p;
2617				nsh->dev[i].orig_page = p;
2618				nsh->dev[i].offset = 0;
2619				if (!p)
2620					err = -ENOMEM;
2621			}
2622#endif
2623		raid5_release_stripe(nsh);
2624	}
2625	/* critical section pass, GFP_NOIO no longer needed */
2626
2627	if (!err)
2628		conf->pool_size = newsize;
2629	mutex_unlock(&conf->cache_size_mutex);
2630
2631	return err;
2632}
2633
2634static int drop_one_stripe(struct r5conf *conf)
2635{
2636	struct stripe_head *sh;
2637	int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
2638
2639	spin_lock_irq(conf->hash_locks + hash);
2640	sh = get_free_stripe(conf, hash);
2641	spin_unlock_irq(conf->hash_locks + hash);
2642	if (!sh)
2643		return 0;
2644	BUG_ON(atomic_read(&sh->count));
2645	shrink_buffers(sh);
2646	free_stripe(conf->slab_cache, sh);
2647	atomic_dec(&conf->active_stripes);
2648	conf->max_nr_stripes--;
2649	return 1;
2650}
2651
2652static void shrink_stripes(struct r5conf *conf)
2653{
2654	while (conf->max_nr_stripes &&
2655	       drop_one_stripe(conf))
2656		;
2657
2658	kmem_cache_destroy(conf->slab_cache);
2659	conf->slab_cache = NULL;
2660}
2661
2662/*
2663 * This helper wraps rcu_dereference_protected() and can be used when
2664 * it is known that the nr_pending of the rdev is elevated.
2665 */
2666static struct md_rdev *rdev_pend_deref(struct md_rdev __rcu *rdev)
2667{
2668	return rcu_dereference_protected(rdev,
2669			atomic_read(&rcu_access_pointer(rdev)->nr_pending));
2670}
2671
2672/*
2673 * This helper wraps rcu_dereference_protected() and should be used
2674 * when it is known that the mddev_lock() is held. This is safe
2675 * seeing raid5_remove_disk() has the same lock held.
2676 */
2677static struct md_rdev *rdev_mdlock_deref(struct mddev *mddev,
2678					 struct md_rdev __rcu *rdev)
2679{
2680	return rcu_dereference_protected(rdev,
2681			lockdep_is_held(&mddev->reconfig_mutex));
2682}
2683
2684static void raid5_end_read_request(struct bio * bi)
2685{
2686	struct stripe_head *sh = bi->bi_private;
2687	struct r5conf *conf = sh->raid_conf;
2688	int disks = sh->disks, i;
2689	struct md_rdev *rdev = NULL;
2690	sector_t s;
2691
2692	for (i=0 ; i<disks; i++)
2693		if (bi == &sh->dev[i].req)
2694			break;
2695
2696	pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
2697		(unsigned long long)sh->sector, i, atomic_read(&sh->count),
2698		bi->bi_status);
2699	if (i == disks) {
2700		BUG();
2701		return;
2702	}
2703	if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
2704		/* If replacement finished while this request was outstanding,
2705		 * 'replacement' might be NULL already.
2706		 * In that case it moved down to 'rdev'.
2707		 * rdev is not removed until all requests are finished.
2708		 */
2709		rdev = rdev_pend_deref(conf->disks[i].replacement);
2710	if (!rdev)
2711		rdev = rdev_pend_deref(conf->disks[i].rdev);
2712
2713	if (use_new_offset(conf, sh))
2714		s = sh->sector + rdev->new_data_offset;
2715	else
2716		s = sh->sector + rdev->data_offset;
2717	if (!bi->bi_status) {
2718		set_bit(R5_UPTODATE, &sh->dev[i].flags);
2719		if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2720			/* Note that this cannot happen on a
2721			 * replacement device.  We just fail those on
2722			 * any error
2723			 */
2724			pr_info_ratelimited(
2725				"md/raid:%s: read error corrected (%lu sectors at %llu on %pg)\n",
2726				mdname(conf->mddev), RAID5_STRIPE_SECTORS(conf),
2727				(unsigned long long)s,
2728				rdev->bdev);
2729			atomic_add(RAID5_STRIPE_SECTORS(conf), &rdev->corrected_errors);
2730			clear_bit(R5_ReadError, &sh->dev[i].flags);
2731			clear_bit(R5_ReWrite, &sh->dev[i].flags);
2732		} else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2733			clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2734
2735		if (test_bit(R5_InJournal, &sh->dev[i].flags))
2736			/*
2737			 * end read for a page in journal, this
2738			 * must be preparing for prexor in rmw
2739			 */
2740			set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
2741
2742		if (atomic_read(&rdev->read_errors))
2743			atomic_set(&rdev->read_errors, 0);
2744	} else {
2745		int retry = 0;
2746		int set_bad = 0;
2747
2748		clear_bit(R5_UPTODATE, &sh->dev[i].flags);
2749		if (!(bi->bi_status == BLK_STS_PROTECTION))
2750			atomic_inc(&rdev->read_errors);
2751		if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
2752			pr_warn_ratelimited(
2753				"md/raid:%s: read error on replacement device (sector %llu on %pg).\n",
2754				mdname(conf->mddev),
2755				(unsigned long long)s,
2756				rdev->bdev);
2757		else if (conf->mddev->degraded >= conf->max_degraded) {
2758			set_bad = 1;
2759			pr_warn_ratelimited(
2760				"md/raid:%s: read error not correctable (sector %llu on %pg).\n",
2761				mdname(conf->mddev),
2762				(unsigned long long)s,
2763				rdev->bdev);
2764		} else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
2765			/* Oh, no!!! */
2766			set_bad = 1;
2767			pr_warn_ratelimited(
2768				"md/raid:%s: read error NOT corrected!! (sector %llu on %pg).\n",
2769				mdname(conf->mddev),
2770				(unsigned long long)s,
2771				rdev->bdev);
2772		} else if (atomic_read(&rdev->read_errors)
2773			 > conf->max_nr_stripes) {
2774			if (!test_bit(Faulty, &rdev->flags)) {
2775				pr_warn("md/raid:%s: %d read_errors > %d stripes\n",
2776				    mdname(conf->mddev),
2777				    atomic_read(&rdev->read_errors),
2778				    conf->max_nr_stripes);
2779				pr_warn("md/raid:%s: Too many read errors, failing device %pg.\n",
2780				    mdname(conf->mddev), rdev->bdev);
2781			}
2782		} else
2783			retry = 1;
2784		if (set_bad && test_bit(In_sync, &rdev->flags)
2785		    && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2786			retry = 1;
2787		if (retry)
2788			if (sh->qd_idx >= 0 && sh->pd_idx == i)
2789				set_bit(R5_ReadError, &sh->dev[i].flags);
2790			else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2791				set_bit(R5_ReadError, &sh->dev[i].flags);
2792				clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2793			} else
2794				set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2795		else {
2796			clear_bit(R5_ReadError, &sh->dev[i].flags);
2797			clear_bit(R5_ReWrite, &sh->dev[i].flags);
2798			if (!(set_bad
2799			      && test_bit(In_sync, &rdev->flags)
2800			      && rdev_set_badblocks(
2801				      rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), 0)))
2802				md_error(conf->mddev, rdev);
2803		}
2804	}
2805	rdev_dec_pending(rdev, conf->mddev);
2806	bio_uninit(bi);
2807	clear_bit(R5_LOCKED, &sh->dev[i].flags);
2808	set_bit(STRIPE_HANDLE, &sh->state);
2809	raid5_release_stripe(sh);
2810}
2811
2812static void raid5_end_write_request(struct bio *bi)
2813{
2814	struct stripe_head *sh = bi->bi_private;
2815	struct r5conf *conf = sh->raid_conf;
2816	int disks = sh->disks, i;
2817	struct md_rdev *rdev;
2818	sector_t first_bad;
2819	int bad_sectors;
2820	int replacement = 0;
2821
2822	for (i = 0 ; i < disks; i++) {
2823		if (bi == &sh->dev[i].req) {
2824			rdev = rdev_pend_deref(conf->disks[i].rdev);
2825			break;
2826		}
2827		if (bi == &sh->dev[i].rreq) {
2828			rdev = rdev_pend_deref(conf->disks[i].replacement);
2829			if (rdev)
2830				replacement = 1;
2831			else
2832				/* rdev was removed and 'replacement'
2833				 * replaced it.  rdev is not removed
2834				 * until all requests are finished.
2835				 */
2836				rdev = rdev_pend_deref(conf->disks[i].rdev);
2837			break;
2838		}
2839	}
2840	pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
2841		(unsigned long long)sh->sector, i, atomic_read(&sh->count),
2842		bi->bi_status);
2843	if (i == disks) {
2844		BUG();
2845		return;
2846	}
2847
2848	if (replacement) {
2849		if (bi->bi_status)
2850			md_error(conf->mddev, rdev);
2851		else if (is_badblock(rdev, sh->sector,
2852				     RAID5_STRIPE_SECTORS(conf),
2853				     &first_bad, &bad_sectors))
2854			set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2855	} else {
2856		if (bi->bi_status) {
2857			set_bit(STRIPE_DEGRADED, &sh->state);
2858			set_bit(WriteErrorSeen, &rdev->flags);
2859			set_bit(R5_WriteError, &sh->dev[i].flags);
2860			if (!test_and_set_bit(WantReplacement, &rdev->flags))
2861				set_bit(MD_RECOVERY_NEEDED,
2862					&rdev->mddev->recovery);
2863		} else if (is_badblock(rdev, sh->sector,
2864				       RAID5_STRIPE_SECTORS(conf),
2865				       &first_bad, &bad_sectors)) {
2866			set_bit(R5_MadeGood, &sh->dev[i].flags);
2867			if (test_bit(R5_ReadError, &sh->dev[i].flags))
2868				/* That was a successful write so make
2869				 * sure it looks like we already did
2870				 * a re-write.
2871				 */
2872				set_bit(R5_ReWrite, &sh->dev[i].flags);
2873		}
2874	}
2875	rdev_dec_pending(rdev, conf->mddev);
2876
2877	if (sh->batch_head && bi->bi_status && !replacement)
2878		set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2879
2880	bio_uninit(bi);
2881	if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2882		clear_bit(R5_LOCKED, &sh->dev[i].flags);
2883	set_bit(STRIPE_HANDLE, &sh->state);
2884	raid5_release_stripe(sh);
2885
2886	if (sh->batch_head && sh != sh->batch_head)
2887		raid5_release_stripe(sh->batch_head);
2888}
2889
2890static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
2891{
2892	struct r5conf *conf = mddev->private;
2893	unsigned long flags;
2894	pr_debug("raid456: error called\n");
2895
2896	pr_crit("md/raid:%s: Disk failure on %pg, disabling device.\n",
2897		mdname(mddev), rdev->bdev);
2898
2899	spin_lock_irqsave(&conf->device_lock, flags);
2900	set_bit(Faulty, &rdev->flags);
2901	clear_bit(In_sync, &rdev->flags);
2902	mddev->degraded = raid5_calc_degraded(conf);
2903
2904	if (has_failed(conf)) {
2905		set_bit(MD_BROKEN, &conf->mddev->flags);
2906		conf->recovery_disabled = mddev->recovery_disabled;
2907
2908		pr_crit("md/raid:%s: Cannot continue operation (%d/%d failed).\n",
2909			mdname(mddev), mddev->degraded, conf->raid_disks);
2910	} else {
2911		pr_crit("md/raid:%s: Operation continuing on %d devices.\n",
2912			mdname(mddev), conf->raid_disks - mddev->degraded);
2913	}
2914
2915	spin_unlock_irqrestore(&conf->device_lock, flags);
2916	set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2917
2918	set_bit(Blocked, &rdev->flags);
2919	set_mask_bits(&mddev->sb_flags, 0,
2920		      BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
2921	r5c_update_on_rdev_error(mddev, rdev);
2922}
2923
2924/*
2925 * Input: a 'big' sector number,
2926 * Output: index of the data and parity disk, and the sector # in them.
2927 */
2928sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2929			      int previous, int *dd_idx,
2930			      struct stripe_head *sh)
2931{
2932	sector_t stripe, stripe2;
2933	sector_t chunk_number;
2934	unsigned int chunk_offset;
2935	int pd_idx, qd_idx;
2936	int ddf_layout = 0;
2937	sector_t new_sector;
2938	int algorithm = previous ? conf->prev_algo
2939				 : conf->algorithm;
2940	int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2941					 : conf->chunk_sectors;
2942	int raid_disks = previous ? conf->previous_raid_disks
2943				  : conf->raid_disks;
2944	int data_disks = raid_disks - conf->max_degraded;
2945
2946	/* First compute the information on this sector */
2947
2948	/*
2949	 * Compute the chunk number and the sector offset inside the chunk
2950	 */
2951	chunk_offset = sector_div(r_sector, sectors_per_chunk);
2952	chunk_number = r_sector;
2953
2954	/*
2955	 * Compute the stripe number
2956	 */
2957	stripe = chunk_number;
2958	*dd_idx = sector_div(stripe, data_disks);
2959	stripe2 = stripe;
2960	/*
2961	 * Select the parity disk based on the user selected algorithm.
2962	 */
2963	pd_idx = qd_idx = -1;
2964	switch(conf->level) {
2965	case 4:
2966		pd_idx = data_disks;
2967		break;
2968	case 5:
2969		switch (algorithm) {
2970		case ALGORITHM_LEFT_ASYMMETRIC:
2971			pd_idx = data_disks - sector_div(stripe2, raid_disks);
2972			if (*dd_idx >= pd_idx)
2973				(*dd_idx)++;
2974			break;
2975		case ALGORITHM_RIGHT_ASYMMETRIC:
2976			pd_idx = sector_div(stripe2, raid_disks);
2977			if (*dd_idx >= pd_idx)
2978				(*dd_idx)++;
2979			break;
2980		case ALGORITHM_LEFT_SYMMETRIC:
2981			pd_idx = data_disks - sector_div(stripe2, raid_disks);
2982			*dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2983			break;
2984		case ALGORITHM_RIGHT_SYMMETRIC:
2985			pd_idx = sector_div(stripe2, raid_disks);
2986			*dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2987			break;
2988		case ALGORITHM_PARITY_0:
2989			pd_idx = 0;
2990			(*dd_idx)++;
2991			break;
2992		case ALGORITHM_PARITY_N:
2993			pd_idx = data_disks;
2994			break;
2995		default:
2996			BUG();
2997		}
2998		break;
2999	case 6:
3000
3001		switch (algorithm) {
3002		case ALGORITHM_LEFT_ASYMMETRIC:
3003			pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
3004			qd_idx = pd_idx + 1;
3005			if (pd_idx == raid_disks-1) {
3006				(*dd_idx)++;	/* Q D D D P */
3007				qd_idx = 0;
3008			} else if (*dd_idx >= pd_idx)
3009				(*dd_idx) += 2; /* D D P Q D */
3010			break;
3011		case ALGORITHM_RIGHT_ASYMMETRIC:
3012			pd_idx = sector_div(stripe2, raid_disks);
3013			qd_idx = pd_idx + 1;
3014			if (pd_idx == raid_disks-1) {
3015				(*dd_idx)++;	/* Q D D D P */
3016				qd_idx = 0;
3017			} else if (*dd_idx >= pd_idx)
3018				(*dd_idx) += 2; /* D D P Q D */
3019			break;
3020		case ALGORITHM_LEFT_SYMMETRIC:
3021			pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
3022			qd_idx = (pd_idx + 1) % raid_disks;
3023			*dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
3024			break;
3025		case ALGORITHM_RIGHT_SYMMETRIC:
3026			pd_idx = sector_div(stripe2, raid_disks);
3027			qd_idx = (pd_idx + 1) % raid_disks;
3028			*dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
3029			break;
3030
3031		case ALGORITHM_PARITY_0:
3032			pd_idx = 0;
3033			qd_idx = 1;
3034			(*dd_idx) += 2;
3035			break;
3036		case ALGORITHM_PARITY_N:
3037			pd_idx = data_disks;
3038			qd_idx = data_disks + 1;
3039			break;
3040
3041		case ALGORITHM_ROTATING_ZERO_RESTART:
3042			/* Exactly the same as RIGHT_ASYMMETRIC, but or
3043			 * of blocks for computing Q is different.
3044			 */
3045			pd_idx = sector_div(stripe2, raid_disks);
3046			qd_idx = pd_idx + 1;
3047			if (pd_idx == raid_disks-1) {
3048				(*dd_idx)++;	/* Q D D D P */
3049				qd_idx = 0;
3050			} else if (*dd_idx >= pd_idx)
3051				(*dd_idx) += 2; /* D D P Q D */
3052			ddf_layout = 1;
3053			break;
3054
3055		case ALGORITHM_ROTATING_N_RESTART:
3056			/* Same a left_asymmetric, by first stripe is
3057			 * D D D P Q  rather than
3058			 * Q D D D P
3059			 */
3060			stripe2 += 1;
3061			pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
3062			qd_idx = pd_idx + 1;
3063			if (pd_idx == raid_disks-1) {
3064				(*dd_idx)++;	/* Q D D D P */
3065				qd_idx = 0;
3066			} else if (*dd_idx >= pd_idx)
3067				(*dd_idx) += 2; /* D D P Q D */
3068			ddf_layout = 1;
3069			break;
3070
3071		case ALGORITHM_ROTATING_N_CONTINUE:
3072			/* Same as left_symmetric but Q is before P */
3073			pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
3074			qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
3075			*dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
3076			ddf_layout = 1;
3077			break;
3078
3079		case ALGORITHM_LEFT_ASYMMETRIC_6:
3080			/* RAID5 left_asymmetric, with Q on last device */
3081			pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
3082			if (*dd_idx >= pd_idx)
3083				(*dd_idx)++;
3084			qd_idx = raid_disks - 1;
3085			break;
3086
3087		case ALGORITHM_RIGHT_ASYMMETRIC_6:
3088			pd_idx = sector_div(stripe2, raid_disks-1);
3089			if (*dd_idx >= pd_idx)
3090				(*dd_idx)++;
3091			qd_idx = raid_disks - 1;
3092			break;
3093
3094		case ALGORITHM_LEFT_SYMMETRIC_6:
3095			pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
3096			*dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
3097			qd_idx = raid_disks - 1;
3098			break;
3099
3100		case ALGORITHM_RIGHT_SYMMETRIC_6:
3101			pd_idx = sector_div(stripe2, raid_disks-1);
3102			*dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
3103			qd_idx = raid_disks - 1;
3104			break;
3105
3106		case ALGORITHM_PARITY_0_6:
3107			pd_idx = 0;
3108			(*dd_idx)++;
3109			qd_idx = raid_disks - 1;
3110			break;
3111
3112		default:
3113			BUG();
3114		}
3115		break;
3116	}
3117
3118	if (sh) {
3119		sh->pd_idx = pd_idx;
3120		sh->qd_idx = qd_idx;
3121		sh->ddf_layout = ddf_layout;
3122	}
3123	/*
3124	 * Finally, compute the new sector number
3125	 */
3126	new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
3127	return new_sector;
3128}
3129
3130sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
3131{
3132	struct r5conf *conf = sh->raid_conf;
3133	int raid_disks = sh->disks;
3134	int data_disks = raid_disks - conf->max_degraded;
3135	sector_t new_sector = sh->sector, check;
3136	int sectors_per_chunk = previous ? conf->prev_chunk_sectors
3137					 : conf->chunk_sectors;
3138	int algorithm = previous ? conf->prev_algo
3139				 : conf->algorithm;
3140	sector_t stripe;
3141	int chunk_offset;
3142	sector_t chunk_number;
3143	int dummy1, dd_idx = i;
3144	sector_t r_sector;
3145	struct stripe_head sh2;
3146
3147	chunk_offset = sector_div(new_sector, sectors_per_chunk);
3148	stripe = new_sector;
3149
3150	if (i == sh->pd_idx)
3151		return 0;
3152	switch(conf->level) {
3153	case 4: break;
3154	case 5:
3155		switch (algorithm) {
3156		case ALGORITHM_LEFT_ASYMMETRIC:
3157		case ALGORITHM_RIGHT_ASYMMETRIC:
3158			if (i > sh->pd_idx)
3159				i--;
3160			break;
3161		case ALGORITHM_LEFT_SYMMETRIC:
3162		case ALGORITHM_RIGHT_SYMMETRIC:
3163			if (i < sh->pd_idx)
3164				i += raid_disks;
3165			i -= (sh->pd_idx + 1);
3166			break;
3167		case ALGORITHM_PARITY_0:
3168			i -= 1;
3169			break;
3170		case ALGORITHM_PARITY_N:
3171			break;
3172		default:
3173			BUG();
3174		}
3175		break;
3176	case 6:
3177		if (i == sh->qd_idx)
3178			return 0; /* It is the Q disk */
3179		switch (algorithm) {
3180		case ALGORITHM_LEFT_ASYMMETRIC:
3181		case ALGORITHM_RIGHT_ASYMMETRIC:
3182		case ALGORITHM_ROTATING_ZERO_RESTART:
3183		case ALGORITHM_ROTATING_N_RESTART:
3184			if (sh->pd_idx == raid_disks-1)
3185				i--;	/* Q D D D P */
3186			else if (i > sh->pd_idx)
3187				i -= 2; /* D D P Q D */
3188			break;
3189		case ALGORITHM_LEFT_SYMMETRIC:
3190		case ALGORITHM_RIGHT_SYMMETRIC:
3191			if (sh->pd_idx == raid_disks-1)
3192				i--; /* Q D D D P */
3193			else {
3194				/* D D P Q D */
3195				if (i < sh->pd_idx)
3196					i += raid_disks;
3197				i -= (sh->pd_idx + 2);
3198			}
3199			break;
3200		case ALGORITHM_PARITY_0:
3201			i -= 2;
3202			break;
3203		case ALGORITHM_PARITY_N:
3204			break;
3205		case ALGORITHM_ROTATING_N_CONTINUE:
3206			/* Like left_symmetric, but P is before Q */
3207			if (sh->pd_idx == 0)
3208				i--;	/* P D D D Q */
3209			else {
3210				/* D D Q P D */
3211				if (i < sh->pd_idx)
3212					i += raid_disks;
3213				i -= (sh->pd_idx + 1);
3214			}
3215			break;
3216		case ALGORITHM_LEFT_ASYMMETRIC_6:
3217		case ALGORITHM_RIGHT_ASYMMETRIC_6:
3218			if (i > sh->pd_idx)
3219				i--;
3220			break;
3221		case ALGORITHM_LEFT_SYMMETRIC_6:
3222		case ALGORITHM_RIGHT_SYMMETRIC_6:
3223			if (i < sh->pd_idx)
3224				i += data_disks + 1;
3225			i -= (sh->pd_idx + 1);
3226			break;
3227		case ALGORITHM_PARITY_0_6:
3228			i -= 1;
3229			break;
3230		default:
3231			BUG();
3232		}
3233		break;
3234	}
3235
3236	chunk_number = stripe * data_disks + i;
3237	r_sector = chunk_number * sectors_per_chunk + chunk_offset;
3238
3239	check = raid5_compute_sector(conf, r_sector,
3240				     previous, &dummy1, &sh2);
3241	if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
3242		|| sh2.qd_idx != sh->qd_idx) {
3243		pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
3244			mdname(conf->mddev));
3245		return 0;
3246	}
3247	return r_sector;
3248}
3249
3250/*
3251 * There are cases where we want handle_stripe_dirtying() and
3252 * schedule_reconstruction() to delay towrite to some dev of a stripe.
3253 *
3254 * This function checks whether we want to delay the towrite. Specifically,
3255 * we delay the towrite when:
3256 *
3257 *   1. degraded stripe has a non-overwrite to the missing dev, AND this
3258 *      stripe has data in journal (for other devices).
3259 *
3260 *      In this case, when reading data for the non-overwrite dev, it is
3261 *      necessary to handle complex rmw of write back cache (prexor with
3262 *      orig_page, and xor with page). To keep read path simple, we would
3263 *      like to flush data in journal to RAID disks first, so complex rmw
3264 *      is handled in the write patch (handle_stripe_dirtying).
3265 *
3266 *   2. when journal space is critical (R5C_LOG_CRITICAL=1)
3267 *
3268 *      It is important to be able to flush all stripes in raid5-cache.
3269 *      Therefore, we need reserve some space on the journal device for
3270 *      these flushes. If flush operation includes pending writes to the
3271 *      stripe, we need to reserve (conf->raid_disk + 1) pages per stripe
3272 *      for the flush out. If we exclude these pending writes from flush
3273 *      operation, we only need (conf->max_degraded + 1) pages per stripe.
3274 *      Therefore, excluding pending writes in these cases enables more
3275 *      efficient use of the journal device.
3276 *
3277 *      Note: To make sure the stripe makes progress, we only delay
3278 *      towrite for stripes with data already in journal (injournal > 0).
3279 *      When LOG_CRITICAL, stripes with injournal == 0 will be sent to
3280 *      no_space_stripes list.
3281 *
3282 *   3. during journal failure
3283 *      In journal failure, we try to flush all cached data to raid disks
3284 *      based on data in stripe cache. The array is read-only to upper
3285 *      layers, so we would skip all pending writes.
3286 *
3287 */
3288static inline bool delay_towrite(struct r5conf *conf,
3289				 struct r5dev *dev,
3290				 struct stripe_head_state *s)
3291{
3292	/* case 1 above */
3293	if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3294	    !test_bit(R5_Insync, &dev->flags) && s->injournal)
3295		return true;
3296	/* case 2 above */
3297	if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
3298	    s->injournal > 0)
3299		return true;
3300	/* case 3 above */
3301	if (s->log_failed && s->injournal)
3302		return true;
3303	return false;
3304}
3305
3306static void
3307schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
3308			 int rcw, int expand)
3309{
3310	int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
3311	struct r5conf *conf = sh->raid_conf;
3312	int level = conf->level;
3313
3314	if (rcw) {
3315		/*
3316		 * In some cases, handle_stripe_dirtying initially decided to
3317		 * run rmw and allocates extra page for prexor. However, rcw is
3318		 * cheaper later on. We need to free the extra page now,
3319		 * because we won't be able to do that in ops_complete_prexor().
3320		 */
3321		r5c_release_extra_page(sh);
3322
3323		for (i = disks; i--; ) {
3324			struct r5dev *dev = &sh->dev[i];
3325
3326			if (dev->towrite && !delay_towrite(conf, dev, s)) {
3327				set_bit(R5_LOCKED, &dev->flags);
3328				set_bit(R5_Wantdrain, &dev->flags);
3329				if (!expand)
3330					clear_bit(R5_UPTODATE, &dev->flags);
3331				s->locked++;
3332			} else if (test_bit(R5_InJournal, &dev->flags)) {
3333				set_bit(R5_LOCKED, &dev->flags);
3334				s->locked++;
3335			}
3336		}
3337		/* if we are not expanding this is a proper write request, and
3338		 * there will be bios with new data to be drained into the
3339		 * stripe cache
3340		 */
3341		if (!expand) {
3342			if (!s->locked)
3343				/* False alarm, nothing to do */
3344				return;
3345			sh->reconstruct_state = reconstruct_state_drain_run;
3346			set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3347		} else
3348			sh->reconstruct_state = reconstruct_state_run;
3349
3350		set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3351
3352		if (s->locked + conf->max_degraded == disks)
3353			if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
3354				atomic_inc(&conf->pending_full_writes);
3355	} else {
3356		BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
3357			test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
3358		BUG_ON(level == 6 &&
3359			(!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
3360			   test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
3361
3362		for (i = disks; i--; ) {
3363			struct r5dev *dev = &sh->dev[i];
3364			if (i == pd_idx || i == qd_idx)
3365				continue;
3366
3367			if (dev->towrite &&
3368			    (test_bit(R5_UPTODATE, &dev->flags) ||
3369			     test_bit(R5_Wantcompute, &dev->flags))) {
3370				set_bit(R5_Wantdrain, &dev->flags);
3371				set_bit(R5_LOCKED, &dev->flags);
3372				clear_bit(R5_UPTODATE, &dev->flags);
3373				s->locked++;
3374			} else if (test_bit(R5_InJournal, &dev->flags)) {
3375				set_bit(R5_LOCKED, &dev->flags);
3376				s->locked++;
3377			}
3378		}
3379		if (!s->locked)
3380			/* False alarm - nothing to do */
3381			return;
3382		sh->reconstruct_state = reconstruct_state_prexor_drain_run;
3383		set_bit(STRIPE_OP_PREXOR, &s->ops_request);
3384		set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3385		set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3386	}
3387
3388	/* keep the parity disk(s) locked while asynchronous operations
3389	 * are in flight
3390	 */
3391	set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
3392	clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3393	s->locked++;
3394
3395	if (level == 6) {
3396		int qd_idx = sh->qd_idx;
3397		struct r5dev *dev = &sh->dev[qd_idx];
3398
3399		set_bit(R5_LOCKED, &dev->flags);
3400		clear_bit(R5_UPTODATE, &dev->flags);
3401		s->locked++;
3402	}
3403
3404	if (raid5_has_ppl(sh->raid_conf) && sh->ppl_page &&
3405	    test_bit(STRIPE_OP_BIODRAIN, &s->ops_request) &&
3406	    !test_bit(STRIPE_FULL_WRITE, &sh->state) &&
3407	    test_bit(R5_Insync, &sh->dev[pd_idx].flags))
3408		set_bit(STRIPE_OP_PARTIAL_PARITY, &s->ops_request);
3409
3410	pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
3411		__func__, (unsigned long long)sh->sector,
3412		s->locked, s->ops_request);
3413}
3414
3415/*
3416 * Each stripe/dev can have one or more bion attached.
3417 * toread/towrite point to the first in a chain.
3418 * The bi_next chain must be in order.
3419 */
3420static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
3421			  int forwrite, int previous)
3422{
3423	struct bio **bip;
3424	struct r5conf *conf = sh->raid_conf;
3425	int firstwrite=0;
3426
3427	pr_debug("adding bi b#%llu to stripe s#%llu\n",
3428		(unsigned long long)bi->bi_iter.bi_sector,
3429		(unsigned long long)sh->sector);
3430
3431	spin_lock_irq(&sh->stripe_lock);
3432	/* Don't allow new IO added to stripes in batch list */
3433	if (sh->batch_head)
3434		goto overlap;
3435	if (forwrite) {
3436		bip = &sh->dev[dd_idx].towrite;
3437		if (*bip == NULL)
3438			firstwrite = 1;
3439	} else
3440		bip = &sh->dev[dd_idx].toread;
3441	while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
3442		if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
3443			goto overlap;
3444		bip = & (*bip)->bi_next;
3445	}
3446	if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
3447		goto overlap;
3448
3449	if (forwrite && raid5_has_ppl(conf)) {
3450		/*
3451		 * With PPL only writes to consecutive data chunks within a
3452		 * stripe are allowed because for a single stripe_head we can
3453		 * only have one PPL entry at a time, which describes one data
3454		 * range. Not really an overlap, but wait_for_overlap can be
3455		 * used to handle this.
3456		 */
3457		sector_t sector;
3458		sector_t first = 0;
3459		sector_t last = 0;
3460		int count = 0;
3461		int i;
3462
3463		for (i = 0; i < sh->disks; i++) {
3464			if (i != sh->pd_idx &&
3465			    (i == dd_idx || sh->dev[i].towrite)) {
3466				sector = sh->dev[i].sector;
3467				if (count == 0 || sector < first)
3468					first = sector;
3469				if (sector > last)
3470					last = sector;
3471				count++;
3472			}
3473		}
3474
3475		if (first + conf->chunk_sectors * (count - 1) != last)
3476			goto overlap;
3477	}
3478
3479	if (!forwrite || previous)
3480		clear_bit(STRIPE_BATCH_READY, &sh->state);
3481
3482	BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
3483	if (*bip)
3484		bi->bi_next = *bip;
3485	*bip = bi;
3486	bio_inc_remaining(bi);
3487	md_write_inc(conf->mddev, bi);
3488
3489	if (forwrite) {
3490		/* check if page is covered */
3491		sector_t sector = sh->dev[dd_idx].sector;
3492		for (bi=sh->dev[dd_idx].towrite;
3493		     sector < sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf) &&
3494			     bi && bi->bi_iter.bi_sector <= sector;
3495		     bi = r5_next_bio(conf, bi, sh->dev[dd_idx].sector)) {
3496			if (bio_end_sector(bi) >= sector)
3497				sector = bio_end_sector(bi);
3498		}
3499		if (sector >= sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf))
3500			if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
3501				sh->overwrite_disks++;
3502	}
3503
3504	pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
3505		(unsigned long long)(*bip)->bi_iter.bi_sector,
3506		(unsigned long long)sh->sector, dd_idx);
3507
3508	if (conf->mddev->bitmap && firstwrite) {
3509		/* Cannot hold spinlock over bitmap_startwrite,
3510		 * but must ensure this isn't added to a batch until
3511		 * we have added to the bitmap and set bm_seq.
3512		 * So set STRIPE_BITMAP_PENDING to prevent
3513		 * batching.
3514		 * If multiple add_stripe_bio() calls race here they
3515		 * much all set STRIPE_BITMAP_PENDING.  So only the first one
3516		 * to complete "bitmap_startwrite" gets to set
3517		 * STRIPE_BIT_DELAY.  This is important as once a stripe
3518		 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3519		 * any more.
3520		 */
3521		set_bit(STRIPE_BITMAP_PENDING, &sh->state);
3522		spin_unlock_irq(&sh->stripe_lock);
3523		md_bitmap_startwrite(conf->mddev->bitmap, sh->sector,
3524				     RAID5_STRIPE_SECTORS(conf), 0);
3525		spin_lock_irq(&sh->stripe_lock);
3526		clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
3527		if (!sh->batch_head) {
3528			sh->bm_seq = conf->seq_flush+1;
3529			set_bit(STRIPE_BIT_DELAY, &sh->state);
3530		}
3531	}
3532	spin_unlock_irq(&sh->stripe_lock);
3533
3534	if (stripe_can_batch(sh))
3535		stripe_add_to_batch_list(conf, sh);
3536	return 1;
3537
3538 overlap:
3539	set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
3540	spin_unlock_irq(&sh->stripe_lock);
3541	return 0;
3542}
3543
3544static void end_reshape(struct r5conf *conf);
3545
3546static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
3547			    struct stripe_head *sh)
3548{
3549	int sectors_per_chunk =
3550		previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
3551	int dd_idx;
3552	int chunk_offset = sector_div(stripe, sectors_per_chunk);
3553	int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
3554
3555	raid5_compute_sector(conf,
3556			     stripe * (disks - conf->max_degraded)
3557			     *sectors_per_chunk + chunk_offset,
3558			     previous,
3559			     &dd_idx, sh);
3560}
3561
3562static void
3563handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
3564		     struct stripe_head_state *s, int disks)
3565{
3566	int i;
3567	BUG_ON(sh->batch_head);
3568	for (i = disks; i--; ) {
3569		struct bio *bi;
3570		int bitmap_end = 0;
3571
3572		if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
3573			struct md_rdev *rdev;
3574			rcu_read_lock();
3575			rdev = rcu_dereference(conf->disks[i].rdev);
3576			if (rdev && test_bit(In_sync, &rdev->flags) &&
3577			    !test_bit(Faulty, &rdev->flags))
3578				atomic_inc(&rdev->nr_pending);
3579			else
3580				rdev = NULL;
3581			rcu_read_unlock();
3582			if (rdev) {
3583				if (!rdev_set_badblocks(
3584					    rdev,
3585					    sh->sector,
3586					    RAID5_STRIPE_SECTORS(conf), 0))
3587					md_error(conf->mddev, rdev);
3588				rdev_dec_pending(rdev, conf->mddev);
3589			}
3590		}
3591		spin_lock_irq(&sh->stripe_lock);
3592		/* fail all writes first */
3593		bi = sh->dev[i].towrite;
3594		sh->dev[i].towrite = NULL;
3595		sh->overwrite_disks = 0;
3596		spin_unlock_irq(&sh->stripe_lock);
3597		if (bi)
3598			bitmap_end = 1;
3599
3600		log_stripe_write_finished(sh);
3601
3602		if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3603			wake_up(&conf->wait_for_overlap);
3604
3605		while (bi && bi->bi_iter.bi_sector <
3606			sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3607			struct bio *nextbi = r5_next_bio(conf, bi, sh->dev[i].sector);
3608
3609			md_write_end(conf->mddev);
3610			bio_io_error(bi);
3611			bi = nextbi;
3612		}
3613		if (bitmap_end)
3614			md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3615					   RAID5_STRIPE_SECTORS(conf), 0, 0);
3616		bitmap_end = 0;
3617		/* and fail all 'written' */
3618		bi = sh->dev[i].written;
3619		sh->dev[i].written = NULL;
3620		if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3621			WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3622			sh->dev[i].page = sh->dev[i].orig_page;
3623		}
3624
3625		if (bi) bitmap_end = 1;
3626		while (bi && bi->bi_iter.bi_sector <
3627		       sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3628			struct bio *bi2 = r5_next_bio(conf, bi, sh->dev[i].sector);
3629
3630			md_write_end(conf->mddev);
3631			bio_io_error(bi);
3632			bi = bi2;
3633		}
3634
3635		/* fail any reads if this device is non-operational and
3636		 * the data has not reached the cache yet.
3637		 */
3638		if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
3639		    s->failed > conf->max_degraded &&
3640		    (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3641		      test_bit(R5_ReadError, &sh->dev[i].flags))) {
3642			spin_lock_irq(&sh->stripe_lock);
3643			bi = sh->dev[i].toread;
3644			sh->dev[i].toread = NULL;
3645			spin_unlock_irq(&sh->stripe_lock);
3646			if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3647				wake_up(&conf->wait_for_overlap);
3648			if (bi)
3649				s->to_read--;
3650			while (bi && bi->bi_iter.bi_sector <
3651			       sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3652				struct bio *nextbi =
3653					r5_next_bio(conf, bi, sh->dev[i].sector);
3654
3655				bio_io_error(bi);
3656				bi = nextbi;
3657			}
3658		}
3659		if (bitmap_end)
3660			md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3661					   RAID5_STRIPE_SECTORS(conf), 0, 0);
3662		/* If we were in the middle of a write the parity block might
3663		 * still be locked - so just clear all R5_LOCKED flags
3664		 */
3665		clear_bit(R5_LOCKED, &sh->dev[i].flags);
3666	}
3667	s->to_write = 0;
3668	s->written = 0;
3669
3670	if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3671		if (atomic_dec_and_test(&conf->pending_full_writes))
3672			md_wakeup_thread(conf->mddev->thread);
3673}
3674
3675static void
3676handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
3677		   struct stripe_head_state *s)
3678{
3679	int abort = 0;
3680	int i;
3681
3682	BUG_ON(sh->batch_head);
3683	clear_bit(STRIPE_SYNCING, &sh->state);
3684	if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3685		wake_up(&conf->wait_for_overlap);
3686	s->syncing = 0;
3687	s->replacing = 0;
3688	/* There is nothing more to do for sync/check/repair.
3689	 * Don't even need to abort as that is handled elsewhere
3690	 * if needed, and not always wanted e.g. if there is a known
3691	 * bad block here.
3692	 * For recover/replace we need to record a bad block on all
3693	 * non-sync devices, or abort the recovery
3694	 */
3695	if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3696		/* During recovery devices cannot be removed, so
3697		 * locking and refcounting of rdevs is not needed
3698		 */
3699		rcu_read_lock();
3700		for (i = 0; i < conf->raid_disks; i++) {
3701			struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
3702			if (rdev
3703			    && !test_bit(Faulty, &rdev->flags)
3704			    && !test_bit(In_sync, &rdev->flags)
3705			    && !rdev_set_badblocks(rdev, sh->sector,
3706						   RAID5_STRIPE_SECTORS(conf), 0))
3707				abort = 1;
3708			rdev = rcu_dereference(conf->disks[i].replacement);
3709			if (rdev
3710			    && !test_bit(Faulty, &rdev->flags)
3711			    && !test_bit(In_sync, &rdev->flags)
3712			    && !rdev_set_badblocks(rdev, sh->sector,
3713						   RAID5_STRIPE_SECTORS(conf), 0))
3714				abort = 1;
3715		}
3716		rcu_read_unlock();
3717		if (abort)
3718			conf->recovery_disabled =
3719				conf->mddev->recovery_disabled;
3720	}
3721	md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), !abort);
3722}
3723
3724static int want_replace(struct stripe_head *sh, int disk_idx)
3725{
3726	struct md_rdev *rdev;
3727	int rv = 0;
3728
3729	rcu_read_lock();
3730	rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
3731	if (rdev
3732	    && !test_bit(Faulty, &rdev->flags)
3733	    && !test_bit(In_sync, &rdev->flags)
3734	    && (rdev->recovery_offset <= sh->sector
3735		|| rdev->mddev->recovery_cp <= sh->sector))
3736		rv = 1;
3737	rcu_read_unlock();
3738	return rv;
3739}
3740
3741static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3742			   int disk_idx, int disks)
3743{
3744	struct r5dev *dev = &sh->dev[disk_idx];
3745	struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3746				  &sh->dev[s->failed_num[1]] };
3747	int i;
3748	bool force_rcw = (sh->raid_conf->rmw_level == PARITY_DISABLE_RMW);
3749
3750
3751	if (test_bit(R5_LOCKED, &dev->flags) ||
3752	    test_bit(R5_UPTODATE, &dev->flags))
3753		/* No point reading this as we already have it or have
3754		 * decided to get it.
3755		 */
3756		return 0;
3757
3758	if (dev->toread ||
3759	    (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3760		/* We need this block to directly satisfy a request */
3761		return 1;
3762
3763	if (s->syncing || s->expanding ||
3764	    (s->replacing && want_replace(sh, disk_idx)))
3765		/* When syncing, or expanding we read everything.
3766		 * When replacing, we need the replaced block.
3767		 */
3768		return 1;
3769
3770	if ((s->failed >= 1 && fdev[0]->toread) ||
3771	    (s->failed >= 2 && fdev[1]->toread))
3772		/* If we want to read from a failed device, then
3773		 * we need to actually read every other device.
3774		 */
3775		return 1;
3776
3777	/* Sometimes neither read-modify-write nor reconstruct-write
3778	 * cycles can work.  In those cases we read every block we
3779	 * can.  Then the parity-update is certain to have enough to
3780	 * work with.
3781	 * This can only be a problem when we need to write something,
3782	 * and some device has failed.  If either of those tests
3783	 * fail we need look no further.
3784	 */
3785	if (!s->failed || !s->to_write)
3786		return 0;
3787
3788	if (test_bit(R5_Insync, &dev->flags) &&
3789	    !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3790		/* Pre-reads at not permitted until after short delay
3791		 * to gather multiple requests.  However if this
3792		 * device is no Insync, the block could only be computed
3793		 * and there is no need to delay that.
3794		 */
3795		return 0;
3796
3797	for (i = 0; i < s->failed && i < 2; i++) {
3798		if (fdev[i]->towrite &&
3799		    !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3800		    !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3801			/* If we have a partial write to a failed
3802			 * device, then we will need to reconstruct
3803			 * the content of that device, so all other
3804			 * devices must be read.
3805			 */
3806			return 1;
3807
3808		if (s->failed >= 2 &&
3809		    (fdev[i]->towrite ||
3810		     s->failed_num[i] == sh->pd_idx ||
3811		     s->failed_num[i] == sh->qd_idx) &&
3812		    !test_bit(R5_UPTODATE, &fdev[i]->flags))
3813			/* In max degraded raid6, If the failed disk is P, Q,
3814			 * or we want to read the failed disk, we need to do
3815			 * reconstruct-write.
3816			 */
3817			force_rcw = true;
3818	}
3819
3820	/* If we are forced to do a reconstruct-write, because parity
3821	 * cannot be trusted and we are currently recovering it, there
3822	 * is extra need to be careful.
3823	 * If one of the devices that we would need to read, because
3824	 * it is not being overwritten (and maybe not written at all)
3825	 * is missing/faulty, then we need to read everything we can.
3826	 */
3827	if (!force_rcw &&
3828	    sh->sector < sh->raid_conf->mddev->recovery_cp)
3829		/* reconstruct-write isn't being forced */
3830		return 0;
3831	for (i = 0; i < s->failed && i < 2; i++) {
3832		if (s->failed_num[i] != sh->pd_idx &&
3833		    s->failed_num[i] != sh->qd_idx &&
3834		    !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3835		    !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3836			return 1;
3837	}
3838
3839	return 0;
3840}
3841
3842/* fetch_block - checks the given member device to see if its data needs
3843 * to be read or computed to satisfy a request.
3844 *
3845 * Returns 1 when no more member devices need to be checked, otherwise returns
3846 * 0 to tell the loop in handle_stripe_fill to continue
3847 */
3848static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3849		       int disk_idx, int disks)
3850{
3851	struct r5dev *dev = &sh->dev[disk_idx];
3852
3853	/* is the data in this block needed, and can we get it? */
3854	if (need_this_block(sh, s, disk_idx, disks)) {
3855		/* we would like to get this block, possibly by computing it,
3856		 * otherwise read it if the backing disk is insync
3857		 */
3858		BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3859		BUG_ON(test_bit(R5_Wantread, &dev->flags));
3860		BUG_ON(sh->batch_head);
3861
3862		/*
3863		 * In the raid6 case if the only non-uptodate disk is P
3864		 * then we already trusted P to compute the other failed
3865		 * drives. It is safe to compute rather than re-read P.
3866		 * In other cases we only compute blocks from failed
3867		 * devices, otherwise check/repair might fail to detect
3868		 * a real inconsistency.
3869		 */
3870
3871		if ((s->uptodate == disks - 1) &&
3872		    ((sh->qd_idx >= 0 && sh->pd_idx == disk_idx) ||
3873		    (s->failed && (disk_idx == s->failed_num[0] ||
3874				   disk_idx == s->failed_num[1])))) {
3875			/* have disk failed, and we're requested to fetch it;
3876			 * do compute it
3877			 */
3878			pr_debug("Computing stripe %llu block %d\n",
3879			       (unsigned long long)sh->sector, disk_idx);
3880			set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3881			set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3882			set_bit(R5_Wantcompute, &dev->flags);
3883			sh->ops.target = disk_idx;
3884			sh->ops.target2 = -1; /* no 2nd target */
3885			s->req_compute = 1;
3886			/* Careful: from this point on 'uptodate' is in the eye
3887			 * of raid_run_ops which services 'compute' operations
3888			 * before writes. R5_Wantcompute flags a block that will
3889			 * be R5_UPTODATE by the time it is needed for a
3890			 * subsequent operation.
3891			 */
3892			s->uptodate++;
3893			return 1;
3894		} else if (s->uptodate == disks-2 && s->failed >= 2) {
3895			/* Computing 2-failure is *very* expensive; only
3896			 * do it if failed >= 2
3897			 */
3898			int other;
3899			for (other = disks; other--; ) {
3900				if (other == disk_idx)
3901					continue;
3902				if (!test_bit(R5_UPTODATE,
3903				      &sh->dev[other].flags))
3904					break;
3905			}
3906			BUG_ON(other < 0);
3907			pr_debug("Computing stripe %llu blocks %d,%d\n",
3908			       (unsigned long long)sh->sector,
3909			       disk_idx, other);
3910			set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3911			set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3912			set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3913			set_bit(R5_Wantcompute, &sh->dev[other].flags);
3914			sh->ops.target = disk_idx;
3915			sh->ops.target2 = other;
3916			s->uptodate += 2;
3917			s->req_compute = 1;
3918			return 1;
3919		} else if (test_bit(R5_Insync, &dev->flags)) {
3920			set_bit(R5_LOCKED, &dev->flags);
3921			set_bit(R5_Wantread, &dev->flags);
3922			s->locked++;
3923			pr_debug("Reading block %d (sync=%d)\n",
3924				disk_idx, s->syncing);
3925		}
3926	}
3927
3928	return 0;
3929}
3930
3931/*
3932 * handle_stripe_fill - read or compute data to satisfy pending requests.
3933 */
3934static void handle_stripe_fill(struct stripe_head *sh,
3935			       struct stripe_head_state *s,
3936			       int disks)
3937{
3938	int i;
3939
3940	/* look for blocks to read/compute, skip this if a compute
3941	 * is already in flight, or if the stripe contents are in the
3942	 * midst of changing due to a write
3943	 */
3944	if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
3945	    !sh->reconstruct_state) {
3946
3947		/*
3948		 * For degraded stripe with data in journal, do not handle
3949		 * read requests yet, instead, flush the stripe to raid
3950		 * disks first, this avoids handling complex rmw of write
3951		 * back cache (prexor with orig_page, and then xor with
3952		 * page) in the read path
3953		 */
3954		if (s->injournal && s->failed) {
3955			if (test_bit(STRIPE_R5C_CACHING, &sh->state))
3956				r5c_make_stripe_write_out(sh);
3957			goto out;
3958		}
3959
3960		for (i = disks; i--; )
3961			if (fetch_block(sh, s, i, disks))
3962				break;
3963	}
3964out:
3965	set_bit(STRIPE_HANDLE, &sh->state);
3966}
3967
3968static void break_stripe_batch_list(struct stripe_head *head_sh,
3969				    unsigned long handle_flags);
3970/* handle_stripe_clean_event
3971 * any written block on an uptodate or failed drive can be returned.
3972 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3973 * never LOCKED, so we don't need to test 'failed' directly.
3974 */
3975static void handle_stripe_clean_event(struct r5conf *conf,
3976	struct stripe_head *sh, int disks)
3977{
3978	int i;
3979	struct r5dev *dev;
3980	int discard_pending = 0;
3981	struct stripe_head *head_sh = sh;
3982	bool do_endio = false;
3983
3984	for (i = disks; i--; )
3985		if (sh->dev[i].written) {
3986			dev = &sh->dev[i];
3987			if (!test_bit(R5_LOCKED, &dev->flags) &&
3988			    (test_bit(R5_UPTODATE, &dev->flags) ||
3989			     test_bit(R5_Discard, &dev->flags) ||
3990			     test_bit(R5_SkipCopy, &dev->flags))) {
3991				/* We can return any write requests */
3992				struct bio *wbi, *wbi2;
3993				pr_debug("Return write for disc %d\n", i);
3994				if (test_and_clear_bit(R5_Discard, &dev->flags))
3995					clear_bit(R5_UPTODATE, &dev->flags);
3996				if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3997					WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
3998				}
3999				do_endio = true;
4000
4001returnbi:
4002				dev->page = dev->orig_page;
4003				wbi = dev->written;
4004				dev->written = NULL;
4005				while (wbi && wbi->bi_iter.bi_sector <
4006					dev->sector + RAID5_STRIPE_SECTORS(conf)) {
4007					wbi2 = r5_next_bio(conf, wbi, dev->sector);
4008					md_write_end(conf->mddev);
4009					bio_endio(wbi);
4010					wbi = wbi2;
4011				}
4012				md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
4013						   RAID5_STRIPE_SECTORS(conf),
4014						   !test_bit(STRIPE_DEGRADED, &sh->state),
4015						   0);
4016				if (head_sh->batch_head) {
4017					sh = list_first_entry(&sh->batch_list,
4018							      struct stripe_head,
4019							      batch_list);
4020					if (sh != head_sh) {
4021						dev = &sh->dev[i];
4022						goto returnbi;
4023					}
4024				}
4025				sh = head_sh;
4026				dev = &sh->dev[i];
4027			} else if (test_bit(R5_Discard, &dev->flags))
4028				discard_pending = 1;
4029		}
4030
4031	log_stripe_write_finished(sh);
4032
4033	if (!discard_pending &&
4034	    test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
4035		int hash;
4036		clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
4037		clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
4038		if (sh->qd_idx >= 0) {
4039			clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
4040			clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
4041		}
4042		/* now that discard is done we can proceed with any sync */
4043		clear_bit(STRIPE_DISCARD, &sh->state);
4044		/*
4045		 * SCSI discard will change some bio fields and the stripe has
4046		 * no updated data, so remove it from hash list and the stripe
4047		 * will be reinitialized
4048		 */
4049unhash:
4050		hash = sh->hash_lock_index;
4051		spin_lock_irq(conf->hash_locks + hash);
4052		remove_hash(sh);
4053		spin_unlock_irq(conf->hash_locks + hash);
4054		if (head_sh->batch_head) {
4055			sh = list_first_entry(&sh->batch_list,
4056					      struct stripe_head, batch_list);
4057			if (sh != head_sh)
4058					goto unhash;
4059		}
4060		sh = head_sh;
4061
4062		if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
4063			set_bit(STRIPE_HANDLE, &sh->state);
4064
4065	}
4066
4067	if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
4068		if (atomic_dec_and_test(&conf->pending_full_writes))
4069			md_wakeup_thread(conf->mddev->thread);
4070
4071	if (head_sh->batch_head && do_endio)
4072		break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
4073}
4074
4075/*
4076 * For RMW in write back cache, we need extra page in prexor to store the
4077 * old data. This page is stored in dev->orig_page.
4078 *
4079 * This function checks whether we have data for prexor. The exact logic
4080 * is:
4081 *       R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
4082 */
4083static inline bool uptodate_for_rmw(struct r5dev *dev)
4084{
4085	return (test_bit(R5_UPTODATE, &dev->flags)) &&
4086		(!test_bit(R5_InJournal, &dev->flags) ||
4087		 test_bit(R5_OrigPageUPTDODATE, &dev->flags));
4088}
4089
4090static int handle_stripe_dirtying(struct r5conf *conf,
4091				  struct stripe_head *sh,
4092				  struct stripe_head_state *s,
4093				  int disks)
4094{
4095	int rmw = 0, rcw = 0, i;
4096	sector_t recovery_cp = conf->mddev->recovery_cp;
4097
4098	/* Check whether resync is now happening or should start.
4099	 * If yes, then the array is dirty (after unclean shutdown or
4100	 * initial creation), so parity in some stripes might be inconsistent.
4101	 * In this case, we need to always do reconstruct-write, to ensure
4102	 * that in case of drive failure or read-error correction, we
4103	 * generate correct data from the parity.
4104	 */
4105	if (conf->rmw_level == PARITY_DISABLE_RMW ||
4106	    (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
4107	     s->failed == 0)) {
4108		/* Calculate the real rcw later - for now make it
4109		 * look like rcw is cheaper
4110		 */
4111		rcw = 1; rmw = 2;
4112		pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
4113			 conf->rmw_level, (unsigned long long)recovery_cp,
4114			 (unsigned long long)sh->sector);
4115	} else for (i = disks; i--; ) {
4116		/* would I have to read this buffer for read_modify_write */
4117		struct r5dev *dev = &sh->dev[i];
4118		if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
4119		     i == sh->pd_idx || i == sh->qd_idx ||
4120		     test_bit(R5_InJournal, &dev->flags)) &&
4121		    !test_bit(R5_LOCKED, &dev->flags) &&
4122		    !(uptodate_for_rmw(dev) ||
4123		      test_bit(R5_Wantcompute, &dev->flags))) {
4124			if (test_bit(R5_Insync, &dev->flags))
4125				rmw++;
4126			else
4127				rmw += 2*disks;  /* cannot read it */
4128		}
4129		/* Would I have to read this buffer for reconstruct_write */
4130		if (!test_bit(R5_OVERWRITE, &dev->flags) &&
4131		    i != sh->pd_idx && i != sh->qd_idx &&
4132		    !test_bit(R5_LOCKED, &dev->flags) &&
4133		    !(test_bit(R5_UPTODATE, &dev->flags) ||
4134		      test_bit(R5_Wantcompute, &dev->flags))) {
4135			if (test_bit(R5_Insync, &dev->flags))
4136				rcw++;
4137			else
4138				rcw += 2*disks;
4139		}
4140	}
4141
4142	pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n",
4143		 (unsigned long long)sh->sector, sh->state, rmw, rcw);
4144	set_bit(STRIPE_HANDLE, &sh->state);
4145	if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
4146		/* prefer read-modify-write, but need to get some data */
4147		if (conf->mddev->queue)
4148			blk_add_trace_msg(conf->mddev->queue,
4149					  "raid5 rmw %llu %d",
4150					  (unsigned long long)sh->sector, rmw);
4151		for (i = disks; i--; ) {
4152			struct r5dev *dev = &sh->dev[i];
4153			if (test_bit(R5_InJournal, &dev->flags) &&
4154			    dev->page == dev->orig_page &&
4155			    !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) {
4156				/* alloc page for prexor */
4157				struct page *p = alloc_page(GFP_NOIO);
4158
4159				if (p) {
4160					dev->orig_page = p;
4161					continue;
4162				}
4163
4164				/*
4165				 * alloc_page() failed, try use
4166				 * disk_info->extra_page
4167				 */
4168				if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE,
4169						      &conf->cache_state)) {
4170					r5c_use_extra_page(sh);
4171					break;
4172				}
4173
4174				/* extra_page in use, add to delayed_list */
4175				set_bit(STRIPE_DELAYED, &sh->state);
4176				s->waiting_extra_page = 1;
4177				return -EAGAIN;
4178			}
4179		}
4180
4181		for (i = disks; i--; ) {
4182			struct r5dev *dev = &sh->dev[i];
4183			if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
4184			     i == sh->pd_idx || i == sh->qd_idx ||
4185			     test_bit(R5_InJournal, &dev->flags)) &&
4186			    !test_bit(R5_LOCKED, &dev->flags) &&
4187			    !(uptodate_for_rmw(dev) ||
4188			      test_bit(R5_Wantcompute, &dev->flags)) &&
4189			    test_bit(R5_Insync, &dev->flags)) {
4190				if (test_bit(STRIPE_PREREAD_ACTIVE,
4191					     &sh->state)) {
4192					pr_debug("Read_old block %d for r-m-w\n",
4193						 i);
4194					set_bit(R5_LOCKED, &dev->flags);
4195					set_bit(R5_Wantread, &dev->flags);
4196					s->locked++;
4197				} else
4198					set_bit(STRIPE_DELAYED, &sh->state);
4199			}
4200		}
4201	}
4202	if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
4203		/* want reconstruct write, but need to get some data */
4204		int qread =0;
4205		rcw = 0;
4206		for (i = disks; i--; ) {
4207			struct r5dev *dev = &sh->dev[i];
4208			if (!test_bit(R5_OVERWRITE, &dev->flags) &&
4209			    i != sh->pd_idx && i != sh->qd_idx &&
4210			    !test_bit(R5_LOCKED, &dev->flags) &&
4211			    !(test_bit(R5_UPTODATE, &dev->flags) ||
4212			      test_bit(R5_Wantcompute, &dev->flags))) {
4213				rcw++;
4214				if (test_bit(R5_Insync, &dev->flags) &&
4215				    test_bit(STRIPE_PREREAD_ACTIVE,
4216					     &sh->state)) {
4217					pr_debug("Read_old block "
4218						"%d for Reconstruct\n", i);
4219					set_bit(R5_LOCKED, &dev->flags);
4220					set_bit(R5_Wantread, &dev->flags);
4221					s->locked++;
4222					qread++;
4223				} else
4224					set_bit(STRIPE_DELAYED, &sh->state);
4225			}
4226		}
4227		if (rcw && conf->mddev->queue)
4228			blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
4229					  (unsigned long long)sh->sector,
4230					  rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
4231	}
4232
4233	if (rcw > disks && rmw > disks &&
4234	    !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4235		set_bit(STRIPE_DELAYED, &sh->state);
4236
4237	/* now if nothing is locked, and if we have enough data,
4238	 * we can start a write request
4239	 */
4240	/* since handle_stripe can be called at any time we need to handle the
4241	 * case where a compute block operation has been submitted and then a
4242	 * subsequent call wants to start a write request.  raid_run_ops only
4243	 * handles the case where compute block and reconstruct are requested
4244	 * simultaneously.  If this is not the case then new writes need to be
4245	 * held off until the compute completes.
4246	 */
4247	if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
4248	    (s->locked == 0 && (rcw == 0 || rmw == 0) &&
4249	     !test_bit(STRIPE_BIT_DELAY, &sh->state)))
4250		schedule_reconstruction(sh, s, rcw == 0, 0);
4251	return 0;
4252}
4253
4254static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
4255				struct stripe_head_state *s, int disks)
4256{
4257	struct r5dev *dev = NULL;
4258
4259	BUG_ON(sh->batch_head);
4260	set_bit(STRIPE_HANDLE, &sh->state);
4261
4262	switch (sh->check_state) {
4263	case check_state_idle:
4264		/* start a new check operation if there are no failures */
4265		if (s->failed == 0) {
4266			BUG_ON(s->uptodate != disks);
4267			sh->check_state = check_state_run;
4268			set_bit(STRIPE_OP_CHECK, &s->ops_request);
4269			clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
4270			s->uptodate--;
4271			break;
4272		}
4273		dev = &sh->dev[s->failed_num[0]];
4274		fallthrough;
4275	case check_state_compute_result:
4276		sh->check_state = check_state_idle;
4277		if (!dev)
4278			dev = &sh->dev[sh->pd_idx];
4279
4280		/* check that a write has not made the stripe insync */
4281		if (test_bit(STRIPE_INSYNC, &sh->state))
4282			break;
4283
4284		/* either failed parity check, or recovery is happening */
4285		BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
4286		BUG_ON(s->uptodate != disks);
4287
4288		set_bit(R5_LOCKED, &dev->flags);
4289		s->locked++;
4290		set_bit(R5_Wantwrite, &dev->flags);
4291
4292		clear_bit(STRIPE_DEGRADED, &sh->state);
4293		set_bit(STRIPE_INSYNC, &sh->state);
4294		break;
4295	case check_state_run:
4296		break; /* we will be called again upon completion */
4297	case check_state_check_result:
4298		sh->check_state = check_state_idle;
4299
4300		/* if a failure occurred during the check operation, leave
4301		 * STRIPE_INSYNC not set and let the stripe be handled again
4302		 */
4303		if (s->failed)
4304			break;
4305
4306		/* handle a successful check operation, if parity is correct
4307		 * we are done.  Otherwise update the mismatch count and repair
4308		 * parity if !MD_RECOVERY_CHECK
4309		 */
4310		if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
4311			/* parity is correct (on disc,
4312			 * not in buffer any more)
4313			 */
4314			set_bit(STRIPE_INSYNC, &sh->state);
4315		else {
4316			atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches);
4317			if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
4318				/* don't try to repair!! */
4319				set_bit(STRIPE_INSYNC, &sh->state);
4320				pr_warn_ratelimited("%s: mismatch sector in range "
4321						    "%llu-%llu\n", mdname(conf->mddev),
4322						    (unsigned long long) sh->sector,
4323						    (unsigned long long) sh->sector +
4324						    RAID5_STRIPE_SECTORS(conf));
4325			} else {
4326				sh->check_state = check_state_compute_run;
4327				set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4328				set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4329				set_bit(R5_Wantcompute,
4330					&sh->dev[sh->pd_idx].flags);
4331				sh->ops.target = sh->pd_idx;
4332				sh->ops.target2 = -1;
4333				s->uptodate++;
4334			}
4335		}
4336		break;
4337	case check_state_compute_run:
4338		break;
4339	default:
4340		pr_err("%s: unknown check_state: %d sector: %llu\n",
4341		       __func__, sh->check_state,
4342		       (unsigned long long) sh->sector);
4343		BUG();
4344	}
4345}
4346
4347static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
4348				  struct stripe_head_state *s,
4349				  int disks)
4350{
4351	int pd_idx = sh->pd_idx;
4352	int qd_idx = sh->qd_idx;
4353	struct r5dev *dev;
4354
4355	BUG_ON(sh->batch_head);
4356	set_bit(STRIPE_HANDLE, &sh->state);
4357
4358	BUG_ON(s->failed > 2);
4359
4360	/* Want to check and possibly repair P and Q.
4361	 * However there could be one 'failed' device, in which
4362	 * case we can only check one of them, possibly using the
4363	 * other to generate missing data
4364	 */
4365
4366	switch (sh->check_state) {
4367	case check_state_idle:
4368		/* start a new check operation if there are < 2 failures */
4369		if (s->failed == s->q_failed) {
4370			/* The only possible failed device holds Q, so it
4371			 * makes sense to check P (If anything else were failed,
4372			 * we would have used P to recreate it).
4373			 */
4374			sh->check_state = check_state_run;
4375		}
4376		if (!s->q_failed && s->failed < 2) {
4377			/* Q is not failed, and we didn't use it to generate
4378			 * anything, so it makes sense to check it
4379			 */
4380			if (sh->check_state == check_state_run)
4381				sh->check_state = check_state_run_pq;
4382			else
4383				sh->check_state = check_state_run_q;
4384		}
4385
4386		/* discard potentially stale zero_sum_result */
4387		sh->ops.zero_sum_result = 0;
4388
4389		if (sh->check_state == check_state_run) {
4390			/* async_xor_zero_sum destroys the contents of P */
4391			clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
4392			s->uptodate--;
4393		}
4394		if (sh->check_state >= check_state_run &&
4395		    sh->check_state <= check_state_run_pq) {
4396			/* async_syndrome_zero_sum preserves P and Q, so
4397			 * no need to mark them !uptodate here
4398			 */
4399			set_bit(STRIPE_OP_CHECK, &s->ops_request);
4400			break;
4401		}
4402
4403		/* we have 2-disk failure */
4404		BUG_ON(s->failed != 2);
4405		fallthrough;
4406	case check_state_compute_result:
4407		sh->check_state = check_state_idle;
4408
4409		/* check that a write has not made the stripe insync */
4410		if (test_bit(STRIPE_INSYNC, &sh->state))
4411			break;
4412
4413		/* now write out any block on a failed drive,
4414		 * or P or Q if they were recomputed
4415		 */
4416		dev = NULL;
4417		if (s->failed == 2) {
4418			dev = &sh->dev[s->failed_num[1]];
4419			s->locked++;
4420			set_bit(R5_LOCKED, &dev->flags);
4421			set_bit(R5_Wantwrite, &dev->flags);
4422		}
4423		if (s->failed >= 1) {
4424			dev = &sh->dev[s->failed_num[0]];
4425			s->locked++;
4426			set_bit(R5_LOCKED, &dev->flags);
4427			set_bit(R5_Wantwrite, &dev->flags);
4428		}
4429		if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4430			dev = &sh->dev[pd_idx];
4431			s->locked++;
4432			set_bit(R5_LOCKED, &dev->flags);
4433			set_bit(R5_Wantwrite, &dev->flags);
4434		}
4435		if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4436			dev = &sh->dev[qd_idx];
4437			s->locked++;
4438			set_bit(R5_LOCKED, &dev->flags);
4439			set_bit(R5_Wantwrite, &dev->flags);
4440		}
4441		if (WARN_ONCE(dev && !test_bit(R5_UPTODATE, &dev->flags),
4442			      "%s: disk%td not up to date\n",
4443			      mdname(conf->mddev),
4444			      dev - (struct r5dev *) &sh->dev)) {
4445			clear_bit(R5_LOCKED, &dev->flags);
4446			clear_bit(R5_Wantwrite, &dev->flags);
4447			s->locked--;
4448		}
4449		clear_bit(STRIPE_DEGRADED, &sh->state);
4450
4451		set_bit(STRIPE_INSYNC, &sh->state);
4452		break;
4453	case check_state_run:
4454	case check_state_run_q:
4455	case check_state_run_pq:
4456		break; /* we will be called again upon completion */
4457	case check_state_check_result:
4458		sh->check_state = check_state_idle;
4459
4460		/* handle a successful check operation, if parity is correct
4461		 * we are done.  Otherwise update the mismatch count and repair
4462		 * parity if !MD_RECOVERY_CHECK
4463		 */
4464		if (sh->ops.zero_sum_result == 0) {
4465			/* both parities are correct */
4466			if (!s->failed)
4467				set_bit(STRIPE_INSYNC, &sh->state);
4468			else {
4469				/* in contrast to the raid5 case we can validate
4470				 * parity, but still have a failure to write
4471				 * back
4472				 */
4473				sh->check_state = check_state_compute_result;
4474				/* Returning at this point means that we may go
4475				 * off and bring p and/or q uptodate again so
4476				 * we make sure to check zero_sum_result again
4477				 * to verify if p or q need writeback
4478				 */
4479			}
4480		} else {
4481			atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches);
4482			if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
4483				/* don't try to repair!! */
4484				set_bit(STRIPE_INSYNC, &sh->state);
4485				pr_warn_ratelimited("%s: mismatch sector in range "
4486						    "%llu-%llu\n", mdname(conf->mddev),
4487						    (unsigned long long) sh->sector,
4488						    (unsigned long long) sh->sector +
4489						    RAID5_STRIPE_SECTORS(conf));
4490			} else {
4491				int *target = &sh->ops.target;
4492
4493				sh->ops.target = -1;
4494				sh->ops.target2 = -1;
4495				sh->check_state = check_state_compute_run;
4496				set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4497				set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4498				if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4499					set_bit(R5_Wantcompute,
4500						&sh->dev[pd_idx].flags);
4501					*target = pd_idx;
4502					target = &sh->ops.target2;
4503					s->uptodate++;
4504				}
4505				if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4506					set_bit(R5_Wantcompute,
4507						&sh->dev[qd_idx].flags);
4508					*target = qd_idx;
4509					s->uptodate++;
4510				}
4511			}
4512		}
4513		break;
4514	case check_state_compute_run:
4515		break;
4516	default:
4517		pr_warn("%s: unknown check_state: %d sector: %llu\n",
4518			__func__, sh->check_state,
4519			(unsigned long long) sh->sector);
4520		BUG();
4521	}
4522}
4523
4524static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
4525{
4526	int i;
4527
4528	/* We have read all the blocks in this stripe and now we need to
4529	 * copy some of them into a target stripe for expand.
4530	 */
4531	struct dma_async_tx_descriptor *tx = NULL;
4532	BUG_ON(sh->batch_head);
4533	clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4534	for (i = 0; i < sh->disks; i++)
4535		if (i != sh->pd_idx && i != sh->qd_idx) {
4536			int dd_idx, j;
4537			struct stripe_head *sh2;
4538			struct async_submit_ctl submit;
4539
4540			sector_t bn = raid5_compute_blocknr(sh, i, 1);
4541			sector_t s = raid5_compute_sector(conf, bn, 0,
4542							  &dd_idx, NULL);
4543			sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
4544			if (sh2 == NULL)
4545				/* so far only the early blocks of this stripe
4546				 * have been requested.  When later blocks
4547				 * get requested, we will try again
4548				 */
4549				continue;
4550			if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
4551			   test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
4552				/* must have already done this block */
4553				raid5_release_stripe(sh2);
4554				continue;
4555			}
4556
4557			/* place all the copies on one channel */
4558			init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
4559			tx = async_memcpy(sh2->dev[dd_idx].page,
4560					  sh->dev[i].page, sh2->dev[dd_idx].offset,
4561					  sh->dev[i].offset, RAID5_STRIPE_SIZE(conf),
4562					  &submit);
4563
4564			set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
4565			set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
4566			for (j = 0; j < conf->raid_disks; j++)
4567				if (j != sh2->pd_idx &&
4568				    j != sh2->qd_idx &&
4569				    !test_bit(R5_Expanded, &sh2->dev[j].flags))
4570					break;
4571			if (j == conf->raid_disks) {
4572				set_bit(STRIPE_EXPAND_READY, &sh2->state);
4573				set_bit(STRIPE_HANDLE, &sh2->state);
4574			}
4575			raid5_release_stripe(sh2);
4576
4577		}
4578	/* done submitting copies, wait for them to complete */
4579	async_tx_quiesce(&tx);
4580}
4581
4582/*
4583 * handle_stripe - do things to a stripe.
4584 *
4585 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4586 * state of various bits to see what needs to be done.
4587 * Possible results:
4588 *    return some read requests which now have data
4589 *    return some write requests which are safely on storage
4590 *    schedule a read on some buffers
4591 *    schedule a write of some buffers
4592 *    return confirmation of parity correctness
4593 *
4594 */
4595
4596static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
4597{
4598	struct r5conf *conf = sh->raid_conf;
4599	int disks = sh->disks;
4600	struct r5dev *dev;
4601	int i;
4602	int do_recovery = 0;
4603
4604	memset(s, 0, sizeof(*s));
4605
4606	s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
4607	s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
4608	s->failed_num[0] = -1;
4609	s->failed_num[1] = -1;
4610	s->log_failed = r5l_log_disk_error(conf);
4611
4612	/* Now to look around and see what can be done */
4613	rcu_read_lock();
4614	for (i=disks; i--; ) {
4615		struct md_rdev *rdev;
4616		sector_t first_bad;
4617		int bad_sectors;
4618		int is_bad = 0;
4619
4620		dev = &sh->dev[i];
4621
4622		pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
4623			 i, dev->flags,
4624			 dev->toread, dev->towrite, dev->written);
4625		/* maybe we can reply to a read
4626		 *
4627		 * new wantfill requests are only permitted while
4628		 * ops_complete_biofill is guaranteed to be inactive
4629		 */
4630		if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4631		    !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4632			set_bit(R5_Wantfill, &dev->flags);
4633
4634		/* now count some things */
4635		if (test_bit(R5_LOCKED, &dev->flags))
4636			s->locked++;
4637		if (test_bit(R5_UPTODATE, &dev->flags))
4638			s->uptodate++;
4639		if (test_bit(R5_Wantcompute, &dev->flags)) {
4640			s->compute++;
4641			BUG_ON(s->compute > 2);
4642		}
4643
4644		if (test_bit(R5_Wantfill, &dev->flags))
4645			s->to_fill++;
4646		else if (dev->toread)
4647			s->to_read++;
4648		if (dev->towrite) {
4649			s->to_write++;
4650			if (!test_bit(R5_OVERWRITE, &dev->flags))
4651				s->non_overwrite++;
4652		}
4653		if (dev->written)
4654			s->written++;
4655		/* Prefer to use the replacement for reads, but only
4656		 * if it is recovered enough and has no bad blocks.
4657		 */
4658		rdev = rcu_dereference(conf->disks[i].replacement);
4659		if (rdev && !test_bit(Faulty, &rdev->flags) &&
4660		    rdev->recovery_offset >= sh->sector + RAID5_STRIPE_SECTORS(conf) &&
4661		    !is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
4662				 &first_bad, &bad_sectors))
4663			set_bit(R5_ReadRepl, &dev->flags);
4664		else {
4665			if (rdev && !test_bit(Faulty, &rdev->flags))
4666				set_bit(R5_NeedReplace, &dev->flags);
4667			else
4668				clear_bit(R5_NeedReplace, &dev->flags);
4669			rdev = rcu_dereference(conf->disks[i].rdev);
4670			clear_bit(R5_ReadRepl, &dev->flags);
4671		}
4672		if (rdev && test_bit(Faulty, &rdev->flags))
4673			rdev = NULL;
4674		if (rdev) {
4675			is_bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
4676					     &first_bad, &bad_sectors);
4677			if (s->blocked_rdev == NULL
4678			    && (test_bit(Blocked, &rdev->flags)
4679				|| is_bad < 0)) {
4680				if (is_bad < 0)
4681					set_bit(BlockedBadBlocks,
4682						&rdev->flags);
4683				s->blocked_rdev = rdev;
4684				atomic_inc(&rdev->nr_pending);
4685			}
4686		}
4687		clear_bit(R5_Insync, &dev->flags);
4688		if (!rdev)
4689			/* Not in-sync */;
4690		else if (is_bad) {
4691			/* also not in-sync */
4692			if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4693			    test_bit(R5_UPTODATE, &dev->flags)) {
4694				/* treat as in-sync, but with a read error
4695				 * which we can now try to correct
4696				 */
4697				set_bit(R5_Insync, &dev->flags);
4698				set_bit(R5_ReadError, &dev->flags);
4699			}
4700		} else if (test_bit(In_sync, &rdev->flags))
4701			set_bit(R5_Insync, &dev->flags);
4702		else if (sh->sector + RAID5_STRIPE_SECTORS(conf) <= rdev->recovery_offset)
4703			/* in sync if before recovery_offset */
4704			set_bit(R5_Insync, &dev->flags);
4705		else if (test_bit(R5_UPTODATE, &dev->flags) &&
4706			 test_bit(R5_Expanded, &dev->flags))
4707			/* If we've reshaped into here, we assume it is Insync.
4708			 * We will shortly update recovery_offset to make
4709			 * it official.
4710			 */
4711			set_bit(R5_Insync, &dev->flags);
4712
4713		if (test_bit(R5_WriteError, &dev->flags)) {
4714			/* This flag does not apply to '.replacement'
4715			 * only to .rdev, so make sure to check that*/
4716			struct md_rdev *rdev2 = rcu_dereference(
4717				conf->disks[i].rdev);
4718			if (rdev2 == rdev)
4719				clear_bit(R5_Insync, &dev->flags);
4720			if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4721				s->handle_bad_blocks = 1;
4722				atomic_inc(&rdev2->nr_pending);
4723			} else
4724				clear_bit(R5_WriteError, &dev->flags);
4725		}
4726		if (test_bit(R5_MadeGood, &dev->flags)) {
4727			/* This flag does not apply to '.replacement'
4728			 * only to .rdev, so make sure to check that*/
4729			struct md_rdev *rdev2 = rcu_dereference(
4730				conf->disks[i].rdev);
4731			if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4732				s->handle_bad_blocks = 1;
4733				atomic_inc(&rdev2->nr_pending);
4734			} else
4735				clear_bit(R5_MadeGood, &dev->flags);
4736		}
4737		if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4738			struct md_rdev *rdev2 = rcu_dereference(
4739				conf->disks[i].replacement);
4740			if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4741				s->handle_bad_blocks = 1;
4742				atomic_inc(&rdev2->nr_pending);
4743			} else
4744				clear_bit(R5_MadeGoodRepl, &dev->flags);
4745		}
4746		if (!test_bit(R5_Insync, &dev->flags)) {
4747			/* The ReadError flag will just be confusing now */
4748			clear_bit(R5_ReadError, &dev->flags);
4749			clear_bit(R5_ReWrite, &dev->flags);
4750		}
4751		if (test_bit(R5_ReadError, &dev->flags))
4752			clear_bit(R5_Insync, &dev->flags);
4753		if (!test_bit(R5_Insync, &dev->flags)) {
4754			if (s->failed < 2)
4755				s->failed_num[s->failed] = i;
4756			s->failed++;
4757			if (rdev && !test_bit(Faulty, &rdev->flags))
4758				do_recovery = 1;
4759			else if (!rdev) {
4760				rdev = rcu_dereference(
4761				    conf->disks[i].replacement);
4762				if (rdev && !test_bit(Faulty, &rdev->flags))
4763					do_recovery = 1;
4764			}
4765		}
4766
4767		if (test_bit(R5_InJournal, &dev->flags))
4768			s->injournal++;
4769		if (test_bit(R5_InJournal, &dev->flags) && dev->written)
4770			s->just_cached++;
4771	}
4772	if (test_bit(STRIPE_SYNCING, &sh->state)) {
4773		/* If there is a failed device being replaced,
4774		 *     we must be recovering.
4775		 * else if we are after recovery_cp, we must be syncing
4776		 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
4777		 * else we can only be replacing
4778		 * sync and recovery both need to read all devices, and so
4779		 * use the same flag.
4780		 */
4781		if (do_recovery ||
4782		    sh->sector >= conf->mddev->recovery_cp ||
4783		    test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
4784			s->syncing = 1;
4785		else
4786			s->replacing = 1;
4787	}
4788	rcu_read_unlock();
4789}
4790
4791/*
4792 * Return '1' if this is a member of batch, or '0' if it is a lone stripe or
4793 * a head which can now be handled.
4794 */
4795static int clear_batch_ready(struct stripe_head *sh)
4796{
4797	struct stripe_head *tmp;
4798	if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
4799		return (sh->batch_head && sh->batch_head != sh);
4800	spin_lock(&sh->stripe_lock);
4801	if (!sh->batch_head) {
4802		spin_unlock(&sh->stripe_lock);
4803		return 0;
4804	}
4805
4806	/*
4807	 * this stripe could be added to a batch list before we check
4808	 * BATCH_READY, skips it
4809	 */
4810	if (sh->batch_head != sh) {
4811		spin_unlock(&sh->stripe_lock);
4812		return 1;
4813	}
4814	spin_lock(&sh->batch_lock);
4815	list_for_each_entry(tmp, &sh->batch_list, batch_list)
4816		clear_bit(STRIPE_BATCH_READY, &tmp->state);
4817	spin_unlock(&sh->batch_lock);
4818	spin_unlock(&sh->stripe_lock);
4819
4820	/*
4821	 * BATCH_READY is cleared, no new stripes can be added.
4822	 * batch_list can be accessed without lock
4823	 */
4824	return 0;
4825}
4826
4827static void break_stripe_batch_list(struct stripe_head *head_sh,
4828				    unsigned long handle_flags)
4829{
4830	struct stripe_head *sh, *next;
4831	int i;
4832	int do_wakeup = 0;
4833
4834	list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4835
4836		list_del_init(&sh->batch_list);
4837
4838		WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
4839					  (1 << STRIPE_SYNCING) |
4840					  (1 << STRIPE_REPLACED) |
4841					  (1 << STRIPE_DELAYED) |
4842					  (1 << STRIPE_BIT_DELAY) |
4843					  (1 << STRIPE_FULL_WRITE) |
4844					  (1 << STRIPE_BIOFILL_RUN) |
4845					  (1 << STRIPE_COMPUTE_RUN)  |
4846					  (1 << STRIPE_DISCARD) |
4847					  (1 << STRIPE_BATCH_READY) |
4848					  (1 << STRIPE_BATCH_ERR) |
4849					  (1 << STRIPE_BITMAP_PENDING)),
4850			"stripe state: %lx\n", sh->state);
4851		WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4852					      (1 << STRIPE_REPLACED)),
4853			"head stripe state: %lx\n", head_sh->state);
4854
4855		set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
4856					    (1 << STRIPE_PREREAD_ACTIVE) |
4857					    (1 << STRIPE_DEGRADED) |
4858					    (1 << STRIPE_ON_UNPLUG_LIST)),
4859			      head_sh->state & (1 << STRIPE_INSYNC));
4860
4861		sh->check_state = head_sh->check_state;
4862		sh->reconstruct_state = head_sh->reconstruct_state;
4863		spin_lock_irq(&sh->stripe_lock);
4864		sh->batch_head = NULL;
4865		spin_unlock_irq(&sh->stripe_lock);
4866		for (i = 0; i < sh->disks; i++) {
4867			if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4868				do_wakeup = 1;
4869			sh->dev[i].flags = head_sh->dev[i].flags &
4870				(~((1 << R5_WriteError) | (1 << R5_Overlap)));
4871		}
4872		if (handle_flags == 0 ||
4873		    sh->state & handle_flags)
4874			set_bit(STRIPE_HANDLE, &sh->state);
4875		raid5_release_stripe(sh);
4876	}
4877	spin_lock_irq(&head_sh->stripe_lock);
4878	head_sh->batch_head = NULL;
4879	spin_unlock_irq(&head_sh->stripe_lock);
4880	for (i = 0; i < head_sh->disks; i++)
4881		if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4882			do_wakeup = 1;
4883	if (head_sh->state & handle_flags)
4884		set_bit(STRIPE_HANDLE, &head_sh->state);
4885
4886	if (do_wakeup)
4887		wake_up(&head_sh->raid_conf->wait_for_overlap);
4888}
4889
4890static void handle_stripe(struct stripe_head *sh)
4891{
4892	struct stripe_head_state s;
4893	struct r5conf *conf = sh->raid_conf;
4894	int i;
4895	int prexor;
4896	int disks = sh->disks;
4897	struct r5dev *pdev, *qdev;
4898
4899	clear_bit(STRIPE_HANDLE, &sh->state);
4900
4901	/*
4902	 * handle_stripe should not continue handle the batched stripe, only
4903	 * the head of batch list or lone stripe can continue. Otherwise we
4904	 * could see break_stripe_batch_list warns about the STRIPE_ACTIVE
4905	 * is set for the batched stripe.
4906	 */
4907	if (clear_batch_ready(sh))
4908		return;
4909
4910	if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
4911		/* already being handled, ensure it gets handled
4912		 * again when current action finishes */
4913		set_bit(STRIPE_HANDLE, &sh->state);
4914		return;
4915	}
4916
4917	if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
4918		break_stripe_batch_list(sh, 0);
4919
4920	if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
4921		spin_lock(&sh->stripe_lock);
4922		/*
4923		 * Cannot process 'sync' concurrently with 'discard'.
4924		 * Flush data in r5cache before 'sync'.
4925		 */
4926		if (!test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) &&
4927		    !test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) &&
4928		    !test_bit(STRIPE_DISCARD, &sh->state) &&
4929		    test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4930			set_bit(STRIPE_SYNCING, &sh->state);
4931			clear_bit(STRIPE_INSYNC, &sh->state);
4932			clear_bit(STRIPE_REPLACED, &sh->state);
4933		}
4934		spin_unlock(&sh->stripe_lock);
4935	}
4936	clear_bit(STRIPE_DELAYED, &sh->state);
4937
4938	pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4939		"pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4940	       (unsigned long long)sh->sector, sh->state,
4941	       atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4942	       sh->check_state, sh->reconstruct_state);
4943
4944	analyse_stripe(sh, &s);
4945
4946	if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
4947		goto finish;
4948
4949	if (s.handle_bad_blocks ||
4950	    test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) {
4951		set_bit(STRIPE_HANDLE, &sh->state);
4952		goto finish;
4953	}
4954
4955	if (unlikely(s.blocked_rdev)) {
4956		if (s.syncing || s.expanding || s.expanded ||
4957		    s.replacing || s.to_write || s.written) {
4958			set_bit(STRIPE_HANDLE, &sh->state);
4959			goto finish;
4960		}
4961		/* There is nothing for the blocked_rdev to block */
4962		rdev_dec_pending(s.blocked_rdev, conf->mddev);
4963		s.blocked_rdev = NULL;
4964	}
4965
4966	if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4967		set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4968		set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4969	}
4970
4971	pr_debug("locked=%d uptodate=%d to_read=%d"
4972	       " to_write=%d failed=%d failed_num=%d,%d\n",
4973	       s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4974	       s.failed_num[0], s.failed_num[1]);
4975	/*
4976	 * check if the array has lost more than max_degraded devices and,
4977	 * if so, some requests might need to be failed.
4978	 *
4979	 * When journal device failed (log_failed), we will only process
4980	 * the stripe if there is data need write to raid disks
4981	 */
4982	if (s.failed > conf->max_degraded ||
4983	    (s.log_failed && s.injournal == 0)) {
4984		sh->check_state = 0;
4985		sh->reconstruct_state = 0;
4986		break_stripe_batch_list(sh, 0);
4987		if (s.to_read+s.to_write+s.written)
4988			handle_failed_stripe(conf, sh, &s, disks);
4989		if (s.syncing + s.replacing)
4990			handle_failed_sync(conf, sh, &s);
4991	}
4992
4993	/* Now we check to see if any write operations have recently
4994	 * completed
4995	 */
4996	prexor = 0;
4997	if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4998		prexor = 1;
4999	if (sh->reconstruct_state == reconstruct_state_drain_result ||
5000	    sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
5001		sh->reconstruct_state = reconstruct_state_idle;
5002
5003		/* All the 'written' buffers and the parity block are ready to
5004		 * be written back to disk
5005		 */
5006		BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
5007		       !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
5008		BUG_ON(sh->qd_idx >= 0 &&
5009		       !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
5010		       !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
5011		for (i = disks; i--; ) {
5012			struct r5dev *dev = &sh->dev[i];
5013			if (test_bit(R5_LOCKED, &dev->flags) &&
5014				(i == sh->pd_idx || i == sh->qd_idx ||
5015				 dev->written || test_bit(R5_InJournal,
5016							  &dev->flags))) {
5017				pr_debug("Writing block %d\n", i);
5018				set_bit(R5_Wantwrite, &dev->flags);
5019				if (prexor)
5020					continue;
5021				if (s.failed > 1)
5022					continue;
5023				if (!test_bit(R5_Insync, &dev->flags) ||
5024				    ((i == sh->pd_idx || i == sh->qd_idx)  &&
5025				     s.failed == 0))
5026					set_bit(STRIPE_INSYNC, &sh->state);
5027			}
5028		}
5029		if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5030			s.dec_preread_active = 1;
5031	}
5032
5033	/*
5034	 * might be able to return some write requests if the parity blocks
5035	 * are safe, or on a failed drive
5036	 */
5037	pdev = &sh->dev[sh->pd_idx];
5038	s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
5039		|| (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
5040	qdev = &sh->dev[sh->qd_idx];
5041	s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
5042		|| (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
5043		|| conf->level < 6;
5044
5045	if (s.written &&
5046	    (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
5047			     && !test_bit(R5_LOCKED, &pdev->flags)
5048			     && (test_bit(R5_UPTODATE, &pdev->flags) ||
5049				 test_bit(R5_Discard, &pdev->flags))))) &&
5050	    (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
5051			     && !test_bit(R5_LOCKED, &qdev->flags)
5052			     && (test_bit(R5_UPTODATE, &qdev->flags) ||
5053				 test_bit(R5_Discard, &qdev->flags))))))
5054		handle_stripe_clean_event(conf, sh, disks);
5055
5056	if (s.just_cached)
5057		r5c_handle_cached_data_endio(conf, sh, disks);
5058	log_stripe_write_finished(sh);
5059
5060	/* Now we might consider reading some blocks, either to check/generate
5061	 * parity, or to satisfy requests
5062	 * or to load a block that is being partially written.
5063	 */
5064	if (s.to_read || s.non_overwrite
5065	    || (s.to_write && s.failed)
5066	    || (s.syncing && (s.uptodate + s.compute < disks))
5067	    || s.replacing
5068	    || s.expanding)
5069		handle_stripe_fill(sh, &s, disks);
5070
5071	/*
5072	 * When the stripe finishes full journal write cycle (write to journal
5073	 * and raid disk), this is the clean up procedure so it is ready for
5074	 * next operation.
5075	 */
5076	r5c_finish_stripe_write_out(conf, sh, &s);
5077
5078	/*
5079	 * Now to consider new write requests, cache write back and what else,
5080	 * if anything should be read.  We do not handle new writes when:
5081	 * 1/ A 'write' operation (copy+xor) is already in flight.
5082	 * 2/ A 'check' operation is in flight, as it may clobber the parity
5083	 *    block.
5084	 * 3/ A r5c cache log write is in flight.
5085	 */
5086
5087	if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
5088		if (!r5c_is_writeback(conf->log)) {
5089			if (s.to_write)
5090				handle_stripe_dirtying(conf, sh, &s, disks);
5091		} else { /* write back cache */
5092			int ret = 0;
5093
5094			/* First, try handle writes in caching phase */
5095			if (s.to_write)
5096				ret = r5c_try_caching_write(conf, sh, &s,
5097							    disks);
5098			/*
5099			 * If caching phase failed: ret == -EAGAIN
5100			 *    OR
5101			 * stripe under reclaim: !caching && injournal
5102			 *
5103			 * fall back to handle_stripe_dirtying()
5104			 */
5105			if (ret == -EAGAIN ||
5106			    /* stripe under reclaim: !caching && injournal */
5107			    (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
5108			     s.injournal > 0)) {
5109				ret = handle_stripe_dirtying(conf, sh, &s,
5110							     disks);
5111				if (ret == -EAGAIN)
5112					goto finish;
5113			}
5114		}
5115	}
5116
5117	/* maybe we need to check and possibly fix the parity for this stripe
5118	 * Any reads will already have been scheduled, so we just see if enough
5119	 * data is available.  The parity check is held off while parity
5120	 * dependent operations are in flight.
5121	 */
5122	if (sh->check_state ||
5123	    (s.syncing && s.locked == 0 &&
5124	     !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
5125	     !test_bit(STRIPE_INSYNC, &sh->state))) {
5126		if (conf->level == 6)
5127			handle_parity_checks6(conf, sh, &s, disks);
5128		else
5129			handle_parity_checks5(conf, sh, &s, disks);
5130	}
5131
5132	if ((s.replacing || s.syncing) && s.locked == 0
5133	    && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
5134	    && !test_bit(STRIPE_REPLACED, &sh->state)) {
5135		/* Write out to replacement devices where possible */
5136		for (i = 0; i < conf->raid_disks; i++)
5137			if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
5138				WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
5139				set_bit(R5_WantReplace, &sh->dev[i].flags);
5140				set_bit(R5_LOCKED, &sh->dev[i].flags);
5141				s.locked++;
5142			}
5143		if (s.replacing)
5144			set_bit(STRIPE_INSYNC, &sh->state);
5145		set_bit(STRIPE_REPLACED, &sh->state);
5146	}
5147	if ((s.syncing || s.replacing) && s.locked == 0 &&
5148	    !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
5149	    test_bit(STRIPE_INSYNC, &sh->state)) {
5150		md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1);
5151		clear_bit(STRIPE_SYNCING, &sh->state);
5152		if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
5153			wake_up(&conf->wait_for_overlap);
5154	}
5155
5156	/* If the failed drives are just a ReadError, then we might need
5157	 * to progress the repair/check process
5158	 */
5159	if (s.failed <= conf->max_degraded && !conf->mddev->ro)
5160		for (i = 0; i < s.failed; i++) {
5161			struct r5dev *dev = &sh->dev[s.failed_num[i]];
5162			if (test_bit(R5_ReadError, &dev->flags)
5163			    && !test_bit(R5_LOCKED, &dev->flags)
5164			    && test_bit(R5_UPTODATE, &dev->flags)
5165				) {
5166				if (!test_bit(R5_ReWrite, &dev->flags)) {
5167					set_bit(R5_Wantwrite, &dev->flags);
5168					set_bit(R5_ReWrite, &dev->flags);
5169				} else
5170					/* let's read it back */
5171					set_bit(R5_Wantread, &dev->flags);
5172				set_bit(R5_LOCKED, &dev->flags);
5173				s.locked++;
5174			}
5175		}
5176
5177	/* Finish reconstruct operations initiated by the expansion process */
5178	if (sh->reconstruct_state == reconstruct_state_result) {
5179		struct stripe_head *sh_src
5180			= raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
5181		if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
5182			/* sh cannot be written until sh_src has been read.
5183			 * so arrange for sh to be delayed a little
5184			 */
5185			set_bit(STRIPE_DELAYED, &sh->state);
5186			set_bit(STRIPE_HANDLE, &sh->state);
5187			if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
5188					      &sh_src->state))
5189				atomic_inc(&conf->preread_active_stripes);
5190			raid5_release_stripe(sh_src);
5191			goto finish;
5192		}
5193		if (sh_src)
5194			raid5_release_stripe(sh_src);
5195
5196		sh->reconstruct_state = reconstruct_state_idle;
5197		clear_bit(STRIPE_EXPANDING, &sh->state);
5198		for (i = conf->raid_disks; i--; ) {
5199			set_bit(R5_Wantwrite, &sh->dev[i].flags);
5200			set_bit(R5_LOCKED, &sh->dev[i].flags);
5201			s.locked++;
5202		}
5203	}
5204
5205	if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
5206	    !sh->reconstruct_state) {
5207		/* Need to write out all blocks after computing parity */
5208		sh->disks = conf->raid_disks;
5209		stripe_set_idx(sh->sector, conf, 0, sh);
5210		schedule_reconstruction(sh, &s, 1, 1);
5211	} else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
5212		clear_bit(STRIPE_EXPAND_READY, &sh->state);
5213		atomic_dec(&conf->reshape_stripes);
5214		wake_up(&conf->wait_for_overlap);
5215		md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1);
5216	}
5217
5218	if (s.expanding && s.locked == 0 &&
5219	    !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
5220		handle_stripe_expansion(conf, sh);
5221
5222finish:
5223	/* wait for this device to become unblocked */
5224	if (unlikely(s.blocked_rdev)) {
5225		if (conf->mddev->external)
5226			md_wait_for_blocked_rdev(s.blocked_rdev,
5227						 conf->mddev);
5228		else
5229			/* Internal metadata will immediately
5230			 * be written by raid5d, so we don't
5231			 * need to wait here.
5232			 */
5233			rdev_dec_pending(s.blocked_rdev,
5234					 conf->mddev);
5235	}
5236
5237	if (s.handle_bad_blocks)
5238		for (i = disks; i--; ) {
5239			struct md_rdev *rdev;
5240			struct r5dev *dev = &sh->dev[i];
5241			if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
5242				/* We own a safe reference to the rdev */
5243				rdev = rdev_pend_deref(conf->disks[i].rdev);
5244				if (!rdev_set_badblocks(rdev, sh->sector,
5245							RAID5_STRIPE_SECTORS(conf), 0))
5246					md_error(conf->mddev, rdev);
5247				rdev_dec_pending(rdev, conf->mddev);
5248			}
5249			if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
5250				rdev = rdev_pend_deref(conf->disks[i].rdev);
5251				rdev_clear_badblocks(rdev, sh->sector,
5252						     RAID5_STRIPE_SECTORS(conf), 0);
5253				rdev_dec_pending(rdev, conf->mddev);
5254			}
5255			if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
5256				rdev = rdev_pend_deref(conf->disks[i].replacement);
5257				if (!rdev)
5258					/* rdev have been moved down */
5259					rdev = rdev_pend_deref(conf->disks[i].rdev);
5260				rdev_clear_badblocks(rdev, sh->sector,
5261						     RAID5_STRIPE_SECTORS(conf), 0);
5262				rdev_dec_pending(rdev, conf->mddev);
5263			}
5264		}
5265
5266	if (s.ops_request)
5267		raid_run_ops(sh, s.ops_request);
5268
5269	ops_run_io(sh, &s);
5270
5271	if (s.dec_preread_active) {
5272		/* We delay this until after ops_run_io so that if make_request
5273		 * is waiting on a flush, it won't continue until the writes
5274		 * have actually been submitted.
5275		 */
5276		atomic_dec(&conf->preread_active_stripes);
5277		if (atomic_read(&conf->preread_active_stripes) <
5278		    IO_THRESHOLD)
5279			md_wakeup_thread(conf->mddev->thread);
5280	}
5281
5282	clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
5283}
5284
5285static void raid5_activate_delayed(struct r5conf *conf)
5286	__must_hold(&conf->device_lock)
5287{
5288	if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
5289		while (!list_empty(&conf->delayed_list)) {
5290			struct list_head *l = conf->delayed_list.next;
5291			struct stripe_head *sh;
5292			sh = list_entry(l, struct stripe_head, lru);
5293			list_del_init(l);
5294			clear_bit(STRIPE_DELAYED, &sh->state);
5295			if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5296				atomic_inc(&conf->preread_active_stripes);
5297			list_add_tail(&sh->lru, &conf->hold_list);
5298			raid5_wakeup_stripe_thread(sh);
5299		}
5300	}
5301}
5302
5303static void activate_bit_delay(struct r5conf *conf,
5304		struct list_head *temp_inactive_list)
5305	__must_hold(&conf->device_lock)
5306{
5307	struct list_head head;
5308	list_add(&head, &conf->bitmap_list);
5309	list_del_init(&conf->bitmap_list);
5310	while (!list_empty(&head)) {
5311		struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
5312		int hash;
5313		list_del_init(&sh->lru);
5314		atomic_inc(&sh->count);
5315		hash = sh->hash_lock_index;
5316		__release_stripe(conf, sh, &temp_inactive_list[hash]);
5317	}
5318}
5319
5320static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
5321{
5322	struct r5conf *conf = mddev->private;
5323	sector_t sector = bio->bi_iter.bi_sector;
5324	unsigned int chunk_sectors;
5325	unsigned int bio_sectors = bio_sectors(bio);