blob: efc2e744cfd3d22cd86959b877f34c389747f91e [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
Shaohua Li849674e2016-01-20 13:52:20 -08001069static void raid1_make_request(struct mddev *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
NeilBrowne8096362011-10-11 16:49:05 +11001071 struct r1conf *conf = mddev->private;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001072 struct raid1_info *mirror;
NeilBrown9f2c9d12011-10-11 16:48:43 +11001073 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 struct bio *read_bio;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001075 int i, disks;
NeilBrown84255d12008-05-23 13:04:32 -07001076 struct bitmap *bitmap;
NeilBrown191ea9b2005-06-21 17:17:23 -07001077 unsigned long flags;
Mike Christie796a5cf2016-06-05 14:32:07 -05001078 const int op = bio_op(bio);
Jens Axboea3623572005-11-01 09:26:16 +01001079 const int rw = bio_data_dir(bio);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001080 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1081 const unsigned long do_flush_fua = (bio->bi_opf &
Mike Christie28a8f0d2016-06-05 14:32:25 -05001082 (REQ_PREFLUSH | REQ_FUA));
NeilBrown3cb03002011-10-11 16:45:26 +11001083 struct md_rdev *blocked_rdev;
NeilBrownf54a9d02012-08-02 08:33:20 +10001084 struct blk_plug_cb *cb;
1085 struct raid1_plug_cb *plug = NULL;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001086 int first_clone;
1087 int sectors_handled;
1088 int max_sectors;
majianpeng79ef3a82013-11-15 14:55:02 +08001089 sector_t start_next_window;
NeilBrown191ea9b2005-06-21 17:17:23 -07001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 /*
1092 * Register the new request and wait if the reconstruction
1093 * thread has put up a bar for new requests.
1094 * Continue immediately if no resync is active currently.
1095 */
NeilBrown62de6082006-05-01 12:15:47 -07001096
NeilBrown3d310eb2005-06-21 17:17:26 -07001097 md_write_start(mddev, bio); /* wait on superblock update early */
1098
NeilBrown6eef4b22009-12-14 12:49:51 +11001099 if (bio_data_dir(bio) == WRITE &&
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001100 ((bio_end_sector(bio) > mddev->suspend_lo &&
1101 bio->bi_iter.bi_sector < mddev->suspend_hi) ||
1102 (mddev_is_clustered(mddev) &&
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -05001103 md_cluster_ops->area_resyncing(mddev, WRITE,
1104 bio->bi_iter.bi_sector, bio_end_sector(bio))))) {
NeilBrown6eef4b22009-12-14 12:49:51 +11001105 /* As the suspend_* range is controlled by
1106 * userspace, we want an interruptible
1107 * wait.
1108 */
1109 DEFINE_WAIT(w);
1110 for (;;) {
1111 flush_signals(current);
1112 prepare_to_wait(&conf->wait_barrier,
1113 &w, TASK_INTERRUPTIBLE);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07001114 if (bio_end_sector(bio) <= mddev->suspend_lo ||
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001115 bio->bi_iter.bi_sector >= mddev->suspend_hi ||
1116 (mddev_is_clustered(mddev) &&
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -05001117 !md_cluster_ops->area_resyncing(mddev, WRITE,
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001118 bio->bi_iter.bi_sector, bio_end_sector(bio))))
NeilBrown6eef4b22009-12-14 12:49:51 +11001119 break;
1120 schedule();
1121 }
1122 finish_wait(&conf->wait_barrier, &w);
1123 }
NeilBrown62de6082006-05-01 12:15:47 -07001124
majianpeng79ef3a82013-11-15 14:55:02 +08001125 start_next_window = wait_barrier(conf, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
NeilBrown84255d12008-05-23 13:04:32 -07001127 bitmap = mddev->bitmap;
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /*
Christoph Hellwig70246282016-07-19 11:28:41 +02001130 * make_request() can abort the operation when read-ahead is being
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 * used and no empty request is available.
1132 *
1133 */
1134 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1135
1136 r1_bio->master_bio = bio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001137 r1_bio->sectors = bio_sectors(bio);
NeilBrown191ea9b2005-06-21 17:17:23 -07001138 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 r1_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001140 r1_bio->sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
NeilBrownd2eb35a2011-07-28 11:31:48 +10001142 /* We might need to issue multiple reads to different
1143 * devices if there are bad blocks around, so we keep
1144 * track of the number of reads in bio->bi_phys_segments.
1145 * If this is 0, there is only one r1_bio and no locking
1146 * will be needed when requests complete. If it is
1147 * non-zero, then it is the number of not-completed requests.
1148 */
1149 bio->bi_phys_segments = 0;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06001150 bio_clear_flag(bio, BIO_SEG_VALID);
NeilBrownd2eb35a2011-07-28 11:31:48 +10001151
Jens Axboea3623572005-11-01 09:26:16 +01001152 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /*
1154 * read balancing logic:
1155 */
NeilBrownd2eb35a2011-07-28 11:31:48 +10001156 int rdisk;
1157
1158read_again:
1159 rdisk = read_balance(conf, r1_bio, &max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 if (rdisk < 0) {
1162 /* couldn't find anywhere to read from */
1163 raid_end_bio_io(r1_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001164 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166 mirror = conf->mirrors + rdisk;
1167
NeilBrowne5551902010-03-31 11:21:44 +11001168 if (test_bit(WriteMostly, &mirror->rdev->flags) &&
1169 bitmap) {
1170 /* Reading from a write-mostly device must
1171 * take care not to over-take any writes
1172 * that are 'behind'
1173 */
NeilBrown578b54a2016-11-14 16:30:21 +11001174 raid1_log(mddev, "wait behind writes");
NeilBrowne5551902010-03-31 11:21:44 +11001175 wait_event(bitmap->behind_wait,
1176 atomic_read(&bitmap->behind_writes) == 0);
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 r1_bio->read_disk = rdisk;
NeilBrownf0cc9a02014-09-22 10:06:23 +10001179 r1_bio->start_next_window = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
NeilBrowna167f662010-10-26 18:31:13 +11001181 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001182 bio_trim(read_bio, r1_bio->sector - bio->bi_iter.bi_sector,
Kent Overstreet6678d832013-08-07 11:14:32 -07001183 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 r1_bio->bios[rdisk] = read_bio;
1186
Kent Overstreet4f024f32013-10-11 15:44:27 -07001187 read_bio->bi_iter.bi_sector = r1_bio->sector +
1188 mirror->rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 read_bio->bi_bdev = mirror->rdev->bdev;
1190 read_bio->bi_end_io = raid1_end_read_request;
Mike Christie796a5cf2016-06-05 14:32:07 -05001191 bio_set_op_attrs(read_bio, op, do_sync);
NeilBrown2e52d442016-11-18 16:16:12 +11001192 if (test_bit(FailFast, &mirror->rdev->flags) &&
1193 test_bit(R1BIO_FailFast, &r1_bio->state))
1194 read_bio->bi_opf |= MD_FAILFAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 read_bio->bi_private = r1_bio;
1196
NeilBrown109e3762016-11-18 13:22:04 +11001197 if (mddev->gendisk)
1198 trace_block_bio_remap(bdev_get_queue(read_bio->bi_bdev),
1199 read_bio, disk_devt(mddev->gendisk),
1200 r1_bio->sector);
1201
NeilBrownd2eb35a2011-07-28 11:31:48 +10001202 if (max_sectors < r1_bio->sectors) {
1203 /* could not read all from this device, so we will
1204 * need another r1_bio.
1205 */
NeilBrownd2eb35a2011-07-28 11:31:48 +10001206
1207 sectors_handled = (r1_bio->sector + max_sectors
Kent Overstreet4f024f32013-10-11 15:44:27 -07001208 - bio->bi_iter.bi_sector);
NeilBrownd2eb35a2011-07-28 11:31:48 +10001209 r1_bio->sectors = max_sectors;
1210 spin_lock_irq(&conf->device_lock);
1211 if (bio->bi_phys_segments == 0)
1212 bio->bi_phys_segments = 2;
1213 else
1214 bio->bi_phys_segments++;
1215 spin_unlock_irq(&conf->device_lock);
1216 /* Cannot call generic_make_request directly
1217 * as that will be queued in __make_request
1218 * and subsequent mempool_alloc might block waiting
1219 * for it. So hand bio over to raid1d.
1220 */
1221 reschedule_retry(r1_bio);
1222
1223 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1224
1225 r1_bio->master_bio = bio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001226 r1_bio->sectors = bio_sectors(bio) - sectors_handled;
NeilBrownd2eb35a2011-07-28 11:31:48 +10001227 r1_bio->state = 0;
1228 r1_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001229 r1_bio->sector = bio->bi_iter.bi_sector +
1230 sectors_handled;
NeilBrownd2eb35a2011-07-28 11:31:48 +10001231 goto read_again;
1232 } else
1233 generic_make_request(read_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001234 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236
1237 /*
1238 * WRITE:
1239 */
NeilBrown34db0cd2011-10-11 16:50:01 +11001240 if (conf->pending_count >= max_queued_requests) {
1241 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001242 raid1_log(mddev, "wait queued");
NeilBrown34db0cd2011-10-11 16:50:01 +11001243 wait_event(conf->wait_barrier,
1244 conf->pending_count < max_queued_requests);
1245 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001246 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 * inc refcount on their rdev. Record them by setting
1248 * bios[x] to bio
NeilBrown1f68f0c2011-07-28 11:31:48 +10001249 * If there are known/acknowledged bad blocks on any device on
1250 * which we have seen a write error, we want to avoid writing those
1251 * blocks.
1252 * This potentially requires several writes to write around
1253 * the bad blocks. Each set of writes gets it's own r1bio
1254 * with a set of bios attached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001256
NeilBrown8f19ccb2011-12-23 10:17:56 +11001257 disks = conf->raid_disks * 2;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001258 retry_write:
majianpeng79ef3a82013-11-15 14:55:02 +08001259 r1_bio->start_next_window = start_next_window;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001260 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 rcu_read_lock();
NeilBrown1f68f0c2011-07-28 11:31:48 +10001262 max_sectors = r1_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 for (i = 0; i < disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001264 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001265 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1266 atomic_inc(&rdev->nr_pending);
1267 blocked_rdev = rdev;
1268 break;
1269 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001270 r1_bio->bios[i] = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001271 if (!rdev || test_bit(Faulty, &rdev->flags)) {
NeilBrown8f19ccb2011-12-23 10:17:56 +11001272 if (i < conf->raid_disks)
1273 set_bit(R1BIO_Degraded, &r1_bio->state);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001274 continue;
1275 }
1276
1277 atomic_inc(&rdev->nr_pending);
1278 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1279 sector_t first_bad;
1280 int bad_sectors;
1281 int is_bad;
1282
1283 is_bad = is_badblock(rdev, r1_bio->sector,
1284 max_sectors,
1285 &first_bad, &bad_sectors);
1286 if (is_bad < 0) {
1287 /* mustn't write here until the bad block is
1288 * acknowledged*/
1289 set_bit(BlockedBadBlocks, &rdev->flags);
1290 blocked_rdev = rdev;
1291 break;
NeilBrown964147d2010-05-18 15:27:13 +10001292 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001293 if (is_bad && first_bad <= r1_bio->sector) {
1294 /* Cannot write here at all */
1295 bad_sectors -= (r1_bio->sector - first_bad);
1296 if (bad_sectors < max_sectors)
1297 /* mustn't write more than bad_sectors
1298 * to other devices yet
1299 */
1300 max_sectors = bad_sectors;
1301 rdev_dec_pending(rdev, mddev);
1302 /* We don't set R1BIO_Degraded as that
1303 * only applies if the disk is
1304 * missing, so it might be re-added,
1305 * and we want to know to recover this
1306 * chunk.
1307 * In this case the device is here,
1308 * and the fact that this chunk is not
1309 * in-sync is recorded in the bad
1310 * block log
1311 */
1312 continue;
1313 }
1314 if (is_bad) {
1315 int good_sectors = first_bad - r1_bio->sector;
1316 if (good_sectors < max_sectors)
1317 max_sectors = good_sectors;
1318 }
1319 }
1320 r1_bio->bios[i] = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322 rcu_read_unlock();
1323
Dan Williams6bfe0b42008-04-30 00:52:32 -07001324 if (unlikely(blocked_rdev)) {
1325 /* Wait for this device to become unblocked */
1326 int j;
majianpeng79ef3a82013-11-15 14:55:02 +08001327 sector_t old = start_next_window;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001328
1329 for (j = 0; j < i; j++)
1330 if (r1_bio->bios[j])
1331 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001332 r1_bio->state = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001333 allow_barrier(conf, start_next_window, bio->bi_iter.bi_sector);
NeilBrown578b54a2016-11-14 16:30:21 +11001334 raid1_log(mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001335 md_wait_for_blocked_rdev(blocked_rdev, mddev);
majianpeng79ef3a82013-11-15 14:55:02 +08001336 start_next_window = wait_barrier(conf, bio);
1337 /*
1338 * We must make sure the multi r1bios of bio have
1339 * the same value of bi_phys_segments
1340 */
1341 if (bio->bi_phys_segments && old &&
1342 old != start_next_window)
1343 /* Wait for the former r1bio(s) to complete */
1344 wait_event(conf->wait_barrier,
1345 bio->bi_phys_segments == 1);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001346 goto retry_write;
1347 }
1348
NeilBrown1f68f0c2011-07-28 11:31:48 +10001349 if (max_sectors < r1_bio->sectors) {
1350 /* We are splitting this write into multiple parts, so
1351 * we need to prepare for allocating another r1_bio.
1352 */
1353 r1_bio->sectors = max_sectors;
1354 spin_lock_irq(&conf->device_lock);
1355 if (bio->bi_phys_segments == 0)
1356 bio->bi_phys_segments = 2;
1357 else
1358 bio->bi_phys_segments++;
1359 spin_unlock_irq(&conf->device_lock);
NeilBrown191ea9b2005-06-21 17:17:23 -07001360 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07001361 sectors_handled = r1_bio->sector + max_sectors - bio->bi_iter.bi_sector;
NeilBrown4b6d2872005-09-09 16:23:47 -07001362
NeilBrown4e780642010-10-19 12:54:01 +11001363 atomic_set(&r1_bio->remaining, 1);
NeilBrown4b6d2872005-09-09 16:23:47 -07001364 atomic_set(&r1_bio->behind_remaining, 0);
NeilBrown191ea9b2005-06-21 17:17:23 -07001365
NeilBrown1f68f0c2011-07-28 11:31:48 +10001366 first_clone = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 for (i = 0; i < disks; i++) {
1368 struct bio *mbio;
1369 if (!r1_bio->bios[i])
1370 continue;
1371
NeilBrowna167f662010-10-26 18:31:13 +11001372 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001373 bio_trim(mbio, r1_bio->sector - bio->bi_iter.bi_sector, max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
NeilBrown1f68f0c2011-07-28 11:31:48 +10001375 if (first_clone) {
1376 /* do behind I/O ?
1377 * Not if there are too many, or cannot
1378 * allocate memory, or a reader on WriteMostly
1379 * is waiting for behind writes to flush */
1380 if (bitmap &&
1381 (atomic_read(&bitmap->behind_writes)
1382 < mddev->bitmap_info.max_write_behind) &&
1383 !waitqueue_active(&bitmap->behind_wait))
1384 alloc_behind_pages(mbio, r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
NeilBrown1f68f0c2011-07-28 11:31:48 +10001386 bitmap_startwrite(bitmap, r1_bio->sector,
1387 r1_bio->sectors,
1388 test_bit(R1BIO_BehindIO,
1389 &r1_bio->state));
1390 first_clone = 0;
1391 }
NeilBrown2ca68f52011-07-28 11:32:10 +10001392 if (r1_bio->behind_bvecs) {
NeilBrown4b6d2872005-09-09 16:23:47 -07001393 struct bio_vec *bvec;
1394 int j;
1395
Kent Overstreetcb34e052012-09-05 15:22:02 -07001396 /*
1397 * We trimmed the bio, so _all is legit
NeilBrown4b6d2872005-09-09 16:23:47 -07001398 */
Kent Overstreetd74c6d52013-02-06 12:23:11 -08001399 bio_for_each_segment_all(bvec, mbio, j)
NeilBrown2ca68f52011-07-28 11:32:10 +10001400 bvec->bv_page = r1_bio->behind_bvecs[j].bv_page;
NeilBrown4b6d2872005-09-09 16:23:47 -07001401 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
1402 atomic_inc(&r1_bio->behind_remaining);
1403 }
1404
NeilBrown1f68f0c2011-07-28 11:31:48 +10001405 r1_bio->bios[i] = mbio;
1406
Kent Overstreet4f024f32013-10-11 15:44:27 -07001407 mbio->bi_iter.bi_sector = (r1_bio->sector +
NeilBrown1f68f0c2011-07-28 11:31:48 +10001408 conf->mirrors[i].rdev->data_offset);
NeilBrown109e3762016-11-18 13:22:04 +11001409 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001410 mbio->bi_end_io = raid1_end_write_request;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001411 bio_set_op_attrs(mbio, op, do_flush_fua | do_sync);
NeilBrown212e7eb2016-11-18 16:16:12 +11001412 if (test_bit(FailFast, &conf->mirrors[i].rdev->flags) &&
1413 !test_bit(WriteMostly, &conf->mirrors[i].rdev->flags) &&
1414 conf->raid_disks - mddev->degraded > 1)
1415 mbio->bi_opf |= MD_FAILFAST;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001416 mbio->bi_private = r1_bio;
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 atomic_inc(&r1_bio->remaining);
NeilBrownf54a9d02012-08-02 08:33:20 +10001419
NeilBrown109e3762016-11-18 13:22:04 +11001420 if (mddev->gendisk)
1421 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1422 mbio, disk_devt(mddev->gendisk),
1423 r1_bio->sector);
1424 /* flush_pending_writes() needs access to the rdev so...*/
1425 mbio->bi_bdev = (void*)conf->mirrors[i].rdev;
1426
NeilBrownf54a9d02012-08-02 08:33:20 +10001427 cb = blk_check_plugged(raid1_unplug, mddev, sizeof(*plug));
1428 if (cb)
1429 plug = container_of(cb, struct raid1_plug_cb, cb);
1430 else
1431 plug = NULL;
NeilBrown4e780642010-10-19 12:54:01 +11001432 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownf54a9d02012-08-02 08:33:20 +10001433 if (plug) {
1434 bio_list_add(&plug->pending, mbio);
1435 plug->pending_cnt++;
1436 } else {
1437 bio_list_add(&conf->pending_bio_list, mbio);
1438 conf->pending_count++;
1439 }
NeilBrown4e780642010-10-19 12:54:01 +11001440 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrownf54a9d02012-08-02 08:33:20 +10001441 if (!plug)
NeilBrownb357f042012-07-03 17:45:31 +10001442 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
NeilBrown079fa162011-09-10 17:21:23 +10001444 /* Mustn't call r1_bio_write_done before this next test,
1445 * as it could result in the bio being freed.
1446 */
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001447 if (sectors_handled < bio_sectors(bio)) {
NeilBrown079fa162011-09-10 17:21:23 +10001448 r1_bio_write_done(r1_bio);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001449 /* We need another r1_bio. It has already been counted
1450 * in bio->bi_phys_segments
1451 */
1452 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1453 r1_bio->master_bio = bio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001454 r1_bio->sectors = bio_sectors(bio) - sectors_handled;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001455 r1_bio->state = 0;
1456 r1_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001457 r1_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001458 goto retry_write;
1459 }
1460
NeilBrown079fa162011-09-10 17:21:23 +10001461 r1_bio_write_done(r1_bio);
1462
1463 /* In case raid1d snuck in to freeze_array */
1464 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
Shaohua Li849674e2016-01-20 13:52:20 -08001467static void raid1_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
NeilBrowne8096362011-10-11 16:49:05 +11001469 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 int i;
1471
1472 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown11ce99e2006-10-03 01:15:52 -07001473 conf->raid_disks - mddev->degraded);
NeilBrownddac7c72006-08-31 21:27:36 -07001474 rcu_read_lock();
1475 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001476 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 seq_printf(seq, "%s",
NeilBrownddac7c72006-08-31 21:27:36 -07001478 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1479 }
1480 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 seq_printf(seq, "]");
1482}
1483
Shaohua Li849674e2016-01-20 13:52:20 -08001484static void raid1_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 char b[BDEVNAME_SIZE];
NeilBrowne8096362011-10-11 16:49:05 +11001487 struct r1conf *conf = mddev->private;
NeilBrown423f04d2015-07-27 11:48:52 +10001488 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 /*
1491 * If it is not operational, then we have already marked it as dead
1492 * else if it is the last working disks, ignore the error, let the
1493 * next level up know.
1494 * else mark the drive as failed
1495 */
NeilBrown2e52d442016-11-18 16:16:12 +11001496 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001497 if (test_bit(In_sync, &rdev->flags)
NeilBrown4044ba52009-01-09 08:31:11 +11001498 && (conf->raid_disks - mddev->degraded) == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 /*
1500 * Don't fail the drive, act as though we were just a
NeilBrown4044ba52009-01-09 08:31:11 +11001501 * normal single drive.
1502 * However don't try a recovery from this drive as
1503 * it is very likely to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 */
NeilBrown53890422011-07-27 11:00:36 +10001505 conf->recovery_disabled = mddev->recovery_disabled;
NeilBrown2e52d442016-11-18 16:16:12 +11001506 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return;
NeilBrown4044ba52009-01-09 08:31:11 +11001508 }
NeilBrownde393cd2011-07-28 11:31:48 +10001509 set_bit(Blocked, &rdev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001510 if (test_and_clear_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 mddev->degraded++;
NeilBrowndd00a992007-05-10 03:15:50 -07001512 set_bit(Faulty, &rdev->flags);
NeilBrowndd00a992007-05-10 03:15:50 -07001513 } else
1514 set_bit(Faulty, &rdev->flags);
NeilBrown423f04d2015-07-27 11:48:52 +10001515 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown2446dba2014-07-31 10:16:29 +10001516 /*
1517 * if recovery is running, make sure it aborts.
1518 */
1519 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04001520 set_mask_bits(&mddev->flags, 0,
1521 BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING));
NeilBrown1d41c212016-11-02 14:16:50 +11001522 pr_crit("md/raid1:%s: Disk failure on %s, disabling device.\n"
1523 "md/raid1:%s: Operation continuing on %d devices.\n",
1524 mdname(mddev), bdevname(rdev->bdev, b),
1525 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
NeilBrowne8096362011-10-11 16:49:05 +11001528static void print_conf(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
NeilBrown1d41c212016-11-02 14:16:50 +11001532 pr_debug("RAID1 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (!conf) {
NeilBrown1d41c212016-11-02 14:16:50 +11001534 pr_debug("(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return;
1536 }
NeilBrown1d41c212016-11-02 14:16:50 +11001537 pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1538 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
NeilBrownddac7c72006-08-31 21:27:36 -07001540 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 for (i = 0; i < conf->raid_disks; i++) {
1542 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11001543 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownddac7c72006-08-31 21:27:36 -07001544 if (rdev)
NeilBrown1d41c212016-11-02 14:16:50 +11001545 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1546 i, !test_bit(In_sync, &rdev->flags),
1547 !test_bit(Faulty, &rdev->flags),
1548 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
NeilBrownddac7c72006-08-31 21:27:36 -07001550 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
NeilBrowne8096362011-10-11 16:49:05 +11001553static void close_sync(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
majianpeng79ef3a82013-11-15 14:55:02 +08001555 wait_barrier(conf, NULL);
1556 allow_barrier(conf, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 mempool_destroy(conf->r1buf_pool);
1559 conf->r1buf_pool = NULL;
majianpeng79ef3a82013-11-15 14:55:02 +08001560
NeilBrown669cc7b2014-09-04 16:30:38 +10001561 spin_lock_irq(&conf->resync_lock);
Jes Sorensene8ff8bf2015-09-16 10:20:05 -04001562 conf->next_resync = MaxSector - 2 * NEXT_NORMALIO_DISTANCE;
majianpeng79ef3a82013-11-15 14:55:02 +08001563 conf->start_next_window = MaxSector;
NeilBrown669cc7b2014-09-04 16:30:38 +10001564 conf->current_window_requests +=
1565 conf->next_window_requests;
1566 conf->next_window_requests = 0;
1567 spin_unlock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
NeilBrownfd01b882011-10-11 16:47:53 +11001570static int raid1_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 int i;
NeilBrowne8096362011-10-11 16:49:05 +11001573 struct r1conf *conf = mddev->private;
NeilBrown6b965622010-08-18 11:56:59 +10001574 int count = 0;
1575 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 /*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001578 * Find all failed disks within the RAID1 configuration
NeilBrownddac7c72006-08-31 21:27:36 -07001579 * and mark them readable.
1580 * Called under mddev lock, so rcu protection not needed.
NeilBrown423f04d2015-07-27 11:48:52 +10001581 * device_lock used to avoid races with raid1_end_read_request
1582 * which expects 'In_sync' flags and ->degraded to be consistent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 */
NeilBrown423f04d2015-07-27 11:48:52 +10001584 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001586 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrown8c7a2c22011-12-23 10:17:57 +11001587 struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev;
1588 if (repl
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001589 && !test_bit(Candidate, &repl->flags)
NeilBrown8c7a2c22011-12-23 10:17:57 +11001590 && repl->recovery_offset == MaxSector
1591 && !test_bit(Faulty, &repl->flags)
1592 && !test_and_set_bit(In_sync, &repl->flags)) {
1593 /* replacement has just become active */
1594 if (!rdev ||
1595 !test_and_clear_bit(In_sync, &rdev->flags))
1596 count++;
1597 if (rdev) {
1598 /* Replaced device not technically
1599 * faulty, but we need to be sure
1600 * it gets removed and never re-added
1601 */
1602 set_bit(Faulty, &rdev->flags);
1603 sysfs_notify_dirent_safe(
1604 rdev->sysfs_state);
1605 }
1606 }
NeilBrownddac7c72006-08-31 21:27:36 -07001607 if (rdev
Lukasz Dorau61e49472013-10-24 12:55:17 +11001608 && rdev->recovery_offset == MaxSector
NeilBrownddac7c72006-08-31 21:27:36 -07001609 && !test_bit(Faulty, &rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001610 && !test_and_set_bit(In_sync, &rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001611 count++;
Jonathan Brassow654e8b52011-07-27 11:00:36 +10001612 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
1614 }
NeilBrown6b965622010-08-18 11:56:59 +10001615 mddev->degraded -= count;
1616 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001619 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
NeilBrownfd01b882011-10-11 16:47:53 +11001622static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
NeilBrowne8096362011-10-11 16:49:05 +11001624 struct r1conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001625 int err = -EEXIST;
NeilBrown41158c72005-06-21 17:17:25 -07001626 int mirror = 0;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001627 struct raid1_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10001628 int first = 0;
NeilBrown30194632011-12-23 10:17:56 +11001629 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
NeilBrown53890422011-07-27 11:00:36 +10001631 if (mddev->recovery_disabled == conf->recovery_disabled)
1632 return -EBUSY;
1633
Dan Williams1501efa2016-01-13 16:00:07 -08001634 if (md_integrity_add_rdev(rdev, mddev))
1635 return -ENXIO;
1636
Neil Brown6c2fce22008-06-28 08:31:31 +10001637 if (rdev->raid_disk >= 0)
1638 first = last = rdev->raid_disk;
1639
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05001640 /*
1641 * find the disk ... but prefer rdev->saved_raid_disk
1642 * if possible.
1643 */
1644 if (rdev->saved_raid_disk >= 0 &&
1645 rdev->saved_raid_disk >= first &&
1646 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1647 first = last = rdev->saved_raid_disk;
1648
NeilBrown7ef449d2011-12-23 10:17:57 +11001649 for (mirror = first; mirror <= last; mirror++) {
1650 p = conf->mirrors+mirror;
1651 if (!p->rdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Jonathan Brassow9092c022013-05-02 14:19:24 -05001653 if (mddev->gendisk)
1654 disk_stack_limits(mddev->gendisk, rdev->bdev,
1655 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 p->head_position = 0;
1658 rdev->raid_disk = mirror;
Neil Brown199050e2008-06-28 08:31:33 +10001659 err = 0;
NeilBrown6aea114a2005-11-28 13:44:13 -08001660 /* As all devices are equivalent, we don't need a full recovery
1661 * if this was recently any drive of the array
1662 */
1663 if (rdev->saved_raid_disk < 0)
NeilBrown41158c72005-06-21 17:17:25 -07001664 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001665 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 break;
1667 }
NeilBrown7ef449d2011-12-23 10:17:57 +11001668 if (test_bit(WantReplacement, &p->rdev->flags) &&
1669 p[conf->raid_disks].rdev == NULL) {
1670 /* Add this device as a replacement */
1671 clear_bit(In_sync, &rdev->flags);
1672 set_bit(Replacement, &rdev->flags);
1673 rdev->raid_disk = mirror;
1674 err = 0;
1675 conf->fullsync = 1;
1676 rcu_assign_pointer(p[conf->raid_disks].rdev, rdev);
1677 break;
1678 }
1679 }
Jonathan Brassow9092c022013-05-02 14:19:24 -05001680 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
Shaohua Li2ff8cc22012-10-11 13:28:54 +11001681 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001683 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
NeilBrownb8321b62011-12-23 10:17:51 +11001686static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
NeilBrowne8096362011-10-11 16:49:05 +11001688 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001690 int number = rdev->raid_disk;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001691 struct raid1_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
NeilBrownb014f142011-12-23 10:17:56 +11001693 if (rdev != p->rdev)
1694 p = conf->mirrors + conf->raid_disks + number;
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 print_conf(conf);
NeilBrownb8321b62011-12-23 10:17:51 +11001697 if (rdev == p->rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001698 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 atomic_read(&rdev->nr_pending)) {
1700 err = -EBUSY;
1701 goto abort;
1702 }
NeilBrown046abee2010-10-26 15:46:20 +11001703 /* Only remove non-faulty devices if recovery
NeilBrowndfc70642008-05-23 13:04:39 -07001704 * is not possible.
1705 */
1706 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown53890422011-07-27 11:00:36 +10001707 mddev->recovery_disabled != conf->recovery_disabled &&
NeilBrowndfc70642008-05-23 13:04:39 -07001708 mddev->degraded < conf->raid_disks) {
1709 err = -EBUSY;
1710 goto abort;
1711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 p->rdev = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10001713 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1714 synchronize_rcu();
1715 if (atomic_read(&rdev->nr_pending)) {
1716 /* lost the race, try later */
1717 err = -EBUSY;
1718 p->rdev = rdev;
1719 goto abort;
1720 }
1721 }
1722 if (conf->mirrors[conf->raid_disks + number].rdev) {
NeilBrown8c7a2c22011-12-23 10:17:57 +11001723 /* We just removed a device that is being replaced.
1724 * Move down the replacement. We drain all IO before
1725 * doing this to avoid confusion.
1726 */
1727 struct md_rdev *repl =
1728 conf->mirrors[conf->raid_disks + number].rdev;
NeilBrowne2d59922013-06-12 11:01:22 +10001729 freeze_array(conf, 0);
NeilBrown8c7a2c22011-12-23 10:17:57 +11001730 clear_bit(Replacement, &repl->flags);
1731 p->rdev = repl;
1732 conf->mirrors[conf->raid_disks + number].rdev = NULL;
NeilBrowne2d59922013-06-12 11:01:22 +10001733 unfreeze_array(conf);
NeilBrownb014f142011-12-23 10:17:56 +11001734 clear_bit(WantReplacement, &rdev->flags);
NeilBrown8c7a2c22011-12-23 10:17:57 +11001735 } else
1736 clear_bit(WantReplacement, &rdev->flags);
Martin K. Petersena91a2782011-03-17 11:11:05 +01001737 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
1739abort:
1740
1741 print_conf(conf);
1742 return err;
1743}
1744
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001745static void end_sync_read(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001747 struct r1bio *r1_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
NeilBrown0fc280f2011-10-07 14:22:55 +11001749 update_head_pos(r1_bio->read_disk, r1_bio);
Namhyung Kimba3ae3b2011-10-07 14:22:53 +11001750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /*
1752 * we have read a block, now it needs to be re-written,
1753 * or re-read if the read failed.
1754 * We don't do much here, just schedule handling by raid1d
1755 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001756 if (!bio->bi_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrownd11c1712006-01-06 00:20:26 -08001758
1759 if (atomic_dec_and_test(&r1_bio->remaining))
1760 reschedule_retry(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001763static void end_sync_write(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001765 int uptodate = !bio->bi_error;
NeilBrown9f2c9d12011-10-11 16:48:43 +11001766 struct r1bio *r1_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001767 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11001768 struct r1conf *conf = mddev->private;
NeilBrown4367af52011-07-28 11:31:49 +10001769 sector_t first_bad;
1770 int bad_sectors;
NeilBrown854abd72016-06-02 16:19:52 +10001771 struct md_rdev *rdev = conf->mirrors[find_bio_disk(r1_bio, bio)].rdev;
Namhyung Kimba3ae3b2011-10-07 14:22:53 +11001772
NeilBrown6b1117d2006-03-31 02:31:57 -08001773 if (!uptodate) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001774 sector_t sync_blocks = 0;
NeilBrown6b1117d2006-03-31 02:31:57 -08001775 sector_t s = r1_bio->sector;
1776 long sectors_to_go = r1_bio->sectors;
1777 /* make sure these bits doesn't get cleared. */
1778 do {
NeilBrown5e3db642006-07-10 04:44:18 -07001779 bitmap_end_sync(mddev->bitmap, s,
NeilBrown6b1117d2006-03-31 02:31:57 -08001780 &sync_blocks, 1);
1781 s += sync_blocks;
1782 sectors_to_go -= sync_blocks;
1783 } while (sectors_to_go > 0);
NeilBrown854abd72016-06-02 16:19:52 +10001784 set_bit(WriteErrorSeen, &rdev->flags);
1785 if (!test_and_set_bit(WantReplacement, &rdev->flags))
NeilBrown19d67162011-12-23 10:17:57 +11001786 set_bit(MD_RECOVERY_NEEDED, &
1787 mddev->recovery);
NeilBrownd8f05d22011-07-28 11:33:00 +10001788 set_bit(R1BIO_WriteError, &r1_bio->state);
NeilBrown854abd72016-06-02 16:19:52 +10001789 } else if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
NeilBrown3a9f28a2011-07-28 11:33:42 +10001790 &first_bad, &bad_sectors) &&
1791 !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
1792 r1_bio->sector,
1793 r1_bio->sectors,
1794 &first_bad, &bad_sectors)
1795 )
NeilBrown4367af52011-07-28 11:31:49 +10001796 set_bit(R1BIO_MadeGood, &r1_bio->state);
NeilBrowne3b97032005-08-04 12:53:34 -07001797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown4367af52011-07-28 11:31:49 +10001799 int s = r1_bio->sectors;
NeilBrownd8f05d22011-07-28 11:33:00 +10001800 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
1801 test_bit(R1BIO_WriteError, &r1_bio->state))
NeilBrown4367af52011-07-28 11:31:49 +10001802 reschedule_retry(r1_bio);
1803 else {
1804 put_buf(r1_bio);
1805 md_done_sync(mddev, s, uptodate);
1806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808}
1809
NeilBrown3cb03002011-10-11 16:45:26 +11001810static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrownd8f05d22011-07-28 11:33:00 +10001811 int sectors, struct page *page, int rw)
1812{
Mike Christie796a5cf2016-06-05 14:32:07 -05001813 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
NeilBrownd8f05d22011-07-28 11:33:00 +10001814 /* success */
1815 return 1;
NeilBrown19d67162011-12-23 10:17:57 +11001816 if (rw == WRITE) {
NeilBrownd8f05d22011-07-28 11:33:00 +10001817 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrown19d67162011-12-23 10:17:57 +11001818 if (!test_and_set_bit(WantReplacement,
1819 &rdev->flags))
1820 set_bit(MD_RECOVERY_NEEDED, &
1821 rdev->mddev->recovery);
1822 }
NeilBrownd8f05d22011-07-28 11:33:00 +10001823 /* need to record an error - either for the block or the device */
1824 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1825 md_error(rdev->mddev, rdev);
1826 return 0;
1827}
1828
NeilBrown9f2c9d12011-10-11 16:48:43 +11001829static int fix_sync_read_error(struct r1bio *r1_bio)
NeilBrowna68e5872011-05-11 14:40:44 +10001830{
1831 /* Try some synchronous reads of other devices to get
1832 * good data, much like with normal read errors. Only
1833 * read into the pages we already have so we don't
1834 * need to re-issue the read request.
1835 * We don't need to freeze the array, because being in an
1836 * active sync request, there is no normal IO, and
1837 * no overlapping syncs.
NeilBrown06f60382011-07-28 11:31:48 +10001838 * We don't need to check is_badblock() again as we
1839 * made sure that anything with a bad block in range
1840 * will have bi_end_io clear.
NeilBrowna68e5872011-05-11 14:40:44 +10001841 */
NeilBrownfd01b882011-10-11 16:47:53 +11001842 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11001843 struct r1conf *conf = mddev->private;
NeilBrowna68e5872011-05-11 14:40:44 +10001844 struct bio *bio = r1_bio->bios[r1_bio->read_disk];
1845 sector_t sect = r1_bio->sector;
1846 int sectors = r1_bio->sectors;
1847 int idx = 0;
NeilBrown2e52d442016-11-18 16:16:12 +11001848 struct md_rdev *rdev;
1849
1850 rdev = conf->mirrors[r1_bio->read_disk].rdev;
1851 if (test_bit(FailFast, &rdev->flags)) {
1852 /* Don't try recovering from here - just fail it
1853 * ... unless it is the last working device of course */
1854 md_error(mddev, rdev);
1855 if (test_bit(Faulty, &rdev->flags))
1856 /* Don't try to read from here, but make sure
1857 * put_buf does it's thing
1858 */
1859 bio->bi_end_io = end_sync_write;
1860 }
NeilBrowna68e5872011-05-11 14:40:44 +10001861
1862 while(sectors) {
1863 int s = sectors;
1864 int d = r1_bio->read_disk;
1865 int success = 0;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001866 int start;
NeilBrowna68e5872011-05-11 14:40:44 +10001867
1868 if (s > (PAGE_SIZE>>9))
1869 s = PAGE_SIZE >> 9;
1870 do {
1871 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1872 /* No rcu protection needed here devices
1873 * can only be removed when no resync is
1874 * active, and resync is currently active
1875 */
1876 rdev = conf->mirrors[d].rdev;
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001877 if (sync_page_io(rdev, sect, s<<9,
NeilBrowna68e5872011-05-11 14:40:44 +10001878 bio->bi_io_vec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05001879 REQ_OP_READ, 0, false)) {
NeilBrowna68e5872011-05-11 14:40:44 +10001880 success = 1;
1881 break;
1882 }
1883 }
1884 d++;
NeilBrown8f19ccb2011-12-23 10:17:56 +11001885 if (d == conf->raid_disks * 2)
NeilBrowna68e5872011-05-11 14:40:44 +10001886 d = 0;
1887 } while (!success && d != r1_bio->read_disk);
1888
NeilBrown78d7f5f2011-05-11 14:48:56 +10001889 if (!success) {
NeilBrowna68e5872011-05-11 14:40:44 +10001890 char b[BDEVNAME_SIZE];
NeilBrown3a9f28a2011-07-28 11:33:42 +10001891 int abort = 0;
1892 /* Cannot read from anywhere, this block is lost.
1893 * Record a bad block on each device. If that doesn't
1894 * work just disable and interrupt the recovery.
1895 * Don't fail devices as that won't really help.
1896 */
NeilBrown1d41c212016-11-02 14:16:50 +11001897 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
1898 mdname(mddev),
1899 bdevname(bio->bi_bdev, b),
1900 (unsigned long long)r1_bio->sector);
NeilBrown8f19ccb2011-12-23 10:17:56 +11001901 for (d = 0; d < conf->raid_disks * 2; d++) {
NeilBrown3a9f28a2011-07-28 11:33:42 +10001902 rdev = conf->mirrors[d].rdev;
1903 if (!rdev || test_bit(Faulty, &rdev->flags))
1904 continue;
1905 if (!rdev_set_badblocks(rdev, sect, s, 0))
1906 abort = 1;
1907 }
1908 if (abort) {
NeilBrownd890fa22011-10-26 11:54:39 +11001909 conf->recovery_disabled =
1910 mddev->recovery_disabled;
NeilBrown3a9f28a2011-07-28 11:33:42 +10001911 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1912 md_done_sync(mddev, r1_bio->sectors, 0);
1913 put_buf(r1_bio);
1914 return 0;
1915 }
1916 /* Try next page */
1917 sectors -= s;
1918 sect += s;
1919 idx++;
1920 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10001921 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001922
1923 start = d;
1924 /* write it back and re-read */
1925 while (d != r1_bio->read_disk) {
1926 if (d == 0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11001927 d = conf->raid_disks * 2;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001928 d--;
1929 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1930 continue;
1931 rdev = conf->mirrors[d].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10001932 if (r1_sync_page_io(rdev, sect, s,
1933 bio->bi_io_vec[idx].bv_page,
1934 WRITE) == 0) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10001935 r1_bio->bios[d]->bi_end_io = NULL;
1936 rdev_dec_pending(rdev, mddev);
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001937 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001938 }
1939 d = start;
1940 while (d != r1_bio->read_disk) {
1941 if (d == 0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11001942 d = conf->raid_disks * 2;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001943 d--;
1944 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1945 continue;
1946 rdev = conf->mirrors[d].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10001947 if (r1_sync_page_io(rdev, sect, s,
1948 bio->bi_io_vec[idx].bv_page,
1949 READ) != 0)
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001950 atomic_add(s, &rdev->corrected_errors);
NeilBrown78d7f5f2011-05-11 14:48:56 +10001951 }
NeilBrowna68e5872011-05-11 14:40:44 +10001952 sectors -= s;
1953 sect += s;
1954 idx ++;
1955 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001956 set_bit(R1BIO_Uptodate, &r1_bio->state);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001957 bio->bi_error = 0;
NeilBrowna68e5872011-05-11 14:40:44 +10001958 return 1;
1959}
1960
NeilBrownc95e6382014-09-09 13:54:11 +10001961static void process_checks(struct r1bio *r1_bio)
NeilBrowna68e5872011-05-11 14:40:44 +10001962{
1963 /* We have read all readable devices. If we haven't
1964 * got the block, then there is no hope left.
1965 * If we have, then we want to do a comparison
1966 * and skip the write if everything is the same.
1967 * If any blocks failed to read, then we need to
1968 * attempt an over-write
1969 */
NeilBrownfd01b882011-10-11 16:47:53 +11001970 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11001971 struct r1conf *conf = mddev->private;
NeilBrowna68e5872011-05-11 14:40:44 +10001972 int primary;
1973 int i;
majianpengf4380a92012-04-12 16:04:47 +10001974 int vcnt;
NeilBrowna68e5872011-05-11 14:40:44 +10001975
NeilBrown30bc9b52013-07-17 15:19:29 +10001976 /* Fix variable parts of all bios */
1977 vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9);
1978 for (i = 0; i < conf->raid_disks * 2; i++) {
1979 int j;
1980 int size;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001981 int error;
NeilBrown30bc9b52013-07-17 15:19:29 +10001982 struct bio *b = r1_bio->bios[i];
1983 if (b->bi_end_io != end_sync_read)
1984 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001985 /* fixup the bio for reuse, but preserve errno */
1986 error = b->bi_error;
NeilBrown30bc9b52013-07-17 15:19:29 +10001987 bio_reset(b);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001988 b->bi_error = error;
NeilBrown30bc9b52013-07-17 15:19:29 +10001989 b->bi_vcnt = vcnt;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001990 b->bi_iter.bi_size = r1_bio->sectors << 9;
1991 b->bi_iter.bi_sector = r1_bio->sector +
NeilBrown30bc9b52013-07-17 15:19:29 +10001992 conf->mirrors[i].rdev->data_offset;
1993 b->bi_bdev = conf->mirrors[i].rdev->bdev;
1994 b->bi_end_io = end_sync_read;
1995 b->bi_private = r1_bio;
1996
Kent Overstreet4f024f32013-10-11 15:44:27 -07001997 size = b->bi_iter.bi_size;
NeilBrown30bc9b52013-07-17 15:19:29 +10001998 for (j = 0; j < vcnt ; j++) {
1999 struct bio_vec *bi;
2000 bi = &b->bi_io_vec[j];
2001 bi->bv_offset = 0;
2002 if (size > PAGE_SIZE)
2003 bi->bv_len = PAGE_SIZE;
2004 else
2005 bi->bv_len = size;
2006 size -= PAGE_SIZE;
2007 }
2008 }
NeilBrown8f19ccb2011-12-23 10:17:56 +11002009 for (primary = 0; primary < conf->raid_disks * 2; primary++)
NeilBrowna68e5872011-05-11 14:40:44 +10002010 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002011 !r1_bio->bios[primary]->bi_error) {
NeilBrowna68e5872011-05-11 14:40:44 +10002012 r1_bio->bios[primary]->bi_end_io = NULL;
2013 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
2014 break;
2015 }
2016 r1_bio->read_disk = primary;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002017 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10002018 int j;
NeilBrown78d7f5f2011-05-11 14:48:56 +10002019 struct bio *pbio = r1_bio->bios[primary];
2020 struct bio *sbio = r1_bio->bios[i];
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002021 int error = sbio->bi_error;
NeilBrowna68e5872011-05-11 14:40:44 +10002022
Kent Overstreet2aabaa62012-09-11 11:26:12 -07002023 if (sbio->bi_end_io != end_sync_read)
NeilBrown78d7f5f2011-05-11 14:48:56 +10002024 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002025 /* Now we can 'fixup' the error value */
2026 sbio->bi_error = 0;
NeilBrowna68e5872011-05-11 14:40:44 +10002027
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002028 if (!error) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10002029 for (j = vcnt; j-- ; ) {
2030 struct page *p, *s;
2031 p = pbio->bi_io_vec[j].bv_page;
2032 s = sbio->bi_io_vec[j].bv_page;
2033 if (memcmp(page_address(p),
2034 page_address(s),
NeilBrown5020ad72012-04-02 01:39:05 +10002035 sbio->bi_io_vec[j].bv_len))
NeilBrown78d7f5f2011-05-11 14:48:56 +10002036 break;
NeilBrowna68e5872011-05-11 14:40:44 +10002037 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10002038 } else
2039 j = 0;
2040 if (j >= 0)
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002041 atomic64_add(r1_bio->sectors, &mddev->resync_mismatches);
NeilBrown78d7f5f2011-05-11 14:48:56 +10002042 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002043 && !error)) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10002044 /* No need to write to this device. */
2045 sbio->bi_end_io = NULL;
2046 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
2047 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10002048 }
Kent Overstreetd3b45c22012-09-10 13:49:33 -07002049
2050 bio_copy_data(sbio, pbio);
NeilBrown78d7f5f2011-05-11 14:48:56 +10002051 }
NeilBrowna68e5872011-05-11 14:40:44 +10002052}
2053
NeilBrown9f2c9d12011-10-11 16:48:43 +11002054static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
NeilBrowne8096362011-10-11 16:49:05 +11002056 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 int i;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002058 int disks = conf->raid_disks * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct bio *bio, *wbio;
2060
2061 bio = r1_bio->bios[r1_bio->read_disk];
2062
NeilBrowna68e5872011-05-11 14:40:44 +10002063 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
2064 /* ouch - failed to read all of that. */
2065 if (!fix_sync_read_error(r1_bio))
2066 return;
NeilBrown7ca78d52011-05-11 14:50:37 +10002067
2068 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrownc95e6382014-09-09 13:54:11 +10002069 process_checks(r1_bio);
2070
NeilBrownd11c1712006-01-06 00:20:26 -08002071 /*
2072 * schedule writes
2073 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 atomic_set(&r1_bio->remaining, 1);
2075 for (i = 0; i < disks ; i++) {
2076 wbio = r1_bio->bios[i];
NeilBrown3e198f72006-01-06 00:20:21 -08002077 if (wbio->bi_end_io == NULL ||
2078 (wbio->bi_end_io == end_sync_read &&
2079 (i == r1_bio->read_disk ||
2080 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 continue;
2082
Mike Christie796a5cf2016-06-05 14:32:07 -05002083 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
NeilBrown212e7eb2016-11-18 16:16:12 +11002084 if (test_bit(FailFast, &conf->mirrors[i].rdev->flags))
2085 wbio->bi_opf |= MD_FAILFAST;
2086
NeilBrown3e198f72006-01-06 00:20:21 -08002087 wbio->bi_end_io = end_sync_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 atomic_inc(&r1_bio->remaining);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002089 md_sync_acct(conf->mirrors[i].rdev->bdev, bio_sectors(wbio));
NeilBrown191ea9b2005-06-21 17:17:23 -07002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 generic_make_request(wbio);
2092 }
2093
2094 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002095 /* if we're here, all write(s) have completed, so clean up */
NeilBrown58e94ae2012-07-19 15:59:18 +10002096 int s = r1_bio->sectors;
2097 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2098 test_bit(R1BIO_WriteError, &r1_bio->state))
2099 reschedule_retry(r1_bio);
2100 else {
2101 put_buf(r1_bio);
2102 md_done_sync(mddev, s, 1);
2103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
2105}
2106
2107/*
2108 * This is a kernel thread which:
2109 *
2110 * 1. Retries failed read operations on working mirrors.
2111 * 2. Updates the raid superblock when problems encounter.
NeilBrownd2eb35a2011-07-28 11:31:48 +10002112 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 */
2114
NeilBrowne8096362011-10-11 16:49:05 +11002115static void fix_read_error(struct r1conf *conf, int read_disk,
NeilBrown867868f2006-10-03 01:15:51 -07002116 sector_t sect, int sectors)
2117{
NeilBrownfd01b882011-10-11 16:47:53 +11002118 struct mddev *mddev = conf->mddev;
NeilBrown867868f2006-10-03 01:15:51 -07002119 while(sectors) {
2120 int s = sectors;
2121 int d = read_disk;
2122 int success = 0;
2123 int start;
NeilBrown3cb03002011-10-11 16:45:26 +11002124 struct md_rdev *rdev;
NeilBrown867868f2006-10-03 01:15:51 -07002125
2126 if (s > (PAGE_SIZE>>9))
2127 s = PAGE_SIZE >> 9;
2128
2129 do {
NeilBrownd2eb35a2011-07-28 11:31:48 +10002130 sector_t first_bad;
2131 int bad_sectors;
2132
NeilBrown707a6a42016-06-02 16:19:52 +10002133 rcu_read_lock();
2134 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002135 if (rdev &&
majianpengda8840a2012-05-22 13:55:03 +10002136 (test_bit(In_sync, &rdev->flags) ||
2137 (!test_bit(Faulty, &rdev->flags) &&
2138 rdev->recovery_offset >= sect + s)) &&
NeilBrownd2eb35a2011-07-28 11:31:48 +10002139 is_badblock(rdev, sect, s,
NeilBrown707a6a42016-06-02 16:19:52 +10002140 &first_bad, &bad_sectors) == 0) {
2141 atomic_inc(&rdev->nr_pending);
2142 rcu_read_unlock();
2143 if (sync_page_io(rdev, sect, s<<9,
Mike Christie796a5cf2016-06-05 14:32:07 -05002144 conf->tmppage, REQ_OP_READ, 0, false))
NeilBrown707a6a42016-06-02 16:19:52 +10002145 success = 1;
2146 rdev_dec_pending(rdev, mddev);
2147 if (success)
2148 break;
2149 } else
2150 rcu_read_unlock();
2151 d++;
2152 if (d == conf->raid_disks * 2)
2153 d = 0;
NeilBrown867868f2006-10-03 01:15:51 -07002154 } while (!success && d != read_disk);
2155
2156 if (!success) {
NeilBrownd8f05d22011-07-28 11:33:00 +10002157 /* Cannot read from anywhere - mark it bad */
NeilBrown3cb03002011-10-11 16:45:26 +11002158 struct md_rdev *rdev = conf->mirrors[read_disk].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10002159 if (!rdev_set_badblocks(rdev, sect, s, 0))
2160 md_error(mddev, rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002161 break;
2162 }
2163 /* write it back and re-read */
2164 start = d;
2165 while (d != read_disk) {
2166 if (d==0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002167 d = conf->raid_disks * 2;
NeilBrown867868f2006-10-03 01:15:51 -07002168 d--;
NeilBrown707a6a42016-06-02 16:19:52 +10002169 rcu_read_lock();
2170 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002171 if (rdev &&
NeilBrown707a6a42016-06-02 16:19:52 +10002172 !test_bit(Faulty, &rdev->flags)) {
2173 atomic_inc(&rdev->nr_pending);
2174 rcu_read_unlock();
NeilBrownd8f05d22011-07-28 11:33:00 +10002175 r1_sync_page_io(rdev, sect, s,
2176 conf->tmppage, WRITE);
NeilBrown707a6a42016-06-02 16:19:52 +10002177 rdev_dec_pending(rdev, mddev);
2178 } else
2179 rcu_read_unlock();
NeilBrown867868f2006-10-03 01:15:51 -07002180 }
2181 d = start;
2182 while (d != read_disk) {
2183 char b[BDEVNAME_SIZE];
2184 if (d==0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002185 d = conf->raid_disks * 2;
NeilBrown867868f2006-10-03 01:15:51 -07002186 d--;
NeilBrown707a6a42016-06-02 16:19:52 +10002187 rcu_read_lock();
2188 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002189 if (rdev &&
NeilBrownb8cb6b42014-09-18 11:09:04 +10002190 !test_bit(Faulty, &rdev->flags)) {
NeilBrown707a6a42016-06-02 16:19:52 +10002191 atomic_inc(&rdev->nr_pending);
2192 rcu_read_unlock();
NeilBrownd8f05d22011-07-28 11:33:00 +10002193 if (r1_sync_page_io(rdev, sect, s,
2194 conf->tmppage, READ)) {
NeilBrown867868f2006-10-03 01:15:51 -07002195 atomic_add(s, &rdev->corrected_errors);
NeilBrown1d41c212016-11-02 14:16:50 +11002196 pr_info("md/raid1:%s: read error corrected (%d sectors at %llu on %s)\n",
2197 mdname(mddev), s,
2198 (unsigned long long)(sect +
2199 rdev->data_offset),
2200 bdevname(rdev->bdev, b));
NeilBrown867868f2006-10-03 01:15:51 -07002201 }
NeilBrown707a6a42016-06-02 16:19:52 +10002202 rdev_dec_pending(rdev, mddev);
2203 } else
2204 rcu_read_unlock();
NeilBrown867868f2006-10-03 01:15:51 -07002205 }
2206 sectors -= s;
2207 sect += s;
2208 }
2209}
2210
NeilBrown9f2c9d12011-10-11 16:48:43 +11002211static int narrow_write_error(struct r1bio *r1_bio, int i)
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002212{
NeilBrownfd01b882011-10-11 16:47:53 +11002213 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11002214 struct r1conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002215 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002216
2217 /* bio has the data to be written to device 'i' where
2218 * we just recently had a write error.
2219 * We repeatedly clone the bio and trim down to one block,
2220 * then try the write. Where the write fails we record
2221 * a bad block.
2222 * It is conceivable that the bio doesn't exactly align with
2223 * blocks. We must handle this somehow.
2224 *
2225 * We currently own a reference on the rdev.
2226 */
2227
2228 int block_sectors;
2229 sector_t sector;
2230 int sectors;
2231 int sect_to_write = r1_bio->sectors;
2232 int ok = 1;
2233
2234 if (rdev->badblocks.shift < 0)
2235 return 0;
2236
Nate Daileyab713cd2015-02-12 12:02:09 -05002237 block_sectors = roundup(1 << rdev->badblocks.shift,
2238 bdev_logical_block_size(rdev->bdev) >> 9);
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002239 sector = r1_bio->sector;
2240 sectors = ((sector + block_sectors)
2241 & ~(sector_t)(block_sectors - 1))
2242 - sector;
2243
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002244 while (sect_to_write) {
2245 struct bio *wbio;
2246 if (sectors > sect_to_write)
2247 sectors = sect_to_write;
2248 /* Write at 'sector' for 'sectors'*/
2249
Kent Overstreetb7838632012-09-10 15:17:11 -07002250 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
2251 unsigned vcnt = r1_bio->behind_page_count;
2252 struct bio_vec *vec = r1_bio->behind_bvecs;
2253
2254 while (!vec->bv_page) {
2255 vec++;
2256 vcnt--;
2257 }
2258
2259 wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev);
2260 memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec));
2261
2262 wbio->bi_vcnt = vcnt;
2263 } else {
2264 wbio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
2265 }
2266
Mike Christie796a5cf2016-06-05 14:32:07 -05002267 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002268 wbio->bi_iter.bi_sector = r1_bio->sector;
2269 wbio->bi_iter.bi_size = r1_bio->sectors << 9;
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002270
Kent Overstreet6678d832013-08-07 11:14:32 -07002271 bio_trim(wbio, sector - r1_bio->sector, sectors);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002272 wbio->bi_iter.bi_sector += rdev->data_offset;
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002273 wbio->bi_bdev = rdev->bdev;
Mike Christie4e49ea42016-06-05 14:31:41 -05002274
2275 if (submit_bio_wait(wbio) < 0)
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002276 /* failure! */
2277 ok = rdev_set_badblocks(rdev, sector,
2278 sectors, 0)
2279 && ok;
2280
2281 bio_put(wbio);
2282 sect_to_write -= sectors;
2283 sector += sectors;
2284 sectors = block_sectors;
2285 }
2286 return ok;
2287}
2288
NeilBrowne8096362011-10-11 16:49:05 +11002289static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002290{
2291 int m;
2292 int s = r1_bio->sectors;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002293 for (m = 0; m < conf->raid_disks * 2 ; m++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002294 struct md_rdev *rdev = conf->mirrors[m].rdev;
NeilBrown62096bc2011-07-28 11:38:13 +10002295 struct bio *bio = r1_bio->bios[m];
2296 if (bio->bi_end_io == NULL)
2297 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002298 if (!bio->bi_error &&
NeilBrown62096bc2011-07-28 11:38:13 +10002299 test_bit(R1BIO_MadeGood, &r1_bio->state)) {
NeilBrownc6563a82012-05-21 09:27:00 +10002300 rdev_clear_badblocks(rdev, r1_bio->sector, s, 0);
NeilBrown62096bc2011-07-28 11:38:13 +10002301 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002302 if (bio->bi_error &&
NeilBrown62096bc2011-07-28 11:38:13 +10002303 test_bit(R1BIO_WriteError, &r1_bio->state)) {
2304 if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
2305 md_error(conf->mddev, rdev);
2306 }
2307 }
2308 put_buf(r1_bio);
2309 md_done_sync(conf->mddev, s, 1);
2310}
2311
NeilBrowne8096362011-10-11 16:49:05 +11002312static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002313{
2314 int m;
NeilBrown55ce74d2015-08-14 11:11:10 +10002315 bool fail = false;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002316 for (m = 0; m < conf->raid_disks * 2 ; m++)
NeilBrown62096bc2011-07-28 11:38:13 +10002317 if (r1_bio->bios[m] == IO_MADE_GOOD) {
NeilBrown3cb03002011-10-11 16:45:26 +11002318 struct md_rdev *rdev = conf->mirrors[m].rdev;
NeilBrown62096bc2011-07-28 11:38:13 +10002319 rdev_clear_badblocks(rdev,
2320 r1_bio->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10002321 r1_bio->sectors, 0);
NeilBrown62096bc2011-07-28 11:38:13 +10002322 rdev_dec_pending(rdev, conf->mddev);
2323 } else if (r1_bio->bios[m] != NULL) {
2324 /* This drive got a write error. We need to
2325 * narrow down and record precise write
2326 * errors.
2327 */
NeilBrown55ce74d2015-08-14 11:11:10 +10002328 fail = true;
NeilBrown62096bc2011-07-28 11:38:13 +10002329 if (!narrow_write_error(r1_bio, m)) {
2330 md_error(conf->mddev,
2331 conf->mirrors[m].rdev);
2332 /* an I/O failed, we can't clear the bitmap */
2333 set_bit(R1BIO_Degraded, &r1_bio->state);
2334 }
2335 rdev_dec_pending(conf->mirrors[m].rdev,
2336 conf->mddev);
2337 }
NeilBrown55ce74d2015-08-14 11:11:10 +10002338 if (fail) {
2339 spin_lock_irq(&conf->device_lock);
2340 list_add(&r1_bio->retry_list, &conf->bio_end_io_list);
Nate Daileyccfc7bf2016-02-29 10:43:58 -05002341 conf->nr_queued++;
NeilBrown55ce74d2015-08-14 11:11:10 +10002342 spin_unlock_irq(&conf->device_lock);
2343 md_wakeup_thread(conf->mddev->thread);
NeilBrownbd8688a2015-10-24 16:02:16 +11002344 } else {
2345 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2346 close_write(r1_bio);
NeilBrown55ce74d2015-08-14 11:11:10 +10002347 raid_end_bio_io(r1_bio);
NeilBrownbd8688a2015-10-24 16:02:16 +11002348 }
NeilBrown62096bc2011-07-28 11:38:13 +10002349}
2350
NeilBrowne8096362011-10-11 16:49:05 +11002351static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002352{
2353 int disk;
2354 int max_sectors;
NeilBrownfd01b882011-10-11 16:47:53 +11002355 struct mddev *mddev = conf->mddev;
NeilBrown62096bc2011-07-28 11:38:13 +10002356 struct bio *bio;
2357 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11002358 struct md_rdev *rdev;
NeilBrown109e3762016-11-18 13:22:04 +11002359 dev_t bio_dev;
2360 sector_t bio_sector;
NeilBrown62096bc2011-07-28 11:38:13 +10002361
2362 clear_bit(R1BIO_ReadError, &r1_bio->state);
2363 /* we got a read error. Maybe the drive is bad. Maybe just
2364 * the block and we can fix it.
2365 * We freeze all other IO, and try reading the block from
2366 * other devices. When we find one, we re-write
2367 * and check it that fixes the read error.
2368 * This is all done synchronously while the array is
2369 * frozen
2370 */
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002371
2372 bio = r1_bio->bios[r1_bio->read_disk];
2373 bdevname(bio->bi_bdev, b);
NeilBrown109e3762016-11-18 13:22:04 +11002374 bio_dev = bio->bi_bdev->bd_dev;
2375 bio_sector = conf->mirrors[r1_bio->read_disk].rdev->data_offset + r1_bio->sector;
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002376 bio_put(bio);
2377 r1_bio->bios[r1_bio->read_disk] = NULL;
2378
NeilBrown2e52d442016-11-18 16:16:12 +11002379 rdev = conf->mirrors[r1_bio->read_disk].rdev;
2380 if (mddev->ro == 0
2381 && !test_bit(FailFast, &rdev->flags)) {
NeilBrowne2d59922013-06-12 11:01:22 +10002382 freeze_array(conf, 1);
NeilBrown62096bc2011-07-28 11:38:13 +10002383 fix_read_error(conf, r1_bio->read_disk,
2384 r1_bio->sector, r1_bio->sectors);
2385 unfreeze_array(conf);
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002386 } else {
2387 r1_bio->bios[r1_bio->read_disk] = IO_BLOCKED;
2388 }
2389
NeilBrown2e52d442016-11-18 16:16:12 +11002390 rdev_dec_pending(rdev, conf->mddev);
NeilBrown62096bc2011-07-28 11:38:13 +10002391
NeilBrown62096bc2011-07-28 11:38:13 +10002392read_more:
2393 disk = read_balance(conf, r1_bio, &max_sectors);
2394 if (disk == -1) {
NeilBrown1d41c212016-11-02 14:16:50 +11002395 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
2396 mdname(mddev), b, (unsigned long long)r1_bio->sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002397 raid_end_bio_io(r1_bio);
2398 } else {
2399 const unsigned long do_sync
Jens Axboe1eff9d32016-08-05 15:35:16 -06002400 = r1_bio->master_bio->bi_opf & REQ_SYNC;
NeilBrown62096bc2011-07-28 11:38:13 +10002401 r1_bio->read_disk = disk;
2402 bio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002403 bio_trim(bio, r1_bio->sector - bio->bi_iter.bi_sector,
2404 max_sectors);
NeilBrown62096bc2011-07-28 11:38:13 +10002405 r1_bio->bios[r1_bio->read_disk] = bio;
2406 rdev = conf->mirrors[disk].rdev;
NeilBrown1d41c212016-11-02 14:16:50 +11002407 pr_info_ratelimited("md/raid1:%s: redirecting sector %llu to other mirror: %s\n",
2408 mdname(mddev),
2409 (unsigned long long)r1_bio->sector,
2410 bdevname(rdev->bdev, b));
Kent Overstreet4f024f32013-10-11 15:44:27 -07002411 bio->bi_iter.bi_sector = r1_bio->sector + rdev->data_offset;
NeilBrown62096bc2011-07-28 11:38:13 +10002412 bio->bi_bdev = rdev->bdev;
2413 bio->bi_end_io = raid1_end_read_request;
Mike Christie796a5cf2016-06-05 14:32:07 -05002414 bio_set_op_attrs(bio, REQ_OP_READ, do_sync);
NeilBrown2e52d442016-11-18 16:16:12 +11002415 if (test_bit(FailFast, &rdev->flags) &&
2416 test_bit(R1BIO_FailFast, &r1_bio->state))
2417 bio->bi_opf |= MD_FAILFAST;
NeilBrown62096bc2011-07-28 11:38:13 +10002418 bio->bi_private = r1_bio;
2419 if (max_sectors < r1_bio->sectors) {
2420 /* Drat - have to split this up more */
2421 struct bio *mbio = r1_bio->master_bio;
2422 int sectors_handled = (r1_bio->sector + max_sectors
Kent Overstreet4f024f32013-10-11 15:44:27 -07002423 - mbio->bi_iter.bi_sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002424 r1_bio->sectors = max_sectors;
2425 spin_lock_irq(&conf->device_lock);
2426 if (mbio->bi_phys_segments == 0)
2427 mbio->bi_phys_segments = 2;
2428 else
2429 mbio->bi_phys_segments++;
2430 spin_unlock_irq(&conf->device_lock);
NeilBrown109e3762016-11-18 13:22:04 +11002431 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2432 bio, bio_dev, bio_sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002433 generic_make_request(bio);
2434 bio = NULL;
2435
2436 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
2437
2438 r1_bio->master_bio = mbio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002439 r1_bio->sectors = bio_sectors(mbio) - sectors_handled;
NeilBrown62096bc2011-07-28 11:38:13 +10002440 r1_bio->state = 0;
2441 set_bit(R1BIO_ReadError, &r1_bio->state);
2442 r1_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002443 r1_bio->sector = mbio->bi_iter.bi_sector +
2444 sectors_handled;
NeilBrown62096bc2011-07-28 11:38:13 +10002445
2446 goto read_more;
NeilBrown109e3762016-11-18 13:22:04 +11002447 } else {
2448 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2449 bio, bio_dev, bio_sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002450 generic_make_request(bio);
NeilBrown109e3762016-11-18 13:22:04 +11002451 }
NeilBrown62096bc2011-07-28 11:38:13 +10002452 }
2453}
2454
Shaohua Li4ed87312012-10-11 13:34:00 +11002455static void raid1d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Shaohua Li4ed87312012-10-11 13:34:00 +11002457 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002458 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 unsigned long flags;
NeilBrowne8096362011-10-11 16:49:05 +11002460 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002462 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 md_check_recovery(mddev);
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002465
NeilBrown55ce74d2015-08-14 11:11:10 +10002466 if (!list_empty_careful(&conf->bio_end_io_list) &&
2467 !test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
2468 LIST_HEAD(tmp);
2469 spin_lock_irqsave(&conf->device_lock, flags);
2470 if (!test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
Nate Daileyccfc7bf2016-02-29 10:43:58 -05002471 while (!list_empty(&conf->bio_end_io_list)) {
2472 list_move(conf->bio_end_io_list.prev, &tmp);
2473 conf->nr_queued--;
2474 }
NeilBrown55ce74d2015-08-14 11:11:10 +10002475 }
2476 spin_unlock_irqrestore(&conf->device_lock, flags);
2477 while (!list_empty(&tmp)) {
Mikulas Patockaa4527442015-10-01 15:17:43 -04002478 r1_bio = list_first_entry(&tmp, struct r1bio,
2479 retry_list);
NeilBrown55ce74d2015-08-14 11:11:10 +10002480 list_del(&r1_bio->retry_list);
NeilBrownbd8688a2015-10-24 16:02:16 +11002481 if (mddev->degraded)
2482 set_bit(R1BIO_Degraded, &r1_bio->state);
2483 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2484 close_write(r1_bio);
NeilBrown55ce74d2015-08-14 11:11:10 +10002485 raid_end_bio_io(r1_bio);
2486 }
2487 }
2488
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002489 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002491
NeilBrown0021b7b2012-07-31 09:08:14 +02002492 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002495 if (list_empty(head)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002496 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002498 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002499 r1_bio = list_entry(head->prev, struct r1bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 list_del(head->prev);
NeilBrownddaf22a2006-01-06 00:20:19 -08002501 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 spin_unlock_irqrestore(&conf->device_lock, flags);
2503
2504 mddev = r1_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002505 conf = mddev->private;
NeilBrown4367af52011-07-28 11:31:49 +10002506 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
NeilBrownd8f05d22011-07-28 11:33:00 +10002507 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
NeilBrown62096bc2011-07-28 11:38:13 +10002508 test_bit(R1BIO_WriteError, &r1_bio->state))
2509 handle_sync_write_finished(conf, r1_bio);
2510 else
NeilBrown4367af52011-07-28 11:31:49 +10002511 sync_request_write(mddev, r1_bio);
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002512 } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
NeilBrown62096bc2011-07-28 11:38:13 +10002513 test_bit(R1BIO_WriteError, &r1_bio->state))
2514 handle_write_finished(conf, r1_bio);
2515 else if (test_bit(R1BIO_ReadError, &r1_bio->state))
2516 handle_read_error(conf, r1_bio);
2517 else
NeilBrownd2eb35a2011-07-28 11:31:48 +10002518 /* just a partial read to be scheduled from separate
2519 * context
2520 */
2521 generic_make_request(r1_bio->bios[r1_bio->read_disk]);
NeilBrown62096bc2011-07-28 11:38:13 +10002522
NeilBrown1d9d5242009-10-16 15:55:32 +11002523 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10002524 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2525 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002527 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528}
2529
NeilBrowne8096362011-10-11 16:49:05 +11002530static int init_resync(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
2532 int buffs;
2533
2534 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhenn9e77c482006-04-01 01:08:49 +02002535 BUG_ON(conf->r1buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
2537 conf->poolinfo);
2538 if (!conf->r1buf_pool)
2539 return -ENOMEM;
2540 conf->next_resync = 0;
2541 return 0;
2542}
2543
2544/*
2545 * perform a "sync" on one "block"
2546 *
2547 * We need to make sure that no normal I/O request - particularly write
2548 * requests - conflict with active sync requests.
2549 *
2550 * This is achieved by tracking pending requests and a 'barrier' concept
2551 * that can be installed to exclude normal IO requests.
2552 */
2553
Shaohua Li849674e2016-01-20 13:52:20 -08002554static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr,
2555 int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
NeilBrowne8096362011-10-11 16:49:05 +11002557 struct r1conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002558 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 struct bio *bio;
2560 sector_t max_sector, nr_sectors;
NeilBrown3e198f72006-01-06 00:20:21 -08002561 int disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 int i;
NeilBrown3e198f72006-01-06 00:20:21 -08002563 int wonly = -1;
2564 int write_targets = 0, read_targets = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11002565 sector_t sync_blocks;
NeilBrowne3b97032005-08-04 12:53:34 -07002566 int still_degraded = 0;
NeilBrown06f60382011-07-28 11:31:48 +10002567 int good_sectors = RESYNC_SECTORS;
2568 int min_bad = 0; /* number of sectors that are bad in all devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
2570 if (!conf->r1buf_pool)
2571 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Andre Noll58c0fed2009-03-31 14:33:13 +11002574 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 if (sector_nr >= max_sector) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002576 /* If we aborted, we need to abort the
2577 * sync on the 'current' bitmap chunk (there will
2578 * only be one in raid1 resync.
2579 * We can find the current addess in mddev->curr_resync
2580 */
NeilBrown6a806c52005-07-15 03:56:35 -07002581 if (mddev->curr_resync < max_sector) /* aborted */
2582 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
NeilBrown191ea9b2005-06-21 17:17:23 -07002583 &sync_blocks, 1);
NeilBrown6a806c52005-07-15 03:56:35 -07002584 else /* completed sync */
NeilBrown191ea9b2005-06-21 17:17:23 -07002585 conf->fullsync = 0;
NeilBrown6a806c52005-07-15 03:56:35 -07002586
2587 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 close_sync(conf);
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002589
2590 if (mddev_is_clustered(mddev)) {
2591 conf->cluster_sync_low = 0;
2592 conf->cluster_sync_high = 0;
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 return 0;
2595 }
2596
NeilBrown07d84d102006-06-26 00:27:56 -07002597 if (mddev->bitmap == NULL &&
2598 mddev->recovery_cp == MaxSector &&
NeilBrown6394cca2006-08-27 01:23:50 -07002599 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown07d84d102006-06-26 00:27:56 -07002600 conf->fullsync == 0) {
2601 *skipped = 1;
2602 return max_sector - sector_nr;
2603 }
NeilBrown6394cca2006-08-27 01:23:50 -07002604 /* before building a request, check if we can skip these blocks..
2605 * This call the bitmap_start_sync doesn't actually record anything
2606 */
NeilBrowne3b97032005-08-04 12:53:34 -07002607 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrowne5de485f2005-11-08 21:39:38 -08002608 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002609 /* We can skip this block, and probably several more */
2610 *skipped = 1;
2611 return sync_blocks;
2612 }
NeilBrown17999be2006-01-06 00:20:12 -08002613
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02002614 /*
2615 * If there is non-resync activity waiting for a turn, then let it
2616 * though before starting on this new sync request.
2617 */
2618 if (conf->nr_waiting)
2619 schedule_timeout_uninterruptible(1);
2620
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002621 /* we are incrementing sector_nr below. To be safe, we check against
2622 * sector_nr + two times RESYNC_SECTORS
2623 */
2624
2625 bitmap_cond_end_sync(mddev->bitmap, sector_nr,
2626 mddev_is_clustered(mddev) && (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
NeilBrown1c4588e2010-10-26 17:41:22 +11002627 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
NeilBrown17999be2006-01-06 00:20:12 -08002628
NeilBrownc2fd4c92014-09-10 16:01:24 +10002629 raise_barrier(conf, sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
NeilBrown3e198f72006-01-06 00:20:21 -08002631 rcu_read_lock();
2632 /*
2633 * If we get a correctably read error during resync or recovery,
2634 * we might want to read from a different device. So we
2635 * flag all drives that could conceivably be read from for READ,
2636 * and any others (which will be non-In_sync devices) for WRITE.
2637 * If a read fails, we try reading from something else for which READ
2638 * is OK.
2639 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 r1_bio->mddev = mddev;
2642 r1_bio->sector = sector_nr;
NeilBrown191ea9b2005-06-21 17:17:23 -07002643 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 set_bit(R1BIO_IsSync, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
NeilBrown8f19ccb2011-12-23 10:17:56 +11002646 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002647 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 bio = r1_bio->bios[i];
Kent Overstreet2aabaa62012-09-11 11:26:12 -07002649 bio_reset(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
NeilBrown3e198f72006-01-06 00:20:21 -08002651 rdev = rcu_dereference(conf->mirrors[i].rdev);
2652 if (rdev == NULL ||
NeilBrown06f60382011-07-28 11:31:48 +10002653 test_bit(Faulty, &rdev->flags)) {
NeilBrown8f19ccb2011-12-23 10:17:56 +11002654 if (i < conf->raid_disks)
2655 still_degraded = 1;
NeilBrown3e198f72006-01-06 00:20:21 -08002656 } else if (!test_bit(In_sync, &rdev->flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05002657 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 bio->bi_end_io = end_sync_write;
2659 write_targets ++;
NeilBrown3e198f72006-01-06 00:20:21 -08002660 } else {
2661 /* may need to read from here */
NeilBrown06f60382011-07-28 11:31:48 +10002662 sector_t first_bad = MaxSector;
2663 int bad_sectors;
2664
2665 if (is_badblock(rdev, sector_nr, good_sectors,
2666 &first_bad, &bad_sectors)) {
2667 if (first_bad > sector_nr)
2668 good_sectors = first_bad - sector_nr;
2669 else {
2670 bad_sectors -= (sector_nr - first_bad);
2671 if (min_bad == 0 ||
2672 min_bad > bad_sectors)
2673 min_bad = bad_sectors;
2674 }
NeilBrown3e198f72006-01-06 00:20:21 -08002675 }
NeilBrown06f60382011-07-28 11:31:48 +10002676 if (sector_nr < first_bad) {
2677 if (test_bit(WriteMostly, &rdev->flags)) {
2678 if (wonly < 0)
2679 wonly = i;
2680 } else {
2681 if (disk < 0)
2682 disk = i;
2683 }
Mike Christie796a5cf2016-06-05 14:32:07 -05002684 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown06f60382011-07-28 11:31:48 +10002685 bio->bi_end_io = end_sync_read;
2686 read_targets++;
Alexander Lyakasd57368a2012-07-17 13:17:55 +03002687 } else if (!test_bit(WriteErrorSeen, &rdev->flags) &&
2688 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
2689 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
2690 /*
2691 * The device is suitable for reading (InSync),
2692 * but has bad block(s) here. Let's try to correct them,
2693 * if we are doing resync or repair. Otherwise, leave
2694 * this device alone for this sync request.
2695 */
Mike Christie796a5cf2016-06-05 14:32:07 -05002696 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Alexander Lyakasd57368a2012-07-17 13:17:55 +03002697 bio->bi_end_io = end_sync_write;
2698 write_targets++;
NeilBrown06f60382011-07-28 11:31:48 +10002699 }
NeilBrown3e198f72006-01-06 00:20:21 -08002700 }
NeilBrown06f60382011-07-28 11:31:48 +10002701 if (bio->bi_end_io) {
2702 atomic_inc(&rdev->nr_pending);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002703 bio->bi_iter.bi_sector = sector_nr + rdev->data_offset;
NeilBrown06f60382011-07-28 11:31:48 +10002704 bio->bi_bdev = rdev->bdev;
2705 bio->bi_private = r1_bio;
NeilBrown2e52d442016-11-18 16:16:12 +11002706 if (test_bit(FailFast, &rdev->flags))
2707 bio->bi_opf |= MD_FAILFAST;
NeilBrown06f60382011-07-28 11:31:48 +10002708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
NeilBrown3e198f72006-01-06 00:20:21 -08002710 rcu_read_unlock();
2711 if (disk < 0)
2712 disk = wonly;
2713 r1_bio->read_disk = disk;
NeilBrown191ea9b2005-06-21 17:17:23 -07002714
NeilBrown06f60382011-07-28 11:31:48 +10002715 if (read_targets == 0 && min_bad > 0) {
2716 /* These sectors are bad on all InSync devices, so we
2717 * need to mark them bad on all write targets
2718 */
2719 int ok = 1;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002720 for (i = 0 ; i < conf->raid_disks * 2 ; i++)
NeilBrown06f60382011-07-28 11:31:48 +10002721 if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
majianpenga42f9d82012-04-02 01:04:19 +10002722 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrown06f60382011-07-28 11:31:48 +10002723 ok = rdev_set_badblocks(rdev, sector_nr,
2724 min_bad, 0
2725 ) && ok;
2726 }
2727 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2728 *skipped = 1;
2729 put_buf(r1_bio);
2730
2731 if (!ok) {
2732 /* Cannot record the badblocks, so need to
2733 * abort the resync.
2734 * If there are multiple read targets, could just
2735 * fail the really bad ones ???
2736 */
2737 conf->recovery_disabled = mddev->recovery_disabled;
2738 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2739 return 0;
2740 } else
2741 return min_bad;
2742
2743 }
2744 if (min_bad > 0 && min_bad < good_sectors) {
2745 /* only resync enough to reach the next bad->good
2746 * transition */
2747 good_sectors = min_bad;
2748 }
2749
NeilBrown3e198f72006-01-06 00:20:21 -08002750 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
2751 /* extra read targets are also write targets */
2752 write_targets += read_targets-1;
2753
2754 if (write_targets == 0 || read_targets == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 /* There is nowhere to write, so all non-sync
2756 * drives must be failed - so we are finished
2757 */
NeilBrownb7219cc2012-07-31 10:05:34 +10002758 sector_t rv;
2759 if (min_bad > 0)
2760 max_sector = sector_nr + min_bad;
2761 rv = max_sector - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07002762 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 put_buf(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return rv;
2765 }
2766
NeilBrownc6207272008-02-06 01:39:52 -08002767 if (max_sector > mddev->resync_max)
2768 max_sector = mddev->resync_max; /* Don't do IO beyond here */
NeilBrown06f60382011-07-28 11:31:48 +10002769 if (max_sector > sector_nr + good_sectors)
2770 max_sector = sector_nr + good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 nr_sectors = 0;
NeilBrown289e99e2005-06-21 17:17:24 -07002772 sync_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 do {
2774 struct page *page;
2775 int len = PAGE_SIZE;
2776 if (sector_nr + (len>>9) > max_sector)
2777 len = (max_sector - sector_nr) << 9;
2778 if (len == 0)
2779 break;
NeilBrown6a806c52005-07-15 03:56:35 -07002780 if (sync_blocks == 0) {
2781 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
NeilBrowne5de485f2005-11-08 21:39:38 -08002782 &sync_blocks, still_degraded) &&
2783 !conf->fullsync &&
2784 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrown6a806c52005-07-15 03:56:35 -07002785 break;
NeilBrown7571ae82010-10-07 11:54:46 +11002786 if ((len >> 9) > sync_blocks)
NeilBrown6a806c52005-07-15 03:56:35 -07002787 len = sync_blocks<<9;
NeilBrownab7a30c2005-06-21 17:17:23 -07002788 }
NeilBrown191ea9b2005-06-21 17:17:23 -07002789
NeilBrown8f19ccb2011-12-23 10:17:56 +11002790 for (i = 0 ; i < conf->raid_disks * 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 bio = r1_bio->bios[i];
2792 if (bio->bi_end_io) {
NeilBrownd11c1712006-01-06 00:20:26 -08002793 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (bio_add_page(bio, page, len, 0) == 0) {
2795 /* stop here */
NeilBrownd11c1712006-01-06 00:20:26 -08002796 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 while (i > 0) {
2798 i--;
2799 bio = r1_bio->bios[i];
NeilBrown6a806c52005-07-15 03:56:35 -07002800 if (bio->bi_end_io==NULL)
2801 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 /* remove last page from this bio */
2803 bio->bi_vcnt--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002804 bio->bi_iter.bi_size -= len;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06002805 bio_clear_flag(bio, BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 }
2807 goto bio_full;
2808 }
2809 }
2810 }
2811 nr_sectors += len>>9;
2812 sector_nr += len>>9;
NeilBrown191ea9b2005-06-21 17:17:23 -07002813 sync_blocks -= (len>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
2815 bio_full:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 r1_bio->sectors = nr_sectors;
2817
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002818 if (mddev_is_clustered(mddev) &&
2819 conf->cluster_sync_high < sector_nr + nr_sectors) {
2820 conf->cluster_sync_low = mddev->curr_resync_completed;
2821 conf->cluster_sync_high = conf->cluster_sync_low + CLUSTER_RESYNC_WINDOW_SECTORS;
2822 /* Send resync message */
2823 md_cluster_ops->resync_info_update(mddev,
2824 conf->cluster_sync_low,
2825 conf->cluster_sync_high);
2826 }
2827
NeilBrownd11c1712006-01-06 00:20:26 -08002828 /* For a user-requested sync, we read all readable devices and do a
2829 * compare
2830 */
2831 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2832 atomic_set(&r1_bio->remaining, read_targets);
NeilBrown2d4f4f32012-07-09 11:34:13 +10002833 for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) {
NeilBrownd11c1712006-01-06 00:20:26 -08002834 bio = r1_bio->bios[i];
2835 if (bio->bi_end_io == end_sync_read) {
NeilBrown2d4f4f32012-07-09 11:34:13 +10002836 read_targets--;
NeilBrownddac7c72006-08-31 21:27:36 -07002837 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrown2e52d442016-11-18 16:16:12 +11002838 if (read_targets == 1)
2839 bio->bi_opf &= ~MD_FAILFAST;
NeilBrownd11c1712006-01-06 00:20:26 -08002840 generic_make_request(bio);
2841 }
2842 }
2843 } else {
2844 atomic_set(&r1_bio->remaining, 1);
2845 bio = r1_bio->bios[r1_bio->read_disk];
NeilBrownddac7c72006-08-31 21:27:36 -07002846 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrown2e52d442016-11-18 16:16:12 +11002847 if (read_targets == 1)
2848 bio->bi_opf &= ~MD_FAILFAST;
NeilBrownd11c1712006-01-06 00:20:26 -08002849 generic_make_request(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
NeilBrownd11c1712006-01-06 00:20:26 -08002851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return nr_sectors;
2853}
2854
NeilBrownfd01b882011-10-11 16:47:53 +11002855static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07002856{
2857 if (sectors)
2858 return sectors;
2859
2860 return mddev->dev_sectors;
2861}
2862
NeilBrowne8096362011-10-11 16:49:05 +11002863static struct r1conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
NeilBrowne8096362011-10-11 16:49:05 +11002865 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11002866 int i;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10002867 struct raid1_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11002868 struct md_rdev *rdev;
NeilBrown709ae482009-12-14 12:49:51 +11002869 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
NeilBrowne8096362011-10-11 16:49:05 +11002871 conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 if (!conf)
NeilBrown709ae482009-12-14 12:49:51 +11002873 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10002875 conf->mirrors = kzalloc(sizeof(struct raid1_info)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002876 * mddev->raid_disks * 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 GFP_KERNEL);
2878 if (!conf->mirrors)
NeilBrown709ae482009-12-14 12:49:51 +11002879 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
NeilBrownddaf22a2006-01-06 00:20:19 -08002881 conf->tmppage = alloc_page(GFP_KERNEL);
2882 if (!conf->tmppage)
NeilBrown709ae482009-12-14 12:49:51 +11002883 goto abort;
NeilBrownddaf22a2006-01-06 00:20:19 -08002884
NeilBrown709ae482009-12-14 12:49:51 +11002885 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 if (!conf->poolinfo)
NeilBrown709ae482009-12-14 12:49:51 +11002887 goto abort;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002888 conf->poolinfo->raid_disks = mddev->raid_disks * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2890 r1bio_pool_free,
2891 conf->poolinfo);
2892 if (!conf->r1bio_pool)
NeilBrown709ae482009-12-14 12:49:51 +11002893 goto abort;
2894
NeilBrowned9bfdf2009-10-16 15:55:44 +11002895 conf->poolinfo->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
NeilBrownc19d5792011-12-23 10:17:57 +11002897 err = -EINVAL;
Neil Browne7e72bf2008-05-14 16:05:54 -07002898 spin_lock_init(&conf->device_lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11002899 rdev_for_each(rdev, mddev) {
NeilBrownaba336b2012-05-31 15:39:11 +10002900 struct request_queue *q;
NeilBrown709ae482009-12-14 12:49:51 +11002901 int disk_idx = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 if (disk_idx >= mddev->raid_disks
2903 || disk_idx < 0)
2904 continue;
NeilBrownc19d5792011-12-23 10:17:57 +11002905 if (test_bit(Replacement, &rdev->flags))
NeilBrown02b898f2012-10-31 11:42:03 +11002906 disk = conf->mirrors + mddev->raid_disks + disk_idx;
NeilBrownc19d5792011-12-23 10:17:57 +11002907 else
2908 disk = conf->mirrors + disk_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
NeilBrownc19d5792011-12-23 10:17:57 +11002910 if (disk->rdev)
2911 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 disk->rdev = rdev;
NeilBrownaba336b2012-05-31 15:39:11 +10002913 q = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
2915 disk->head_position = 0;
Shaohua Li12cee5a2012-07-31 10:03:53 +10002916 disk->seq_start = MaxSector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 }
2918 conf->raid_disks = mddev->raid_disks;
2919 conf->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 INIT_LIST_HEAD(&conf->retry_list);
NeilBrown55ce74d2015-08-14 11:11:10 +10002921 INIT_LIST_HEAD(&conf->bio_end_io_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
2923 spin_lock_init(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -08002924 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
NeilBrown191ea9b2005-06-21 17:17:23 -07002926 bio_list_init(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +11002927 conf->pending_count = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11002928 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown191ea9b2005-06-21 17:17:23 -07002929
majianpeng79ef3a82013-11-15 14:55:02 +08002930 conf->start_next_window = MaxSector;
2931 conf->current_window_requests = conf->next_window_requests = 0;
2932
NeilBrownc19d5792011-12-23 10:17:57 +11002933 err = -EIO;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002934 for (i = 0; i < conf->raid_disks * 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
2936 disk = conf->mirrors + i;
2937
NeilBrownc19d5792011-12-23 10:17:57 +11002938 if (i < conf->raid_disks &&
2939 disk[conf->raid_disks].rdev) {
2940 /* This slot has a replacement. */
2941 if (!disk->rdev) {
2942 /* No original, just make the replacement
2943 * a recovering spare
2944 */
2945 disk->rdev =
2946 disk[conf->raid_disks].rdev;
2947 disk[conf->raid_disks].rdev = NULL;
2948 } else if (!test_bit(In_sync, &disk->rdev->flags))
2949 /* Original is not in_sync - bad */
2950 goto abort;
2951 }
2952
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002953 if (!disk->rdev ||
2954 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 disk->head_position = 0;
Jonathan Brassow4f0a5e02012-05-22 13:55:31 +10002956 if (disk->rdev &&
2957 (disk->rdev->saved_raid_disk < 0))
NeilBrown918f0232007-08-22 14:01:52 -07002958 conf->fullsync = 1;
Shaohua Libe4d3282012-07-31 10:03:53 +10002959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 }
NeilBrown709ae482009-12-14 12:49:51 +11002961
NeilBrown709ae482009-12-14 12:49:51 +11002962 err = -ENOMEM;
NeilBrown02326052012-07-03 15:56:52 +10002963 conf->thread = md_register_thread(raid1d, mddev, "raid1");
NeilBrown1d41c212016-11-02 14:16:50 +11002964 if (!conf->thread)
NeilBrown709ae482009-12-14 12:49:51 +11002965 goto abort;
NeilBrown191ea9b2005-06-21 17:17:23 -07002966
NeilBrown709ae482009-12-14 12:49:51 +11002967 return conf;
2968
2969 abort:
2970 if (conf) {
Julia Lawall644df1a2015-09-13 14:15:10 +02002971 mempool_destroy(conf->r1bio_pool);
NeilBrown709ae482009-12-14 12:49:51 +11002972 kfree(conf->mirrors);
2973 safe_put_page(conf->tmppage);
2974 kfree(conf->poolinfo);
2975 kfree(conf);
2976 }
2977 return ERR_PTR(err);
2978}
2979
NeilBrownafa0f552014-12-15 12:56:58 +11002980static void raid1_free(struct mddev *mddev, void *priv);
Shaohua Li849674e2016-01-20 13:52:20 -08002981static int raid1_run(struct mddev *mddev)
NeilBrown709ae482009-12-14 12:49:51 +11002982{
NeilBrowne8096362011-10-11 16:49:05 +11002983 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11002984 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11002985 struct md_rdev *rdev;
majianpeng5220ea12012-04-02 09:48:38 +10002986 int ret;
Shaohua Li2ff8cc22012-10-11 13:28:54 +11002987 bool discard_supported = false;
NeilBrown709ae482009-12-14 12:49:51 +11002988
2989 if (mddev->level != 1) {
NeilBrown1d41c212016-11-02 14:16:50 +11002990 pr_warn("md/raid1:%s: raid level not set to mirroring (%d)\n",
2991 mdname(mddev), mddev->level);
NeilBrown709ae482009-12-14 12:49:51 +11002992 return -EIO;
2993 }
2994 if (mddev->reshape_position != MaxSector) {
NeilBrown1d41c212016-11-02 14:16:50 +11002995 pr_warn("md/raid1:%s: reshape_position set but not supported\n",
2996 mdname(mddev));
NeilBrown709ae482009-12-14 12:49:51 +11002997 return -EIO;
2998 }
2999 /*
3000 * copy the already verified devices into our private RAID1
3001 * bookkeeping area. [whatever we allocate in run(),
NeilBrownafa0f552014-12-15 12:56:58 +11003002 * should be freed in raid1_free()]
NeilBrown709ae482009-12-14 12:49:51 +11003003 */
3004 if (mddev->private == NULL)
3005 conf = setup_conf(mddev);
3006 else
3007 conf = mddev->private;
3008
3009 if (IS_ERR(conf))
3010 return PTR_ERR(conf);
3011
Joe Lawrencec8dc9c62013-02-21 13:28:09 +11003012 if (mddev->queue)
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07003013 blk_queue_max_write_same_sectors(mddev->queue, 0);
3014
NeilBrowndafb20f2012-03-19 12:46:39 +11003015 rdev_for_each(rdev, mddev) {
Jonathan Brassow1ed72422011-06-07 17:50:35 -05003016 if (!mddev->gendisk)
3017 continue;
NeilBrown709ae482009-12-14 12:49:51 +11003018 disk_stack_limits(mddev->gendisk, rdev->bdev,
3019 rdev->data_offset << 9);
Shaohua Li2ff8cc22012-10-11 13:28:54 +11003020 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3021 discard_supported = true;
NeilBrown709ae482009-12-14 12:49:51 +11003022 }
3023
3024 mddev->degraded = 0;
3025 for (i=0; i < conf->raid_disks; i++)
3026 if (conf->mirrors[i].rdev == NULL ||
3027 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
3028 test_bit(Faulty, &conf->mirrors[i].rdev->flags))
3029 mddev->degraded++;
3030
3031 if (conf->raid_disks - mddev->degraded == 1)
3032 mddev->recovery_cp = MaxSector;
3033
Andre Noll8c6ac8682009-06-18 08:48:06 +10003034 if (mddev->recovery_cp != MaxSector)
NeilBrown1d41c212016-11-02 14:16:50 +11003035 pr_info("md/raid1:%s: not clean -- starting background reconstruction\n",
3036 mdname(mddev));
3037 pr_info("md/raid1:%s: active with %d out of %d mirrors\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10003038 mdname(mddev), mddev->raid_disks - mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 mddev->raid_disks);
NeilBrown709ae482009-12-14 12:49:51 +11003040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 /*
3042 * Ok, everything is just fine now
3043 */
NeilBrown709ae482009-12-14 12:49:51 +11003044 mddev->thread = conf->thread;
3045 conf->thread = NULL;
3046 mddev->private = conf;
NeilBrown46533ff2016-11-18 16:16:11 +11003047 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
NeilBrown709ae482009-12-14 12:49:51 +11003048
Dan Williams1f403622009-03-31 14:59:03 +11003049 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Jonathan Brassow1ed72422011-06-07 17:50:35 -05003051 if (mddev->queue) {
Shaohua Li2ff8cc22012-10-11 13:28:54 +11003052 if (discard_supported)
3053 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3054 mddev->queue);
3055 else
3056 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3057 mddev->queue);
Jonathan Brassow1ed72422011-06-07 17:50:35 -05003058 }
majianpeng5220ea12012-04-02 09:48:38 +10003059
3060 ret = md_integrity_register(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003061 if (ret) {
3062 md_unregister_thread(&mddev->thread);
NeilBrownafa0f552014-12-15 12:56:58 +11003063 raid1_free(mddev, conf);
NeilBrown5aa61f42014-12-15 12:56:57 +11003064 }
majianpeng5220ea12012-04-02 09:48:38 +10003065 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
NeilBrownafa0f552014-12-15 12:56:58 +11003068static void raid1_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
NeilBrownafa0f552014-12-15 12:56:58 +11003070 struct r1conf *conf = priv;
NeilBrown4b6d2872005-09-09 16:23:47 -07003071
Julia Lawall644df1a2015-09-13 14:15:10 +02003072 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003073 kfree(conf->mirrors);
Hirokazu Takahashi0fea7ed2013-04-24 11:42:44 +10003074 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003075 kfree(conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
NeilBrownfd01b882011-10-11 16:47:53 +11003079static int raid1_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
3081 /* no resync is happening, and there is enough space
3082 * on all devices, so we can resize.
3083 * We need to make sure resync covers any new space.
3084 * If the array is shrinking we should possibly wait until
3085 * any io in the removed space completes, but it hardly seems
3086 * worth it.
3087 */
NeilBrowna4a61252012-05-22 13:55:27 +10003088 sector_t newsize = raid1_size(mddev, sectors, 0);
3089 if (mddev->external_size &&
3090 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11003091 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10003092 if (mddev->bitmap) {
3093 int ret = bitmap_resize(mddev->bitmap, newsize, 0, 0);
3094 if (ret)
3095 return ret;
3096 }
3097 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10003098 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003099 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003100 if (sectors > mddev->dev_sectors &&
NeilBrownb0986362011-05-11 15:52:21 +10003101 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003102 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3104 }
Dan Williamsb522adc2009-03-31 15:00:31 +11003105 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07003106 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 return 0;
3108}
3109
NeilBrownfd01b882011-10-11 16:47:53 +11003110static int raid1_reshape(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
3112 /* We need to:
3113 * 1/ resize the r1bio_pool
3114 * 2/ resize conf->mirrors
3115 *
3116 * We allocate a new r1bio_pool if we can.
3117 * Then raise a device barrier and wait until all IO stops.
3118 * Then resize conf->mirrors and swap in the new r1bio pool.
NeilBrown6ea9c072005-06-21 17:17:09 -07003119 *
3120 * At the same time, we "pack" the devices so that all the missing
3121 * devices have the higher raid_disk numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 */
3123 mempool_t *newpool, *oldpool;
3124 struct pool_info *newpoolinfo;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10003125 struct raid1_info *newmirrors;
NeilBrowne8096362011-10-11 16:49:05 +11003126 struct r1conf *conf = mddev->private;
NeilBrown63c70c42006-03-27 01:18:13 -08003127 int cnt, raid_disks;
NeilBrownc04be0a2006-10-03 01:15:53 -07003128 unsigned long flags;
Dan Williamsb5470dc2008-06-27 21:44:04 -07003129 int d, d2, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
NeilBrown63c70c42006-03-27 01:18:13 -08003131 /* Cannot change chunk_size, layout, or level */
Andre Noll664e7c42009-06-18 08:45:27 +10003132 if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
NeilBrown63c70c42006-03-27 01:18:13 -08003133 mddev->layout != mddev->new_layout ||
3134 mddev->level != mddev->new_level) {
Andre Noll664e7c42009-06-18 08:45:27 +10003135 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown63c70c42006-03-27 01:18:13 -08003136 mddev->new_layout = mddev->layout;
3137 mddev->new_level = mddev->level;
3138 return -EINVAL;
3139 }
3140
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003141 if (!mddev_is_clustered(mddev)) {
3142 err = md_allow_write(mddev);
3143 if (err)
3144 return err;
3145 }
NeilBrown2a2275d2007-01-26 00:57:11 -08003146
NeilBrown63c70c42006-03-27 01:18:13 -08003147 raid_disks = mddev->raid_disks + mddev->delta_disks;
3148
NeilBrown6ea9c072005-06-21 17:17:09 -07003149 if (raid_disks < conf->raid_disks) {
3150 cnt=0;
3151 for (d= 0; d < conf->raid_disks; d++)
3152 if (conf->mirrors[d].rdev)
3153 cnt++;
3154 if (cnt > raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return -EBUSY;
NeilBrown6ea9c072005-06-21 17:17:09 -07003156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
3158 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
3159 if (!newpoolinfo)
3160 return -ENOMEM;
3161 newpoolinfo->mddev = mddev;
NeilBrown8f19ccb2011-12-23 10:17:56 +11003162 newpoolinfo->raid_disks = raid_disks * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
3164 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
3165 r1bio_pool_free, newpoolinfo);
3166 if (!newpool) {
3167 kfree(newpoolinfo);
3168 return -ENOMEM;
3169 }
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10003170 newmirrors = kzalloc(sizeof(struct raid1_info) * raid_disks * 2,
NeilBrown8f19ccb2011-12-23 10:17:56 +11003171 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 if (!newmirrors) {
3173 kfree(newpoolinfo);
3174 mempool_destroy(newpool);
3175 return -ENOMEM;
3176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
NeilBrowne2d59922013-06-12 11:01:22 +10003178 freeze_array(conf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
3180 /* ok, everything is stopped */
3181 oldpool = conf->r1bio_pool;
3182 conf->r1bio_pool = newpool;
NeilBrown6ea9c072005-06-21 17:17:09 -07003183
NeilBrowna88aa782007-08-22 14:01:53 -07003184 for (d = d2 = 0; d < conf->raid_disks; d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11003185 struct md_rdev *rdev = conf->mirrors[d].rdev;
NeilBrowna88aa782007-08-22 14:01:53 -07003186 if (rdev && rdev->raid_disk != d2) {
Namhyung Kim36fad852011-07-27 11:00:36 +10003187 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna88aa782007-08-22 14:01:53 -07003188 rdev->raid_disk = d2;
Namhyung Kim36fad852011-07-27 11:00:36 +10003189 sysfs_unlink_rdev(mddev, rdev);
3190 if (sysfs_link_rdev(mddev, rdev))
NeilBrown1d41c212016-11-02 14:16:50 +11003191 pr_warn("md/raid1:%s: cannot register rd%d\n",
3192 mdname(mddev), rdev->raid_disk);
NeilBrown6ea9c072005-06-21 17:17:09 -07003193 }
NeilBrowna88aa782007-08-22 14:01:53 -07003194 if (rdev)
3195 newmirrors[d2++].rdev = rdev;
3196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 kfree(conf->mirrors);
3198 conf->mirrors = newmirrors;
3199 kfree(conf->poolinfo);
3200 conf->poolinfo = newpoolinfo;
3201
NeilBrownc04be0a2006-10-03 01:15:53 -07003202 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 mddev->degraded += (raid_disks - conf->raid_disks);
NeilBrownc04be0a2006-10-03 01:15:53 -07003204 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 conf->raid_disks = mddev->raid_disks = raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08003206 mddev->delta_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
NeilBrowne2d59922013-06-12 11:01:22 +10003208 unfreeze_array(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
NeilBrown985ca972015-07-06 12:26:57 +10003210 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3212 md_wakeup_thread(mddev->thread);
3213
3214 mempool_destroy(oldpool);
3215 return 0;
3216}
3217
NeilBrownfd01b882011-10-11 16:47:53 +11003218static void raid1_quiesce(struct mddev *mddev, int state)
NeilBrown36fa3062005-09-09 16:23:45 -07003219{
NeilBrowne8096362011-10-11 16:49:05 +11003220 struct r1conf *conf = mddev->private;
NeilBrown36fa3062005-09-09 16:23:45 -07003221
3222 switch(state) {
NeilBrown6eef4b22009-12-14 12:49:51 +11003223 case 2: /* wake for suspend */
3224 wake_up(&conf->wait_barrier);
3225 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07003226 case 1:
majianpeng07169fd2013-11-14 15:16:18 +11003227 freeze_array(conf, 0);
NeilBrown36fa3062005-09-09 16:23:45 -07003228 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07003229 case 0:
majianpeng07169fd2013-11-14 15:16:18 +11003230 unfreeze_array(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07003231 break;
3232 }
NeilBrown36fa3062005-09-09 16:23:45 -07003233}
3234
NeilBrownfd01b882011-10-11 16:47:53 +11003235static void *raid1_takeover(struct mddev *mddev)
NeilBrown709ae482009-12-14 12:49:51 +11003236{
3237 /* raid1 can take over:
3238 * raid5 with 2 devices, any layout or chunk size
3239 */
3240 if (mddev->level == 5 && mddev->raid_disks == 2) {
NeilBrowne8096362011-10-11 16:49:05 +11003241 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11003242 mddev->new_level = 1;
3243 mddev->new_layout = 0;
3244 mddev->new_chunk_sectors = 0;
3245 conf = setup_conf(mddev);
Shaohua Li6995f0b2016-12-08 15:48:17 -08003246 if (!IS_ERR(conf)) {
majianpeng07169fd2013-11-14 15:16:18 +11003247 /* Array must appear to be quiesced */
3248 conf->array_frozen = 1;
Shaohua Li6995f0b2016-12-08 15:48:17 -08003249 clear_bit(MD_HAS_JOURNAL, &mddev->flags);
3250 clear_bit(MD_JOURNAL_CLEAN, &mddev->flags);
3251 }
NeilBrown709ae482009-12-14 12:49:51 +11003252 return conf;
3253 }
3254 return ERR_PTR(-EINVAL);
3255}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
NeilBrown84fc4b52011-10-11 16:49:58 +11003257static struct md_personality raid1_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
3259 .name = "raid1",
NeilBrown2604b702006-01-06 00:20:36 -08003260 .level = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08003262 .make_request = raid1_make_request,
3263 .run = raid1_run,
NeilBrownafa0f552014-12-15 12:56:58 +11003264 .free = raid1_free,
Shaohua Li849674e2016-01-20 13:52:20 -08003265 .status = raid1_status,
3266 .error_handler = raid1_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 .hot_add_disk = raid1_add_disk,
3268 .hot_remove_disk= raid1_remove_disk,
3269 .spare_active = raid1_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08003270 .sync_request = raid1_sync_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 .resize = raid1_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07003272 .size = raid1_size,
NeilBrown63c70c42006-03-27 01:18:13 -08003273 .check_reshape = raid1_reshape,
NeilBrown36fa3062005-09-09 16:23:45 -07003274 .quiesce = raid1_quiesce,
NeilBrown709ae482009-12-14 12:49:51 +11003275 .takeover = raid1_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11003276 .congested = raid1_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277};
3278
3279static int __init raid_init(void)
3280{
NeilBrown2604b702006-01-06 00:20:36 -08003281 return register_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282}
3283
3284static void raid_exit(void)
3285{
NeilBrown2604b702006-01-06 00:20:36 -08003286 unregister_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287}
3288
3289module_init(raid_init);
3290module_exit(raid_exit);
3291MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11003292MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293MODULE_ALIAS("md-personality-3"); /* RAID1 */
NeilBrownd9d166c2006-01-06 00:20:51 -08003294MODULE_ALIAS("md-raid1");
NeilBrown2604b702006-01-06 00:20:36 -08003295MODULE_ALIAS("md-level-1");
NeilBrown34db0cd2011-10-11 16:50:01 +11003296
3297module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);