xref: /openbmc/linux/drivers/md/raid1.c (revision 2446dba0)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * raid1.c : Multiple Devices driver for Linux
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * RAID-1 management functions.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
111da177e4SLinus Torvalds  *
1296de0e25SJan Engelhardt  * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk>
131da177e4SLinus Torvalds  * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
141da177e4SLinus Torvalds  *
15191ea9b2SNeilBrown  * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support
16191ea9b2SNeilBrown  * bitmapped intelligence in resync:
17191ea9b2SNeilBrown  *
18191ea9b2SNeilBrown  *      - bitmap marked during normal i/o
19191ea9b2SNeilBrown  *      - bitmap used to skip nondirty blocks during sync
20191ea9b2SNeilBrown  *
21191ea9b2SNeilBrown  * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
22191ea9b2SNeilBrown  * - persistent bitmap code
23191ea9b2SNeilBrown  *
241da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
251da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
261da177e4SLinus Torvalds  * the Free Software Foundation; either version 2, or (at your option)
271da177e4SLinus Torvalds  * any later version.
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
301da177e4SLinus Torvalds  * (for example /usr/src/linux/COPYING); if not, write to the Free
311da177e4SLinus Torvalds  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
321da177e4SLinus Torvalds  */
331da177e4SLinus Torvalds 
345a0e3ad6STejun Heo #include <linux/slab.h>
3525570727SStephen Rothwell #include <linux/delay.h>
36bff61975SNeilBrown #include <linux/blkdev.h>
37056075c7SPaul Gortmaker #include <linux/module.h>
38bff61975SNeilBrown #include <linux/seq_file.h>
398bda470eSChristian Dietrich #include <linux/ratelimit.h>
4043b2e5d8SNeilBrown #include "md.h"
41ef740c37SChristoph Hellwig #include "raid1.h"
42ef740c37SChristoph Hellwig #include "bitmap.h"
43191ea9b2SNeilBrown 
441da177e4SLinus Torvalds /*
451da177e4SLinus Torvalds  * Number of guaranteed r1bios in case of extreme VM load:
461da177e4SLinus Torvalds  */
471da177e4SLinus Torvalds #define	NR_RAID1_BIOS 256
481da177e4SLinus Torvalds 
49473e87ceSJonathan Brassow /* when we get a read error on a read-only array, we redirect to another
50473e87ceSJonathan Brassow  * device without failing the first device, or trying to over-write to
51473e87ceSJonathan Brassow  * correct the read error.  To keep track of bad blocks on a per-bio
52473e87ceSJonathan Brassow  * level, we store IO_BLOCKED in the appropriate 'bios' pointer
53473e87ceSJonathan Brassow  */
54473e87ceSJonathan Brassow #define IO_BLOCKED ((struct bio *)1)
55473e87ceSJonathan Brassow /* When we successfully write to a known bad-block, we need to remove the
56473e87ceSJonathan Brassow  * bad-block marking which must be done from process context.  So we record
57473e87ceSJonathan Brassow  * the success by setting devs[n].bio to IO_MADE_GOOD
58473e87ceSJonathan Brassow  */
59473e87ceSJonathan Brassow #define IO_MADE_GOOD ((struct bio *)2)
60473e87ceSJonathan Brassow 
61473e87ceSJonathan Brassow #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
62473e87ceSJonathan Brassow 
6334db0cd6SNeilBrown /* When there are this many requests queue to be written by
6434db0cd6SNeilBrown  * the raid1 thread, we become 'congested' to provide back-pressure
6534db0cd6SNeilBrown  * for writeback.
6634db0cd6SNeilBrown  */
6734db0cd6SNeilBrown static int max_queued_requests = 1024;
681da177e4SLinus Torvalds 
6979ef3a8aSmajianpeng static void allow_barrier(struct r1conf *conf, sector_t start_next_window,
7079ef3a8aSmajianpeng 			  sector_t bi_sector);
71e8096360SNeilBrown static void lower_barrier(struct r1conf *conf);
721da177e4SLinus Torvalds 
73dd0fc66fSAl Viro static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	struct pool_info *pi = data;
769f2c9d12SNeilBrown 	int size = offsetof(struct r1bio, bios[pi->raid_disks]);
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	/* allocate a r1bio with room for raid_disks entries in the bios array */
797eaceaccSJens Axboe 	return kzalloc(size, gfp_flags);
801da177e4SLinus Torvalds }
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds static void r1bio_pool_free(void *r1_bio, void *data)
831da177e4SLinus Torvalds {
841da177e4SLinus Torvalds 	kfree(r1_bio);
851da177e4SLinus Torvalds }
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds #define RESYNC_BLOCK_SIZE (64*1024)
888e005f7cSmajianpeng #define RESYNC_DEPTH 32
891da177e4SLinus Torvalds #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
901da177e4SLinus Torvalds #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
918e005f7cSmajianpeng #define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH)
928e005f7cSmajianpeng #define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9)
938e005f7cSmajianpeng #define NEXT_NORMALIO_DISTANCE (3 * RESYNC_WINDOW_SECTORS)
941da177e4SLinus Torvalds 
95dd0fc66fSAl Viro static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
961da177e4SLinus Torvalds {
971da177e4SLinus Torvalds 	struct pool_info *pi = data;
989f2c9d12SNeilBrown 	struct r1bio *r1_bio;
991da177e4SLinus Torvalds 	struct bio *bio;
100da1aab3dSNeilBrown 	int need_pages;
1011da177e4SLinus Torvalds 	int i, j;
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	r1_bio = r1bio_pool_alloc(gfp_flags, pi);
1047eaceaccSJens Axboe 	if (!r1_bio)
1051da177e4SLinus Torvalds 		return NULL;
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	/*
1081da177e4SLinus Torvalds 	 * Allocate bios : 1 for reading, n-1 for writing
1091da177e4SLinus Torvalds 	 */
1101da177e4SLinus Torvalds 	for (j = pi->raid_disks ; j-- ; ) {
1116746557fSNeilBrown 		bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
1121da177e4SLinus Torvalds 		if (!bio)
1131da177e4SLinus Torvalds 			goto out_free_bio;
1141da177e4SLinus Torvalds 		r1_bio->bios[j] = bio;
1151da177e4SLinus Torvalds 	}
1161da177e4SLinus Torvalds 	/*
1171da177e4SLinus Torvalds 	 * Allocate RESYNC_PAGES data pages and attach them to
118d11c171eSNeilBrown 	 * the first bio.
119d11c171eSNeilBrown 	 * If this is a user-requested check/repair, allocate
120d11c171eSNeilBrown 	 * RESYNC_PAGES for each bio.
1211da177e4SLinus Torvalds 	 */
122d11c171eSNeilBrown 	if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
123da1aab3dSNeilBrown 		need_pages = pi->raid_disks;
124d11c171eSNeilBrown 	else
125da1aab3dSNeilBrown 		need_pages = 1;
126da1aab3dSNeilBrown 	for (j = 0; j < need_pages; j++) {
127d11c171eSNeilBrown 		bio = r1_bio->bios[j];
128a0787606SKent Overstreet 		bio->bi_vcnt = RESYNC_PAGES;
1291da177e4SLinus Torvalds 
130a0787606SKent Overstreet 		if (bio_alloc_pages(bio, gfp_flags))
131da1aab3dSNeilBrown 			goto out_free_pages;
132d11c171eSNeilBrown 	}
133d11c171eSNeilBrown 	/* If not user-requests, copy the page pointers to all bios */
134d11c171eSNeilBrown 	if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) {
135d11c171eSNeilBrown 		for (i=0; i<RESYNC_PAGES ; i++)
136d11c171eSNeilBrown 			for (j=1; j<pi->raid_disks; j++)
137d11c171eSNeilBrown 				r1_bio->bios[j]->bi_io_vec[i].bv_page =
138d11c171eSNeilBrown 					r1_bio->bios[0]->bi_io_vec[i].bv_page;
139d11c171eSNeilBrown 	}
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	r1_bio->master_bio = NULL;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	return r1_bio;
1441da177e4SLinus Torvalds 
145da1aab3dSNeilBrown out_free_pages:
146da1aab3dSNeilBrown 	while (--j >= 0) {
147da1aab3dSNeilBrown 		struct bio_vec *bv;
148da1aab3dSNeilBrown 
149da1aab3dSNeilBrown 		bio_for_each_segment_all(bv, r1_bio->bios[j], i)
150da1aab3dSNeilBrown 			__free_page(bv->bv_page);
151da1aab3dSNeilBrown 	}
152da1aab3dSNeilBrown 
1531da177e4SLinus Torvalds out_free_bio:
1541da177e4SLinus Torvalds 	while (++j < pi->raid_disks)
1551da177e4SLinus Torvalds 		bio_put(r1_bio->bios[j]);
1561da177e4SLinus Torvalds 	r1bio_pool_free(r1_bio, data);
1571da177e4SLinus Torvalds 	return NULL;
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds static void r1buf_pool_free(void *__r1_bio, void *data)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	struct pool_info *pi = data;
163d11c171eSNeilBrown 	int i,j;
1649f2c9d12SNeilBrown 	struct r1bio *r1bio = __r1_bio;
1651da177e4SLinus Torvalds 
166d11c171eSNeilBrown 	for (i = 0; i < RESYNC_PAGES; i++)
167d11c171eSNeilBrown 		for (j = pi->raid_disks; j-- ;) {
168d11c171eSNeilBrown 			if (j == 0 ||
169d11c171eSNeilBrown 			    r1bio->bios[j]->bi_io_vec[i].bv_page !=
170d11c171eSNeilBrown 			    r1bio->bios[0]->bi_io_vec[i].bv_page)
1711345b1d8SNeilBrown 				safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page);
1721da177e4SLinus Torvalds 		}
1731da177e4SLinus Torvalds 	for (i=0 ; i < pi->raid_disks; i++)
1741da177e4SLinus Torvalds 		bio_put(r1bio->bios[i]);
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	r1bio_pool_free(r1bio, data);
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
179e8096360SNeilBrown static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	int i;
1821da177e4SLinus Torvalds 
1838f19ccb2SNeilBrown 	for (i = 0; i < conf->raid_disks * 2; i++) {
1841da177e4SLinus Torvalds 		struct bio **bio = r1_bio->bios + i;
1854367af55SNeilBrown 		if (!BIO_SPECIAL(*bio))
1861da177e4SLinus Torvalds 			bio_put(*bio);
1871da177e4SLinus Torvalds 		*bio = NULL;
1881da177e4SLinus Torvalds 	}
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1919f2c9d12SNeilBrown static void free_r1bio(struct r1bio *r1_bio)
1921da177e4SLinus Torvalds {
193e8096360SNeilBrown 	struct r1conf *conf = r1_bio->mddev->private;
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	put_all_bios(conf, r1_bio);
1961da177e4SLinus Torvalds 	mempool_free(r1_bio, conf->r1bio_pool);
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
1999f2c9d12SNeilBrown static void put_buf(struct r1bio *r1_bio)
2001da177e4SLinus Torvalds {
201e8096360SNeilBrown 	struct r1conf *conf = r1_bio->mddev->private;
2023e198f78SNeilBrown 	int i;
2033e198f78SNeilBrown 
2048f19ccb2SNeilBrown 	for (i = 0; i < conf->raid_disks * 2; i++) {
2053e198f78SNeilBrown 		struct bio *bio = r1_bio->bios[i];
2063e198f78SNeilBrown 		if (bio->bi_end_io)
2073e198f78SNeilBrown 			rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
2083e198f78SNeilBrown 	}
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	mempool_free(r1_bio, conf->r1buf_pool);
2111da177e4SLinus Torvalds 
21217999be4SNeilBrown 	lower_barrier(conf);
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
2159f2c9d12SNeilBrown static void reschedule_retry(struct r1bio *r1_bio)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds 	unsigned long flags;
218fd01b88cSNeilBrown 	struct mddev *mddev = r1_bio->mddev;
219e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	spin_lock_irqsave(&conf->device_lock, flags);
2221da177e4SLinus Torvalds 	list_add(&r1_bio->retry_list, &conf->retry_list);
223ddaf22abSNeilBrown 	conf->nr_queued ++;
2241da177e4SLinus Torvalds 	spin_unlock_irqrestore(&conf->device_lock, flags);
2251da177e4SLinus Torvalds 
22617999be4SNeilBrown 	wake_up(&conf->wait_barrier);
2271da177e4SLinus Torvalds 	md_wakeup_thread(mddev->thread);
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds /*
2311da177e4SLinus Torvalds  * raid_end_bio_io() is called when we have finished servicing a mirrored
2321da177e4SLinus Torvalds  * operation and are ready to return a success/failure code to the buffer
2331da177e4SLinus Torvalds  * cache layer.
2341da177e4SLinus Torvalds  */
2359f2c9d12SNeilBrown static void call_bio_endio(struct r1bio *r1_bio)
236d2eb35acSNeilBrown {
237d2eb35acSNeilBrown 	struct bio *bio = r1_bio->master_bio;
238d2eb35acSNeilBrown 	int done;
239e8096360SNeilBrown 	struct r1conf *conf = r1_bio->mddev->private;
24079ef3a8aSmajianpeng 	sector_t start_next_window = r1_bio->start_next_window;
2414f024f37SKent Overstreet 	sector_t bi_sector = bio->bi_iter.bi_sector;
242d2eb35acSNeilBrown 
243d2eb35acSNeilBrown 	if (bio->bi_phys_segments) {
244d2eb35acSNeilBrown 		unsigned long flags;
245d2eb35acSNeilBrown 		spin_lock_irqsave(&conf->device_lock, flags);
246d2eb35acSNeilBrown 		bio->bi_phys_segments--;
247d2eb35acSNeilBrown 		done = (bio->bi_phys_segments == 0);
248d2eb35acSNeilBrown 		spin_unlock_irqrestore(&conf->device_lock, flags);
24979ef3a8aSmajianpeng 		/*
25079ef3a8aSmajianpeng 		 * make_request() might be waiting for
25179ef3a8aSmajianpeng 		 * bi_phys_segments to decrease
25279ef3a8aSmajianpeng 		 */
25379ef3a8aSmajianpeng 		wake_up(&conf->wait_barrier);
254d2eb35acSNeilBrown 	} else
255d2eb35acSNeilBrown 		done = 1;
256d2eb35acSNeilBrown 
257d2eb35acSNeilBrown 	if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
258d2eb35acSNeilBrown 		clear_bit(BIO_UPTODATE, &bio->bi_flags);
259d2eb35acSNeilBrown 	if (done) {
260d2eb35acSNeilBrown 		bio_endio(bio, 0);
261d2eb35acSNeilBrown 		/*
262d2eb35acSNeilBrown 		 * Wake up any possible resync thread that waits for the device
263d2eb35acSNeilBrown 		 * to go idle.
264d2eb35acSNeilBrown 		 */
26579ef3a8aSmajianpeng 		allow_barrier(conf, start_next_window, bi_sector);
266d2eb35acSNeilBrown 	}
267d2eb35acSNeilBrown }
268d2eb35acSNeilBrown 
2699f2c9d12SNeilBrown static void raid_end_bio_io(struct r1bio *r1_bio)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	struct bio *bio = r1_bio->master_bio;
2721da177e4SLinus Torvalds 
2734b6d287fSNeilBrown 	/* if nobody has done the final endio yet, do it now */
2744b6d287fSNeilBrown 	if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
27536a4e1feSNeilBrown 		pr_debug("raid1: sync end %s on sectors %llu-%llu\n",
2764b6d287fSNeilBrown 			 (bio_data_dir(bio) == WRITE) ? "write" : "read",
2774f024f37SKent Overstreet 			 (unsigned long long) bio->bi_iter.bi_sector,
2784f024f37SKent Overstreet 			 (unsigned long long) bio_end_sector(bio) - 1);
2794b6d287fSNeilBrown 
280d2eb35acSNeilBrown 		call_bio_endio(r1_bio);
2814b6d287fSNeilBrown 	}
2821da177e4SLinus Torvalds 	free_r1bio(r1_bio);
2831da177e4SLinus Torvalds }
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds /*
2861da177e4SLinus Torvalds  * Update disk head position estimator based on IRQ completion info.
2871da177e4SLinus Torvalds  */
2889f2c9d12SNeilBrown static inline void update_head_pos(int disk, struct r1bio *r1_bio)
2891da177e4SLinus Torvalds {
290e8096360SNeilBrown 	struct r1conf *conf = r1_bio->mddev->private;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	conf->mirrors[disk].head_position =
2931da177e4SLinus Torvalds 		r1_bio->sector + (r1_bio->sectors);
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
296ba3ae3beSNamhyung Kim /*
297ba3ae3beSNamhyung Kim  * Find the disk number which triggered given bio
298ba3ae3beSNamhyung Kim  */
2999f2c9d12SNeilBrown static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio)
300ba3ae3beSNamhyung Kim {
301ba3ae3beSNamhyung Kim 	int mirror;
30230194636SNeilBrown 	struct r1conf *conf = r1_bio->mddev->private;
30330194636SNeilBrown 	int raid_disks = conf->raid_disks;
304ba3ae3beSNamhyung Kim 
3058f19ccb2SNeilBrown 	for (mirror = 0; mirror < raid_disks * 2; mirror++)
306ba3ae3beSNamhyung Kim 		if (r1_bio->bios[mirror] == bio)
307ba3ae3beSNamhyung Kim 			break;
308ba3ae3beSNamhyung Kim 
3098f19ccb2SNeilBrown 	BUG_ON(mirror == raid_disks * 2);
310ba3ae3beSNamhyung Kim 	update_head_pos(mirror, r1_bio);
311ba3ae3beSNamhyung Kim 
312ba3ae3beSNamhyung Kim 	return mirror;
313ba3ae3beSNamhyung Kim }
314ba3ae3beSNamhyung Kim 
3156712ecf8SNeilBrown static void raid1_end_read_request(struct bio *bio, int error)
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds 	int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
3189f2c9d12SNeilBrown 	struct r1bio *r1_bio = bio->bi_private;
3191da177e4SLinus Torvalds 	int mirror;
320e8096360SNeilBrown 	struct r1conf *conf = r1_bio->mddev->private;
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	mirror = r1_bio->read_disk;
3231da177e4SLinus Torvalds 	/*
3241da177e4SLinus Torvalds 	 * this branch is our 'one mirror IO has finished' event handler:
3251da177e4SLinus Torvalds 	 */
326ddaf22abSNeilBrown 	update_head_pos(mirror, r1_bio);
327ddaf22abSNeilBrown 
328220946c9SNeilBrown 	if (uptodate)
3291da177e4SLinus Torvalds 		set_bit(R1BIO_Uptodate, &r1_bio->state);
330dd00a99eSNeilBrown 	else {
331dd00a99eSNeilBrown 		/* If all other devices have failed, we want to return
332dd00a99eSNeilBrown 		 * the error upwards rather than fail the last device.
333dd00a99eSNeilBrown 		 * Here we redefine "uptodate" to mean "Don't want to retry"
334dd00a99eSNeilBrown 		 */
335dd00a99eSNeilBrown 		unsigned long flags;
336dd00a99eSNeilBrown 		spin_lock_irqsave(&conf->device_lock, flags);
337dd00a99eSNeilBrown 		if (r1_bio->mddev->degraded == conf->raid_disks ||
338dd00a99eSNeilBrown 		    (r1_bio->mddev->degraded == conf->raid_disks-1 &&
339dd00a99eSNeilBrown 		     !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags)))
340dd00a99eSNeilBrown 			uptodate = 1;
341dd00a99eSNeilBrown 		spin_unlock_irqrestore(&conf->device_lock, flags);
342dd00a99eSNeilBrown 	}
3431da177e4SLinus Torvalds 
3447ad4d4a6SNeilBrown 	if (uptodate) {
3451da177e4SLinus Torvalds 		raid_end_bio_io(r1_bio);
3467ad4d4a6SNeilBrown 		rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
3477ad4d4a6SNeilBrown 	} else {
3481da177e4SLinus Torvalds 		/*
3491da177e4SLinus Torvalds 		 * oops, read error:
3501da177e4SLinus Torvalds 		 */
3511da177e4SLinus Torvalds 		char b[BDEVNAME_SIZE];
3528bda470eSChristian Dietrich 		printk_ratelimited(
3538bda470eSChristian Dietrich 			KERN_ERR "md/raid1:%s: %s: "
3548bda470eSChristian Dietrich 			"rescheduling sector %llu\n",
3559dd1e2faSNeilBrown 			mdname(conf->mddev),
3568bda470eSChristian Dietrich 			bdevname(conf->mirrors[mirror].rdev->bdev,
3578bda470eSChristian Dietrich 				 b),
3588bda470eSChristian Dietrich 			(unsigned long long)r1_bio->sector);
359d2eb35acSNeilBrown 		set_bit(R1BIO_ReadError, &r1_bio->state);
3601da177e4SLinus Torvalds 		reschedule_retry(r1_bio);
3617ad4d4a6SNeilBrown 		/* don't drop the reference on read_disk yet */
3621da177e4SLinus Torvalds 	}
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds 
3659f2c9d12SNeilBrown static void close_write(struct r1bio *r1_bio)
3664e78064fSNeilBrown {
3674e78064fSNeilBrown 	/* it really is the end of this request */
3684e78064fSNeilBrown 	if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
3694e78064fSNeilBrown 		/* free extra copy of the data pages */
370af6d7b76SNeilBrown 		int i = r1_bio->behind_page_count;
3714e78064fSNeilBrown 		while (i--)
3722ca68f5eSNeilBrown 			safe_put_page(r1_bio->behind_bvecs[i].bv_page);
3732ca68f5eSNeilBrown 		kfree(r1_bio->behind_bvecs);
3742ca68f5eSNeilBrown 		r1_bio->behind_bvecs = NULL;
3754e78064fSNeilBrown 	}
3764e78064fSNeilBrown 	/* clear the bitmap if all writes complete successfully */
3774e78064fSNeilBrown 	bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
3784e78064fSNeilBrown 			r1_bio->sectors,
3794e78064fSNeilBrown 			!test_bit(R1BIO_Degraded, &r1_bio->state),
380af6d7b76SNeilBrown 			test_bit(R1BIO_BehindIO, &r1_bio->state));
3814e78064fSNeilBrown 	md_write_end(r1_bio->mddev);
382cd5ff9a1SNeilBrown }
383cd5ff9a1SNeilBrown 
3849f2c9d12SNeilBrown static void r1_bio_write_done(struct r1bio *r1_bio)
385cd5ff9a1SNeilBrown {
386cd5ff9a1SNeilBrown 	if (!atomic_dec_and_test(&r1_bio->remaining))
387cd5ff9a1SNeilBrown 		return;
388cd5ff9a1SNeilBrown 
389cd5ff9a1SNeilBrown 	if (test_bit(R1BIO_WriteError, &r1_bio->state))
390cd5ff9a1SNeilBrown 		reschedule_retry(r1_bio);
391cd5ff9a1SNeilBrown 	else {
392cd5ff9a1SNeilBrown 		close_write(r1_bio);
3934367af55SNeilBrown 		if (test_bit(R1BIO_MadeGood, &r1_bio->state))
3944367af55SNeilBrown 			reschedule_retry(r1_bio);
3954367af55SNeilBrown 		else
3964e78064fSNeilBrown 			raid_end_bio_io(r1_bio);
3974e78064fSNeilBrown 	}
3984e78064fSNeilBrown }
3994e78064fSNeilBrown 
4006712ecf8SNeilBrown static void raid1_end_write_request(struct bio *bio, int error)
4011da177e4SLinus Torvalds {
4021da177e4SLinus Torvalds 	int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
4039f2c9d12SNeilBrown 	struct r1bio *r1_bio = bio->bi_private;
404a9701a30SNeilBrown 	int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
405e8096360SNeilBrown 	struct r1conf *conf = r1_bio->mddev->private;
40604b857f7SNeilBrown 	struct bio *to_put = NULL;
4071da177e4SLinus Torvalds 
408ba3ae3beSNamhyung Kim 	mirror = find_bio_disk(r1_bio, bio);
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	/*
411e9c7469bSTejun Heo 	 * 'one mirror IO has finished' event handler:
4121da177e4SLinus Torvalds 	 */
413191ea9b2SNeilBrown 	if (!uptodate) {
414cd5ff9a1SNeilBrown 		set_bit(WriteErrorSeen,
415cd5ff9a1SNeilBrown 			&conf->mirrors[mirror].rdev->flags);
41619d67169SNeilBrown 		if (!test_and_set_bit(WantReplacement,
41719d67169SNeilBrown 				      &conf->mirrors[mirror].rdev->flags))
41819d67169SNeilBrown 			set_bit(MD_RECOVERY_NEEDED, &
41919d67169SNeilBrown 				conf->mddev->recovery);
42019d67169SNeilBrown 
421cd5ff9a1SNeilBrown 		set_bit(R1BIO_WriteError, &r1_bio->state);
4224367af55SNeilBrown 	} else {
4231da177e4SLinus Torvalds 		/*
424e9c7469bSTejun Heo 		 * Set R1BIO_Uptodate in our master bio, so that we
425e9c7469bSTejun Heo 		 * will return a good error code for to the higher
426e9c7469bSTejun Heo 		 * levels even if IO on some other mirrored buffer
427e9c7469bSTejun Heo 		 * fails.
4281da177e4SLinus Torvalds 		 *
429e9c7469bSTejun Heo 		 * The 'master' represents the composite IO operation
430e9c7469bSTejun Heo 		 * to user-side. So if something waits for IO, then it
431e9c7469bSTejun Heo 		 * will wait for the 'master' bio.
4321da177e4SLinus Torvalds 		 */
4334367af55SNeilBrown 		sector_t first_bad;
4344367af55SNeilBrown 		int bad_sectors;
4354367af55SNeilBrown 
436cd5ff9a1SNeilBrown 		r1_bio->bios[mirror] = NULL;
437cd5ff9a1SNeilBrown 		to_put = bio;
4383056e3aeSAlex Lyakas 		/*
4393056e3aeSAlex Lyakas 		 * Do not set R1BIO_Uptodate if the current device is
4403056e3aeSAlex Lyakas 		 * rebuilding or Faulty. This is because we cannot use
4413056e3aeSAlex Lyakas 		 * such device for properly reading the data back (we could
4423056e3aeSAlex Lyakas 		 * potentially use it, if the current write would have felt
4433056e3aeSAlex Lyakas 		 * before rdev->recovery_offset, but for simplicity we don't
4443056e3aeSAlex Lyakas 		 * check this here.
4453056e3aeSAlex Lyakas 		 */
4463056e3aeSAlex Lyakas 		if (test_bit(In_sync, &conf->mirrors[mirror].rdev->flags) &&
4473056e3aeSAlex Lyakas 		    !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags))
4481da177e4SLinus Torvalds 			set_bit(R1BIO_Uptodate, &r1_bio->state);
4491da177e4SLinus Torvalds 
4504367af55SNeilBrown 		/* Maybe we can clear some bad blocks. */
4514367af55SNeilBrown 		if (is_badblock(conf->mirrors[mirror].rdev,
4524367af55SNeilBrown 				r1_bio->sector, r1_bio->sectors,
4534367af55SNeilBrown 				&first_bad, &bad_sectors)) {
4544367af55SNeilBrown 			r1_bio->bios[mirror] = IO_MADE_GOOD;
4554367af55SNeilBrown 			set_bit(R1BIO_MadeGood, &r1_bio->state);
4564367af55SNeilBrown 		}
4574367af55SNeilBrown 	}
4584367af55SNeilBrown 
4594b6d287fSNeilBrown 	if (behind) {
4604b6d287fSNeilBrown 		if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags))
4614b6d287fSNeilBrown 			atomic_dec(&r1_bio->behind_remaining);
4624b6d287fSNeilBrown 
463e9c7469bSTejun Heo 		/*
464e9c7469bSTejun Heo 		 * In behind mode, we ACK the master bio once the I/O
465e9c7469bSTejun Heo 		 * has safely reached all non-writemostly
466e9c7469bSTejun Heo 		 * disks. Setting the Returned bit ensures that this
467e9c7469bSTejun Heo 		 * gets done only once -- we don't ever want to return
468e9c7469bSTejun Heo 		 * -EIO here, instead we'll wait
469e9c7469bSTejun Heo 		 */
4704b6d287fSNeilBrown 		if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
4714b6d287fSNeilBrown 		    test_bit(R1BIO_Uptodate, &r1_bio->state)) {
4724b6d287fSNeilBrown 			/* Maybe we can return now */
4734b6d287fSNeilBrown 			if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
4744b6d287fSNeilBrown 				struct bio *mbio = r1_bio->master_bio;
47536a4e1feSNeilBrown 				pr_debug("raid1: behind end write sectors"
47636a4e1feSNeilBrown 					 " %llu-%llu\n",
4774f024f37SKent Overstreet 					 (unsigned long long) mbio->bi_iter.bi_sector,
4784f024f37SKent Overstreet 					 (unsigned long long) bio_end_sector(mbio) - 1);
479d2eb35acSNeilBrown 				call_bio_endio(r1_bio);
4804b6d287fSNeilBrown 			}
4814b6d287fSNeilBrown 		}
4824b6d287fSNeilBrown 	}
4834367af55SNeilBrown 	if (r1_bio->bios[mirror] == NULL)
4844367af55SNeilBrown 		rdev_dec_pending(conf->mirrors[mirror].rdev,
4854367af55SNeilBrown 				 conf->mddev);
486e9c7469bSTejun Heo 
4871da177e4SLinus Torvalds 	/*
4881da177e4SLinus Torvalds 	 * Let's see if all mirrored write operations have finished
4891da177e4SLinus Torvalds 	 * already.
4901da177e4SLinus Torvalds 	 */
491af6d7b76SNeilBrown 	r1_bio_write_done(r1_bio);
492c70810b3SNeilBrown 
49304b857f7SNeilBrown 	if (to_put)
49404b857f7SNeilBrown 		bio_put(to_put);
4951da177e4SLinus Torvalds }
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds /*
4991da177e4SLinus Torvalds  * This routine returns the disk from which the requested read should
5001da177e4SLinus Torvalds  * be done. There is a per-array 'next expected sequential IO' sector
5011da177e4SLinus Torvalds  * number - if this matches on the next IO then we use the last disk.
5021da177e4SLinus Torvalds  * There is also a per-disk 'last know head position' sector that is
5031da177e4SLinus Torvalds  * maintained from IRQ contexts, both the normal and the resync IO
5041da177e4SLinus Torvalds  * completion handlers update this position correctly. If there is no
5051da177e4SLinus Torvalds  * perfect sequential match then we pick the disk whose head is closest.
5061da177e4SLinus Torvalds  *
5071da177e4SLinus Torvalds  * If there are 2 mirrors in the same 2 devices, performance degrades
5081da177e4SLinus Torvalds  * because position is mirror, not device based.
5091da177e4SLinus Torvalds  *
5101da177e4SLinus Torvalds  * The rdev for the device selected will have nr_pending incremented.
5111da177e4SLinus Torvalds  */
512e8096360SNeilBrown static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sectors)
5131da177e4SLinus Torvalds {
514af3a2cd6SNeilBrown 	const sector_t this_sector = r1_bio->sector;
515d2eb35acSNeilBrown 	int sectors;
516d2eb35acSNeilBrown 	int best_good_sectors;
5179dedf603SShaohua Li 	int best_disk, best_dist_disk, best_pending_disk;
5189dedf603SShaohua Li 	int has_nonrot_disk;
519be4d3280SShaohua Li 	int disk;
52076073054SNeilBrown 	sector_t best_dist;
5219dedf603SShaohua Li 	unsigned int min_pending;
5223cb03002SNeilBrown 	struct md_rdev *rdev;
523f3ac8bf7SNeilBrown 	int choose_first;
52412cee5a8SShaohua Li 	int choose_next_idle;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	rcu_read_lock();
5271da177e4SLinus Torvalds 	/*
5288ddf9efeSNeilBrown 	 * Check if we can balance. We can balance on the whole
5291da177e4SLinus Torvalds 	 * device if no resync is going on, or below the resync window.
5301da177e4SLinus Torvalds 	 * We take the first readable disk when above the resync window.
5311da177e4SLinus Torvalds 	 */
5321da177e4SLinus Torvalds  retry:
533d2eb35acSNeilBrown 	sectors = r1_bio->sectors;
53476073054SNeilBrown 	best_disk = -1;
5359dedf603SShaohua Li 	best_dist_disk = -1;
53676073054SNeilBrown 	best_dist = MaxSector;
5379dedf603SShaohua Li 	best_pending_disk = -1;
5389dedf603SShaohua Li 	min_pending = UINT_MAX;
539d2eb35acSNeilBrown 	best_good_sectors = 0;
5409dedf603SShaohua Li 	has_nonrot_disk = 0;
54112cee5a8SShaohua Li 	choose_next_idle = 0;
542d2eb35acSNeilBrown 
5431da177e4SLinus Torvalds 	if (conf->mddev->recovery_cp < MaxSector &&
544be4d3280SShaohua Li 	    (this_sector + sectors >= conf->next_resync))
545f3ac8bf7SNeilBrown 		choose_first = 1;
546be4d3280SShaohua Li 	else
547f3ac8bf7SNeilBrown 		choose_first = 0;
5481da177e4SLinus Torvalds 
549be4d3280SShaohua Li 	for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
55076073054SNeilBrown 		sector_t dist;
551d2eb35acSNeilBrown 		sector_t first_bad;
552d2eb35acSNeilBrown 		int bad_sectors;
5539dedf603SShaohua Li 		unsigned int pending;
55412cee5a8SShaohua Li 		bool nonrot;
555d2eb35acSNeilBrown 
556f3ac8bf7SNeilBrown 		rdev = rcu_dereference(conf->mirrors[disk].rdev);
557f3ac8bf7SNeilBrown 		if (r1_bio->bios[disk] == IO_BLOCKED
558f3ac8bf7SNeilBrown 		    || rdev == NULL
5596b740b8dSNeilBrown 		    || test_bit(Unmerged, &rdev->flags)
56076073054SNeilBrown 		    || test_bit(Faulty, &rdev->flags))
561f3ac8bf7SNeilBrown 			continue;
56276073054SNeilBrown 		if (!test_bit(In_sync, &rdev->flags) &&
56376073054SNeilBrown 		    rdev->recovery_offset < this_sector + sectors)
56476073054SNeilBrown 			continue;
56576073054SNeilBrown 		if (test_bit(WriteMostly, &rdev->flags)) {
56676073054SNeilBrown 			/* Don't balance among write-mostly, just
56776073054SNeilBrown 			 * use the first as a last resort */
568307729c8SNeilBrown 			if (best_disk < 0) {
569307729c8SNeilBrown 				if (is_badblock(rdev, this_sector, sectors,
570307729c8SNeilBrown 						&first_bad, &bad_sectors)) {
571307729c8SNeilBrown 					if (first_bad < this_sector)
572307729c8SNeilBrown 						/* Cannot use this */
573307729c8SNeilBrown 						continue;
574307729c8SNeilBrown 					best_good_sectors = first_bad - this_sector;
575307729c8SNeilBrown 				} else
576307729c8SNeilBrown 					best_good_sectors = sectors;
57776073054SNeilBrown 				best_disk = disk;
578307729c8SNeilBrown 			}
57976073054SNeilBrown 			continue;
5808ddf9efeSNeilBrown 		}
58176073054SNeilBrown 		/* This is a reasonable device to use.  It might
58276073054SNeilBrown 		 * even be best.
5831da177e4SLinus Torvalds 		 */
584d2eb35acSNeilBrown 		if (is_badblock(rdev, this_sector, sectors,
585d2eb35acSNeilBrown 				&first_bad, &bad_sectors)) {
586d2eb35acSNeilBrown 			if (best_dist < MaxSector)
587d2eb35acSNeilBrown 				/* already have a better device */
588d2eb35acSNeilBrown 				continue;
589d2eb35acSNeilBrown 			if (first_bad <= this_sector) {
590d2eb35acSNeilBrown 				/* cannot read here. If this is the 'primary'
591d2eb35acSNeilBrown 				 * device, then we must not read beyond
592d2eb35acSNeilBrown 				 * bad_sectors from another device..
593d2eb35acSNeilBrown 				 */
594d2eb35acSNeilBrown 				bad_sectors -= (this_sector - first_bad);
595d2eb35acSNeilBrown 				if (choose_first && sectors > bad_sectors)
596d2eb35acSNeilBrown 					sectors = bad_sectors;
597d2eb35acSNeilBrown 				if (best_good_sectors > sectors)
598d2eb35acSNeilBrown 					best_good_sectors = sectors;
599d2eb35acSNeilBrown 
600d2eb35acSNeilBrown 			} else {
601d2eb35acSNeilBrown 				sector_t good_sectors = first_bad - this_sector;
602d2eb35acSNeilBrown 				if (good_sectors > best_good_sectors) {
603d2eb35acSNeilBrown 					best_good_sectors = good_sectors;
604d2eb35acSNeilBrown 					best_disk = disk;
605d2eb35acSNeilBrown 				}
606d2eb35acSNeilBrown 				if (choose_first)
607d2eb35acSNeilBrown 					break;
608d2eb35acSNeilBrown 			}
609d2eb35acSNeilBrown 			continue;
610d2eb35acSNeilBrown 		} else
611d2eb35acSNeilBrown 			best_good_sectors = sectors;
612d2eb35acSNeilBrown 
61312cee5a8SShaohua Li 		nonrot = blk_queue_nonrot(bdev_get_queue(rdev->bdev));
61412cee5a8SShaohua Li 		has_nonrot_disk |= nonrot;
6159dedf603SShaohua Li 		pending = atomic_read(&rdev->nr_pending);
61676073054SNeilBrown 		dist = abs(this_sector - conf->mirrors[disk].head_position);
61712cee5a8SShaohua Li 		if (choose_first) {
61876073054SNeilBrown 			best_disk = disk;
6191da177e4SLinus Torvalds 			break;
6201da177e4SLinus Torvalds 		}
62112cee5a8SShaohua Li 		/* Don't change to another disk for sequential reads */
62212cee5a8SShaohua Li 		if (conf->mirrors[disk].next_seq_sect == this_sector
62312cee5a8SShaohua Li 		    || dist == 0) {
62412cee5a8SShaohua Li 			int opt_iosize = bdev_io_opt(rdev->bdev) >> 9;
62512cee5a8SShaohua Li 			struct raid1_info *mirror = &conf->mirrors[disk];
62612cee5a8SShaohua Li 
62712cee5a8SShaohua Li 			best_disk = disk;
62812cee5a8SShaohua Li 			/*
62912cee5a8SShaohua Li 			 * If buffered sequential IO size exceeds optimal
63012cee5a8SShaohua Li 			 * iosize, check if there is idle disk. If yes, choose
63112cee5a8SShaohua Li 			 * the idle disk. read_balance could already choose an
63212cee5a8SShaohua Li 			 * idle disk before noticing it's a sequential IO in
63312cee5a8SShaohua Li 			 * this disk. This doesn't matter because this disk
63412cee5a8SShaohua Li 			 * will idle, next time it will be utilized after the
63512cee5a8SShaohua Li 			 * first disk has IO size exceeds optimal iosize. In
63612cee5a8SShaohua Li 			 * this way, iosize of the first disk will be optimal
63712cee5a8SShaohua Li 			 * iosize at least. iosize of the second disk might be
63812cee5a8SShaohua Li 			 * small, but not a big deal since when the second disk
63912cee5a8SShaohua Li 			 * starts IO, the first disk is likely still busy.
64012cee5a8SShaohua Li 			 */
64112cee5a8SShaohua Li 			if (nonrot && opt_iosize > 0 &&
64212cee5a8SShaohua Li 			    mirror->seq_start != MaxSector &&
64312cee5a8SShaohua Li 			    mirror->next_seq_sect > opt_iosize &&
64412cee5a8SShaohua Li 			    mirror->next_seq_sect - opt_iosize >=
64512cee5a8SShaohua Li 			    mirror->seq_start) {
64612cee5a8SShaohua Li 				choose_next_idle = 1;
64712cee5a8SShaohua Li 				continue;
64812cee5a8SShaohua Li 			}
64912cee5a8SShaohua Li 			break;
65012cee5a8SShaohua Li 		}
65112cee5a8SShaohua Li 		/* If device is idle, use it */
65212cee5a8SShaohua Li 		if (pending == 0) {
65312cee5a8SShaohua Li 			best_disk = disk;
65412cee5a8SShaohua Li 			break;
65512cee5a8SShaohua Li 		}
65612cee5a8SShaohua Li 
65712cee5a8SShaohua Li 		if (choose_next_idle)
65812cee5a8SShaohua Li 			continue;
6599dedf603SShaohua Li 
6609dedf603SShaohua Li 		if (min_pending > pending) {
6619dedf603SShaohua Li 			min_pending = pending;
6629dedf603SShaohua Li 			best_pending_disk = disk;
6639dedf603SShaohua Li 		}
6649dedf603SShaohua Li 
66576073054SNeilBrown 		if (dist < best_dist) {
66676073054SNeilBrown 			best_dist = dist;
6679dedf603SShaohua Li 			best_dist_disk = disk;
6681da177e4SLinus Torvalds 		}
669f3ac8bf7SNeilBrown 	}
6701da177e4SLinus Torvalds 
6719dedf603SShaohua Li 	/*
6729dedf603SShaohua Li 	 * If all disks are rotational, choose the closest disk. If any disk is
6739dedf603SShaohua Li 	 * non-rotational, choose the disk with less pending request even the
6749dedf603SShaohua Li 	 * disk is rotational, which might/might not be optimal for raids with
6759dedf603SShaohua Li 	 * mixed ratation/non-rotational disks depending on workload.
6769dedf603SShaohua Li 	 */
6779dedf603SShaohua Li 	if (best_disk == -1) {
6789dedf603SShaohua Li 		if (has_nonrot_disk)
6799dedf603SShaohua Li 			best_disk = best_pending_disk;
6809dedf603SShaohua Li 		else
6819dedf603SShaohua Li 			best_disk = best_dist_disk;
6829dedf603SShaohua Li 	}
6839dedf603SShaohua Li 
68476073054SNeilBrown 	if (best_disk >= 0) {
68576073054SNeilBrown 		rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
6868ddf9efeSNeilBrown 		if (!rdev)
6878ddf9efeSNeilBrown 			goto retry;
6888ddf9efeSNeilBrown 		atomic_inc(&rdev->nr_pending);
68976073054SNeilBrown 		if (test_bit(Faulty, &rdev->flags)) {
6901da177e4SLinus Torvalds 			/* cannot risk returning a device that failed
6911da177e4SLinus Torvalds 			 * before we inc'ed nr_pending
6921da177e4SLinus Torvalds 			 */
69303c902e1SNeilBrown 			rdev_dec_pending(rdev, conf->mddev);
6941da177e4SLinus Torvalds 			goto retry;
6951da177e4SLinus Torvalds 		}
696d2eb35acSNeilBrown 		sectors = best_good_sectors;
69712cee5a8SShaohua Li 
69812cee5a8SShaohua Li 		if (conf->mirrors[best_disk].next_seq_sect != this_sector)
69912cee5a8SShaohua Li 			conf->mirrors[best_disk].seq_start = this_sector;
70012cee5a8SShaohua Li 
701be4d3280SShaohua Li 		conf->mirrors[best_disk].next_seq_sect = this_sector + sectors;
7021da177e4SLinus Torvalds 	}
7031da177e4SLinus Torvalds 	rcu_read_unlock();
704d2eb35acSNeilBrown 	*max_sectors = sectors;
7051da177e4SLinus Torvalds 
70676073054SNeilBrown 	return best_disk;
7071da177e4SLinus Torvalds }
7081da177e4SLinus Torvalds 
7096b740b8dSNeilBrown static int raid1_mergeable_bvec(struct request_queue *q,
7106b740b8dSNeilBrown 				struct bvec_merge_data *bvm,
7116b740b8dSNeilBrown 				struct bio_vec *biovec)
7126b740b8dSNeilBrown {
7136b740b8dSNeilBrown 	struct mddev *mddev = q->queuedata;
7146b740b8dSNeilBrown 	struct r1conf *conf = mddev->private;
7156b740b8dSNeilBrown 	sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
7166b740b8dSNeilBrown 	int max = biovec->bv_len;
7176b740b8dSNeilBrown 
7186b740b8dSNeilBrown 	if (mddev->merge_check_needed) {
7196b740b8dSNeilBrown 		int disk;
7206b740b8dSNeilBrown 		rcu_read_lock();
7216b740b8dSNeilBrown 		for (disk = 0; disk < conf->raid_disks * 2; disk++) {
7226b740b8dSNeilBrown 			struct md_rdev *rdev = rcu_dereference(
7236b740b8dSNeilBrown 				conf->mirrors[disk].rdev);
7246b740b8dSNeilBrown 			if (rdev && !test_bit(Faulty, &rdev->flags)) {
7256b740b8dSNeilBrown 				struct request_queue *q =
7266b740b8dSNeilBrown 					bdev_get_queue(rdev->bdev);
7276b740b8dSNeilBrown 				if (q->merge_bvec_fn) {
7286b740b8dSNeilBrown 					bvm->bi_sector = sector +
7296b740b8dSNeilBrown 						rdev->data_offset;
7306b740b8dSNeilBrown 					bvm->bi_bdev = rdev->bdev;
7316b740b8dSNeilBrown 					max = min(max, q->merge_bvec_fn(
7326b740b8dSNeilBrown 							  q, bvm, biovec));
7336b740b8dSNeilBrown 				}
7346b740b8dSNeilBrown 			}
7356b740b8dSNeilBrown 		}
7366b740b8dSNeilBrown 		rcu_read_unlock();
7376b740b8dSNeilBrown 	}
7386b740b8dSNeilBrown 	return max;
7396b740b8dSNeilBrown 
7406b740b8dSNeilBrown }
7416b740b8dSNeilBrown 
742fd01b88cSNeilBrown int md_raid1_congested(struct mddev *mddev, int bits)
7430d129228SNeilBrown {
744e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
7450d129228SNeilBrown 	int i, ret = 0;
7460d129228SNeilBrown 
74734db0cd6SNeilBrown 	if ((bits & (1 << BDI_async_congested)) &&
74834db0cd6SNeilBrown 	    conf->pending_count >= max_queued_requests)
74934db0cd6SNeilBrown 		return 1;
75034db0cd6SNeilBrown 
7510d129228SNeilBrown 	rcu_read_lock();
752f53e29fcSNeilBrown 	for (i = 0; i < conf->raid_disks * 2; i++) {
7533cb03002SNeilBrown 		struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
7540d129228SNeilBrown 		if (rdev && !test_bit(Faulty, &rdev->flags)) {
755165125e1SJens Axboe 			struct request_queue *q = bdev_get_queue(rdev->bdev);
7560d129228SNeilBrown 
7571ed7242eSJonathan Brassow 			BUG_ON(!q);
7581ed7242eSJonathan Brassow 
7590d129228SNeilBrown 			/* Note the '|| 1' - when read_balance prefers
7600d129228SNeilBrown 			 * non-congested targets, it can be removed
7610d129228SNeilBrown 			 */
76291a9e99dSAlexander Beregalov 			if ((bits & (1<<BDI_async_congested)) || 1)
7630d129228SNeilBrown 				ret |= bdi_congested(&q->backing_dev_info, bits);
7640d129228SNeilBrown 			else
7650d129228SNeilBrown 				ret &= bdi_congested(&q->backing_dev_info, bits);
7660d129228SNeilBrown 		}
7670d129228SNeilBrown 	}
7680d129228SNeilBrown 	rcu_read_unlock();
7690d129228SNeilBrown 	return ret;
7700d129228SNeilBrown }
7711ed7242eSJonathan Brassow EXPORT_SYMBOL_GPL(md_raid1_congested);
7720d129228SNeilBrown 
7731ed7242eSJonathan Brassow static int raid1_congested(void *data, int bits)
7741ed7242eSJonathan Brassow {
775fd01b88cSNeilBrown 	struct mddev *mddev = data;
7761ed7242eSJonathan Brassow 
7771ed7242eSJonathan Brassow 	return mddev_congested(mddev, bits) ||
7781ed7242eSJonathan Brassow 		md_raid1_congested(mddev, bits);
7791ed7242eSJonathan Brassow }
7800d129228SNeilBrown 
781e8096360SNeilBrown static void flush_pending_writes(struct r1conf *conf)
782a35e63efSNeilBrown {
783a35e63efSNeilBrown 	/* Any writes that have been queued but are awaiting
784a35e63efSNeilBrown 	 * bitmap updates get flushed here.
785a35e63efSNeilBrown 	 */
786a35e63efSNeilBrown 	spin_lock_irq(&conf->device_lock);
787a35e63efSNeilBrown 
788a35e63efSNeilBrown 	if (conf->pending_bio_list.head) {
789a35e63efSNeilBrown 		struct bio *bio;
790a35e63efSNeilBrown 		bio = bio_list_get(&conf->pending_bio_list);
79134db0cd6SNeilBrown 		conf->pending_count = 0;
792a35e63efSNeilBrown 		spin_unlock_irq(&conf->device_lock);
793a35e63efSNeilBrown 		/* flush any pending bitmap writes to
794a35e63efSNeilBrown 		 * disk before proceeding w/ I/O */
795a35e63efSNeilBrown 		bitmap_unplug(conf->mddev->bitmap);
79634db0cd6SNeilBrown 		wake_up(&conf->wait_barrier);
797a35e63efSNeilBrown 
798a35e63efSNeilBrown 		while (bio) { /* submit pending writes */
799a35e63efSNeilBrown 			struct bio *next = bio->bi_next;
800a35e63efSNeilBrown 			bio->bi_next = NULL;
8012ff8cc2cSShaohua Li 			if (unlikely((bio->bi_rw & REQ_DISCARD) &&
8022ff8cc2cSShaohua Li 			    !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
8032ff8cc2cSShaohua Li 				/* Just ignore it */
8042ff8cc2cSShaohua Li 				bio_endio(bio, 0);
8052ff8cc2cSShaohua Li 			else
806a35e63efSNeilBrown 				generic_make_request(bio);
807a35e63efSNeilBrown 			bio = next;
808a35e63efSNeilBrown 		}
809a35e63efSNeilBrown 	} else
810a35e63efSNeilBrown 		spin_unlock_irq(&conf->device_lock);
8117eaceaccSJens Axboe }
8127eaceaccSJens Axboe 
81317999be4SNeilBrown /* Barriers....
81417999be4SNeilBrown  * Sometimes we need to suspend IO while we do something else,
81517999be4SNeilBrown  * either some resync/recovery, or reconfigure the array.
81617999be4SNeilBrown  * To do this we raise a 'barrier'.
81717999be4SNeilBrown  * The 'barrier' is a counter that can be raised multiple times
81817999be4SNeilBrown  * to count how many activities are happening which preclude
81917999be4SNeilBrown  * normal IO.
82017999be4SNeilBrown  * We can only raise the barrier if there is no pending IO.
82117999be4SNeilBrown  * i.e. if nr_pending == 0.
82217999be4SNeilBrown  * We choose only to raise the barrier if no-one is waiting for the
82317999be4SNeilBrown  * barrier to go down.  This means that as soon as an IO request
82417999be4SNeilBrown  * is ready, no other operations which require a barrier will start
82517999be4SNeilBrown  * until the IO request has had a chance.
82617999be4SNeilBrown  *
82717999be4SNeilBrown  * So: regular IO calls 'wait_barrier'.  When that returns there
82817999be4SNeilBrown  *    is no backgroup IO happening,  It must arrange to call
82917999be4SNeilBrown  *    allow_barrier when it has finished its IO.
83017999be4SNeilBrown  * backgroup IO calls must call raise_barrier.  Once that returns
83117999be4SNeilBrown  *    there is no normal IO happeing.  It must arrange to call
83217999be4SNeilBrown  *    lower_barrier when the particular background IO completes.
8331da177e4SLinus Torvalds  */
834e8096360SNeilBrown static void raise_barrier(struct r1conf *conf)
8351da177e4SLinus Torvalds {
8361da177e4SLinus Torvalds 	spin_lock_irq(&conf->resync_lock);
8371da177e4SLinus Torvalds 
83817999be4SNeilBrown 	/* Wait until no block IO is waiting */
83917999be4SNeilBrown 	wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
840eed8c02eSLukas Czerner 			    conf->resync_lock);
84117999be4SNeilBrown 
84217999be4SNeilBrown 	/* block any new IO from starting */
84317999be4SNeilBrown 	conf->barrier++;
84417999be4SNeilBrown 
84579ef3a8aSmajianpeng 	/* For these conditions we must wait:
84679ef3a8aSmajianpeng 	 * A: while the array is in frozen state
84779ef3a8aSmajianpeng 	 * B: while barrier >= RESYNC_DEPTH, meaning resync reach
84879ef3a8aSmajianpeng 	 *    the max count which allowed.
84979ef3a8aSmajianpeng 	 * C: next_resync + RESYNC_SECTORS > start_next_window, meaning
85079ef3a8aSmajianpeng 	 *    next resync will reach to the window which normal bios are
85179ef3a8aSmajianpeng 	 *    handling.
85279ef3a8aSmajianpeng 	 */
85317999be4SNeilBrown 	wait_event_lock_irq(conf->wait_barrier,
854b364e3d0Smajianpeng 			    !conf->array_frozen &&
85579ef3a8aSmajianpeng 			    conf->barrier < RESYNC_DEPTH &&
85679ef3a8aSmajianpeng 			    (conf->start_next_window >=
85779ef3a8aSmajianpeng 			     conf->next_resync + RESYNC_SECTORS),
858eed8c02eSLukas Czerner 			    conf->resync_lock);
85917999be4SNeilBrown 
8601da177e4SLinus Torvalds 	spin_unlock_irq(&conf->resync_lock);
8611da177e4SLinus Torvalds }
8621da177e4SLinus Torvalds 
863e8096360SNeilBrown static void lower_barrier(struct r1conf *conf)
86417999be4SNeilBrown {
86517999be4SNeilBrown 	unsigned long flags;
866709ae487SNeilBrown 	BUG_ON(conf->barrier <= 0);
86717999be4SNeilBrown 	spin_lock_irqsave(&conf->resync_lock, flags);
86817999be4SNeilBrown 	conf->barrier--;
86917999be4SNeilBrown 	spin_unlock_irqrestore(&conf->resync_lock, flags);
87017999be4SNeilBrown 	wake_up(&conf->wait_barrier);
87117999be4SNeilBrown }
87217999be4SNeilBrown 
87379ef3a8aSmajianpeng static bool need_to_wait_for_sync(struct r1conf *conf, struct bio *bio)
87417999be4SNeilBrown {
87579ef3a8aSmajianpeng 	bool wait = false;
87679ef3a8aSmajianpeng 
87779ef3a8aSmajianpeng 	if (conf->array_frozen || !bio)
87879ef3a8aSmajianpeng 		wait = true;
87979ef3a8aSmajianpeng 	else if (conf->barrier && bio_data_dir(bio) == WRITE) {
88079ef3a8aSmajianpeng 		if (conf->next_resync < RESYNC_WINDOW_SECTORS)
88179ef3a8aSmajianpeng 			wait = true;
88279ef3a8aSmajianpeng 		else if ((conf->next_resync - RESYNC_WINDOW_SECTORS
88379ef3a8aSmajianpeng 				>= bio_end_sector(bio)) ||
88479ef3a8aSmajianpeng 			 (conf->next_resync + NEXT_NORMALIO_DISTANCE
8854f024f37SKent Overstreet 				<= bio->bi_iter.bi_sector))
88679ef3a8aSmajianpeng 			wait = false;
88779ef3a8aSmajianpeng 		else
88879ef3a8aSmajianpeng 			wait = true;
88979ef3a8aSmajianpeng 	}
89079ef3a8aSmajianpeng 
89179ef3a8aSmajianpeng 	return wait;
89279ef3a8aSmajianpeng }
89379ef3a8aSmajianpeng 
89479ef3a8aSmajianpeng static sector_t wait_barrier(struct r1conf *conf, struct bio *bio)
89579ef3a8aSmajianpeng {
89679ef3a8aSmajianpeng 	sector_t sector = 0;
89779ef3a8aSmajianpeng 
89817999be4SNeilBrown 	spin_lock_irq(&conf->resync_lock);
89979ef3a8aSmajianpeng 	if (need_to_wait_for_sync(conf, bio)) {
90017999be4SNeilBrown 		conf->nr_waiting++;
901d6b42dcbSNeilBrown 		/* Wait for the barrier to drop.
902d6b42dcbSNeilBrown 		 * However if there are already pending
903d6b42dcbSNeilBrown 		 * requests (preventing the barrier from
904d6b42dcbSNeilBrown 		 * rising completely), and the
905d6b42dcbSNeilBrown 		 * pre-process bio queue isn't empty,
906d6b42dcbSNeilBrown 		 * then don't wait, as we need to empty
907d6b42dcbSNeilBrown 		 * that queue to get the nr_pending
908d6b42dcbSNeilBrown 		 * count down.
909d6b42dcbSNeilBrown 		 */
910d6b42dcbSNeilBrown 		wait_event_lock_irq(conf->wait_barrier,
911b364e3d0Smajianpeng 				    !conf->array_frozen &&
912b364e3d0Smajianpeng 				    (!conf->barrier ||
91379ef3a8aSmajianpeng 				    ((conf->start_next_window <
91479ef3a8aSmajianpeng 				      conf->next_resync + RESYNC_SECTORS) &&
915d6b42dcbSNeilBrown 				     current->bio_list &&
916b364e3d0Smajianpeng 				     !bio_list_empty(current->bio_list))),
917eed8c02eSLukas Czerner 				    conf->resync_lock);
91817999be4SNeilBrown 		conf->nr_waiting--;
91917999be4SNeilBrown 	}
92079ef3a8aSmajianpeng 
92179ef3a8aSmajianpeng 	if (bio && bio_data_dir(bio) == WRITE) {
92279ef3a8aSmajianpeng 		if (conf->next_resync + NEXT_NORMALIO_DISTANCE
9234f024f37SKent Overstreet 		    <= bio->bi_iter.bi_sector) {
92479ef3a8aSmajianpeng 			if (conf->start_next_window == MaxSector)
92579ef3a8aSmajianpeng 				conf->start_next_window =
92679ef3a8aSmajianpeng 					conf->next_resync +
92779ef3a8aSmajianpeng 					NEXT_NORMALIO_DISTANCE;
92879ef3a8aSmajianpeng 
92979ef3a8aSmajianpeng 			if ((conf->start_next_window + NEXT_NORMALIO_DISTANCE)
9304f024f37SKent Overstreet 			    <= bio->bi_iter.bi_sector)
93179ef3a8aSmajianpeng 				conf->next_window_requests++;
93279ef3a8aSmajianpeng 			else
93379ef3a8aSmajianpeng 				conf->current_window_requests++;
93479ef3a8aSmajianpeng 			sector = conf->start_next_window;
93517999be4SNeilBrown 		}
93641a336e0SNeilBrown 	}
93717999be4SNeilBrown 
93879ef3a8aSmajianpeng 	conf->nr_pending++;
93979ef3a8aSmajianpeng 	spin_unlock_irq(&conf->resync_lock);
94079ef3a8aSmajianpeng 	return sector;
94179ef3a8aSmajianpeng }
94279ef3a8aSmajianpeng 
94379ef3a8aSmajianpeng static void allow_barrier(struct r1conf *conf, sector_t start_next_window,
94479ef3a8aSmajianpeng 			  sector_t bi_sector)
94517999be4SNeilBrown {
94617999be4SNeilBrown 	unsigned long flags;
94779ef3a8aSmajianpeng 
94817999be4SNeilBrown 	spin_lock_irqsave(&conf->resync_lock, flags);
94917999be4SNeilBrown 	conf->nr_pending--;
95079ef3a8aSmajianpeng 	if (start_next_window) {
95179ef3a8aSmajianpeng 		if (start_next_window == conf->start_next_window) {
95279ef3a8aSmajianpeng 			if (conf->start_next_window + NEXT_NORMALIO_DISTANCE
95379ef3a8aSmajianpeng 			    <= bi_sector)
95479ef3a8aSmajianpeng 				conf->next_window_requests--;
95579ef3a8aSmajianpeng 			else
95679ef3a8aSmajianpeng 				conf->current_window_requests--;
95779ef3a8aSmajianpeng 		} else
95879ef3a8aSmajianpeng 			conf->current_window_requests--;
95979ef3a8aSmajianpeng 
96079ef3a8aSmajianpeng 		if (!conf->current_window_requests) {
96179ef3a8aSmajianpeng 			if (conf->next_window_requests) {
96279ef3a8aSmajianpeng 				conf->current_window_requests =
96379ef3a8aSmajianpeng 					conf->next_window_requests;
96479ef3a8aSmajianpeng 				conf->next_window_requests = 0;
96579ef3a8aSmajianpeng 				conf->start_next_window +=
96679ef3a8aSmajianpeng 					NEXT_NORMALIO_DISTANCE;
96779ef3a8aSmajianpeng 			} else
96879ef3a8aSmajianpeng 				conf->start_next_window = MaxSector;
96979ef3a8aSmajianpeng 		}
97079ef3a8aSmajianpeng 	}
97117999be4SNeilBrown 	spin_unlock_irqrestore(&conf->resync_lock, flags);
97217999be4SNeilBrown 	wake_up(&conf->wait_barrier);
97317999be4SNeilBrown }
97417999be4SNeilBrown 
975e2d59925SNeilBrown static void freeze_array(struct r1conf *conf, int extra)
976ddaf22abSNeilBrown {
977ddaf22abSNeilBrown 	/* stop syncio and normal IO and wait for everything to
978ddaf22abSNeilBrown 	 * go quite.
979b364e3d0Smajianpeng 	 * We wait until nr_pending match nr_queued+extra
9801c830532SNeilBrown 	 * This is called in the context of one normal IO request
9811c830532SNeilBrown 	 * that has failed. Thus any sync request that might be pending
9821c830532SNeilBrown 	 * will be blocked by nr_pending, and we need to wait for
9831c830532SNeilBrown 	 * pending IO requests to complete or be queued for re-try.
984e2d59925SNeilBrown 	 * Thus the number queued (nr_queued) plus this request (extra)
9851c830532SNeilBrown 	 * must match the number of pending IOs (nr_pending) before
9861c830532SNeilBrown 	 * we continue.
987ddaf22abSNeilBrown 	 */
988ddaf22abSNeilBrown 	spin_lock_irq(&conf->resync_lock);
989b364e3d0Smajianpeng 	conf->array_frozen = 1;
990eed8c02eSLukas Czerner 	wait_event_lock_irq_cmd(conf->wait_barrier,
991e2d59925SNeilBrown 				conf->nr_pending == conf->nr_queued+extra,
992ddaf22abSNeilBrown 				conf->resync_lock,
993c3b328acSNeilBrown 				flush_pending_writes(conf));
994ddaf22abSNeilBrown 	spin_unlock_irq(&conf->resync_lock);
995ddaf22abSNeilBrown }
996e8096360SNeilBrown static void unfreeze_array(struct r1conf *conf)
997ddaf22abSNeilBrown {
998ddaf22abSNeilBrown 	/* reverse the effect of the freeze */
999ddaf22abSNeilBrown 	spin_lock_irq(&conf->resync_lock);
1000b364e3d0Smajianpeng 	conf->array_frozen = 0;
1001ddaf22abSNeilBrown 	wake_up(&conf->wait_barrier);
1002ddaf22abSNeilBrown 	spin_unlock_irq(&conf->resync_lock);
1003ddaf22abSNeilBrown }
1004ddaf22abSNeilBrown 
100517999be4SNeilBrown 
10064e78064fSNeilBrown /* duplicate the data pages for behind I/O
10074e78064fSNeilBrown  */
10089f2c9d12SNeilBrown static void alloc_behind_pages(struct bio *bio, struct r1bio *r1_bio)
10094b6d287fSNeilBrown {
10104b6d287fSNeilBrown 	int i;
10114b6d287fSNeilBrown 	struct bio_vec *bvec;
10122ca68f5eSNeilBrown 	struct bio_vec *bvecs = kzalloc(bio->bi_vcnt * sizeof(struct bio_vec),
10134b6d287fSNeilBrown 					GFP_NOIO);
10142ca68f5eSNeilBrown 	if (unlikely(!bvecs))
1015af6d7b76SNeilBrown 		return;
10164b6d287fSNeilBrown 
1017cb34e057SKent Overstreet 	bio_for_each_segment_all(bvec, bio, i) {
10182ca68f5eSNeilBrown 		bvecs[i] = *bvec;
10192ca68f5eSNeilBrown 		bvecs[i].bv_page = alloc_page(GFP_NOIO);
10202ca68f5eSNeilBrown 		if (unlikely(!bvecs[i].bv_page))
10214b6d287fSNeilBrown 			goto do_sync_io;
10222ca68f5eSNeilBrown 		memcpy(kmap(bvecs[i].bv_page) + bvec->bv_offset,
10234b6d287fSNeilBrown 		       kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
10242ca68f5eSNeilBrown 		kunmap(bvecs[i].bv_page);
10254b6d287fSNeilBrown 		kunmap(bvec->bv_page);
10264b6d287fSNeilBrown 	}
10272ca68f5eSNeilBrown 	r1_bio->behind_bvecs = bvecs;
1028af6d7b76SNeilBrown 	r1_bio->behind_page_count = bio->bi_vcnt;
1029af6d7b76SNeilBrown 	set_bit(R1BIO_BehindIO, &r1_bio->state);
1030af6d7b76SNeilBrown 	return;
10314b6d287fSNeilBrown 
10324b6d287fSNeilBrown do_sync_io:
1033af6d7b76SNeilBrown 	for (i = 0; i < bio->bi_vcnt; i++)
10342ca68f5eSNeilBrown 		if (bvecs[i].bv_page)
10352ca68f5eSNeilBrown 			put_page(bvecs[i].bv_page);
10362ca68f5eSNeilBrown 	kfree(bvecs);
10374f024f37SKent Overstreet 	pr_debug("%dB behind alloc failed, doing sync I/O\n",
10384f024f37SKent Overstreet 		 bio->bi_iter.bi_size);
10394b6d287fSNeilBrown }
10404b6d287fSNeilBrown 
1041f54a9d0eSNeilBrown struct raid1_plug_cb {
1042f54a9d0eSNeilBrown 	struct blk_plug_cb	cb;
1043f54a9d0eSNeilBrown 	struct bio_list		pending;
1044f54a9d0eSNeilBrown 	int			pending_cnt;
1045f54a9d0eSNeilBrown };
1046f54a9d0eSNeilBrown 
1047f54a9d0eSNeilBrown static void raid1_unplug(struct blk_plug_cb *cb, bool from_schedule)
1048f54a9d0eSNeilBrown {
1049f54a9d0eSNeilBrown 	struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb,
1050f54a9d0eSNeilBrown 						  cb);
1051f54a9d0eSNeilBrown 	struct mddev *mddev = plug->cb.data;
1052f54a9d0eSNeilBrown 	struct r1conf *conf = mddev->private;
1053f54a9d0eSNeilBrown 	struct bio *bio;
1054f54a9d0eSNeilBrown 
1055874807a8SNeilBrown 	if (from_schedule || current->bio_list) {
1056f54a9d0eSNeilBrown 		spin_lock_irq(&conf->device_lock);
1057f54a9d0eSNeilBrown 		bio_list_merge(&conf->pending_bio_list, &plug->pending);
1058f54a9d0eSNeilBrown 		conf->pending_count += plug->pending_cnt;
1059f54a9d0eSNeilBrown 		spin_unlock_irq(&conf->device_lock);
1060ee0b0244SNeilBrown 		wake_up(&conf->wait_barrier);
1061f54a9d0eSNeilBrown 		md_wakeup_thread(mddev->thread);
1062f54a9d0eSNeilBrown 		kfree(plug);
1063f54a9d0eSNeilBrown 		return;
1064f54a9d0eSNeilBrown 	}
1065f54a9d0eSNeilBrown 
1066f54a9d0eSNeilBrown 	/* we aren't scheduling, so we can do the write-out directly. */
1067f54a9d0eSNeilBrown 	bio = bio_list_get(&plug->pending);
1068f54a9d0eSNeilBrown 	bitmap_unplug(mddev->bitmap);
1069f54a9d0eSNeilBrown 	wake_up(&conf->wait_barrier);
1070f54a9d0eSNeilBrown 
1071f54a9d0eSNeilBrown 	while (bio) { /* submit pending writes */
1072f54a9d0eSNeilBrown 		struct bio *next = bio->bi_next;
1073f54a9d0eSNeilBrown 		bio->bi_next = NULL;
107432f9f570SShaohua Li 		if (unlikely((bio->bi_rw & REQ_DISCARD) &&
107532f9f570SShaohua Li 		    !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
107632f9f570SShaohua Li 			/* Just ignore it */
107732f9f570SShaohua Li 			bio_endio(bio, 0);
107832f9f570SShaohua Li 		else
1079f54a9d0eSNeilBrown 			generic_make_request(bio);
1080f54a9d0eSNeilBrown 		bio = next;
1081f54a9d0eSNeilBrown 	}
1082f54a9d0eSNeilBrown 	kfree(plug);
1083f54a9d0eSNeilBrown }
1084f54a9d0eSNeilBrown 
1085b4fdcb02SLinus Torvalds static void make_request(struct mddev *mddev, struct bio * bio)
10861da177e4SLinus Torvalds {
1087e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
10880eaf822cSJonathan Brassow 	struct raid1_info *mirror;
10899f2c9d12SNeilBrown 	struct r1bio *r1_bio;
10901da177e4SLinus Torvalds 	struct bio *read_bio;
10911f68f0c4SNeilBrown 	int i, disks;
109284255d10SNeilBrown 	struct bitmap *bitmap;
1093191ea9b2SNeilBrown 	unsigned long flags;
1094a362357bSJens Axboe 	const int rw = bio_data_dir(bio);
10952c7d46ecSNeilBrown 	const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
1096e9c7469bSTejun Heo 	const unsigned long do_flush_fua = (bio->bi_rw & (REQ_FLUSH | REQ_FUA));
10972ff8cc2cSShaohua Li 	const unsigned long do_discard = (bio->bi_rw
10982ff8cc2cSShaohua Li 					  & (REQ_DISCARD | REQ_SECURE));
1099c8dc9c65SJoe Lawrence 	const unsigned long do_same = (bio->bi_rw & REQ_WRITE_SAME);
11003cb03002SNeilBrown 	struct md_rdev *blocked_rdev;
1101f54a9d0eSNeilBrown 	struct blk_plug_cb *cb;
1102f54a9d0eSNeilBrown 	struct raid1_plug_cb *plug = NULL;
11031f68f0c4SNeilBrown 	int first_clone;
11041f68f0c4SNeilBrown 	int sectors_handled;
11051f68f0c4SNeilBrown 	int max_sectors;
110679ef3a8aSmajianpeng 	sector_t start_next_window;
1107191ea9b2SNeilBrown 
11081da177e4SLinus Torvalds 	/*
11091da177e4SLinus Torvalds 	 * Register the new request and wait if the reconstruction
11101da177e4SLinus Torvalds 	 * thread has put up a bar for new requests.
11111da177e4SLinus Torvalds 	 * Continue immediately if no resync is active currently.
11121da177e4SLinus Torvalds 	 */
111362de608dSNeilBrown 
11143d310eb7SNeilBrown 	md_write_start(mddev, bio); /* wait on superblock update early */
11153d310eb7SNeilBrown 
11166eef4b21SNeilBrown 	if (bio_data_dir(bio) == WRITE &&
1117f73a1c7dSKent Overstreet 	    bio_end_sector(bio) > mddev->suspend_lo &&
11184f024f37SKent Overstreet 	    bio->bi_iter.bi_sector < mddev->suspend_hi) {
11196eef4b21SNeilBrown 		/* As the suspend_* range is controlled by
11206eef4b21SNeilBrown 		 * userspace, we want an interruptible
11216eef4b21SNeilBrown 		 * wait.
11226eef4b21SNeilBrown 		 */
11236eef4b21SNeilBrown 		DEFINE_WAIT(w);
11246eef4b21SNeilBrown 		for (;;) {
11256eef4b21SNeilBrown 			flush_signals(current);
11266eef4b21SNeilBrown 			prepare_to_wait(&conf->wait_barrier,
11276eef4b21SNeilBrown 					&w, TASK_INTERRUPTIBLE);
1128f73a1c7dSKent Overstreet 			if (bio_end_sector(bio) <= mddev->suspend_lo ||
11294f024f37SKent Overstreet 			    bio->bi_iter.bi_sector >= mddev->suspend_hi)
11306eef4b21SNeilBrown 				break;
11316eef4b21SNeilBrown 			schedule();
11326eef4b21SNeilBrown 		}
11336eef4b21SNeilBrown 		finish_wait(&conf->wait_barrier, &w);
11346eef4b21SNeilBrown 	}
113562de608dSNeilBrown 
113679ef3a8aSmajianpeng 	start_next_window = wait_barrier(conf, bio);
11371da177e4SLinus Torvalds 
113884255d10SNeilBrown 	bitmap = mddev->bitmap;
113984255d10SNeilBrown 
11401da177e4SLinus Torvalds 	/*
11411da177e4SLinus Torvalds 	 * make_request() can abort the operation when READA is being
11421da177e4SLinus Torvalds 	 * used and no empty request is available.
11431da177e4SLinus Torvalds 	 *
11441da177e4SLinus Torvalds 	 */
11451da177e4SLinus Torvalds 	r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	r1_bio->master_bio = bio;
1148aa8b57aaSKent Overstreet 	r1_bio->sectors = bio_sectors(bio);
1149191ea9b2SNeilBrown 	r1_bio->state = 0;
11501da177e4SLinus Torvalds 	r1_bio->mddev = mddev;
11514f024f37SKent Overstreet 	r1_bio->sector = bio->bi_iter.bi_sector;
11521da177e4SLinus Torvalds 
1153d2eb35acSNeilBrown 	/* We might need to issue multiple reads to different
1154d2eb35acSNeilBrown 	 * devices if there are bad blocks around, so we keep
1155d2eb35acSNeilBrown 	 * track of the number of reads in bio->bi_phys_segments.
1156d2eb35acSNeilBrown 	 * If this is 0, there is only one r1_bio and no locking
1157d2eb35acSNeilBrown 	 * will be needed when requests complete.  If it is
1158d2eb35acSNeilBrown 	 * non-zero, then it is the number of not-completed requests.
1159d2eb35acSNeilBrown 	 */
1160d2eb35acSNeilBrown 	bio->bi_phys_segments = 0;
1161d2eb35acSNeilBrown 	clear_bit(BIO_SEG_VALID, &bio->bi_flags);
1162d2eb35acSNeilBrown 
1163a362357bSJens Axboe 	if (rw == READ) {
11641da177e4SLinus Torvalds 		/*
11651da177e4SLinus Torvalds 		 * read balancing logic:
11661da177e4SLinus Torvalds 		 */
1167d2eb35acSNeilBrown 		int rdisk;
1168d2eb35acSNeilBrown 
1169d2eb35acSNeilBrown read_again:
1170d2eb35acSNeilBrown 		rdisk = read_balance(conf, r1_bio, &max_sectors);
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 		if (rdisk < 0) {
11731da177e4SLinus Torvalds 			/* couldn't find anywhere to read from */
11741da177e4SLinus Torvalds 			raid_end_bio_io(r1_bio);
11755a7bbad2SChristoph Hellwig 			return;
11761da177e4SLinus Torvalds 		}
11771da177e4SLinus Torvalds 		mirror = conf->mirrors + rdisk;
11781da177e4SLinus Torvalds 
1179e555190dSNeilBrown 		if (test_bit(WriteMostly, &mirror->rdev->flags) &&
1180e555190dSNeilBrown 		    bitmap) {
1181e555190dSNeilBrown 			/* Reading from a write-mostly device must
1182e555190dSNeilBrown 			 * take care not to over-take any writes
1183e555190dSNeilBrown 			 * that are 'behind'
1184e555190dSNeilBrown 			 */
1185e555190dSNeilBrown 			wait_event(bitmap->behind_wait,
1186e555190dSNeilBrown 				   atomic_read(&bitmap->behind_writes) == 0);
1187e555190dSNeilBrown 		}
11881da177e4SLinus Torvalds 		r1_bio->read_disk = rdisk;
11891da177e4SLinus Torvalds 
1190a167f663SNeilBrown 		read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
11914f024f37SKent Overstreet 		bio_trim(read_bio, r1_bio->sector - bio->bi_iter.bi_sector,
1192d2eb35acSNeilBrown 			 max_sectors);
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 		r1_bio->bios[rdisk] = read_bio;
11951da177e4SLinus Torvalds 
11964f024f37SKent Overstreet 		read_bio->bi_iter.bi_sector = r1_bio->sector +
11974f024f37SKent Overstreet 			mirror->rdev->data_offset;
11981da177e4SLinus Torvalds 		read_bio->bi_bdev = mirror->rdev->bdev;
11991da177e4SLinus Torvalds 		read_bio->bi_end_io = raid1_end_read_request;
12007b6d91daSChristoph Hellwig 		read_bio->bi_rw = READ | do_sync;
12011da177e4SLinus Torvalds 		read_bio->bi_private = r1_bio;
12021da177e4SLinus Torvalds 
1203d2eb35acSNeilBrown 		if (max_sectors < r1_bio->sectors) {
1204d2eb35acSNeilBrown 			/* could not read all from this device, so we will
1205d2eb35acSNeilBrown 			 * need another r1_bio.
1206d2eb35acSNeilBrown 			 */
1207d2eb35acSNeilBrown 
1208d2eb35acSNeilBrown 			sectors_handled = (r1_bio->sector + max_sectors
12094f024f37SKent Overstreet 					   - bio->bi_iter.bi_sector);
1210d2eb35acSNeilBrown 			r1_bio->sectors = max_sectors;
1211d2eb35acSNeilBrown 			spin_lock_irq(&conf->device_lock);
1212d2eb35acSNeilBrown 			if (bio->bi_phys_segments == 0)
1213d2eb35acSNeilBrown 				bio->bi_phys_segments = 2;
1214d2eb35acSNeilBrown 			else
1215d2eb35acSNeilBrown 				bio->bi_phys_segments++;
1216d2eb35acSNeilBrown 			spin_unlock_irq(&conf->device_lock);
1217d2eb35acSNeilBrown 			/* Cannot call generic_make_request directly
1218d2eb35acSNeilBrown 			 * as that will be queued in __make_request
1219d2eb35acSNeilBrown 			 * and subsequent mempool_alloc might block waiting
1220d2eb35acSNeilBrown 			 * for it.  So hand bio over to raid1d.
1221d2eb35acSNeilBrown 			 */
1222d2eb35acSNeilBrown 			reschedule_retry(r1_bio);
1223d2eb35acSNeilBrown 
1224d2eb35acSNeilBrown 			r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1225d2eb35acSNeilBrown 
1226d2eb35acSNeilBrown 			r1_bio->master_bio = bio;
1227aa8b57aaSKent Overstreet 			r1_bio->sectors = bio_sectors(bio) - sectors_handled;
1228d2eb35acSNeilBrown 			r1_bio->state = 0;
1229d2eb35acSNeilBrown 			r1_bio->mddev = mddev;
12304f024f37SKent Overstreet 			r1_bio->sector = bio->bi_iter.bi_sector +
12314f024f37SKent Overstreet 				sectors_handled;
1232d2eb35acSNeilBrown 			goto read_again;
1233d2eb35acSNeilBrown 		} else
12341da177e4SLinus Torvalds 			generic_make_request(read_bio);
12355a7bbad2SChristoph Hellwig 		return;
12361da177e4SLinus Torvalds 	}
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 	/*
12391da177e4SLinus Torvalds 	 * WRITE:
12401da177e4SLinus Torvalds 	 */
124134db0cd6SNeilBrown 	if (conf->pending_count >= max_queued_requests) {
124234db0cd6SNeilBrown 		md_wakeup_thread(mddev->thread);
124334db0cd6SNeilBrown 		wait_event(conf->wait_barrier,
124434db0cd6SNeilBrown 			   conf->pending_count < max_queued_requests);
124534db0cd6SNeilBrown 	}
12461f68f0c4SNeilBrown 	/* first select target devices under rcu_lock and
12471da177e4SLinus Torvalds 	 * inc refcount on their rdev.  Record them by setting
12481da177e4SLinus Torvalds 	 * bios[x] to bio
12491f68f0c4SNeilBrown 	 * If there are known/acknowledged bad blocks on any device on
12501f68f0c4SNeilBrown 	 * which we have seen a write error, we want to avoid writing those
12511f68f0c4SNeilBrown 	 * blocks.
12521f68f0c4SNeilBrown 	 * This potentially requires several writes to write around
12531f68f0c4SNeilBrown 	 * the bad blocks.  Each set of writes gets it's own r1bio
12541f68f0c4SNeilBrown 	 * with a set of bios attached.
12551da177e4SLinus Torvalds 	 */
1256c3b328acSNeilBrown 
12578f19ccb2SNeilBrown 	disks = conf->raid_disks * 2;
12586bfe0b49SDan Williams  retry_write:
125979ef3a8aSmajianpeng 	r1_bio->start_next_window = start_next_window;
12606bfe0b49SDan Williams 	blocked_rdev = NULL;
12611da177e4SLinus Torvalds 	rcu_read_lock();
12621f68f0c4SNeilBrown 	max_sectors = r1_bio->sectors;
12631da177e4SLinus Torvalds 	for (i = 0;  i < disks; i++) {
12643cb03002SNeilBrown 		struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
12656bfe0b49SDan Williams 		if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
12666bfe0b49SDan Williams 			atomic_inc(&rdev->nr_pending);
12676bfe0b49SDan Williams 			blocked_rdev = rdev;
12686bfe0b49SDan Williams 			break;
12696bfe0b49SDan Williams 		}
12701da177e4SLinus Torvalds 		r1_bio->bios[i] = NULL;
12716b740b8dSNeilBrown 		if (!rdev || test_bit(Faulty, &rdev->flags)
12726b740b8dSNeilBrown 		    || test_bit(Unmerged, &rdev->flags)) {
12738f19ccb2SNeilBrown 			if (i < conf->raid_disks)
12741f68f0c4SNeilBrown 				set_bit(R1BIO_Degraded, &r1_bio->state);
12751f68f0c4SNeilBrown 			continue;
1276964147d5SNeilBrown 		}
12771f68f0c4SNeilBrown 
12781f68f0c4SNeilBrown 		atomic_inc(&rdev->nr_pending);
12791f68f0c4SNeilBrown 		if (test_bit(WriteErrorSeen, &rdev->flags)) {
12801f68f0c4SNeilBrown 			sector_t first_bad;
12811f68f0c4SNeilBrown 			int bad_sectors;
12821f68f0c4SNeilBrown 			int is_bad;
12831f68f0c4SNeilBrown 
12841f68f0c4SNeilBrown 			is_bad = is_badblock(rdev, r1_bio->sector,
12851f68f0c4SNeilBrown 					     max_sectors,
12861f68f0c4SNeilBrown 					     &first_bad, &bad_sectors);
12871f68f0c4SNeilBrown 			if (is_bad < 0) {
12881f68f0c4SNeilBrown 				/* mustn't write here until the bad block is
12891f68f0c4SNeilBrown 				 * acknowledged*/
12901f68f0c4SNeilBrown 				set_bit(BlockedBadBlocks, &rdev->flags);
12911f68f0c4SNeilBrown 				blocked_rdev = rdev;
12921f68f0c4SNeilBrown 				break;
12931f68f0c4SNeilBrown 			}
12941f68f0c4SNeilBrown 			if (is_bad && first_bad <= r1_bio->sector) {
12951f68f0c4SNeilBrown 				/* Cannot write here at all */
12961f68f0c4SNeilBrown 				bad_sectors -= (r1_bio->sector - first_bad);
12971f68f0c4SNeilBrown 				if (bad_sectors < max_sectors)
12981f68f0c4SNeilBrown 					/* mustn't write more than bad_sectors
12991f68f0c4SNeilBrown 					 * to other devices yet
13001f68f0c4SNeilBrown 					 */
13011f68f0c4SNeilBrown 					max_sectors = bad_sectors;
13021f68f0c4SNeilBrown 				rdev_dec_pending(rdev, mddev);
13031f68f0c4SNeilBrown 				/* We don't set R1BIO_Degraded as that
13041f68f0c4SNeilBrown 				 * only applies if the disk is
13051f68f0c4SNeilBrown 				 * missing, so it might be re-added,
13061f68f0c4SNeilBrown 				 * and we want to know to recover this
13071f68f0c4SNeilBrown 				 * chunk.
13081f68f0c4SNeilBrown 				 * In this case the device is here,
13091f68f0c4SNeilBrown 				 * and the fact that this chunk is not
13101f68f0c4SNeilBrown 				 * in-sync is recorded in the bad
13111f68f0c4SNeilBrown 				 * block log
13121f68f0c4SNeilBrown 				 */
13131f68f0c4SNeilBrown 				continue;
13141f68f0c4SNeilBrown 			}
13151f68f0c4SNeilBrown 			if (is_bad) {
13161f68f0c4SNeilBrown 				int good_sectors = first_bad - r1_bio->sector;
13171f68f0c4SNeilBrown 				if (good_sectors < max_sectors)
13181f68f0c4SNeilBrown 					max_sectors = good_sectors;
13191f68f0c4SNeilBrown 			}
13201f68f0c4SNeilBrown 		}
13211f68f0c4SNeilBrown 		r1_bio->bios[i] = bio;
13221da177e4SLinus Torvalds 	}
13231da177e4SLinus Torvalds 	rcu_read_unlock();
13241da177e4SLinus Torvalds 
13256bfe0b49SDan Williams 	if (unlikely(blocked_rdev)) {
13266bfe0b49SDan Williams 		/* Wait for this device to become unblocked */
13276bfe0b49SDan Williams 		int j;
132879ef3a8aSmajianpeng 		sector_t old = start_next_window;
13296bfe0b49SDan Williams 
13306bfe0b49SDan Williams 		for (j = 0; j < i; j++)
13316bfe0b49SDan Williams 			if (r1_bio->bios[j])
13326bfe0b49SDan Williams 				rdev_dec_pending(conf->mirrors[j].rdev, mddev);
13331f68f0c4SNeilBrown 		r1_bio->state = 0;
13344f024f37SKent Overstreet 		allow_barrier(conf, start_next_window, bio->bi_iter.bi_sector);
13356bfe0b49SDan Williams 		md_wait_for_blocked_rdev(blocked_rdev, mddev);
133679ef3a8aSmajianpeng 		start_next_window = wait_barrier(conf, bio);
133779ef3a8aSmajianpeng 		/*
133879ef3a8aSmajianpeng 		 * We must make sure the multi r1bios of bio have
133979ef3a8aSmajianpeng 		 * the same value of bi_phys_segments
134079ef3a8aSmajianpeng 		 */
134179ef3a8aSmajianpeng 		if (bio->bi_phys_segments && old &&
134279ef3a8aSmajianpeng 		    old != start_next_window)
134379ef3a8aSmajianpeng 			/* Wait for the former r1bio(s) to complete */
134479ef3a8aSmajianpeng 			wait_event(conf->wait_barrier,
134579ef3a8aSmajianpeng 				   bio->bi_phys_segments == 1);
13466bfe0b49SDan Williams 		goto retry_write;
13476bfe0b49SDan Williams 	}
13486bfe0b49SDan Williams 
13491f68f0c4SNeilBrown 	if (max_sectors < r1_bio->sectors) {
13501f68f0c4SNeilBrown 		/* We are splitting this write into multiple parts, so
13511f68f0c4SNeilBrown 		 * we need to prepare for allocating another r1_bio.
13521f68f0c4SNeilBrown 		 */
13531f68f0c4SNeilBrown 		r1_bio->sectors = max_sectors;
13541f68f0c4SNeilBrown 		spin_lock_irq(&conf->device_lock);
13551f68f0c4SNeilBrown 		if (bio->bi_phys_segments == 0)
13561f68f0c4SNeilBrown 			bio->bi_phys_segments = 2;
13571f68f0c4SNeilBrown 		else
13581f68f0c4SNeilBrown 			bio->bi_phys_segments++;
13591f68f0c4SNeilBrown 		spin_unlock_irq(&conf->device_lock);
1360191ea9b2SNeilBrown 	}
13614f024f37SKent Overstreet 	sectors_handled = r1_bio->sector + max_sectors - bio->bi_iter.bi_sector;
13624b6d287fSNeilBrown 
13634e78064fSNeilBrown 	atomic_set(&r1_bio->remaining, 1);
13644b6d287fSNeilBrown 	atomic_set(&r1_bio->behind_remaining, 0);
1365191ea9b2SNeilBrown 
13661f68f0c4SNeilBrown 	first_clone = 1;
13671da177e4SLinus Torvalds 	for (i = 0; i < disks; i++) {
13681da177e4SLinus Torvalds 		struct bio *mbio;
13691da177e4SLinus Torvalds 		if (!r1_bio->bios[i])
13701da177e4SLinus Torvalds 			continue;
13711da177e4SLinus Torvalds 
1372a167f663SNeilBrown 		mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
13734f024f37SKent Overstreet 		bio_trim(mbio, r1_bio->sector - bio->bi_iter.bi_sector, max_sectors);
13741da177e4SLinus Torvalds 
13751f68f0c4SNeilBrown 		if (first_clone) {
13761f68f0c4SNeilBrown 			/* do behind I/O ?
13771f68f0c4SNeilBrown 			 * Not if there are too many, or cannot
13781f68f0c4SNeilBrown 			 * allocate memory, or a reader on WriteMostly
13791f68f0c4SNeilBrown 			 * is waiting for behind writes to flush */
13801f68f0c4SNeilBrown 			if (bitmap &&
13811f68f0c4SNeilBrown 			    (atomic_read(&bitmap->behind_writes)
13821f68f0c4SNeilBrown 			     < mddev->bitmap_info.max_write_behind) &&
13831f68f0c4SNeilBrown 			    !waitqueue_active(&bitmap->behind_wait))
13841f68f0c4SNeilBrown 				alloc_behind_pages(mbio, r1_bio);
13851da177e4SLinus Torvalds 
13861f68f0c4SNeilBrown 			bitmap_startwrite(bitmap, r1_bio->sector,
13871f68f0c4SNeilBrown 					  r1_bio->sectors,
13881f68f0c4SNeilBrown 					  test_bit(R1BIO_BehindIO,
13891f68f0c4SNeilBrown 						   &r1_bio->state));
13901f68f0c4SNeilBrown 			first_clone = 0;
13911f68f0c4SNeilBrown 		}
13922ca68f5eSNeilBrown 		if (r1_bio->behind_bvecs) {
13934b6d287fSNeilBrown 			struct bio_vec *bvec;
13944b6d287fSNeilBrown 			int j;
13954b6d287fSNeilBrown 
1396cb34e057SKent Overstreet 			/*
1397cb34e057SKent Overstreet 			 * We trimmed the bio, so _all is legit
13984b6d287fSNeilBrown 			 */
1399d74c6d51SKent Overstreet 			bio_for_each_segment_all(bvec, mbio, j)
14002ca68f5eSNeilBrown 				bvec->bv_page = r1_bio->behind_bvecs[j].bv_page;
14014b6d287fSNeilBrown 			if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
14024b6d287fSNeilBrown 				atomic_inc(&r1_bio->behind_remaining);
14034b6d287fSNeilBrown 		}
14044b6d287fSNeilBrown 
14051f68f0c4SNeilBrown 		r1_bio->bios[i] = mbio;
14061f68f0c4SNeilBrown 
14074f024f37SKent Overstreet 		mbio->bi_iter.bi_sector	= (r1_bio->sector +
14081f68f0c4SNeilBrown 				   conf->mirrors[i].rdev->data_offset);
14091f68f0c4SNeilBrown 		mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
14101f68f0c4SNeilBrown 		mbio->bi_end_io	= raid1_end_write_request;
1411c8dc9c65SJoe Lawrence 		mbio->bi_rw =
1412c8dc9c65SJoe Lawrence 			WRITE | do_flush_fua | do_sync | do_discard | do_same;
14131f68f0c4SNeilBrown 		mbio->bi_private = r1_bio;
14141f68f0c4SNeilBrown 
14151da177e4SLinus Torvalds 		atomic_inc(&r1_bio->remaining);
1416f54a9d0eSNeilBrown 
1417f54a9d0eSNeilBrown 		cb = blk_check_plugged(raid1_unplug, mddev, sizeof(*plug));
1418f54a9d0eSNeilBrown 		if (cb)
1419f54a9d0eSNeilBrown 			plug = container_of(cb, struct raid1_plug_cb, cb);
1420f54a9d0eSNeilBrown 		else
1421f54a9d0eSNeilBrown 			plug = NULL;
1422191ea9b2SNeilBrown 		spin_lock_irqsave(&conf->device_lock, flags);
1423f54a9d0eSNeilBrown 		if (plug) {
1424f54a9d0eSNeilBrown 			bio_list_add(&plug->pending, mbio);
1425f54a9d0eSNeilBrown 			plug->pending_cnt++;
1426f54a9d0eSNeilBrown 		} else {
14274e78064fSNeilBrown 			bio_list_add(&conf->pending_bio_list, mbio);
142834db0cd6SNeilBrown 			conf->pending_count++;
1429f54a9d0eSNeilBrown 		}
1430191ea9b2SNeilBrown 		spin_unlock_irqrestore(&conf->device_lock, flags);
1431f54a9d0eSNeilBrown 		if (!plug)
1432b357f04aSNeilBrown 			md_wakeup_thread(mddev->thread);
14334e78064fSNeilBrown 	}
1434079fa166SNeilBrown 	/* Mustn't call r1_bio_write_done before this next test,
1435079fa166SNeilBrown 	 * as it could result in the bio being freed.
1436079fa166SNeilBrown 	 */
1437aa8b57aaSKent Overstreet 	if (sectors_handled < bio_sectors(bio)) {
1438079fa166SNeilBrown 		r1_bio_write_done(r1_bio);
14391f68f0c4SNeilBrown 		/* We need another r1_bio.  It has already been counted
14401f68f0c4SNeilBrown 		 * in bio->bi_phys_segments
14411f68f0c4SNeilBrown 		 */
14421f68f0c4SNeilBrown 		r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
14431f68f0c4SNeilBrown 		r1_bio->master_bio = bio;
1444aa8b57aaSKent Overstreet 		r1_bio->sectors = bio_sectors(bio) - sectors_handled;
14451f68f0c4SNeilBrown 		r1_bio->state = 0;
14461f68f0c4SNeilBrown 		r1_bio->mddev = mddev;
14474f024f37SKent Overstreet 		r1_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
14481f68f0c4SNeilBrown 		goto retry_write;
14491f68f0c4SNeilBrown 	}
14501f68f0c4SNeilBrown 
1451079fa166SNeilBrown 	r1_bio_write_done(r1_bio);
1452079fa166SNeilBrown 
1453079fa166SNeilBrown 	/* In case raid1d snuck in to freeze_array */
1454079fa166SNeilBrown 	wake_up(&conf->wait_barrier);
14551da177e4SLinus Torvalds }
14561da177e4SLinus Torvalds 
1457fd01b88cSNeilBrown static void status(struct seq_file *seq, struct mddev *mddev)
14581da177e4SLinus Torvalds {
1459e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
14601da177e4SLinus Torvalds 	int i;
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds 	seq_printf(seq, " [%d/%d] [", conf->raid_disks,
146311ce99e6SNeilBrown 		   conf->raid_disks - mddev->degraded);
1464ddac7c7eSNeilBrown 	rcu_read_lock();
1465ddac7c7eSNeilBrown 	for (i = 0; i < conf->raid_disks; i++) {
14663cb03002SNeilBrown 		struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
14671da177e4SLinus Torvalds 		seq_printf(seq, "%s",
1468ddac7c7eSNeilBrown 			   rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1469ddac7c7eSNeilBrown 	}
1470ddac7c7eSNeilBrown 	rcu_read_unlock();
14711da177e4SLinus Torvalds 	seq_printf(seq, "]");
14721da177e4SLinus Torvalds }
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds 
1475fd01b88cSNeilBrown static void error(struct mddev *mddev, struct md_rdev *rdev)
14761da177e4SLinus Torvalds {
14771da177e4SLinus Torvalds 	char b[BDEVNAME_SIZE];
1478e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds 	/*
14811da177e4SLinus Torvalds 	 * If it is not operational, then we have already marked it as dead
14821da177e4SLinus Torvalds 	 * else if it is the last working disks, ignore the error, let the
14831da177e4SLinus Torvalds 	 * next level up know.
14841da177e4SLinus Torvalds 	 * else mark the drive as failed
14851da177e4SLinus Torvalds 	 */
1486b2d444d7SNeilBrown 	if (test_bit(In_sync, &rdev->flags)
14874044ba58SNeilBrown 	    && (conf->raid_disks - mddev->degraded) == 1) {
14881da177e4SLinus Torvalds 		/*
14891da177e4SLinus Torvalds 		 * Don't fail the drive, act as though we were just a
14904044ba58SNeilBrown 		 * normal single drive.
14914044ba58SNeilBrown 		 * However don't try a recovery from this drive as
14924044ba58SNeilBrown 		 * it is very likely to fail.
14931da177e4SLinus Torvalds 		 */
14945389042fSNeilBrown 		conf->recovery_disabled = mddev->recovery_disabled;
14951da177e4SLinus Torvalds 		return;
14964044ba58SNeilBrown 	}
1497de393cdeSNeilBrown 	set_bit(Blocked, &rdev->flags);
1498c04be0aaSNeilBrown 	if (test_and_clear_bit(In_sync, &rdev->flags)) {
1499c04be0aaSNeilBrown 		unsigned long flags;
1500c04be0aaSNeilBrown 		spin_lock_irqsave(&conf->device_lock, flags);
15011da177e4SLinus Torvalds 		mddev->degraded++;
1502dd00a99eSNeilBrown 		set_bit(Faulty, &rdev->flags);
1503c04be0aaSNeilBrown 		spin_unlock_irqrestore(&conf->device_lock, flags);
15042446dba0SNeilBrown 	} else
15052446dba0SNeilBrown 		set_bit(Faulty, &rdev->flags);
15061da177e4SLinus Torvalds 	/*
15071da177e4SLinus Torvalds 	 * if recovery is running, make sure it aborts.
15081da177e4SLinus Torvalds 	 */
1509dfc70645SNeilBrown 	set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1510850b2b42SNeilBrown 	set_bit(MD_CHANGE_DEVS, &mddev->flags);
1511067032bcSJoe Perches 	printk(KERN_ALERT
1512067032bcSJoe Perches 	       "md/raid1:%s: Disk failure on %s, disabling device.\n"
1513067032bcSJoe Perches 	       "md/raid1:%s: Operation continuing on %d devices.\n",
15149dd1e2faSNeilBrown 	       mdname(mddev), bdevname(rdev->bdev, b),
15159dd1e2faSNeilBrown 	       mdname(mddev), conf->raid_disks - mddev->degraded);
15161da177e4SLinus Torvalds }
15171da177e4SLinus Torvalds 
1518e8096360SNeilBrown static void print_conf(struct r1conf *conf)
15191da177e4SLinus Torvalds {
15201da177e4SLinus Torvalds 	int i;
15211da177e4SLinus Torvalds 
15229dd1e2faSNeilBrown 	printk(KERN_DEBUG "RAID1 conf printout:\n");
15231da177e4SLinus Torvalds 	if (!conf) {
15249dd1e2faSNeilBrown 		printk(KERN_DEBUG "(!conf)\n");
15251da177e4SLinus Torvalds 		return;
15261da177e4SLinus Torvalds 	}
15279dd1e2faSNeilBrown 	printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
15281da177e4SLinus Torvalds 		conf->raid_disks);
15291da177e4SLinus Torvalds 
1530ddac7c7eSNeilBrown 	rcu_read_lock();
15311da177e4SLinus Torvalds 	for (i = 0; i < conf->raid_disks; i++) {
15321da177e4SLinus Torvalds 		char b[BDEVNAME_SIZE];
15333cb03002SNeilBrown 		struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1534ddac7c7eSNeilBrown 		if (rdev)
15359dd1e2faSNeilBrown 			printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
1536ddac7c7eSNeilBrown 			       i, !test_bit(In_sync, &rdev->flags),
1537ddac7c7eSNeilBrown 			       !test_bit(Faulty, &rdev->flags),
1538ddac7c7eSNeilBrown 			       bdevname(rdev->bdev,b));
15391da177e4SLinus Torvalds 	}
1540ddac7c7eSNeilBrown 	rcu_read_unlock();
15411da177e4SLinus Torvalds }
15421da177e4SLinus Torvalds 
1543e8096360SNeilBrown static void close_sync(struct r1conf *conf)
15441da177e4SLinus Torvalds {
154579ef3a8aSmajianpeng 	wait_barrier(conf, NULL);
154679ef3a8aSmajianpeng 	allow_barrier(conf, 0, 0);
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 	mempool_destroy(conf->r1buf_pool);
15491da177e4SLinus Torvalds 	conf->r1buf_pool = NULL;
155079ef3a8aSmajianpeng 
155179ef3a8aSmajianpeng 	conf->next_resync = 0;
155279ef3a8aSmajianpeng 	conf->start_next_window = MaxSector;
15531da177e4SLinus Torvalds }
15541da177e4SLinus Torvalds 
1555fd01b88cSNeilBrown static int raid1_spare_active(struct mddev *mddev)
15561da177e4SLinus Torvalds {
15571da177e4SLinus Torvalds 	int i;
1558e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
15596b965620SNeilBrown 	int count = 0;
15606b965620SNeilBrown 	unsigned long flags;
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds 	/*
15631da177e4SLinus Torvalds 	 * Find all failed disks within the RAID1 configuration
1564ddac7c7eSNeilBrown 	 * and mark them readable.
1565ddac7c7eSNeilBrown 	 * Called under mddev lock, so rcu protection not needed.
15661da177e4SLinus Torvalds 	 */
15671da177e4SLinus Torvalds 	for (i = 0; i < conf->raid_disks; i++) {
15683cb03002SNeilBrown 		struct md_rdev *rdev = conf->mirrors[i].rdev;
15698c7a2c2bSNeilBrown 		struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev;
15708c7a2c2bSNeilBrown 		if (repl
15718c7a2c2bSNeilBrown 		    && repl->recovery_offset == MaxSector
15728c7a2c2bSNeilBrown 		    && !test_bit(Faulty, &repl->flags)
15738c7a2c2bSNeilBrown 		    && !test_and_set_bit(In_sync, &repl->flags)) {
15748c7a2c2bSNeilBrown 			/* replacement has just become active */
15758c7a2c2bSNeilBrown 			if (!rdev ||
15768c7a2c2bSNeilBrown 			    !test_and_clear_bit(In_sync, &rdev->flags))
15778c7a2c2bSNeilBrown 				count++;
15788c7a2c2bSNeilBrown 			if (rdev) {
15798c7a2c2bSNeilBrown 				/* Replaced device not technically
15808c7a2c2bSNeilBrown 				 * faulty, but we need to be sure
15818c7a2c2bSNeilBrown 				 * it gets removed and never re-added
15828c7a2c2bSNeilBrown 				 */
15838c7a2c2bSNeilBrown 				set_bit(Faulty, &rdev->flags);
15848c7a2c2bSNeilBrown 				sysfs_notify_dirent_safe(
15858c7a2c2bSNeilBrown 					rdev->sysfs_state);
15868c7a2c2bSNeilBrown 			}
15878c7a2c2bSNeilBrown 		}
1588ddac7c7eSNeilBrown 		if (rdev
158961e4947cSLukasz Dorau 		    && rdev->recovery_offset == MaxSector
1590ddac7c7eSNeilBrown 		    && !test_bit(Faulty, &rdev->flags)
1591c04be0aaSNeilBrown 		    && !test_and_set_bit(In_sync, &rdev->flags)) {
15926b965620SNeilBrown 			count++;
1593654e8b5aSJonathan Brassow 			sysfs_notify_dirent_safe(rdev->sysfs_state);
15941da177e4SLinus Torvalds 		}
15951da177e4SLinus Torvalds 	}
15966b965620SNeilBrown 	spin_lock_irqsave(&conf->device_lock, flags);
15976b965620SNeilBrown 	mddev->degraded -= count;
15986b965620SNeilBrown 	spin_unlock_irqrestore(&conf->device_lock, flags);
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 	print_conf(conf);
16016b965620SNeilBrown 	return count;
16021da177e4SLinus Torvalds }
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 
1605fd01b88cSNeilBrown static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev)
16061da177e4SLinus Torvalds {
1607e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
1608199050eaSNeil Brown 	int err = -EEXIST;
160941158c7eSNeilBrown 	int mirror = 0;
16100eaf822cSJonathan Brassow 	struct raid1_info *p;
16116c2fce2eSNeil Brown 	int first = 0;
161230194636SNeilBrown 	int last = conf->raid_disks - 1;
16136b740b8dSNeilBrown 	struct request_queue *q = bdev_get_queue(rdev->bdev);
16141da177e4SLinus Torvalds 
16155389042fSNeilBrown 	if (mddev->recovery_disabled == conf->recovery_disabled)
16165389042fSNeilBrown 		return -EBUSY;
16175389042fSNeilBrown 
16186c2fce2eSNeil Brown 	if (rdev->raid_disk >= 0)
16196c2fce2eSNeil Brown 		first = last = rdev->raid_disk;
16206c2fce2eSNeil Brown 
16216b740b8dSNeilBrown 	if (q->merge_bvec_fn) {
16226b740b8dSNeilBrown 		set_bit(Unmerged, &rdev->flags);
16236b740b8dSNeilBrown 		mddev->merge_check_needed = 1;
16246b740b8dSNeilBrown 	}
16256b740b8dSNeilBrown 
16267ef449d1SNeilBrown 	for (mirror = first; mirror <= last; mirror++) {
16277ef449d1SNeilBrown 		p = conf->mirrors+mirror;
16287ef449d1SNeilBrown 		if (!p->rdev) {
16291da177e4SLinus Torvalds 
16309092c02dSJonathan Brassow 			if (mddev->gendisk)
16318f6c2e4bSMartin K. Petersen 				disk_stack_limits(mddev->gendisk, rdev->bdev,
16328f6c2e4bSMartin K. Petersen 						  rdev->data_offset << 9);
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds 			p->head_position = 0;
16351da177e4SLinus Torvalds 			rdev->raid_disk = mirror;
1636199050eaSNeil Brown 			err = 0;
16376aea114aSNeilBrown 			/* As all devices are equivalent, we don't need a full recovery
16386aea114aSNeilBrown 			 * if this was recently any drive of the array
16396aea114aSNeilBrown 			 */
16406aea114aSNeilBrown 			if (rdev->saved_raid_disk < 0)
164141158c7eSNeilBrown 				conf->fullsync = 1;
1642d6065f7bSSuzanne Wood 			rcu_assign_pointer(p->rdev, rdev);
16431da177e4SLinus Torvalds 			break;
16441da177e4SLinus Torvalds 		}
16457ef449d1SNeilBrown 		if (test_bit(WantReplacement, &p->rdev->flags) &&
16467ef449d1SNeilBrown 		    p[conf->raid_disks].rdev == NULL) {
16477ef449d1SNeilBrown 			/* Add this device as a replacement */
16487ef449d1SNeilBrown 			clear_bit(In_sync, &rdev->flags);
16497ef449d1SNeilBrown 			set_bit(Replacement, &rdev->flags);
16507ef449d1SNeilBrown 			rdev->raid_disk = mirror;
16517ef449d1SNeilBrown 			err = 0;
16527ef449d1SNeilBrown 			conf->fullsync = 1;
16537ef449d1SNeilBrown 			rcu_assign_pointer(p[conf->raid_disks].rdev, rdev);
16547ef449d1SNeilBrown 			break;
16557ef449d1SNeilBrown 		}
16567ef449d1SNeilBrown 	}
16576b740b8dSNeilBrown 	if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
16586b740b8dSNeilBrown 		/* Some requests might not have seen this new
16596b740b8dSNeilBrown 		 * merge_bvec_fn.  We must wait for them to complete
16606b740b8dSNeilBrown 		 * before merging the device fully.
16616b740b8dSNeilBrown 		 * First we make sure any code which has tested
16626b740b8dSNeilBrown 		 * our function has submitted the request, then
16636b740b8dSNeilBrown 		 * we wait for all outstanding requests to complete.
16646b740b8dSNeilBrown 		 */
16656b740b8dSNeilBrown 		synchronize_sched();
1666e2d59925SNeilBrown 		freeze_array(conf, 0);
1667e2d59925SNeilBrown 		unfreeze_array(conf);
16686b740b8dSNeilBrown 		clear_bit(Unmerged, &rdev->flags);
16696b740b8dSNeilBrown 	}
1670ac5e7113SAndre Noll 	md_integrity_add_rdev(rdev, mddev);
16719092c02dSJonathan Brassow 	if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
16722ff8cc2cSShaohua Li 		queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
16731da177e4SLinus Torvalds 	print_conf(conf);
1674199050eaSNeil Brown 	return err;
16751da177e4SLinus Torvalds }
16761da177e4SLinus Torvalds 
1677b8321b68SNeilBrown static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
16781da177e4SLinus Torvalds {
1679e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
16801da177e4SLinus Torvalds 	int err = 0;
1681b8321b68SNeilBrown 	int number = rdev->raid_disk;
16820eaf822cSJonathan Brassow 	struct raid1_info *p = conf->mirrors + number;
16831da177e4SLinus Torvalds 
1684b014f14cSNeilBrown 	if (rdev != p->rdev)
1685b014f14cSNeilBrown 		p = conf->mirrors + conf->raid_disks + number;
1686b014f14cSNeilBrown 
16871da177e4SLinus Torvalds 	print_conf(conf);
1688b8321b68SNeilBrown 	if (rdev == p->rdev) {
1689b2d444d7SNeilBrown 		if (test_bit(In_sync, &rdev->flags) ||
16901da177e4SLinus Torvalds 		    atomic_read(&rdev->nr_pending)) {
16911da177e4SLinus Torvalds 			err = -EBUSY;
16921da177e4SLinus Torvalds 			goto abort;
16931da177e4SLinus Torvalds 		}
1694046abeedSNeilBrown 		/* Only remove non-faulty devices if recovery
1695dfc70645SNeilBrown 		 * is not possible.
1696dfc70645SNeilBrown 		 */
1697dfc70645SNeilBrown 		if (!test_bit(Faulty, &rdev->flags) &&
16985389042fSNeilBrown 		    mddev->recovery_disabled != conf->recovery_disabled &&
1699dfc70645SNeilBrown 		    mddev->degraded < conf->raid_disks) {
1700dfc70645SNeilBrown 			err = -EBUSY;
1701dfc70645SNeilBrown 			goto abort;
1702dfc70645SNeilBrown 		}
17031da177e4SLinus Torvalds 		p->rdev = NULL;
1704fbd568a3SPaul E. McKenney 		synchronize_rcu();
17051da177e4SLinus Torvalds 		if (atomic_read(&rdev->nr_pending)) {
17061da177e4SLinus Torvalds 			/* lost the race, try later */
17071da177e4SLinus Torvalds 			err = -EBUSY;
17081da177e4SLinus Torvalds 			p->rdev = rdev;
1709ac5e7113SAndre Noll 			goto abort;
17108c7a2c2bSNeilBrown 		} else if (conf->mirrors[conf->raid_disks + number].rdev) {
17118c7a2c2bSNeilBrown 			/* We just removed a device that is being replaced.
17128c7a2c2bSNeilBrown 			 * Move down the replacement.  We drain all IO before
17138c7a2c2bSNeilBrown 			 * doing this to avoid confusion.
17148c7a2c2bSNeilBrown 			 */
17158c7a2c2bSNeilBrown 			struct md_rdev *repl =
17168c7a2c2bSNeilBrown 				conf->mirrors[conf->raid_disks + number].rdev;
1717e2d59925SNeilBrown 			freeze_array(conf, 0);
17188c7a2c2bSNeilBrown 			clear_bit(Replacement, &repl->flags);
17198c7a2c2bSNeilBrown 			p->rdev = repl;
17208c7a2c2bSNeilBrown 			conf->mirrors[conf->raid_disks + number].rdev = NULL;
1721e2d59925SNeilBrown 			unfreeze_array(conf);
1722b014f14cSNeilBrown 			clear_bit(WantReplacement, &rdev->flags);
17238c7a2c2bSNeilBrown 		} else
17248c7a2c2bSNeilBrown 			clear_bit(WantReplacement, &rdev->flags);
1725a91a2785SMartin K. Petersen 		err = md_integrity_register(mddev);
17261da177e4SLinus Torvalds 	}
17271da177e4SLinus Torvalds abort:
17281da177e4SLinus Torvalds 
17291da177e4SLinus Torvalds 	print_conf(conf);
17301da177e4SLinus Torvalds 	return err;
17311da177e4SLinus Torvalds }
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 
17346712ecf8SNeilBrown static void end_sync_read(struct bio *bio, int error)
17351da177e4SLinus Torvalds {
17369f2c9d12SNeilBrown 	struct r1bio *r1_bio = bio->bi_private;
17371da177e4SLinus Torvalds 
17380fc280f6SNeilBrown 	update_head_pos(r1_bio->read_disk, r1_bio);
1739ba3ae3beSNamhyung Kim 
17401da177e4SLinus Torvalds 	/*
17411da177e4SLinus Torvalds 	 * we have read a block, now it needs to be re-written,
17421da177e4SLinus Torvalds 	 * or re-read if the read failed.
17431da177e4SLinus Torvalds 	 * We don't do much here, just schedule handling by raid1d
17441da177e4SLinus Torvalds 	 */
174569382e85SNeilBrown 	if (test_bit(BIO_UPTODATE, &bio->bi_flags))
17461da177e4SLinus Torvalds 		set_bit(R1BIO_Uptodate, &r1_bio->state);
1747d11c171eSNeilBrown 
1748d11c171eSNeilBrown 	if (atomic_dec_and_test(&r1_bio->remaining))
17491da177e4SLinus Torvalds 		reschedule_retry(r1_bio);
17501da177e4SLinus Torvalds }
17511da177e4SLinus Torvalds 
17526712ecf8SNeilBrown static void end_sync_write(struct bio *bio, int error)
17531da177e4SLinus Torvalds {
17541da177e4SLinus Torvalds 	int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
17559f2c9d12SNeilBrown 	struct r1bio *r1_bio = bio->bi_private;
1756fd01b88cSNeilBrown 	struct mddev *mddev = r1_bio->mddev;
1757e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
17581da177e4SLinus Torvalds 	int mirror=0;
17594367af55SNeilBrown 	sector_t first_bad;
17604367af55SNeilBrown 	int bad_sectors;
17611da177e4SLinus Torvalds 
1762ba3ae3beSNamhyung Kim 	mirror = find_bio_disk(r1_bio, bio);
1763ba3ae3beSNamhyung Kim 
17646b1117d5SNeilBrown 	if (!uptodate) {
176557dab0bdSNeilBrown 		sector_t sync_blocks = 0;
17666b1117d5SNeilBrown 		sector_t s = r1_bio->sector;
17676b1117d5SNeilBrown 		long sectors_to_go = r1_bio->sectors;
17686b1117d5SNeilBrown 		/* make sure these bits doesn't get cleared. */
17696b1117d5SNeilBrown 		do {
17705e3db645SNeilBrown 			bitmap_end_sync(mddev->bitmap, s,
17716b1117d5SNeilBrown 					&sync_blocks, 1);
17726b1117d5SNeilBrown 			s += sync_blocks;
17736b1117d5SNeilBrown 			sectors_to_go -= sync_blocks;
17746b1117d5SNeilBrown 		} while (sectors_to_go > 0);
1775d8f05d29SNeilBrown 		set_bit(WriteErrorSeen,
1776d8f05d29SNeilBrown 			&conf->mirrors[mirror].rdev->flags);
177719d67169SNeilBrown 		if (!test_and_set_bit(WantReplacement,
177819d67169SNeilBrown 				      &conf->mirrors[mirror].rdev->flags))
177919d67169SNeilBrown 			set_bit(MD_RECOVERY_NEEDED, &
178019d67169SNeilBrown 				mddev->recovery);
1781d8f05d29SNeilBrown 		set_bit(R1BIO_WriteError, &r1_bio->state);
17824367af55SNeilBrown 	} else if (is_badblock(conf->mirrors[mirror].rdev,
17834367af55SNeilBrown 			       r1_bio->sector,
17844367af55SNeilBrown 			       r1_bio->sectors,
17853a9f28a5SNeilBrown 			       &first_bad, &bad_sectors) &&
17863a9f28a5SNeilBrown 		   !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
17873a9f28a5SNeilBrown 				r1_bio->sector,
17883a9f28a5SNeilBrown 				r1_bio->sectors,
17893a9f28a5SNeilBrown 				&first_bad, &bad_sectors)
17903a9f28a5SNeilBrown 		)
17914367af55SNeilBrown 		set_bit(R1BIO_MadeGood, &r1_bio->state);
1792e3b9703eSNeilBrown 
17931da177e4SLinus Torvalds 	if (atomic_dec_and_test(&r1_bio->remaining)) {
17944367af55SNeilBrown 		int s = r1_bio->sectors;
1795d8f05d29SNeilBrown 		if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
1796d8f05d29SNeilBrown 		    test_bit(R1BIO_WriteError, &r1_bio->state))
17974367af55SNeilBrown 			reschedule_retry(r1_bio);
17984367af55SNeilBrown 		else {
17991da177e4SLinus Torvalds 			put_buf(r1_bio);
180073d5c38aSNeilBrown 			md_done_sync(mddev, s, uptodate);
18011da177e4SLinus Torvalds 		}
18021da177e4SLinus Torvalds 	}
18034367af55SNeilBrown }
18041da177e4SLinus Torvalds 
18053cb03002SNeilBrown static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector,
1806d8f05d29SNeilBrown 			    int sectors, struct page *page, int rw)
1807d8f05d29SNeilBrown {
1808d8f05d29SNeilBrown 	if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
1809d8f05d29SNeilBrown 		/* success */
1810d8f05d29SNeilBrown 		return 1;
181119d67169SNeilBrown 	if (rw == WRITE) {
1812d8f05d29SNeilBrown 		set_bit(WriteErrorSeen, &rdev->flags);
181319d67169SNeilBrown 		if (!test_and_set_bit(WantReplacement,
181419d67169SNeilBrown 				      &rdev->flags))
181519d67169SNeilBrown 			set_bit(MD_RECOVERY_NEEDED, &
181619d67169SNeilBrown 				rdev->mddev->recovery);
181719d67169SNeilBrown 	}
1818d8f05d29SNeilBrown 	/* need to record an error - either for the block or the device */
1819d8f05d29SNeilBrown 	if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1820d8f05d29SNeilBrown 		md_error(rdev->mddev, rdev);
1821d8f05d29SNeilBrown 	return 0;
1822d8f05d29SNeilBrown }
1823d8f05d29SNeilBrown 
18249f2c9d12SNeilBrown static int fix_sync_read_error(struct r1bio *r1_bio)
18251da177e4SLinus Torvalds {
1826a68e5870SNeilBrown 	/* Try some synchronous reads of other devices to get
182769382e85SNeilBrown 	 * good data, much like with normal read errors.  Only
1828ddac7c7eSNeilBrown 	 * read into the pages we already have so we don't
182969382e85SNeilBrown 	 * need to re-issue the read request.
183069382e85SNeilBrown 	 * We don't need to freeze the array, because being in an
183169382e85SNeilBrown 	 * active sync request, there is no normal IO, and
183269382e85SNeilBrown 	 * no overlapping syncs.
183306f60385SNeilBrown 	 * We don't need to check is_badblock() again as we
183406f60385SNeilBrown 	 * made sure that anything with a bad block in range
183506f60385SNeilBrown 	 * will have bi_end_io clear.
18361da177e4SLinus Torvalds 	 */
1837fd01b88cSNeilBrown 	struct mddev *mddev = r1_bio->mddev;
1838e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
1839a68e5870SNeilBrown 	struct bio *bio = r1_bio->bios[r1_bio->read_disk];
184069382e85SNeilBrown 	sector_t sect = r1_bio->sector;
184169382e85SNeilBrown 	int sectors = r1_bio->sectors;
184269382e85SNeilBrown 	int idx = 0;
184369382e85SNeilBrown 
184469382e85SNeilBrown 	while(sectors) {
184569382e85SNeilBrown 		int s = sectors;
184669382e85SNeilBrown 		int d = r1_bio->read_disk;
184769382e85SNeilBrown 		int success = 0;
18483cb03002SNeilBrown 		struct md_rdev *rdev;
184978d7f5f7SNeilBrown 		int start;
185069382e85SNeilBrown 
185169382e85SNeilBrown 		if (s > (PAGE_SIZE>>9))
185269382e85SNeilBrown 			s = PAGE_SIZE >> 9;
185369382e85SNeilBrown 		do {
185469382e85SNeilBrown 			if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1855ddac7c7eSNeilBrown 				/* No rcu protection needed here devices
1856ddac7c7eSNeilBrown 				 * can only be removed when no resync is
1857ddac7c7eSNeilBrown 				 * active, and resync is currently active
1858ddac7c7eSNeilBrown 				 */
185969382e85SNeilBrown 				rdev = conf->mirrors[d].rdev;
18609d3d8011SNamhyung Kim 				if (sync_page_io(rdev, sect, s<<9,
186169382e85SNeilBrown 						 bio->bi_io_vec[idx].bv_page,
1862ccebd4c4SJonathan Brassow 						 READ, false)) {
186369382e85SNeilBrown 					success = 1;
186469382e85SNeilBrown 					break;
186569382e85SNeilBrown 				}
186669382e85SNeilBrown 			}
186769382e85SNeilBrown 			d++;
18688f19ccb2SNeilBrown 			if (d == conf->raid_disks * 2)
186969382e85SNeilBrown 				d = 0;
187069382e85SNeilBrown 		} while (!success && d != r1_bio->read_disk);
187169382e85SNeilBrown 
187278d7f5f7SNeilBrown 		if (!success) {
187378d7f5f7SNeilBrown 			char b[BDEVNAME_SIZE];
18743a9f28a5SNeilBrown 			int abort = 0;
18753a9f28a5SNeilBrown 			/* Cannot read from anywhere, this block is lost.
18763a9f28a5SNeilBrown 			 * Record a bad block on each device.  If that doesn't
18773a9f28a5SNeilBrown 			 * work just disable and interrupt the recovery.
18783a9f28a5SNeilBrown 			 * Don't fail devices as that won't really help.
18793a9f28a5SNeilBrown 			 */
188078d7f5f7SNeilBrown 			printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O read error"
188178d7f5f7SNeilBrown 			       " for block %llu\n",
188278d7f5f7SNeilBrown 			       mdname(mddev),
188378d7f5f7SNeilBrown 			       bdevname(bio->bi_bdev, b),
188478d7f5f7SNeilBrown 			       (unsigned long long)r1_bio->sector);
18858f19ccb2SNeilBrown 			for (d = 0; d < conf->raid_disks * 2; d++) {
18863a9f28a5SNeilBrown 				rdev = conf->mirrors[d].rdev;
18873a9f28a5SNeilBrown 				if (!rdev || test_bit(Faulty, &rdev->flags))
18883a9f28a5SNeilBrown 					continue;
18893a9f28a5SNeilBrown 				if (!rdev_set_badblocks(rdev, sect, s, 0))
18903a9f28a5SNeilBrown 					abort = 1;
18913a9f28a5SNeilBrown 			}
18923a9f28a5SNeilBrown 			if (abort) {
1893d890fa2bSNeilBrown 				conf->recovery_disabled =
1894d890fa2bSNeilBrown 					mddev->recovery_disabled;
18953a9f28a5SNeilBrown 				set_bit(MD_RECOVERY_INTR, &mddev->recovery);
189678d7f5f7SNeilBrown 				md_done_sync(mddev, r1_bio->sectors, 0);
189778d7f5f7SNeilBrown 				put_buf(r1_bio);
189878d7f5f7SNeilBrown 				return 0;
189978d7f5f7SNeilBrown 			}
19003a9f28a5SNeilBrown 			/* Try next page */
19013a9f28a5SNeilBrown 			sectors -= s;
19023a9f28a5SNeilBrown 			sect += s;
19033a9f28a5SNeilBrown 			idx++;
19043a9f28a5SNeilBrown 			continue;
19053a9f28a5SNeilBrown 		}
190678d7f5f7SNeilBrown 
190778d7f5f7SNeilBrown 		start = d;
190869382e85SNeilBrown 		/* write it back and re-read */
190969382e85SNeilBrown 		while (d != r1_bio->read_disk) {
191069382e85SNeilBrown 			if (d == 0)
19118f19ccb2SNeilBrown 				d = conf->raid_disks * 2;
191269382e85SNeilBrown 			d--;
191369382e85SNeilBrown 			if (r1_bio->bios[d]->bi_end_io != end_sync_read)
191469382e85SNeilBrown 				continue;
191569382e85SNeilBrown 			rdev = conf->mirrors[d].rdev;
1916d8f05d29SNeilBrown 			if (r1_sync_page_io(rdev, sect, s,
191769382e85SNeilBrown 					    bio->bi_io_vec[idx].bv_page,
1918d8f05d29SNeilBrown 					    WRITE) == 0) {
191978d7f5f7SNeilBrown 				r1_bio->bios[d]->bi_end_io = NULL;
192078d7f5f7SNeilBrown 				rdev_dec_pending(rdev, mddev);
19219d3d8011SNamhyung Kim 			}
1922097426f6SNeilBrown 		}
1923097426f6SNeilBrown 		d = start;
1924097426f6SNeilBrown 		while (d != r1_bio->read_disk) {
1925097426f6SNeilBrown 			if (d == 0)
19268f19ccb2SNeilBrown 				d = conf->raid_disks * 2;
1927097426f6SNeilBrown 			d--;
1928097426f6SNeilBrown 			if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1929097426f6SNeilBrown 				continue;
1930097426f6SNeilBrown 			rdev = conf->mirrors[d].rdev;
1931d8f05d29SNeilBrown 			if (r1_sync_page_io(rdev, sect, s,
193269382e85SNeilBrown 					    bio->bi_io_vec[idx].bv_page,
1933d8f05d29SNeilBrown 					    READ) != 0)
19349d3d8011SNamhyung Kim 				atomic_add(s, &rdev->corrected_errors);
193569382e85SNeilBrown 		}
193669382e85SNeilBrown 		sectors -= s;
193769382e85SNeilBrown 		sect += s;
193869382e85SNeilBrown 		idx ++;
193969382e85SNeilBrown 	}
194078d7f5f7SNeilBrown 	set_bit(R1BIO_Uptodate, &r1_bio->state);
19417ca78d57SNeilBrown 	set_bit(BIO_UPTODATE, &bio->bi_flags);
1942a68e5870SNeilBrown 	return 1;
194369382e85SNeilBrown }
1944d11c171eSNeilBrown 
19459f2c9d12SNeilBrown static int process_checks(struct r1bio *r1_bio)
1946a68e5870SNeilBrown {
1947a68e5870SNeilBrown 	/* We have read all readable devices.  If we haven't
1948a68e5870SNeilBrown 	 * got the block, then there is no hope left.
1949a68e5870SNeilBrown 	 * If we have, then we want to do a comparison
1950a68e5870SNeilBrown 	 * and skip the write if everything is the same.
1951a68e5870SNeilBrown 	 * If any blocks failed to read, then we need to
1952a68e5870SNeilBrown 	 * attempt an over-write
1953a68e5870SNeilBrown 	 */
1954fd01b88cSNeilBrown 	struct mddev *mddev = r1_bio->mddev;
1955e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
1956a68e5870SNeilBrown 	int primary;
1957a68e5870SNeilBrown 	int i;
1958f4380a91Smajianpeng 	int vcnt;
1959a68e5870SNeilBrown 
196030bc9b53SNeilBrown 	/* Fix variable parts of all bios */
196130bc9b53SNeilBrown 	vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9);
196230bc9b53SNeilBrown 	for (i = 0; i < conf->raid_disks * 2; i++) {
196330bc9b53SNeilBrown 		int j;
196430bc9b53SNeilBrown 		int size;
19651877db75SNeilBrown 		int uptodate;
196630bc9b53SNeilBrown 		struct bio *b = r1_bio->bios[i];
196730bc9b53SNeilBrown 		if (b->bi_end_io != end_sync_read)
196830bc9b53SNeilBrown 			continue;
19691877db75SNeilBrown 		/* fixup the bio for reuse, but preserve BIO_UPTODATE */
19701877db75SNeilBrown 		uptodate = test_bit(BIO_UPTODATE, &b->bi_flags);
197130bc9b53SNeilBrown 		bio_reset(b);
19721877db75SNeilBrown 		if (!uptodate)
19731877db75SNeilBrown 			clear_bit(BIO_UPTODATE, &b->bi_flags);
197430bc9b53SNeilBrown 		b->bi_vcnt = vcnt;
19754f024f37SKent Overstreet 		b->bi_iter.bi_size = r1_bio->sectors << 9;
19764f024f37SKent Overstreet 		b->bi_iter.bi_sector = r1_bio->sector +
197730bc9b53SNeilBrown 			conf->mirrors[i].rdev->data_offset;
197830bc9b53SNeilBrown 		b->bi_bdev = conf->mirrors[i].rdev->bdev;
197930bc9b53SNeilBrown 		b->bi_end_io = end_sync_read;
198030bc9b53SNeilBrown 		b->bi_private = r1_bio;
198130bc9b53SNeilBrown 
19824f024f37SKent Overstreet 		size = b->bi_iter.bi_size;
198330bc9b53SNeilBrown 		for (j = 0; j < vcnt ; j++) {
198430bc9b53SNeilBrown 			struct bio_vec *bi;
198530bc9b53SNeilBrown 			bi = &b->bi_io_vec[j];
198630bc9b53SNeilBrown 			bi->bv_offset = 0;
198730bc9b53SNeilBrown 			if (size > PAGE_SIZE)
198830bc9b53SNeilBrown 				bi->bv_len = PAGE_SIZE;
198930bc9b53SNeilBrown 			else
199030bc9b53SNeilBrown 				bi->bv_len = size;
199130bc9b53SNeilBrown 			size -= PAGE_SIZE;
199230bc9b53SNeilBrown 		}
199330bc9b53SNeilBrown 	}
19948f19ccb2SNeilBrown 	for (primary = 0; primary < conf->raid_disks * 2; primary++)
1995a68e5870SNeilBrown 		if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
1996a68e5870SNeilBrown 		    test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) {
1997a68e5870SNeilBrown 			r1_bio->bios[primary]->bi_end_io = NULL;
1998a68e5870SNeilBrown 			rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
1999a68e5870SNeilBrown 			break;
2000a68e5870SNeilBrown 		}
2001a68e5870SNeilBrown 	r1_bio->read_disk = primary;
20028f19ccb2SNeilBrown 	for (i = 0; i < conf->raid_disks * 2; i++) {
2003a68e5870SNeilBrown 		int j;
2004a68e5870SNeilBrown 		struct bio *pbio = r1_bio->bios[primary];
2005a68e5870SNeilBrown 		struct bio *sbio = r1_bio->bios[i];
20061877db75SNeilBrown 		int uptodate = test_bit(BIO_UPTODATE, &sbio->bi_flags);
200778d7f5f7SNeilBrown 
20082aabaa65SKent Overstreet 		if (sbio->bi_end_io != end_sync_read)
200978d7f5f7SNeilBrown 			continue;
20101877db75SNeilBrown 		/* Now we can 'fixup' the BIO_UPTODATE flag */
20111877db75SNeilBrown 		set_bit(BIO_UPTODATE, &sbio->bi_flags);
2012a68e5870SNeilBrown 
20131877db75SNeilBrown 		if (uptodate) {
2014a68e5870SNeilBrown 			for (j = vcnt; j-- ; ) {
2015a68e5870SNeilBrown 				struct page *p, *s;
2016a68e5870SNeilBrown 				p = pbio->bi_io_vec[j].bv_page;
2017a68e5870SNeilBrown 				s = sbio->bi_io_vec[j].bv_page;
2018a68e5870SNeilBrown 				if (memcmp(page_address(p),
2019a68e5870SNeilBrown 					   page_address(s),
20205020ad7dSNeilBrown 					   sbio->bi_io_vec[j].bv_len))
2021a68e5870SNeilBrown 					break;
2022a68e5870SNeilBrown 			}
2023a68e5870SNeilBrown 		} else
2024a68e5870SNeilBrown 			j = 0;
2025a68e5870SNeilBrown 		if (j >= 0)
20267f7583d4SJianpeng Ma 			atomic64_add(r1_bio->sectors, &mddev->resync_mismatches);
2027a68e5870SNeilBrown 		if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
20281877db75SNeilBrown 			      && uptodate)) {
202978d7f5f7SNeilBrown 			/* No need to write to this device. */
2030a68e5870SNeilBrown 			sbio->bi_end_io = NULL;
2031a68e5870SNeilBrown 			rdev_dec_pending(conf->mirrors[i].rdev, mddev);
203278d7f5f7SNeilBrown 			continue;
203378d7f5f7SNeilBrown 		}
2034d3b45c2aSKent Overstreet 
2035d3b45c2aSKent Overstreet 		bio_copy_data(sbio, pbio);
2036a68e5870SNeilBrown 	}
2037a68e5870SNeilBrown 	return 0;
2038a68e5870SNeilBrown }
2039a68e5870SNeilBrown 
20409f2c9d12SNeilBrown static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio)
2041a68e5870SNeilBrown {
2042e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
2043a68e5870SNeilBrown 	int i;
20448f19ccb2SNeilBrown 	int disks = conf->raid_disks * 2;
2045a68e5870SNeilBrown 	struct bio *bio, *wbio;
2046a68e5870SNeilBrown 
2047a68e5870SNeilBrown 	bio = r1_bio->bios[r1_bio->read_disk];
2048a68e5870SNeilBrown 
2049a68e5870SNeilBrown 	if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
2050a68e5870SNeilBrown 		/* ouch - failed to read all of that. */
2051a68e5870SNeilBrown 		if (!fix_sync_read_error(r1_bio))
2052a68e5870SNeilBrown 			return;
20537ca78d57SNeilBrown 
20547ca78d57SNeilBrown 	if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
20557ca78d57SNeilBrown 		if (process_checks(r1_bio) < 0)
20567ca78d57SNeilBrown 			return;
2057d11c171eSNeilBrown 	/*
2058d11c171eSNeilBrown 	 * schedule writes
2059d11c171eSNeilBrown 	 */
20601da177e4SLinus Torvalds 	atomic_set(&r1_bio->remaining, 1);
20611da177e4SLinus Torvalds 	for (i = 0; i < disks ; i++) {
20621da177e4SLinus Torvalds 		wbio = r1_bio->bios[i];
20633e198f78SNeilBrown 		if (wbio->bi_end_io == NULL ||
20643e198f78SNeilBrown 		    (wbio->bi_end_io == end_sync_read &&
20653e198f78SNeilBrown 		     (i == r1_bio->read_disk ||
20663e198f78SNeilBrown 		      !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
20671da177e4SLinus Torvalds 			continue;
20681da177e4SLinus Torvalds 
20693e198f78SNeilBrown 		wbio->bi_rw = WRITE;
20703e198f78SNeilBrown 		wbio->bi_end_io = end_sync_write;
20711da177e4SLinus Torvalds 		atomic_inc(&r1_bio->remaining);
2072aa8b57aaSKent Overstreet 		md_sync_acct(conf->mirrors[i].rdev->bdev, bio_sectors(wbio));
2073191ea9b2SNeilBrown 
20741da177e4SLinus Torvalds 		generic_make_request(wbio);
20751da177e4SLinus Torvalds 	}
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds 	if (atomic_dec_and_test(&r1_bio->remaining)) {
2078191ea9b2SNeilBrown 		/* if we're here, all write(s) have completed, so clean up */
207958e94ae1SNeilBrown 		int s = r1_bio->sectors;
208058e94ae1SNeilBrown 		if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
208158e94ae1SNeilBrown 		    test_bit(R1BIO_WriteError, &r1_bio->state))
208258e94ae1SNeilBrown 			reschedule_retry(r1_bio);
208358e94ae1SNeilBrown 		else {
20841da177e4SLinus Torvalds 			put_buf(r1_bio);
208558e94ae1SNeilBrown 			md_done_sync(mddev, s, 1);
208658e94ae1SNeilBrown 		}
20871da177e4SLinus Torvalds 	}
20881da177e4SLinus Torvalds }
20891da177e4SLinus Torvalds 
20901da177e4SLinus Torvalds /*
20911da177e4SLinus Torvalds  * This is a kernel thread which:
20921da177e4SLinus Torvalds  *
20931da177e4SLinus Torvalds  *	1.	Retries failed read operations on working mirrors.
20941da177e4SLinus Torvalds  *	2.	Updates the raid superblock when problems encounter.
2095d2eb35acSNeilBrown  *	3.	Performs writes following reads for array synchronising.
20961da177e4SLinus Torvalds  */
20971da177e4SLinus Torvalds 
2098e8096360SNeilBrown static void fix_read_error(struct r1conf *conf, int read_disk,
2099867868fbSNeilBrown 			   sector_t sect, int sectors)
2100867868fbSNeilBrown {
2101fd01b88cSNeilBrown 	struct mddev *mddev = conf->mddev;
2102867868fbSNeilBrown 	while(sectors) {
2103867868fbSNeilBrown 		int s = sectors;
2104867868fbSNeilBrown 		int d = read_disk;
2105867868fbSNeilBrown 		int success = 0;
2106867868fbSNeilBrown 		int start;
21073cb03002SNeilBrown 		struct md_rdev *rdev;
2108867868fbSNeilBrown 
2109867868fbSNeilBrown 		if (s > (PAGE_SIZE>>9))
2110867868fbSNeilBrown 			s = PAGE_SIZE >> 9;
2111867868fbSNeilBrown 
2112867868fbSNeilBrown 		do {
2113867868fbSNeilBrown 			/* Note: no rcu protection needed here
2114867868fbSNeilBrown 			 * as this is synchronous in the raid1d thread
2115867868fbSNeilBrown 			 * which is the thread that might remove
2116867868fbSNeilBrown 			 * a device.  If raid1d ever becomes multi-threaded....
2117867868fbSNeilBrown 			 */
2118d2eb35acSNeilBrown 			sector_t first_bad;
2119d2eb35acSNeilBrown 			int bad_sectors;
2120d2eb35acSNeilBrown 
2121867868fbSNeilBrown 			rdev = conf->mirrors[d].rdev;
2122867868fbSNeilBrown 			if (rdev &&
2123da8840a7Smajianpeng 			    (test_bit(In_sync, &rdev->flags) ||
2124da8840a7Smajianpeng 			     (!test_bit(Faulty, &rdev->flags) &&
2125da8840a7Smajianpeng 			      rdev->recovery_offset >= sect + s)) &&
2126d2eb35acSNeilBrown 			    is_badblock(rdev, sect, s,
2127d2eb35acSNeilBrown 					&first_bad, &bad_sectors) == 0 &&
2128ccebd4c4SJonathan Brassow 			    sync_page_io(rdev, sect, s<<9,
2129ccebd4c4SJonathan Brassow 					 conf->tmppage, READ, false))
2130867868fbSNeilBrown 				success = 1;
2131867868fbSNeilBrown 			else {
2132867868fbSNeilBrown 				d++;
21338f19ccb2SNeilBrown 				if (d == conf->raid_disks * 2)
2134867868fbSNeilBrown 					d = 0;
2135867868fbSNeilBrown 			}
2136867868fbSNeilBrown 		} while (!success && d != read_disk);
2137867868fbSNeilBrown 
2138867868fbSNeilBrown 		if (!success) {
2139d8f05d29SNeilBrown 			/* Cannot read from anywhere - mark it bad */
21403cb03002SNeilBrown 			struct md_rdev *rdev = conf->mirrors[read_disk].rdev;
2141d8f05d29SNeilBrown 			if (!rdev_set_badblocks(rdev, sect, s, 0))
2142d8f05d29SNeilBrown 				md_error(mddev, rdev);
2143867868fbSNeilBrown 			break;
2144867868fbSNeilBrown 		}
2145867868fbSNeilBrown 		/* write it back and re-read */
2146867868fbSNeilBrown 		start = d;
2147867868fbSNeilBrown 		while (d != read_disk) {
2148867868fbSNeilBrown 			if (d==0)
21498f19ccb2SNeilBrown 				d = conf->raid_disks * 2;
2150867868fbSNeilBrown 			d--;
2151867868fbSNeilBrown 			rdev = conf->mirrors[d].rdev;
2152867868fbSNeilBrown 			if (rdev &&
2153d8f05d29SNeilBrown 			    test_bit(In_sync, &rdev->flags))
2154d8f05d29SNeilBrown 				r1_sync_page_io(rdev, sect, s,
2155d8f05d29SNeilBrown 						conf->tmppage, WRITE);
2156867868fbSNeilBrown 		}
2157867868fbSNeilBrown 		d = start;
2158867868fbSNeilBrown 		while (d != read_disk) {
2159867868fbSNeilBrown 			char b[BDEVNAME_SIZE];
2160867868fbSNeilBrown 			if (d==0)
21618f19ccb2SNeilBrown 				d = conf->raid_disks * 2;
2162867868fbSNeilBrown 			d--;
2163867868fbSNeilBrown 			rdev = conf->mirrors[d].rdev;
2164867868fbSNeilBrown 			if (rdev &&
2165867868fbSNeilBrown 			    test_bit(In_sync, &rdev->flags)) {
2166d8f05d29SNeilBrown 				if (r1_sync_page_io(rdev, sect, s,
2167d8f05d29SNeilBrown 						    conf->tmppage, READ)) {
2168867868fbSNeilBrown 					atomic_add(s, &rdev->corrected_errors);
2169867868fbSNeilBrown 					printk(KERN_INFO
21709dd1e2faSNeilBrown 					       "md/raid1:%s: read error corrected "
2171867868fbSNeilBrown 					       "(%d sectors at %llu on %s)\n",
2172867868fbSNeilBrown 					       mdname(mddev), s,
2173969b755aSRandy Dunlap 					       (unsigned long long)(sect +
2174969b755aSRandy Dunlap 					           rdev->data_offset),
2175867868fbSNeilBrown 					       bdevname(rdev->bdev, b));
2176867868fbSNeilBrown 				}
2177867868fbSNeilBrown 			}
2178867868fbSNeilBrown 		}
2179867868fbSNeilBrown 		sectors -= s;
2180867868fbSNeilBrown 		sect += s;
2181867868fbSNeilBrown 	}
2182867868fbSNeilBrown }
2183867868fbSNeilBrown 
21849f2c9d12SNeilBrown static int narrow_write_error(struct r1bio *r1_bio, int i)
2185cd5ff9a1SNeilBrown {
2186fd01b88cSNeilBrown 	struct mddev *mddev = r1_bio->mddev;
2187e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
21883cb03002SNeilBrown 	struct md_rdev *rdev = conf->mirrors[i].rdev;
2189cd5ff9a1SNeilBrown 
2190cd5ff9a1SNeilBrown 	/* bio has the data to be written to device 'i' where
2191cd5ff9a1SNeilBrown 	 * we just recently had a write error.
2192cd5ff9a1SNeilBrown 	 * We repeatedly clone the bio and trim down to one block,
2193cd5ff9a1SNeilBrown 	 * then try the write.  Where the write fails we record
2194cd5ff9a1SNeilBrown 	 * a bad block.
2195cd5ff9a1SNeilBrown 	 * It is conceivable that the bio doesn't exactly align with
2196cd5ff9a1SNeilBrown 	 * blocks.  We must handle this somehow.
2197cd5ff9a1SNeilBrown 	 *
2198cd5ff9a1SNeilBrown 	 * We currently own a reference on the rdev.
2199cd5ff9a1SNeilBrown 	 */
2200cd5ff9a1SNeilBrown 
2201cd5ff9a1SNeilBrown 	int block_sectors;
2202cd5ff9a1SNeilBrown 	sector_t sector;
2203cd5ff9a1SNeilBrown 	int sectors;
2204cd5ff9a1SNeilBrown 	int sect_to_write = r1_bio->sectors;
2205cd5ff9a1SNeilBrown 	int ok = 1;
2206cd5ff9a1SNeilBrown 
2207cd5ff9a1SNeilBrown 	if (rdev->badblocks.shift < 0)
2208cd5ff9a1SNeilBrown 		return 0;
2209cd5ff9a1SNeilBrown 
2210cd5ff9a1SNeilBrown 	block_sectors = 1 << rdev->badblocks.shift;
2211cd5ff9a1SNeilBrown 	sector = r1_bio->sector;
2212cd5ff9a1SNeilBrown 	sectors = ((sector + block_sectors)
2213cd5ff9a1SNeilBrown 		   & ~(sector_t)(block_sectors - 1))
2214cd5ff9a1SNeilBrown 		- sector;
2215cd5ff9a1SNeilBrown 
2216cd5ff9a1SNeilBrown 	while (sect_to_write) {
2217cd5ff9a1SNeilBrown 		struct bio *wbio;
2218cd5ff9a1SNeilBrown 		if (sectors > sect_to_write)
2219cd5ff9a1SNeilBrown 			sectors = sect_to_write;
2220cd5ff9a1SNeilBrown 		/* Write at 'sector' for 'sectors'*/
2221cd5ff9a1SNeilBrown 
2222b783863fSKent Overstreet 		if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
2223b783863fSKent Overstreet 			unsigned vcnt = r1_bio->behind_page_count;
2224b783863fSKent Overstreet 			struct bio_vec *vec = r1_bio->behind_bvecs;
2225b783863fSKent Overstreet 
2226b783863fSKent Overstreet 			while (!vec->bv_page) {
2227b783863fSKent Overstreet 				vec++;
2228b783863fSKent Overstreet 				vcnt--;
2229b783863fSKent Overstreet 			}
2230b783863fSKent Overstreet 
2231cd5ff9a1SNeilBrown 			wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev);
2232cd5ff9a1SNeilBrown 			memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec));
2233b783863fSKent Overstreet 
2234cd5ff9a1SNeilBrown 			wbio->bi_vcnt = vcnt;
2235b783863fSKent Overstreet 		} else {
2236b783863fSKent Overstreet 			wbio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
2237b783863fSKent Overstreet 		}
2238b783863fSKent Overstreet 
2239b783863fSKent Overstreet 		wbio->bi_rw = WRITE;
22404f024f37SKent Overstreet 		wbio->bi_iter.bi_sector = r1_bio->sector;
22414f024f37SKent Overstreet 		wbio->bi_iter.bi_size = r1_bio->sectors << 9;
2242cd5ff9a1SNeilBrown 
22436678d83fSKent Overstreet 		bio_trim(wbio, sector - r1_bio->sector, sectors);
22444f024f37SKent Overstreet 		wbio->bi_iter.bi_sector += rdev->data_offset;
2245cd5ff9a1SNeilBrown 		wbio->bi_bdev = rdev->bdev;
2246cd5ff9a1SNeilBrown 		if (submit_bio_wait(WRITE, wbio) == 0)
2247cd5ff9a1SNeilBrown 			/* failure! */
2248cd5ff9a1SNeilBrown 			ok = rdev_set_badblocks(rdev, sector,
2249cd5ff9a1SNeilBrown 						sectors, 0)
2250cd5ff9a1SNeilBrown 				&& ok;
2251cd5ff9a1SNeilBrown 
2252cd5ff9a1SNeilBrown 		bio_put(wbio);
2253cd5ff9a1SNeilBrown 		sect_to_write -= sectors;
2254cd5ff9a1SNeilBrown 		sector += sectors;
2255cd5ff9a1SNeilBrown 		sectors = block_sectors;
2256cd5ff9a1SNeilBrown 	}
2257cd5ff9a1SNeilBrown 	return ok;
2258cd5ff9a1SNeilBrown }
2259cd5ff9a1SNeilBrown 
2260e8096360SNeilBrown static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
226162096bceSNeilBrown {
226262096bceSNeilBrown 	int m;
226362096bceSNeilBrown 	int s = r1_bio->sectors;
22648f19ccb2SNeilBrown 	for (m = 0; m < conf->raid_disks * 2 ; m++) {
22653cb03002SNeilBrown 		struct md_rdev *rdev = conf->mirrors[m].rdev;
226662096bceSNeilBrown 		struct bio *bio = r1_bio->bios[m];
226762096bceSNeilBrown 		if (bio->bi_end_io == NULL)
226862096bceSNeilBrown 			continue;
226962096bceSNeilBrown 		if (test_bit(BIO_UPTODATE, &bio->bi_flags) &&
227062096bceSNeilBrown 		    test_bit(R1BIO_MadeGood, &r1_bio->state)) {
2271c6563a8cSNeilBrown 			rdev_clear_badblocks(rdev, r1_bio->sector, s, 0);
227262096bceSNeilBrown 		}
227362096bceSNeilBrown 		if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
227462096bceSNeilBrown 		    test_bit(R1BIO_WriteError, &r1_bio->state)) {
227562096bceSNeilBrown 			if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
227662096bceSNeilBrown 				md_error(conf->mddev, rdev);
227762096bceSNeilBrown 		}
227862096bceSNeilBrown 	}
227962096bceSNeilBrown 	put_buf(r1_bio);
228062096bceSNeilBrown 	md_done_sync(conf->mddev, s, 1);
228162096bceSNeilBrown }
228262096bceSNeilBrown 
2283e8096360SNeilBrown static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
228462096bceSNeilBrown {
228562096bceSNeilBrown 	int m;
22868f19ccb2SNeilBrown 	for (m = 0; m < conf->raid_disks * 2 ; m++)
228762096bceSNeilBrown 		if (r1_bio->bios[m] == IO_MADE_GOOD) {
22883cb03002SNeilBrown 			struct md_rdev *rdev = conf->mirrors[m].rdev;
228962096bceSNeilBrown 			rdev_clear_badblocks(rdev,
229062096bceSNeilBrown 					     r1_bio->sector,
2291c6563a8cSNeilBrown 					     r1_bio->sectors, 0);
229262096bceSNeilBrown 			rdev_dec_pending(rdev, conf->mddev);
229362096bceSNeilBrown 		} else if (r1_bio->bios[m] != NULL) {
229462096bceSNeilBrown 			/* This drive got a write error.  We need to
229562096bceSNeilBrown 			 * narrow down and record precise write
229662096bceSNeilBrown 			 * errors.
229762096bceSNeilBrown 			 */
229862096bceSNeilBrown 			if (!narrow_write_error(r1_bio, m)) {
229962096bceSNeilBrown 				md_error(conf->mddev,
230062096bceSNeilBrown 					 conf->mirrors[m].rdev);
230162096bceSNeilBrown 				/* an I/O failed, we can't clear the bitmap */
230262096bceSNeilBrown 				set_bit(R1BIO_Degraded, &r1_bio->state);
230362096bceSNeilBrown 			}
230462096bceSNeilBrown 			rdev_dec_pending(conf->mirrors[m].rdev,
230562096bceSNeilBrown 					 conf->mddev);
230662096bceSNeilBrown 		}
230762096bceSNeilBrown 	if (test_bit(R1BIO_WriteError, &r1_bio->state))
230862096bceSNeilBrown 		close_write(r1_bio);
230962096bceSNeilBrown 	raid_end_bio_io(r1_bio);
231062096bceSNeilBrown }
231162096bceSNeilBrown 
2312e8096360SNeilBrown static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
231362096bceSNeilBrown {
231462096bceSNeilBrown 	int disk;
231562096bceSNeilBrown 	int max_sectors;
2316fd01b88cSNeilBrown 	struct mddev *mddev = conf->mddev;
231762096bceSNeilBrown 	struct bio *bio;
231862096bceSNeilBrown 	char b[BDEVNAME_SIZE];
23193cb03002SNeilBrown 	struct md_rdev *rdev;
232062096bceSNeilBrown 
232162096bceSNeilBrown 	clear_bit(R1BIO_ReadError, &r1_bio->state);
232262096bceSNeilBrown 	/* we got a read error. Maybe the drive is bad.  Maybe just
232362096bceSNeilBrown 	 * the block and we can fix it.
232462096bceSNeilBrown 	 * We freeze all other IO, and try reading the block from
232562096bceSNeilBrown 	 * other devices.  When we find one, we re-write
232662096bceSNeilBrown 	 * and check it that fixes the read error.
232762096bceSNeilBrown 	 * This is all done synchronously while the array is
232862096bceSNeilBrown 	 * frozen
232962096bceSNeilBrown 	 */
233062096bceSNeilBrown 	if (mddev->ro == 0) {
2331e2d59925SNeilBrown 		freeze_array(conf, 1);
233262096bceSNeilBrown 		fix_read_error(conf, r1_bio->read_disk,
233362096bceSNeilBrown 			       r1_bio->sector, r1_bio->sectors);
233462096bceSNeilBrown 		unfreeze_array(conf);
233562096bceSNeilBrown 	} else
233662096bceSNeilBrown 		md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
23377ad4d4a6SNeilBrown 	rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev);
233862096bceSNeilBrown 
233962096bceSNeilBrown 	bio = r1_bio->bios[r1_bio->read_disk];
234062096bceSNeilBrown 	bdevname(bio->bi_bdev, b);
234162096bceSNeilBrown read_more:
234262096bceSNeilBrown 	disk = read_balance(conf, r1_bio, &max_sectors);
234362096bceSNeilBrown 	if (disk == -1) {
234462096bceSNeilBrown 		printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O"
234562096bceSNeilBrown 		       " read error for block %llu\n",
234662096bceSNeilBrown 		       mdname(mddev), b, (unsigned long long)r1_bio->sector);
234762096bceSNeilBrown 		raid_end_bio_io(r1_bio);
234862096bceSNeilBrown 	} else {
234962096bceSNeilBrown 		const unsigned long do_sync
235062096bceSNeilBrown 			= r1_bio->master_bio->bi_rw & REQ_SYNC;
235162096bceSNeilBrown 		if (bio) {
235262096bceSNeilBrown 			r1_bio->bios[r1_bio->read_disk] =
235362096bceSNeilBrown 				mddev->ro ? IO_BLOCKED : NULL;
235462096bceSNeilBrown 			bio_put(bio);
235562096bceSNeilBrown 		}
235662096bceSNeilBrown 		r1_bio->read_disk = disk;
235762096bceSNeilBrown 		bio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
23584f024f37SKent Overstreet 		bio_trim(bio, r1_bio->sector - bio->bi_iter.bi_sector,
23594f024f37SKent Overstreet 			 max_sectors);
236062096bceSNeilBrown 		r1_bio->bios[r1_bio->read_disk] = bio;
236162096bceSNeilBrown 		rdev = conf->mirrors[disk].rdev;
236262096bceSNeilBrown 		printk_ratelimited(KERN_ERR
236362096bceSNeilBrown 				   "md/raid1:%s: redirecting sector %llu"
236462096bceSNeilBrown 				   " to other mirror: %s\n",
236562096bceSNeilBrown 				   mdname(mddev),
236662096bceSNeilBrown 				   (unsigned long long)r1_bio->sector,
236762096bceSNeilBrown 				   bdevname(rdev->bdev, b));
23684f024f37SKent Overstreet 		bio->bi_iter.bi_sector = r1_bio->sector + rdev->data_offset;
236962096bceSNeilBrown 		bio->bi_bdev = rdev->bdev;
237062096bceSNeilBrown 		bio->bi_end_io = raid1_end_read_request;
237162096bceSNeilBrown 		bio->bi_rw = READ | do_sync;
237262096bceSNeilBrown 		bio->bi_private = r1_bio;
237362096bceSNeilBrown 		if (max_sectors < r1_bio->sectors) {
237462096bceSNeilBrown 			/* Drat - have to split this up more */
237562096bceSNeilBrown 			struct bio *mbio = r1_bio->master_bio;
237662096bceSNeilBrown 			int sectors_handled = (r1_bio->sector + max_sectors
23774f024f37SKent Overstreet 					       - mbio->bi_iter.bi_sector);
237862096bceSNeilBrown 			r1_bio->sectors = max_sectors;
237962096bceSNeilBrown 			spin_lock_irq(&conf->device_lock);
238062096bceSNeilBrown 			if (mbio->bi_phys_segments == 0)
238162096bceSNeilBrown 				mbio->bi_phys_segments = 2;
238262096bceSNeilBrown 			else
238362096bceSNeilBrown 				mbio->bi_phys_segments++;
238462096bceSNeilBrown 			spin_unlock_irq(&conf->device_lock);
238562096bceSNeilBrown 			generic_make_request(bio);
238662096bceSNeilBrown 			bio = NULL;
238762096bceSNeilBrown 
238862096bceSNeilBrown 			r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
238962096bceSNeilBrown 
239062096bceSNeilBrown 			r1_bio->master_bio = mbio;
2391aa8b57aaSKent Overstreet 			r1_bio->sectors = bio_sectors(mbio) - sectors_handled;
239262096bceSNeilBrown 			r1_bio->state = 0;
239362096bceSNeilBrown 			set_bit(R1BIO_ReadError, &r1_bio->state);
239462096bceSNeilBrown 			r1_bio->mddev = mddev;
23954f024f37SKent Overstreet 			r1_bio->sector = mbio->bi_iter.bi_sector +
23964f024f37SKent Overstreet 				sectors_handled;
239762096bceSNeilBrown 
239862096bceSNeilBrown 			goto read_more;
239962096bceSNeilBrown 		} else
240062096bceSNeilBrown 			generic_make_request(bio);
240162096bceSNeilBrown 	}
240262096bceSNeilBrown }
240362096bceSNeilBrown 
24044ed8731dSShaohua Li static void raid1d(struct md_thread *thread)
24051da177e4SLinus Torvalds {
24064ed8731dSShaohua Li 	struct mddev *mddev = thread->mddev;
24079f2c9d12SNeilBrown 	struct r1bio *r1_bio;
24081da177e4SLinus Torvalds 	unsigned long flags;
2409e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
24101da177e4SLinus Torvalds 	struct list_head *head = &conf->retry_list;
2411e1dfa0a2SNeilBrown 	struct blk_plug plug;
24121da177e4SLinus Torvalds 
24131da177e4SLinus Torvalds 	md_check_recovery(mddev);
24141da177e4SLinus Torvalds 
2415e1dfa0a2SNeilBrown 	blk_start_plug(&plug);
24161da177e4SLinus Torvalds 	for (;;) {
2417a35e63efSNeilBrown 
24187eaceaccSJens Axboe 		flush_pending_writes(conf);
2419a35e63efSNeilBrown 
24201da177e4SLinus Torvalds 		spin_lock_irqsave(&conf->device_lock, flags);
2421a35e63efSNeilBrown 		if (list_empty(head)) {
2422191ea9b2SNeilBrown 			spin_unlock_irqrestore(&conf->device_lock, flags);
24231da177e4SLinus Torvalds 			break;
2424a35e63efSNeilBrown 		}
24259f2c9d12SNeilBrown 		r1_bio = list_entry(head->prev, struct r1bio, retry_list);
24261da177e4SLinus Torvalds 		list_del(head->prev);
2427ddaf22abSNeilBrown 		conf->nr_queued--;
24281da177e4SLinus Torvalds 		spin_unlock_irqrestore(&conf->device_lock, flags);
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds 		mddev = r1_bio->mddev;
2431070ec55dSNeilBrown 		conf = mddev->private;
24324367af55SNeilBrown 		if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
2433d8f05d29SNeilBrown 			if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
243462096bceSNeilBrown 			    test_bit(R1BIO_WriteError, &r1_bio->state))
243562096bceSNeilBrown 				handle_sync_write_finished(conf, r1_bio);
243662096bceSNeilBrown 			else
24371da177e4SLinus Torvalds 				sync_request_write(mddev, r1_bio);
2438cd5ff9a1SNeilBrown 		} else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
243962096bceSNeilBrown 			   test_bit(R1BIO_WriteError, &r1_bio->state))
244062096bceSNeilBrown 			handle_write_finished(conf, r1_bio);
244162096bceSNeilBrown 		else if (test_bit(R1BIO_ReadError, &r1_bio->state))
244262096bceSNeilBrown 			handle_read_error(conf, r1_bio);
2443d2eb35acSNeilBrown 		else
2444d2eb35acSNeilBrown 			/* just a partial read to be scheduled from separate
2445d2eb35acSNeilBrown 			 * context
2446d2eb35acSNeilBrown 			 */
2447d2eb35acSNeilBrown 			generic_make_request(r1_bio->bios[r1_bio->read_disk]);
244862096bceSNeilBrown 
24491d9d5241SNeilBrown 		cond_resched();
2450de393cdeSNeilBrown 		if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2451de393cdeSNeilBrown 			md_check_recovery(mddev);
24521da177e4SLinus Torvalds 	}
2453e1dfa0a2SNeilBrown 	blk_finish_plug(&plug);
24541da177e4SLinus Torvalds }
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds 
2457e8096360SNeilBrown static int init_resync(struct r1conf *conf)
24581da177e4SLinus Torvalds {
24591da177e4SLinus Torvalds 	int buffs;
24601da177e4SLinus Torvalds 
24611da177e4SLinus Torvalds 	buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
24629e77c485SEric Sesterhenn 	BUG_ON(conf->r1buf_pool);
24631da177e4SLinus Torvalds 	conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
24641da177e4SLinus Torvalds 					  conf->poolinfo);
24651da177e4SLinus Torvalds 	if (!conf->r1buf_pool)
24661da177e4SLinus Torvalds 		return -ENOMEM;
24671da177e4SLinus Torvalds 	conf->next_resync = 0;
24681da177e4SLinus Torvalds 	return 0;
24691da177e4SLinus Torvalds }
24701da177e4SLinus Torvalds 
24711da177e4SLinus Torvalds /*
24721da177e4SLinus Torvalds  * perform a "sync" on one "block"
24731da177e4SLinus Torvalds  *
24741da177e4SLinus Torvalds  * We need to make sure that no normal I/O request - particularly write
24751da177e4SLinus Torvalds  * requests - conflict with active sync requests.
24761da177e4SLinus Torvalds  *
24771da177e4SLinus Torvalds  * This is achieved by tracking pending requests and a 'barrier' concept
24781da177e4SLinus Torvalds  * that can be installed to exclude normal IO requests.
24791da177e4SLinus Torvalds  */
24801da177e4SLinus Torvalds 
2481fd01b88cSNeilBrown static sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
24821da177e4SLinus Torvalds {
2483e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
24849f2c9d12SNeilBrown 	struct r1bio *r1_bio;
24851da177e4SLinus Torvalds 	struct bio *bio;
24861da177e4SLinus Torvalds 	sector_t max_sector, nr_sectors;
24873e198f78SNeilBrown 	int disk = -1;
24881da177e4SLinus Torvalds 	int i;
24893e198f78SNeilBrown 	int wonly = -1;
24903e198f78SNeilBrown 	int write_targets = 0, read_targets = 0;
249157dab0bdSNeilBrown 	sector_t sync_blocks;
2492e3b9703eSNeilBrown 	int still_degraded = 0;
249306f60385SNeilBrown 	int good_sectors = RESYNC_SECTORS;
249406f60385SNeilBrown 	int min_bad = 0; /* number of sectors that are bad in all devices */
24951da177e4SLinus Torvalds 
24961da177e4SLinus Torvalds 	if (!conf->r1buf_pool)
24971da177e4SLinus Torvalds 		if (init_resync(conf))
249857afd89fSNeilBrown 			return 0;
24991da177e4SLinus Torvalds 
250058c0fed4SAndre Noll 	max_sector = mddev->dev_sectors;
25011da177e4SLinus Torvalds 	if (sector_nr >= max_sector) {
2502191ea9b2SNeilBrown 		/* If we aborted, we need to abort the
2503191ea9b2SNeilBrown 		 * sync on the 'current' bitmap chunk (there will
2504191ea9b2SNeilBrown 		 * only be one in raid1 resync.
2505191ea9b2SNeilBrown 		 * We can find the current addess in mddev->curr_resync
2506191ea9b2SNeilBrown 		 */
25076a806c51SNeilBrown 		if (mddev->curr_resync < max_sector) /* aborted */
25086a806c51SNeilBrown 			bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2509191ea9b2SNeilBrown 						&sync_blocks, 1);
25106a806c51SNeilBrown 		else /* completed sync */
2511191ea9b2SNeilBrown 			conf->fullsync = 0;
25126a806c51SNeilBrown 
25136a806c51SNeilBrown 		bitmap_close_sync(mddev->bitmap);
25141da177e4SLinus Torvalds 		close_sync(conf);
25151da177e4SLinus Torvalds 		return 0;
25161da177e4SLinus Torvalds 	}
25171da177e4SLinus Torvalds 
251807d84d10SNeilBrown 	if (mddev->bitmap == NULL &&
251907d84d10SNeilBrown 	    mddev->recovery_cp == MaxSector &&
25206394cca5SNeilBrown 	    !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
252107d84d10SNeilBrown 	    conf->fullsync == 0) {
252207d84d10SNeilBrown 		*skipped = 1;
252307d84d10SNeilBrown 		return max_sector - sector_nr;
252407d84d10SNeilBrown 	}
25256394cca5SNeilBrown 	/* before building a request, check if we can skip these blocks..
25266394cca5SNeilBrown 	 * This call the bitmap_start_sync doesn't actually record anything
25276394cca5SNeilBrown 	 */
2528e3b9703eSNeilBrown 	if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
2529e5de485fSNeilBrown 	    !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2530191ea9b2SNeilBrown 		/* We can skip this block, and probably several more */
2531191ea9b2SNeilBrown 		*skipped = 1;
2532191ea9b2SNeilBrown 		return sync_blocks;
2533191ea9b2SNeilBrown 	}
25341da177e4SLinus Torvalds 	/*
253517999be4SNeilBrown 	 * If there is non-resync activity waiting for a turn,
253617999be4SNeilBrown 	 * and resync is going fast enough,
253717999be4SNeilBrown 	 * then let it though before starting on this new sync request.
25381da177e4SLinus Torvalds 	 */
253917999be4SNeilBrown 	if (!go_faster && conf->nr_waiting)
25401da177e4SLinus Torvalds 		msleep_interruptible(1000);
254117999be4SNeilBrown 
2542b47490c9SNeilBrown 	bitmap_cond_end_sync(mddev->bitmap, sector_nr);
25431c4588e9SNeilBrown 	r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
254417999be4SNeilBrown 	raise_barrier(conf);
254517999be4SNeilBrown 
254617999be4SNeilBrown 	conf->next_resync = sector_nr;
25471da177e4SLinus Torvalds 
25483e198f78SNeilBrown 	rcu_read_lock();
25493e198f78SNeilBrown 	/*
25503e198f78SNeilBrown 	 * If we get a correctably read error during resync or recovery,
25513e198f78SNeilBrown 	 * we might want to read from a different device.  So we
25523e198f78SNeilBrown 	 * flag all drives that could conceivably be read from for READ,
25533e198f78SNeilBrown 	 * and any others (which will be non-In_sync devices) for WRITE.
25543e198f78SNeilBrown 	 * If a read fails, we try reading from something else for which READ
25553e198f78SNeilBrown 	 * is OK.
25563e198f78SNeilBrown 	 */
25571da177e4SLinus Torvalds 
25581da177e4SLinus Torvalds 	r1_bio->mddev = mddev;
25591da177e4SLinus Torvalds 	r1_bio->sector = sector_nr;
2560191ea9b2SNeilBrown 	r1_bio->state = 0;
25611da177e4SLinus Torvalds 	set_bit(R1BIO_IsSync, &r1_bio->state);
25621da177e4SLinus Torvalds 
25638f19ccb2SNeilBrown 	for (i = 0; i < conf->raid_disks * 2; i++) {
25643cb03002SNeilBrown 		struct md_rdev *rdev;
25651da177e4SLinus Torvalds 		bio = r1_bio->bios[i];
25662aabaa65SKent Overstreet 		bio_reset(bio);
25671da177e4SLinus Torvalds 
25683e198f78SNeilBrown 		rdev = rcu_dereference(conf->mirrors[i].rdev);
25693e198f78SNeilBrown 		if (rdev == NULL ||
25703e198f78SNeilBrown 		    test_bit(Faulty, &rdev->flags)) {
25718f19ccb2SNeilBrown 			if (i < conf->raid_disks)
2572e3b9703eSNeilBrown 				still_degraded = 1;
25733e198f78SNeilBrown 		} else if (!test_bit(In_sync, &rdev->flags)) {
25741da177e4SLinus Torvalds 			bio->bi_rw = WRITE;
25751da177e4SLinus Torvalds 			bio->bi_end_io = end_sync_write;
25761da177e4SLinus Torvalds 			write_targets ++;
25773e198f78SNeilBrown 		} else {
25783e198f78SNeilBrown 			/* may need to read from here */
257906f60385SNeilBrown 			sector_t first_bad = MaxSector;
258006f60385SNeilBrown 			int bad_sectors;
258106f60385SNeilBrown 
258206f60385SNeilBrown 			if (is_badblock(rdev, sector_nr, good_sectors,
258306f60385SNeilBrown 					&first_bad, &bad_sectors)) {
258406f60385SNeilBrown 				if (first_bad > sector_nr)
258506f60385SNeilBrown 					good_sectors = first_bad - sector_nr;
258606f60385SNeilBrown 				else {
258706f60385SNeilBrown 					bad_sectors -= (sector_nr - first_bad);
258806f60385SNeilBrown 					if (min_bad == 0 ||
258906f60385SNeilBrown 					    min_bad > bad_sectors)
259006f60385SNeilBrown 						min_bad = bad_sectors;
259106f60385SNeilBrown 				}
259206f60385SNeilBrown 			}
259306f60385SNeilBrown 			if (sector_nr < first_bad) {
25943e198f78SNeilBrown 				if (test_bit(WriteMostly, &rdev->flags)) {
25953e198f78SNeilBrown 					if (wonly < 0)
25963e198f78SNeilBrown 						wonly = i;
25973e198f78SNeilBrown 				} else {
25983e198f78SNeilBrown 					if (disk < 0)
25993e198f78SNeilBrown 						disk = i;
26003e198f78SNeilBrown 				}
260106f60385SNeilBrown 				bio->bi_rw = READ;
260206f60385SNeilBrown 				bio->bi_end_io = end_sync_read;
26033e198f78SNeilBrown 				read_targets++;
2604d57368afSAlexander Lyakas 			} else if (!test_bit(WriteErrorSeen, &rdev->flags) &&
2605d57368afSAlexander Lyakas 				test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
2606d57368afSAlexander Lyakas 				!test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
2607d57368afSAlexander Lyakas 				/*
2608d57368afSAlexander Lyakas 				 * The device is suitable for reading (InSync),
2609d57368afSAlexander Lyakas 				 * but has bad block(s) here. Let's try to correct them,
2610d57368afSAlexander Lyakas 				 * if we are doing resync or repair. Otherwise, leave
2611d57368afSAlexander Lyakas 				 * this device alone for this sync request.
2612d57368afSAlexander Lyakas 				 */
2613d57368afSAlexander Lyakas 				bio->bi_rw = WRITE;
2614d57368afSAlexander Lyakas 				bio->bi_end_io = end_sync_write;
2615d57368afSAlexander Lyakas 				write_targets++;
26163e198f78SNeilBrown 			}
261706f60385SNeilBrown 		}
261806f60385SNeilBrown 		if (bio->bi_end_io) {
26193e198f78SNeilBrown 			atomic_inc(&rdev->nr_pending);
26204f024f37SKent Overstreet 			bio->bi_iter.bi_sector = sector_nr + rdev->data_offset;
26213e198f78SNeilBrown 			bio->bi_bdev = rdev->bdev;
26221da177e4SLinus Torvalds 			bio->bi_private = r1_bio;
26231da177e4SLinus Torvalds 		}
262406f60385SNeilBrown 	}
26253e198f78SNeilBrown 	rcu_read_unlock();
26263e198f78SNeilBrown 	if (disk < 0)
26273e198f78SNeilBrown 		disk = wonly;
26283e198f78SNeilBrown 	r1_bio->read_disk = disk;
2629191ea9b2SNeilBrown 
263006f60385SNeilBrown 	if (read_targets == 0 && min_bad > 0) {
263106f60385SNeilBrown 		/* These sectors are bad on all InSync devices, so we
263206f60385SNeilBrown 		 * need to mark them bad on all write targets
263306f60385SNeilBrown 		 */
263406f60385SNeilBrown 		int ok = 1;
26358f19ccb2SNeilBrown 		for (i = 0 ; i < conf->raid_disks * 2 ; i++)
263606f60385SNeilBrown 			if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
2637a42f9d83Smajianpeng 				struct md_rdev *rdev = conf->mirrors[i].rdev;
263806f60385SNeilBrown 				ok = rdev_set_badblocks(rdev, sector_nr,
263906f60385SNeilBrown 							min_bad, 0
264006f60385SNeilBrown 					) && ok;
264106f60385SNeilBrown 			}
264206f60385SNeilBrown 		set_bit(MD_CHANGE_DEVS, &mddev->flags);
264306f60385SNeilBrown 		*skipped = 1;
264406f60385SNeilBrown 		put_buf(r1_bio);
264506f60385SNeilBrown 
264606f60385SNeilBrown 		if (!ok) {
264706f60385SNeilBrown 			/* Cannot record the badblocks, so need to
264806f60385SNeilBrown 			 * abort the resync.
264906f60385SNeilBrown 			 * If there are multiple read targets, could just
265006f60385SNeilBrown 			 * fail the really bad ones ???
265106f60385SNeilBrown 			 */
265206f60385SNeilBrown 			conf->recovery_disabled = mddev->recovery_disabled;
265306f60385SNeilBrown 			set_bit(MD_RECOVERY_INTR, &mddev->recovery);
265406f60385SNeilBrown 			return 0;
265506f60385SNeilBrown 		} else
265606f60385SNeilBrown 			return min_bad;
265706f60385SNeilBrown 
265806f60385SNeilBrown 	}
265906f60385SNeilBrown 	if (min_bad > 0 && min_bad < good_sectors) {
266006f60385SNeilBrown 		/* only resync enough to reach the next bad->good
266106f60385SNeilBrown 		 * transition */
266206f60385SNeilBrown 		good_sectors = min_bad;
266306f60385SNeilBrown 	}
266406f60385SNeilBrown 
26653e198f78SNeilBrown 	if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
26663e198f78SNeilBrown 		/* extra read targets are also write targets */
26673e198f78SNeilBrown 		write_targets += read_targets-1;
26683e198f78SNeilBrown 
26693e198f78SNeilBrown 	if (write_targets == 0 || read_targets == 0) {
26701da177e4SLinus Torvalds 		/* There is nowhere to write, so all non-sync
26711da177e4SLinus Torvalds 		 * drives must be failed - so we are finished
26721da177e4SLinus Torvalds 		 */
2673b7219ccbSNeilBrown 		sector_t rv;
2674b7219ccbSNeilBrown 		if (min_bad > 0)
2675b7219ccbSNeilBrown 			max_sector = sector_nr + min_bad;
2676b7219ccbSNeilBrown 		rv = max_sector - sector_nr;
267757afd89fSNeilBrown 		*skipped = 1;
26781da177e4SLinus Torvalds 		put_buf(r1_bio);
26791da177e4SLinus Torvalds 		return rv;
26801da177e4SLinus Torvalds 	}
26811da177e4SLinus Torvalds 
2682c6207277SNeilBrown 	if (max_sector > mddev->resync_max)
2683c6207277SNeilBrown 		max_sector = mddev->resync_max; /* Don't do IO beyond here */
268406f60385SNeilBrown 	if (max_sector > sector_nr + good_sectors)
268506f60385SNeilBrown 		max_sector = sector_nr + good_sectors;
26861da177e4SLinus Torvalds 	nr_sectors = 0;
2687289e99e8SNeilBrown 	sync_blocks = 0;
26881da177e4SLinus Torvalds 	do {
26891da177e4SLinus Torvalds 		struct page *page;
26901da177e4SLinus Torvalds 		int len = PAGE_SIZE;
26911da177e4SLinus Torvalds 		if (sector_nr + (len>>9) > max_sector)
26921da177e4SLinus Torvalds 			len = (max_sector - sector_nr) << 9;
26931da177e4SLinus Torvalds 		if (len == 0)
26941da177e4SLinus Torvalds 			break;
2695ab7a30c7SNeilBrown 		if (sync_blocks == 0) {
26966a806c51SNeilBrown 			if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2697e3b9703eSNeilBrown 					       &sync_blocks, still_degraded) &&
2698e5de485fSNeilBrown 			    !conf->fullsync &&
2699e5de485fSNeilBrown 			    !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2700191ea9b2SNeilBrown 				break;
27019e77c485SEric Sesterhenn 			BUG_ON(sync_blocks < (PAGE_SIZE>>9));
27027571ae88SNeilBrown 			if ((len >> 9) > sync_blocks)
27036a806c51SNeilBrown 				len = sync_blocks<<9;
2704ab7a30c7SNeilBrown 		}
2705191ea9b2SNeilBrown 
27068f19ccb2SNeilBrown 		for (i = 0 ; i < conf->raid_disks * 2; i++) {
27071da177e4SLinus Torvalds 			bio = r1_bio->bios[i];
27081da177e4SLinus Torvalds 			if (bio->bi_end_io) {
2709d11c171eSNeilBrown 				page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
27101da177e4SLinus Torvalds 				if (bio_add_page(bio, page, len, 0) == 0) {
27111da177e4SLinus Torvalds 					/* stop here */
2712d11c171eSNeilBrown 					bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
27131da177e4SLinus Torvalds 					while (i > 0) {
27141da177e4SLinus Torvalds 						i--;
27151da177e4SLinus Torvalds 						bio = r1_bio->bios[i];
27166a806c51SNeilBrown 						if (bio->bi_end_io==NULL)
27176a806c51SNeilBrown 							continue;
27181da177e4SLinus Torvalds 						/* remove last page from this bio */
27191da177e4SLinus Torvalds 						bio->bi_vcnt--;
27204f024f37SKent Overstreet 						bio->bi_iter.bi_size -= len;
27211da177e4SLinus Torvalds 						bio->bi_flags &= ~(1<< BIO_SEG_VALID);
27221da177e4SLinus Torvalds 					}
27231da177e4SLinus Torvalds 					goto bio_full;
27241da177e4SLinus Torvalds 				}
27251da177e4SLinus Torvalds 			}
27261da177e4SLinus Torvalds 		}
27271da177e4SLinus Torvalds 		nr_sectors += len>>9;
27281da177e4SLinus Torvalds 		sector_nr += len>>9;
2729191ea9b2SNeilBrown 		sync_blocks -= (len>>9);
27301da177e4SLinus Torvalds 	} while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
27311da177e4SLinus Torvalds  bio_full:
27321da177e4SLinus Torvalds 	r1_bio->sectors = nr_sectors;
27331da177e4SLinus Torvalds 
2734d11c171eSNeilBrown 	/* For a user-requested sync, we read all readable devices and do a
2735d11c171eSNeilBrown 	 * compare
2736d11c171eSNeilBrown 	 */
2737d11c171eSNeilBrown 	if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2738d11c171eSNeilBrown 		atomic_set(&r1_bio->remaining, read_targets);
27392d4f4f33SNeilBrown 		for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) {
2740d11c171eSNeilBrown 			bio = r1_bio->bios[i];
2741d11c171eSNeilBrown 			if (bio->bi_end_io == end_sync_read) {
27422d4f4f33SNeilBrown 				read_targets--;
2743ddac7c7eSNeilBrown 				md_sync_acct(bio->bi_bdev, nr_sectors);
27441da177e4SLinus Torvalds 				generic_make_request(bio);
2745d11c171eSNeilBrown 			}
2746d11c171eSNeilBrown 		}
2747d11c171eSNeilBrown 	} else {
2748d11c171eSNeilBrown 		atomic_set(&r1_bio->remaining, 1);
2749d11c171eSNeilBrown 		bio = r1_bio->bios[r1_bio->read_disk];
2750ddac7c7eSNeilBrown 		md_sync_acct(bio->bi_bdev, nr_sectors);
2751d11c171eSNeilBrown 		generic_make_request(bio);
2752d11c171eSNeilBrown 
2753d11c171eSNeilBrown 	}
27541da177e4SLinus Torvalds 	return nr_sectors;
27551da177e4SLinus Torvalds }
27561da177e4SLinus Torvalds 
2757fd01b88cSNeilBrown static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks)
275880c3a6ceSDan Williams {
275980c3a6ceSDan Williams 	if (sectors)
276080c3a6ceSDan Williams 		return sectors;
276180c3a6ceSDan Williams 
276280c3a6ceSDan Williams 	return mddev->dev_sectors;
276380c3a6ceSDan Williams }
276480c3a6ceSDan Williams 
2765e8096360SNeilBrown static struct r1conf *setup_conf(struct mddev *mddev)
27661da177e4SLinus Torvalds {
2767e8096360SNeilBrown 	struct r1conf *conf;
2768709ae487SNeilBrown 	int i;
27690eaf822cSJonathan Brassow 	struct raid1_info *disk;
27703cb03002SNeilBrown 	struct md_rdev *rdev;
2771709ae487SNeilBrown 	int err = -ENOMEM;
27721da177e4SLinus Torvalds 
2773e8096360SNeilBrown 	conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL);
27741da177e4SLinus Torvalds 	if (!conf)
2775709ae487SNeilBrown 		goto abort;
27761da177e4SLinus Torvalds 
27770eaf822cSJonathan Brassow 	conf->mirrors = kzalloc(sizeof(struct raid1_info)
27788f19ccb2SNeilBrown 				* mddev->raid_disks * 2,
27791da177e4SLinus Torvalds 				 GFP_KERNEL);
27801da177e4SLinus Torvalds 	if (!conf->mirrors)
2781709ae487SNeilBrown 		goto abort;
27821da177e4SLinus Torvalds 
2783ddaf22abSNeilBrown 	conf->tmppage = alloc_page(GFP_KERNEL);
2784ddaf22abSNeilBrown 	if (!conf->tmppage)
2785709ae487SNeilBrown 		goto abort;
2786ddaf22abSNeilBrown 
2787709ae487SNeilBrown 	conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
27881da177e4SLinus Torvalds 	if (!conf->poolinfo)
2789709ae487SNeilBrown 		goto abort;
27908f19ccb2SNeilBrown 	conf->poolinfo->raid_disks = mddev->raid_disks * 2;
27911da177e4SLinus Torvalds 	conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
27921da177e4SLinus Torvalds 					  r1bio_pool_free,
27931da177e4SLinus Torvalds 					  conf->poolinfo);
27941da177e4SLinus Torvalds 	if (!conf->r1bio_pool)
2795709ae487SNeilBrown 		goto abort;
2796709ae487SNeilBrown 
2797ed9bfdf1SNeilBrown 	conf->poolinfo->mddev = mddev;
27981da177e4SLinus Torvalds 
2799c19d5798SNeilBrown 	err = -EINVAL;
2800e7e72bf6SNeil Brown 	spin_lock_init(&conf->device_lock);
2801dafb20faSNeilBrown 	rdev_for_each(rdev, mddev) {
2802aba336bdSNeilBrown 		struct request_queue *q;
2803709ae487SNeilBrown 		int disk_idx = rdev->raid_disk;
28041da177e4SLinus Torvalds 		if (disk_idx >= mddev->raid_disks
28051da177e4SLinus Torvalds 		    || disk_idx < 0)
28061da177e4SLinus Torvalds 			continue;
2807c19d5798SNeilBrown 		if (test_bit(Replacement, &rdev->flags))
280802b898f2SNeilBrown 			disk = conf->mirrors + mddev->raid_disks + disk_idx;
2809c19d5798SNeilBrown 		else
28101da177e4SLinus Torvalds 			disk = conf->mirrors + disk_idx;
28111da177e4SLinus Torvalds 
2812c19d5798SNeilBrown 		if (disk->rdev)
2813c19d5798SNeilBrown 			goto abort;
28141da177e4SLinus Torvalds 		disk->rdev = rdev;
2815aba336bdSNeilBrown 		q = bdev_get_queue(rdev->bdev);
2816aba336bdSNeilBrown 		if (q->merge_bvec_fn)
2817aba336bdSNeilBrown 			mddev->merge_check_needed = 1;
28181da177e4SLinus Torvalds 
28191da177e4SLinus Torvalds 		disk->head_position = 0;
282012cee5a8SShaohua Li 		disk->seq_start = MaxSector;
28211da177e4SLinus Torvalds 	}
28221da177e4SLinus Torvalds 	conf->raid_disks = mddev->raid_disks;
28231da177e4SLinus Torvalds 	conf->mddev = mddev;
28241da177e4SLinus Torvalds 	INIT_LIST_HEAD(&conf->retry_list);
28251da177e4SLinus Torvalds 
28261da177e4SLinus Torvalds 	spin_lock_init(&conf->resync_lock);
282717999be4SNeilBrown 	init_waitqueue_head(&conf->wait_barrier);
28281da177e4SLinus Torvalds 
2829191ea9b2SNeilBrown 	bio_list_init(&conf->pending_bio_list);
283034db0cd6SNeilBrown 	conf->pending_count = 0;
2831d890fa2bSNeilBrown 	conf->recovery_disabled = mddev->recovery_disabled - 1;
2832191ea9b2SNeilBrown 
283379ef3a8aSmajianpeng 	conf->start_next_window = MaxSector;
283479ef3a8aSmajianpeng 	conf->current_window_requests = conf->next_window_requests = 0;
283579ef3a8aSmajianpeng 
2836c19d5798SNeilBrown 	err = -EIO;
28378f19ccb2SNeilBrown 	for (i = 0; i < conf->raid_disks * 2; i++) {
28381da177e4SLinus Torvalds 
28391da177e4SLinus Torvalds 		disk = conf->mirrors + i;
28401da177e4SLinus Torvalds 
2841c19d5798SNeilBrown 		if (i < conf->raid_disks &&
2842c19d5798SNeilBrown 		    disk[conf->raid_disks].rdev) {
2843c19d5798SNeilBrown 			/* This slot has a replacement. */
2844c19d5798SNeilBrown 			if (!disk->rdev) {
2845c19d5798SNeilBrown 				/* No original, just make the replacement
2846c19d5798SNeilBrown 				 * a recovering spare
2847c19d5798SNeilBrown 				 */
2848c19d5798SNeilBrown 				disk->rdev =
2849c19d5798SNeilBrown 					disk[conf->raid_disks].rdev;
2850c19d5798SNeilBrown 				disk[conf->raid_disks].rdev = NULL;
2851c19d5798SNeilBrown 			} else if (!test_bit(In_sync, &disk->rdev->flags))
2852c19d5798SNeilBrown 				/* Original is not in_sync - bad */
2853c19d5798SNeilBrown 				goto abort;
2854c19d5798SNeilBrown 		}
2855c19d5798SNeilBrown 
28565fd6c1dcSNeilBrown 		if (!disk->rdev ||
28575fd6c1dcSNeilBrown 		    !test_bit(In_sync, &disk->rdev->flags)) {
28581da177e4SLinus Torvalds 			disk->head_position = 0;
28594f0a5e01SJonathan Brassow 			if (disk->rdev &&
28604f0a5e01SJonathan Brassow 			    (disk->rdev->saved_raid_disk < 0))
286117571284SNeilBrown 				conf->fullsync = 1;
2862be4d3280SShaohua Li 		}
28631da177e4SLinus Torvalds 	}
2864709ae487SNeilBrown 
2865709ae487SNeilBrown 	err = -ENOMEM;
28660232605dSNeilBrown 	conf->thread = md_register_thread(raid1d, mddev, "raid1");
2867709ae487SNeilBrown 	if (!conf->thread) {
28681da177e4SLinus Torvalds 		printk(KERN_ERR
28699dd1e2faSNeilBrown 		       "md/raid1:%s: couldn't allocate thread\n",
28701da177e4SLinus Torvalds 		       mdname(mddev));
2871709ae487SNeilBrown 		goto abort;
28721da177e4SLinus Torvalds 	}
2873191ea9b2SNeilBrown 
2874709ae487SNeilBrown 	return conf;
2875709ae487SNeilBrown 
2876709ae487SNeilBrown  abort:
2877709ae487SNeilBrown 	if (conf) {
2878709ae487SNeilBrown 		if (conf->r1bio_pool)
2879709ae487SNeilBrown 			mempool_destroy(conf->r1bio_pool);
2880709ae487SNeilBrown 		kfree(conf->mirrors);
2881709ae487SNeilBrown 		safe_put_page(conf->tmppage);
2882709ae487SNeilBrown 		kfree(conf->poolinfo);
2883709ae487SNeilBrown 		kfree(conf);
2884709ae487SNeilBrown 	}
2885709ae487SNeilBrown 	return ERR_PTR(err);
2886709ae487SNeilBrown }
2887709ae487SNeilBrown 
28885220ea1eSmajianpeng static int stop(struct mddev *mddev);
2889fd01b88cSNeilBrown static int run(struct mddev *mddev)
2890709ae487SNeilBrown {
2891e8096360SNeilBrown 	struct r1conf *conf;
2892709ae487SNeilBrown 	int i;
28933cb03002SNeilBrown 	struct md_rdev *rdev;
28945220ea1eSmajianpeng 	int ret;
28952ff8cc2cSShaohua Li 	bool discard_supported = false;
2896709ae487SNeilBrown 
2897709ae487SNeilBrown 	if (mddev->level != 1) {
28989dd1e2faSNeilBrown 		printk(KERN_ERR "md/raid1:%s: raid level not set to mirroring (%d)\n",
2899709ae487SNeilBrown 		       mdname(mddev), mddev->level);
2900709ae487SNeilBrown 		return -EIO;
2901709ae487SNeilBrown 	}
2902709ae487SNeilBrown 	if (mddev->reshape_position != MaxSector) {
29039dd1e2faSNeilBrown 		printk(KERN_ERR "md/raid1:%s: reshape_position set but not supported\n",
2904709ae487SNeilBrown 		       mdname(mddev));
2905709ae487SNeilBrown 		return -EIO;
2906709ae487SNeilBrown 	}
2907709ae487SNeilBrown 	/*
2908709ae487SNeilBrown 	 * copy the already verified devices into our private RAID1
2909709ae487SNeilBrown 	 * bookkeeping area. [whatever we allocate in run(),
2910709ae487SNeilBrown 	 * should be freed in stop()]
2911709ae487SNeilBrown 	 */
2912709ae487SNeilBrown 	if (mddev->private == NULL)
2913709ae487SNeilBrown 		conf = setup_conf(mddev);
2914709ae487SNeilBrown 	else
2915709ae487SNeilBrown 		conf = mddev->private;
2916709ae487SNeilBrown 
2917709ae487SNeilBrown 	if (IS_ERR(conf))
2918709ae487SNeilBrown 		return PTR_ERR(conf);
2919709ae487SNeilBrown 
2920c8dc9c65SJoe Lawrence 	if (mddev->queue)
29215026d7a9SH. Peter Anvin 		blk_queue_max_write_same_sectors(mddev->queue, 0);
29225026d7a9SH. Peter Anvin 
2923dafb20faSNeilBrown 	rdev_for_each(rdev, mddev) {
29241ed7242eSJonathan Brassow 		if (!mddev->gendisk)
29251ed7242eSJonathan Brassow 			continue;
2926709ae487SNeilBrown 		disk_stack_limits(mddev->gendisk, rdev->bdev,
2927709ae487SNeilBrown 				  rdev->data_offset << 9);
29282ff8cc2cSShaohua Li 		if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
29292ff8cc2cSShaohua Li 			discard_supported = true;
2930709ae487SNeilBrown 	}
2931709ae487SNeilBrown 
2932709ae487SNeilBrown 	mddev->degraded = 0;
2933709ae487SNeilBrown 	for (i=0; i < conf->raid_disks; i++)
2934709ae487SNeilBrown 		if (conf->mirrors[i].rdev == NULL ||
2935709ae487SNeilBrown 		    !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
2936709ae487SNeilBrown 		    test_bit(Faulty, &conf->mirrors[i].rdev->flags))
2937709ae487SNeilBrown 			mddev->degraded++;
2938709ae487SNeilBrown 
2939709ae487SNeilBrown 	if (conf->raid_disks - mddev->degraded == 1)
2940709ae487SNeilBrown 		mddev->recovery_cp = MaxSector;
2941709ae487SNeilBrown 
29428c6ac868SAndre Noll 	if (mddev->recovery_cp != MaxSector)
29439dd1e2faSNeilBrown 		printk(KERN_NOTICE "md/raid1:%s: not clean"
29448c6ac868SAndre Noll 		       " -- starting background reconstruction\n",
29458c6ac868SAndre Noll 		       mdname(mddev));
29461da177e4SLinus Torvalds 	printk(KERN_INFO
29479dd1e2faSNeilBrown 		"md/raid1:%s: active with %d out of %d mirrors\n",
29481da177e4SLinus Torvalds 		mdname(mddev), mddev->raid_disks - mddev->degraded,
29491da177e4SLinus Torvalds 		mddev->raid_disks);
2950709ae487SNeilBrown 
29511da177e4SLinus Torvalds 	/*
29521da177e4SLinus Torvalds 	 * Ok, everything is just fine now
29531da177e4SLinus Torvalds 	 */
2954709ae487SNeilBrown 	mddev->thread = conf->thread;
2955709ae487SNeilBrown 	conf->thread = NULL;
2956709ae487SNeilBrown 	mddev->private = conf;
2957709ae487SNeilBrown 
29581f403624SDan Williams 	md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
29591da177e4SLinus Torvalds 
29601ed7242eSJonathan Brassow 	if (mddev->queue) {
29610d129228SNeilBrown 		mddev->queue->backing_dev_info.congested_fn = raid1_congested;
29620d129228SNeilBrown 		mddev->queue->backing_dev_info.congested_data = mddev;
29636b740b8dSNeilBrown 		blk_queue_merge_bvec(mddev->queue, raid1_mergeable_bvec);
29642ff8cc2cSShaohua Li 
29652ff8cc2cSShaohua Li 		if (discard_supported)
29662ff8cc2cSShaohua Li 			queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
29672ff8cc2cSShaohua Li 						mddev->queue);
29682ff8cc2cSShaohua Li 		else
29692ff8cc2cSShaohua Li 			queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
29702ff8cc2cSShaohua Li 						  mddev->queue);
29711ed7242eSJonathan Brassow 	}
29725220ea1eSmajianpeng 
29735220ea1eSmajianpeng 	ret =  md_integrity_register(mddev);
29745220ea1eSmajianpeng 	if (ret)
29755220ea1eSmajianpeng 		stop(mddev);
29765220ea1eSmajianpeng 	return ret;
29771da177e4SLinus Torvalds }
29781da177e4SLinus Torvalds 
2979fd01b88cSNeilBrown static int stop(struct mddev *mddev)
29801da177e4SLinus Torvalds {
2981e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
29824b6d287fSNeilBrown 	struct bitmap *bitmap = mddev->bitmap;
29834b6d287fSNeilBrown 
29844b6d287fSNeilBrown 	/* wait for behind writes to complete */
2985e555190dSNeilBrown 	if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
29869dd1e2faSNeilBrown 		printk(KERN_INFO "md/raid1:%s: behind writes in progress - waiting to stop.\n",
29879dd1e2faSNeilBrown 		       mdname(mddev));
29884b6d287fSNeilBrown 		/* need to kick something here to make sure I/O goes? */
2989e555190dSNeilBrown 		wait_event(bitmap->behind_wait,
2990e555190dSNeilBrown 			   atomic_read(&bitmap->behind_writes) == 0);
29914b6d287fSNeilBrown 	}
29921da177e4SLinus Torvalds 
299307169fd4Smajianpeng 	freeze_array(conf, 0);
299407169fd4Smajianpeng 	unfreeze_array(conf);
2995409c57f3SNeilBrown 
299601f96c0aSNeilBrown 	md_unregister_thread(&mddev->thread);
29971da177e4SLinus Torvalds 	if (conf->r1bio_pool)
29981da177e4SLinus Torvalds 		mempool_destroy(conf->r1bio_pool);
29991da177e4SLinus Torvalds 	kfree(conf->mirrors);
30000fea7ed8SHirokazu Takahashi 	safe_put_page(conf->tmppage);
30011da177e4SLinus Torvalds 	kfree(conf->poolinfo);
30021da177e4SLinus Torvalds 	kfree(conf);
30031da177e4SLinus Torvalds 	mddev->private = NULL;
30041da177e4SLinus Torvalds 	return 0;
30051da177e4SLinus Torvalds }
30061da177e4SLinus Torvalds 
3007fd01b88cSNeilBrown static int raid1_resize(struct mddev *mddev, sector_t sectors)
30081da177e4SLinus Torvalds {
30091da177e4SLinus Torvalds 	/* no resync is happening, and there is enough space
30101da177e4SLinus Torvalds 	 * on all devices, so we can resize.
30111da177e4SLinus Torvalds 	 * We need to make sure resync covers any new space.
30121da177e4SLinus Torvalds 	 * If the array is shrinking we should possibly wait until
30131da177e4SLinus Torvalds 	 * any io in the removed space completes, but it hardly seems
30141da177e4SLinus Torvalds 	 * worth it.
30151da177e4SLinus Torvalds 	 */
3016a4a6125aSNeilBrown 	sector_t newsize = raid1_size(mddev, sectors, 0);
3017a4a6125aSNeilBrown 	if (mddev->external_size &&
3018a4a6125aSNeilBrown 	    mddev->array_sectors > newsize)
3019b522adcdSDan Williams 		return -EINVAL;
3020a4a6125aSNeilBrown 	if (mddev->bitmap) {
3021a4a6125aSNeilBrown 		int ret = bitmap_resize(mddev->bitmap, newsize, 0, 0);
3022a4a6125aSNeilBrown 		if (ret)
3023a4a6125aSNeilBrown 			return ret;
3024a4a6125aSNeilBrown 	}
3025a4a6125aSNeilBrown 	md_set_array_sectors(mddev, newsize);
3026f233ea5cSAndre Noll 	set_capacity(mddev->gendisk, mddev->array_sectors);
3027449aad3eSNeilBrown 	revalidate_disk(mddev->gendisk);
3028b522adcdSDan Williams 	if (sectors > mddev->dev_sectors &&
3029b098636cSNeilBrown 	    mddev->recovery_cp > mddev->dev_sectors) {
303058c0fed4SAndre Noll 		mddev->recovery_cp = mddev->dev_sectors;
30311da177e4SLinus Torvalds 		set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
30321da177e4SLinus Torvalds 	}
3033b522adcdSDan Williams 	mddev->dev_sectors = sectors;
30344b5c7ae8SNeilBrown 	mddev->resync_max_sectors = sectors;
30351da177e4SLinus Torvalds 	return 0;
30361da177e4SLinus Torvalds }
30371da177e4SLinus Torvalds 
3038fd01b88cSNeilBrown static int raid1_reshape(struct mddev *mddev)
30391da177e4SLinus Torvalds {
30401da177e4SLinus Torvalds 	/* We need to:
30411da177e4SLinus Torvalds 	 * 1/ resize the r1bio_pool
30421da177e4SLinus Torvalds 	 * 2/ resize conf->mirrors
30431da177e4SLinus Torvalds 	 *
30441da177e4SLinus Torvalds 	 * We allocate a new r1bio_pool if we can.
30451da177e4SLinus Torvalds 	 * Then raise a device barrier and wait until all IO stops.
30461da177e4SLinus Torvalds 	 * Then resize conf->mirrors and swap in the new r1bio pool.
30476ea9c07cSNeilBrown 	 *
30486ea9c07cSNeilBrown 	 * At the same time, we "pack" the devices so that all the missing
30496ea9c07cSNeilBrown 	 * devices have the higher raid_disk numbers.
30501da177e4SLinus Torvalds 	 */
30511da177e4SLinus Torvalds 	mempool_t *newpool, *oldpool;
30521da177e4SLinus Torvalds 	struct pool_info *newpoolinfo;
30530eaf822cSJonathan Brassow 	struct raid1_info *newmirrors;
3054e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
305563c70c4fSNeilBrown 	int cnt, raid_disks;
3056c04be0aaSNeilBrown 	unsigned long flags;
3057b5470dc5SDan Williams 	int d, d2, err;
30581da177e4SLinus Torvalds 
305963c70c4fSNeilBrown 	/* Cannot change chunk_size, layout, or level */
3060664e7c41SAndre Noll 	if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
306163c70c4fSNeilBrown 	    mddev->layout != mddev->new_layout ||
306263c70c4fSNeilBrown 	    mddev->level != mddev->new_level) {
3063664e7c41SAndre Noll 		mddev->new_chunk_sectors = mddev->chunk_sectors;
306463c70c4fSNeilBrown 		mddev->new_layout = mddev->layout;
306563c70c4fSNeilBrown 		mddev->new_level = mddev->level;
306663c70c4fSNeilBrown 		return -EINVAL;
306763c70c4fSNeilBrown 	}
306863c70c4fSNeilBrown 
3069b5470dc5SDan Williams 	err = md_allow_write(mddev);
3070b5470dc5SDan Williams 	if (err)
3071b5470dc5SDan Williams 		return err;
30722a2275d6SNeilBrown 
307363c70c4fSNeilBrown 	raid_disks = mddev->raid_disks + mddev->delta_disks;
307463c70c4fSNeilBrown 
30756ea9c07cSNeilBrown 	if (raid_disks < conf->raid_disks) {
30766ea9c07cSNeilBrown 		cnt=0;
30776ea9c07cSNeilBrown 		for (d= 0; d < conf->raid_disks; d++)
30781da177e4SLinus Torvalds 			if (conf->mirrors[d].rdev)
30796ea9c07cSNeilBrown 				cnt++;
30806ea9c07cSNeilBrown 		if (cnt > raid_disks)
30811da177e4SLinus Torvalds 			return -EBUSY;
30826ea9c07cSNeilBrown 	}
30831da177e4SLinus Torvalds 
30841da177e4SLinus Torvalds 	newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
30851da177e4SLinus Torvalds 	if (!newpoolinfo)
30861da177e4SLinus Torvalds 		return -ENOMEM;
30871da177e4SLinus Torvalds 	newpoolinfo->mddev = mddev;
30888f19ccb2SNeilBrown 	newpoolinfo->raid_disks = raid_disks * 2;
30891da177e4SLinus Torvalds 
30901da177e4SLinus Torvalds 	newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
30911da177e4SLinus Torvalds 				 r1bio_pool_free, newpoolinfo);
30921da177e4SLinus Torvalds 	if (!newpool) {
30931da177e4SLinus Torvalds 		kfree(newpoolinfo);
30941da177e4SLinus Torvalds 		return -ENOMEM;
30951da177e4SLinus Torvalds 	}
30960eaf822cSJonathan Brassow 	newmirrors = kzalloc(sizeof(struct raid1_info) * raid_disks * 2,
30978f19ccb2SNeilBrown 			     GFP_KERNEL);
30981da177e4SLinus Torvalds 	if (!newmirrors) {
30991da177e4SLinus Torvalds 		kfree(newpoolinfo);
31001da177e4SLinus Torvalds 		mempool_destroy(newpool);
31011da177e4SLinus Torvalds 		return -ENOMEM;
31021da177e4SLinus Torvalds 	}
31031da177e4SLinus Torvalds 
3104e2d59925SNeilBrown 	freeze_array(conf, 0);
31051da177e4SLinus Torvalds 
31061da177e4SLinus Torvalds 	/* ok, everything is stopped */
31071da177e4SLinus Torvalds 	oldpool = conf->r1bio_pool;
31081da177e4SLinus Torvalds 	conf->r1bio_pool = newpool;
31096ea9c07cSNeilBrown 
3110a88aa786SNeilBrown 	for (d = d2 = 0; d < conf->raid_disks; d++) {
31113cb03002SNeilBrown 		struct md_rdev *rdev = conf->mirrors[d].rdev;
3112a88aa786SNeilBrown 		if (rdev && rdev->raid_disk != d2) {
311336fad858SNamhyung Kim 			sysfs_unlink_rdev(mddev, rdev);
3114a88aa786SNeilBrown 			rdev->raid_disk = d2;
311536fad858SNamhyung Kim 			sysfs_unlink_rdev(mddev, rdev);
311636fad858SNamhyung Kim 			if (sysfs_link_rdev(mddev, rdev))
3117a88aa786SNeilBrown 				printk(KERN_WARNING
311836fad858SNamhyung Kim 				       "md/raid1:%s: cannot register rd%d\n",
311936fad858SNamhyung Kim 				       mdname(mddev), rdev->raid_disk);
3120a88aa786SNeilBrown 		}
3121a88aa786SNeilBrown 		if (rdev)
3122a88aa786SNeilBrown 			newmirrors[d2++].rdev = rdev;
31236ea9c07cSNeilBrown 	}
31241da177e4SLinus Torvalds 	kfree(conf->mirrors);
31251da177e4SLinus Torvalds 	conf->mirrors = newmirrors;
31261da177e4SLinus Torvalds 	kfree(conf->poolinfo);
31271da177e4SLinus Torvalds 	conf->poolinfo = newpoolinfo;
31281da177e4SLinus Torvalds 
3129c04be0aaSNeilBrown 	spin_lock_irqsave(&conf->device_lock, flags);
31301da177e4SLinus Torvalds 	mddev->degraded += (raid_disks - conf->raid_disks);
3131c04be0aaSNeilBrown 	spin_unlock_irqrestore(&conf->device_lock, flags);
31321da177e4SLinus Torvalds 	conf->raid_disks = mddev->raid_disks = raid_disks;
313363c70c4fSNeilBrown 	mddev->delta_disks = 0;
31341da177e4SLinus Torvalds 
3135e2d59925SNeilBrown 	unfreeze_array(conf);
31361da177e4SLinus Torvalds 
31371da177e4SLinus Torvalds 	set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
31381da177e4SLinus Torvalds 	md_wakeup_thread(mddev->thread);
31391da177e4SLinus Torvalds 
31401da177e4SLinus Torvalds 	mempool_destroy(oldpool);
31411da177e4SLinus Torvalds 	return 0;
31421da177e4SLinus Torvalds }
31431da177e4SLinus Torvalds 
3144fd01b88cSNeilBrown static void raid1_quiesce(struct mddev *mddev, int state)
314536fa3063SNeilBrown {
3146e8096360SNeilBrown 	struct r1conf *conf = mddev->private;
314736fa3063SNeilBrown 
314836fa3063SNeilBrown 	switch(state) {
31496eef4b21SNeilBrown 	case 2: /* wake for suspend */
31506eef4b21SNeilBrown 		wake_up(&conf->wait_barrier);
31516eef4b21SNeilBrown 		break;
31529e6603daSNeilBrown 	case 1:
315307169fd4Smajianpeng 		freeze_array(conf, 0);
315436fa3063SNeilBrown 		break;
31559e6603daSNeilBrown 	case 0:
315607169fd4Smajianpeng 		unfreeze_array(conf);
315736fa3063SNeilBrown 		break;
315836fa3063SNeilBrown 	}
315936fa3063SNeilBrown }
316036fa3063SNeilBrown 
3161fd01b88cSNeilBrown static void *raid1_takeover(struct mddev *mddev)
3162709ae487SNeilBrown {
3163709ae487SNeilBrown 	/* raid1 can take over:
3164709ae487SNeilBrown 	 *  raid5 with 2 devices, any layout or chunk size
3165709ae487SNeilBrown 	 */
3166709ae487SNeilBrown 	if (mddev->level == 5 && mddev->raid_disks == 2) {
3167e8096360SNeilBrown 		struct r1conf *conf;
3168709ae487SNeilBrown 		mddev->new_level = 1;
3169709ae487SNeilBrown 		mddev->new_layout = 0;
3170709ae487SNeilBrown 		mddev->new_chunk_sectors = 0;
3171709ae487SNeilBrown 		conf = setup_conf(mddev);
3172709ae487SNeilBrown 		if (!IS_ERR(conf))
317307169fd4Smajianpeng 			/* Array must appear to be quiesced */
317407169fd4Smajianpeng 			conf->array_frozen = 1;
3175709ae487SNeilBrown 		return conf;
3176709ae487SNeilBrown 	}
3177709ae487SNeilBrown 	return ERR_PTR(-EINVAL);
3178709ae487SNeilBrown }
31791da177e4SLinus Torvalds 
318084fc4b56SNeilBrown static struct md_personality raid1_personality =
31811da177e4SLinus Torvalds {
31821da177e4SLinus Torvalds 	.name		= "raid1",
31832604b703SNeilBrown 	.level		= 1,
31841da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
31851da177e4SLinus Torvalds 	.make_request	= make_request,
31861da177e4SLinus Torvalds 	.run		= run,
31871da177e4SLinus Torvalds 	.stop		= stop,
31881da177e4SLinus Torvalds 	.status		= status,
31891da177e4SLinus Torvalds 	.error_handler	= error,
31901da177e4SLinus Torvalds 	.hot_add_disk	= raid1_add_disk,
31911da177e4SLinus Torvalds 	.hot_remove_disk= raid1_remove_disk,
31921da177e4SLinus Torvalds 	.spare_active	= raid1_spare_active,
31931da177e4SLinus Torvalds 	.sync_request	= sync_request,
31941da177e4SLinus Torvalds 	.resize		= raid1_resize,
319580c3a6ceSDan Williams 	.size		= raid1_size,
319663c70c4fSNeilBrown 	.check_reshape	= raid1_reshape,
319736fa3063SNeilBrown 	.quiesce	= raid1_quiesce,
3198709ae487SNeilBrown 	.takeover	= raid1_takeover,
31991da177e4SLinus Torvalds };
32001da177e4SLinus Torvalds 
32011da177e4SLinus Torvalds static int __init raid_init(void)
32021da177e4SLinus Torvalds {
32032604b703SNeilBrown 	return register_md_personality(&raid1_personality);
32041da177e4SLinus Torvalds }
32051da177e4SLinus Torvalds 
32061da177e4SLinus Torvalds static void raid_exit(void)
32071da177e4SLinus Torvalds {
32082604b703SNeilBrown 	unregister_md_personality(&raid1_personality);
32091da177e4SLinus Torvalds }
32101da177e4SLinus Torvalds 
32111da177e4SLinus Torvalds module_init(raid_init);
32121da177e4SLinus Torvalds module_exit(raid_exit);
32131da177e4SLinus Torvalds MODULE_LICENSE("GPL");
32140efb9e61SNeilBrown MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
32151da177e4SLinus Torvalds MODULE_ALIAS("md-personality-3"); /* RAID1 */
3216d9d166c2SNeilBrown MODULE_ALIAS("md-raid1");
32172604b703SNeilBrown MODULE_ALIAS("md-level-1");
321834db0cd6SNeilBrown 
321934db0cd6SNeilBrown module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);
3220