blob: 14422407e52018b82daaf21d9a91aa7f7b3cc7b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid1.c : Multiple Devices driver for Linux
3 *
4 * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
5 *
6 * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
7 *
8 * RAID-1 management functions.
9 *
10 * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
11 *
Jan Engelhardt96de0e22007-10-19 23:21:04 +020012 * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
14 *
NeilBrown191ea9b2005-06-21 17:17:23 -070015 * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support
16 * bitmapped intelligence in resync:
17 *
18 * - bitmap marked during normal i/o
19 * - bitmap used to skip nondirty blocks during sync
20 *
21 * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
22 * - persistent bitmap code
23 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2, or (at your option)
27 * any later version.
28 *
29 * You should have received a copy of the GNU General Public License
30 * (for example /usr/src/linux/COPYING); if not, write to the Free
31 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 */
33
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110035#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040037#include <linux/module.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100039#include <linux/ratelimit.h>
NeilBrown109e3762016-11-18 13:22:04 +110040#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110041#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110042#include "raid1.h"
43#include "bitmap.h"
NeilBrown191ea9b2005-06-21 17:17:23 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * Number of guaranteed r1bios in case of extreme VM load:
47 */
48#define NR_RAID1_BIOS 256
49
Jonathan Brassow473e87c2012-07-31 10:03:52 +100050/* when we get a read error on a read-only array, we redirect to another
51 * device without failing the first device, or trying to over-write to
52 * correct the read error. To keep track of bad blocks on a per-bio
53 * level, we store IO_BLOCKED in the appropriate 'bios' pointer
54 */
55#define IO_BLOCKED ((struct bio *)1)
56/* When we successfully write to a known bad-block, we need to remove the
57 * bad-block marking which must be done from process context. So we record
58 * the success by setting devs[n].bio to IO_MADE_GOOD
59 */
60#define IO_MADE_GOOD ((struct bio *)2)
61
62#define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
63
NeilBrown34db0cd2011-10-11 16:50:01 +110064/* When there are this many requests queue to be written by
65 * the raid1 thread, we become 'congested' to provide back-pressure
66 * for writeback.
67 */
68static int max_queued_requests = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
majianpeng79ef3a82013-11-15 14:55:02 +080070static void allow_barrier(struct r1conf *conf, sector_t start_next_window,
71 sector_t bi_sector);
NeilBrowne8096362011-10-11 16:49:05 +110072static void lower_barrier(struct r1conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
NeilBrown578b54a2016-11-14 16:30:21 +110074#define raid1_log(md, fmt, args...) \
75 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid1 " fmt, ##args); } while (0)
76
Al Virodd0fc662005-10-07 07:46:04 +010077static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 struct pool_info *pi = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +110080 int size = offsetof(struct r1bio, bios[pi->raid_disks]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 /* allocate a r1bio with room for raid_disks entries in the bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +010083 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86static void r1bio_pool_free(void *r1_bio, void *data)
87{
88 kfree(r1_bio);
89}
90
91#define RESYNC_BLOCK_SIZE (64*1024)
majianpeng8e005f72013-11-14 15:16:18 +110092#define RESYNC_DEPTH 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
94#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
majianpeng8e005f72013-11-14 15:16:18 +110095#define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH)
96#define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9)
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +100097#define CLUSTER_RESYNC_WINDOW (16 * RESYNC_WINDOW)
98#define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
majianpeng8e005f72013-11-14 15:16:18 +110099#define NEXT_NORMALIO_DISTANCE (3 * RESYNC_WINDOW_SECTORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Al Virodd0fc662005-10-07 07:46:04 +0100101static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct pool_info *pi = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100104 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 struct bio *bio;
NeilBrownda1aab32014-04-09 12:25:43 +1000106 int need_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int i, j;
108
109 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100110 if (!r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 /*
114 * Allocate bios : 1 for reading, n-1 for writing
115 */
116 for (j = pi->raid_disks ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100117 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (!bio)
119 goto out_free_bio;
120 r1_bio->bios[j] = bio;
121 }
122 /*
123 * Allocate RESYNC_PAGES data pages and attach them to
NeilBrownd11c1712006-01-06 00:20:26 -0800124 * the first bio.
125 * If this is a user-requested check/repair, allocate
126 * RESYNC_PAGES for each bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 */
NeilBrownd11c1712006-01-06 00:20:26 -0800128 if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
NeilBrownda1aab32014-04-09 12:25:43 +1000129 need_pages = pi->raid_disks;
NeilBrownd11c1712006-01-06 00:20:26 -0800130 else
NeilBrownda1aab32014-04-09 12:25:43 +1000131 need_pages = 1;
132 for (j = 0; j < need_pages; j++) {
NeilBrownd11c1712006-01-06 00:20:26 -0800133 bio = r1_bio->bios[j];
Kent Overstreeta0787602012-09-10 14:03:28 -0700134 bio->bi_vcnt = RESYNC_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Kent Overstreeta0787602012-09-10 14:03:28 -0700136 if (bio_alloc_pages(bio, gfp_flags))
NeilBrownda1aab32014-04-09 12:25:43 +1000137 goto out_free_pages;
NeilBrownd11c1712006-01-06 00:20:26 -0800138 }
139 /* If not user-requests, copy the page pointers to all bios */
140 if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) {
141 for (i=0; i<RESYNC_PAGES ; i++)
142 for (j=1; j<pi->raid_disks; j++)
143 r1_bio->bios[j]->bi_io_vec[i].bv_page =
144 r1_bio->bios[0]->bi_io_vec[i].bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
147 r1_bio->master_bio = NULL;
148
149 return r1_bio;
150
NeilBrownda1aab32014-04-09 12:25:43 +1000151out_free_pages:
Guoqing Jiang491221f2016-09-22 03:10:01 -0400152 while (--j >= 0)
153 bio_free_pages(r1_bio->bios[j]);
NeilBrownda1aab32014-04-09 12:25:43 +1000154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155out_free_bio:
NeilBrown8f19ccb2011-12-23 10:17:56 +1100156 while (++j < pi->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 bio_put(r1_bio->bios[j]);
158 r1bio_pool_free(r1_bio, data);
159 return NULL;
160}
161
162static void r1buf_pool_free(void *__r1_bio, void *data)
163{
164 struct pool_info *pi = data;
NeilBrownd11c1712006-01-06 00:20:26 -0800165 int i,j;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100166 struct r1bio *r1bio = __r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
NeilBrownd11c1712006-01-06 00:20:26 -0800168 for (i = 0; i < RESYNC_PAGES; i++)
169 for (j = pi->raid_disks; j-- ;) {
170 if (j == 0 ||
171 r1bio->bios[j]->bi_io_vec[i].bv_page !=
172 r1bio->bios[0]->bi_io_vec[i].bv_page)
NeilBrown1345b1d2006-01-06 00:20:40 -0800173 safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page);
NeilBrownd11c1712006-01-06 00:20:26 -0800174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 for (i=0 ; i < pi->raid_disks; i++)
176 bio_put(r1bio->bios[i]);
177
178 r1bio_pool_free(r1bio, data);
179}
180
NeilBrowne8096362011-10-11 16:49:05 +1100181static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 int i;
184
NeilBrown8f19ccb2011-12-23 10:17:56 +1100185 for (i = 0; i < conf->raid_disks * 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct bio **bio = r1_bio->bios + i;
NeilBrown4367af52011-07-28 11:31:49 +1000187 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 bio_put(*bio);
189 *bio = NULL;
190 }
191}
192
NeilBrown9f2c9d12011-10-11 16:48:43 +1100193static void free_r1bio(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
NeilBrowne8096362011-10-11 16:49:05 +1100195 struct r1conf *conf = r1_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 put_all_bios(conf, r1_bio);
198 mempool_free(r1_bio, conf->r1bio_pool);
199}
200
NeilBrown9f2c9d12011-10-11 16:48:43 +1100201static void put_buf(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
NeilBrowne8096362011-10-11 16:49:05 +1100203 struct r1conf *conf = r1_bio->mddev->private;
NeilBrown3e198f72006-01-06 00:20:21 -0800204 int i;
205
NeilBrown8f19ccb2011-12-23 10:17:56 +1100206 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown3e198f72006-01-06 00:20:21 -0800207 struct bio *bio = r1_bio->bios[i];
208 if (bio->bi_end_io)
209 rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 mempool_free(r1_bio, conf->r1buf_pool);
213
NeilBrown17999be2006-01-06 00:20:12 -0800214 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
NeilBrown9f2c9d12011-10-11 16:48:43 +1100217static void reschedule_retry(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100220 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +1100221 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 spin_lock_irqsave(&conf->device_lock, flags);
224 list_add(&r1_bio->retry_list, &conf->retry_list);
NeilBrownddaf22a2006-01-06 00:20:19 -0800225 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 spin_unlock_irqrestore(&conf->device_lock, flags);
227
NeilBrown17999be2006-01-06 00:20:12 -0800228 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 md_wakeup_thread(mddev->thread);
230}
231
232/*
233 * raid_end_bio_io() is called when we have finished servicing a mirrored
234 * operation and are ready to return a success/failure code to the buffer
235 * cache layer.
236 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100237static void call_bio_endio(struct r1bio *r1_bio)
NeilBrownd2eb35a2011-07-28 11:31:48 +1000238{
239 struct bio *bio = r1_bio->master_bio;
240 int done;
NeilBrowne8096362011-10-11 16:49:05 +1100241 struct r1conf *conf = r1_bio->mddev->private;
majianpeng79ef3a82013-11-15 14:55:02 +0800242 sector_t start_next_window = r1_bio->start_next_window;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700243 sector_t bi_sector = bio->bi_iter.bi_sector;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000244
245 if (bio->bi_phys_segments) {
246 unsigned long flags;
247 spin_lock_irqsave(&conf->device_lock, flags);
248 bio->bi_phys_segments--;
249 done = (bio->bi_phys_segments == 0);
250 spin_unlock_irqrestore(&conf->device_lock, flags);
majianpeng79ef3a82013-11-15 14:55:02 +0800251 /*
252 * make_request() might be waiting for
253 * bi_phys_segments to decrease
254 */
255 wake_up(&conf->wait_barrier);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000256 } else
257 done = 1;
258
259 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200260 bio->bi_error = -EIO;
261
NeilBrownd2eb35a2011-07-28 11:31:48 +1000262 if (done) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200263 bio_endio(bio);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000264 /*
265 * Wake up any possible resync thread that waits for the device
266 * to go idle.
267 */
majianpeng79ef3a82013-11-15 14:55:02 +0800268 allow_barrier(conf, start_next_window, bi_sector);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000269 }
270}
271
NeilBrown9f2c9d12011-10-11 16:48:43 +1100272static void raid_end_bio_io(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct bio *bio = r1_bio->master_bio;
275
NeilBrown4b6d2872005-09-09 16:23:47 -0700276 /* if nobody has done the final endio yet, do it now */
277 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
NeilBrown36a4e1f2011-10-07 14:23:17 +1100278 pr_debug("raid1: sync end %s on sectors %llu-%llu\n",
279 (bio_data_dir(bio) == WRITE) ? "write" : "read",
Kent Overstreet4f024f32013-10-11 15:44:27 -0700280 (unsigned long long) bio->bi_iter.bi_sector,
281 (unsigned long long) bio_end_sector(bio) - 1);
NeilBrown4b6d2872005-09-09 16:23:47 -0700282
NeilBrownd2eb35a2011-07-28 11:31:48 +1000283 call_bio_endio(r1_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -0700284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 free_r1bio(r1_bio);
286}
287
288/*
289 * Update disk head position estimator based on IRQ completion info.
290 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100291static inline void update_head_pos(int disk, struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
NeilBrowne8096362011-10-11 16:49:05 +1100293 struct r1conf *conf = r1_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 conf->mirrors[disk].head_position =
296 r1_bio->sector + (r1_bio->sectors);
297}
298
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100299/*
300 * Find the disk number which triggered given bio
301 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100302static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio)
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100303{
304 int mirror;
NeilBrown30194632011-12-23 10:17:56 +1100305 struct r1conf *conf = r1_bio->mddev->private;
306 int raid_disks = conf->raid_disks;
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100307
NeilBrown8f19ccb2011-12-23 10:17:56 +1100308 for (mirror = 0; mirror < raid_disks * 2; mirror++)
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100309 if (r1_bio->bios[mirror] == bio)
310 break;
311
NeilBrown8f19ccb2011-12-23 10:17:56 +1100312 BUG_ON(mirror == raid_disks * 2);
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100313 update_head_pos(mirror, r1_bio);
314
315 return mirror;
316}
317
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200318static void raid1_end_read_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200320 int uptodate = !bio->bi_error;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100321 struct r1bio *r1_bio = bio->bi_private;
NeilBrowne8096362011-10-11 16:49:05 +1100322 struct r1conf *conf = r1_bio->mddev->private;
NeilBrowne5872d52016-06-02 16:19:52 +1000323 struct md_rdev *rdev = conf->mirrors[r1_bio->read_disk].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /*
326 * this branch is our 'one mirror IO has finished' event handler:
327 */
NeilBrowne5872d52016-06-02 16:19:52 +1000328 update_head_pos(r1_bio->read_disk, r1_bio);
NeilBrownddaf22a2006-01-06 00:20:19 -0800329
NeilBrowndd00a992007-05-10 03:15:50 -0700330 if (uptodate)
331 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrown2e52d442016-11-18 16:16:12 +1100332 else if (test_bit(FailFast, &rdev->flags) &&
333 test_bit(R1BIO_FailFast, &r1_bio->state))
334 /* This was a fail-fast read so we definitely
335 * want to retry */
336 ;
NeilBrowndd00a992007-05-10 03:15:50 -0700337 else {
338 /* If all other devices have failed, we want to return
339 * the error upwards rather than fail the last device.
340 * Here we redefine "uptodate" to mean "Don't want to retry"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 */
NeilBrowndd00a992007-05-10 03:15:50 -0700342 unsigned long flags;
343 spin_lock_irqsave(&conf->device_lock, flags);
344 if (r1_bio->mddev->degraded == conf->raid_disks ||
345 (r1_bio->mddev->degraded == conf->raid_disks-1 &&
NeilBrowne5872d52016-06-02 16:19:52 +1000346 test_bit(In_sync, &rdev->flags)))
NeilBrowndd00a992007-05-10 03:15:50 -0700347 uptodate = 1;
348 spin_unlock_irqrestore(&conf->device_lock, flags);
349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
NeilBrown7ad4d4a2012-10-11 13:44:30 +1100351 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 raid_end_bio_io(r1_bio);
NeilBrowne5872d52016-06-02 16:19:52 +1000353 rdev_dec_pending(rdev, conf->mddev);
NeilBrown7ad4d4a2012-10-11 13:44:30 +1100354 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /*
356 * oops, read error:
357 */
358 char b[BDEVNAME_SIZE];
NeilBrown1d41c212016-11-02 14:16:50 +1100359 pr_err_ratelimited("md/raid1:%s: %s: rescheduling sector %llu\n",
360 mdname(conf->mddev),
361 bdevname(rdev->bdev, b),
362 (unsigned long long)r1_bio->sector);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000363 set_bit(R1BIO_ReadError, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 reschedule_retry(r1_bio);
NeilBrown7ad4d4a2012-10-11 13:44:30 +1100365 /* don't drop the reference on read_disk yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
NeilBrown9f2c9d12011-10-11 16:48:43 +1100369static void close_write(struct r1bio *r1_bio)
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000370{
371 /* it really is the end of this request */
372 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
373 /* free extra copy of the data pages */
374 int i = r1_bio->behind_page_count;
375 while (i--)
376 safe_put_page(r1_bio->behind_bvecs[i].bv_page);
377 kfree(r1_bio->behind_bvecs);
378 r1_bio->behind_bvecs = NULL;
379 }
380 /* clear the bitmap if all writes complete successfully */
381 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
382 r1_bio->sectors,
383 !test_bit(R1BIO_Degraded, &r1_bio->state),
384 test_bit(R1BIO_BehindIO, &r1_bio->state));
385 md_write_end(r1_bio->mddev);
386}
387
NeilBrown9f2c9d12011-10-11 16:48:43 +1100388static void r1_bio_write_done(struct r1bio *r1_bio)
NeilBrown4e780642010-10-19 12:54:01 +1100389{
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000390 if (!atomic_dec_and_test(&r1_bio->remaining))
391 return;
392
393 if (test_bit(R1BIO_WriteError, &r1_bio->state))
394 reschedule_retry(r1_bio);
395 else {
396 close_write(r1_bio);
NeilBrown4367af52011-07-28 11:31:49 +1000397 if (test_bit(R1BIO_MadeGood, &r1_bio->state))
398 reschedule_retry(r1_bio);
399 else
400 raid_end_bio_io(r1_bio);
NeilBrown4e780642010-10-19 12:54:01 +1100401 }
402}
403
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200404static void raid1_end_write_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
NeilBrown9f2c9d12011-10-11 16:48:43 +1100406 struct r1bio *r1_bio = bio->bi_private;
NeilBrowne5872d52016-06-02 16:19:52 +1000407 int behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
NeilBrowne8096362011-10-11 16:49:05 +1100408 struct r1conf *conf = r1_bio->mddev->private;
NeilBrown04b857f2006-03-09 17:33:46 -0800409 struct bio *to_put = NULL;
NeilBrowne5872d52016-06-02 16:19:52 +1000410 int mirror = find_bio_disk(r1_bio, bio);
411 struct md_rdev *rdev = conf->mirrors[mirror].rdev;
Shaohua Lie3f948c2016-10-06 14:09:16 -0700412 bool discard_error;
413
414 discard_error = bio->bi_error && bio_op(bio) == REQ_OP_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Tejun Heoe9c74692010-09-03 11:56:18 +0200416 /*
417 * 'one mirror IO has finished' event handler:
418 */
Shaohua Lie3f948c2016-10-06 14:09:16 -0700419 if (bio->bi_error && !discard_error) {
NeilBrowne5872d52016-06-02 16:19:52 +1000420 set_bit(WriteErrorSeen, &rdev->flags);
421 if (!test_and_set_bit(WantReplacement, &rdev->flags))
NeilBrown19d67162011-12-23 10:17:57 +1100422 set_bit(MD_RECOVERY_NEEDED, &
423 conf->mddev->recovery);
424
NeilBrown212e7eb2016-11-18 16:16:12 +1100425 if (test_bit(FailFast, &rdev->flags) &&
426 (bio->bi_opf & MD_FAILFAST) &&
427 /* We never try FailFast to WriteMostly devices */
428 !test_bit(WriteMostly, &rdev->flags)) {
429 md_error(r1_bio->mddev, rdev);
430 if (!test_bit(Faulty, &rdev->flags))
431 /* This is the only remaining device,
432 * We need to retry the write without
433 * FailFast
434 */
435 set_bit(R1BIO_WriteError, &r1_bio->state);
436 else {
437 /* Finished with this branch */
438 r1_bio->bios[mirror] = NULL;
439 to_put = bio;
440 }
441 } else
442 set_bit(R1BIO_WriteError, &r1_bio->state);
NeilBrown4367af52011-07-28 11:31:49 +1000443 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /*
Tejun Heoe9c74692010-09-03 11:56:18 +0200445 * Set R1BIO_Uptodate in our master bio, so that we
446 * will return a good error code for to the higher
447 * levels even if IO on some other mirrored buffer
448 * fails.
449 *
450 * The 'master' represents the composite IO operation
451 * to user-side. So if something waits for IO, then it
452 * will wait for the 'master' bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 */
NeilBrown4367af52011-07-28 11:31:49 +1000454 sector_t first_bad;
455 int bad_sectors;
456
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000457 r1_bio->bios[mirror] = NULL;
458 to_put = bio;
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300459 /*
460 * Do not set R1BIO_Uptodate if the current device is
461 * rebuilding or Faulty. This is because we cannot use
462 * such device for properly reading the data back (we could
463 * potentially use it, if the current write would have felt
464 * before rdev->recovery_offset, but for simplicity we don't
465 * check this here.
466 */
NeilBrowne5872d52016-06-02 16:19:52 +1000467 if (test_bit(In_sync, &rdev->flags) &&
468 !test_bit(Faulty, &rdev->flags))
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300469 set_bit(R1BIO_Uptodate, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
NeilBrown4367af52011-07-28 11:31:49 +1000471 /* Maybe we can clear some bad blocks. */
NeilBrowne5872d52016-06-02 16:19:52 +1000472 if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
Shaohua Lie3f948c2016-10-06 14:09:16 -0700473 &first_bad, &bad_sectors) && !discard_error) {
NeilBrown4367af52011-07-28 11:31:49 +1000474 r1_bio->bios[mirror] = IO_MADE_GOOD;
475 set_bit(R1BIO_MadeGood, &r1_bio->state);
476 }
477 }
478
Tejun Heoe9c74692010-09-03 11:56:18 +0200479 if (behind) {
NeilBrowne5872d52016-06-02 16:19:52 +1000480 if (test_bit(WriteMostly, &rdev->flags))
Tejun Heoe9c74692010-09-03 11:56:18 +0200481 atomic_dec(&r1_bio->behind_remaining);
NeilBrown4b6d2872005-09-09 16:23:47 -0700482
Tejun Heoe9c74692010-09-03 11:56:18 +0200483 /*
484 * In behind mode, we ACK the master bio once the I/O
485 * has safely reached all non-writemostly
486 * disks. Setting the Returned bit ensures that this
487 * gets done only once -- we don't ever want to return
488 * -EIO here, instead we'll wait
489 */
490 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
491 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
492 /* Maybe we can return now */
493 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
494 struct bio *mbio = r1_bio->master_bio;
NeilBrown36a4e1f2011-10-07 14:23:17 +1100495 pr_debug("raid1: behind end write sectors"
496 " %llu-%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -0700497 (unsigned long long) mbio->bi_iter.bi_sector,
498 (unsigned long long) bio_end_sector(mbio) - 1);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000499 call_bio_endio(r1_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -0700500 }
501 }
502 }
NeilBrown4367af52011-07-28 11:31:49 +1000503 if (r1_bio->bios[mirror] == NULL)
NeilBrowne5872d52016-06-02 16:19:52 +1000504 rdev_dec_pending(rdev, conf->mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * Let's see if all mirrored write operations have finished
508 * already.
509 */
NeilBrownaf6d7b72011-05-11 14:51:19 +1000510 r1_bio_write_done(r1_bio);
NeilBrownc70810b2006-06-26 00:27:35 -0700511
NeilBrown04b857f2006-03-09 17:33:46 -0800512 if (to_put)
513 bio_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516/*
517 * This routine returns the disk from which the requested read should
518 * be done. There is a per-array 'next expected sequential IO' sector
519 * number - if this matches on the next IO then we use the last disk.
520 * There is also a per-disk 'last know head position' sector that is
521 * maintained from IRQ contexts, both the normal and the resync IO
522 * completion handlers update this position correctly. If there is no
523 * perfect sequential match then we pick the disk whose head is closest.
524 *
525 * If there are 2 mirrors in the same 2 devices, performance degrades
526 * because position is mirror, not device based.
527 *
528 * The rdev for the device selected will have nr_pending incremented.
529 */
NeilBrowne8096362011-10-11 16:49:05 +1100530static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000532 const sector_t this_sector = r1_bio->sector;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000533 int sectors;
534 int best_good_sectors;
Shaohua Li9dedf602012-07-31 10:03:53 +1000535 int best_disk, best_dist_disk, best_pending_disk;
536 int has_nonrot_disk;
Shaohua Libe4d3282012-07-31 10:03:53 +1000537 int disk;
NeilBrown76073052011-05-11 14:34:56 +1000538 sector_t best_dist;
Shaohua Li9dedf602012-07-31 10:03:53 +1000539 unsigned int min_pending;
NeilBrown3cb03002011-10-11 16:45:26 +1100540 struct md_rdev *rdev;
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000541 int choose_first;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000542 int choose_next_idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 rcu_read_lock();
545 /*
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700546 * Check if we can balance. We can balance on the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 * device if no resync is going on, or below the resync window.
548 * We take the first readable disk when above the resync window.
549 */
550 retry:
NeilBrownd2eb35a2011-07-28 11:31:48 +1000551 sectors = r1_bio->sectors;
NeilBrown76073052011-05-11 14:34:56 +1000552 best_disk = -1;
Shaohua Li9dedf602012-07-31 10:03:53 +1000553 best_dist_disk = -1;
NeilBrown76073052011-05-11 14:34:56 +1000554 best_dist = MaxSector;
Shaohua Li9dedf602012-07-31 10:03:53 +1000555 best_pending_disk = -1;
556 min_pending = UINT_MAX;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000557 best_good_sectors = 0;
Shaohua Li9dedf602012-07-31 10:03:53 +1000558 has_nonrot_disk = 0;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000559 choose_next_idle = 0;
NeilBrown2e52d442016-11-18 16:16:12 +1100560 clear_bit(R1BIO_FailFast, &r1_bio->state);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000561
Goldwyn Rodrigues7d49ffc2014-08-12 10:13:19 -0500562 if ((conf->mddev->recovery_cp < this_sector + sectors) ||
563 (mddev_is_clustered(conf->mddev) &&
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500564 md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
Goldwyn Rodrigues7d49ffc2014-08-12 10:13:19 -0500565 this_sector + sectors)))
566 choose_first = 1;
567 else
568 choose_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Shaohua Libe4d3282012-07-31 10:03:53 +1000570 for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
NeilBrown76073052011-05-11 14:34:56 +1000571 sector_t dist;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000572 sector_t first_bad;
573 int bad_sectors;
Shaohua Li9dedf602012-07-31 10:03:53 +1000574 unsigned int pending;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000575 bool nonrot;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000576
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000577 rdev = rcu_dereference(conf->mirrors[disk].rdev);
578 if (r1_bio->bios[disk] == IO_BLOCKED
579 || rdev == NULL
NeilBrown76073052011-05-11 14:34:56 +1000580 || test_bit(Faulty, &rdev->flags))
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000581 continue;
NeilBrown76073052011-05-11 14:34:56 +1000582 if (!test_bit(In_sync, &rdev->flags) &&
583 rdev->recovery_offset < this_sector + sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 continue;
NeilBrown76073052011-05-11 14:34:56 +1000585 if (test_bit(WriteMostly, &rdev->flags)) {
586 /* Don't balance among write-mostly, just
587 * use the first as a last resort */
Tomáš Hodekd1901ef2015-02-23 11:00:38 +1100588 if (best_dist_disk < 0) {
NeilBrown307729c2012-01-09 01:41:51 +1100589 if (is_badblock(rdev, this_sector, sectors,
590 &first_bad, &bad_sectors)) {
Wei Fang816b0ac2016-03-21 19:18:32 +0800591 if (first_bad <= this_sector)
NeilBrown307729c2012-01-09 01:41:51 +1100592 /* Cannot use this */
593 continue;
594 best_good_sectors = first_bad - this_sector;
595 } else
596 best_good_sectors = sectors;
Tomáš Hodekd1901ef2015-02-23 11:00:38 +1100597 best_dist_disk = disk;
598 best_pending_disk = disk;
NeilBrown307729c2012-01-09 01:41:51 +1100599 }
NeilBrown76073052011-05-11 14:34:56 +1000600 continue;
601 }
602 /* This is a reasonable device to use. It might
603 * even be best.
604 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000605 if (is_badblock(rdev, this_sector, sectors,
606 &first_bad, &bad_sectors)) {
607 if (best_dist < MaxSector)
608 /* already have a better device */
609 continue;
610 if (first_bad <= this_sector) {
611 /* cannot read here. If this is the 'primary'
612 * device, then we must not read beyond
613 * bad_sectors from another device..
614 */
615 bad_sectors -= (this_sector - first_bad);
616 if (choose_first && sectors > bad_sectors)
617 sectors = bad_sectors;
618 if (best_good_sectors > sectors)
619 best_good_sectors = sectors;
620
621 } else {
622 sector_t good_sectors = first_bad - this_sector;
623 if (good_sectors > best_good_sectors) {
624 best_good_sectors = good_sectors;
625 best_disk = disk;
626 }
627 if (choose_first)
628 break;
629 }
630 continue;
631 } else
632 best_good_sectors = sectors;
633
NeilBrown2e52d442016-11-18 16:16:12 +1100634 if (best_disk >= 0)
635 /* At least two disks to choose from so failfast is OK */
636 set_bit(R1BIO_FailFast, &r1_bio->state);
637
Shaohua Li12cee5a2012-07-31 10:03:53 +1000638 nonrot = blk_queue_nonrot(bdev_get_queue(rdev->bdev));
639 has_nonrot_disk |= nonrot;
Shaohua Li9dedf602012-07-31 10:03:53 +1000640 pending = atomic_read(&rdev->nr_pending);
NeilBrown76073052011-05-11 14:34:56 +1000641 dist = abs(this_sector - conf->mirrors[disk].head_position);
Shaohua Li12cee5a2012-07-31 10:03:53 +1000642 if (choose_first) {
NeilBrown76073052011-05-11 14:34:56 +1000643 best_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 break;
645 }
Shaohua Li12cee5a2012-07-31 10:03:53 +1000646 /* Don't change to another disk for sequential reads */
647 if (conf->mirrors[disk].next_seq_sect == this_sector
648 || dist == 0) {
649 int opt_iosize = bdev_io_opt(rdev->bdev) >> 9;
650 struct raid1_info *mirror = &conf->mirrors[disk];
651
652 best_disk = disk;
653 /*
654 * If buffered sequential IO size exceeds optimal
655 * iosize, check if there is idle disk. If yes, choose
656 * the idle disk. read_balance could already choose an
657 * idle disk before noticing it's a sequential IO in
658 * this disk. This doesn't matter because this disk
659 * will idle, next time it will be utilized after the
660 * first disk has IO size exceeds optimal iosize. In
661 * this way, iosize of the first disk will be optimal
662 * iosize at least. iosize of the second disk might be
663 * small, but not a big deal since when the second disk
664 * starts IO, the first disk is likely still busy.
665 */
666 if (nonrot && opt_iosize > 0 &&
667 mirror->seq_start != MaxSector &&
668 mirror->next_seq_sect > opt_iosize &&
669 mirror->next_seq_sect - opt_iosize >=
670 mirror->seq_start) {
671 choose_next_idle = 1;
672 continue;
673 }
674 break;
675 }
Shaohua Li12cee5a2012-07-31 10:03:53 +1000676
677 if (choose_next_idle)
678 continue;
Shaohua Li9dedf602012-07-31 10:03:53 +1000679
680 if (min_pending > pending) {
681 min_pending = pending;
682 best_pending_disk = disk;
683 }
684
NeilBrown76073052011-05-11 14:34:56 +1000685 if (dist < best_dist) {
686 best_dist = dist;
Shaohua Li9dedf602012-07-31 10:03:53 +1000687 best_dist_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Shaohua Li9dedf602012-07-31 10:03:53 +1000691 /*
692 * If all disks are rotational, choose the closest disk. If any disk is
693 * non-rotational, choose the disk with less pending request even the
694 * disk is rotational, which might/might not be optimal for raids with
695 * mixed ratation/non-rotational disks depending on workload.
696 */
697 if (best_disk == -1) {
NeilBrown2e52d442016-11-18 16:16:12 +1100698 if (has_nonrot_disk || min_pending == 0)
Shaohua Li9dedf602012-07-31 10:03:53 +1000699 best_disk = best_pending_disk;
700 else
701 best_disk = best_dist_disk;
702 }
703
NeilBrown76073052011-05-11 14:34:56 +1000704 if (best_disk >= 0) {
705 rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700706 if (!rdev)
707 goto retry;
708 atomic_inc(&rdev->nr_pending);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000709 sectors = best_good_sectors;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000710
711 if (conf->mirrors[best_disk].next_seq_sect != this_sector)
712 conf->mirrors[best_disk].seq_start = this_sector;
713
Shaohua Libe4d3282012-07-31 10:03:53 +1000714 conf->mirrors[best_disk].next_seq_sect = this_sector + sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716 rcu_read_unlock();
NeilBrownd2eb35a2011-07-28 11:31:48 +1000717 *max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
NeilBrown76073052011-05-11 14:34:56 +1000719 return best_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
NeilBrown5c675f82014-12-15 12:56:56 +1100722static int raid1_congested(struct mddev *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700723{
NeilBrowne8096362011-10-11 16:49:05 +1100724 struct r1conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700725 int i, ret = 0;
726
Tejun Heo44522262015-05-22 17:13:26 -0400727 if ((bits & (1 << WB_async_congested)) &&
NeilBrown34db0cd2011-10-11 16:50:01 +1100728 conf->pending_count >= max_queued_requests)
729 return 1;
730
NeilBrown0d129222006-10-03 01:15:54 -0700731 rcu_read_lock();
NeilBrownf53e29f2012-02-13 14:24:05 +1100732 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100733 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700734 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200735 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700736
Jonathan Brassow1ed72422011-06-07 17:50:35 -0500737 BUG_ON(!q);
738
NeilBrown0d129222006-10-03 01:15:54 -0700739 /* Note the '|| 1' - when read_balance prefers
740 * non-congested targets, it can be removed
741 */
Tejun Heo44522262015-05-22 17:13:26 -0400742 if ((bits & (1 << WB_async_congested)) || 1)
NeilBrown0d129222006-10-03 01:15:54 -0700743 ret |= bdi_congested(&q->backing_dev_info, bits);
744 else
745 ret &= bdi_congested(&q->backing_dev_info, bits);
746 }
747 }
748 rcu_read_unlock();
749 return ret;
750}
NeilBrown0d129222006-10-03 01:15:54 -0700751
NeilBrowne8096362011-10-11 16:49:05 +1100752static void flush_pending_writes(struct r1conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800753{
754 /* Any writes that have been queued but are awaiting
755 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800756 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800757 spin_lock_irq(&conf->device_lock);
758
759 if (conf->pending_bio_list.head) {
760 struct bio *bio;
761 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100762 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800763 spin_unlock_irq(&conf->device_lock);
764 /* flush any pending bitmap writes to
765 * disk before proceeding w/ I/O */
766 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100767 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800768
769 while (bio) { /* submit pending writes */
770 struct bio *next = bio->bi_next;
NeilBrown5e2c7a32016-11-04 16:46:03 +1100771 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrowna35e63e2008-03-04 14:29:29 -0800772 bio->bi_next = NULL;
NeilBrown5e2c7a32016-11-04 16:46:03 +1100773 bio->bi_bdev = rdev->bdev;
774 if (test_bit(Faulty, &rdev->flags)) {
775 bio->bi_error = -EIO;
776 bio_endio(bio);
777 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
778 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li2ff8cc22012-10-11 13:28:54 +1100779 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200780 bio_endio(bio);
Shaohua Li2ff8cc22012-10-11 13:28:54 +1100781 else
782 generic_make_request(bio);
NeilBrowna35e63e2008-03-04 14:29:29 -0800783 bio = next;
784 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800785 } else
786 spin_unlock_irq(&conf->device_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100787}
788
NeilBrown17999be2006-01-06 00:20:12 -0800789/* Barriers....
790 * Sometimes we need to suspend IO while we do something else,
791 * either some resync/recovery, or reconfigure the array.
792 * To do this we raise a 'barrier'.
793 * The 'barrier' is a counter that can be raised multiple times
794 * to count how many activities are happening which preclude
795 * normal IO.
796 * We can only raise the barrier if there is no pending IO.
797 * i.e. if nr_pending == 0.
798 * We choose only to raise the barrier if no-one is waiting for the
799 * barrier to go down. This means that as soon as an IO request
800 * is ready, no other operations which require a barrier will start
801 * until the IO request has had a chance.
802 *
803 * So: regular IO calls 'wait_barrier'. When that returns there
804 * is no backgroup IO happening, It must arrange to call
805 * allow_barrier when it has finished its IO.
806 * backgroup IO calls must call raise_barrier. Once that returns
807 * there is no normal IO happeing. It must arrange to call
808 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 */
NeilBrownc2fd4c92014-09-10 16:01:24 +1000810static void raise_barrier(struct r1conf *conf, sector_t sector_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 spin_lock_irq(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800813
814 /* Wait until no block IO is waiting */
815 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
Lukas Czernereed8c022012-11-30 11:42:40 +0100816 conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800817
818 /* block any new IO from starting */
819 conf->barrier++;
NeilBrownc2fd4c92014-09-10 16:01:24 +1000820 conf->next_resync = sector_nr;
NeilBrown17999be2006-01-06 00:20:12 -0800821
majianpeng79ef3a82013-11-15 14:55:02 +0800822 /* For these conditions we must wait:
823 * A: while the array is in frozen state
824 * B: while barrier >= RESYNC_DEPTH, meaning resync reach
825 * the max count which allowed.
826 * C: next_resync + RESYNC_SECTORS > start_next_window, meaning
827 * next resync will reach to the window which normal bios are
828 * handling.
NeilBrown2f73d3c2014-09-10 15:01:49 +1000829 * D: while there are any active requests in the current window.
majianpeng79ef3a82013-11-15 14:55:02 +0800830 */
NeilBrown17999be2006-01-06 00:20:12 -0800831 wait_event_lock_irq(conf->wait_barrier,
majianpengb364e3d2013-11-14 15:16:18 +1100832 !conf->array_frozen &&
majianpeng79ef3a82013-11-15 14:55:02 +0800833 conf->barrier < RESYNC_DEPTH &&
NeilBrown2f73d3c2014-09-10 15:01:49 +1000834 conf->current_window_requests == 0 &&
majianpeng79ef3a82013-11-15 14:55:02 +0800835 (conf->start_next_window >=
836 conf->next_resync + RESYNC_SECTORS),
Lukas Czernereed8c022012-11-30 11:42:40 +0100837 conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800838
NeilBrown34e97f12014-09-16 12:14:14 +1000839 conf->nr_pending++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 spin_unlock_irq(&conf->resync_lock);
841}
842
NeilBrowne8096362011-10-11 16:49:05 +1100843static void lower_barrier(struct r1conf *conf)
NeilBrown17999be2006-01-06 00:20:12 -0800844{
845 unsigned long flags;
NeilBrown709ae482009-12-14 12:49:51 +1100846 BUG_ON(conf->barrier <= 0);
NeilBrown17999be2006-01-06 00:20:12 -0800847 spin_lock_irqsave(&conf->resync_lock, flags);
848 conf->barrier--;
NeilBrown34e97f12014-09-16 12:14:14 +1000849 conf->nr_pending--;
NeilBrown17999be2006-01-06 00:20:12 -0800850 spin_unlock_irqrestore(&conf->resync_lock, flags);
851 wake_up(&conf->wait_barrier);
852}
853
majianpeng79ef3a82013-11-15 14:55:02 +0800854static bool need_to_wait_for_sync(struct r1conf *conf, struct bio *bio)
NeilBrown17999be2006-01-06 00:20:12 -0800855{
majianpeng79ef3a82013-11-15 14:55:02 +0800856 bool wait = false;
857
858 if (conf->array_frozen || !bio)
859 wait = true;
860 else if (conf->barrier && bio_data_dir(bio) == WRITE) {
NeilBrown23554962014-09-10 15:56:57 +1000861 if ((conf->mddev->curr_resync_completed
862 >= bio_end_sector(bio)) ||
NeilBrownf2c771a2016-11-09 10:21:32 +1100863 (conf->start_next_window + NEXT_NORMALIO_DISTANCE
NeilBrown23554962014-09-10 15:56:57 +1000864 <= bio->bi_iter.bi_sector))
majianpeng79ef3a82013-11-15 14:55:02 +0800865 wait = false;
866 else
867 wait = true;
868 }
869
870 return wait;
871}
872
873static sector_t wait_barrier(struct r1conf *conf, struct bio *bio)
874{
875 sector_t sector = 0;
876
NeilBrown17999be2006-01-06 00:20:12 -0800877 spin_lock_irq(&conf->resync_lock);
majianpeng79ef3a82013-11-15 14:55:02 +0800878 if (need_to_wait_for_sync(conf, bio)) {
NeilBrown17999be2006-01-06 00:20:12 -0800879 conf->nr_waiting++;
NeilBrownd6b42dc2012-03-19 12:46:38 +1100880 /* Wait for the barrier to drop.
881 * However if there are already pending
882 * requests (preventing the barrier from
883 * rising completely), and the
NeilBrown5965b642014-09-04 15:51:44 +1000884 * per-process bio queue isn't empty,
NeilBrownd6b42dc2012-03-19 12:46:38 +1100885 * then don't wait, as we need to empty
NeilBrown5965b642014-09-04 15:51:44 +1000886 * that queue to allow conf->start_next_window
887 * to increase.
NeilBrownd6b42dc2012-03-19 12:46:38 +1100888 */
NeilBrown578b54a2016-11-14 16:30:21 +1100889 raid1_log(conf->mddev, "wait barrier");
NeilBrownd6b42dc2012-03-19 12:46:38 +1100890 wait_event_lock_irq(conf->wait_barrier,
majianpengb364e3d2013-11-14 15:16:18 +1100891 !conf->array_frozen &&
892 (!conf->barrier ||
NeilBrown5965b642014-09-04 15:51:44 +1000893 ((conf->start_next_window <
894 conf->next_resync + RESYNC_SECTORS) &&
895 current->bio_list &&
896 !bio_list_empty(current->bio_list))),
Lukas Czernereed8c022012-11-30 11:42:40 +0100897 conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800898 conf->nr_waiting--;
899 }
majianpeng79ef3a82013-11-15 14:55:02 +0800900
901 if (bio && bio_data_dir(bio) == WRITE) {
Jes Sorensene8ff8bf2015-09-16 10:20:05 -0400902 if (bio->bi_iter.bi_sector >= conf->next_resync) {
majianpeng79ef3a82013-11-15 14:55:02 +0800903 if (conf->start_next_window == MaxSector)
904 conf->start_next_window =
905 conf->next_resync +
906 NEXT_NORMALIO_DISTANCE;
907
908 if ((conf->start_next_window + NEXT_NORMALIO_DISTANCE)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700909 <= bio->bi_iter.bi_sector)
majianpeng79ef3a82013-11-15 14:55:02 +0800910 conf->next_window_requests++;
911 else
912 conf->current_window_requests++;
majianpeng79ef3a82013-11-15 14:55:02 +0800913 sector = conf->start_next_window;
NeilBrown41a336e2014-01-14 11:56:14 +1100914 }
majianpeng79ef3a82013-11-15 14:55:02 +0800915 }
916
NeilBrown17999be2006-01-06 00:20:12 -0800917 conf->nr_pending++;
918 spin_unlock_irq(&conf->resync_lock);
majianpeng79ef3a82013-11-15 14:55:02 +0800919 return sector;
NeilBrown17999be2006-01-06 00:20:12 -0800920}
921
majianpeng79ef3a82013-11-15 14:55:02 +0800922static void allow_barrier(struct r1conf *conf, sector_t start_next_window,
923 sector_t bi_sector)
NeilBrown17999be2006-01-06 00:20:12 -0800924{
925 unsigned long flags;
majianpeng79ef3a82013-11-15 14:55:02 +0800926
NeilBrown17999be2006-01-06 00:20:12 -0800927 spin_lock_irqsave(&conf->resync_lock, flags);
928 conf->nr_pending--;
majianpeng79ef3a82013-11-15 14:55:02 +0800929 if (start_next_window) {
930 if (start_next_window == conf->start_next_window) {
931 if (conf->start_next_window + NEXT_NORMALIO_DISTANCE
932 <= bi_sector)
933 conf->next_window_requests--;
934 else
935 conf->current_window_requests--;
936 } else
937 conf->current_window_requests--;
938
939 if (!conf->current_window_requests) {
940 if (conf->next_window_requests) {
941 conf->current_window_requests =
942 conf->next_window_requests;
943 conf->next_window_requests = 0;
944 conf->start_next_window +=
945 NEXT_NORMALIO_DISTANCE;
946 } else
947 conf->start_next_window = MaxSector;
948 }
949 }
NeilBrown17999be2006-01-06 00:20:12 -0800950 spin_unlock_irqrestore(&conf->resync_lock, flags);
951 wake_up(&conf->wait_barrier);
952}
953
NeilBrowne2d59922013-06-12 11:01:22 +1000954static void freeze_array(struct r1conf *conf, int extra)
NeilBrownddaf22a2006-01-06 00:20:19 -0800955{
956 /* stop syncio and normal IO and wait for everything to
957 * go quite.
majianpengb364e3d2013-11-14 15:16:18 +1100958 * We wait until nr_pending match nr_queued+extra
NeilBrown1c830532008-03-04 14:29:35 -0800959 * This is called in the context of one normal IO request
960 * that has failed. Thus any sync request that might be pending
961 * will be blocked by nr_pending, and we need to wait for
962 * pending IO requests to complete or be queued for re-try.
NeilBrowne2d59922013-06-12 11:01:22 +1000963 * Thus the number queued (nr_queued) plus this request (extra)
NeilBrown1c830532008-03-04 14:29:35 -0800964 * must match the number of pending IOs (nr_pending) before
965 * we continue.
NeilBrownddaf22a2006-01-06 00:20:19 -0800966 */
967 spin_lock_irq(&conf->resync_lock);
majianpengb364e3d2013-11-14 15:16:18 +1100968 conf->array_frozen = 1;
NeilBrown578b54a2016-11-14 16:30:21 +1100969 raid1_log(conf->mddev, "wait freeze");
Lukas Czernereed8c022012-11-30 11:42:40 +0100970 wait_event_lock_irq_cmd(conf->wait_barrier,
NeilBrowne2d59922013-06-12 11:01:22 +1000971 conf->nr_pending == conf->nr_queued+extra,
Lukas Czernereed8c022012-11-30 11:42:40 +0100972 conf->resync_lock,
973 flush_pending_writes(conf));
NeilBrownddaf22a2006-01-06 00:20:19 -0800974 spin_unlock_irq(&conf->resync_lock);
975}
NeilBrowne8096362011-10-11 16:49:05 +1100976static void unfreeze_array(struct r1conf *conf)
NeilBrownddaf22a2006-01-06 00:20:19 -0800977{
978 /* reverse the effect of the freeze */
979 spin_lock_irq(&conf->resync_lock);
majianpengb364e3d2013-11-14 15:16:18 +1100980 conf->array_frozen = 0;
NeilBrownddaf22a2006-01-06 00:20:19 -0800981 wake_up(&conf->wait_barrier);
982 spin_unlock_irq(&conf->resync_lock);
983}
984
NeilBrownf72ffdd2014-09-30 14:23:59 +1000985/* duplicate the data pages for behind I/O
NeilBrown4e780642010-10-19 12:54:01 +1100986 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100987static void alloc_behind_pages(struct bio *bio, struct r1bio *r1_bio)
NeilBrown4b6d2872005-09-09 16:23:47 -0700988{
989 int i;
990 struct bio_vec *bvec;
NeilBrown2ca68f52011-07-28 11:32:10 +1000991 struct bio_vec *bvecs = kzalloc(bio->bi_vcnt * sizeof(struct bio_vec),
NeilBrown4b6d2872005-09-09 16:23:47 -0700992 GFP_NOIO);
NeilBrown2ca68f52011-07-28 11:32:10 +1000993 if (unlikely(!bvecs))
NeilBrownaf6d7b72011-05-11 14:51:19 +1000994 return;
NeilBrown4b6d2872005-09-09 16:23:47 -0700995
Kent Overstreetcb34e052012-09-05 15:22:02 -0700996 bio_for_each_segment_all(bvec, bio, i) {
NeilBrown2ca68f52011-07-28 11:32:10 +1000997 bvecs[i] = *bvec;
998 bvecs[i].bv_page = alloc_page(GFP_NOIO);
999 if (unlikely(!bvecs[i].bv_page))
NeilBrown4b6d2872005-09-09 16:23:47 -07001000 goto do_sync_io;
NeilBrown2ca68f52011-07-28 11:32:10 +10001001 memcpy(kmap(bvecs[i].bv_page) + bvec->bv_offset,
1002 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
1003 kunmap(bvecs[i].bv_page);
NeilBrown4b6d2872005-09-09 16:23:47 -07001004 kunmap(bvec->bv_page);
1005 }
NeilBrown2ca68f52011-07-28 11:32:10 +10001006 r1_bio->behind_bvecs = bvecs;
NeilBrownaf6d7b72011-05-11 14:51:19 +10001007 r1_bio->behind_page_count = bio->bi_vcnt;
1008 set_bit(R1BIO_BehindIO, &r1_bio->state);
1009 return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001010
1011do_sync_io:
NeilBrownaf6d7b72011-05-11 14:51:19 +10001012 for (i = 0; i < bio->bi_vcnt; i++)
NeilBrown2ca68f52011-07-28 11:32:10 +10001013 if (bvecs[i].bv_page)
1014 put_page(bvecs[i].bv_page);
1015 kfree(bvecs);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001016 pr_debug("%dB behind alloc failed, doing sync I/O\n",
1017 bio->bi_iter.bi_size);
NeilBrown4b6d2872005-09-09 16:23:47 -07001018}
1019
NeilBrownf54a9d02012-08-02 08:33:20 +10001020struct raid1_plug_cb {
1021 struct blk_plug_cb cb;
1022 struct bio_list pending;
1023 int pending_cnt;
1024};
1025
1026static void raid1_unplug(struct blk_plug_cb *cb, bool from_schedule)
1027{
1028 struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb,
1029 cb);
1030 struct mddev *mddev = plug->cb.data;
1031 struct r1conf *conf = mddev->private;
1032 struct bio *bio;
1033
NeilBrown874807a2012-11-27 12:14:40 +11001034 if (from_schedule || current->bio_list) {
NeilBrownf54a9d02012-08-02 08:33:20 +10001035 spin_lock_irq(&conf->device_lock);
1036 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1037 conf->pending_count += plug->pending_cnt;
1038 spin_unlock_irq(&conf->device_lock);
NeilBrownee0b0242013-02-25 12:38:29 +11001039 wake_up(&conf->wait_barrier);
NeilBrownf54a9d02012-08-02 08:33:20 +10001040 md_wakeup_thread(mddev->thread);
1041 kfree(plug);
1042 return;
1043 }
1044
1045 /* we aren't scheduling, so we can do the write-out directly. */
1046 bio = bio_list_get(&plug->pending);
1047 bitmap_unplug(mddev->bitmap);
1048 wake_up(&conf->wait_barrier);
1049
1050 while (bio) { /* submit pending writes */
1051 struct bio *next = bio->bi_next;
NeilBrown5e2c7a32016-11-04 16:46:03 +11001052 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrownf54a9d02012-08-02 08:33:20 +10001053 bio->bi_next = NULL;
NeilBrown5e2c7a32016-11-04 16:46:03 +11001054 bio->bi_bdev = rdev->bdev;
1055 if (test_bit(Faulty, &rdev->flags)) {
1056 bio->bi_error = -EIO;
1057 bio_endio(bio);
1058 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1059 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li32f9f572013-04-28 18:26:38 +08001060 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001061 bio_endio(bio);
Shaohua Li32f9f572013-04-28 18:26:38 +08001062 else
1063 generic_make_request(bio);
NeilBrownf54a9d02012-08-02 08:33:20 +10001064 bio = next;
1065 }
1066 kfree(plug);
1067}
1068
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001069static void raid1_read_request(struct mddev *mddev, struct bio *bio,
1070 struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
NeilBrowne8096362011-10-11 16:49:05 +11001072 struct r1conf *conf = mddev->private;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001073 struct raid1_info *mirror;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 struct bio *read_bio;
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001075 struct bitmap *bitmap = mddev->bitmap;
1076 const int op = bio_op(bio);
1077 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1078 int sectors_handled;
1079 int max_sectors;
1080 int rdisk;
1081
1082 wait_barrier(conf, bio);
1083
1084read_again:
1085 rdisk = read_balance(conf, r1_bio, &max_sectors);
1086
1087 if (rdisk < 0) {
1088 /* couldn't find anywhere to read from */
1089 raid_end_bio_io(r1_bio);
1090 return;
1091 }
1092 mirror = conf->mirrors + rdisk;
1093
1094 if (test_bit(WriteMostly, &mirror->rdev->flags) &&
1095 bitmap) {
1096 /*
1097 * Reading from a write-mostly device must take care not to
1098 * over-take any writes that are 'behind'
1099 */
1100 raid1_log(mddev, "wait behind writes");
1101 wait_event(bitmap->behind_wait,
1102 atomic_read(&bitmap->behind_writes) == 0);
1103 }
1104 r1_bio->read_disk = rdisk;
1105 r1_bio->start_next_window = 0;
1106
1107 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1108 bio_trim(read_bio, r1_bio->sector - bio->bi_iter.bi_sector,
1109 max_sectors);
1110
1111 r1_bio->bios[rdisk] = read_bio;
1112
1113 read_bio->bi_iter.bi_sector = r1_bio->sector +
1114 mirror->rdev->data_offset;
1115 read_bio->bi_bdev = mirror->rdev->bdev;
1116 read_bio->bi_end_io = raid1_end_read_request;
1117 bio_set_op_attrs(read_bio, op, do_sync);
1118 if (test_bit(FailFast, &mirror->rdev->flags) &&
1119 test_bit(R1BIO_FailFast, &r1_bio->state))
1120 read_bio->bi_opf |= MD_FAILFAST;
1121 read_bio->bi_private = r1_bio;
1122
1123 if (mddev->gendisk)
1124 trace_block_bio_remap(bdev_get_queue(read_bio->bi_bdev),
1125 read_bio, disk_devt(mddev->gendisk),
1126 r1_bio->sector);
1127
1128 if (max_sectors < r1_bio->sectors) {
1129 /*
1130 * could not read all from this device, so we will need another
1131 * r1_bio.
1132 */
1133 sectors_handled = (r1_bio->sector + max_sectors
1134 - bio->bi_iter.bi_sector);
1135 r1_bio->sectors = max_sectors;
1136 spin_lock_irq(&conf->device_lock);
1137 if (bio->bi_phys_segments == 0)
1138 bio->bi_phys_segments = 2;
1139 else
1140 bio->bi_phys_segments++;
1141 spin_unlock_irq(&conf->device_lock);
1142
1143 /*
1144 * Cannot call generic_make_request directly as that will be
1145 * queued in __make_request and subsequent mempool_alloc might
1146 * block waiting for it. So hand bio over to raid1d.
1147 */
1148 reschedule_retry(r1_bio);
1149
1150 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1151
1152 r1_bio->master_bio = bio;
1153 r1_bio->sectors = bio_sectors(bio) - sectors_handled;
1154 r1_bio->state = 0;
1155 r1_bio->mddev = mddev;
1156 r1_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
1157 goto read_again;
1158 } else
1159 generic_make_request(read_bio);
1160}
1161
1162static void raid1_write_request(struct mddev *mddev, struct bio *bio,
1163 struct r1bio *r1_bio)
1164{
1165 struct r1conf *conf = mddev->private;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001166 int i, disks;
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001167 struct bitmap *bitmap = mddev->bitmap;
NeilBrown191ea9b2005-06-21 17:17:23 -07001168 unsigned long flags;
Mike Christie796a5cf2016-06-05 14:32:07 -05001169 const int op = bio_op(bio);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001170 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1171 const unsigned long do_flush_fua = (bio->bi_opf &
Mike Christie28a8f0d2016-06-05 14:32:25 -05001172 (REQ_PREFLUSH | REQ_FUA));
NeilBrown3cb03002011-10-11 16:45:26 +11001173 struct md_rdev *blocked_rdev;
NeilBrownf54a9d02012-08-02 08:33:20 +10001174 struct blk_plug_cb *cb;
1175 struct raid1_plug_cb *plug = NULL;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001176 int first_clone;
1177 int sectors_handled;
1178 int max_sectors;
majianpeng79ef3a82013-11-15 14:55:02 +08001179 sector_t start_next_window;
NeilBrown191ea9b2005-06-21 17:17:23 -07001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /*
1182 * Register the new request and wait if the reconstruction
1183 * thread has put up a bar for new requests.
1184 * Continue immediately if no resync is active currently.
1185 */
NeilBrown62de6082006-05-01 12:15:47 -07001186
NeilBrown3d310eb2005-06-21 17:17:26 -07001187 md_write_start(mddev, bio); /* wait on superblock update early */
1188
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001189 if ((bio_end_sector(bio) > mddev->suspend_lo &&
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001190 bio->bi_iter.bi_sector < mddev->suspend_hi) ||
1191 (mddev_is_clustered(mddev) &&
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -05001192 md_cluster_ops->area_resyncing(mddev, WRITE,
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001193 bio->bi_iter.bi_sector, bio_end_sector(bio)))) {
1194
1195 /*
1196 * As the suspend_* range is controlled by userspace, we want
1197 * an interruptible wait.
NeilBrown6eef4b22009-12-14 12:49:51 +11001198 */
1199 DEFINE_WAIT(w);
1200 for (;;) {
1201 flush_signals(current);
1202 prepare_to_wait(&conf->wait_barrier,
1203 &w, TASK_INTERRUPTIBLE);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07001204 if (bio_end_sector(bio) <= mddev->suspend_lo ||
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001205 bio->bi_iter.bi_sector >= mddev->suspend_hi ||
1206 (mddev_is_clustered(mddev) &&
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -05001207 !md_cluster_ops->area_resyncing(mddev, WRITE,
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001208 bio->bi_iter.bi_sector,
1209 bio_end_sector(bio))))
NeilBrown6eef4b22009-12-14 12:49:51 +11001210 break;
1211 schedule();
1212 }
1213 finish_wait(&conf->wait_barrier, &w);
1214 }
majianpeng79ef3a82013-11-15 14:55:02 +08001215 start_next_window = wait_barrier(conf, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
NeilBrown34db0cd2011-10-11 16:50:01 +11001217 if (conf->pending_count >= max_queued_requests) {
1218 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001219 raid1_log(mddev, "wait queued");
NeilBrown34db0cd2011-10-11 16:50:01 +11001220 wait_event(conf->wait_barrier,
1221 conf->pending_count < max_queued_requests);
1222 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001223 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 * inc refcount on their rdev. Record them by setting
1225 * bios[x] to bio
NeilBrown1f68f0c2011-07-28 11:31:48 +10001226 * If there are known/acknowledged bad blocks on any device on
1227 * which we have seen a write error, we want to avoid writing those
1228 * blocks.
1229 * This potentially requires several writes to write around
1230 * the bad blocks. Each set of writes gets it's own r1bio
1231 * with a set of bios attached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001233
NeilBrown8f19ccb2011-12-23 10:17:56 +11001234 disks = conf->raid_disks * 2;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001235 retry_write:
majianpeng79ef3a82013-11-15 14:55:02 +08001236 r1_bio->start_next_window = start_next_window;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001237 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 rcu_read_lock();
NeilBrown1f68f0c2011-07-28 11:31:48 +10001239 max_sectors = r1_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 for (i = 0; i < disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001241 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001242 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1243 atomic_inc(&rdev->nr_pending);
1244 blocked_rdev = rdev;
1245 break;
1246 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001247 r1_bio->bios[i] = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001248 if (!rdev || test_bit(Faulty, &rdev->flags)) {
NeilBrown8f19ccb2011-12-23 10:17:56 +11001249 if (i < conf->raid_disks)
1250 set_bit(R1BIO_Degraded, &r1_bio->state);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001251 continue;
1252 }
1253
1254 atomic_inc(&rdev->nr_pending);
1255 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1256 sector_t first_bad;
1257 int bad_sectors;
1258 int is_bad;
1259
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001260 is_bad = is_badblock(rdev, r1_bio->sector, max_sectors,
NeilBrown1f68f0c2011-07-28 11:31:48 +10001261 &first_bad, &bad_sectors);
1262 if (is_bad < 0) {
1263 /* mustn't write here until the bad block is
1264 * acknowledged*/
1265 set_bit(BlockedBadBlocks, &rdev->flags);
1266 blocked_rdev = rdev;
1267 break;
NeilBrown964147d2010-05-18 15:27:13 +10001268 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001269 if (is_bad && first_bad <= r1_bio->sector) {
1270 /* Cannot write here at all */
1271 bad_sectors -= (r1_bio->sector - first_bad);
1272 if (bad_sectors < max_sectors)
1273 /* mustn't write more than bad_sectors
1274 * to other devices yet
1275 */
1276 max_sectors = bad_sectors;
1277 rdev_dec_pending(rdev, mddev);
1278 /* We don't set R1BIO_Degraded as that
1279 * only applies if the disk is
1280 * missing, so it might be re-added,
1281 * and we want to know to recover this
1282 * chunk.
1283 * In this case the device is here,
1284 * and the fact that this chunk is not
1285 * in-sync is recorded in the bad
1286 * block log
1287 */
1288 continue;
1289 }
1290 if (is_bad) {
1291 int good_sectors = first_bad - r1_bio->sector;
1292 if (good_sectors < max_sectors)
1293 max_sectors = good_sectors;
1294 }
1295 }
1296 r1_bio->bios[i] = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298 rcu_read_unlock();
1299
Dan Williams6bfe0b42008-04-30 00:52:32 -07001300 if (unlikely(blocked_rdev)) {
1301 /* Wait for this device to become unblocked */
1302 int j;
majianpeng79ef3a82013-11-15 14:55:02 +08001303 sector_t old = start_next_window;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001304
1305 for (j = 0; j < i; j++)
1306 if (r1_bio->bios[j])
1307 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001308 r1_bio->state = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001309 allow_barrier(conf, start_next_window, bio->bi_iter.bi_sector);
NeilBrown578b54a2016-11-14 16:30:21 +11001310 raid1_log(mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001311 md_wait_for_blocked_rdev(blocked_rdev, mddev);
majianpeng79ef3a82013-11-15 14:55:02 +08001312 start_next_window = wait_barrier(conf, bio);
1313 /*
1314 * We must make sure the multi r1bios of bio have
1315 * the same value of bi_phys_segments
1316 */
1317 if (bio->bi_phys_segments && old &&
1318 old != start_next_window)
1319 /* Wait for the former r1bio(s) to complete */
1320 wait_event(conf->wait_barrier,
1321 bio->bi_phys_segments == 1);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001322 goto retry_write;
1323 }
1324
NeilBrown1f68f0c2011-07-28 11:31:48 +10001325 if (max_sectors < r1_bio->sectors) {
1326 /* We are splitting this write into multiple parts, so
1327 * we need to prepare for allocating another r1_bio.
1328 */
1329 r1_bio->sectors = max_sectors;
1330 spin_lock_irq(&conf->device_lock);
1331 if (bio->bi_phys_segments == 0)
1332 bio->bi_phys_segments = 2;
1333 else
1334 bio->bi_phys_segments++;
1335 spin_unlock_irq(&conf->device_lock);
NeilBrown191ea9b2005-06-21 17:17:23 -07001336 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07001337 sectors_handled = r1_bio->sector + max_sectors - bio->bi_iter.bi_sector;
NeilBrown4b6d2872005-09-09 16:23:47 -07001338
NeilBrown4e780642010-10-19 12:54:01 +11001339 atomic_set(&r1_bio->remaining, 1);
NeilBrown4b6d2872005-09-09 16:23:47 -07001340 atomic_set(&r1_bio->behind_remaining, 0);
NeilBrown191ea9b2005-06-21 17:17:23 -07001341
NeilBrown1f68f0c2011-07-28 11:31:48 +10001342 first_clone = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 for (i = 0; i < disks; i++) {
1344 struct bio *mbio;
1345 if (!r1_bio->bios[i])
1346 continue;
1347
NeilBrowna167f662010-10-26 18:31:13 +11001348 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001349 bio_trim(mbio, r1_bio->sector - bio->bi_iter.bi_sector,
1350 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
NeilBrown1f68f0c2011-07-28 11:31:48 +10001352 if (first_clone) {
1353 /* do behind I/O ?
1354 * Not if there are too many, or cannot
1355 * allocate memory, or a reader on WriteMostly
1356 * is waiting for behind writes to flush */
1357 if (bitmap &&
1358 (atomic_read(&bitmap->behind_writes)
1359 < mddev->bitmap_info.max_write_behind) &&
1360 !waitqueue_active(&bitmap->behind_wait))
1361 alloc_behind_pages(mbio, r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
NeilBrown1f68f0c2011-07-28 11:31:48 +10001363 bitmap_startwrite(bitmap, r1_bio->sector,
1364 r1_bio->sectors,
1365 test_bit(R1BIO_BehindIO,
1366 &r1_bio->state));
1367 first_clone = 0;
1368 }
NeilBrown2ca68f52011-07-28 11:32:10 +10001369 if (r1_bio->behind_bvecs) {
NeilBrown4b6d2872005-09-09 16:23:47 -07001370 struct bio_vec *bvec;
1371 int j;
1372
Kent Overstreetcb34e052012-09-05 15:22:02 -07001373 /*
1374 * We trimmed the bio, so _all is legit
NeilBrown4b6d2872005-09-09 16:23:47 -07001375 */
Kent Overstreetd74c6d52013-02-06 12:23:11 -08001376 bio_for_each_segment_all(bvec, mbio, j)
NeilBrown2ca68f52011-07-28 11:32:10 +10001377 bvec->bv_page = r1_bio->behind_bvecs[j].bv_page;
NeilBrown4b6d2872005-09-09 16:23:47 -07001378 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
1379 atomic_inc(&r1_bio->behind_remaining);
1380 }
1381
NeilBrown1f68f0c2011-07-28 11:31:48 +10001382 r1_bio->bios[i] = mbio;
1383
Kent Overstreet4f024f32013-10-11 15:44:27 -07001384 mbio->bi_iter.bi_sector = (r1_bio->sector +
NeilBrown1f68f0c2011-07-28 11:31:48 +10001385 conf->mirrors[i].rdev->data_offset);
NeilBrown109e3762016-11-18 13:22:04 +11001386 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001387 mbio->bi_end_io = raid1_end_write_request;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001388 bio_set_op_attrs(mbio, op, do_flush_fua | do_sync);
NeilBrown212e7eb2016-11-18 16:16:12 +11001389 if (test_bit(FailFast, &conf->mirrors[i].rdev->flags) &&
1390 !test_bit(WriteMostly, &conf->mirrors[i].rdev->flags) &&
1391 conf->raid_disks - mddev->degraded > 1)
1392 mbio->bi_opf |= MD_FAILFAST;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001393 mbio->bi_private = r1_bio;
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 atomic_inc(&r1_bio->remaining);
NeilBrownf54a9d02012-08-02 08:33:20 +10001396
NeilBrown109e3762016-11-18 13:22:04 +11001397 if (mddev->gendisk)
1398 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1399 mbio, disk_devt(mddev->gendisk),
1400 r1_bio->sector);
1401 /* flush_pending_writes() needs access to the rdev so...*/
1402 mbio->bi_bdev = (void*)conf->mirrors[i].rdev;
1403
NeilBrownf54a9d02012-08-02 08:33:20 +10001404 cb = blk_check_plugged(raid1_unplug, mddev, sizeof(*plug));
1405 if (cb)
1406 plug = container_of(cb, struct raid1_plug_cb, cb);
1407 else
1408 plug = NULL;
NeilBrown4e780642010-10-19 12:54:01 +11001409 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownf54a9d02012-08-02 08:33:20 +10001410 if (plug) {
1411 bio_list_add(&plug->pending, mbio);
1412 plug->pending_cnt++;
1413 } else {
1414 bio_list_add(&conf->pending_bio_list, mbio);
1415 conf->pending_count++;
1416 }
NeilBrown4e780642010-10-19 12:54:01 +11001417 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrownf54a9d02012-08-02 08:33:20 +10001418 if (!plug)
NeilBrownb357f042012-07-03 17:45:31 +10001419 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
NeilBrown079fa162011-09-10 17:21:23 +10001421 /* Mustn't call r1_bio_write_done before this next test,
1422 * as it could result in the bio being freed.
1423 */
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001424 if (sectors_handled < bio_sectors(bio)) {
NeilBrown079fa162011-09-10 17:21:23 +10001425 r1_bio_write_done(r1_bio);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001426 /* We need another r1_bio. It has already been counted
1427 * in bio->bi_phys_segments
1428 */
1429 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1430 r1_bio->master_bio = bio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001431 r1_bio->sectors = bio_sectors(bio) - sectors_handled;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001432 r1_bio->state = 0;
1433 r1_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001434 r1_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001435 goto retry_write;
1436 }
1437
NeilBrown079fa162011-09-10 17:21:23 +10001438 r1_bio_write_done(r1_bio);
1439
1440 /* In case raid1d snuck in to freeze_array */
1441 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001444static void raid1_make_request(struct mddev *mddev, struct bio *bio)
1445{
1446 struct r1conf *conf = mddev->private;
1447 struct r1bio *r1_bio;
1448
1449 /*
1450 * make_request() can abort the operation when read-ahead is being
1451 * used and no empty request is available.
1452 *
1453 */
1454 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1455
1456 r1_bio->master_bio = bio;
1457 r1_bio->sectors = bio_sectors(bio);
1458 r1_bio->state = 0;
1459 r1_bio->mddev = mddev;
1460 r1_bio->sector = bio->bi_iter.bi_sector;
1461
1462 /*
1463 * We might need to issue multiple reads to different devices if there
1464 * are bad blocks around, so we keep track of the number of reads in
1465 * bio->bi_phys_segments. If this is 0, there is only one r1_bio and
1466 * no locking will be needed when requests complete. If it is
1467 * non-zero, then it is the number of not-completed requests.
1468 */
1469 bio->bi_phys_segments = 0;
1470 bio_clear_flag(bio, BIO_SEG_VALID);
1471
1472 if (bio_data_dir(bio) == READ)
1473 raid1_read_request(mddev, bio, r1_bio);
1474 else
1475 raid1_write_request(mddev, bio, r1_bio);
1476}
1477
Shaohua Li849674e2016-01-20 13:52:20 -08001478static void raid1_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
NeilBrowne8096362011-10-11 16:49:05 +11001480 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 int i;
1482
1483 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown11ce99e2006-10-03 01:15:52 -07001484 conf->raid_disks - mddev->degraded);
NeilBrownddac7c72006-08-31 21:27:36 -07001485 rcu_read_lock();
1486 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001487 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 seq_printf(seq, "%s",
NeilBrownddac7c72006-08-31 21:27:36 -07001489 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1490 }
1491 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 seq_printf(seq, "]");
1493}
1494
Shaohua Li849674e2016-01-20 13:52:20 -08001495static void raid1_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 char b[BDEVNAME_SIZE];
NeilBrowne8096362011-10-11 16:49:05 +11001498 struct r1conf *conf = mddev->private;
NeilBrown423f04d2015-07-27 11:48:52 +10001499 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 /*
1502 * If it is not operational, then we have already marked it as dead
1503 * else if it is the last working disks, ignore the error, let the
1504 * next level up know.
1505 * else mark the drive as failed
1506 */
NeilBrown2e52d442016-11-18 16:16:12 +11001507 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001508 if (test_bit(In_sync, &rdev->flags)
NeilBrown4044ba52009-01-09 08:31:11 +11001509 && (conf->raid_disks - mddev->degraded) == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /*
1511 * Don't fail the drive, act as though we were just a
NeilBrown4044ba52009-01-09 08:31:11 +11001512 * normal single drive.
1513 * However don't try a recovery from this drive as
1514 * it is very likely to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 */
NeilBrown53890422011-07-27 11:00:36 +10001516 conf->recovery_disabled = mddev->recovery_disabled;
NeilBrown2e52d442016-11-18 16:16:12 +11001517 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return;
NeilBrown4044ba52009-01-09 08:31:11 +11001519 }
NeilBrownde393cd2011-07-28 11:31:48 +10001520 set_bit(Blocked, &rdev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001521 if (test_and_clear_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 mddev->degraded++;
NeilBrowndd00a992007-05-10 03:15:50 -07001523 set_bit(Faulty, &rdev->flags);
NeilBrowndd00a992007-05-10 03:15:50 -07001524 } else
1525 set_bit(Faulty, &rdev->flags);
NeilBrown423f04d2015-07-27 11:48:52 +10001526 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown2446dba2014-07-31 10:16:29 +10001527 /*
1528 * if recovery is running, make sure it aborts.
1529 */
1530 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08001531 set_mask_bits(&mddev->sb_flags, 0,
1532 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown1d41c212016-11-02 14:16:50 +11001533 pr_crit("md/raid1:%s: Disk failure on %s, disabling device.\n"
1534 "md/raid1:%s: Operation continuing on %d devices.\n",
1535 mdname(mddev), bdevname(rdev->bdev, b),
1536 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
NeilBrowne8096362011-10-11 16:49:05 +11001539static void print_conf(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
NeilBrown1d41c212016-11-02 14:16:50 +11001543 pr_debug("RAID1 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (!conf) {
NeilBrown1d41c212016-11-02 14:16:50 +11001545 pr_debug("(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return;
1547 }
NeilBrown1d41c212016-11-02 14:16:50 +11001548 pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1549 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
NeilBrownddac7c72006-08-31 21:27:36 -07001551 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 for (i = 0; i < conf->raid_disks; i++) {
1553 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11001554 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownddac7c72006-08-31 21:27:36 -07001555 if (rdev)
NeilBrown1d41c212016-11-02 14:16:50 +11001556 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1557 i, !test_bit(In_sync, &rdev->flags),
1558 !test_bit(Faulty, &rdev->flags),
1559 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
NeilBrownddac7c72006-08-31 21:27:36 -07001561 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
NeilBrowne8096362011-10-11 16:49:05 +11001564static void close_sync(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
majianpeng79ef3a82013-11-15 14:55:02 +08001566 wait_barrier(conf, NULL);
1567 allow_barrier(conf, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 mempool_destroy(conf->r1buf_pool);
1570 conf->r1buf_pool = NULL;
majianpeng79ef3a82013-11-15 14:55:02 +08001571
NeilBrown669cc7b2014-09-04 16:30:38 +10001572 spin_lock_irq(&conf->resync_lock);
Jes Sorensene8ff8bf2015-09-16 10:20:05 -04001573 conf->next_resync = MaxSector - 2 * NEXT_NORMALIO_DISTANCE;
majianpeng79ef3a82013-11-15 14:55:02 +08001574 conf->start_next_window = MaxSector;
NeilBrown669cc7b2014-09-04 16:30:38 +10001575 conf->current_window_requests +=
1576 conf->next_window_requests;
1577 conf->next_window_requests = 0;
1578 spin_unlock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
NeilBrownfd01b882011-10-11 16:47:53 +11001581static int raid1_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
1583 int i;
NeilBrowne8096362011-10-11 16:49:05 +11001584 struct r1conf *conf = mddev->private;
NeilBrown6b965622010-08-18 11:56:59 +10001585 int count = 0;
1586 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 /*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001589 * Find all failed disks within the RAID1 configuration
NeilBrownddac7c72006-08-31 21:27:36 -07001590 * and mark them readable.
1591 * Called under mddev lock, so rcu protection not needed.
NeilBrown423f04d2015-07-27 11:48:52 +10001592 * device_lock used to avoid races with raid1_end_read_request
1593 * which expects 'In_sync' flags and ->degraded to be consistent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 */
NeilBrown423f04d2015-07-27 11:48:52 +10001595 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001597 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrown8c7a2c22011-12-23 10:17:57 +11001598 struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev;
1599 if (repl
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001600 && !test_bit(Candidate, &repl->flags)
NeilBrown8c7a2c22011-12-23 10:17:57 +11001601 && repl->recovery_offset == MaxSector
1602 && !test_bit(Faulty, &repl->flags)
1603 && !test_and_set_bit(In_sync, &repl->flags)) {
1604 /* replacement has just become active */
1605 if (!rdev ||
1606 !test_and_clear_bit(In_sync, &rdev->flags))
1607 count++;
1608 if (rdev) {
1609 /* Replaced device not technically
1610 * faulty, but we need to be sure
1611 * it gets removed and never re-added
1612 */
1613 set_bit(Faulty, &rdev->flags);
1614 sysfs_notify_dirent_safe(
1615 rdev->sysfs_state);
1616 }
1617 }
NeilBrownddac7c72006-08-31 21:27:36 -07001618 if (rdev
Lukasz Dorau61e49472013-10-24 12:55:17 +11001619 && rdev->recovery_offset == MaxSector
NeilBrownddac7c72006-08-31 21:27:36 -07001620 && !test_bit(Faulty, &rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001621 && !test_and_set_bit(In_sync, &rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001622 count++;
Jonathan Brassow654e8b52011-07-27 11:00:36 +10001623 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625 }
NeilBrown6b965622010-08-18 11:56:59 +10001626 mddev->degraded -= count;
1627 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001630 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
NeilBrownfd01b882011-10-11 16:47:53 +11001633static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
NeilBrowne8096362011-10-11 16:49:05 +11001635 struct r1conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001636 int err = -EEXIST;
NeilBrown41158c72005-06-21 17:17:25 -07001637 int mirror = 0;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001638 struct raid1_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10001639 int first = 0;
NeilBrown30194632011-12-23 10:17:56 +11001640 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
NeilBrown53890422011-07-27 11:00:36 +10001642 if (mddev->recovery_disabled == conf->recovery_disabled)
1643 return -EBUSY;
1644
Dan Williams1501efa2016-01-13 16:00:07 -08001645 if (md_integrity_add_rdev(rdev, mddev))
1646 return -ENXIO;
1647
Neil Brown6c2fce22008-06-28 08:31:31 +10001648 if (rdev->raid_disk >= 0)
1649 first = last = rdev->raid_disk;
1650
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05001651 /*
1652 * find the disk ... but prefer rdev->saved_raid_disk
1653 * if possible.
1654 */
1655 if (rdev->saved_raid_disk >= 0 &&
1656 rdev->saved_raid_disk >= first &&
1657 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1658 first = last = rdev->saved_raid_disk;
1659
NeilBrown7ef449d2011-12-23 10:17:57 +11001660 for (mirror = first; mirror <= last; mirror++) {
1661 p = conf->mirrors+mirror;
1662 if (!p->rdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Jonathan Brassow9092c022013-05-02 14:19:24 -05001664 if (mddev->gendisk)
1665 disk_stack_limits(mddev->gendisk, rdev->bdev,
1666 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 p->head_position = 0;
1669 rdev->raid_disk = mirror;
Neil Brown199050e2008-06-28 08:31:33 +10001670 err = 0;
NeilBrown6aea114a2005-11-28 13:44:13 -08001671 /* As all devices are equivalent, we don't need a full recovery
1672 * if this was recently any drive of the array
1673 */
1674 if (rdev->saved_raid_disk < 0)
NeilBrown41158c72005-06-21 17:17:25 -07001675 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001676 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 break;
1678 }
NeilBrown7ef449d2011-12-23 10:17:57 +11001679 if (test_bit(WantReplacement, &p->rdev->flags) &&
1680 p[conf->raid_disks].rdev == NULL) {
1681 /* Add this device as a replacement */
1682 clear_bit(In_sync, &rdev->flags);
1683 set_bit(Replacement, &rdev->flags);
1684 rdev->raid_disk = mirror;
1685 err = 0;
1686 conf->fullsync = 1;
1687 rcu_assign_pointer(p[conf->raid_disks].rdev, rdev);
1688 break;
1689 }
1690 }
Jonathan Brassow9092c022013-05-02 14:19:24 -05001691 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
Shaohua Li2ff8cc22012-10-11 13:28:54 +11001692 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001694 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
NeilBrownb8321b62011-12-23 10:17:51 +11001697static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
NeilBrowne8096362011-10-11 16:49:05 +11001699 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001701 int number = rdev->raid_disk;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001702 struct raid1_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
NeilBrownb014f142011-12-23 10:17:56 +11001704 if (rdev != p->rdev)
1705 p = conf->mirrors + conf->raid_disks + number;
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 print_conf(conf);
NeilBrownb8321b62011-12-23 10:17:51 +11001708 if (rdev == p->rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001709 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 atomic_read(&rdev->nr_pending)) {
1711 err = -EBUSY;
1712 goto abort;
1713 }
NeilBrown046abee2010-10-26 15:46:20 +11001714 /* Only remove non-faulty devices if recovery
NeilBrowndfc70642008-05-23 13:04:39 -07001715 * is not possible.
1716 */
1717 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown53890422011-07-27 11:00:36 +10001718 mddev->recovery_disabled != conf->recovery_disabled &&
NeilBrowndfc70642008-05-23 13:04:39 -07001719 mddev->degraded < conf->raid_disks) {
1720 err = -EBUSY;
1721 goto abort;
1722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 p->rdev = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10001724 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1725 synchronize_rcu();
1726 if (atomic_read(&rdev->nr_pending)) {
1727 /* lost the race, try later */
1728 err = -EBUSY;
1729 p->rdev = rdev;
1730 goto abort;
1731 }
1732 }
1733 if (conf->mirrors[conf->raid_disks + number].rdev) {
NeilBrown8c7a2c22011-12-23 10:17:57 +11001734 /* We just removed a device that is being replaced.
1735 * Move down the replacement. We drain all IO before
1736 * doing this to avoid confusion.
1737 */
1738 struct md_rdev *repl =
1739 conf->mirrors[conf->raid_disks + number].rdev;
NeilBrowne2d59922013-06-12 11:01:22 +10001740 freeze_array(conf, 0);
NeilBrown8c7a2c22011-12-23 10:17:57 +11001741 clear_bit(Replacement, &repl->flags);
1742 p->rdev = repl;
1743 conf->mirrors[conf->raid_disks + number].rdev = NULL;
NeilBrowne2d59922013-06-12 11:01:22 +10001744 unfreeze_array(conf);
NeilBrownb014f142011-12-23 10:17:56 +11001745 clear_bit(WantReplacement, &rdev->flags);
NeilBrown8c7a2c22011-12-23 10:17:57 +11001746 } else
1747 clear_bit(WantReplacement, &rdev->flags);
Martin K. Petersena91a2782011-03-17 11:11:05 +01001748 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
1750abort:
1751
1752 print_conf(conf);
1753 return err;
1754}
1755
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001756static void end_sync_read(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001758 struct r1bio *r1_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
NeilBrown0fc280f2011-10-07 14:22:55 +11001760 update_head_pos(r1_bio->read_disk, r1_bio);
Namhyung Kimba3ae3b2011-10-07 14:22:53 +11001761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 /*
1763 * we have read a block, now it needs to be re-written,
1764 * or re-read if the read failed.
1765 * We don't do much here, just schedule handling by raid1d
1766 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001767 if (!bio->bi_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrownd11c1712006-01-06 00:20:26 -08001769
1770 if (atomic_dec_and_test(&r1_bio->remaining))
1771 reschedule_retry(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001774static void end_sync_write(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001776 int uptodate = !bio->bi_error;
NeilBrown9f2c9d12011-10-11 16:48:43 +11001777 struct r1bio *r1_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001778 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11001779 struct r1conf *conf = mddev->private;
NeilBrown4367af52011-07-28 11:31:49 +10001780 sector_t first_bad;
1781 int bad_sectors;
NeilBrown854abd72016-06-02 16:19:52 +10001782 struct md_rdev *rdev = conf->mirrors[find_bio_disk(r1_bio, bio)].rdev;
Namhyung Kimba3ae3b2011-10-07 14:22:53 +11001783
NeilBrown6b1117d2006-03-31 02:31:57 -08001784 if (!uptodate) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001785 sector_t sync_blocks = 0;
NeilBrown6b1117d2006-03-31 02:31:57 -08001786 sector_t s = r1_bio->sector;
1787 long sectors_to_go = r1_bio->sectors;
1788 /* make sure these bits doesn't get cleared. */
1789 do {
NeilBrown5e3db642006-07-10 04:44:18 -07001790 bitmap_end_sync(mddev->bitmap, s,
NeilBrown6b1117d2006-03-31 02:31:57 -08001791 &sync_blocks, 1);
1792 s += sync_blocks;
1793 sectors_to_go -= sync_blocks;
1794 } while (sectors_to_go > 0);
NeilBrown854abd72016-06-02 16:19:52 +10001795 set_bit(WriteErrorSeen, &rdev->flags);
1796 if (!test_and_set_bit(WantReplacement, &rdev->flags))
NeilBrown19d67162011-12-23 10:17:57 +11001797 set_bit(MD_RECOVERY_NEEDED, &
1798 mddev->recovery);
NeilBrownd8f05d22011-07-28 11:33:00 +10001799 set_bit(R1BIO_WriteError, &r1_bio->state);
NeilBrown854abd72016-06-02 16:19:52 +10001800 } else if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
NeilBrown3a9f28a2011-07-28 11:33:42 +10001801 &first_bad, &bad_sectors) &&
1802 !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
1803 r1_bio->sector,
1804 r1_bio->sectors,
1805 &first_bad, &bad_sectors)
1806 )
NeilBrown4367af52011-07-28 11:31:49 +10001807 set_bit(R1BIO_MadeGood, &r1_bio->state);
NeilBrowne3b97032005-08-04 12:53:34 -07001808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown4367af52011-07-28 11:31:49 +10001810 int s = r1_bio->sectors;
NeilBrownd8f05d22011-07-28 11:33:00 +10001811 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
1812 test_bit(R1BIO_WriteError, &r1_bio->state))
NeilBrown4367af52011-07-28 11:31:49 +10001813 reschedule_retry(r1_bio);
1814 else {
1815 put_buf(r1_bio);
1816 md_done_sync(mddev, s, uptodate);
1817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
1820
NeilBrown3cb03002011-10-11 16:45:26 +11001821static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrownd8f05d22011-07-28 11:33:00 +10001822 int sectors, struct page *page, int rw)
1823{
Mike Christie796a5cf2016-06-05 14:32:07 -05001824 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
NeilBrownd8f05d22011-07-28 11:33:00 +10001825 /* success */
1826 return 1;
NeilBrown19d67162011-12-23 10:17:57 +11001827 if (rw == WRITE) {
NeilBrownd8f05d22011-07-28 11:33:00 +10001828 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrown19d67162011-12-23 10:17:57 +11001829 if (!test_and_set_bit(WantReplacement,
1830 &rdev->flags))
1831 set_bit(MD_RECOVERY_NEEDED, &
1832 rdev->mddev->recovery);
1833 }
NeilBrownd8f05d22011-07-28 11:33:00 +10001834 /* need to record an error - either for the block or the device */
1835 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1836 md_error(rdev->mddev, rdev);
1837 return 0;
1838}
1839
NeilBrown9f2c9d12011-10-11 16:48:43 +11001840static int fix_sync_read_error(struct r1bio *r1_bio)
NeilBrowna68e5872011-05-11 14:40:44 +10001841{
1842 /* Try some synchronous reads of other devices to get
1843 * good data, much like with normal read errors. Only
1844 * read into the pages we already have so we don't
1845 * need to re-issue the read request.
1846 * We don't need to freeze the array, because being in an
1847 * active sync request, there is no normal IO, and
1848 * no overlapping syncs.
NeilBrown06f60382011-07-28 11:31:48 +10001849 * We don't need to check is_badblock() again as we
1850 * made sure that anything with a bad block in range
1851 * will have bi_end_io clear.
NeilBrowna68e5872011-05-11 14:40:44 +10001852 */
NeilBrownfd01b882011-10-11 16:47:53 +11001853 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11001854 struct r1conf *conf = mddev->private;
NeilBrowna68e5872011-05-11 14:40:44 +10001855 struct bio *bio = r1_bio->bios[r1_bio->read_disk];
1856 sector_t sect = r1_bio->sector;
1857 int sectors = r1_bio->sectors;
1858 int idx = 0;
NeilBrown2e52d442016-11-18 16:16:12 +11001859 struct md_rdev *rdev;
1860
1861 rdev = conf->mirrors[r1_bio->read_disk].rdev;
1862 if (test_bit(FailFast, &rdev->flags)) {
1863 /* Don't try recovering from here - just fail it
1864 * ... unless it is the last working device of course */
1865 md_error(mddev, rdev);
1866 if (test_bit(Faulty, &rdev->flags))
1867 /* Don't try to read from here, but make sure
1868 * put_buf does it's thing
1869 */
1870 bio->bi_end_io = end_sync_write;
1871 }
NeilBrowna68e5872011-05-11 14:40:44 +10001872
1873 while(sectors) {
1874 int s = sectors;
1875 int d = r1_bio->read_disk;
1876 int success = 0;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001877 int start;
NeilBrowna68e5872011-05-11 14:40:44 +10001878
1879 if (s > (PAGE_SIZE>>9))
1880 s = PAGE_SIZE >> 9;
1881 do {
1882 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1883 /* No rcu protection needed here devices
1884 * can only be removed when no resync is
1885 * active, and resync is currently active
1886 */
1887 rdev = conf->mirrors[d].rdev;
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001888 if (sync_page_io(rdev, sect, s<<9,
NeilBrowna68e5872011-05-11 14:40:44 +10001889 bio->bi_io_vec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05001890 REQ_OP_READ, 0, false)) {
NeilBrowna68e5872011-05-11 14:40:44 +10001891 success = 1;
1892 break;
1893 }
1894 }
1895 d++;
NeilBrown8f19ccb2011-12-23 10:17:56 +11001896 if (d == conf->raid_disks * 2)
NeilBrowna68e5872011-05-11 14:40:44 +10001897 d = 0;
1898 } while (!success && d != r1_bio->read_disk);
1899
NeilBrown78d7f5f2011-05-11 14:48:56 +10001900 if (!success) {
NeilBrowna68e5872011-05-11 14:40:44 +10001901 char b[BDEVNAME_SIZE];
NeilBrown3a9f28a2011-07-28 11:33:42 +10001902 int abort = 0;
1903 /* Cannot read from anywhere, this block is lost.
1904 * Record a bad block on each device. If that doesn't
1905 * work just disable and interrupt the recovery.
1906 * Don't fail devices as that won't really help.
1907 */
NeilBrown1d41c212016-11-02 14:16:50 +11001908 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
1909 mdname(mddev),
1910 bdevname(bio->bi_bdev, b),
1911 (unsigned long long)r1_bio->sector);
NeilBrown8f19ccb2011-12-23 10:17:56 +11001912 for (d = 0; d < conf->raid_disks * 2; d++) {
NeilBrown3a9f28a2011-07-28 11:33:42 +10001913 rdev = conf->mirrors[d].rdev;
1914 if (!rdev || test_bit(Faulty, &rdev->flags))
1915 continue;
1916 if (!rdev_set_badblocks(rdev, sect, s, 0))
1917 abort = 1;
1918 }
1919 if (abort) {
NeilBrownd890fa22011-10-26 11:54:39 +11001920 conf->recovery_disabled =
1921 mddev->recovery_disabled;
NeilBrown3a9f28a2011-07-28 11:33:42 +10001922 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1923 md_done_sync(mddev, r1_bio->sectors, 0);
1924 put_buf(r1_bio);
1925 return 0;
1926 }
1927 /* Try next page */
1928 sectors -= s;
1929 sect += s;
1930 idx++;
1931 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10001932 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001933
1934 start = d;
1935 /* write it back and re-read */
1936 while (d != r1_bio->read_disk) {
1937 if (d == 0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11001938 d = conf->raid_disks * 2;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001939 d--;
1940 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1941 continue;
1942 rdev = conf->mirrors[d].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10001943 if (r1_sync_page_io(rdev, sect, s,
1944 bio->bi_io_vec[idx].bv_page,
1945 WRITE) == 0) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10001946 r1_bio->bios[d]->bi_end_io = NULL;
1947 rdev_dec_pending(rdev, mddev);
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001948 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001949 }
1950 d = start;
1951 while (d != r1_bio->read_disk) {
1952 if (d == 0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11001953 d = conf->raid_disks * 2;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001954 d--;
1955 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1956 continue;
1957 rdev = conf->mirrors[d].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10001958 if (r1_sync_page_io(rdev, sect, s,
1959 bio->bi_io_vec[idx].bv_page,
1960 READ) != 0)
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001961 atomic_add(s, &rdev->corrected_errors);
NeilBrown78d7f5f2011-05-11 14:48:56 +10001962 }
NeilBrowna68e5872011-05-11 14:40:44 +10001963 sectors -= s;
1964 sect += s;
1965 idx ++;
1966 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001967 set_bit(R1BIO_Uptodate, &r1_bio->state);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001968 bio->bi_error = 0;
NeilBrowna68e5872011-05-11 14:40:44 +10001969 return 1;
1970}
1971
NeilBrownc95e6382014-09-09 13:54:11 +10001972static void process_checks(struct r1bio *r1_bio)
NeilBrowna68e5872011-05-11 14:40:44 +10001973{
1974 /* We have read all readable devices. If we haven't
1975 * got the block, then there is no hope left.
1976 * If we have, then we want to do a comparison
1977 * and skip the write if everything is the same.
1978 * If any blocks failed to read, then we need to
1979 * attempt an over-write
1980 */
NeilBrownfd01b882011-10-11 16:47:53 +11001981 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11001982 struct r1conf *conf = mddev->private;
NeilBrowna68e5872011-05-11 14:40:44 +10001983 int primary;
1984 int i;
majianpengf4380a92012-04-12 16:04:47 +10001985 int vcnt;
NeilBrowna68e5872011-05-11 14:40:44 +10001986
NeilBrown30bc9b52013-07-17 15:19:29 +10001987 /* Fix variable parts of all bios */
1988 vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9);
1989 for (i = 0; i < conf->raid_disks * 2; i++) {
1990 int j;
1991 int size;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001992 int error;
NeilBrown30bc9b52013-07-17 15:19:29 +10001993 struct bio *b = r1_bio->bios[i];
1994 if (b->bi_end_io != end_sync_read)
1995 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001996 /* fixup the bio for reuse, but preserve errno */
1997 error = b->bi_error;
NeilBrown30bc9b52013-07-17 15:19:29 +10001998 bio_reset(b);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001999 b->bi_error = error;
NeilBrown30bc9b52013-07-17 15:19:29 +10002000 b->bi_vcnt = vcnt;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002001 b->bi_iter.bi_size = r1_bio->sectors << 9;
2002 b->bi_iter.bi_sector = r1_bio->sector +
NeilBrown30bc9b52013-07-17 15:19:29 +10002003 conf->mirrors[i].rdev->data_offset;
2004 b->bi_bdev = conf->mirrors[i].rdev->bdev;
2005 b->bi_end_io = end_sync_read;
2006 b->bi_private = r1_bio;
2007
Kent Overstreet4f024f32013-10-11 15:44:27 -07002008 size = b->bi_iter.bi_size;
NeilBrown30bc9b52013-07-17 15:19:29 +10002009 for (j = 0; j < vcnt ; j++) {
2010 struct bio_vec *bi;
2011 bi = &b->bi_io_vec[j];
2012 bi->bv_offset = 0;
2013 if (size > PAGE_SIZE)
2014 bi->bv_len = PAGE_SIZE;
2015 else
2016 bi->bv_len = size;
2017 size -= PAGE_SIZE;
2018 }
2019 }
NeilBrown8f19ccb2011-12-23 10:17:56 +11002020 for (primary = 0; primary < conf->raid_disks * 2; primary++)
NeilBrowna68e5872011-05-11 14:40:44 +10002021 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002022 !r1_bio->bios[primary]->bi_error) {
NeilBrowna68e5872011-05-11 14:40:44 +10002023 r1_bio->bios[primary]->bi_end_io = NULL;
2024 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
2025 break;
2026 }
2027 r1_bio->read_disk = primary;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002028 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10002029 int j;
NeilBrown78d7f5f2011-05-11 14:48:56 +10002030 struct bio *pbio = r1_bio->bios[primary];
2031 struct bio *sbio = r1_bio->bios[i];
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002032 int error = sbio->bi_error;
NeilBrowna68e5872011-05-11 14:40:44 +10002033
Kent Overstreet2aabaa62012-09-11 11:26:12 -07002034 if (sbio->bi_end_io != end_sync_read)
NeilBrown78d7f5f2011-05-11 14:48:56 +10002035 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002036 /* Now we can 'fixup' the error value */
2037 sbio->bi_error = 0;
NeilBrowna68e5872011-05-11 14:40:44 +10002038
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002039 if (!error) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10002040 for (j = vcnt; j-- ; ) {
2041 struct page *p, *s;
2042 p = pbio->bi_io_vec[j].bv_page;
2043 s = sbio->bi_io_vec[j].bv_page;
2044 if (memcmp(page_address(p),
2045 page_address(s),
NeilBrown5020ad72012-04-02 01:39:05 +10002046 sbio->bi_io_vec[j].bv_len))
NeilBrown78d7f5f2011-05-11 14:48:56 +10002047 break;
NeilBrowna68e5872011-05-11 14:40:44 +10002048 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10002049 } else
2050 j = 0;
2051 if (j >= 0)
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002052 atomic64_add(r1_bio->sectors, &mddev->resync_mismatches);
NeilBrown78d7f5f2011-05-11 14:48:56 +10002053 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002054 && !error)) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10002055 /* No need to write to this device. */
2056 sbio->bi_end_io = NULL;
2057 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
2058 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10002059 }
Kent Overstreetd3b45c22012-09-10 13:49:33 -07002060
2061 bio_copy_data(sbio, pbio);
NeilBrown78d7f5f2011-05-11 14:48:56 +10002062 }
NeilBrowna68e5872011-05-11 14:40:44 +10002063}
2064
NeilBrown9f2c9d12011-10-11 16:48:43 +11002065static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
NeilBrowne8096362011-10-11 16:49:05 +11002067 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 int i;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002069 int disks = conf->raid_disks * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 struct bio *bio, *wbio;
2071
2072 bio = r1_bio->bios[r1_bio->read_disk];
2073
NeilBrowna68e5872011-05-11 14:40:44 +10002074 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
2075 /* ouch - failed to read all of that. */
2076 if (!fix_sync_read_error(r1_bio))
2077 return;
NeilBrown7ca78d52011-05-11 14:50:37 +10002078
2079 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrownc95e6382014-09-09 13:54:11 +10002080 process_checks(r1_bio);
2081
NeilBrownd11c1712006-01-06 00:20:26 -08002082 /*
2083 * schedule writes
2084 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 atomic_set(&r1_bio->remaining, 1);
2086 for (i = 0; i < disks ; i++) {
2087 wbio = r1_bio->bios[i];
NeilBrown3e198f72006-01-06 00:20:21 -08002088 if (wbio->bi_end_io == NULL ||
2089 (wbio->bi_end_io == end_sync_read &&
2090 (i == r1_bio->read_disk ||
2091 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 continue;
2093
Mike Christie796a5cf2016-06-05 14:32:07 -05002094 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
NeilBrown212e7eb2016-11-18 16:16:12 +11002095 if (test_bit(FailFast, &conf->mirrors[i].rdev->flags))
2096 wbio->bi_opf |= MD_FAILFAST;
2097
NeilBrown3e198f72006-01-06 00:20:21 -08002098 wbio->bi_end_io = end_sync_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 atomic_inc(&r1_bio->remaining);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002100 md_sync_acct(conf->mirrors[i].rdev->bdev, bio_sectors(wbio));
NeilBrown191ea9b2005-06-21 17:17:23 -07002101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 generic_make_request(wbio);
2103 }
2104
2105 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002106 /* if we're here, all write(s) have completed, so clean up */
NeilBrown58e94ae2012-07-19 15:59:18 +10002107 int s = r1_bio->sectors;
2108 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2109 test_bit(R1BIO_WriteError, &r1_bio->state))
2110 reschedule_retry(r1_bio);
2111 else {
2112 put_buf(r1_bio);
2113 md_done_sync(mddev, s, 1);
2114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
2116}
2117
2118/*
2119 * This is a kernel thread which:
2120 *
2121 * 1. Retries failed read operations on working mirrors.
2122 * 2. Updates the raid superblock when problems encounter.
NeilBrownd2eb35a2011-07-28 11:31:48 +10002123 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 */
2125
NeilBrowne8096362011-10-11 16:49:05 +11002126static void fix_read_error(struct r1conf *conf, int read_disk,
NeilBrown867868f2006-10-03 01:15:51 -07002127 sector_t sect, int sectors)
2128{
NeilBrownfd01b882011-10-11 16:47:53 +11002129 struct mddev *mddev = conf->mddev;
NeilBrown867868f2006-10-03 01:15:51 -07002130 while(sectors) {
2131 int s = sectors;
2132 int d = read_disk;
2133 int success = 0;
2134 int start;
NeilBrown3cb03002011-10-11 16:45:26 +11002135 struct md_rdev *rdev;
NeilBrown867868f2006-10-03 01:15:51 -07002136
2137 if (s > (PAGE_SIZE>>9))
2138 s = PAGE_SIZE >> 9;
2139
2140 do {
NeilBrownd2eb35a2011-07-28 11:31:48 +10002141 sector_t first_bad;
2142 int bad_sectors;
2143
NeilBrown707a6a42016-06-02 16:19:52 +10002144 rcu_read_lock();
2145 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002146 if (rdev &&
majianpengda8840a2012-05-22 13:55:03 +10002147 (test_bit(In_sync, &rdev->flags) ||
2148 (!test_bit(Faulty, &rdev->flags) &&
2149 rdev->recovery_offset >= sect + s)) &&
NeilBrownd2eb35a2011-07-28 11:31:48 +10002150 is_badblock(rdev, sect, s,
NeilBrown707a6a42016-06-02 16:19:52 +10002151 &first_bad, &bad_sectors) == 0) {
2152 atomic_inc(&rdev->nr_pending);
2153 rcu_read_unlock();
2154 if (sync_page_io(rdev, sect, s<<9,
Mike Christie796a5cf2016-06-05 14:32:07 -05002155 conf->tmppage, REQ_OP_READ, 0, false))
NeilBrown707a6a42016-06-02 16:19:52 +10002156 success = 1;
2157 rdev_dec_pending(rdev, mddev);
2158 if (success)
2159 break;
2160 } else
2161 rcu_read_unlock();
2162 d++;
2163 if (d == conf->raid_disks * 2)
2164 d = 0;
NeilBrown867868f2006-10-03 01:15:51 -07002165 } while (!success && d != read_disk);
2166
2167 if (!success) {
NeilBrownd8f05d22011-07-28 11:33:00 +10002168 /* Cannot read from anywhere - mark it bad */
NeilBrown3cb03002011-10-11 16:45:26 +11002169 struct md_rdev *rdev = conf->mirrors[read_disk].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10002170 if (!rdev_set_badblocks(rdev, sect, s, 0))
2171 md_error(mddev, rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002172 break;
2173 }
2174 /* write it back and re-read */
2175 start = d;
2176 while (d != read_disk) {
2177 if (d==0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002178 d = conf->raid_disks * 2;
NeilBrown867868f2006-10-03 01:15:51 -07002179 d--;
NeilBrown707a6a42016-06-02 16:19:52 +10002180 rcu_read_lock();
2181 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002182 if (rdev &&
NeilBrown707a6a42016-06-02 16:19:52 +10002183 !test_bit(Faulty, &rdev->flags)) {
2184 atomic_inc(&rdev->nr_pending);
2185 rcu_read_unlock();
NeilBrownd8f05d22011-07-28 11:33:00 +10002186 r1_sync_page_io(rdev, sect, s,
2187 conf->tmppage, WRITE);
NeilBrown707a6a42016-06-02 16:19:52 +10002188 rdev_dec_pending(rdev, mddev);
2189 } else
2190 rcu_read_unlock();
NeilBrown867868f2006-10-03 01:15:51 -07002191 }
2192 d = start;
2193 while (d != read_disk) {
2194 char b[BDEVNAME_SIZE];
2195 if (d==0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002196 d = conf->raid_disks * 2;
NeilBrown867868f2006-10-03 01:15:51 -07002197 d--;
NeilBrown707a6a42016-06-02 16:19:52 +10002198 rcu_read_lock();
2199 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002200 if (rdev &&
NeilBrownb8cb6b42014-09-18 11:09:04 +10002201 !test_bit(Faulty, &rdev->flags)) {
NeilBrown707a6a42016-06-02 16:19:52 +10002202 atomic_inc(&rdev->nr_pending);
2203 rcu_read_unlock();
NeilBrownd8f05d22011-07-28 11:33:00 +10002204 if (r1_sync_page_io(rdev, sect, s,
2205 conf->tmppage, READ)) {
NeilBrown867868f2006-10-03 01:15:51 -07002206 atomic_add(s, &rdev->corrected_errors);
NeilBrown1d41c212016-11-02 14:16:50 +11002207 pr_info("md/raid1:%s: read error corrected (%d sectors at %llu on %s)\n",
2208 mdname(mddev), s,
2209 (unsigned long long)(sect +
2210 rdev->data_offset),
2211 bdevname(rdev->bdev, b));
NeilBrown867868f2006-10-03 01:15:51 -07002212 }
NeilBrown707a6a42016-06-02 16:19:52 +10002213 rdev_dec_pending(rdev, mddev);
2214 } else
2215 rcu_read_unlock();
NeilBrown867868f2006-10-03 01:15:51 -07002216 }
2217 sectors -= s;
2218 sect += s;
2219 }
2220}
2221
NeilBrown9f2c9d12011-10-11 16:48:43 +11002222static int narrow_write_error(struct r1bio *r1_bio, int i)
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002223{
NeilBrownfd01b882011-10-11 16:47:53 +11002224 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11002225 struct r1conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002226 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002227
2228 /* bio has the data to be written to device 'i' where
2229 * we just recently had a write error.
2230 * We repeatedly clone the bio and trim down to one block,
2231 * then try the write. Where the write fails we record
2232 * a bad block.
2233 * It is conceivable that the bio doesn't exactly align with
2234 * blocks. We must handle this somehow.
2235 *
2236 * We currently own a reference on the rdev.
2237 */
2238
2239 int block_sectors;
2240 sector_t sector;
2241 int sectors;
2242 int sect_to_write = r1_bio->sectors;
2243 int ok = 1;
2244
2245 if (rdev->badblocks.shift < 0)
2246 return 0;
2247
Nate Daileyab713cd2015-02-12 12:02:09 -05002248 block_sectors = roundup(1 << rdev->badblocks.shift,
2249 bdev_logical_block_size(rdev->bdev) >> 9);
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002250 sector = r1_bio->sector;
2251 sectors = ((sector + block_sectors)
2252 & ~(sector_t)(block_sectors - 1))
2253 - sector;
2254
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002255 while (sect_to_write) {
2256 struct bio *wbio;
2257 if (sectors > sect_to_write)
2258 sectors = sect_to_write;
2259 /* Write at 'sector' for 'sectors'*/
2260
Kent Overstreetb7838632012-09-10 15:17:11 -07002261 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
2262 unsigned vcnt = r1_bio->behind_page_count;
2263 struct bio_vec *vec = r1_bio->behind_bvecs;
2264
2265 while (!vec->bv_page) {
2266 vec++;
2267 vcnt--;
2268 }
2269
2270 wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev);
2271 memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec));
2272
2273 wbio->bi_vcnt = vcnt;
2274 } else {
2275 wbio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
2276 }
2277
Mike Christie796a5cf2016-06-05 14:32:07 -05002278 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002279 wbio->bi_iter.bi_sector = r1_bio->sector;
2280 wbio->bi_iter.bi_size = r1_bio->sectors << 9;
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002281
Kent Overstreet6678d832013-08-07 11:14:32 -07002282 bio_trim(wbio, sector - r1_bio->sector, sectors);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002283 wbio->bi_iter.bi_sector += rdev->data_offset;
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002284 wbio->bi_bdev = rdev->bdev;
Mike Christie4e49ea42016-06-05 14:31:41 -05002285
2286 if (submit_bio_wait(wbio) < 0)
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002287 /* failure! */
2288 ok = rdev_set_badblocks(rdev, sector,
2289 sectors, 0)
2290 && ok;
2291
2292 bio_put(wbio);
2293 sect_to_write -= sectors;
2294 sector += sectors;
2295 sectors = block_sectors;
2296 }
2297 return ok;
2298}
2299
NeilBrowne8096362011-10-11 16:49:05 +11002300static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002301{
2302 int m;
2303 int s = r1_bio->sectors;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002304 for (m = 0; m < conf->raid_disks * 2 ; m++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002305 struct md_rdev *rdev = conf->mirrors[m].rdev;
NeilBrown62096bc2011-07-28 11:38:13 +10002306 struct bio *bio = r1_bio->bios[m];
2307 if (bio->bi_end_io == NULL)
2308 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002309 if (!bio->bi_error &&
NeilBrown62096bc2011-07-28 11:38:13 +10002310 test_bit(R1BIO_MadeGood, &r1_bio->state)) {
NeilBrownc6563a82012-05-21 09:27:00 +10002311 rdev_clear_badblocks(rdev, r1_bio->sector, s, 0);
NeilBrown62096bc2011-07-28 11:38:13 +10002312 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002313 if (bio->bi_error &&
NeilBrown62096bc2011-07-28 11:38:13 +10002314 test_bit(R1BIO_WriteError, &r1_bio->state)) {
2315 if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
2316 md_error(conf->mddev, rdev);
2317 }
2318 }
2319 put_buf(r1_bio);
2320 md_done_sync(conf->mddev, s, 1);
2321}
2322
NeilBrowne8096362011-10-11 16:49:05 +11002323static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002324{
2325 int m;
NeilBrown55ce74d2015-08-14 11:11:10 +10002326 bool fail = false;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002327 for (m = 0; m < conf->raid_disks * 2 ; m++)
NeilBrown62096bc2011-07-28 11:38:13 +10002328 if (r1_bio->bios[m] == IO_MADE_GOOD) {
NeilBrown3cb03002011-10-11 16:45:26 +11002329 struct md_rdev *rdev = conf->mirrors[m].rdev;
NeilBrown62096bc2011-07-28 11:38:13 +10002330 rdev_clear_badblocks(rdev,
2331 r1_bio->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10002332 r1_bio->sectors, 0);
NeilBrown62096bc2011-07-28 11:38:13 +10002333 rdev_dec_pending(rdev, conf->mddev);
2334 } else if (r1_bio->bios[m] != NULL) {
2335 /* This drive got a write error. We need to
2336 * narrow down and record precise write
2337 * errors.
2338 */
NeilBrown55ce74d2015-08-14 11:11:10 +10002339 fail = true;
NeilBrown62096bc2011-07-28 11:38:13 +10002340 if (!narrow_write_error(r1_bio, m)) {
2341 md_error(conf->mddev,
2342 conf->mirrors[m].rdev);
2343 /* an I/O failed, we can't clear the bitmap */
2344 set_bit(R1BIO_Degraded, &r1_bio->state);
2345 }
2346 rdev_dec_pending(conf->mirrors[m].rdev,
2347 conf->mddev);
2348 }
NeilBrown55ce74d2015-08-14 11:11:10 +10002349 if (fail) {
2350 spin_lock_irq(&conf->device_lock);
2351 list_add(&r1_bio->retry_list, &conf->bio_end_io_list);
Nate Daileyccfc7bf2016-02-29 10:43:58 -05002352 conf->nr_queued++;
NeilBrown55ce74d2015-08-14 11:11:10 +10002353 spin_unlock_irq(&conf->device_lock);
2354 md_wakeup_thread(conf->mddev->thread);
NeilBrownbd8688a2015-10-24 16:02:16 +11002355 } else {
2356 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2357 close_write(r1_bio);
NeilBrown55ce74d2015-08-14 11:11:10 +10002358 raid_end_bio_io(r1_bio);
NeilBrownbd8688a2015-10-24 16:02:16 +11002359 }
NeilBrown62096bc2011-07-28 11:38:13 +10002360}
2361
NeilBrowne8096362011-10-11 16:49:05 +11002362static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002363{
2364 int disk;
2365 int max_sectors;
NeilBrownfd01b882011-10-11 16:47:53 +11002366 struct mddev *mddev = conf->mddev;
NeilBrown62096bc2011-07-28 11:38:13 +10002367 struct bio *bio;
2368 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11002369 struct md_rdev *rdev;
NeilBrown109e3762016-11-18 13:22:04 +11002370 dev_t bio_dev;
2371 sector_t bio_sector;
NeilBrown62096bc2011-07-28 11:38:13 +10002372
2373 clear_bit(R1BIO_ReadError, &r1_bio->state);
2374 /* we got a read error. Maybe the drive is bad. Maybe just
2375 * the block and we can fix it.
2376 * We freeze all other IO, and try reading the block from
2377 * other devices. When we find one, we re-write
2378 * and check it that fixes the read error.
2379 * This is all done synchronously while the array is
2380 * frozen
2381 */
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002382
2383 bio = r1_bio->bios[r1_bio->read_disk];
2384 bdevname(bio->bi_bdev, b);
NeilBrown109e3762016-11-18 13:22:04 +11002385 bio_dev = bio->bi_bdev->bd_dev;
2386 bio_sector = conf->mirrors[r1_bio->read_disk].rdev->data_offset + r1_bio->sector;
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002387 bio_put(bio);
2388 r1_bio->bios[r1_bio->read_disk] = NULL;
2389
NeilBrown2e52d442016-11-18 16:16:12 +11002390 rdev = conf->mirrors[r1_bio->read_disk].rdev;
2391 if (mddev->ro == 0
2392 && !test_bit(FailFast, &rdev->flags)) {
NeilBrowne2d59922013-06-12 11:01:22 +10002393 freeze_array(conf, 1);
NeilBrown62096bc2011-07-28 11:38:13 +10002394 fix_read_error(conf, r1_bio->read_disk,
2395 r1_bio->sector, r1_bio->sectors);
2396 unfreeze_array(conf);
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002397 } else {
2398 r1_bio->bios[r1_bio->read_disk] = IO_BLOCKED;
2399 }
2400
NeilBrown2e52d442016-11-18 16:16:12 +11002401 rdev_dec_pending(rdev, conf->mddev);
NeilBrown62096bc2011-07-28 11:38:13 +10002402
NeilBrown62096bc2011-07-28 11:38:13 +10002403read_more:
2404 disk = read_balance(conf, r1_bio, &max_sectors);
2405 if (disk == -1) {
NeilBrown1d41c212016-11-02 14:16:50 +11002406 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
2407 mdname(mddev), b, (unsigned long long)r1_bio->sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002408 raid_end_bio_io(r1_bio);
2409 } else {
2410 const unsigned long do_sync
Jens Axboe1eff9d32016-08-05 15:35:16 -06002411 = r1_bio->master_bio->bi_opf & REQ_SYNC;
NeilBrown62096bc2011-07-28 11:38:13 +10002412 r1_bio->read_disk = disk;
2413 bio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002414 bio_trim(bio, r1_bio->sector - bio->bi_iter.bi_sector,
2415 max_sectors);
NeilBrown62096bc2011-07-28 11:38:13 +10002416 r1_bio->bios[r1_bio->read_disk] = bio;
2417 rdev = conf->mirrors[disk].rdev;
NeilBrown1d41c212016-11-02 14:16:50 +11002418 pr_info_ratelimited("md/raid1:%s: redirecting sector %llu to other mirror: %s\n",
2419 mdname(mddev),
2420 (unsigned long long)r1_bio->sector,
2421 bdevname(rdev->bdev, b));
Kent Overstreet4f024f32013-10-11 15:44:27 -07002422 bio->bi_iter.bi_sector = r1_bio->sector + rdev->data_offset;
NeilBrown62096bc2011-07-28 11:38:13 +10002423 bio->bi_bdev = rdev->bdev;
2424 bio->bi_end_io = raid1_end_read_request;
Mike Christie796a5cf2016-06-05 14:32:07 -05002425 bio_set_op_attrs(bio, REQ_OP_READ, do_sync);
NeilBrown2e52d442016-11-18 16:16:12 +11002426 if (test_bit(FailFast, &rdev->flags) &&
2427 test_bit(R1BIO_FailFast, &r1_bio->state))
2428 bio->bi_opf |= MD_FAILFAST;
NeilBrown62096bc2011-07-28 11:38:13 +10002429 bio->bi_private = r1_bio;
2430 if (max_sectors < r1_bio->sectors) {
2431 /* Drat - have to split this up more */
2432 struct bio *mbio = r1_bio->master_bio;
2433 int sectors_handled = (r1_bio->sector + max_sectors
Kent Overstreet4f024f32013-10-11 15:44:27 -07002434 - mbio->bi_iter.bi_sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002435 r1_bio->sectors = max_sectors;
2436 spin_lock_irq(&conf->device_lock);
2437 if (mbio->bi_phys_segments == 0)
2438 mbio->bi_phys_segments = 2;
2439 else
2440 mbio->bi_phys_segments++;
2441 spin_unlock_irq(&conf->device_lock);
NeilBrown109e3762016-11-18 13:22:04 +11002442 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2443 bio, bio_dev, bio_sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002444 generic_make_request(bio);
2445 bio = NULL;
2446
2447 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
2448
2449 r1_bio->master_bio = mbio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002450 r1_bio->sectors = bio_sectors(mbio) - sectors_handled;
NeilBrown62096bc2011-07-28 11:38:13 +10002451 r1_bio->state = 0;
2452 set_bit(R1BIO_ReadError, &r1_bio->state);
2453 r1_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002454 r1_bio->sector = mbio->bi_iter.bi_sector +
2455 sectors_handled;
NeilBrown62096bc2011-07-28 11:38:13 +10002456
2457 goto read_more;
NeilBrown109e3762016-11-18 13:22:04 +11002458 } else {
2459 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2460 bio, bio_dev, bio_sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002461 generic_make_request(bio);
NeilBrown109e3762016-11-18 13:22:04 +11002462 }
NeilBrown62096bc2011-07-28 11:38:13 +10002463 }
2464}
2465
Shaohua Li4ed87312012-10-11 13:34:00 +11002466static void raid1d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
Shaohua Li4ed87312012-10-11 13:34:00 +11002468 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002469 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 unsigned long flags;
NeilBrowne8096362011-10-11 16:49:05 +11002471 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002473 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
2475 md_check_recovery(mddev);
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002476
NeilBrown55ce74d2015-08-14 11:11:10 +10002477 if (!list_empty_careful(&conf->bio_end_io_list) &&
Shaohua Li29530792016-12-08 15:48:19 -08002478 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrown55ce74d2015-08-14 11:11:10 +10002479 LIST_HEAD(tmp);
2480 spin_lock_irqsave(&conf->device_lock, flags);
Shaohua Li29530792016-12-08 15:48:19 -08002481 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
Nate Daileyccfc7bf2016-02-29 10:43:58 -05002482 while (!list_empty(&conf->bio_end_io_list)) {
2483 list_move(conf->bio_end_io_list.prev, &tmp);
2484 conf->nr_queued--;
2485 }
NeilBrown55ce74d2015-08-14 11:11:10 +10002486 }
2487 spin_unlock_irqrestore(&conf->device_lock, flags);
2488 while (!list_empty(&tmp)) {
Mikulas Patockaa4527442015-10-01 15:17:43 -04002489 r1_bio = list_first_entry(&tmp, struct r1bio,
2490 retry_list);
NeilBrown55ce74d2015-08-14 11:11:10 +10002491 list_del(&r1_bio->retry_list);
NeilBrownbd8688a2015-10-24 16:02:16 +11002492 if (mddev->degraded)
2493 set_bit(R1BIO_Degraded, &r1_bio->state);
2494 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2495 close_write(r1_bio);
NeilBrown55ce74d2015-08-14 11:11:10 +10002496 raid_end_bio_io(r1_bio);
2497 }
2498 }
2499
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002500 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002502
NeilBrown0021b7b2012-07-31 09:08:14 +02002503 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002506 if (list_empty(head)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002507 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002509 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002510 r1_bio = list_entry(head->prev, struct r1bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 list_del(head->prev);
NeilBrownddaf22a2006-01-06 00:20:19 -08002512 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 spin_unlock_irqrestore(&conf->device_lock, flags);
2514
2515 mddev = r1_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002516 conf = mddev->private;
NeilBrown4367af52011-07-28 11:31:49 +10002517 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
NeilBrownd8f05d22011-07-28 11:33:00 +10002518 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
NeilBrown62096bc2011-07-28 11:38:13 +10002519 test_bit(R1BIO_WriteError, &r1_bio->state))
2520 handle_sync_write_finished(conf, r1_bio);
2521 else
NeilBrown4367af52011-07-28 11:31:49 +10002522 sync_request_write(mddev, r1_bio);
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002523 } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
NeilBrown62096bc2011-07-28 11:38:13 +10002524 test_bit(R1BIO_WriteError, &r1_bio->state))
2525 handle_write_finished(conf, r1_bio);
2526 else if (test_bit(R1BIO_ReadError, &r1_bio->state))
2527 handle_read_error(conf, r1_bio);
2528 else
NeilBrownd2eb35a2011-07-28 11:31:48 +10002529 /* just a partial read to be scheduled from separate
2530 * context
2531 */
2532 generic_make_request(r1_bio->bios[r1_bio->read_disk]);
NeilBrown62096bc2011-07-28 11:38:13 +10002533
NeilBrown1d9d5242009-10-16 15:55:32 +11002534 cond_resched();
Shaohua Li29530792016-12-08 15:48:19 -08002535 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
NeilBrownde393cd2011-07-28 11:31:48 +10002536 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002538 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
2540
NeilBrowne8096362011-10-11 16:49:05 +11002541static int init_resync(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
2543 int buffs;
2544
2545 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhenn9e77c482006-04-01 01:08:49 +02002546 BUG_ON(conf->r1buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
2548 conf->poolinfo);
2549 if (!conf->r1buf_pool)
2550 return -ENOMEM;
2551 conf->next_resync = 0;
2552 return 0;
2553}
2554
2555/*
2556 * perform a "sync" on one "block"
2557 *
2558 * We need to make sure that no normal I/O request - particularly write
2559 * requests - conflict with active sync requests.
2560 *
2561 * This is achieved by tracking pending requests and a 'barrier' concept
2562 * that can be installed to exclude normal IO requests.
2563 */
2564
Shaohua Li849674e2016-01-20 13:52:20 -08002565static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr,
2566 int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
NeilBrowne8096362011-10-11 16:49:05 +11002568 struct r1conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002569 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 struct bio *bio;
2571 sector_t max_sector, nr_sectors;
NeilBrown3e198f72006-01-06 00:20:21 -08002572 int disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 int i;
NeilBrown3e198f72006-01-06 00:20:21 -08002574 int wonly = -1;
2575 int write_targets = 0, read_targets = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11002576 sector_t sync_blocks;
NeilBrowne3b97032005-08-04 12:53:34 -07002577 int still_degraded = 0;
NeilBrown06f60382011-07-28 11:31:48 +10002578 int good_sectors = RESYNC_SECTORS;
2579 int min_bad = 0; /* number of sectors that are bad in all devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581 if (!conf->r1buf_pool)
2582 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Andre Noll58c0fed2009-03-31 14:33:13 +11002585 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 if (sector_nr >= max_sector) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002587 /* If we aborted, we need to abort the
2588 * sync on the 'current' bitmap chunk (there will
2589 * only be one in raid1 resync.
2590 * We can find the current addess in mddev->curr_resync
2591 */
NeilBrown6a806c52005-07-15 03:56:35 -07002592 if (mddev->curr_resync < max_sector) /* aborted */
2593 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
NeilBrown191ea9b2005-06-21 17:17:23 -07002594 &sync_blocks, 1);
NeilBrown6a806c52005-07-15 03:56:35 -07002595 else /* completed sync */
NeilBrown191ea9b2005-06-21 17:17:23 -07002596 conf->fullsync = 0;
NeilBrown6a806c52005-07-15 03:56:35 -07002597
2598 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 close_sync(conf);
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002600
2601 if (mddev_is_clustered(mddev)) {
2602 conf->cluster_sync_low = 0;
2603 conf->cluster_sync_high = 0;
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 return 0;
2606 }
2607
NeilBrown07d84d102006-06-26 00:27:56 -07002608 if (mddev->bitmap == NULL &&
2609 mddev->recovery_cp == MaxSector &&
NeilBrown6394cca2006-08-27 01:23:50 -07002610 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown07d84d102006-06-26 00:27:56 -07002611 conf->fullsync == 0) {
2612 *skipped = 1;
2613 return max_sector - sector_nr;
2614 }
NeilBrown6394cca2006-08-27 01:23:50 -07002615 /* before building a request, check if we can skip these blocks..
2616 * This call the bitmap_start_sync doesn't actually record anything
2617 */
NeilBrowne3b97032005-08-04 12:53:34 -07002618 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrowne5de485f2005-11-08 21:39:38 -08002619 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002620 /* We can skip this block, and probably several more */
2621 *skipped = 1;
2622 return sync_blocks;
2623 }
NeilBrown17999be2006-01-06 00:20:12 -08002624
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02002625 /*
2626 * If there is non-resync activity waiting for a turn, then let it
2627 * though before starting on this new sync request.
2628 */
2629 if (conf->nr_waiting)
2630 schedule_timeout_uninterruptible(1);
2631
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002632 /* we are incrementing sector_nr below. To be safe, we check against
2633 * sector_nr + two times RESYNC_SECTORS
2634 */
2635
2636 bitmap_cond_end_sync(mddev->bitmap, sector_nr,
2637 mddev_is_clustered(mddev) && (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
NeilBrown1c4588e2010-10-26 17:41:22 +11002638 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
NeilBrown17999be2006-01-06 00:20:12 -08002639
NeilBrownc2fd4c92014-09-10 16:01:24 +10002640 raise_barrier(conf, sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
NeilBrown3e198f72006-01-06 00:20:21 -08002642 rcu_read_lock();
2643 /*
2644 * If we get a correctably read error during resync or recovery,
2645 * we might want to read from a different device. So we
2646 * flag all drives that could conceivably be read from for READ,
2647 * and any others (which will be non-In_sync devices) for WRITE.
2648 * If a read fails, we try reading from something else for which READ
2649 * is OK.
2650 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 r1_bio->mddev = mddev;
2653 r1_bio->sector = sector_nr;
NeilBrown191ea9b2005-06-21 17:17:23 -07002654 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 set_bit(R1BIO_IsSync, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
NeilBrown8f19ccb2011-12-23 10:17:56 +11002657 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002658 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 bio = r1_bio->bios[i];
Kent Overstreet2aabaa62012-09-11 11:26:12 -07002660 bio_reset(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
NeilBrown3e198f72006-01-06 00:20:21 -08002662 rdev = rcu_dereference(conf->mirrors[i].rdev);
2663 if (rdev == NULL ||
NeilBrown06f60382011-07-28 11:31:48 +10002664 test_bit(Faulty, &rdev->flags)) {
NeilBrown8f19ccb2011-12-23 10:17:56 +11002665 if (i < conf->raid_disks)
2666 still_degraded = 1;
NeilBrown3e198f72006-01-06 00:20:21 -08002667 } else if (!test_bit(In_sync, &rdev->flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05002668 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 bio->bi_end_io = end_sync_write;
2670 write_targets ++;
NeilBrown3e198f72006-01-06 00:20:21 -08002671 } else {
2672 /* may need to read from here */
NeilBrown06f60382011-07-28 11:31:48 +10002673 sector_t first_bad = MaxSector;
2674 int bad_sectors;
2675
2676 if (is_badblock(rdev, sector_nr, good_sectors,
2677 &first_bad, &bad_sectors)) {
2678 if (first_bad > sector_nr)
2679 good_sectors = first_bad - sector_nr;
2680 else {
2681 bad_sectors -= (sector_nr - first_bad);
2682 if (min_bad == 0 ||
2683 min_bad > bad_sectors)
2684 min_bad = bad_sectors;
2685 }
NeilBrown3e198f72006-01-06 00:20:21 -08002686 }
NeilBrown06f60382011-07-28 11:31:48 +10002687 if (sector_nr < first_bad) {
2688 if (test_bit(WriteMostly, &rdev->flags)) {
2689 if (wonly < 0)
2690 wonly = i;
2691 } else {
2692 if (disk < 0)
2693 disk = i;
2694 }
Mike Christie796a5cf2016-06-05 14:32:07 -05002695 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown06f60382011-07-28 11:31:48 +10002696 bio->bi_end_io = end_sync_read;
2697 read_targets++;
Alexander Lyakasd57368a2012-07-17 13:17:55 +03002698 } else if (!test_bit(WriteErrorSeen, &rdev->flags) &&
2699 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
2700 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
2701 /*
2702 * The device is suitable for reading (InSync),
2703 * but has bad block(s) here. Let's try to correct them,
2704 * if we are doing resync or repair. Otherwise, leave
2705 * this device alone for this sync request.
2706 */
Mike Christie796a5cf2016-06-05 14:32:07 -05002707 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Alexander Lyakasd57368a2012-07-17 13:17:55 +03002708 bio->bi_end_io = end_sync_write;
2709 write_targets++;
NeilBrown06f60382011-07-28 11:31:48 +10002710 }
NeilBrown3e198f72006-01-06 00:20:21 -08002711 }
NeilBrown06f60382011-07-28 11:31:48 +10002712 if (bio->bi_end_io) {
2713 atomic_inc(&rdev->nr_pending);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002714 bio->bi_iter.bi_sector = sector_nr + rdev->data_offset;
NeilBrown06f60382011-07-28 11:31:48 +10002715 bio->bi_bdev = rdev->bdev;
2716 bio->bi_private = r1_bio;
NeilBrown2e52d442016-11-18 16:16:12 +11002717 if (test_bit(FailFast, &rdev->flags))
2718 bio->bi_opf |= MD_FAILFAST;
NeilBrown06f60382011-07-28 11:31:48 +10002719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 }
NeilBrown3e198f72006-01-06 00:20:21 -08002721 rcu_read_unlock();
2722 if (disk < 0)
2723 disk = wonly;
2724 r1_bio->read_disk = disk;
NeilBrown191ea9b2005-06-21 17:17:23 -07002725
NeilBrown06f60382011-07-28 11:31:48 +10002726 if (read_targets == 0 && min_bad > 0) {
2727 /* These sectors are bad on all InSync devices, so we
2728 * need to mark them bad on all write targets
2729 */
2730 int ok = 1;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002731 for (i = 0 ; i < conf->raid_disks * 2 ; i++)
NeilBrown06f60382011-07-28 11:31:48 +10002732 if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
majianpenga42f9d82012-04-02 01:04:19 +10002733 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrown06f60382011-07-28 11:31:48 +10002734 ok = rdev_set_badblocks(rdev, sector_nr,
2735 min_bad, 0
2736 ) && ok;
2737 }
Shaohua Li29530792016-12-08 15:48:19 -08002738 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown06f60382011-07-28 11:31:48 +10002739 *skipped = 1;
2740 put_buf(r1_bio);
2741
2742 if (!ok) {
2743 /* Cannot record the badblocks, so need to
2744 * abort the resync.
2745 * If there are multiple read targets, could just
2746 * fail the really bad ones ???
2747 */
2748 conf->recovery_disabled = mddev->recovery_disabled;
2749 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2750 return 0;
2751 } else
2752 return min_bad;
2753
2754 }
2755 if (min_bad > 0 && min_bad < good_sectors) {
2756 /* only resync enough to reach the next bad->good
2757 * transition */
2758 good_sectors = min_bad;
2759 }
2760
NeilBrown3e198f72006-01-06 00:20:21 -08002761 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
2762 /* extra read targets are also write targets */
2763 write_targets += read_targets-1;
2764
2765 if (write_targets == 0 || read_targets == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 /* There is nowhere to write, so all non-sync
2767 * drives must be failed - so we are finished
2768 */
NeilBrownb7219cc2012-07-31 10:05:34 +10002769 sector_t rv;
2770 if (min_bad > 0)
2771 max_sector = sector_nr + min_bad;
2772 rv = max_sector - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07002773 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 put_buf(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 return rv;
2776 }
2777
NeilBrownc6207272008-02-06 01:39:52 -08002778 if (max_sector > mddev->resync_max)
2779 max_sector = mddev->resync_max; /* Don't do IO beyond here */
NeilBrown06f60382011-07-28 11:31:48 +10002780 if (max_sector > sector_nr + good_sectors)
2781 max_sector = sector_nr + good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 nr_sectors = 0;
NeilBrown289e99e2005-06-21 17:17:24 -07002783 sync_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 do {
2785 struct page *page;
2786 int len = PAGE_SIZE;
2787 if (sector_nr + (len>>9) > max_sector)
2788 len = (max_sector - sector_nr) << 9;
2789 if (len == 0)
2790 break;
NeilBrown6a806c52005-07-15 03:56:35 -07002791 if (sync_blocks == 0) {
2792 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
NeilBrowne5de485f2005-11-08 21:39:38 -08002793 &sync_blocks, still_degraded) &&
2794 !conf->fullsync &&
2795 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrown6a806c52005-07-15 03:56:35 -07002796 break;
NeilBrown7571ae82010-10-07 11:54:46 +11002797 if ((len >> 9) > sync_blocks)
NeilBrown6a806c52005-07-15 03:56:35 -07002798 len = sync_blocks<<9;
NeilBrownab7a30c2005-06-21 17:17:23 -07002799 }
NeilBrown191ea9b2005-06-21 17:17:23 -07002800
NeilBrown8f19ccb2011-12-23 10:17:56 +11002801 for (i = 0 ; i < conf->raid_disks * 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 bio = r1_bio->bios[i];
2803 if (bio->bi_end_io) {
NeilBrownd11c1712006-01-06 00:20:26 -08002804 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 if (bio_add_page(bio, page, len, 0) == 0) {
2806 /* stop here */
NeilBrownd11c1712006-01-06 00:20:26 -08002807 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 while (i > 0) {
2809 i--;
2810 bio = r1_bio->bios[i];
NeilBrown6a806c52005-07-15 03:56:35 -07002811 if (bio->bi_end_io==NULL)
2812 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 /* remove last page from this bio */
2814 bio->bi_vcnt--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002815 bio->bi_iter.bi_size -= len;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06002816 bio_clear_flag(bio, BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 }
2818 goto bio_full;
2819 }
2820 }
2821 }
2822 nr_sectors += len>>9;
2823 sector_nr += len>>9;
NeilBrown191ea9b2005-06-21 17:17:23 -07002824 sync_blocks -= (len>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
2826 bio_full:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 r1_bio->sectors = nr_sectors;
2828
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002829 if (mddev_is_clustered(mddev) &&
2830 conf->cluster_sync_high < sector_nr + nr_sectors) {
2831 conf->cluster_sync_low = mddev->curr_resync_completed;
2832 conf->cluster_sync_high = conf->cluster_sync_low + CLUSTER_RESYNC_WINDOW_SECTORS;
2833 /* Send resync message */
2834 md_cluster_ops->resync_info_update(mddev,
2835 conf->cluster_sync_low,
2836 conf->cluster_sync_high);
2837 }
2838
NeilBrownd11c1712006-01-06 00:20:26 -08002839 /* For a user-requested sync, we read all readable devices and do a
2840 * compare
2841 */
2842 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2843 atomic_set(&r1_bio->remaining, read_targets);
NeilBrown2d4f4f32012-07-09 11:34:13 +10002844 for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) {
NeilBrownd11c1712006-01-06 00:20:26 -08002845 bio = r1_bio->bios[i];
2846 if (bio->bi_end_io == end_sync_read) {
NeilBrown2d4f4f32012-07-09 11:34:13 +10002847 read_targets--;
NeilBrownddac7c72006-08-31 21:27:36 -07002848 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrown2e52d442016-11-18 16:16:12 +11002849 if (read_targets == 1)
2850 bio->bi_opf &= ~MD_FAILFAST;
NeilBrownd11c1712006-01-06 00:20:26 -08002851 generic_make_request(bio);
2852 }
2853 }
2854 } else {
2855 atomic_set(&r1_bio->remaining, 1);
2856 bio = r1_bio->bios[r1_bio->read_disk];
NeilBrownddac7c72006-08-31 21:27:36 -07002857 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrown2e52d442016-11-18 16:16:12 +11002858 if (read_targets == 1)
2859 bio->bi_opf &= ~MD_FAILFAST;
NeilBrownd11c1712006-01-06 00:20:26 -08002860 generic_make_request(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
NeilBrownd11c1712006-01-06 00:20:26 -08002862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return nr_sectors;
2864}
2865
NeilBrownfd01b882011-10-11 16:47:53 +11002866static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07002867{
2868 if (sectors)
2869 return sectors;
2870
2871 return mddev->dev_sectors;
2872}
2873
NeilBrowne8096362011-10-11 16:49:05 +11002874static struct r1conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
NeilBrowne8096362011-10-11 16:49:05 +11002876 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11002877 int i;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10002878 struct raid1_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11002879 struct md_rdev *rdev;
NeilBrown709ae482009-12-14 12:49:51 +11002880 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
NeilBrowne8096362011-10-11 16:49:05 +11002882 conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (!conf)
NeilBrown709ae482009-12-14 12:49:51 +11002884 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10002886 conf->mirrors = kzalloc(sizeof(struct raid1_info)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002887 * mddev->raid_disks * 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 GFP_KERNEL);
2889 if (!conf->mirrors)
NeilBrown709ae482009-12-14 12:49:51 +11002890 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
NeilBrownddaf22a2006-01-06 00:20:19 -08002892 conf->tmppage = alloc_page(GFP_KERNEL);
2893 if (!conf->tmppage)
NeilBrown709ae482009-12-14 12:49:51 +11002894 goto abort;
NeilBrownddaf22a2006-01-06 00:20:19 -08002895
NeilBrown709ae482009-12-14 12:49:51 +11002896 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 if (!conf->poolinfo)
NeilBrown709ae482009-12-14 12:49:51 +11002898 goto abort;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002899 conf->poolinfo->raid_disks = mddev->raid_disks * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2901 r1bio_pool_free,
2902 conf->poolinfo);
2903 if (!conf->r1bio_pool)
NeilBrown709ae482009-12-14 12:49:51 +11002904 goto abort;
2905
NeilBrowned9bfdf2009-10-16 15:55:44 +11002906 conf->poolinfo->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
NeilBrownc19d5792011-12-23 10:17:57 +11002908 err = -EINVAL;
Neil Browne7e72bf2008-05-14 16:05:54 -07002909 spin_lock_init(&conf->device_lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11002910 rdev_for_each(rdev, mddev) {
NeilBrownaba336b2012-05-31 15:39:11 +10002911 struct request_queue *q;
NeilBrown709ae482009-12-14 12:49:51 +11002912 int disk_idx = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 if (disk_idx >= mddev->raid_disks
2914 || disk_idx < 0)
2915 continue;
NeilBrownc19d5792011-12-23 10:17:57 +11002916 if (test_bit(Replacement, &rdev->flags))
NeilBrown02b898f2012-10-31 11:42:03 +11002917 disk = conf->mirrors + mddev->raid_disks + disk_idx;
NeilBrownc19d5792011-12-23 10:17:57 +11002918 else
2919 disk = conf->mirrors + disk_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
NeilBrownc19d5792011-12-23 10:17:57 +11002921 if (disk->rdev)
2922 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 disk->rdev = rdev;
NeilBrownaba336b2012-05-31 15:39:11 +10002924 q = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
2926 disk->head_position = 0;
Shaohua Li12cee5a2012-07-31 10:03:53 +10002927 disk->seq_start = MaxSector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 }
2929 conf->raid_disks = mddev->raid_disks;
2930 conf->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 INIT_LIST_HEAD(&conf->retry_list);
NeilBrown55ce74d2015-08-14 11:11:10 +10002932 INIT_LIST_HEAD(&conf->bio_end_io_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
2934 spin_lock_init(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -08002935 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
NeilBrown191ea9b2005-06-21 17:17:23 -07002937 bio_list_init(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +11002938 conf->pending_count = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11002939 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown191ea9b2005-06-21 17:17:23 -07002940
majianpeng79ef3a82013-11-15 14:55:02 +08002941 conf->start_next_window = MaxSector;
2942 conf->current_window_requests = conf->next_window_requests = 0;
2943
NeilBrownc19d5792011-12-23 10:17:57 +11002944 err = -EIO;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002945 for (i = 0; i < conf->raid_disks * 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
2947 disk = conf->mirrors + i;
2948
NeilBrownc19d5792011-12-23 10:17:57 +11002949 if (i < conf->raid_disks &&
2950 disk[conf->raid_disks].rdev) {
2951 /* This slot has a replacement. */
2952 if (!disk->rdev) {
2953 /* No original, just make the replacement
2954 * a recovering spare
2955 */
2956 disk->rdev =
2957 disk[conf->raid_disks].rdev;
2958 disk[conf->raid_disks].rdev = NULL;
2959 } else if (!test_bit(In_sync, &disk->rdev->flags))
2960 /* Original is not in_sync - bad */
2961 goto abort;
2962 }
2963
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002964 if (!disk->rdev ||
2965 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 disk->head_position = 0;
Jonathan Brassow4f0a5e02012-05-22 13:55:31 +10002967 if (disk->rdev &&
2968 (disk->rdev->saved_raid_disk < 0))
NeilBrown918f0232007-08-22 14:01:52 -07002969 conf->fullsync = 1;
Shaohua Libe4d3282012-07-31 10:03:53 +10002970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 }
NeilBrown709ae482009-12-14 12:49:51 +11002972
NeilBrown709ae482009-12-14 12:49:51 +11002973 err = -ENOMEM;
NeilBrown02326052012-07-03 15:56:52 +10002974 conf->thread = md_register_thread(raid1d, mddev, "raid1");
NeilBrown1d41c212016-11-02 14:16:50 +11002975 if (!conf->thread)
NeilBrown709ae482009-12-14 12:49:51 +11002976 goto abort;
NeilBrown191ea9b2005-06-21 17:17:23 -07002977
NeilBrown709ae482009-12-14 12:49:51 +11002978 return conf;
2979
2980 abort:
2981 if (conf) {
Julia Lawall644df1a2015-09-13 14:15:10 +02002982 mempool_destroy(conf->r1bio_pool);
NeilBrown709ae482009-12-14 12:49:51 +11002983 kfree(conf->mirrors);
2984 safe_put_page(conf->tmppage);
2985 kfree(conf->poolinfo);
2986 kfree(conf);
2987 }
2988 return ERR_PTR(err);
2989}
2990
NeilBrownafa0f552014-12-15 12:56:58 +11002991static void raid1_free(struct mddev *mddev, void *priv);
Shaohua Li849674e2016-01-20 13:52:20 -08002992static int raid1_run(struct mddev *mddev)
NeilBrown709ae482009-12-14 12:49:51 +11002993{
NeilBrowne8096362011-10-11 16:49:05 +11002994 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11002995 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11002996 struct md_rdev *rdev;
majianpeng5220ea12012-04-02 09:48:38 +10002997 int ret;
Shaohua Li2ff8cc22012-10-11 13:28:54 +11002998 bool discard_supported = false;
NeilBrown709ae482009-12-14 12:49:51 +11002999
3000 if (mddev->level != 1) {
NeilBrown1d41c212016-11-02 14:16:50 +11003001 pr_warn("md/raid1:%s: raid level not set to mirroring (%d)\n",
3002 mdname(mddev), mddev->level);
NeilBrown709ae482009-12-14 12:49:51 +11003003 return -EIO;
3004 }
3005 if (mddev->reshape_position != MaxSector) {
NeilBrown1d41c212016-11-02 14:16:50 +11003006 pr_warn("md/raid1:%s: reshape_position set but not supported\n",
3007 mdname(mddev));
NeilBrown709ae482009-12-14 12:49:51 +11003008 return -EIO;
3009 }
3010 /*
3011 * copy the already verified devices into our private RAID1
3012 * bookkeeping area. [whatever we allocate in run(),
NeilBrownafa0f552014-12-15 12:56:58 +11003013 * should be freed in raid1_free()]
NeilBrown709ae482009-12-14 12:49:51 +11003014 */
3015 if (mddev->private == NULL)
3016 conf = setup_conf(mddev);
3017 else
3018 conf = mddev->private;
3019
3020 if (IS_ERR(conf))
3021 return PTR_ERR(conf);
3022
Joe Lawrencec8dc9c62013-02-21 13:28:09 +11003023 if (mddev->queue)
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07003024 blk_queue_max_write_same_sectors(mddev->queue, 0);
3025
NeilBrowndafb20f2012-03-19 12:46:39 +11003026 rdev_for_each(rdev, mddev) {
Jonathan Brassow1ed72422011-06-07 17:50:35 -05003027 if (!mddev->gendisk)
3028 continue;
NeilBrown709ae482009-12-14 12:49:51 +11003029 disk_stack_limits(mddev->gendisk, rdev->bdev,
3030 rdev->data_offset << 9);
Shaohua Li2ff8cc22012-10-11 13:28:54 +11003031 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3032 discard_supported = true;
NeilBrown709ae482009-12-14 12:49:51 +11003033 }
3034
3035 mddev->degraded = 0;
3036 for (i=0; i < conf->raid_disks; i++)
3037 if (conf->mirrors[i].rdev == NULL ||
3038 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
3039 test_bit(Faulty, &conf->mirrors[i].rdev->flags))
3040 mddev->degraded++;
3041
3042 if (conf->raid_disks - mddev->degraded == 1)
3043 mddev->recovery_cp = MaxSector;
3044
Andre Noll8c6ac862009-06-18 08:48:06 +10003045 if (mddev->recovery_cp != MaxSector)
NeilBrown1d41c212016-11-02 14:16:50 +11003046 pr_info("md/raid1:%s: not clean -- starting background reconstruction\n",
3047 mdname(mddev));
3048 pr_info("md/raid1:%s: active with %d out of %d mirrors\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10003049 mdname(mddev), mddev->raid_disks - mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 mddev->raid_disks);
NeilBrown709ae482009-12-14 12:49:51 +11003051
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 /*
3053 * Ok, everything is just fine now
3054 */
NeilBrown709ae482009-12-14 12:49:51 +11003055 mddev->thread = conf->thread;
3056 conf->thread = NULL;
3057 mddev->private = conf;
NeilBrown46533ff2016-11-18 16:16:11 +11003058 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
NeilBrown709ae482009-12-14 12:49:51 +11003059
Dan Williams1f403622009-03-31 14:59:03 +11003060 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Jonathan Brassow1ed72422011-06-07 17:50:35 -05003062 if (mddev->queue) {
Shaohua Li2ff8cc22012-10-11 13:28:54 +11003063 if (discard_supported)
3064 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3065 mddev->queue);
3066 else
3067 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3068 mddev->queue);
Jonathan Brassow1ed72422011-06-07 17:50:35 -05003069 }
majianpeng5220ea12012-04-02 09:48:38 +10003070
3071 ret = md_integrity_register(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003072 if (ret) {
3073 md_unregister_thread(&mddev->thread);
NeilBrownafa0f552014-12-15 12:56:58 +11003074 raid1_free(mddev, conf);
NeilBrown5aa61f42014-12-15 12:56:57 +11003075 }
majianpeng5220ea12012-04-02 09:48:38 +10003076 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
NeilBrownafa0f552014-12-15 12:56:58 +11003079static void raid1_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
NeilBrownafa0f552014-12-15 12:56:58 +11003081 struct r1conf *conf = priv;
NeilBrown4b6d2872005-09-09 16:23:47 -07003082
Julia Lawall644df1a2015-09-13 14:15:10 +02003083 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003084 kfree(conf->mirrors);
Hirokazu Takahashi0fea7ed2013-04-24 11:42:44 +10003085 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003086 kfree(conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
NeilBrownfd01b882011-10-11 16:47:53 +11003090static int raid1_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
3092 /* no resync is happening, and there is enough space
3093 * on all devices, so we can resize.
3094 * We need to make sure resync covers any new space.
3095 * If the array is shrinking we should possibly wait until
3096 * any io in the removed space completes, but it hardly seems
3097 * worth it.
3098 */
NeilBrowna4a61252012-05-22 13:55:27 +10003099 sector_t newsize = raid1_size(mddev, sectors, 0);
3100 if (mddev->external_size &&
3101 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11003102 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10003103 if (mddev->bitmap) {
3104 int ret = bitmap_resize(mddev->bitmap, newsize, 0, 0);
3105 if (ret)
3106 return ret;
3107 }
3108 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10003109 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003110 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003111 if (sectors > mddev->dev_sectors &&
NeilBrownb0986362011-05-11 15:52:21 +10003112 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003113 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3115 }
Dan Williamsb522adc2009-03-31 15:00:31 +11003116 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07003117 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 return 0;
3119}
3120
NeilBrownfd01b882011-10-11 16:47:53 +11003121static int raid1_reshape(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122{
3123 /* We need to:
3124 * 1/ resize the r1bio_pool
3125 * 2/ resize conf->mirrors
3126 *
3127 * We allocate a new r1bio_pool if we can.
3128 * Then raise a device barrier and wait until all IO stops.
3129 * Then resize conf->mirrors and swap in the new r1bio pool.
NeilBrown6ea9c072005-06-21 17:17:09 -07003130 *
3131 * At the same time, we "pack" the devices so that all the missing
3132 * devices have the higher raid_disk numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 */
3134 mempool_t *newpool, *oldpool;
3135 struct pool_info *newpoolinfo;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10003136 struct raid1_info *newmirrors;
NeilBrowne8096362011-10-11 16:49:05 +11003137 struct r1conf *conf = mddev->private;
NeilBrown63c70c42006-03-27 01:18:13 -08003138 int cnt, raid_disks;
NeilBrownc04be0a2006-10-03 01:15:53 -07003139 unsigned long flags;
Dan Williamsb5470dc2008-06-27 21:44:04 -07003140 int d, d2, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
NeilBrown63c70c42006-03-27 01:18:13 -08003142 /* Cannot change chunk_size, layout, or level */
Andre Noll664e7c42009-06-18 08:45:27 +10003143 if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
NeilBrown63c70c42006-03-27 01:18:13 -08003144 mddev->layout != mddev->new_layout ||
3145 mddev->level != mddev->new_level) {
Andre Noll664e7c42009-06-18 08:45:27 +10003146 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown63c70c42006-03-27 01:18:13 -08003147 mddev->new_layout = mddev->layout;
3148 mddev->new_level = mddev->level;
3149 return -EINVAL;
3150 }
3151
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003152 if (!mddev_is_clustered(mddev)) {
3153 err = md_allow_write(mddev);
3154 if (err)
3155 return err;
3156 }
NeilBrown2a2275d2007-01-26 00:57:11 -08003157
NeilBrown63c70c42006-03-27 01:18:13 -08003158 raid_disks = mddev->raid_disks + mddev->delta_disks;
3159
NeilBrown6ea9c072005-06-21 17:17:09 -07003160 if (raid_disks < conf->raid_disks) {
3161 cnt=0;
3162 for (d= 0; d < conf->raid_disks; d++)
3163 if (conf->mirrors[d].rdev)
3164 cnt++;
3165 if (cnt > raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 return -EBUSY;
NeilBrown6ea9c072005-06-21 17:17:09 -07003167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
3169 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
3170 if (!newpoolinfo)
3171 return -ENOMEM;
3172 newpoolinfo->mddev = mddev;
NeilBrown8f19ccb2011-12-23 10:17:56 +11003173 newpoolinfo->raid_disks = raid_disks * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
3175 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
3176 r1bio_pool_free, newpoolinfo);
3177 if (!newpool) {
3178 kfree(newpoolinfo);
3179 return -ENOMEM;
3180 }
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10003181 newmirrors = kzalloc(sizeof(struct raid1_info) * raid_disks * 2,
NeilBrown8f19ccb2011-12-23 10:17:56 +11003182 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (!newmirrors) {
3184 kfree(newpoolinfo);
3185 mempool_destroy(newpool);
3186 return -ENOMEM;
3187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
NeilBrowne2d59922013-06-12 11:01:22 +10003189 freeze_array(conf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
3191 /* ok, everything is stopped */
3192 oldpool = conf->r1bio_pool;
3193 conf->r1bio_pool = newpool;
NeilBrown6ea9c072005-06-21 17:17:09 -07003194
NeilBrowna88aa782007-08-22 14:01:53 -07003195 for (d = d2 = 0; d < conf->raid_disks; d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11003196 struct md_rdev *rdev = conf->mirrors[d].rdev;
NeilBrowna88aa782007-08-22 14:01:53 -07003197 if (rdev && rdev->raid_disk != d2) {
Namhyung Kim36fad852011-07-27 11:00:36 +10003198 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna88aa782007-08-22 14:01:53 -07003199 rdev->raid_disk = d2;
Namhyung Kim36fad852011-07-27 11:00:36 +10003200 sysfs_unlink_rdev(mddev, rdev);
3201 if (sysfs_link_rdev(mddev, rdev))
NeilBrown1d41c212016-11-02 14:16:50 +11003202 pr_warn("md/raid1:%s: cannot register rd%d\n",
3203 mdname(mddev), rdev->raid_disk);
NeilBrown6ea9c072005-06-21 17:17:09 -07003204 }
NeilBrowna88aa782007-08-22 14:01:53 -07003205 if (rdev)
3206 newmirrors[d2++].rdev = rdev;
3207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 kfree(conf->mirrors);
3209 conf->mirrors = newmirrors;
3210 kfree(conf->poolinfo);
3211 conf->poolinfo = newpoolinfo;
3212
NeilBrownc04be0a2006-10-03 01:15:53 -07003213 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 mddev->degraded += (raid_disks - conf->raid_disks);
NeilBrownc04be0a2006-10-03 01:15:53 -07003215 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 conf->raid_disks = mddev->raid_disks = raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08003217 mddev->delta_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
NeilBrowne2d59922013-06-12 11:01:22 +10003219 unfreeze_array(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
NeilBrown985ca972015-07-06 12:26:57 +10003221 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3223 md_wakeup_thread(mddev->thread);
3224
3225 mempool_destroy(oldpool);
3226 return 0;
3227}
3228
NeilBrownfd01b882011-10-11 16:47:53 +11003229static void raid1_quiesce(struct mddev *mddev, int state)
NeilBrown36fa3062005-09-09 16:23:45 -07003230{
NeilBrowne8096362011-10-11 16:49:05 +11003231 struct r1conf *conf = mddev->private;
NeilBrown36fa3062005-09-09 16:23:45 -07003232
3233 switch(state) {
NeilBrown6eef4b22009-12-14 12:49:51 +11003234 case 2: /* wake for suspend */
3235 wake_up(&conf->wait_barrier);
3236 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07003237 case 1:
majianpeng07169fd2013-11-14 15:16:18 +11003238 freeze_array(conf, 0);
NeilBrown36fa3062005-09-09 16:23:45 -07003239 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07003240 case 0:
majianpeng07169fd2013-11-14 15:16:18 +11003241 unfreeze_array(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07003242 break;
3243 }
NeilBrown36fa3062005-09-09 16:23:45 -07003244}
3245
NeilBrownfd01b882011-10-11 16:47:53 +11003246static void *raid1_takeover(struct mddev *mddev)
NeilBrown709ae482009-12-14 12:49:51 +11003247{
3248 /* raid1 can take over:
3249 * raid5 with 2 devices, any layout or chunk size
3250 */
3251 if (mddev->level == 5 && mddev->raid_disks == 2) {
NeilBrowne8096362011-10-11 16:49:05 +11003252 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11003253 mddev->new_level = 1;
3254 mddev->new_layout = 0;
3255 mddev->new_chunk_sectors = 0;
3256 conf = setup_conf(mddev);
Shaohua Li6995f0b2016-12-08 15:48:17 -08003257 if (!IS_ERR(conf)) {
majianpeng07169fd2013-11-14 15:16:18 +11003258 /* Array must appear to be quiesced */
3259 conf->array_frozen = 1;
Shaohua Li6995f0b2016-12-08 15:48:17 -08003260 clear_bit(MD_HAS_JOURNAL, &mddev->flags);
3261 clear_bit(MD_JOURNAL_CLEAN, &mddev->flags);
3262 }
NeilBrown709ae482009-12-14 12:49:51 +11003263 return conf;
3264 }
3265 return ERR_PTR(-EINVAL);
3266}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
NeilBrown84fc4b52011-10-11 16:49:58 +11003268static struct md_personality raid1_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
3270 .name = "raid1",
NeilBrown2604b702006-01-06 00:20:36 -08003271 .level = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08003273 .make_request = raid1_make_request,
3274 .run = raid1_run,
NeilBrownafa0f552014-12-15 12:56:58 +11003275 .free = raid1_free,
Shaohua Li849674e2016-01-20 13:52:20 -08003276 .status = raid1_status,
3277 .error_handler = raid1_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 .hot_add_disk = raid1_add_disk,
3279 .hot_remove_disk= raid1_remove_disk,
3280 .spare_active = raid1_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08003281 .sync_request = raid1_sync_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 .resize = raid1_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07003283 .size = raid1_size,
NeilBrown63c70c42006-03-27 01:18:13 -08003284 .check_reshape = raid1_reshape,
NeilBrown36fa3062005-09-09 16:23:45 -07003285 .quiesce = raid1_quiesce,
NeilBrown709ae482009-12-14 12:49:51 +11003286 .takeover = raid1_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11003287 .congested = raid1_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288};
3289
3290static int __init raid_init(void)
3291{
NeilBrown2604b702006-01-06 00:20:36 -08003292 return register_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293}
3294
3295static void raid_exit(void)
3296{
NeilBrown2604b702006-01-06 00:20:36 -08003297 unregister_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298}
3299
3300module_init(raid_init);
3301module_exit(raid_exit);
3302MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11003303MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304MODULE_ALIAS("md-personality-3"); /* RAID1 */
NeilBrownd9d166c2006-01-06 00:20:51 -08003305MODULE_ALIAS("md-raid1");
NeilBrown2604b702006-01-06 00:20:36 -08003306MODULE_ALIAS("md-level-1");
NeilBrown34db0cd2011-10-11 16:50:01 +11003307
3308module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);