blob: 2dc1934925ecd0765cea7685aa6c86f64b3f3d0b [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
Al Virodd0fc662005-10-07 07:46:04 +010074static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 struct pool_info *pi = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +110077 int size = offsetof(struct r1bio, bios[pi->raid_disks]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 /* allocate a r1bio with room for raid_disks entries in the bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +010080 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
83static void r1bio_pool_free(void *r1_bio, void *data)
84{
85 kfree(r1_bio);
86}
87
88#define RESYNC_BLOCK_SIZE (64*1024)
majianpeng8e005f72013-11-14 15:16:18 +110089#define RESYNC_DEPTH 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
91#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
majianpeng8e005f72013-11-14 15:16:18 +110092#define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH)
93#define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9)
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +100094#define CLUSTER_RESYNC_WINDOW (16 * RESYNC_WINDOW)
95#define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
majianpeng8e005f72013-11-14 15:16:18 +110096#define NEXT_NORMALIO_DISTANCE (3 * RESYNC_WINDOW_SECTORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Al Virodd0fc662005-10-07 07:46:04 +010098static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 struct pool_info *pi = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100101 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 struct bio *bio;
NeilBrownda1aab32014-04-09 12:25:43 +1000103 int need_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 int i, j;
105
106 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100107 if (!r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 /*
111 * Allocate bios : 1 for reading, n-1 for writing
112 */
113 for (j = pi->raid_disks ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100114 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (!bio)
116 goto out_free_bio;
117 r1_bio->bios[j] = bio;
118 }
119 /*
120 * Allocate RESYNC_PAGES data pages and attach them to
NeilBrownd11c1712006-01-06 00:20:26 -0800121 * the first bio.
122 * If this is a user-requested check/repair, allocate
123 * RESYNC_PAGES for each bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 */
NeilBrownd11c1712006-01-06 00:20:26 -0800125 if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
NeilBrownda1aab32014-04-09 12:25:43 +1000126 need_pages = pi->raid_disks;
NeilBrownd11c1712006-01-06 00:20:26 -0800127 else
NeilBrownda1aab32014-04-09 12:25:43 +1000128 need_pages = 1;
129 for (j = 0; j < need_pages; j++) {
NeilBrownd11c1712006-01-06 00:20:26 -0800130 bio = r1_bio->bios[j];
Kent Overstreeta0787602012-09-10 14:03:28 -0700131 bio->bi_vcnt = RESYNC_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Kent Overstreeta0787602012-09-10 14:03:28 -0700133 if (bio_alloc_pages(bio, gfp_flags))
NeilBrownda1aab32014-04-09 12:25:43 +1000134 goto out_free_pages;
NeilBrownd11c1712006-01-06 00:20:26 -0800135 }
136 /* If not user-requests, copy the page pointers to all bios */
137 if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) {
138 for (i=0; i<RESYNC_PAGES ; i++)
139 for (j=1; j<pi->raid_disks; j++)
140 r1_bio->bios[j]->bi_io_vec[i].bv_page =
141 r1_bio->bios[0]->bi_io_vec[i].bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143
144 r1_bio->master_bio = NULL;
145
146 return r1_bio;
147
NeilBrownda1aab32014-04-09 12:25:43 +1000148out_free_pages:
Guoqing Jiang491221f2016-09-22 03:10:01 -0400149 while (--j >= 0)
150 bio_free_pages(r1_bio->bios[j]);
NeilBrownda1aab32014-04-09 12:25:43 +1000151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152out_free_bio:
NeilBrown8f19ccb2011-12-23 10:17:56 +1100153 while (++j < pi->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 bio_put(r1_bio->bios[j]);
155 r1bio_pool_free(r1_bio, data);
156 return NULL;
157}
158
159static void r1buf_pool_free(void *__r1_bio, void *data)
160{
161 struct pool_info *pi = data;
NeilBrownd11c1712006-01-06 00:20:26 -0800162 int i,j;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100163 struct r1bio *r1bio = __r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
NeilBrownd11c1712006-01-06 00:20:26 -0800165 for (i = 0; i < RESYNC_PAGES; i++)
166 for (j = pi->raid_disks; j-- ;) {
167 if (j == 0 ||
168 r1bio->bios[j]->bi_io_vec[i].bv_page !=
169 r1bio->bios[0]->bi_io_vec[i].bv_page)
NeilBrown1345b1d2006-01-06 00:20:40 -0800170 safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page);
NeilBrownd11c1712006-01-06 00:20:26 -0800171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 for (i=0 ; i < pi->raid_disks; i++)
173 bio_put(r1bio->bios[i]);
174
175 r1bio_pool_free(r1bio, data);
176}
177
NeilBrowne8096362011-10-11 16:49:05 +1100178static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 int i;
181
NeilBrown8f19ccb2011-12-23 10:17:56 +1100182 for (i = 0; i < conf->raid_disks * 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 struct bio **bio = r1_bio->bios + i;
NeilBrown4367af52011-07-28 11:31:49 +1000184 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 bio_put(*bio);
186 *bio = NULL;
187 }
188}
189
NeilBrown9f2c9d12011-10-11 16:48:43 +1100190static void free_r1bio(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
NeilBrowne8096362011-10-11 16:49:05 +1100192 struct r1conf *conf = r1_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 put_all_bios(conf, r1_bio);
195 mempool_free(r1_bio, conf->r1bio_pool);
196}
197
NeilBrown9f2c9d12011-10-11 16:48:43 +1100198static void put_buf(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
NeilBrowne8096362011-10-11 16:49:05 +1100200 struct r1conf *conf = r1_bio->mddev->private;
NeilBrown3e198f72006-01-06 00:20:21 -0800201 int i;
202
NeilBrown8f19ccb2011-12-23 10:17:56 +1100203 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown3e198f72006-01-06 00:20:21 -0800204 struct bio *bio = r1_bio->bios[i];
205 if (bio->bi_end_io)
206 rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 mempool_free(r1_bio, conf->r1buf_pool);
210
NeilBrown17999be2006-01-06 00:20:12 -0800211 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
NeilBrown9f2c9d12011-10-11 16:48:43 +1100214static void reschedule_retry(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100217 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +1100218 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 spin_lock_irqsave(&conf->device_lock, flags);
221 list_add(&r1_bio->retry_list, &conf->retry_list);
NeilBrownddaf22a2006-01-06 00:20:19 -0800222 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 spin_unlock_irqrestore(&conf->device_lock, flags);
224
NeilBrown17999be2006-01-06 00:20:12 -0800225 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 md_wakeup_thread(mddev->thread);
227}
228
229/*
230 * raid_end_bio_io() is called when we have finished servicing a mirrored
231 * operation and are ready to return a success/failure code to the buffer
232 * cache layer.
233 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100234static void call_bio_endio(struct r1bio *r1_bio)
NeilBrownd2eb35a2011-07-28 11:31:48 +1000235{
236 struct bio *bio = r1_bio->master_bio;
237 int done;
NeilBrowne8096362011-10-11 16:49:05 +1100238 struct r1conf *conf = r1_bio->mddev->private;
majianpeng79ef3a82013-11-15 14:55:02 +0800239 sector_t start_next_window = r1_bio->start_next_window;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700240 sector_t bi_sector = bio->bi_iter.bi_sector;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000241
242 if (bio->bi_phys_segments) {
243 unsigned long flags;
244 spin_lock_irqsave(&conf->device_lock, flags);
245 bio->bi_phys_segments--;
246 done = (bio->bi_phys_segments == 0);
247 spin_unlock_irqrestore(&conf->device_lock, flags);
majianpeng79ef3a82013-11-15 14:55:02 +0800248 /*
249 * make_request() might be waiting for
250 * bi_phys_segments to decrease
251 */
252 wake_up(&conf->wait_barrier);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000253 } else
254 done = 1;
255
256 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200257 bio->bi_error = -EIO;
258
NeilBrownd2eb35a2011-07-28 11:31:48 +1000259 if (done) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200260 bio_endio(bio);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000261 /*
262 * Wake up any possible resync thread that waits for the device
263 * to go idle.
264 */
majianpeng79ef3a82013-11-15 14:55:02 +0800265 allow_barrier(conf, start_next_window, bi_sector);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000266 }
267}
268
NeilBrown9f2c9d12011-10-11 16:48:43 +1100269static void raid_end_bio_io(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 struct bio *bio = r1_bio->master_bio;
272
NeilBrown4b6d2872005-09-09 16:23:47 -0700273 /* if nobody has done the final endio yet, do it now */
274 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
NeilBrown36a4e1f2011-10-07 14:23:17 +1100275 pr_debug("raid1: sync end %s on sectors %llu-%llu\n",
276 (bio_data_dir(bio) == WRITE) ? "write" : "read",
Kent Overstreet4f024f32013-10-11 15:44:27 -0700277 (unsigned long long) bio->bi_iter.bi_sector,
278 (unsigned long long) bio_end_sector(bio) - 1);
NeilBrown4b6d2872005-09-09 16:23:47 -0700279
NeilBrownd2eb35a2011-07-28 11:31:48 +1000280 call_bio_endio(r1_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -0700281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 free_r1bio(r1_bio);
283}
284
285/*
286 * Update disk head position estimator based on IRQ completion info.
287 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100288static inline void update_head_pos(int disk, struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
NeilBrowne8096362011-10-11 16:49:05 +1100290 struct r1conf *conf = r1_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 conf->mirrors[disk].head_position =
293 r1_bio->sector + (r1_bio->sectors);
294}
295
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100296/*
297 * Find the disk number which triggered given bio
298 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100299static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio)
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100300{
301 int mirror;
NeilBrown30194632011-12-23 10:17:56 +1100302 struct r1conf *conf = r1_bio->mddev->private;
303 int raid_disks = conf->raid_disks;
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100304
NeilBrown8f19ccb2011-12-23 10:17:56 +1100305 for (mirror = 0; mirror < raid_disks * 2; mirror++)
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100306 if (r1_bio->bios[mirror] == bio)
307 break;
308
NeilBrown8f19ccb2011-12-23 10:17:56 +1100309 BUG_ON(mirror == raid_disks * 2);
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100310 update_head_pos(mirror, r1_bio);
311
312 return mirror;
313}
314
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200315static void raid1_end_read_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200317 int uptodate = !bio->bi_error;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100318 struct r1bio *r1_bio = bio->bi_private;
NeilBrowne8096362011-10-11 16:49:05 +1100319 struct r1conf *conf = r1_bio->mddev->private;
NeilBrowne5872d52016-06-02 16:19:52 +1000320 struct md_rdev *rdev = conf->mirrors[r1_bio->read_disk].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /*
323 * this branch is our 'one mirror IO has finished' event handler:
324 */
NeilBrowne5872d52016-06-02 16:19:52 +1000325 update_head_pos(r1_bio->read_disk, r1_bio);
NeilBrownddaf22a2006-01-06 00:20:19 -0800326
NeilBrowndd00a992007-05-10 03:15:50 -0700327 if (uptodate)
328 set_bit(R1BIO_Uptodate, &r1_bio->state);
329 else {
330 /* If all other devices have failed, we want to return
331 * the error upwards rather than fail the last device.
332 * Here we redefine "uptodate" to mean "Don't want to retry"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 */
NeilBrowndd00a992007-05-10 03:15:50 -0700334 unsigned long flags;
335 spin_lock_irqsave(&conf->device_lock, flags);
336 if (r1_bio->mddev->degraded == conf->raid_disks ||
337 (r1_bio->mddev->degraded == conf->raid_disks-1 &&
NeilBrowne5872d52016-06-02 16:19:52 +1000338 test_bit(In_sync, &rdev->flags)))
NeilBrowndd00a992007-05-10 03:15:50 -0700339 uptodate = 1;
340 spin_unlock_irqrestore(&conf->device_lock, flags);
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
NeilBrown7ad4d4a2012-10-11 13:44:30 +1100343 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 raid_end_bio_io(r1_bio);
NeilBrowne5872d52016-06-02 16:19:52 +1000345 rdev_dec_pending(rdev, conf->mddev);
NeilBrown7ad4d4a2012-10-11 13:44:30 +1100346 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 /*
348 * oops, read error:
349 */
350 char b[BDEVNAME_SIZE];
NeilBrown1d41c212016-11-02 14:16:50 +1100351 pr_err_ratelimited("md/raid1:%s: %s: rescheduling sector %llu\n",
352 mdname(conf->mddev),
353 bdevname(rdev->bdev, b),
354 (unsigned long long)r1_bio->sector);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000355 set_bit(R1BIO_ReadError, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 reschedule_retry(r1_bio);
NeilBrown7ad4d4a2012-10-11 13:44:30 +1100357 /* don't drop the reference on read_disk yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
NeilBrown9f2c9d12011-10-11 16:48:43 +1100361static void close_write(struct r1bio *r1_bio)
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000362{
363 /* it really is the end of this request */
364 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
365 /* free extra copy of the data pages */
366 int i = r1_bio->behind_page_count;
367 while (i--)
368 safe_put_page(r1_bio->behind_bvecs[i].bv_page);
369 kfree(r1_bio->behind_bvecs);
370 r1_bio->behind_bvecs = NULL;
371 }
372 /* clear the bitmap if all writes complete successfully */
373 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
374 r1_bio->sectors,
375 !test_bit(R1BIO_Degraded, &r1_bio->state),
376 test_bit(R1BIO_BehindIO, &r1_bio->state));
377 md_write_end(r1_bio->mddev);
378}
379
NeilBrown9f2c9d12011-10-11 16:48:43 +1100380static void r1_bio_write_done(struct r1bio *r1_bio)
NeilBrown4e780642010-10-19 12:54:01 +1100381{
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000382 if (!atomic_dec_and_test(&r1_bio->remaining))
383 return;
384
385 if (test_bit(R1BIO_WriteError, &r1_bio->state))
386 reschedule_retry(r1_bio);
387 else {
388 close_write(r1_bio);
NeilBrown4367af52011-07-28 11:31:49 +1000389 if (test_bit(R1BIO_MadeGood, &r1_bio->state))
390 reschedule_retry(r1_bio);
391 else
392 raid_end_bio_io(r1_bio);
NeilBrown4e780642010-10-19 12:54:01 +1100393 }
394}
395
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200396static void raid1_end_write_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
NeilBrown9f2c9d12011-10-11 16:48:43 +1100398 struct r1bio *r1_bio = bio->bi_private;
NeilBrowne5872d52016-06-02 16:19:52 +1000399 int behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
NeilBrowne8096362011-10-11 16:49:05 +1100400 struct r1conf *conf = r1_bio->mddev->private;
NeilBrown04b857f2006-03-09 17:33:46 -0800401 struct bio *to_put = NULL;
NeilBrowne5872d52016-06-02 16:19:52 +1000402 int mirror = find_bio_disk(r1_bio, bio);
403 struct md_rdev *rdev = conf->mirrors[mirror].rdev;
Shaohua Lie3f948c2016-10-06 14:09:16 -0700404 bool discard_error;
405
406 discard_error = bio->bi_error && bio_op(bio) == REQ_OP_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Tejun Heoe9c74692010-09-03 11:56:18 +0200408 /*
409 * 'one mirror IO has finished' event handler:
410 */
Shaohua Lie3f948c2016-10-06 14:09:16 -0700411 if (bio->bi_error && !discard_error) {
NeilBrowne5872d52016-06-02 16:19:52 +1000412 set_bit(WriteErrorSeen, &rdev->flags);
413 if (!test_and_set_bit(WantReplacement, &rdev->flags))
NeilBrown19d67162011-12-23 10:17:57 +1100414 set_bit(MD_RECOVERY_NEEDED, &
415 conf->mddev->recovery);
416
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000417 set_bit(R1BIO_WriteError, &r1_bio->state);
NeilBrown4367af52011-07-28 11:31:49 +1000418 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /*
Tejun Heoe9c74692010-09-03 11:56:18 +0200420 * Set R1BIO_Uptodate in our master bio, so that we
421 * will return a good error code for to the higher
422 * levels even if IO on some other mirrored buffer
423 * fails.
424 *
425 * The 'master' represents the composite IO operation
426 * to user-side. So if something waits for IO, then it
427 * will wait for the 'master' bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 */
NeilBrown4367af52011-07-28 11:31:49 +1000429 sector_t first_bad;
430 int bad_sectors;
431
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000432 r1_bio->bios[mirror] = NULL;
433 to_put = bio;
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300434 /*
435 * Do not set R1BIO_Uptodate if the current device is
436 * rebuilding or Faulty. This is because we cannot use
437 * such device for properly reading the data back (we could
438 * potentially use it, if the current write would have felt
439 * before rdev->recovery_offset, but for simplicity we don't
440 * check this here.
441 */
NeilBrowne5872d52016-06-02 16:19:52 +1000442 if (test_bit(In_sync, &rdev->flags) &&
443 !test_bit(Faulty, &rdev->flags))
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300444 set_bit(R1BIO_Uptodate, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
NeilBrown4367af52011-07-28 11:31:49 +1000446 /* Maybe we can clear some bad blocks. */
NeilBrowne5872d52016-06-02 16:19:52 +1000447 if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
Shaohua Lie3f948c2016-10-06 14:09:16 -0700448 &first_bad, &bad_sectors) && !discard_error) {
NeilBrown4367af52011-07-28 11:31:49 +1000449 r1_bio->bios[mirror] = IO_MADE_GOOD;
450 set_bit(R1BIO_MadeGood, &r1_bio->state);
451 }
452 }
453
Tejun Heoe9c74692010-09-03 11:56:18 +0200454 if (behind) {
NeilBrowne5872d52016-06-02 16:19:52 +1000455 if (test_bit(WriteMostly, &rdev->flags))
Tejun Heoe9c74692010-09-03 11:56:18 +0200456 atomic_dec(&r1_bio->behind_remaining);
NeilBrown4b6d2872005-09-09 16:23:47 -0700457
Tejun Heoe9c74692010-09-03 11:56:18 +0200458 /*
459 * In behind mode, we ACK the master bio once the I/O
460 * has safely reached all non-writemostly
461 * disks. Setting the Returned bit ensures that this
462 * gets done only once -- we don't ever want to return
463 * -EIO here, instead we'll wait
464 */
465 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
466 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
467 /* Maybe we can return now */
468 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
469 struct bio *mbio = r1_bio->master_bio;
NeilBrown36a4e1f2011-10-07 14:23:17 +1100470 pr_debug("raid1: behind end write sectors"
471 " %llu-%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -0700472 (unsigned long long) mbio->bi_iter.bi_sector,
473 (unsigned long long) bio_end_sector(mbio) - 1);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000474 call_bio_endio(r1_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -0700475 }
476 }
477 }
NeilBrown4367af52011-07-28 11:31:49 +1000478 if (r1_bio->bios[mirror] == NULL)
NeilBrowne5872d52016-06-02 16:19:52 +1000479 rdev_dec_pending(rdev, conf->mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 * Let's see if all mirrored write operations have finished
483 * already.
484 */
NeilBrownaf6d7b72011-05-11 14:51:19 +1000485 r1_bio_write_done(r1_bio);
NeilBrownc70810b2006-06-26 00:27:35 -0700486
NeilBrown04b857f2006-03-09 17:33:46 -0800487 if (to_put)
488 bio_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/*
492 * This routine returns the disk from which the requested read should
493 * be done. There is a per-array 'next expected sequential IO' sector
494 * number - if this matches on the next IO then we use the last disk.
495 * There is also a per-disk 'last know head position' sector that is
496 * maintained from IRQ contexts, both the normal and the resync IO
497 * completion handlers update this position correctly. If there is no
498 * perfect sequential match then we pick the disk whose head is closest.
499 *
500 * If there are 2 mirrors in the same 2 devices, performance degrades
501 * because position is mirror, not device based.
502 *
503 * The rdev for the device selected will have nr_pending incremented.
504 */
NeilBrowne8096362011-10-11 16:49:05 +1100505static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000507 const sector_t this_sector = r1_bio->sector;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000508 int sectors;
509 int best_good_sectors;
Shaohua Li9dedf602012-07-31 10:03:53 +1000510 int best_disk, best_dist_disk, best_pending_disk;
511 int has_nonrot_disk;
Shaohua Libe4d3282012-07-31 10:03:53 +1000512 int disk;
NeilBrown76073052011-05-11 14:34:56 +1000513 sector_t best_dist;
Shaohua Li9dedf602012-07-31 10:03:53 +1000514 unsigned int min_pending;
NeilBrown3cb03002011-10-11 16:45:26 +1100515 struct md_rdev *rdev;
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000516 int choose_first;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000517 int choose_next_idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 rcu_read_lock();
520 /*
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700521 * Check if we can balance. We can balance on the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 * device if no resync is going on, or below the resync window.
523 * We take the first readable disk when above the resync window.
524 */
525 retry:
NeilBrownd2eb35a2011-07-28 11:31:48 +1000526 sectors = r1_bio->sectors;
NeilBrown76073052011-05-11 14:34:56 +1000527 best_disk = -1;
Shaohua Li9dedf602012-07-31 10:03:53 +1000528 best_dist_disk = -1;
NeilBrown76073052011-05-11 14:34:56 +1000529 best_dist = MaxSector;
Shaohua Li9dedf602012-07-31 10:03:53 +1000530 best_pending_disk = -1;
531 min_pending = UINT_MAX;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000532 best_good_sectors = 0;
Shaohua Li9dedf602012-07-31 10:03:53 +1000533 has_nonrot_disk = 0;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000534 choose_next_idle = 0;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000535
Goldwyn Rodrigues7d49ffc2014-08-12 10:13:19 -0500536 if ((conf->mddev->recovery_cp < this_sector + sectors) ||
537 (mddev_is_clustered(conf->mddev) &&
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500538 md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
Goldwyn Rodrigues7d49ffc2014-08-12 10:13:19 -0500539 this_sector + sectors)))
540 choose_first = 1;
541 else
542 choose_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Shaohua Libe4d3282012-07-31 10:03:53 +1000544 for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
NeilBrown76073052011-05-11 14:34:56 +1000545 sector_t dist;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000546 sector_t first_bad;
547 int bad_sectors;
Shaohua Li9dedf602012-07-31 10:03:53 +1000548 unsigned int pending;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000549 bool nonrot;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000550
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000551 rdev = rcu_dereference(conf->mirrors[disk].rdev);
552 if (r1_bio->bios[disk] == IO_BLOCKED
553 || rdev == NULL
NeilBrown76073052011-05-11 14:34:56 +1000554 || test_bit(Faulty, &rdev->flags))
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000555 continue;
NeilBrown76073052011-05-11 14:34:56 +1000556 if (!test_bit(In_sync, &rdev->flags) &&
557 rdev->recovery_offset < this_sector + sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 continue;
NeilBrown76073052011-05-11 14:34:56 +1000559 if (test_bit(WriteMostly, &rdev->flags)) {
560 /* Don't balance among write-mostly, just
561 * use the first as a last resort */
Tomáš Hodekd1901ef2015-02-23 11:00:38 +1100562 if (best_dist_disk < 0) {
NeilBrown307729c2012-01-09 01:41:51 +1100563 if (is_badblock(rdev, this_sector, sectors,
564 &first_bad, &bad_sectors)) {
Wei Fang816b0ac2016-03-21 19:18:32 +0800565 if (first_bad <= this_sector)
NeilBrown307729c2012-01-09 01:41:51 +1100566 /* Cannot use this */
567 continue;
568 best_good_sectors = first_bad - this_sector;
569 } else
570 best_good_sectors = sectors;
Tomáš Hodekd1901ef2015-02-23 11:00:38 +1100571 best_dist_disk = disk;
572 best_pending_disk = disk;
NeilBrown307729c2012-01-09 01:41:51 +1100573 }
NeilBrown76073052011-05-11 14:34:56 +1000574 continue;
575 }
576 /* This is a reasonable device to use. It might
577 * even be best.
578 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000579 if (is_badblock(rdev, this_sector, sectors,
580 &first_bad, &bad_sectors)) {
581 if (best_dist < MaxSector)
582 /* already have a better device */
583 continue;
584 if (first_bad <= this_sector) {
585 /* cannot read here. If this is the 'primary'
586 * device, then we must not read beyond
587 * bad_sectors from another device..
588 */
589 bad_sectors -= (this_sector - first_bad);
590 if (choose_first && sectors > bad_sectors)
591 sectors = bad_sectors;
592 if (best_good_sectors > sectors)
593 best_good_sectors = sectors;
594
595 } else {
596 sector_t good_sectors = first_bad - this_sector;
597 if (good_sectors > best_good_sectors) {
598 best_good_sectors = good_sectors;
599 best_disk = disk;
600 }
601 if (choose_first)
602 break;
603 }
604 continue;
605 } else
606 best_good_sectors = sectors;
607
Shaohua Li12cee5a2012-07-31 10:03:53 +1000608 nonrot = blk_queue_nonrot(bdev_get_queue(rdev->bdev));
609 has_nonrot_disk |= nonrot;
Shaohua Li9dedf602012-07-31 10:03:53 +1000610 pending = atomic_read(&rdev->nr_pending);
NeilBrown76073052011-05-11 14:34:56 +1000611 dist = abs(this_sector - conf->mirrors[disk].head_position);
Shaohua Li12cee5a2012-07-31 10:03:53 +1000612 if (choose_first) {
NeilBrown76073052011-05-11 14:34:56 +1000613 best_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 break;
615 }
Shaohua Li12cee5a2012-07-31 10:03:53 +1000616 /* Don't change to another disk for sequential reads */
617 if (conf->mirrors[disk].next_seq_sect == this_sector
618 || dist == 0) {
619 int opt_iosize = bdev_io_opt(rdev->bdev) >> 9;
620 struct raid1_info *mirror = &conf->mirrors[disk];
621
622 best_disk = disk;
623 /*
624 * If buffered sequential IO size exceeds optimal
625 * iosize, check if there is idle disk. If yes, choose
626 * the idle disk. read_balance could already choose an
627 * idle disk before noticing it's a sequential IO in
628 * this disk. This doesn't matter because this disk
629 * will idle, next time it will be utilized after the
630 * first disk has IO size exceeds optimal iosize. In
631 * this way, iosize of the first disk will be optimal
632 * iosize at least. iosize of the second disk might be
633 * small, but not a big deal since when the second disk
634 * starts IO, the first disk is likely still busy.
635 */
636 if (nonrot && opt_iosize > 0 &&
637 mirror->seq_start != MaxSector &&
638 mirror->next_seq_sect > opt_iosize &&
639 mirror->next_seq_sect - opt_iosize >=
640 mirror->seq_start) {
641 choose_next_idle = 1;
642 continue;
643 }
644 break;
645 }
646 /* If device is idle, use it */
647 if (pending == 0) {
648 best_disk = disk;
649 break;
650 }
651
652 if (choose_next_idle)
653 continue;
Shaohua Li9dedf602012-07-31 10:03:53 +1000654
655 if (min_pending > pending) {
656 min_pending = pending;
657 best_pending_disk = disk;
658 }
659
NeilBrown76073052011-05-11 14:34:56 +1000660 if (dist < best_dist) {
661 best_dist = dist;
Shaohua Li9dedf602012-07-31 10:03:53 +1000662 best_dist_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Shaohua Li9dedf602012-07-31 10:03:53 +1000666 /*
667 * If all disks are rotational, choose the closest disk. If any disk is
668 * non-rotational, choose the disk with less pending request even the
669 * disk is rotational, which might/might not be optimal for raids with
670 * mixed ratation/non-rotational disks depending on workload.
671 */
672 if (best_disk == -1) {
673 if (has_nonrot_disk)
674 best_disk = best_pending_disk;
675 else
676 best_disk = best_dist_disk;
677 }
678
NeilBrown76073052011-05-11 14:34:56 +1000679 if (best_disk >= 0) {
680 rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700681 if (!rdev)
682 goto retry;
683 atomic_inc(&rdev->nr_pending);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000684 sectors = best_good_sectors;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000685
686 if (conf->mirrors[best_disk].next_seq_sect != this_sector)
687 conf->mirrors[best_disk].seq_start = this_sector;
688
Shaohua Libe4d3282012-07-31 10:03:53 +1000689 conf->mirrors[best_disk].next_seq_sect = this_sector + sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691 rcu_read_unlock();
NeilBrownd2eb35a2011-07-28 11:31:48 +1000692 *max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
NeilBrown76073052011-05-11 14:34:56 +1000694 return best_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
NeilBrown5c675f82014-12-15 12:56:56 +1100697static int raid1_congested(struct mddev *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700698{
NeilBrowne8096362011-10-11 16:49:05 +1100699 struct r1conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700700 int i, ret = 0;
701
Tejun Heo44522262015-05-22 17:13:26 -0400702 if ((bits & (1 << WB_async_congested)) &&
NeilBrown34db0cd2011-10-11 16:50:01 +1100703 conf->pending_count >= max_queued_requests)
704 return 1;
705
NeilBrown0d129222006-10-03 01:15:54 -0700706 rcu_read_lock();
NeilBrownf53e29f2012-02-13 14:24:05 +1100707 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100708 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700709 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200710 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700711
Jonathan Brassow1ed72422011-06-07 17:50:35 -0500712 BUG_ON(!q);
713
NeilBrown0d129222006-10-03 01:15:54 -0700714 /* Note the '|| 1' - when read_balance prefers
715 * non-congested targets, it can be removed
716 */
Tejun Heo44522262015-05-22 17:13:26 -0400717 if ((bits & (1 << WB_async_congested)) || 1)
NeilBrown0d129222006-10-03 01:15:54 -0700718 ret |= bdi_congested(&q->backing_dev_info, bits);
719 else
720 ret &= bdi_congested(&q->backing_dev_info, bits);
721 }
722 }
723 rcu_read_unlock();
724 return ret;
725}
NeilBrown0d129222006-10-03 01:15:54 -0700726
NeilBrowne8096362011-10-11 16:49:05 +1100727static void flush_pending_writes(struct r1conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800728{
729 /* Any writes that have been queued but are awaiting
730 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800731 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800732 spin_lock_irq(&conf->device_lock);
733
734 if (conf->pending_bio_list.head) {
735 struct bio *bio;
736 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100737 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800738 spin_unlock_irq(&conf->device_lock);
739 /* flush any pending bitmap writes to
740 * disk before proceeding w/ I/O */
741 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100742 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800743
744 while (bio) { /* submit pending writes */
745 struct bio *next = bio->bi_next;
NeilBrown5e2c7a32016-11-04 16:46:03 +1100746 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrowna35e63e2008-03-04 14:29:29 -0800747 bio->bi_next = NULL;
NeilBrown5e2c7a32016-11-04 16:46:03 +1100748 bio->bi_bdev = rdev->bdev;
749 if (test_bit(Faulty, &rdev->flags)) {
750 bio->bi_error = -EIO;
751 bio_endio(bio);
752 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
753 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li2ff8cc22012-10-11 13:28:54 +1100754 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200755 bio_endio(bio);
Shaohua Li2ff8cc22012-10-11 13:28:54 +1100756 else
757 generic_make_request(bio);
NeilBrowna35e63e2008-03-04 14:29:29 -0800758 bio = next;
759 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800760 } else
761 spin_unlock_irq(&conf->device_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100762}
763
NeilBrown17999be2006-01-06 00:20:12 -0800764/* Barriers....
765 * Sometimes we need to suspend IO while we do something else,
766 * either some resync/recovery, or reconfigure the array.
767 * To do this we raise a 'barrier'.
768 * The 'barrier' is a counter that can be raised multiple times
769 * to count how many activities are happening which preclude
770 * normal IO.
771 * We can only raise the barrier if there is no pending IO.
772 * i.e. if nr_pending == 0.
773 * We choose only to raise the barrier if no-one is waiting for the
774 * barrier to go down. This means that as soon as an IO request
775 * is ready, no other operations which require a barrier will start
776 * until the IO request has had a chance.
777 *
778 * So: regular IO calls 'wait_barrier'. When that returns there
779 * is no backgroup IO happening, It must arrange to call
780 * allow_barrier when it has finished its IO.
781 * backgroup IO calls must call raise_barrier. Once that returns
782 * there is no normal IO happeing. It must arrange to call
783 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
NeilBrownc2fd4c92014-09-10 16:01:24 +1000785static void raise_barrier(struct r1conf *conf, sector_t sector_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 spin_lock_irq(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800788
789 /* Wait until no block IO is waiting */
790 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
Lukas Czernereed8c022012-11-30 11:42:40 +0100791 conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800792
793 /* block any new IO from starting */
794 conf->barrier++;
NeilBrownc2fd4c92014-09-10 16:01:24 +1000795 conf->next_resync = sector_nr;
NeilBrown17999be2006-01-06 00:20:12 -0800796
majianpeng79ef3a82013-11-15 14:55:02 +0800797 /* For these conditions we must wait:
798 * A: while the array is in frozen state
799 * B: while barrier >= RESYNC_DEPTH, meaning resync reach
800 * the max count which allowed.
801 * C: next_resync + RESYNC_SECTORS > start_next_window, meaning
802 * next resync will reach to the window which normal bios are
803 * handling.
NeilBrown2f73d3c2014-09-10 15:01:49 +1000804 * D: while there are any active requests in the current window.
majianpeng79ef3a82013-11-15 14:55:02 +0800805 */
NeilBrown17999be2006-01-06 00:20:12 -0800806 wait_event_lock_irq(conf->wait_barrier,
majianpengb364e3d2013-11-14 15:16:18 +1100807 !conf->array_frozen &&
majianpeng79ef3a82013-11-15 14:55:02 +0800808 conf->barrier < RESYNC_DEPTH &&
NeilBrown2f73d3c2014-09-10 15:01:49 +1000809 conf->current_window_requests == 0 &&
majianpeng79ef3a82013-11-15 14:55:02 +0800810 (conf->start_next_window >=
811 conf->next_resync + RESYNC_SECTORS),
Lukas Czernereed8c022012-11-30 11:42:40 +0100812 conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800813
NeilBrown34e97f12014-09-16 12:14:14 +1000814 conf->nr_pending++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 spin_unlock_irq(&conf->resync_lock);
816}
817
NeilBrowne8096362011-10-11 16:49:05 +1100818static void lower_barrier(struct r1conf *conf)
NeilBrown17999be2006-01-06 00:20:12 -0800819{
820 unsigned long flags;
NeilBrown709ae482009-12-14 12:49:51 +1100821 BUG_ON(conf->barrier <= 0);
NeilBrown17999be2006-01-06 00:20:12 -0800822 spin_lock_irqsave(&conf->resync_lock, flags);
823 conf->barrier--;
NeilBrown34e97f12014-09-16 12:14:14 +1000824 conf->nr_pending--;
NeilBrown17999be2006-01-06 00:20:12 -0800825 spin_unlock_irqrestore(&conf->resync_lock, flags);
826 wake_up(&conf->wait_barrier);
827}
828
majianpeng79ef3a82013-11-15 14:55:02 +0800829static bool need_to_wait_for_sync(struct r1conf *conf, struct bio *bio)
NeilBrown17999be2006-01-06 00:20:12 -0800830{
majianpeng79ef3a82013-11-15 14:55:02 +0800831 bool wait = false;
832
833 if (conf->array_frozen || !bio)
834 wait = true;
835 else if (conf->barrier && bio_data_dir(bio) == WRITE) {
NeilBrown23554962014-09-10 15:56:57 +1000836 if ((conf->mddev->curr_resync_completed
837 >= bio_end_sector(bio)) ||
NeilBrownf2c771a2016-11-09 10:21:32 +1100838 (conf->start_next_window + NEXT_NORMALIO_DISTANCE
NeilBrown23554962014-09-10 15:56:57 +1000839 <= bio->bi_iter.bi_sector))
majianpeng79ef3a82013-11-15 14:55:02 +0800840 wait = false;
841 else
842 wait = true;
843 }
844
845 return wait;
846}
847
848static sector_t wait_barrier(struct r1conf *conf, struct bio *bio)
849{
850 sector_t sector = 0;
851
NeilBrown17999be2006-01-06 00:20:12 -0800852 spin_lock_irq(&conf->resync_lock);
majianpeng79ef3a82013-11-15 14:55:02 +0800853 if (need_to_wait_for_sync(conf, bio)) {
NeilBrown17999be2006-01-06 00:20:12 -0800854 conf->nr_waiting++;
NeilBrownd6b42dc2012-03-19 12:46:38 +1100855 /* Wait for the barrier to drop.
856 * However if there are already pending
857 * requests (preventing the barrier from
858 * rising completely), and the
NeilBrown5965b642014-09-04 15:51:44 +1000859 * per-process bio queue isn't empty,
NeilBrownd6b42dc2012-03-19 12:46:38 +1100860 * then don't wait, as we need to empty
NeilBrown5965b642014-09-04 15:51:44 +1000861 * that queue to allow conf->start_next_window
862 * to increase.
NeilBrownd6b42dc2012-03-19 12:46:38 +1100863 */
864 wait_event_lock_irq(conf->wait_barrier,
majianpengb364e3d2013-11-14 15:16:18 +1100865 !conf->array_frozen &&
866 (!conf->barrier ||
NeilBrown5965b642014-09-04 15:51:44 +1000867 ((conf->start_next_window <
868 conf->next_resync + RESYNC_SECTORS) &&
869 current->bio_list &&
870 !bio_list_empty(current->bio_list))),
Lukas Czernereed8c022012-11-30 11:42:40 +0100871 conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800872 conf->nr_waiting--;
873 }
majianpeng79ef3a82013-11-15 14:55:02 +0800874
875 if (bio && bio_data_dir(bio) == WRITE) {
Jes Sorensene8ff8bf2015-09-16 10:20:05 -0400876 if (bio->bi_iter.bi_sector >= conf->next_resync) {
majianpeng79ef3a82013-11-15 14:55:02 +0800877 if (conf->start_next_window == MaxSector)
878 conf->start_next_window =
879 conf->next_resync +
880 NEXT_NORMALIO_DISTANCE;
881
882 if ((conf->start_next_window + NEXT_NORMALIO_DISTANCE)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700883 <= bio->bi_iter.bi_sector)
majianpeng79ef3a82013-11-15 14:55:02 +0800884 conf->next_window_requests++;
885 else
886 conf->current_window_requests++;
majianpeng79ef3a82013-11-15 14:55:02 +0800887 sector = conf->start_next_window;
NeilBrown41a336e2014-01-14 11:56:14 +1100888 }
majianpeng79ef3a82013-11-15 14:55:02 +0800889 }
890
NeilBrown17999be2006-01-06 00:20:12 -0800891 conf->nr_pending++;
892 spin_unlock_irq(&conf->resync_lock);
majianpeng79ef3a82013-11-15 14:55:02 +0800893 return sector;
NeilBrown17999be2006-01-06 00:20:12 -0800894}
895
majianpeng79ef3a82013-11-15 14:55:02 +0800896static void allow_barrier(struct r1conf *conf, sector_t start_next_window,
897 sector_t bi_sector)
NeilBrown17999be2006-01-06 00:20:12 -0800898{
899 unsigned long flags;
majianpeng79ef3a82013-11-15 14:55:02 +0800900
NeilBrown17999be2006-01-06 00:20:12 -0800901 spin_lock_irqsave(&conf->resync_lock, flags);
902 conf->nr_pending--;
majianpeng79ef3a82013-11-15 14:55:02 +0800903 if (start_next_window) {
904 if (start_next_window == conf->start_next_window) {
905 if (conf->start_next_window + NEXT_NORMALIO_DISTANCE
906 <= bi_sector)
907 conf->next_window_requests--;
908 else
909 conf->current_window_requests--;
910 } else
911 conf->current_window_requests--;
912
913 if (!conf->current_window_requests) {
914 if (conf->next_window_requests) {
915 conf->current_window_requests =
916 conf->next_window_requests;
917 conf->next_window_requests = 0;
918 conf->start_next_window +=
919 NEXT_NORMALIO_DISTANCE;
920 } else
921 conf->start_next_window = MaxSector;
922 }
923 }
NeilBrown17999be2006-01-06 00:20:12 -0800924 spin_unlock_irqrestore(&conf->resync_lock, flags);
925 wake_up(&conf->wait_barrier);
926}
927
NeilBrowne2d59922013-06-12 11:01:22 +1000928static void freeze_array(struct r1conf *conf, int extra)
NeilBrownddaf22a2006-01-06 00:20:19 -0800929{
930 /* stop syncio and normal IO and wait for everything to
931 * go quite.
majianpengb364e3d2013-11-14 15:16:18 +1100932 * We wait until nr_pending match nr_queued+extra
NeilBrown1c830532008-03-04 14:29:35 -0800933 * This is called in the context of one normal IO request
934 * that has failed. Thus any sync request that might be pending
935 * will be blocked by nr_pending, and we need to wait for
936 * pending IO requests to complete or be queued for re-try.
NeilBrowne2d59922013-06-12 11:01:22 +1000937 * Thus the number queued (nr_queued) plus this request (extra)
NeilBrown1c830532008-03-04 14:29:35 -0800938 * must match the number of pending IOs (nr_pending) before
939 * we continue.
NeilBrownddaf22a2006-01-06 00:20:19 -0800940 */
941 spin_lock_irq(&conf->resync_lock);
majianpengb364e3d2013-11-14 15:16:18 +1100942 conf->array_frozen = 1;
Lukas Czernereed8c022012-11-30 11:42:40 +0100943 wait_event_lock_irq_cmd(conf->wait_barrier,
NeilBrowne2d59922013-06-12 11:01:22 +1000944 conf->nr_pending == conf->nr_queued+extra,
Lukas Czernereed8c022012-11-30 11:42:40 +0100945 conf->resync_lock,
946 flush_pending_writes(conf));
NeilBrownddaf22a2006-01-06 00:20:19 -0800947 spin_unlock_irq(&conf->resync_lock);
948}
NeilBrowne8096362011-10-11 16:49:05 +1100949static void unfreeze_array(struct r1conf *conf)
NeilBrownddaf22a2006-01-06 00:20:19 -0800950{
951 /* reverse the effect of the freeze */
952 spin_lock_irq(&conf->resync_lock);
majianpengb364e3d2013-11-14 15:16:18 +1100953 conf->array_frozen = 0;
NeilBrownddaf22a2006-01-06 00:20:19 -0800954 wake_up(&conf->wait_barrier);
955 spin_unlock_irq(&conf->resync_lock);
956}
957
NeilBrownf72ffdd2014-09-30 14:23:59 +1000958/* duplicate the data pages for behind I/O
NeilBrown4e780642010-10-19 12:54:01 +1100959 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100960static void alloc_behind_pages(struct bio *bio, struct r1bio *r1_bio)
NeilBrown4b6d2872005-09-09 16:23:47 -0700961{
962 int i;
963 struct bio_vec *bvec;
NeilBrown2ca68f52011-07-28 11:32:10 +1000964 struct bio_vec *bvecs = kzalloc(bio->bi_vcnt * sizeof(struct bio_vec),
NeilBrown4b6d2872005-09-09 16:23:47 -0700965 GFP_NOIO);
NeilBrown2ca68f52011-07-28 11:32:10 +1000966 if (unlikely(!bvecs))
NeilBrownaf6d7b72011-05-11 14:51:19 +1000967 return;
NeilBrown4b6d2872005-09-09 16:23:47 -0700968
Kent Overstreetcb34e052012-09-05 15:22:02 -0700969 bio_for_each_segment_all(bvec, bio, i) {
NeilBrown2ca68f52011-07-28 11:32:10 +1000970 bvecs[i] = *bvec;
971 bvecs[i].bv_page = alloc_page(GFP_NOIO);
972 if (unlikely(!bvecs[i].bv_page))
NeilBrown4b6d2872005-09-09 16:23:47 -0700973 goto do_sync_io;
NeilBrown2ca68f52011-07-28 11:32:10 +1000974 memcpy(kmap(bvecs[i].bv_page) + bvec->bv_offset,
975 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
976 kunmap(bvecs[i].bv_page);
NeilBrown4b6d2872005-09-09 16:23:47 -0700977 kunmap(bvec->bv_page);
978 }
NeilBrown2ca68f52011-07-28 11:32:10 +1000979 r1_bio->behind_bvecs = bvecs;
NeilBrownaf6d7b72011-05-11 14:51:19 +1000980 r1_bio->behind_page_count = bio->bi_vcnt;
981 set_bit(R1BIO_BehindIO, &r1_bio->state);
982 return;
NeilBrown4b6d2872005-09-09 16:23:47 -0700983
984do_sync_io:
NeilBrownaf6d7b72011-05-11 14:51:19 +1000985 for (i = 0; i < bio->bi_vcnt; i++)
NeilBrown2ca68f52011-07-28 11:32:10 +1000986 if (bvecs[i].bv_page)
987 put_page(bvecs[i].bv_page);
988 kfree(bvecs);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700989 pr_debug("%dB behind alloc failed, doing sync I/O\n",
990 bio->bi_iter.bi_size);
NeilBrown4b6d2872005-09-09 16:23:47 -0700991}
992
NeilBrownf54a9d02012-08-02 08:33:20 +1000993struct raid1_plug_cb {
994 struct blk_plug_cb cb;
995 struct bio_list pending;
996 int pending_cnt;
997};
998
999static void raid1_unplug(struct blk_plug_cb *cb, bool from_schedule)
1000{
1001 struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb,
1002 cb);
1003 struct mddev *mddev = plug->cb.data;
1004 struct r1conf *conf = mddev->private;
1005 struct bio *bio;
1006
NeilBrown874807a2012-11-27 12:14:40 +11001007 if (from_schedule || current->bio_list) {
NeilBrownf54a9d02012-08-02 08:33:20 +10001008 spin_lock_irq(&conf->device_lock);
1009 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1010 conf->pending_count += plug->pending_cnt;
1011 spin_unlock_irq(&conf->device_lock);
NeilBrownee0b0242013-02-25 12:38:29 +11001012 wake_up(&conf->wait_barrier);
NeilBrownf54a9d02012-08-02 08:33:20 +10001013 md_wakeup_thread(mddev->thread);
1014 kfree(plug);
1015 return;
1016 }
1017
1018 /* we aren't scheduling, so we can do the write-out directly. */
1019 bio = bio_list_get(&plug->pending);
1020 bitmap_unplug(mddev->bitmap);
1021 wake_up(&conf->wait_barrier);
1022
1023 while (bio) { /* submit pending writes */
1024 struct bio *next = bio->bi_next;
NeilBrown5e2c7a32016-11-04 16:46:03 +11001025 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrownf54a9d02012-08-02 08:33:20 +10001026 bio->bi_next = NULL;
NeilBrown5e2c7a32016-11-04 16:46:03 +11001027 bio->bi_bdev = rdev->bdev;
1028 if (test_bit(Faulty, &rdev->flags)) {
1029 bio->bi_error = -EIO;
1030 bio_endio(bio);
1031 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1032 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li32f9f572013-04-28 18:26:38 +08001033 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001034 bio_endio(bio);
Shaohua Li32f9f572013-04-28 18:26:38 +08001035 else
1036 generic_make_request(bio);
NeilBrownf54a9d02012-08-02 08:33:20 +10001037 bio = next;
1038 }
1039 kfree(plug);
1040}
1041
Shaohua Li849674e2016-01-20 13:52:20 -08001042static void raid1_make_request(struct mddev *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
NeilBrowne8096362011-10-11 16:49:05 +11001044 struct r1conf *conf = mddev->private;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001045 struct raid1_info *mirror;
NeilBrown9f2c9d12011-10-11 16:48:43 +11001046 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 struct bio *read_bio;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001048 int i, disks;
NeilBrown84255d12008-05-23 13:04:32 -07001049 struct bitmap *bitmap;
NeilBrown191ea9b2005-06-21 17:17:23 -07001050 unsigned long flags;
Mike Christie796a5cf2016-06-05 14:32:07 -05001051 const int op = bio_op(bio);
Jens Axboea3623572005-11-01 09:26:16 +01001052 const int rw = bio_data_dir(bio);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001053 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1054 const unsigned long do_flush_fua = (bio->bi_opf &
Mike Christie28a8f0d2016-06-05 14:32:25 -05001055 (REQ_PREFLUSH | REQ_FUA));
NeilBrown3cb03002011-10-11 16:45:26 +11001056 struct md_rdev *blocked_rdev;
NeilBrownf54a9d02012-08-02 08:33:20 +10001057 struct blk_plug_cb *cb;
1058 struct raid1_plug_cb *plug = NULL;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001059 int first_clone;
1060 int sectors_handled;
1061 int max_sectors;
majianpeng79ef3a82013-11-15 14:55:02 +08001062 sector_t start_next_window;
NeilBrown191ea9b2005-06-21 17:17:23 -07001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 /*
1065 * Register the new request and wait if the reconstruction
1066 * thread has put up a bar for new requests.
1067 * Continue immediately if no resync is active currently.
1068 */
NeilBrown62de6082006-05-01 12:15:47 -07001069
NeilBrown3d310eb2005-06-21 17:17:26 -07001070 md_write_start(mddev, bio); /* wait on superblock update early */
1071
NeilBrown6eef4b22009-12-14 12:49:51 +11001072 if (bio_data_dir(bio) == WRITE &&
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001073 ((bio_end_sector(bio) > mddev->suspend_lo &&
1074 bio->bi_iter.bi_sector < mddev->suspend_hi) ||
1075 (mddev_is_clustered(mddev) &&
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -05001076 md_cluster_ops->area_resyncing(mddev, WRITE,
1077 bio->bi_iter.bi_sector, bio_end_sector(bio))))) {
NeilBrown6eef4b22009-12-14 12:49:51 +11001078 /* As the suspend_* range is controlled by
1079 * userspace, we want an interruptible
1080 * wait.
1081 */
1082 DEFINE_WAIT(w);
1083 for (;;) {
1084 flush_signals(current);
1085 prepare_to_wait(&conf->wait_barrier,
1086 &w, TASK_INTERRUPTIBLE);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07001087 if (bio_end_sector(bio) <= mddev->suspend_lo ||
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001088 bio->bi_iter.bi_sector >= mddev->suspend_hi ||
1089 (mddev_is_clustered(mddev) &&
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -05001090 !md_cluster_ops->area_resyncing(mddev, WRITE,
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001091 bio->bi_iter.bi_sector, bio_end_sector(bio))))
NeilBrown6eef4b22009-12-14 12:49:51 +11001092 break;
1093 schedule();
1094 }
1095 finish_wait(&conf->wait_barrier, &w);
1096 }
NeilBrown62de6082006-05-01 12:15:47 -07001097
majianpeng79ef3a82013-11-15 14:55:02 +08001098 start_next_window = wait_barrier(conf, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
NeilBrown84255d12008-05-23 13:04:32 -07001100 bitmap = mddev->bitmap;
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /*
Christoph Hellwig70246282016-07-19 11:28:41 +02001103 * make_request() can abort the operation when read-ahead is being
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 * used and no empty request is available.
1105 *
1106 */
1107 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1108
1109 r1_bio->master_bio = bio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001110 r1_bio->sectors = bio_sectors(bio);
NeilBrown191ea9b2005-06-21 17:17:23 -07001111 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 r1_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001113 r1_bio->sector = bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
NeilBrownd2eb35a2011-07-28 11:31:48 +10001115 /* We might need to issue multiple reads to different
1116 * devices if there are bad blocks around, so we keep
1117 * track of the number of reads in bio->bi_phys_segments.
1118 * If this is 0, there is only one r1_bio and no locking
1119 * will be needed when requests complete. If it is
1120 * non-zero, then it is the number of not-completed requests.
1121 */
1122 bio->bi_phys_segments = 0;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06001123 bio_clear_flag(bio, BIO_SEG_VALID);
NeilBrownd2eb35a2011-07-28 11:31:48 +10001124
Jens Axboea3623572005-11-01 09:26:16 +01001125 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /*
1127 * read balancing logic:
1128 */
NeilBrownd2eb35a2011-07-28 11:31:48 +10001129 int rdisk;
1130
1131read_again:
1132 rdisk = read_balance(conf, r1_bio, &max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 if (rdisk < 0) {
1135 /* couldn't find anywhere to read from */
1136 raid_end_bio_io(r1_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001137 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139 mirror = conf->mirrors + rdisk;
1140
NeilBrowne5551902010-03-31 11:21:44 +11001141 if (test_bit(WriteMostly, &mirror->rdev->flags) &&
1142 bitmap) {
1143 /* Reading from a write-mostly device must
1144 * take care not to over-take any writes
1145 * that are 'behind'
1146 */
1147 wait_event(bitmap->behind_wait,
1148 atomic_read(&bitmap->behind_writes) == 0);
1149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 r1_bio->read_disk = rdisk;
NeilBrownf0cc9a02014-09-22 10:06:23 +10001151 r1_bio->start_next_window = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
NeilBrowna167f662010-10-26 18:31:13 +11001153 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001154 bio_trim(read_bio, r1_bio->sector - bio->bi_iter.bi_sector,
Kent Overstreet6678d832013-08-07 11:14:32 -07001155 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 r1_bio->bios[rdisk] = read_bio;
1158
Kent Overstreet4f024f32013-10-11 15:44:27 -07001159 read_bio->bi_iter.bi_sector = r1_bio->sector +
1160 mirror->rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 read_bio->bi_bdev = mirror->rdev->bdev;
1162 read_bio->bi_end_io = raid1_end_read_request;
Mike Christie796a5cf2016-06-05 14:32:07 -05001163 bio_set_op_attrs(read_bio, op, do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 read_bio->bi_private = r1_bio;
1165
NeilBrown109e3762016-11-18 13:22:04 +11001166 if (mddev->gendisk)
1167 trace_block_bio_remap(bdev_get_queue(read_bio->bi_bdev),
1168 read_bio, disk_devt(mddev->gendisk),
1169 r1_bio->sector);
1170
NeilBrownd2eb35a2011-07-28 11:31:48 +10001171 if (max_sectors < r1_bio->sectors) {
1172 /* could not read all from this device, so we will
1173 * need another r1_bio.
1174 */
NeilBrownd2eb35a2011-07-28 11:31:48 +10001175
1176 sectors_handled = (r1_bio->sector + max_sectors
Kent Overstreet4f024f32013-10-11 15:44:27 -07001177 - bio->bi_iter.bi_sector);
NeilBrownd2eb35a2011-07-28 11:31:48 +10001178 r1_bio->sectors = max_sectors;
1179 spin_lock_irq(&conf->device_lock);
1180 if (bio->bi_phys_segments == 0)
1181 bio->bi_phys_segments = 2;
1182 else
1183 bio->bi_phys_segments++;
1184 spin_unlock_irq(&conf->device_lock);
1185 /* Cannot call generic_make_request directly
1186 * as that will be queued in __make_request
1187 * and subsequent mempool_alloc might block waiting
1188 * for it. So hand bio over to raid1d.
1189 */
1190 reschedule_retry(r1_bio);
1191
1192 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1193
1194 r1_bio->master_bio = bio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001195 r1_bio->sectors = bio_sectors(bio) - sectors_handled;
NeilBrownd2eb35a2011-07-28 11:31:48 +10001196 r1_bio->state = 0;
1197 r1_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001198 r1_bio->sector = bio->bi_iter.bi_sector +
1199 sectors_handled;
NeilBrownd2eb35a2011-07-28 11:31:48 +10001200 goto read_again;
1201 } else
1202 generic_make_request(read_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001203 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205
1206 /*
1207 * WRITE:
1208 */
NeilBrown34db0cd2011-10-11 16:50:01 +11001209 if (conf->pending_count >= max_queued_requests) {
1210 md_wakeup_thread(mddev->thread);
1211 wait_event(conf->wait_barrier,
1212 conf->pending_count < max_queued_requests);
1213 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001214 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 * inc refcount on their rdev. Record them by setting
1216 * bios[x] to bio
NeilBrown1f68f0c2011-07-28 11:31:48 +10001217 * If there are known/acknowledged bad blocks on any device on
1218 * which we have seen a write error, we want to avoid writing those
1219 * blocks.
1220 * This potentially requires several writes to write around
1221 * the bad blocks. Each set of writes gets it's own r1bio
1222 * with a set of bios attached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001224
NeilBrown8f19ccb2011-12-23 10:17:56 +11001225 disks = conf->raid_disks * 2;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001226 retry_write:
majianpeng79ef3a82013-11-15 14:55:02 +08001227 r1_bio->start_next_window = start_next_window;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001228 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 rcu_read_lock();
NeilBrown1f68f0c2011-07-28 11:31:48 +10001230 max_sectors = r1_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 for (i = 0; i < disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001232 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001233 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1234 atomic_inc(&rdev->nr_pending);
1235 blocked_rdev = rdev;
1236 break;
1237 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001238 r1_bio->bios[i] = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001239 if (!rdev || test_bit(Faulty, &rdev->flags)) {
NeilBrown8f19ccb2011-12-23 10:17:56 +11001240 if (i < conf->raid_disks)
1241 set_bit(R1BIO_Degraded, &r1_bio->state);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001242 continue;
1243 }
1244
1245 atomic_inc(&rdev->nr_pending);
1246 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1247 sector_t first_bad;
1248 int bad_sectors;
1249 int is_bad;
1250
1251 is_bad = is_badblock(rdev, r1_bio->sector,
1252 max_sectors,
1253 &first_bad, &bad_sectors);
1254 if (is_bad < 0) {
1255 /* mustn't write here until the bad block is
1256 * acknowledged*/
1257 set_bit(BlockedBadBlocks, &rdev->flags);
1258 blocked_rdev = rdev;
1259 break;
NeilBrown964147d2010-05-18 15:27:13 +10001260 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001261 if (is_bad && first_bad <= r1_bio->sector) {
1262 /* Cannot write here at all */
1263 bad_sectors -= (r1_bio->sector - first_bad);
1264 if (bad_sectors < max_sectors)
1265 /* mustn't write more than bad_sectors
1266 * to other devices yet
1267 */
1268 max_sectors = bad_sectors;
1269 rdev_dec_pending(rdev, mddev);
1270 /* We don't set R1BIO_Degraded as that
1271 * only applies if the disk is
1272 * missing, so it might be re-added,
1273 * and we want to know to recover this
1274 * chunk.
1275 * In this case the device is here,
1276 * and the fact that this chunk is not
1277 * in-sync is recorded in the bad
1278 * block log
1279 */
1280 continue;
1281 }
1282 if (is_bad) {
1283 int good_sectors = first_bad - r1_bio->sector;
1284 if (good_sectors < max_sectors)
1285 max_sectors = good_sectors;
1286 }
1287 }
1288 r1_bio->bios[i] = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290 rcu_read_unlock();
1291
Dan Williams6bfe0b42008-04-30 00:52:32 -07001292 if (unlikely(blocked_rdev)) {
1293 /* Wait for this device to become unblocked */
1294 int j;
majianpeng79ef3a82013-11-15 14:55:02 +08001295 sector_t old = start_next_window;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001296
1297 for (j = 0; j < i; j++)
1298 if (r1_bio->bios[j])
1299 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001300 r1_bio->state = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001301 allow_barrier(conf, start_next_window, bio->bi_iter.bi_sector);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001302 md_wait_for_blocked_rdev(blocked_rdev, mddev);
majianpeng79ef3a82013-11-15 14:55:02 +08001303 start_next_window = wait_barrier(conf, bio);
1304 /*
1305 * We must make sure the multi r1bios of bio have
1306 * the same value of bi_phys_segments
1307 */
1308 if (bio->bi_phys_segments && old &&
1309 old != start_next_window)
1310 /* Wait for the former r1bio(s) to complete */
1311 wait_event(conf->wait_barrier,
1312 bio->bi_phys_segments == 1);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001313 goto retry_write;
1314 }
1315
NeilBrown1f68f0c2011-07-28 11:31:48 +10001316 if (max_sectors < r1_bio->sectors) {
1317 /* We are splitting this write into multiple parts, so
1318 * we need to prepare for allocating another r1_bio.
1319 */
1320 r1_bio->sectors = max_sectors;
1321 spin_lock_irq(&conf->device_lock);
1322 if (bio->bi_phys_segments == 0)
1323 bio->bi_phys_segments = 2;
1324 else
1325 bio->bi_phys_segments++;
1326 spin_unlock_irq(&conf->device_lock);
NeilBrown191ea9b2005-06-21 17:17:23 -07001327 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07001328 sectors_handled = r1_bio->sector + max_sectors - bio->bi_iter.bi_sector;
NeilBrown4b6d2872005-09-09 16:23:47 -07001329
NeilBrown4e780642010-10-19 12:54:01 +11001330 atomic_set(&r1_bio->remaining, 1);
NeilBrown4b6d2872005-09-09 16:23:47 -07001331 atomic_set(&r1_bio->behind_remaining, 0);
NeilBrown191ea9b2005-06-21 17:17:23 -07001332
NeilBrown1f68f0c2011-07-28 11:31:48 +10001333 first_clone = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 for (i = 0; i < disks; i++) {
1335 struct bio *mbio;
1336 if (!r1_bio->bios[i])
1337 continue;
1338
NeilBrowna167f662010-10-26 18:31:13 +11001339 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001340 bio_trim(mbio, r1_bio->sector - bio->bi_iter.bi_sector, max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
NeilBrown1f68f0c2011-07-28 11:31:48 +10001342 if (first_clone) {
1343 /* do behind I/O ?
1344 * Not if there are too many, or cannot
1345 * allocate memory, or a reader on WriteMostly
1346 * is waiting for behind writes to flush */
1347 if (bitmap &&
1348 (atomic_read(&bitmap->behind_writes)
1349 < mddev->bitmap_info.max_write_behind) &&
1350 !waitqueue_active(&bitmap->behind_wait))
1351 alloc_behind_pages(mbio, r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
NeilBrown1f68f0c2011-07-28 11:31:48 +10001353 bitmap_startwrite(bitmap, r1_bio->sector,
1354 r1_bio->sectors,
1355 test_bit(R1BIO_BehindIO,
1356 &r1_bio->state));
1357 first_clone = 0;
1358 }
NeilBrown2ca68f52011-07-28 11:32:10 +10001359 if (r1_bio->behind_bvecs) {
NeilBrown4b6d2872005-09-09 16:23:47 -07001360 struct bio_vec *bvec;
1361 int j;
1362
Kent Overstreetcb34e052012-09-05 15:22:02 -07001363 /*
1364 * We trimmed the bio, so _all is legit
NeilBrown4b6d2872005-09-09 16:23:47 -07001365 */
Kent Overstreetd74c6d52013-02-06 12:23:11 -08001366 bio_for_each_segment_all(bvec, mbio, j)
NeilBrown2ca68f52011-07-28 11:32:10 +10001367 bvec->bv_page = r1_bio->behind_bvecs[j].bv_page;
NeilBrown4b6d2872005-09-09 16:23:47 -07001368 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
1369 atomic_inc(&r1_bio->behind_remaining);
1370 }
1371
NeilBrown1f68f0c2011-07-28 11:31:48 +10001372 r1_bio->bios[i] = mbio;
1373
Kent Overstreet4f024f32013-10-11 15:44:27 -07001374 mbio->bi_iter.bi_sector = (r1_bio->sector +
NeilBrown1f68f0c2011-07-28 11:31:48 +10001375 conf->mirrors[i].rdev->data_offset);
NeilBrown109e3762016-11-18 13:22:04 +11001376 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001377 mbio->bi_end_io = raid1_end_write_request;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001378 bio_set_op_attrs(mbio, op, do_flush_fua | do_sync);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001379 mbio->bi_private = r1_bio;
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 atomic_inc(&r1_bio->remaining);
NeilBrownf54a9d02012-08-02 08:33:20 +10001382
NeilBrown109e3762016-11-18 13:22:04 +11001383 if (mddev->gendisk)
1384 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1385 mbio, disk_devt(mddev->gendisk),
1386 r1_bio->sector);
1387 /* flush_pending_writes() needs access to the rdev so...*/
1388 mbio->bi_bdev = (void*)conf->mirrors[i].rdev;
1389
NeilBrownf54a9d02012-08-02 08:33:20 +10001390 cb = blk_check_plugged(raid1_unplug, mddev, sizeof(*plug));
1391 if (cb)
1392 plug = container_of(cb, struct raid1_plug_cb, cb);
1393 else
1394 plug = NULL;
NeilBrown4e780642010-10-19 12:54:01 +11001395 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownf54a9d02012-08-02 08:33:20 +10001396 if (plug) {
1397 bio_list_add(&plug->pending, mbio);
1398 plug->pending_cnt++;
1399 } else {
1400 bio_list_add(&conf->pending_bio_list, mbio);
1401 conf->pending_count++;
1402 }
NeilBrown4e780642010-10-19 12:54:01 +11001403 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrownf54a9d02012-08-02 08:33:20 +10001404 if (!plug)
NeilBrownb357f042012-07-03 17:45:31 +10001405 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
NeilBrown079fa162011-09-10 17:21:23 +10001407 /* Mustn't call r1_bio_write_done before this next test,
1408 * as it could result in the bio being freed.
1409 */
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001410 if (sectors_handled < bio_sectors(bio)) {
NeilBrown079fa162011-09-10 17:21:23 +10001411 r1_bio_write_done(r1_bio);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001412 /* We need another r1_bio. It has already been counted
1413 * in bio->bi_phys_segments
1414 */
1415 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1416 r1_bio->master_bio = bio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001417 r1_bio->sectors = bio_sectors(bio) - sectors_handled;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001418 r1_bio->state = 0;
1419 r1_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001420 r1_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001421 goto retry_write;
1422 }
1423
NeilBrown079fa162011-09-10 17:21:23 +10001424 r1_bio_write_done(r1_bio);
1425
1426 /* In case raid1d snuck in to freeze_array */
1427 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429
Shaohua Li849674e2016-01-20 13:52:20 -08001430static void raid1_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
NeilBrowne8096362011-10-11 16:49:05 +11001432 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 int i;
1434
1435 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown11ce99e2006-10-03 01:15:52 -07001436 conf->raid_disks - mddev->degraded);
NeilBrownddac7c72006-08-31 21:27:36 -07001437 rcu_read_lock();
1438 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001439 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 seq_printf(seq, "%s",
NeilBrownddac7c72006-08-31 21:27:36 -07001441 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1442 }
1443 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 seq_printf(seq, "]");
1445}
1446
Shaohua Li849674e2016-01-20 13:52:20 -08001447static void raid1_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 char b[BDEVNAME_SIZE];
NeilBrowne8096362011-10-11 16:49:05 +11001450 struct r1conf *conf = mddev->private;
NeilBrown423f04d2015-07-27 11:48:52 +10001451 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 /*
1454 * If it is not operational, then we have already marked it as dead
1455 * else if it is the last working disks, ignore the error, let the
1456 * next level up know.
1457 * else mark the drive as failed
1458 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001459 if (test_bit(In_sync, &rdev->flags)
NeilBrown4044ba52009-01-09 08:31:11 +11001460 && (conf->raid_disks - mddev->degraded) == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 /*
1462 * Don't fail the drive, act as though we were just a
NeilBrown4044ba52009-01-09 08:31:11 +11001463 * normal single drive.
1464 * However don't try a recovery from this drive as
1465 * it is very likely to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 */
NeilBrown53890422011-07-27 11:00:36 +10001467 conf->recovery_disabled = mddev->recovery_disabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return;
NeilBrown4044ba52009-01-09 08:31:11 +11001469 }
NeilBrownde393cd2011-07-28 11:31:48 +10001470 set_bit(Blocked, &rdev->flags);
NeilBrown423f04d2015-07-27 11:48:52 +10001471 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001472 if (test_and_clear_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 mddev->degraded++;
NeilBrowndd00a992007-05-10 03:15:50 -07001474 set_bit(Faulty, &rdev->flags);
NeilBrowndd00a992007-05-10 03:15:50 -07001475 } else
1476 set_bit(Faulty, &rdev->flags);
NeilBrown423f04d2015-07-27 11:48:52 +10001477 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown2446dba2014-07-31 10:16:29 +10001478 /*
1479 * if recovery is running, make sure it aborts.
1480 */
1481 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04001482 set_mask_bits(&mddev->flags, 0,
1483 BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING));
NeilBrown1d41c212016-11-02 14:16:50 +11001484 pr_crit("md/raid1:%s: Disk failure on %s, disabling device.\n"
1485 "md/raid1:%s: Operation continuing on %d devices.\n",
1486 mdname(mddev), bdevname(rdev->bdev, b),
1487 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
NeilBrowne8096362011-10-11 16:49:05 +11001490static void print_conf(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
1492 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
NeilBrown1d41c212016-11-02 14:16:50 +11001494 pr_debug("RAID1 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (!conf) {
NeilBrown1d41c212016-11-02 14:16:50 +11001496 pr_debug("(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return;
1498 }
NeilBrown1d41c212016-11-02 14:16:50 +11001499 pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1500 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
NeilBrownddac7c72006-08-31 21:27:36 -07001502 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 for (i = 0; i < conf->raid_disks; i++) {
1504 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11001505 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownddac7c72006-08-31 21:27:36 -07001506 if (rdev)
NeilBrown1d41c212016-11-02 14:16:50 +11001507 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1508 i, !test_bit(In_sync, &rdev->flags),
1509 !test_bit(Faulty, &rdev->flags),
1510 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
NeilBrownddac7c72006-08-31 21:27:36 -07001512 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
NeilBrowne8096362011-10-11 16:49:05 +11001515static void close_sync(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
majianpeng79ef3a82013-11-15 14:55:02 +08001517 wait_barrier(conf, NULL);
1518 allow_barrier(conf, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 mempool_destroy(conf->r1buf_pool);
1521 conf->r1buf_pool = NULL;
majianpeng79ef3a82013-11-15 14:55:02 +08001522
NeilBrown669cc7b2014-09-04 16:30:38 +10001523 spin_lock_irq(&conf->resync_lock);
Jes Sorensene8ff8bf2015-09-16 10:20:05 -04001524 conf->next_resync = MaxSector - 2 * NEXT_NORMALIO_DISTANCE;
majianpeng79ef3a82013-11-15 14:55:02 +08001525 conf->start_next_window = MaxSector;
NeilBrown669cc7b2014-09-04 16:30:38 +10001526 conf->current_window_requests +=
1527 conf->next_window_requests;
1528 conf->next_window_requests = 0;
1529 spin_unlock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
NeilBrownfd01b882011-10-11 16:47:53 +11001532static int raid1_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
1534 int i;
NeilBrowne8096362011-10-11 16:49:05 +11001535 struct r1conf *conf = mddev->private;
NeilBrown6b965622010-08-18 11:56:59 +10001536 int count = 0;
1537 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 /*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001540 * Find all failed disks within the RAID1 configuration
NeilBrownddac7c72006-08-31 21:27:36 -07001541 * and mark them readable.
1542 * Called under mddev lock, so rcu protection not needed.
NeilBrown423f04d2015-07-27 11:48:52 +10001543 * device_lock used to avoid races with raid1_end_read_request
1544 * which expects 'In_sync' flags and ->degraded to be consistent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 */
NeilBrown423f04d2015-07-27 11:48:52 +10001546 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001548 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrown8c7a2c22011-12-23 10:17:57 +11001549 struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev;
1550 if (repl
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001551 && !test_bit(Candidate, &repl->flags)
NeilBrown8c7a2c22011-12-23 10:17:57 +11001552 && repl->recovery_offset == MaxSector
1553 && !test_bit(Faulty, &repl->flags)
1554 && !test_and_set_bit(In_sync, &repl->flags)) {
1555 /* replacement has just become active */
1556 if (!rdev ||
1557 !test_and_clear_bit(In_sync, &rdev->flags))
1558 count++;
1559 if (rdev) {
1560 /* Replaced device not technically
1561 * faulty, but we need to be sure
1562 * it gets removed and never re-added
1563 */
1564 set_bit(Faulty, &rdev->flags);
1565 sysfs_notify_dirent_safe(
1566 rdev->sysfs_state);
1567 }
1568 }
NeilBrownddac7c72006-08-31 21:27:36 -07001569 if (rdev
Lukasz Dorau61e49472013-10-24 12:55:17 +11001570 && rdev->recovery_offset == MaxSector
NeilBrownddac7c72006-08-31 21:27:36 -07001571 && !test_bit(Faulty, &rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001572 && !test_and_set_bit(In_sync, &rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001573 count++;
Jonathan Brassow654e8b52011-07-27 11:00:36 +10001574 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
1576 }
NeilBrown6b965622010-08-18 11:56:59 +10001577 mddev->degraded -= count;
1578 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001581 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
NeilBrownfd01b882011-10-11 16:47:53 +11001584static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
NeilBrowne8096362011-10-11 16:49:05 +11001586 struct r1conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001587 int err = -EEXIST;
NeilBrown41158c72005-06-21 17:17:25 -07001588 int mirror = 0;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001589 struct raid1_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10001590 int first = 0;
NeilBrown30194632011-12-23 10:17:56 +11001591 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
NeilBrown53890422011-07-27 11:00:36 +10001593 if (mddev->recovery_disabled == conf->recovery_disabled)
1594 return -EBUSY;
1595
Dan Williams1501efa2016-01-13 16:00:07 -08001596 if (md_integrity_add_rdev(rdev, mddev))
1597 return -ENXIO;
1598
Neil Brown6c2fce22008-06-28 08:31:31 +10001599 if (rdev->raid_disk >= 0)
1600 first = last = rdev->raid_disk;
1601
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05001602 /*
1603 * find the disk ... but prefer rdev->saved_raid_disk
1604 * if possible.
1605 */
1606 if (rdev->saved_raid_disk >= 0 &&
1607 rdev->saved_raid_disk >= first &&
1608 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1609 first = last = rdev->saved_raid_disk;
1610
NeilBrown7ef449d2011-12-23 10:17:57 +11001611 for (mirror = first; mirror <= last; mirror++) {
1612 p = conf->mirrors+mirror;
1613 if (!p->rdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Jonathan Brassow9092c022013-05-02 14:19:24 -05001615 if (mddev->gendisk)
1616 disk_stack_limits(mddev->gendisk, rdev->bdev,
1617 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 p->head_position = 0;
1620 rdev->raid_disk = mirror;
Neil Brown199050e2008-06-28 08:31:33 +10001621 err = 0;
NeilBrown6aea114a2005-11-28 13:44:13 -08001622 /* As all devices are equivalent, we don't need a full recovery
1623 * if this was recently any drive of the array
1624 */
1625 if (rdev->saved_raid_disk < 0)
NeilBrown41158c72005-06-21 17:17:25 -07001626 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001627 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 break;
1629 }
NeilBrown7ef449d2011-12-23 10:17:57 +11001630 if (test_bit(WantReplacement, &p->rdev->flags) &&
1631 p[conf->raid_disks].rdev == NULL) {
1632 /* Add this device as a replacement */
1633 clear_bit(In_sync, &rdev->flags);
1634 set_bit(Replacement, &rdev->flags);
1635 rdev->raid_disk = mirror;
1636 err = 0;
1637 conf->fullsync = 1;
1638 rcu_assign_pointer(p[conf->raid_disks].rdev, rdev);
1639 break;
1640 }
1641 }
Jonathan Brassow9092c022013-05-02 14:19:24 -05001642 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
Shaohua Li2ff8cc22012-10-11 13:28:54 +11001643 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001645 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
NeilBrownb8321b62011-12-23 10:17:51 +11001648static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
NeilBrowne8096362011-10-11 16:49:05 +11001650 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001652 int number = rdev->raid_disk;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001653 struct raid1_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
NeilBrownb014f142011-12-23 10:17:56 +11001655 if (rdev != p->rdev)
1656 p = conf->mirrors + conf->raid_disks + number;
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 print_conf(conf);
NeilBrownb8321b62011-12-23 10:17:51 +11001659 if (rdev == p->rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001660 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 atomic_read(&rdev->nr_pending)) {
1662 err = -EBUSY;
1663 goto abort;
1664 }
NeilBrown046abee2010-10-26 15:46:20 +11001665 /* Only remove non-faulty devices if recovery
NeilBrowndfc70642008-05-23 13:04:39 -07001666 * is not possible.
1667 */
1668 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown53890422011-07-27 11:00:36 +10001669 mddev->recovery_disabled != conf->recovery_disabled &&
NeilBrowndfc70642008-05-23 13:04:39 -07001670 mddev->degraded < conf->raid_disks) {
1671 err = -EBUSY;
1672 goto abort;
1673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 p->rdev = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10001675 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1676 synchronize_rcu();
1677 if (atomic_read(&rdev->nr_pending)) {
1678 /* lost the race, try later */
1679 err = -EBUSY;
1680 p->rdev = rdev;
1681 goto abort;
1682 }
1683 }
1684 if (conf->mirrors[conf->raid_disks + number].rdev) {
NeilBrown8c7a2c22011-12-23 10:17:57 +11001685 /* We just removed a device that is being replaced.
1686 * Move down the replacement. We drain all IO before
1687 * doing this to avoid confusion.
1688 */
1689 struct md_rdev *repl =
1690 conf->mirrors[conf->raid_disks + number].rdev;
NeilBrowne2d59922013-06-12 11:01:22 +10001691 freeze_array(conf, 0);
NeilBrown8c7a2c22011-12-23 10:17:57 +11001692 clear_bit(Replacement, &repl->flags);
1693 p->rdev = repl;
1694 conf->mirrors[conf->raid_disks + number].rdev = NULL;
NeilBrowne2d59922013-06-12 11:01:22 +10001695 unfreeze_array(conf);
NeilBrownb014f142011-12-23 10:17:56 +11001696 clear_bit(WantReplacement, &rdev->flags);
NeilBrown8c7a2c22011-12-23 10:17:57 +11001697 } else
1698 clear_bit(WantReplacement, &rdev->flags);
Martin K. Petersena91a2782011-03-17 11:11:05 +01001699 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701abort:
1702
1703 print_conf(conf);
1704 return err;
1705}
1706
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001707static void end_sync_read(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001709 struct r1bio *r1_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
NeilBrown0fc280f2011-10-07 14:22:55 +11001711 update_head_pos(r1_bio->read_disk, r1_bio);
Namhyung Kimba3ae3b2011-10-07 14:22:53 +11001712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 /*
1714 * we have read a block, now it needs to be re-written,
1715 * or re-read if the read failed.
1716 * We don't do much here, just schedule handling by raid1d
1717 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001718 if (!bio->bi_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrownd11c1712006-01-06 00:20:26 -08001720
1721 if (atomic_dec_and_test(&r1_bio->remaining))
1722 reschedule_retry(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001725static void end_sync_write(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001727 int uptodate = !bio->bi_error;
NeilBrown9f2c9d12011-10-11 16:48:43 +11001728 struct r1bio *r1_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001729 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11001730 struct r1conf *conf = mddev->private;
NeilBrown4367af52011-07-28 11:31:49 +10001731 sector_t first_bad;
1732 int bad_sectors;
NeilBrown854abd72016-06-02 16:19:52 +10001733 struct md_rdev *rdev = conf->mirrors[find_bio_disk(r1_bio, bio)].rdev;
Namhyung Kimba3ae3b2011-10-07 14:22:53 +11001734
NeilBrown6b1117d2006-03-31 02:31:57 -08001735 if (!uptodate) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001736 sector_t sync_blocks = 0;
NeilBrown6b1117d2006-03-31 02:31:57 -08001737 sector_t s = r1_bio->sector;
1738 long sectors_to_go = r1_bio->sectors;
1739 /* make sure these bits doesn't get cleared. */
1740 do {
NeilBrown5e3db642006-07-10 04:44:18 -07001741 bitmap_end_sync(mddev->bitmap, s,
NeilBrown6b1117d2006-03-31 02:31:57 -08001742 &sync_blocks, 1);
1743 s += sync_blocks;
1744 sectors_to_go -= sync_blocks;
1745 } while (sectors_to_go > 0);
NeilBrown854abd72016-06-02 16:19:52 +10001746 set_bit(WriteErrorSeen, &rdev->flags);
1747 if (!test_and_set_bit(WantReplacement, &rdev->flags))
NeilBrown19d67162011-12-23 10:17:57 +11001748 set_bit(MD_RECOVERY_NEEDED, &
1749 mddev->recovery);
NeilBrownd8f05d22011-07-28 11:33:00 +10001750 set_bit(R1BIO_WriteError, &r1_bio->state);
NeilBrown854abd72016-06-02 16:19:52 +10001751 } else if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
NeilBrown3a9f28a2011-07-28 11:33:42 +10001752 &first_bad, &bad_sectors) &&
1753 !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
1754 r1_bio->sector,
1755 r1_bio->sectors,
1756 &first_bad, &bad_sectors)
1757 )
NeilBrown4367af52011-07-28 11:31:49 +10001758 set_bit(R1BIO_MadeGood, &r1_bio->state);
NeilBrowne3b97032005-08-04 12:53:34 -07001759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown4367af52011-07-28 11:31:49 +10001761 int s = r1_bio->sectors;
NeilBrownd8f05d22011-07-28 11:33:00 +10001762 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
1763 test_bit(R1BIO_WriteError, &r1_bio->state))
NeilBrown4367af52011-07-28 11:31:49 +10001764 reschedule_retry(r1_bio);
1765 else {
1766 put_buf(r1_bio);
1767 md_done_sync(mddev, s, uptodate);
1768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
NeilBrown3cb03002011-10-11 16:45:26 +11001772static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrownd8f05d22011-07-28 11:33:00 +10001773 int sectors, struct page *page, int rw)
1774{
Mike Christie796a5cf2016-06-05 14:32:07 -05001775 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
NeilBrownd8f05d22011-07-28 11:33:00 +10001776 /* success */
1777 return 1;
NeilBrown19d67162011-12-23 10:17:57 +11001778 if (rw == WRITE) {
NeilBrownd8f05d22011-07-28 11:33:00 +10001779 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrown19d67162011-12-23 10:17:57 +11001780 if (!test_and_set_bit(WantReplacement,
1781 &rdev->flags))
1782 set_bit(MD_RECOVERY_NEEDED, &
1783 rdev->mddev->recovery);
1784 }
NeilBrownd8f05d22011-07-28 11:33:00 +10001785 /* need to record an error - either for the block or the device */
1786 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1787 md_error(rdev->mddev, rdev);
1788 return 0;
1789}
1790
NeilBrown9f2c9d12011-10-11 16:48:43 +11001791static int fix_sync_read_error(struct r1bio *r1_bio)
NeilBrowna68e5872011-05-11 14:40:44 +10001792{
1793 /* Try some synchronous reads of other devices to get
1794 * good data, much like with normal read errors. Only
1795 * read into the pages we already have so we don't
1796 * need to re-issue the read request.
1797 * We don't need to freeze the array, because being in an
1798 * active sync request, there is no normal IO, and
1799 * no overlapping syncs.
NeilBrown06f60382011-07-28 11:31:48 +10001800 * We don't need to check is_badblock() again as we
1801 * made sure that anything with a bad block in range
1802 * will have bi_end_io clear.
NeilBrowna68e5872011-05-11 14:40:44 +10001803 */
NeilBrownfd01b882011-10-11 16:47:53 +11001804 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11001805 struct r1conf *conf = mddev->private;
NeilBrowna68e5872011-05-11 14:40:44 +10001806 struct bio *bio = r1_bio->bios[r1_bio->read_disk];
1807 sector_t sect = r1_bio->sector;
1808 int sectors = r1_bio->sectors;
1809 int idx = 0;
1810
1811 while(sectors) {
1812 int s = sectors;
1813 int d = r1_bio->read_disk;
1814 int success = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11001815 struct md_rdev *rdev;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001816 int start;
NeilBrowna68e5872011-05-11 14:40:44 +10001817
1818 if (s > (PAGE_SIZE>>9))
1819 s = PAGE_SIZE >> 9;
1820 do {
1821 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1822 /* No rcu protection needed here devices
1823 * can only be removed when no resync is
1824 * active, and resync is currently active
1825 */
1826 rdev = conf->mirrors[d].rdev;
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001827 if (sync_page_io(rdev, sect, s<<9,
NeilBrowna68e5872011-05-11 14:40:44 +10001828 bio->bi_io_vec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05001829 REQ_OP_READ, 0, false)) {
NeilBrowna68e5872011-05-11 14:40:44 +10001830 success = 1;
1831 break;
1832 }
1833 }
1834 d++;
NeilBrown8f19ccb2011-12-23 10:17:56 +11001835 if (d == conf->raid_disks * 2)
NeilBrowna68e5872011-05-11 14:40:44 +10001836 d = 0;
1837 } while (!success && d != r1_bio->read_disk);
1838
NeilBrown78d7f5f2011-05-11 14:48:56 +10001839 if (!success) {
NeilBrowna68e5872011-05-11 14:40:44 +10001840 char b[BDEVNAME_SIZE];
NeilBrown3a9f28a2011-07-28 11:33:42 +10001841 int abort = 0;
1842 /* Cannot read from anywhere, this block is lost.
1843 * Record a bad block on each device. If that doesn't
1844 * work just disable and interrupt the recovery.
1845 * Don't fail devices as that won't really help.
1846 */
NeilBrown1d41c212016-11-02 14:16:50 +11001847 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
1848 mdname(mddev),
1849 bdevname(bio->bi_bdev, b),
1850 (unsigned long long)r1_bio->sector);
NeilBrown8f19ccb2011-12-23 10:17:56 +11001851 for (d = 0; d < conf->raid_disks * 2; d++) {
NeilBrown3a9f28a2011-07-28 11:33:42 +10001852 rdev = conf->mirrors[d].rdev;
1853 if (!rdev || test_bit(Faulty, &rdev->flags))
1854 continue;
1855 if (!rdev_set_badblocks(rdev, sect, s, 0))
1856 abort = 1;
1857 }
1858 if (abort) {
NeilBrownd890fa22011-10-26 11:54:39 +11001859 conf->recovery_disabled =
1860 mddev->recovery_disabled;
NeilBrown3a9f28a2011-07-28 11:33:42 +10001861 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1862 md_done_sync(mddev, r1_bio->sectors, 0);
1863 put_buf(r1_bio);
1864 return 0;
1865 }
1866 /* Try next page */
1867 sectors -= s;
1868 sect += s;
1869 idx++;
1870 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10001871 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001872
1873 start = d;
1874 /* write it back and re-read */
1875 while (d != r1_bio->read_disk) {
1876 if (d == 0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11001877 d = conf->raid_disks * 2;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001878 d--;
1879 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1880 continue;
1881 rdev = conf->mirrors[d].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10001882 if (r1_sync_page_io(rdev, sect, s,
1883 bio->bi_io_vec[idx].bv_page,
1884 WRITE) == 0) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10001885 r1_bio->bios[d]->bi_end_io = NULL;
1886 rdev_dec_pending(rdev, mddev);
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001887 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001888 }
1889 d = start;
1890 while (d != r1_bio->read_disk) {
1891 if (d == 0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11001892 d = conf->raid_disks * 2;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001893 d--;
1894 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1895 continue;
1896 rdev = conf->mirrors[d].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10001897 if (r1_sync_page_io(rdev, sect, s,
1898 bio->bi_io_vec[idx].bv_page,
1899 READ) != 0)
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001900 atomic_add(s, &rdev->corrected_errors);
NeilBrown78d7f5f2011-05-11 14:48:56 +10001901 }
NeilBrowna68e5872011-05-11 14:40:44 +10001902 sectors -= s;
1903 sect += s;
1904 idx ++;
1905 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001906 set_bit(R1BIO_Uptodate, &r1_bio->state);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001907 bio->bi_error = 0;
NeilBrowna68e5872011-05-11 14:40:44 +10001908 return 1;
1909}
1910
NeilBrownc95e6382014-09-09 13:54:11 +10001911static void process_checks(struct r1bio *r1_bio)
NeilBrowna68e5872011-05-11 14:40:44 +10001912{
1913 /* We have read all readable devices. If we haven't
1914 * got the block, then there is no hope left.
1915 * If we have, then we want to do a comparison
1916 * and skip the write if everything is the same.
1917 * If any blocks failed to read, then we need to
1918 * attempt an over-write
1919 */
NeilBrownfd01b882011-10-11 16:47:53 +11001920 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11001921 struct r1conf *conf = mddev->private;
NeilBrowna68e5872011-05-11 14:40:44 +10001922 int primary;
1923 int i;
majianpengf4380a92012-04-12 16:04:47 +10001924 int vcnt;
NeilBrowna68e5872011-05-11 14:40:44 +10001925
NeilBrown30bc9b52013-07-17 15:19:29 +10001926 /* Fix variable parts of all bios */
1927 vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9);
1928 for (i = 0; i < conf->raid_disks * 2; i++) {
1929 int j;
1930 int size;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001931 int error;
NeilBrown30bc9b52013-07-17 15:19:29 +10001932 struct bio *b = r1_bio->bios[i];
1933 if (b->bi_end_io != end_sync_read)
1934 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001935 /* fixup the bio for reuse, but preserve errno */
1936 error = b->bi_error;
NeilBrown30bc9b52013-07-17 15:19:29 +10001937 bio_reset(b);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001938 b->bi_error = error;
NeilBrown30bc9b52013-07-17 15:19:29 +10001939 b->bi_vcnt = vcnt;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001940 b->bi_iter.bi_size = r1_bio->sectors << 9;
1941 b->bi_iter.bi_sector = r1_bio->sector +
NeilBrown30bc9b52013-07-17 15:19:29 +10001942 conf->mirrors[i].rdev->data_offset;
1943 b->bi_bdev = conf->mirrors[i].rdev->bdev;
1944 b->bi_end_io = end_sync_read;
1945 b->bi_private = r1_bio;
1946
Kent Overstreet4f024f32013-10-11 15:44:27 -07001947 size = b->bi_iter.bi_size;
NeilBrown30bc9b52013-07-17 15:19:29 +10001948 for (j = 0; j < vcnt ; j++) {
1949 struct bio_vec *bi;
1950 bi = &b->bi_io_vec[j];
1951 bi->bv_offset = 0;
1952 if (size > PAGE_SIZE)
1953 bi->bv_len = PAGE_SIZE;
1954 else
1955 bi->bv_len = size;
1956 size -= PAGE_SIZE;
1957 }
1958 }
NeilBrown8f19ccb2011-12-23 10:17:56 +11001959 for (primary = 0; primary < conf->raid_disks * 2; primary++)
NeilBrowna68e5872011-05-11 14:40:44 +10001960 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001961 !r1_bio->bios[primary]->bi_error) {
NeilBrowna68e5872011-05-11 14:40:44 +10001962 r1_bio->bios[primary]->bi_end_io = NULL;
1963 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
1964 break;
1965 }
1966 r1_bio->read_disk = primary;
NeilBrown8f19ccb2011-12-23 10:17:56 +11001967 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10001968 int j;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001969 struct bio *pbio = r1_bio->bios[primary];
1970 struct bio *sbio = r1_bio->bios[i];
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001971 int error = sbio->bi_error;
NeilBrowna68e5872011-05-11 14:40:44 +10001972
Kent Overstreet2aabaa62012-09-11 11:26:12 -07001973 if (sbio->bi_end_io != end_sync_read)
NeilBrown78d7f5f2011-05-11 14:48:56 +10001974 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001975 /* Now we can 'fixup' the error value */
1976 sbio->bi_error = 0;
NeilBrowna68e5872011-05-11 14:40:44 +10001977
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001978 if (!error) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10001979 for (j = vcnt; j-- ; ) {
1980 struct page *p, *s;
1981 p = pbio->bi_io_vec[j].bv_page;
1982 s = sbio->bi_io_vec[j].bv_page;
1983 if (memcmp(page_address(p),
1984 page_address(s),
NeilBrown5020ad72012-04-02 01:39:05 +10001985 sbio->bi_io_vec[j].bv_len))
NeilBrown78d7f5f2011-05-11 14:48:56 +10001986 break;
NeilBrowna68e5872011-05-11 14:40:44 +10001987 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10001988 } else
1989 j = 0;
1990 if (j >= 0)
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11001991 atomic64_add(r1_bio->sectors, &mddev->resync_mismatches);
NeilBrown78d7f5f2011-05-11 14:48:56 +10001992 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001993 && !error)) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10001994 /* No need to write to this device. */
1995 sbio->bi_end_io = NULL;
1996 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
1997 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10001998 }
Kent Overstreetd3b45c22012-09-10 13:49:33 -07001999
2000 bio_copy_data(sbio, pbio);
NeilBrown78d7f5f2011-05-11 14:48:56 +10002001 }
NeilBrowna68e5872011-05-11 14:40:44 +10002002}
2003
NeilBrown9f2c9d12011-10-11 16:48:43 +11002004static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
NeilBrowne8096362011-10-11 16:49:05 +11002006 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 int i;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002008 int disks = conf->raid_disks * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 struct bio *bio, *wbio;
2010
2011 bio = r1_bio->bios[r1_bio->read_disk];
2012
NeilBrowna68e5872011-05-11 14:40:44 +10002013 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
2014 /* ouch - failed to read all of that. */
2015 if (!fix_sync_read_error(r1_bio))
2016 return;
NeilBrown7ca78d52011-05-11 14:50:37 +10002017
2018 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrownc95e6382014-09-09 13:54:11 +10002019 process_checks(r1_bio);
2020
NeilBrownd11c1712006-01-06 00:20:26 -08002021 /*
2022 * schedule writes
2023 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 atomic_set(&r1_bio->remaining, 1);
2025 for (i = 0; i < disks ; i++) {
2026 wbio = r1_bio->bios[i];
NeilBrown3e198f72006-01-06 00:20:21 -08002027 if (wbio->bi_end_io == NULL ||
2028 (wbio->bi_end_io == end_sync_read &&
2029 (i == r1_bio->read_disk ||
2030 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 continue;
2032
Mike Christie796a5cf2016-06-05 14:32:07 -05002033 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
NeilBrown3e198f72006-01-06 00:20:21 -08002034 wbio->bi_end_io = end_sync_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 atomic_inc(&r1_bio->remaining);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002036 md_sync_acct(conf->mirrors[i].rdev->bdev, bio_sectors(wbio));
NeilBrown191ea9b2005-06-21 17:17:23 -07002037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 generic_make_request(wbio);
2039 }
2040
2041 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002042 /* if we're here, all write(s) have completed, so clean up */
NeilBrown58e94ae2012-07-19 15:59:18 +10002043 int s = r1_bio->sectors;
2044 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2045 test_bit(R1BIO_WriteError, &r1_bio->state))
2046 reschedule_retry(r1_bio);
2047 else {
2048 put_buf(r1_bio);
2049 md_done_sync(mddev, s, 1);
2050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052}
2053
2054/*
2055 * This is a kernel thread which:
2056 *
2057 * 1. Retries failed read operations on working mirrors.
2058 * 2. Updates the raid superblock when problems encounter.
NeilBrownd2eb35a2011-07-28 11:31:48 +10002059 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 */
2061
NeilBrowne8096362011-10-11 16:49:05 +11002062static void fix_read_error(struct r1conf *conf, int read_disk,
NeilBrown867868f2006-10-03 01:15:51 -07002063 sector_t sect, int sectors)
2064{
NeilBrownfd01b882011-10-11 16:47:53 +11002065 struct mddev *mddev = conf->mddev;
NeilBrown867868f2006-10-03 01:15:51 -07002066 while(sectors) {
2067 int s = sectors;
2068 int d = read_disk;
2069 int success = 0;
2070 int start;
NeilBrown3cb03002011-10-11 16:45:26 +11002071 struct md_rdev *rdev;
NeilBrown867868f2006-10-03 01:15:51 -07002072
2073 if (s > (PAGE_SIZE>>9))
2074 s = PAGE_SIZE >> 9;
2075
2076 do {
NeilBrownd2eb35a2011-07-28 11:31:48 +10002077 sector_t first_bad;
2078 int bad_sectors;
2079
NeilBrown707a6a42016-06-02 16:19:52 +10002080 rcu_read_lock();
2081 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002082 if (rdev &&
majianpengda8840a2012-05-22 13:55:03 +10002083 (test_bit(In_sync, &rdev->flags) ||
2084 (!test_bit(Faulty, &rdev->flags) &&
2085 rdev->recovery_offset >= sect + s)) &&
NeilBrownd2eb35a2011-07-28 11:31:48 +10002086 is_badblock(rdev, sect, s,
NeilBrown707a6a42016-06-02 16:19:52 +10002087 &first_bad, &bad_sectors) == 0) {
2088 atomic_inc(&rdev->nr_pending);
2089 rcu_read_unlock();
2090 if (sync_page_io(rdev, sect, s<<9,
Mike Christie796a5cf2016-06-05 14:32:07 -05002091 conf->tmppage, REQ_OP_READ, 0, false))
NeilBrown707a6a42016-06-02 16:19:52 +10002092 success = 1;
2093 rdev_dec_pending(rdev, mddev);
2094 if (success)
2095 break;
2096 } else
2097 rcu_read_unlock();
2098 d++;
2099 if (d == conf->raid_disks * 2)
2100 d = 0;
NeilBrown867868f2006-10-03 01:15:51 -07002101 } while (!success && d != read_disk);
2102
2103 if (!success) {
NeilBrownd8f05d22011-07-28 11:33:00 +10002104 /* Cannot read from anywhere - mark it bad */
NeilBrown3cb03002011-10-11 16:45:26 +11002105 struct md_rdev *rdev = conf->mirrors[read_disk].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10002106 if (!rdev_set_badblocks(rdev, sect, s, 0))
2107 md_error(mddev, rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002108 break;
2109 }
2110 /* write it back and re-read */
2111 start = d;
2112 while (d != read_disk) {
2113 if (d==0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002114 d = conf->raid_disks * 2;
NeilBrown867868f2006-10-03 01:15:51 -07002115 d--;
NeilBrown707a6a42016-06-02 16:19:52 +10002116 rcu_read_lock();
2117 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002118 if (rdev &&
NeilBrown707a6a42016-06-02 16:19:52 +10002119 !test_bit(Faulty, &rdev->flags)) {
2120 atomic_inc(&rdev->nr_pending);
2121 rcu_read_unlock();
NeilBrownd8f05d22011-07-28 11:33:00 +10002122 r1_sync_page_io(rdev, sect, s,
2123 conf->tmppage, WRITE);
NeilBrown707a6a42016-06-02 16:19:52 +10002124 rdev_dec_pending(rdev, mddev);
2125 } else
2126 rcu_read_unlock();
NeilBrown867868f2006-10-03 01:15:51 -07002127 }
2128 d = start;
2129 while (d != read_disk) {
2130 char b[BDEVNAME_SIZE];
2131 if (d==0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002132 d = conf->raid_disks * 2;
NeilBrown867868f2006-10-03 01:15:51 -07002133 d--;
NeilBrown707a6a42016-06-02 16:19:52 +10002134 rcu_read_lock();
2135 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002136 if (rdev &&
NeilBrownb8cb6b42014-09-18 11:09:04 +10002137 !test_bit(Faulty, &rdev->flags)) {
NeilBrown707a6a42016-06-02 16:19:52 +10002138 atomic_inc(&rdev->nr_pending);
2139 rcu_read_unlock();
NeilBrownd8f05d22011-07-28 11:33:00 +10002140 if (r1_sync_page_io(rdev, sect, s,
2141 conf->tmppage, READ)) {
NeilBrown867868f2006-10-03 01:15:51 -07002142 atomic_add(s, &rdev->corrected_errors);
NeilBrown1d41c212016-11-02 14:16:50 +11002143 pr_info("md/raid1:%s: read error corrected (%d sectors at %llu on %s)\n",
2144 mdname(mddev), s,
2145 (unsigned long long)(sect +
2146 rdev->data_offset),
2147 bdevname(rdev->bdev, b));
NeilBrown867868f2006-10-03 01:15:51 -07002148 }
NeilBrown707a6a42016-06-02 16:19:52 +10002149 rdev_dec_pending(rdev, mddev);
2150 } else
2151 rcu_read_unlock();
NeilBrown867868f2006-10-03 01:15:51 -07002152 }
2153 sectors -= s;
2154 sect += s;
2155 }
2156}
2157
NeilBrown9f2c9d12011-10-11 16:48:43 +11002158static int narrow_write_error(struct r1bio *r1_bio, int i)
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002159{
NeilBrownfd01b882011-10-11 16:47:53 +11002160 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11002161 struct r1conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002162 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002163
2164 /* bio has the data to be written to device 'i' where
2165 * we just recently had a write error.
2166 * We repeatedly clone the bio and trim down to one block,
2167 * then try the write. Where the write fails we record
2168 * a bad block.
2169 * It is conceivable that the bio doesn't exactly align with
2170 * blocks. We must handle this somehow.
2171 *
2172 * We currently own a reference on the rdev.
2173 */
2174
2175 int block_sectors;
2176 sector_t sector;
2177 int sectors;
2178 int sect_to_write = r1_bio->sectors;
2179 int ok = 1;
2180
2181 if (rdev->badblocks.shift < 0)
2182 return 0;
2183
Nate Daileyab713cd2015-02-12 12:02:09 -05002184 block_sectors = roundup(1 << rdev->badblocks.shift,
2185 bdev_logical_block_size(rdev->bdev) >> 9);
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002186 sector = r1_bio->sector;
2187 sectors = ((sector + block_sectors)
2188 & ~(sector_t)(block_sectors - 1))
2189 - sector;
2190
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002191 while (sect_to_write) {
2192 struct bio *wbio;
2193 if (sectors > sect_to_write)
2194 sectors = sect_to_write;
2195 /* Write at 'sector' for 'sectors'*/
2196
Kent Overstreetb7838632012-09-10 15:17:11 -07002197 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
2198 unsigned vcnt = r1_bio->behind_page_count;
2199 struct bio_vec *vec = r1_bio->behind_bvecs;
2200
2201 while (!vec->bv_page) {
2202 vec++;
2203 vcnt--;
2204 }
2205
2206 wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev);
2207 memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec));
2208
2209 wbio->bi_vcnt = vcnt;
2210 } else {
2211 wbio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
2212 }
2213
Mike Christie796a5cf2016-06-05 14:32:07 -05002214 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002215 wbio->bi_iter.bi_sector = r1_bio->sector;
2216 wbio->bi_iter.bi_size = r1_bio->sectors << 9;
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002217
Kent Overstreet6678d832013-08-07 11:14:32 -07002218 bio_trim(wbio, sector - r1_bio->sector, sectors);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002219 wbio->bi_iter.bi_sector += rdev->data_offset;
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002220 wbio->bi_bdev = rdev->bdev;
Mike Christie4e49ea42016-06-05 14:31:41 -05002221
2222 if (submit_bio_wait(wbio) < 0)
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002223 /* failure! */
2224 ok = rdev_set_badblocks(rdev, sector,
2225 sectors, 0)
2226 && ok;
2227
2228 bio_put(wbio);
2229 sect_to_write -= sectors;
2230 sector += sectors;
2231 sectors = block_sectors;
2232 }
2233 return ok;
2234}
2235
NeilBrowne8096362011-10-11 16:49:05 +11002236static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002237{
2238 int m;
2239 int s = r1_bio->sectors;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002240 for (m = 0; m < conf->raid_disks * 2 ; m++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002241 struct md_rdev *rdev = conf->mirrors[m].rdev;
NeilBrown62096bc2011-07-28 11:38:13 +10002242 struct bio *bio = r1_bio->bios[m];
2243 if (bio->bi_end_io == NULL)
2244 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002245 if (!bio->bi_error &&
NeilBrown62096bc2011-07-28 11:38:13 +10002246 test_bit(R1BIO_MadeGood, &r1_bio->state)) {
NeilBrownc6563a82012-05-21 09:27:00 +10002247 rdev_clear_badblocks(rdev, r1_bio->sector, s, 0);
NeilBrown62096bc2011-07-28 11:38:13 +10002248 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002249 if (bio->bi_error &&
NeilBrown62096bc2011-07-28 11:38:13 +10002250 test_bit(R1BIO_WriteError, &r1_bio->state)) {
2251 if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
2252 md_error(conf->mddev, rdev);
2253 }
2254 }
2255 put_buf(r1_bio);
2256 md_done_sync(conf->mddev, s, 1);
2257}
2258
NeilBrowne8096362011-10-11 16:49:05 +11002259static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002260{
2261 int m;
NeilBrown55ce74d2015-08-14 11:11:10 +10002262 bool fail = false;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002263 for (m = 0; m < conf->raid_disks * 2 ; m++)
NeilBrown62096bc2011-07-28 11:38:13 +10002264 if (r1_bio->bios[m] == IO_MADE_GOOD) {
NeilBrown3cb03002011-10-11 16:45:26 +11002265 struct md_rdev *rdev = conf->mirrors[m].rdev;
NeilBrown62096bc2011-07-28 11:38:13 +10002266 rdev_clear_badblocks(rdev,
2267 r1_bio->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10002268 r1_bio->sectors, 0);
NeilBrown62096bc2011-07-28 11:38:13 +10002269 rdev_dec_pending(rdev, conf->mddev);
2270 } else if (r1_bio->bios[m] != NULL) {
2271 /* This drive got a write error. We need to
2272 * narrow down and record precise write
2273 * errors.
2274 */
NeilBrown55ce74d2015-08-14 11:11:10 +10002275 fail = true;
NeilBrown62096bc2011-07-28 11:38:13 +10002276 if (!narrow_write_error(r1_bio, m)) {
2277 md_error(conf->mddev,
2278 conf->mirrors[m].rdev);
2279 /* an I/O failed, we can't clear the bitmap */
2280 set_bit(R1BIO_Degraded, &r1_bio->state);
2281 }
2282 rdev_dec_pending(conf->mirrors[m].rdev,
2283 conf->mddev);
2284 }
NeilBrown55ce74d2015-08-14 11:11:10 +10002285 if (fail) {
2286 spin_lock_irq(&conf->device_lock);
2287 list_add(&r1_bio->retry_list, &conf->bio_end_io_list);
Nate Daileyccfc7bf2016-02-29 10:43:58 -05002288 conf->nr_queued++;
NeilBrown55ce74d2015-08-14 11:11:10 +10002289 spin_unlock_irq(&conf->device_lock);
2290 md_wakeup_thread(conf->mddev->thread);
NeilBrownbd8688a2015-10-24 16:02:16 +11002291 } else {
2292 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2293 close_write(r1_bio);
NeilBrown55ce74d2015-08-14 11:11:10 +10002294 raid_end_bio_io(r1_bio);
NeilBrownbd8688a2015-10-24 16:02:16 +11002295 }
NeilBrown62096bc2011-07-28 11:38:13 +10002296}
2297
NeilBrowne8096362011-10-11 16:49:05 +11002298static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002299{
2300 int disk;
2301 int max_sectors;
NeilBrownfd01b882011-10-11 16:47:53 +11002302 struct mddev *mddev = conf->mddev;
NeilBrown62096bc2011-07-28 11:38:13 +10002303 struct bio *bio;
2304 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11002305 struct md_rdev *rdev;
NeilBrown109e3762016-11-18 13:22:04 +11002306 dev_t bio_dev;
2307 sector_t bio_sector;
NeilBrown62096bc2011-07-28 11:38:13 +10002308
2309 clear_bit(R1BIO_ReadError, &r1_bio->state);
2310 /* we got a read error. Maybe the drive is bad. Maybe just
2311 * the block and we can fix it.
2312 * We freeze all other IO, and try reading the block from
2313 * other devices. When we find one, we re-write
2314 * and check it that fixes the read error.
2315 * This is all done synchronously while the array is
2316 * frozen
2317 */
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002318
2319 bio = r1_bio->bios[r1_bio->read_disk];
2320 bdevname(bio->bi_bdev, b);
NeilBrown109e3762016-11-18 13:22:04 +11002321 bio_dev = bio->bi_bdev->bd_dev;
2322 bio_sector = conf->mirrors[r1_bio->read_disk].rdev->data_offset + r1_bio->sector;
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002323 bio_put(bio);
2324 r1_bio->bios[r1_bio->read_disk] = NULL;
2325
NeilBrown62096bc2011-07-28 11:38:13 +10002326 if (mddev->ro == 0) {
NeilBrowne2d59922013-06-12 11:01:22 +10002327 freeze_array(conf, 1);
NeilBrown62096bc2011-07-28 11:38:13 +10002328 fix_read_error(conf, r1_bio->read_disk,
2329 r1_bio->sector, r1_bio->sectors);
2330 unfreeze_array(conf);
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002331 } else {
2332 r1_bio->bios[r1_bio->read_disk] = IO_BLOCKED;
2333 }
2334
NeilBrown7ad4d4a2012-10-11 13:44:30 +11002335 rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev);
NeilBrown62096bc2011-07-28 11:38:13 +10002336
NeilBrown62096bc2011-07-28 11:38:13 +10002337read_more:
2338 disk = read_balance(conf, r1_bio, &max_sectors);
2339 if (disk == -1) {
NeilBrown1d41c212016-11-02 14:16:50 +11002340 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
2341 mdname(mddev), b, (unsigned long long)r1_bio->sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002342 raid_end_bio_io(r1_bio);
2343 } else {
2344 const unsigned long do_sync
Jens Axboe1eff9d32016-08-05 15:35:16 -06002345 = r1_bio->master_bio->bi_opf & REQ_SYNC;
NeilBrown62096bc2011-07-28 11:38:13 +10002346 r1_bio->read_disk = disk;
2347 bio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002348 bio_trim(bio, r1_bio->sector - bio->bi_iter.bi_sector,
2349 max_sectors);
NeilBrown62096bc2011-07-28 11:38:13 +10002350 r1_bio->bios[r1_bio->read_disk] = bio;
2351 rdev = conf->mirrors[disk].rdev;
NeilBrown1d41c212016-11-02 14:16:50 +11002352 pr_info_ratelimited("md/raid1:%s: redirecting sector %llu to other mirror: %s\n",
2353 mdname(mddev),
2354 (unsigned long long)r1_bio->sector,
2355 bdevname(rdev->bdev, b));
Kent Overstreet4f024f32013-10-11 15:44:27 -07002356 bio->bi_iter.bi_sector = r1_bio->sector + rdev->data_offset;
NeilBrown62096bc2011-07-28 11:38:13 +10002357 bio->bi_bdev = rdev->bdev;
2358 bio->bi_end_io = raid1_end_read_request;
Mike Christie796a5cf2016-06-05 14:32:07 -05002359 bio_set_op_attrs(bio, REQ_OP_READ, do_sync);
NeilBrown62096bc2011-07-28 11:38:13 +10002360 bio->bi_private = r1_bio;
2361 if (max_sectors < r1_bio->sectors) {
2362 /* Drat - have to split this up more */
2363 struct bio *mbio = r1_bio->master_bio;
2364 int sectors_handled = (r1_bio->sector + max_sectors
Kent Overstreet4f024f32013-10-11 15:44:27 -07002365 - mbio->bi_iter.bi_sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002366 r1_bio->sectors = max_sectors;
2367 spin_lock_irq(&conf->device_lock);
2368 if (mbio->bi_phys_segments == 0)
2369 mbio->bi_phys_segments = 2;
2370 else
2371 mbio->bi_phys_segments++;
2372 spin_unlock_irq(&conf->device_lock);
NeilBrown109e3762016-11-18 13:22:04 +11002373 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2374 bio, bio_dev, bio_sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002375 generic_make_request(bio);
2376 bio = NULL;
2377
2378 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
2379
2380 r1_bio->master_bio = mbio;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002381 r1_bio->sectors = bio_sectors(mbio) - sectors_handled;
NeilBrown62096bc2011-07-28 11:38:13 +10002382 r1_bio->state = 0;
2383 set_bit(R1BIO_ReadError, &r1_bio->state);
2384 r1_bio->mddev = mddev;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002385 r1_bio->sector = mbio->bi_iter.bi_sector +
2386 sectors_handled;
NeilBrown62096bc2011-07-28 11:38:13 +10002387
2388 goto read_more;
NeilBrown109e3762016-11-18 13:22:04 +11002389 } else {
2390 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2391 bio, bio_dev, bio_sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002392 generic_make_request(bio);
NeilBrown109e3762016-11-18 13:22:04 +11002393 }
NeilBrown62096bc2011-07-28 11:38:13 +10002394 }
2395}
2396
Shaohua Li4ed87312012-10-11 13:34:00 +11002397static void raid1d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
Shaohua Li4ed87312012-10-11 13:34:00 +11002399 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002400 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 unsigned long flags;
NeilBrowne8096362011-10-11 16:49:05 +11002402 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002404 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 md_check_recovery(mddev);
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002407
NeilBrown55ce74d2015-08-14 11:11:10 +10002408 if (!list_empty_careful(&conf->bio_end_io_list) &&
2409 !test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
2410 LIST_HEAD(tmp);
2411 spin_lock_irqsave(&conf->device_lock, flags);
2412 if (!test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
Nate Daileyccfc7bf2016-02-29 10:43:58 -05002413 while (!list_empty(&conf->bio_end_io_list)) {
2414 list_move(conf->bio_end_io_list.prev, &tmp);
2415 conf->nr_queued--;
2416 }
NeilBrown55ce74d2015-08-14 11:11:10 +10002417 }
2418 spin_unlock_irqrestore(&conf->device_lock, flags);
2419 while (!list_empty(&tmp)) {
Mikulas Patockaa4527442015-10-01 15:17:43 -04002420 r1_bio = list_first_entry(&tmp, struct r1bio,
2421 retry_list);
NeilBrown55ce74d2015-08-14 11:11:10 +10002422 list_del(&r1_bio->retry_list);
NeilBrownbd8688a2015-10-24 16:02:16 +11002423 if (mddev->degraded)
2424 set_bit(R1BIO_Degraded, &r1_bio->state);
2425 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2426 close_write(r1_bio);
NeilBrown55ce74d2015-08-14 11:11:10 +10002427 raid_end_bio_io(r1_bio);
2428 }
2429 }
2430
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002431 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002433
NeilBrown0021b7b2012-07-31 09:08:14 +02002434 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002437 if (list_empty(head)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002438 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002440 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002441 r1_bio = list_entry(head->prev, struct r1bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 list_del(head->prev);
NeilBrownddaf22a2006-01-06 00:20:19 -08002443 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 spin_unlock_irqrestore(&conf->device_lock, flags);
2445
2446 mddev = r1_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002447 conf = mddev->private;
NeilBrown4367af52011-07-28 11:31:49 +10002448 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
NeilBrownd8f05d22011-07-28 11:33:00 +10002449 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
NeilBrown62096bc2011-07-28 11:38:13 +10002450 test_bit(R1BIO_WriteError, &r1_bio->state))
2451 handle_sync_write_finished(conf, r1_bio);
2452 else
NeilBrown4367af52011-07-28 11:31:49 +10002453 sync_request_write(mddev, r1_bio);
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002454 } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
NeilBrown62096bc2011-07-28 11:38:13 +10002455 test_bit(R1BIO_WriteError, &r1_bio->state))
2456 handle_write_finished(conf, r1_bio);
2457 else if (test_bit(R1BIO_ReadError, &r1_bio->state))
2458 handle_read_error(conf, r1_bio);
2459 else
NeilBrownd2eb35a2011-07-28 11:31:48 +10002460 /* just a partial read to be scheduled from separate
2461 * context
2462 */
2463 generic_make_request(r1_bio->bios[r1_bio->read_disk]);
NeilBrown62096bc2011-07-28 11:38:13 +10002464
NeilBrown1d9d5242009-10-16 15:55:32 +11002465 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10002466 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2467 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002469 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
2471
NeilBrowne8096362011-10-11 16:49:05 +11002472static int init_resync(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
2474 int buffs;
2475
2476 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhenn9e77c482006-04-01 01:08:49 +02002477 BUG_ON(conf->r1buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
2479 conf->poolinfo);
2480 if (!conf->r1buf_pool)
2481 return -ENOMEM;
2482 conf->next_resync = 0;
2483 return 0;
2484}
2485
2486/*
2487 * perform a "sync" on one "block"
2488 *
2489 * We need to make sure that no normal I/O request - particularly write
2490 * requests - conflict with active sync requests.
2491 *
2492 * This is achieved by tracking pending requests and a 'barrier' concept
2493 * that can be installed to exclude normal IO requests.
2494 */
2495
Shaohua Li849674e2016-01-20 13:52:20 -08002496static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr,
2497 int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
NeilBrowne8096362011-10-11 16:49:05 +11002499 struct r1conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002500 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 struct bio *bio;
2502 sector_t max_sector, nr_sectors;
NeilBrown3e198f72006-01-06 00:20:21 -08002503 int disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 int i;
NeilBrown3e198f72006-01-06 00:20:21 -08002505 int wonly = -1;
2506 int write_targets = 0, read_targets = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11002507 sector_t sync_blocks;
NeilBrowne3b97032005-08-04 12:53:34 -07002508 int still_degraded = 0;
NeilBrown06f60382011-07-28 11:31:48 +10002509 int good_sectors = RESYNC_SECTORS;
2510 int min_bad = 0; /* number of sectors that are bad in all devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 if (!conf->r1buf_pool)
2513 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Andre Noll58c0fed2009-03-31 14:33:13 +11002516 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 if (sector_nr >= max_sector) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002518 /* If we aborted, we need to abort the
2519 * sync on the 'current' bitmap chunk (there will
2520 * only be one in raid1 resync.
2521 * We can find the current addess in mddev->curr_resync
2522 */
NeilBrown6a806c52005-07-15 03:56:35 -07002523 if (mddev->curr_resync < max_sector) /* aborted */
2524 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
NeilBrown191ea9b2005-06-21 17:17:23 -07002525 &sync_blocks, 1);
NeilBrown6a806c52005-07-15 03:56:35 -07002526 else /* completed sync */
NeilBrown191ea9b2005-06-21 17:17:23 -07002527 conf->fullsync = 0;
NeilBrown6a806c52005-07-15 03:56:35 -07002528
2529 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 close_sync(conf);
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002531
2532 if (mddev_is_clustered(mddev)) {
2533 conf->cluster_sync_low = 0;
2534 conf->cluster_sync_high = 0;
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return 0;
2537 }
2538
NeilBrown07d84d102006-06-26 00:27:56 -07002539 if (mddev->bitmap == NULL &&
2540 mddev->recovery_cp == MaxSector &&
NeilBrown6394cca2006-08-27 01:23:50 -07002541 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown07d84d102006-06-26 00:27:56 -07002542 conf->fullsync == 0) {
2543 *skipped = 1;
2544 return max_sector - sector_nr;
2545 }
NeilBrown6394cca2006-08-27 01:23:50 -07002546 /* before building a request, check if we can skip these blocks..
2547 * This call the bitmap_start_sync doesn't actually record anything
2548 */
NeilBrowne3b97032005-08-04 12:53:34 -07002549 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrowne5de485f2005-11-08 21:39:38 -08002550 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002551 /* We can skip this block, and probably several more */
2552 *skipped = 1;
2553 return sync_blocks;
2554 }
NeilBrown17999be2006-01-06 00:20:12 -08002555
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02002556 /*
2557 * If there is non-resync activity waiting for a turn, then let it
2558 * though before starting on this new sync request.
2559 */
2560 if (conf->nr_waiting)
2561 schedule_timeout_uninterruptible(1);
2562
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002563 /* we are incrementing sector_nr below. To be safe, we check against
2564 * sector_nr + two times RESYNC_SECTORS
2565 */
2566
2567 bitmap_cond_end_sync(mddev->bitmap, sector_nr,
2568 mddev_is_clustered(mddev) && (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
NeilBrown1c4588e2010-10-26 17:41:22 +11002569 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
NeilBrown17999be2006-01-06 00:20:12 -08002570
NeilBrownc2fd4c92014-09-10 16:01:24 +10002571 raise_barrier(conf, sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
NeilBrown3e198f72006-01-06 00:20:21 -08002573 rcu_read_lock();
2574 /*
2575 * If we get a correctably read error during resync or recovery,
2576 * we might want to read from a different device. So we
2577 * flag all drives that could conceivably be read from for READ,
2578 * and any others (which will be non-In_sync devices) for WRITE.
2579 * If a read fails, we try reading from something else for which READ
2580 * is OK.
2581 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 r1_bio->mddev = mddev;
2584 r1_bio->sector = sector_nr;
NeilBrown191ea9b2005-06-21 17:17:23 -07002585 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 set_bit(R1BIO_IsSync, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
NeilBrown8f19ccb2011-12-23 10:17:56 +11002588 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002589 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 bio = r1_bio->bios[i];
Kent Overstreet2aabaa62012-09-11 11:26:12 -07002591 bio_reset(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
NeilBrown3e198f72006-01-06 00:20:21 -08002593 rdev = rcu_dereference(conf->mirrors[i].rdev);
2594 if (rdev == NULL ||
NeilBrown06f60382011-07-28 11:31:48 +10002595 test_bit(Faulty, &rdev->flags)) {
NeilBrown8f19ccb2011-12-23 10:17:56 +11002596 if (i < conf->raid_disks)
2597 still_degraded = 1;
NeilBrown3e198f72006-01-06 00:20:21 -08002598 } else if (!test_bit(In_sync, &rdev->flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05002599 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 bio->bi_end_io = end_sync_write;
2601 write_targets ++;
NeilBrown3e198f72006-01-06 00:20:21 -08002602 } else {
2603 /* may need to read from here */
NeilBrown06f60382011-07-28 11:31:48 +10002604 sector_t first_bad = MaxSector;
2605 int bad_sectors;
2606
2607 if (is_badblock(rdev, sector_nr, good_sectors,
2608 &first_bad, &bad_sectors)) {
2609 if (first_bad > sector_nr)
2610 good_sectors = first_bad - sector_nr;
2611 else {
2612 bad_sectors -= (sector_nr - first_bad);
2613 if (min_bad == 0 ||
2614 min_bad > bad_sectors)
2615 min_bad = bad_sectors;
2616 }
NeilBrown3e198f72006-01-06 00:20:21 -08002617 }
NeilBrown06f60382011-07-28 11:31:48 +10002618 if (sector_nr < first_bad) {
2619 if (test_bit(WriteMostly, &rdev->flags)) {
2620 if (wonly < 0)
2621 wonly = i;
2622 } else {
2623 if (disk < 0)
2624 disk = i;
2625 }
Mike Christie796a5cf2016-06-05 14:32:07 -05002626 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown06f60382011-07-28 11:31:48 +10002627 bio->bi_end_io = end_sync_read;
2628 read_targets++;
Alexander Lyakasd57368a2012-07-17 13:17:55 +03002629 } else if (!test_bit(WriteErrorSeen, &rdev->flags) &&
2630 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
2631 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
2632 /*
2633 * The device is suitable for reading (InSync),
2634 * but has bad block(s) here. Let's try to correct them,
2635 * if we are doing resync or repair. Otherwise, leave
2636 * this device alone for this sync request.
2637 */
Mike Christie796a5cf2016-06-05 14:32:07 -05002638 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Alexander Lyakasd57368a2012-07-17 13:17:55 +03002639 bio->bi_end_io = end_sync_write;
2640 write_targets++;
NeilBrown06f60382011-07-28 11:31:48 +10002641 }
NeilBrown3e198f72006-01-06 00:20:21 -08002642 }
NeilBrown06f60382011-07-28 11:31:48 +10002643 if (bio->bi_end_io) {
2644 atomic_inc(&rdev->nr_pending);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002645 bio->bi_iter.bi_sector = sector_nr + rdev->data_offset;
NeilBrown06f60382011-07-28 11:31:48 +10002646 bio->bi_bdev = rdev->bdev;
2647 bio->bi_private = r1_bio;
2648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
NeilBrown3e198f72006-01-06 00:20:21 -08002650 rcu_read_unlock();
2651 if (disk < 0)
2652 disk = wonly;
2653 r1_bio->read_disk = disk;
NeilBrown191ea9b2005-06-21 17:17:23 -07002654
NeilBrown06f60382011-07-28 11:31:48 +10002655 if (read_targets == 0 && min_bad > 0) {
2656 /* These sectors are bad on all InSync devices, so we
2657 * need to mark them bad on all write targets
2658 */
2659 int ok = 1;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002660 for (i = 0 ; i < conf->raid_disks * 2 ; i++)
NeilBrown06f60382011-07-28 11:31:48 +10002661 if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
majianpenga42f9d82012-04-02 01:04:19 +10002662 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrown06f60382011-07-28 11:31:48 +10002663 ok = rdev_set_badblocks(rdev, sector_nr,
2664 min_bad, 0
2665 ) && ok;
2666 }
2667 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2668 *skipped = 1;
2669 put_buf(r1_bio);
2670
2671 if (!ok) {
2672 /* Cannot record the badblocks, so need to
2673 * abort the resync.
2674 * If there are multiple read targets, could just
2675 * fail the really bad ones ???
2676 */
2677 conf->recovery_disabled = mddev->recovery_disabled;
2678 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2679 return 0;
2680 } else
2681 return min_bad;
2682
2683 }
2684 if (min_bad > 0 && min_bad < good_sectors) {
2685 /* only resync enough to reach the next bad->good
2686 * transition */
2687 good_sectors = min_bad;
2688 }
2689
NeilBrown3e198f72006-01-06 00:20:21 -08002690 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
2691 /* extra read targets are also write targets */
2692 write_targets += read_targets-1;
2693
2694 if (write_targets == 0 || read_targets == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 /* There is nowhere to write, so all non-sync
2696 * drives must be failed - so we are finished
2697 */
NeilBrownb7219cc2012-07-31 10:05:34 +10002698 sector_t rv;
2699 if (min_bad > 0)
2700 max_sector = sector_nr + min_bad;
2701 rv = max_sector - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07002702 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 put_buf(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 return rv;
2705 }
2706
NeilBrownc6207272008-02-06 01:39:52 -08002707 if (max_sector > mddev->resync_max)
2708 max_sector = mddev->resync_max; /* Don't do IO beyond here */
NeilBrown06f60382011-07-28 11:31:48 +10002709 if (max_sector > sector_nr + good_sectors)
2710 max_sector = sector_nr + good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 nr_sectors = 0;
NeilBrown289e99e2005-06-21 17:17:24 -07002712 sync_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 do {
2714 struct page *page;
2715 int len = PAGE_SIZE;
2716 if (sector_nr + (len>>9) > max_sector)
2717 len = (max_sector - sector_nr) << 9;
2718 if (len == 0)
2719 break;
NeilBrown6a806c52005-07-15 03:56:35 -07002720 if (sync_blocks == 0) {
2721 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
NeilBrowne5de485f2005-11-08 21:39:38 -08002722 &sync_blocks, still_degraded) &&
2723 !conf->fullsync &&
2724 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrown6a806c52005-07-15 03:56:35 -07002725 break;
NeilBrown7571ae82010-10-07 11:54:46 +11002726 if ((len >> 9) > sync_blocks)
NeilBrown6a806c52005-07-15 03:56:35 -07002727 len = sync_blocks<<9;
NeilBrownab7a30c2005-06-21 17:17:23 -07002728 }
NeilBrown191ea9b2005-06-21 17:17:23 -07002729
NeilBrown8f19ccb2011-12-23 10:17:56 +11002730 for (i = 0 ; i < conf->raid_disks * 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 bio = r1_bio->bios[i];
2732 if (bio->bi_end_io) {
NeilBrownd11c1712006-01-06 00:20:26 -08002733 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 if (bio_add_page(bio, page, len, 0) == 0) {
2735 /* stop here */
NeilBrownd11c1712006-01-06 00:20:26 -08002736 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 while (i > 0) {
2738 i--;
2739 bio = r1_bio->bios[i];
NeilBrown6a806c52005-07-15 03:56:35 -07002740 if (bio->bi_end_io==NULL)
2741 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 /* remove last page from this bio */
2743 bio->bi_vcnt--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002744 bio->bi_iter.bi_size -= len;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06002745 bio_clear_flag(bio, BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 }
2747 goto bio_full;
2748 }
2749 }
2750 }
2751 nr_sectors += len>>9;
2752 sector_nr += len>>9;
NeilBrown191ea9b2005-06-21 17:17:23 -07002753 sync_blocks -= (len>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
2755 bio_full:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 r1_bio->sectors = nr_sectors;
2757
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002758 if (mddev_is_clustered(mddev) &&
2759 conf->cluster_sync_high < sector_nr + nr_sectors) {
2760 conf->cluster_sync_low = mddev->curr_resync_completed;
2761 conf->cluster_sync_high = conf->cluster_sync_low + CLUSTER_RESYNC_WINDOW_SECTORS;
2762 /* Send resync message */
2763 md_cluster_ops->resync_info_update(mddev,
2764 conf->cluster_sync_low,
2765 conf->cluster_sync_high);
2766 }
2767
NeilBrownd11c1712006-01-06 00:20:26 -08002768 /* For a user-requested sync, we read all readable devices and do a
2769 * compare
2770 */
2771 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2772 atomic_set(&r1_bio->remaining, read_targets);
NeilBrown2d4f4f32012-07-09 11:34:13 +10002773 for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) {
NeilBrownd11c1712006-01-06 00:20:26 -08002774 bio = r1_bio->bios[i];
2775 if (bio->bi_end_io == end_sync_read) {
NeilBrown2d4f4f32012-07-09 11:34:13 +10002776 read_targets--;
NeilBrownddac7c72006-08-31 21:27:36 -07002777 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrownd11c1712006-01-06 00:20:26 -08002778 generic_make_request(bio);
2779 }
2780 }
2781 } else {
2782 atomic_set(&r1_bio->remaining, 1);
2783 bio = r1_bio->bios[r1_bio->read_disk];
NeilBrownddac7c72006-08-31 21:27:36 -07002784 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrownd11c1712006-01-06 00:20:26 -08002785 generic_make_request(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
NeilBrownd11c1712006-01-06 00:20:26 -08002787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 return nr_sectors;
2789}
2790
NeilBrownfd01b882011-10-11 16:47:53 +11002791static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07002792{
2793 if (sectors)
2794 return sectors;
2795
2796 return mddev->dev_sectors;
2797}
2798
NeilBrowne8096362011-10-11 16:49:05 +11002799static struct r1conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800{
NeilBrowne8096362011-10-11 16:49:05 +11002801 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11002802 int i;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10002803 struct raid1_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11002804 struct md_rdev *rdev;
NeilBrown709ae482009-12-14 12:49:51 +11002805 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
NeilBrowne8096362011-10-11 16:49:05 +11002807 conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (!conf)
NeilBrown709ae482009-12-14 12:49:51 +11002809 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10002811 conf->mirrors = kzalloc(sizeof(struct raid1_info)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002812 * mddev->raid_disks * 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 GFP_KERNEL);
2814 if (!conf->mirrors)
NeilBrown709ae482009-12-14 12:49:51 +11002815 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
NeilBrownddaf22a2006-01-06 00:20:19 -08002817 conf->tmppage = alloc_page(GFP_KERNEL);
2818 if (!conf->tmppage)
NeilBrown709ae482009-12-14 12:49:51 +11002819 goto abort;
NeilBrownddaf22a2006-01-06 00:20:19 -08002820
NeilBrown709ae482009-12-14 12:49:51 +11002821 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (!conf->poolinfo)
NeilBrown709ae482009-12-14 12:49:51 +11002823 goto abort;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002824 conf->poolinfo->raid_disks = mddev->raid_disks * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2826 r1bio_pool_free,
2827 conf->poolinfo);
2828 if (!conf->r1bio_pool)
NeilBrown709ae482009-12-14 12:49:51 +11002829 goto abort;
2830
NeilBrowned9bfdf2009-10-16 15:55:44 +11002831 conf->poolinfo->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
NeilBrownc19d5792011-12-23 10:17:57 +11002833 err = -EINVAL;
Neil Browne7e72bf2008-05-14 16:05:54 -07002834 spin_lock_init(&conf->device_lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11002835 rdev_for_each(rdev, mddev) {
NeilBrownaba336b2012-05-31 15:39:11 +10002836 struct request_queue *q;
NeilBrown709ae482009-12-14 12:49:51 +11002837 int disk_idx = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 if (disk_idx >= mddev->raid_disks
2839 || disk_idx < 0)
2840 continue;
NeilBrownc19d5792011-12-23 10:17:57 +11002841 if (test_bit(Replacement, &rdev->flags))
NeilBrown02b898f2012-10-31 11:42:03 +11002842 disk = conf->mirrors + mddev->raid_disks + disk_idx;
NeilBrownc19d5792011-12-23 10:17:57 +11002843 else
2844 disk = conf->mirrors + disk_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
NeilBrownc19d5792011-12-23 10:17:57 +11002846 if (disk->rdev)
2847 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 disk->rdev = rdev;
NeilBrownaba336b2012-05-31 15:39:11 +10002849 q = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 disk->head_position = 0;
Shaohua Li12cee5a2012-07-31 10:03:53 +10002852 disk->seq_start = MaxSector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 }
2854 conf->raid_disks = mddev->raid_disks;
2855 conf->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 INIT_LIST_HEAD(&conf->retry_list);
NeilBrown55ce74d2015-08-14 11:11:10 +10002857 INIT_LIST_HEAD(&conf->bio_end_io_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
2859 spin_lock_init(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -08002860 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
NeilBrown191ea9b2005-06-21 17:17:23 -07002862 bio_list_init(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +11002863 conf->pending_count = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11002864 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown191ea9b2005-06-21 17:17:23 -07002865
majianpeng79ef3a82013-11-15 14:55:02 +08002866 conf->start_next_window = MaxSector;
2867 conf->current_window_requests = conf->next_window_requests = 0;
2868
NeilBrownc19d5792011-12-23 10:17:57 +11002869 err = -EIO;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002870 for (i = 0; i < conf->raid_disks * 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
2872 disk = conf->mirrors + i;
2873
NeilBrownc19d5792011-12-23 10:17:57 +11002874 if (i < conf->raid_disks &&
2875 disk[conf->raid_disks].rdev) {
2876 /* This slot has a replacement. */
2877 if (!disk->rdev) {
2878 /* No original, just make the replacement
2879 * a recovering spare
2880 */
2881 disk->rdev =
2882 disk[conf->raid_disks].rdev;
2883 disk[conf->raid_disks].rdev = NULL;
2884 } else if (!test_bit(In_sync, &disk->rdev->flags))
2885 /* Original is not in_sync - bad */
2886 goto abort;
2887 }
2888
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002889 if (!disk->rdev ||
2890 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 disk->head_position = 0;
Jonathan Brassow4f0a5e02012-05-22 13:55:31 +10002892 if (disk->rdev &&
2893 (disk->rdev->saved_raid_disk < 0))
NeilBrown918f0232007-08-22 14:01:52 -07002894 conf->fullsync = 1;
Shaohua Libe4d3282012-07-31 10:03:53 +10002895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 }
NeilBrown709ae482009-12-14 12:49:51 +11002897
NeilBrown709ae482009-12-14 12:49:51 +11002898 err = -ENOMEM;
NeilBrown02326052012-07-03 15:56:52 +10002899 conf->thread = md_register_thread(raid1d, mddev, "raid1");
NeilBrown1d41c212016-11-02 14:16:50 +11002900 if (!conf->thread)
NeilBrown709ae482009-12-14 12:49:51 +11002901 goto abort;
NeilBrown191ea9b2005-06-21 17:17:23 -07002902
NeilBrown709ae482009-12-14 12:49:51 +11002903 return conf;
2904
2905 abort:
2906 if (conf) {
Julia Lawall644df1a2015-09-13 14:15:10 +02002907 mempool_destroy(conf->r1bio_pool);
NeilBrown709ae482009-12-14 12:49:51 +11002908 kfree(conf->mirrors);
2909 safe_put_page(conf->tmppage);
2910 kfree(conf->poolinfo);
2911 kfree(conf);
2912 }
2913 return ERR_PTR(err);
2914}
2915
NeilBrownafa0f552014-12-15 12:56:58 +11002916static void raid1_free(struct mddev *mddev, void *priv);
Shaohua Li849674e2016-01-20 13:52:20 -08002917static int raid1_run(struct mddev *mddev)
NeilBrown709ae482009-12-14 12:49:51 +11002918{
NeilBrowne8096362011-10-11 16:49:05 +11002919 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11002920 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11002921 struct md_rdev *rdev;
majianpeng5220ea12012-04-02 09:48:38 +10002922 int ret;
Shaohua Li2ff8cc22012-10-11 13:28:54 +11002923 bool discard_supported = false;
NeilBrown709ae482009-12-14 12:49:51 +11002924
2925 if (mddev->level != 1) {
NeilBrown1d41c212016-11-02 14:16:50 +11002926 pr_warn("md/raid1:%s: raid level not set to mirroring (%d)\n",
2927 mdname(mddev), mddev->level);
NeilBrown709ae482009-12-14 12:49:51 +11002928 return -EIO;
2929 }
2930 if (mddev->reshape_position != MaxSector) {
NeilBrown1d41c212016-11-02 14:16:50 +11002931 pr_warn("md/raid1:%s: reshape_position set but not supported\n",
2932 mdname(mddev));
NeilBrown709ae482009-12-14 12:49:51 +11002933 return -EIO;
2934 }
2935 /*
2936 * copy the already verified devices into our private RAID1
2937 * bookkeeping area. [whatever we allocate in run(),
NeilBrownafa0f552014-12-15 12:56:58 +11002938 * should be freed in raid1_free()]
NeilBrown709ae482009-12-14 12:49:51 +11002939 */
2940 if (mddev->private == NULL)
2941 conf = setup_conf(mddev);
2942 else
2943 conf = mddev->private;
2944
2945 if (IS_ERR(conf))
2946 return PTR_ERR(conf);
2947
Joe Lawrencec8dc9c62013-02-21 13:28:09 +11002948 if (mddev->queue)
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07002949 blk_queue_max_write_same_sectors(mddev->queue, 0);
2950
NeilBrowndafb20f2012-03-19 12:46:39 +11002951 rdev_for_each(rdev, mddev) {
Jonathan Brassow1ed72422011-06-07 17:50:35 -05002952 if (!mddev->gendisk)
2953 continue;
NeilBrown709ae482009-12-14 12:49:51 +11002954 disk_stack_limits(mddev->gendisk, rdev->bdev,
2955 rdev->data_offset << 9);
Shaohua Li2ff8cc22012-10-11 13:28:54 +11002956 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
2957 discard_supported = true;
NeilBrown709ae482009-12-14 12:49:51 +11002958 }
2959
2960 mddev->degraded = 0;
2961 for (i=0; i < conf->raid_disks; i++)
2962 if (conf->mirrors[i].rdev == NULL ||
2963 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
2964 test_bit(Faulty, &conf->mirrors[i].rdev->flags))
2965 mddev->degraded++;
2966
2967 if (conf->raid_disks - mddev->degraded == 1)
2968 mddev->recovery_cp = MaxSector;
2969
Andre Noll8c6ac862009-06-18 08:48:06 +10002970 if (mddev->recovery_cp != MaxSector)
NeilBrown1d41c212016-11-02 14:16:50 +11002971 pr_info("md/raid1:%s: not clean -- starting background reconstruction\n",
2972 mdname(mddev));
2973 pr_info("md/raid1:%s: active with %d out of %d mirrors\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10002974 mdname(mddev), mddev->raid_disks - mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 mddev->raid_disks);
NeilBrown709ae482009-12-14 12:49:51 +11002976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 /*
2978 * Ok, everything is just fine now
2979 */
NeilBrown709ae482009-12-14 12:49:51 +11002980 mddev->thread = conf->thread;
2981 conf->thread = NULL;
2982 mddev->private = conf;
2983
Dan Williams1f403622009-03-31 14:59:03 +11002984 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Jonathan Brassow1ed72422011-06-07 17:50:35 -05002986 if (mddev->queue) {
Shaohua Li2ff8cc22012-10-11 13:28:54 +11002987 if (discard_supported)
2988 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
2989 mddev->queue);
2990 else
2991 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
2992 mddev->queue);
Jonathan Brassow1ed72422011-06-07 17:50:35 -05002993 }
majianpeng5220ea12012-04-02 09:48:38 +10002994
2995 ret = md_integrity_register(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11002996 if (ret) {
2997 md_unregister_thread(&mddev->thread);
NeilBrownafa0f552014-12-15 12:56:58 +11002998 raid1_free(mddev, conf);
NeilBrown5aa61f42014-12-15 12:56:57 +11002999 }
majianpeng5220ea12012-04-02 09:48:38 +10003000 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
3002
NeilBrownafa0f552014-12-15 12:56:58 +11003003static void raid1_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
NeilBrownafa0f552014-12-15 12:56:58 +11003005 struct r1conf *conf = priv;
NeilBrown4b6d2872005-09-09 16:23:47 -07003006
Julia Lawall644df1a2015-09-13 14:15:10 +02003007 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003008 kfree(conf->mirrors);
Hirokazu Takahashi0fea7ed2013-04-24 11:42:44 +10003009 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003010 kfree(conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012}
3013
NeilBrownfd01b882011-10-11 16:47:53 +11003014static int raid1_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
3016 /* no resync is happening, and there is enough space
3017 * on all devices, so we can resize.
3018 * We need to make sure resync covers any new space.
3019 * If the array is shrinking we should possibly wait until
3020 * any io in the removed space completes, but it hardly seems
3021 * worth it.
3022 */
NeilBrowna4a61252012-05-22 13:55:27 +10003023 sector_t newsize = raid1_size(mddev, sectors, 0);
3024 if (mddev->external_size &&
3025 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11003026 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10003027 if (mddev->bitmap) {
3028 int ret = bitmap_resize(mddev->bitmap, newsize, 0, 0);
3029 if (ret)
3030 return ret;
3031 }
3032 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10003033 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003034 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003035 if (sectors > mddev->dev_sectors &&
NeilBrownb0986362011-05-11 15:52:21 +10003036 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003037 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3039 }
Dan Williamsb522adc2009-03-31 15:00:31 +11003040 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07003041 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 return 0;
3043}
3044
NeilBrownfd01b882011-10-11 16:47:53 +11003045static int raid1_reshape(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
3047 /* We need to:
3048 * 1/ resize the r1bio_pool
3049 * 2/ resize conf->mirrors
3050 *
3051 * We allocate a new r1bio_pool if we can.
3052 * Then raise a device barrier and wait until all IO stops.
3053 * Then resize conf->mirrors and swap in the new r1bio pool.
NeilBrown6ea9c072005-06-21 17:17:09 -07003054 *
3055 * At the same time, we "pack" the devices so that all the missing
3056 * devices have the higher raid_disk numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 */
3058 mempool_t *newpool, *oldpool;
3059 struct pool_info *newpoolinfo;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10003060 struct raid1_info *newmirrors;
NeilBrowne8096362011-10-11 16:49:05 +11003061 struct r1conf *conf = mddev->private;
NeilBrown63c70c42006-03-27 01:18:13 -08003062 int cnt, raid_disks;
NeilBrownc04be0a2006-10-03 01:15:53 -07003063 unsigned long flags;
Dan Williamsb5470dc2008-06-27 21:44:04 -07003064 int d, d2, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
NeilBrown63c70c42006-03-27 01:18:13 -08003066 /* Cannot change chunk_size, layout, or level */
Andre Noll664e7c42009-06-18 08:45:27 +10003067 if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
NeilBrown63c70c42006-03-27 01:18:13 -08003068 mddev->layout != mddev->new_layout ||
3069 mddev->level != mddev->new_level) {
Andre Noll664e7c42009-06-18 08:45:27 +10003070 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown63c70c42006-03-27 01:18:13 -08003071 mddev->new_layout = mddev->layout;
3072 mddev->new_level = mddev->level;
3073 return -EINVAL;
3074 }
3075
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003076 if (!mddev_is_clustered(mddev)) {
3077 err = md_allow_write(mddev);
3078 if (err)
3079 return err;
3080 }
NeilBrown2a2275d2007-01-26 00:57:11 -08003081
NeilBrown63c70c42006-03-27 01:18:13 -08003082 raid_disks = mddev->raid_disks + mddev->delta_disks;
3083
NeilBrown6ea9c072005-06-21 17:17:09 -07003084 if (raid_disks < conf->raid_disks) {
3085 cnt=0;
3086 for (d= 0; d < conf->raid_disks; d++)
3087 if (conf->mirrors[d].rdev)
3088 cnt++;
3089 if (cnt > raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 return -EBUSY;
NeilBrown6ea9c072005-06-21 17:17:09 -07003091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
3093 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
3094 if (!newpoolinfo)
3095 return -ENOMEM;
3096 newpoolinfo->mddev = mddev;
NeilBrown8f19ccb2011-12-23 10:17:56 +11003097 newpoolinfo->raid_disks = raid_disks * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
3099 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
3100 r1bio_pool_free, newpoolinfo);
3101 if (!newpool) {
3102 kfree(newpoolinfo);
3103 return -ENOMEM;
3104 }
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10003105 newmirrors = kzalloc(sizeof(struct raid1_info) * raid_disks * 2,
NeilBrown8f19ccb2011-12-23 10:17:56 +11003106 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 if (!newmirrors) {
3108 kfree(newpoolinfo);
3109 mempool_destroy(newpool);
3110 return -ENOMEM;
3111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
NeilBrowne2d59922013-06-12 11:01:22 +10003113 freeze_array(conf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
3115 /* ok, everything is stopped */
3116 oldpool = conf->r1bio_pool;
3117 conf->r1bio_pool = newpool;
NeilBrown6ea9c072005-06-21 17:17:09 -07003118
NeilBrowna88aa782007-08-22 14:01:53 -07003119 for (d = d2 = 0; d < conf->raid_disks; d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11003120 struct md_rdev *rdev = conf->mirrors[d].rdev;
NeilBrowna88aa782007-08-22 14:01:53 -07003121 if (rdev && rdev->raid_disk != d2) {
Namhyung Kim36fad852011-07-27 11:00:36 +10003122 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna88aa782007-08-22 14:01:53 -07003123 rdev->raid_disk = d2;
Namhyung Kim36fad852011-07-27 11:00:36 +10003124 sysfs_unlink_rdev(mddev, rdev);
3125 if (sysfs_link_rdev(mddev, rdev))
NeilBrown1d41c212016-11-02 14:16:50 +11003126 pr_warn("md/raid1:%s: cannot register rd%d\n",
3127 mdname(mddev), rdev->raid_disk);
NeilBrown6ea9c072005-06-21 17:17:09 -07003128 }
NeilBrowna88aa782007-08-22 14:01:53 -07003129 if (rdev)
3130 newmirrors[d2++].rdev = rdev;
3131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 kfree(conf->mirrors);
3133 conf->mirrors = newmirrors;
3134 kfree(conf->poolinfo);
3135 conf->poolinfo = newpoolinfo;
3136
NeilBrownc04be0a2006-10-03 01:15:53 -07003137 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 mddev->degraded += (raid_disks - conf->raid_disks);
NeilBrownc04be0a2006-10-03 01:15:53 -07003139 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 conf->raid_disks = mddev->raid_disks = raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08003141 mddev->delta_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
NeilBrowne2d59922013-06-12 11:01:22 +10003143 unfreeze_array(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
NeilBrown985ca972015-07-06 12:26:57 +10003145 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3147 md_wakeup_thread(mddev->thread);
3148
3149 mempool_destroy(oldpool);
3150 return 0;
3151}
3152
NeilBrownfd01b882011-10-11 16:47:53 +11003153static void raid1_quiesce(struct mddev *mddev, int state)
NeilBrown36fa3062005-09-09 16:23:45 -07003154{
NeilBrowne8096362011-10-11 16:49:05 +11003155 struct r1conf *conf = mddev->private;
NeilBrown36fa3062005-09-09 16:23:45 -07003156
3157 switch(state) {
NeilBrown6eef4b22009-12-14 12:49:51 +11003158 case 2: /* wake for suspend */
3159 wake_up(&conf->wait_barrier);
3160 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07003161 case 1:
majianpeng07169fd2013-11-14 15:16:18 +11003162 freeze_array(conf, 0);
NeilBrown36fa3062005-09-09 16:23:45 -07003163 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07003164 case 0:
majianpeng07169fd2013-11-14 15:16:18 +11003165 unfreeze_array(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07003166 break;
3167 }
NeilBrown36fa3062005-09-09 16:23:45 -07003168}
3169
NeilBrownfd01b882011-10-11 16:47:53 +11003170static void *raid1_takeover(struct mddev *mddev)
NeilBrown709ae482009-12-14 12:49:51 +11003171{
3172 /* raid1 can take over:
3173 * raid5 with 2 devices, any layout or chunk size
3174 */
3175 if (mddev->level == 5 && mddev->raid_disks == 2) {
NeilBrowne8096362011-10-11 16:49:05 +11003176 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11003177 mddev->new_level = 1;
3178 mddev->new_layout = 0;
3179 mddev->new_chunk_sectors = 0;
3180 conf = setup_conf(mddev);
3181 if (!IS_ERR(conf))
majianpeng07169fd2013-11-14 15:16:18 +11003182 /* Array must appear to be quiesced */
3183 conf->array_frozen = 1;
NeilBrown709ae482009-12-14 12:49:51 +11003184 return conf;
3185 }
3186 return ERR_PTR(-EINVAL);
3187}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
NeilBrown84fc4b52011-10-11 16:49:58 +11003189static struct md_personality raid1_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
3191 .name = "raid1",
NeilBrown2604b702006-01-06 00:20:36 -08003192 .level = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08003194 .make_request = raid1_make_request,
3195 .run = raid1_run,
NeilBrownafa0f552014-12-15 12:56:58 +11003196 .free = raid1_free,
Shaohua Li849674e2016-01-20 13:52:20 -08003197 .status = raid1_status,
3198 .error_handler = raid1_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 .hot_add_disk = raid1_add_disk,
3200 .hot_remove_disk= raid1_remove_disk,
3201 .spare_active = raid1_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08003202 .sync_request = raid1_sync_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 .resize = raid1_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07003204 .size = raid1_size,
NeilBrown63c70c42006-03-27 01:18:13 -08003205 .check_reshape = raid1_reshape,
NeilBrown36fa3062005-09-09 16:23:45 -07003206 .quiesce = raid1_quiesce,
NeilBrown709ae482009-12-14 12:49:51 +11003207 .takeover = raid1_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11003208 .congested = raid1_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209};
3210
3211static int __init raid_init(void)
3212{
NeilBrown2604b702006-01-06 00:20:36 -08003213 return register_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214}
3215
3216static void raid_exit(void)
3217{
NeilBrown2604b702006-01-06 00:20:36 -08003218 unregister_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219}
3220
3221module_init(raid_init);
3222module_exit(raid_exit);
3223MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11003224MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225MODULE_ALIAS("md-personality-3"); /* RAID1 */
NeilBrownd9d166c2006-01-06 00:20:51 -08003226MODULE_ALIAS("md-raid1");
NeilBrown2604b702006-01-06 00:20:36 -08003227MODULE_ALIAS("md-level-1");
NeilBrown34db0cd2011-10-11 16:50:01 +11003228
3229module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);