blob: cd810e19508619db468fc777028e9620a6da055c [file] [log] [blame]
Thomas Gleixneraf1a8892019-05-20 19:08:12 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * raid1.c : Multiple Devices driver for Linux
4 *
5 * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
6 *
7 * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
8 *
9 * RAID-1 management functions.
10 *
11 * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
12 *
Jan Engelhardt96de0e22007-10-19 23:21:04 +020013 * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
15 *
NeilBrown191ea9b2005-06-21 17:17:23 -070016 * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support
17 * bitmapped intelligence in resync:
18 *
19 * - bitmap marked during normal i/o
20 * - bitmap used to skip nondirty blocks during sync
21 *
22 * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
23 * - persistent bitmap code
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110027#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110028#include <linux/blkdev.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040029#include <linux/module.h>
NeilBrownbff61972009-03-31 14:33:13 +110030#include <linux/seq_file.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100031#include <linux/ratelimit.h>
Guoqing Jiang69b00b52019-12-23 10:49:00 +010032#include <linux/interval_tree_generic.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010033
NeilBrown109e3762016-11-18 13:22:04 +110034#include <trace/events/block.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010035
NeilBrown43b2e5d2009-03-31 14:33:13 +110036#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110037#include "raid1.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040038#include "md-bitmap.h"
NeilBrown191ea9b2005-06-21 17:17:23 -070039
Shaohua Li394ed8e2017-01-04 16:10:19 -080040#define UNSUPPORTED_MDDEV_FLAGS \
41 ((1L << MD_HAS_JOURNAL) | \
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +010042 (1L << MD_JOURNAL_CLEAN) | \
Pawel Baldysiakddc08822017-08-16 17:13:45 +020043 (1L << MD_HAS_PPL) | \
44 (1L << MD_HAS_MULTIPLE_PPLS))
Shaohua Li394ed8e2017-01-04 16:10:19 -080045
colyli@suse.defd768632017-02-18 03:05:56 +080046static void allow_barrier(struct r1conf *conf, sector_t sector_nr);
47static void lower_barrier(struct r1conf *conf, sector_t sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
NeilBrown578b54a2016-11-14 16:30:21 +110049#define raid1_log(md, fmt, args...) \
50 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid1 " fmt, ##args); } while (0)
51
Ming Leifb0eb5d2017-07-14 16:14:43 +080052#include "raid1-10.c"
53
Guoqing Jiang69b00b52019-12-23 10:49:00 +010054#define START(node) ((node)->start)
55#define LAST(node) ((node)->last)
56INTERVAL_TREE_DEFINE(struct serial_info, node, sector_t, _subtree_last,
57 START, LAST, static inline, raid1_rb);
58
Guoqing Jiangd0d2d8b2019-12-23 10:49:02 +010059static int check_and_add_serial(struct md_rdev *rdev, struct r1bio *r1_bio,
60 struct serial_info *si, int idx)
Guoqing Jiang3e148a32019-06-19 17:30:46 +080061{
Guoqing Jiang3e148a32019-06-19 17:30:46 +080062 unsigned long flags;
63 int ret = 0;
Guoqing Jiangd0d2d8b2019-12-23 10:49:02 +010064 sector_t lo = r1_bio->sector;
65 sector_t hi = lo + r1_bio->sectors;
Guoqing Jiang025471f2019-12-23 10:49:01 +010066 struct serial_in_rdev *serial = &rdev->serial[idx];
Guoqing Jiang3e148a32019-06-19 17:30:46 +080067
Guoqing Jiang69b00b52019-12-23 10:49:00 +010068 spin_lock_irqsave(&serial->serial_lock, flags);
69 /* collision happened */
70 if (raid1_rb_iter_first(&serial->serial_rb, lo, hi))
71 ret = -EBUSY;
Guoqing Jiangd0d2d8b2019-12-23 10:49:02 +010072 else {
Guoqing Jiang69b00b52019-12-23 10:49:00 +010073 si->start = lo;
74 si->last = hi;
75 raid1_rb_insert(si, &serial->serial_rb);
Guoqing Jiangd0d2d8b2019-12-23 10:49:02 +010076 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +010077 spin_unlock_irqrestore(&serial->serial_lock, flags);
Guoqing Jiang3e148a32019-06-19 17:30:46 +080078
79 return ret;
80}
81
Guoqing Jiangd0d2d8b2019-12-23 10:49:02 +010082static void wait_for_serialization(struct md_rdev *rdev, struct r1bio *r1_bio)
83{
84 struct mddev *mddev = rdev->mddev;
85 struct serial_info *si;
86 int idx = sector_to_idx(r1_bio->sector);
87 struct serial_in_rdev *serial = &rdev->serial[idx];
88
89 if (WARN_ON(!mddev->serial_info_pool))
90 return;
91 si = mempool_alloc(mddev->serial_info_pool, GFP_NOIO);
92 wait_event(serial->serial_io_wait,
93 check_and_add_serial(rdev, r1_bio, si, idx) == 0);
94}
95
Guoqing Jiang404659c2019-12-23 10:48:53 +010096static void remove_serial(struct md_rdev *rdev, sector_t lo, sector_t hi)
Guoqing Jiang3e148a32019-06-19 17:30:46 +080097{
Guoqing Jiang69b00b52019-12-23 10:49:00 +010098 struct serial_info *si;
Guoqing Jiang3e148a32019-06-19 17:30:46 +080099 unsigned long flags;
100 int found = 0;
101 struct mddev *mddev = rdev->mddev;
Guoqing Jiang025471f2019-12-23 10:49:01 +0100102 int idx = sector_to_idx(lo);
103 struct serial_in_rdev *serial = &rdev->serial[idx];
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800104
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100105 spin_lock_irqsave(&serial->serial_lock, flags);
106 for (si = raid1_rb_iter_first(&serial->serial_rb, lo, hi);
107 si; si = raid1_rb_iter_next(si, lo, hi)) {
108 if (si->start == lo && si->last == hi) {
109 raid1_rb_remove(si, &serial->serial_rb);
110 mempool_free(si, mddev->serial_info_pool);
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800111 found = 1;
112 break;
113 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100114 }
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800115 if (!found)
Guoqing Jiang404659c2019-12-23 10:48:53 +0100116 WARN(1, "The write IO is not recorded for serialization\n");
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100117 spin_unlock_irqrestore(&serial->serial_lock, flags);
118 wake_up(&serial->serial_io_wait);
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800119}
120
Ming Lei98d30c52017-03-17 00:12:26 +0800121/*
Ming Lei98d30c52017-03-17 00:12:26 +0800122 * for resync bio, r1bio pointer can be retrieved from the per-bio
123 * 'struct resync_pages'.
124 */
125static inline struct r1bio *get_resync_r1bio(struct bio *bio)
126{
127 return get_resync_pages(bio)->raid_bio;
128}
129
Al Virodd0fc662005-10-07 07:46:04 +0100130static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct pool_info *pi = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100133 int size = offsetof(struct r1bio, bios[pi->raid_disks]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 /* allocate a r1bio with room for raid_disks entries in the bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +0100136 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
majianpeng8e005f72013-11-14 15:16:18 +1100139#define RESYNC_DEPTH 32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
majianpeng8e005f72013-11-14 15:16:18 +1100141#define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH)
142#define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9)
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +1000143#define CLUSTER_RESYNC_WINDOW (16 * RESYNC_WINDOW)
144#define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Al Virodd0fc662005-10-07 07:46:04 +0100146static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 struct pool_info *pi = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100149 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct bio *bio;
NeilBrownda1aab32014-04-09 12:25:43 +1000151 int need_pages;
Ming Lei98d30c52017-03-17 00:12:26 +0800152 int j;
153 struct resync_pages *rps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100156 if (!r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Kees Cook6da2ec52018-06-12 13:55:00 -0700159 rps = kmalloc_array(pi->raid_disks, sizeof(struct resync_pages),
160 gfp_flags);
Ming Lei98d30c52017-03-17 00:12:26 +0800161 if (!rps)
162 goto out_free_r1bio;
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /*
165 * Allocate bios : 1 for reading, n-1 for writing
166 */
167 for (j = pi->raid_disks ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100168 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (!bio)
170 goto out_free_bio;
171 r1_bio->bios[j] = bio;
172 }
173 /*
174 * Allocate RESYNC_PAGES data pages and attach them to
NeilBrownd11c1712006-01-06 00:20:26 -0800175 * the first bio.
176 * If this is a user-requested check/repair, allocate
177 * RESYNC_PAGES for each bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 */
NeilBrownd11c1712006-01-06 00:20:26 -0800179 if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
NeilBrownda1aab32014-04-09 12:25:43 +1000180 need_pages = pi->raid_disks;
NeilBrownd11c1712006-01-06 00:20:26 -0800181 else
NeilBrownda1aab32014-04-09 12:25:43 +1000182 need_pages = 1;
Ming Lei98d30c52017-03-17 00:12:26 +0800183 for (j = 0; j < pi->raid_disks; j++) {
184 struct resync_pages *rp = &rps[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Ming Lei98d30c52017-03-17 00:12:26 +0800186 bio = r1_bio->bios[j];
187
188 if (j < need_pages) {
189 if (resync_alloc_pages(rp, gfp_flags))
190 goto out_free_pages;
191 } else {
192 memcpy(rp, &rps[0], sizeof(*rp));
193 resync_get_all_pages(rp);
194 }
195
Ming Lei98d30c52017-03-17 00:12:26 +0800196 rp->raid_bio = r1_bio;
197 bio->bi_private = rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199
200 r1_bio->master_bio = NULL;
201
202 return r1_bio;
203
NeilBrownda1aab32014-04-09 12:25:43 +1000204out_free_pages:
Guoqing Jiang491221f2016-09-22 03:10:01 -0400205 while (--j >= 0)
Ming Lei98d30c52017-03-17 00:12:26 +0800206 resync_free_pages(&rps[j]);
NeilBrownda1aab32014-04-09 12:25:43 +1000207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208out_free_bio:
NeilBrown8f19ccb2011-12-23 10:17:56 +1100209 while (++j < pi->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 bio_put(r1_bio->bios[j]);
Ming Lei98d30c52017-03-17 00:12:26 +0800211 kfree(rps);
212
213out_free_r1bio:
Marcos Paulo de Souzac7afa802019-06-14 15:41:10 -0700214 rbio_pool_free(r1_bio, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return NULL;
216}
217
218static void r1buf_pool_free(void *__r1_bio, void *data)
219{
220 struct pool_info *pi = data;
Ming Lei98d30c52017-03-17 00:12:26 +0800221 int i;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100222 struct r1bio *r1bio = __r1_bio;
Ming Lei98d30c52017-03-17 00:12:26 +0800223 struct resync_pages *rp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Ming Lei98d30c52017-03-17 00:12:26 +0800225 for (i = pi->raid_disks; i--; ) {
226 rp = get_resync_pages(r1bio->bios[i]);
227 resync_free_pages(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 bio_put(r1bio->bios[i]);
Ming Lei98d30c52017-03-17 00:12:26 +0800229 }
230
231 /* resync pages array stored in the 1st bio's .bi_private */
232 kfree(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Marcos Paulo de Souzac7afa802019-06-14 15:41:10 -0700234 rbio_pool_free(r1bio, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
NeilBrowne8096362011-10-11 16:49:05 +1100237static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 int i;
240
NeilBrown8f19ccb2011-12-23 10:17:56 +1100241 for (i = 0; i < conf->raid_disks * 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 struct bio **bio = r1_bio->bios + i;
NeilBrown4367af52011-07-28 11:31:49 +1000243 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 bio_put(*bio);
245 *bio = NULL;
246 }
247}
248
NeilBrown9f2c9d12011-10-11 16:48:43 +1100249static void free_r1bio(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
NeilBrowne8096362011-10-11 16:49:05 +1100251 struct r1conf *conf = r1_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 put_all_bios(conf, r1_bio);
Kent Overstreetafeee512018-05-20 18:25:52 -0400254 mempool_free(r1_bio, &conf->r1bio_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
NeilBrown9f2c9d12011-10-11 16:48:43 +1100257static void put_buf(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
NeilBrowne8096362011-10-11 16:49:05 +1100259 struct r1conf *conf = r1_bio->mddev->private;
Shaohua Liaf5f42a2017-02-19 22:41:27 -0800260 sector_t sect = r1_bio->sector;
NeilBrown3e198f72006-01-06 00:20:21 -0800261 int i;
262
NeilBrown8f19ccb2011-12-23 10:17:56 +1100263 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown3e198f72006-01-06 00:20:21 -0800264 struct bio *bio = r1_bio->bios[i];
265 if (bio->bi_end_io)
266 rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Kent Overstreetafeee512018-05-20 18:25:52 -0400269 mempool_free(r1_bio, &conf->r1buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Shaohua Liaf5f42a2017-02-19 22:41:27 -0800271 lower_barrier(conf, sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
NeilBrown9f2c9d12011-10-11 16:48:43 +1100274static void reschedule_retry(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100277 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +1100278 struct r1conf *conf = mddev->private;
colyli@suse.defd768632017-02-18 03:05:56 +0800279 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
colyli@suse.defd768632017-02-18 03:05:56 +0800281 idx = sector_to_idx(r1_bio->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 spin_lock_irqsave(&conf->device_lock, flags);
283 list_add(&r1_bio->retry_list, &conf->retry_list);
colyli@suse.de824e47d2017-02-18 03:05:57 +0800284 atomic_inc(&conf->nr_queued[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 spin_unlock_irqrestore(&conf->device_lock, flags);
286
NeilBrown17999be2006-01-06 00:20:12 -0800287 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 md_wakeup_thread(mddev->thread);
289}
290
291/*
292 * raid_end_bio_io() is called when we have finished servicing a mirrored
293 * operation and are ready to return a success/failure code to the buffer
294 * cache layer.
295 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100296static void call_bio_endio(struct r1bio *r1_bio)
NeilBrownd2eb35a2011-07-28 11:31:48 +1000297{
298 struct bio *bio = r1_bio->master_bio;
NeilBrowne8096362011-10-11 16:49:05 +1100299 struct r1conf *conf = r1_bio->mddev->private;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000300
301 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200302 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200303
NeilBrown37011e32017-03-15 14:05:14 +1100304 bio_endio(bio);
305 /*
306 * Wake up any possible resync thread that waits for the device
307 * to go idle.
308 */
309 allow_barrier(conf, r1_bio->sector);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000310}
311
NeilBrown9f2c9d12011-10-11 16:48:43 +1100312static void raid_end_bio_io(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 struct bio *bio = r1_bio->master_bio;
315
NeilBrown4b6d2872005-09-09 16:23:47 -0700316 /* if nobody has done the final endio yet, do it now */
317 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
NeilBrown36a4e1f2011-10-07 14:23:17 +1100318 pr_debug("raid1: sync end %s on sectors %llu-%llu\n",
319 (bio_data_dir(bio) == WRITE) ? "write" : "read",
Kent Overstreet4f024f32013-10-11 15:44:27 -0700320 (unsigned long long) bio->bi_iter.bi_sector,
321 (unsigned long long) bio_end_sector(bio) - 1);
NeilBrown4b6d2872005-09-09 16:23:47 -0700322
NeilBrownd2eb35a2011-07-28 11:31:48 +1000323 call_bio_endio(r1_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -0700324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 free_r1bio(r1_bio);
326}
327
328/*
329 * Update disk head position estimator based on IRQ completion info.
330 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100331static inline void update_head_pos(int disk, struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
NeilBrowne8096362011-10-11 16:49:05 +1100333 struct r1conf *conf = r1_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 conf->mirrors[disk].head_position =
336 r1_bio->sector + (r1_bio->sectors);
337}
338
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100339/*
340 * Find the disk number which triggered given bio
341 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100342static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio)
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100343{
344 int mirror;
NeilBrown30194632011-12-23 10:17:56 +1100345 struct r1conf *conf = r1_bio->mddev->private;
346 int raid_disks = conf->raid_disks;
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100347
NeilBrown8f19ccb2011-12-23 10:17:56 +1100348 for (mirror = 0; mirror < raid_disks * 2; mirror++)
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100349 if (r1_bio->bios[mirror] == bio)
350 break;
351
NeilBrown8f19ccb2011-12-23 10:17:56 +1100352 BUG_ON(mirror == raid_disks * 2);
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100353 update_head_pos(mirror, r1_bio);
354
355 return mirror;
356}
357
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200358static void raid1_end_read_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200360 int uptodate = !bio->bi_status;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100361 struct r1bio *r1_bio = bio->bi_private;
NeilBrowne8096362011-10-11 16:49:05 +1100362 struct r1conf *conf = r1_bio->mddev->private;
NeilBrowne5872d52016-06-02 16:19:52 +1000363 struct md_rdev *rdev = conf->mirrors[r1_bio->read_disk].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /*
366 * this branch is our 'one mirror IO has finished' event handler:
367 */
NeilBrowne5872d52016-06-02 16:19:52 +1000368 update_head_pos(r1_bio->read_disk, r1_bio);
NeilBrownddaf22a2006-01-06 00:20:19 -0800369
NeilBrowndd00a992007-05-10 03:15:50 -0700370 if (uptodate)
371 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrown2e52d442016-11-18 16:16:12 +1100372 else if (test_bit(FailFast, &rdev->flags) &&
373 test_bit(R1BIO_FailFast, &r1_bio->state))
374 /* This was a fail-fast read so we definitely
375 * want to retry */
376 ;
NeilBrowndd00a992007-05-10 03:15:50 -0700377 else {
378 /* If all other devices have failed, we want to return
379 * the error upwards rather than fail the last device.
380 * Here we redefine "uptodate" to mean "Don't want to retry"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
NeilBrowndd00a992007-05-10 03:15:50 -0700382 unsigned long flags;
383 spin_lock_irqsave(&conf->device_lock, flags);
384 if (r1_bio->mddev->degraded == conf->raid_disks ||
385 (r1_bio->mddev->degraded == conf->raid_disks-1 &&
NeilBrowne5872d52016-06-02 16:19:52 +1000386 test_bit(In_sync, &rdev->flags)))
NeilBrowndd00a992007-05-10 03:15:50 -0700387 uptodate = 1;
388 spin_unlock_irqrestore(&conf->device_lock, flags);
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
NeilBrown7ad4d4a2012-10-11 13:44:30 +1100391 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 raid_end_bio_io(r1_bio);
NeilBrowne5872d52016-06-02 16:19:52 +1000393 rdev_dec_pending(rdev, conf->mddev);
NeilBrown7ad4d4a2012-10-11 13:44:30 +1100394 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /*
396 * oops, read error:
397 */
398 char b[BDEVNAME_SIZE];
NeilBrown1d41c212016-11-02 14:16:50 +1100399 pr_err_ratelimited("md/raid1:%s: %s: rescheduling sector %llu\n",
400 mdname(conf->mddev),
401 bdevname(rdev->bdev, b),
402 (unsigned long long)r1_bio->sector);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000403 set_bit(R1BIO_ReadError, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 reschedule_retry(r1_bio);
NeilBrown7ad4d4a2012-10-11 13:44:30 +1100405 /* don't drop the reference on read_disk yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
NeilBrown9f2c9d12011-10-11 16:48:43 +1100409static void close_write(struct r1bio *r1_bio)
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000410{
411 /* it really is the end of this request */
412 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
Ming Lei841c1312017-03-17 00:12:31 +0800413 bio_free_pages(r1_bio->behind_master_bio);
414 bio_put(r1_bio->behind_master_bio);
415 r1_bio->behind_master_bio = NULL;
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000416 }
417 /* clear the bitmap if all writes complete successfully */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700418 md_bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
419 r1_bio->sectors,
420 !test_bit(R1BIO_Degraded, &r1_bio->state),
421 test_bit(R1BIO_BehindIO, &r1_bio->state));
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000422 md_write_end(r1_bio->mddev);
423}
424
NeilBrown9f2c9d12011-10-11 16:48:43 +1100425static void r1_bio_write_done(struct r1bio *r1_bio)
NeilBrown4e780642010-10-19 12:54:01 +1100426{
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000427 if (!atomic_dec_and_test(&r1_bio->remaining))
428 return;
429
430 if (test_bit(R1BIO_WriteError, &r1_bio->state))
431 reschedule_retry(r1_bio);
432 else {
433 close_write(r1_bio);
NeilBrown4367af52011-07-28 11:31:49 +1000434 if (test_bit(R1BIO_MadeGood, &r1_bio->state))
435 reschedule_retry(r1_bio);
436 else
437 raid_end_bio_io(r1_bio);
NeilBrown4e780642010-10-19 12:54:01 +1100438 }
439}
440
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200441static void raid1_end_write_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
NeilBrown9f2c9d12011-10-11 16:48:43 +1100443 struct r1bio *r1_bio = bio->bi_private;
NeilBrowne5872d52016-06-02 16:19:52 +1000444 int behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
NeilBrowne8096362011-10-11 16:49:05 +1100445 struct r1conf *conf = r1_bio->mddev->private;
NeilBrown04b857f2006-03-09 17:33:46 -0800446 struct bio *to_put = NULL;
NeilBrowne5872d52016-06-02 16:19:52 +1000447 int mirror = find_bio_disk(r1_bio, bio);
448 struct md_rdev *rdev = conf->mirrors[mirror].rdev;
Shaohua Lie3f948c2016-10-06 14:09:16 -0700449 bool discard_error;
Guoqing Jiang69df9cf2019-12-23 10:48:58 +0100450 sector_t lo = r1_bio->sector;
451 sector_t hi = r1_bio->sector + r1_bio->sectors;
Shaohua Lie3f948c2016-10-06 14:09:16 -0700452
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200453 discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Tejun Heoe9c74692010-09-03 11:56:18 +0200455 /*
456 * 'one mirror IO has finished' event handler:
457 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200458 if (bio->bi_status && !discard_error) {
NeilBrowne5872d52016-06-02 16:19:52 +1000459 set_bit(WriteErrorSeen, &rdev->flags);
460 if (!test_and_set_bit(WantReplacement, &rdev->flags))
NeilBrown19d67162011-12-23 10:17:57 +1100461 set_bit(MD_RECOVERY_NEEDED, &
462 conf->mddev->recovery);
463
NeilBrown212e7eb2016-11-18 16:16:12 +1100464 if (test_bit(FailFast, &rdev->flags) &&
465 (bio->bi_opf & MD_FAILFAST) &&
466 /* We never try FailFast to WriteMostly devices */
467 !test_bit(WriteMostly, &rdev->flags)) {
468 md_error(r1_bio->mddev, rdev);
Yufen Yueeba6802019-07-19 13:48:46 +0800469 }
470
471 /*
472 * When the device is faulty, it is not necessary to
473 * handle write error.
474 * For failfast, this is the only remaining device,
475 * We need to retry the write without FailFast.
476 */
477 if (!test_bit(Faulty, &rdev->flags))
NeilBrown212e7eb2016-11-18 16:16:12 +1100478 set_bit(R1BIO_WriteError, &r1_bio->state);
Yufen Yueeba6802019-07-19 13:48:46 +0800479 else {
480 /* Finished with this branch */
481 r1_bio->bios[mirror] = NULL;
482 to_put = bio;
483 }
NeilBrown4367af52011-07-28 11:31:49 +1000484 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /*
Tejun Heoe9c74692010-09-03 11:56:18 +0200486 * Set R1BIO_Uptodate in our master bio, so that we
487 * will return a good error code for to the higher
488 * levels even if IO on some other mirrored buffer
489 * fails.
490 *
491 * The 'master' represents the composite IO operation
492 * to user-side. So if something waits for IO, then it
493 * will wait for the 'master' bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 */
NeilBrown4367af52011-07-28 11:31:49 +1000495 sector_t first_bad;
496 int bad_sectors;
497
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000498 r1_bio->bios[mirror] = NULL;
499 to_put = bio;
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300500 /*
501 * Do not set R1BIO_Uptodate if the current device is
502 * rebuilding or Faulty. This is because we cannot use
503 * such device for properly reading the data back (we could
504 * potentially use it, if the current write would have felt
505 * before rdev->recovery_offset, but for simplicity we don't
506 * check this here.
507 */
NeilBrowne5872d52016-06-02 16:19:52 +1000508 if (test_bit(In_sync, &rdev->flags) &&
509 !test_bit(Faulty, &rdev->flags))
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300510 set_bit(R1BIO_Uptodate, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
NeilBrown4367af52011-07-28 11:31:49 +1000512 /* Maybe we can clear some bad blocks. */
NeilBrowne5872d52016-06-02 16:19:52 +1000513 if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
Shaohua Lie3f948c2016-10-06 14:09:16 -0700514 &first_bad, &bad_sectors) && !discard_error) {
NeilBrown4367af52011-07-28 11:31:49 +1000515 r1_bio->bios[mirror] = IO_MADE_GOOD;
516 set_bit(R1BIO_MadeGood, &r1_bio->state);
517 }
518 }
519
Tejun Heoe9c74692010-09-03 11:56:18 +0200520 if (behind) {
Guoqing Jiang69df9cf2019-12-23 10:48:58 +0100521 if (test_bit(CollisionCheck, &rdev->flags))
Guoqing Jiang404659c2019-12-23 10:48:53 +0100522 remove_serial(rdev, lo, hi);
NeilBrowne5872d52016-06-02 16:19:52 +1000523 if (test_bit(WriteMostly, &rdev->flags))
Tejun Heoe9c74692010-09-03 11:56:18 +0200524 atomic_dec(&r1_bio->behind_remaining);
NeilBrown4b6d2872005-09-09 16:23:47 -0700525
Tejun Heoe9c74692010-09-03 11:56:18 +0200526 /*
527 * In behind mode, we ACK the master bio once the I/O
528 * has safely reached all non-writemostly
529 * disks. Setting the Returned bit ensures that this
530 * gets done only once -- we don't ever want to return
531 * -EIO here, instead we'll wait
532 */
533 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
534 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
535 /* Maybe we can return now */
536 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
537 struct bio *mbio = r1_bio->master_bio;
NeilBrown36a4e1f2011-10-07 14:23:17 +1100538 pr_debug("raid1: behind end write sectors"
539 " %llu-%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -0700540 (unsigned long long) mbio->bi_iter.bi_sector,
541 (unsigned long long) bio_end_sector(mbio) - 1);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000542 call_bio_endio(r1_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -0700543 }
544 }
Guoqing Jiang69df9cf2019-12-23 10:48:58 +0100545 } else if (rdev->mddev->serialize_policy)
546 remove_serial(rdev, lo, hi);
NeilBrown4367af52011-07-28 11:31:49 +1000547 if (r1_bio->bios[mirror] == NULL)
NeilBrowne5872d52016-06-02 16:19:52 +1000548 rdev_dec_pending(rdev, conf->mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 * Let's see if all mirrored write operations have finished
552 * already.
553 */
NeilBrownaf6d7b72011-05-11 14:51:19 +1000554 r1_bio_write_done(r1_bio);
NeilBrownc70810b2006-06-26 00:27:35 -0700555
NeilBrown04b857f2006-03-09 17:33:46 -0800556 if (to_put)
557 bio_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
colyli@suse.defd768632017-02-18 03:05:56 +0800560static sector_t align_to_barrier_unit_end(sector_t start_sector,
561 sector_t sectors)
562{
563 sector_t len;
564
565 WARN_ON(sectors == 0);
566 /*
567 * len is the number of sectors from start_sector to end of the
568 * barrier unit which start_sector belongs to.
569 */
570 len = round_up(start_sector + 1, BARRIER_UNIT_SECTOR_SIZE) -
571 start_sector;
572
573 if (len > sectors)
574 len = sectors;
575
576 return len;
577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/*
580 * This routine returns the disk from which the requested read should
581 * be done. There is a per-array 'next expected sequential IO' sector
582 * number - if this matches on the next IO then we use the last disk.
583 * There is also a per-disk 'last know head position' sector that is
584 * maintained from IRQ contexts, both the normal and the resync IO
585 * completion handlers update this position correctly. If there is no
586 * perfect sequential match then we pick the disk whose head is closest.
587 *
588 * If there are 2 mirrors in the same 2 devices, performance degrades
589 * because position is mirror, not device based.
590 *
591 * The rdev for the device selected will have nr_pending incremented.
592 */
NeilBrowne8096362011-10-11 16:49:05 +1100593static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000595 const sector_t this_sector = r1_bio->sector;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000596 int sectors;
597 int best_good_sectors;
Shaohua Li9dedf602012-07-31 10:03:53 +1000598 int best_disk, best_dist_disk, best_pending_disk;
599 int has_nonrot_disk;
Shaohua Libe4d3282012-07-31 10:03:53 +1000600 int disk;
NeilBrown76073052011-05-11 14:34:56 +1000601 sector_t best_dist;
Shaohua Li9dedf602012-07-31 10:03:53 +1000602 unsigned int min_pending;
NeilBrown3cb03002011-10-11 16:45:26 +1100603 struct md_rdev *rdev;
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000604 int choose_first;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000605 int choose_next_idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 rcu_read_lock();
608 /*
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700609 * Check if we can balance. We can balance on the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 * device if no resync is going on, or below the resync window.
611 * We take the first readable disk when above the resync window.
612 */
613 retry:
NeilBrownd2eb35a2011-07-28 11:31:48 +1000614 sectors = r1_bio->sectors;
NeilBrown76073052011-05-11 14:34:56 +1000615 best_disk = -1;
Shaohua Li9dedf602012-07-31 10:03:53 +1000616 best_dist_disk = -1;
NeilBrown76073052011-05-11 14:34:56 +1000617 best_dist = MaxSector;
Shaohua Li9dedf602012-07-31 10:03:53 +1000618 best_pending_disk = -1;
619 min_pending = UINT_MAX;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000620 best_good_sectors = 0;
Shaohua Li9dedf602012-07-31 10:03:53 +1000621 has_nonrot_disk = 0;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000622 choose_next_idle = 0;
NeilBrown2e52d442016-11-18 16:16:12 +1100623 clear_bit(R1BIO_FailFast, &r1_bio->state);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000624
Goldwyn Rodrigues7d49ffc2014-08-12 10:13:19 -0500625 if ((conf->mddev->recovery_cp < this_sector + sectors) ||
626 (mddev_is_clustered(conf->mddev) &&
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500627 md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
Goldwyn Rodrigues7d49ffc2014-08-12 10:13:19 -0500628 this_sector + sectors)))
629 choose_first = 1;
630 else
631 choose_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Shaohua Libe4d3282012-07-31 10:03:53 +1000633 for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
NeilBrown76073052011-05-11 14:34:56 +1000634 sector_t dist;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000635 sector_t first_bad;
636 int bad_sectors;
Shaohua Li9dedf602012-07-31 10:03:53 +1000637 unsigned int pending;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000638 bool nonrot;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000639
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000640 rdev = rcu_dereference(conf->mirrors[disk].rdev);
641 if (r1_bio->bios[disk] == IO_BLOCKED
642 || rdev == NULL
NeilBrown76073052011-05-11 14:34:56 +1000643 || test_bit(Faulty, &rdev->flags))
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000644 continue;
NeilBrown76073052011-05-11 14:34:56 +1000645 if (!test_bit(In_sync, &rdev->flags) &&
646 rdev->recovery_offset < this_sector + sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 continue;
NeilBrown76073052011-05-11 14:34:56 +1000648 if (test_bit(WriteMostly, &rdev->flags)) {
649 /* Don't balance among write-mostly, just
650 * use the first as a last resort */
Tomáš Hodekd1901ef2015-02-23 11:00:38 +1100651 if (best_dist_disk < 0) {
NeilBrown307729c2012-01-09 01:41:51 +1100652 if (is_badblock(rdev, this_sector, sectors,
653 &first_bad, &bad_sectors)) {
Wei Fang816b0ac2016-03-21 19:18:32 +0800654 if (first_bad <= this_sector)
NeilBrown307729c2012-01-09 01:41:51 +1100655 /* Cannot use this */
656 continue;
657 best_good_sectors = first_bad - this_sector;
658 } else
659 best_good_sectors = sectors;
Tomáš Hodekd1901ef2015-02-23 11:00:38 +1100660 best_dist_disk = disk;
661 best_pending_disk = disk;
NeilBrown307729c2012-01-09 01:41:51 +1100662 }
NeilBrown76073052011-05-11 14:34:56 +1000663 continue;
664 }
665 /* This is a reasonable device to use. It might
666 * even be best.
667 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000668 if (is_badblock(rdev, this_sector, sectors,
669 &first_bad, &bad_sectors)) {
670 if (best_dist < MaxSector)
671 /* already have a better device */
672 continue;
673 if (first_bad <= this_sector) {
674 /* cannot read here. If this is the 'primary'
675 * device, then we must not read beyond
676 * bad_sectors from another device..
677 */
678 bad_sectors -= (this_sector - first_bad);
679 if (choose_first && sectors > bad_sectors)
680 sectors = bad_sectors;
681 if (best_good_sectors > sectors)
682 best_good_sectors = sectors;
683
684 } else {
685 sector_t good_sectors = first_bad - this_sector;
686 if (good_sectors > best_good_sectors) {
687 best_good_sectors = good_sectors;
688 best_disk = disk;
689 }
690 if (choose_first)
691 break;
692 }
693 continue;
Tomasz Majchrzakd82dd0e2017-05-12 14:26:10 +0200694 } else {
695 if ((sectors > best_good_sectors) && (best_disk >= 0))
696 best_disk = -1;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000697 best_good_sectors = sectors;
Tomasz Majchrzakd82dd0e2017-05-12 14:26:10 +0200698 }
NeilBrownd2eb35a2011-07-28 11:31:48 +1000699
NeilBrown2e52d442016-11-18 16:16:12 +1100700 if (best_disk >= 0)
701 /* At least two disks to choose from so failfast is OK */
702 set_bit(R1BIO_FailFast, &r1_bio->state);
703
Shaohua Li12cee5a2012-07-31 10:03:53 +1000704 nonrot = blk_queue_nonrot(bdev_get_queue(rdev->bdev));
705 has_nonrot_disk |= nonrot;
Shaohua Li9dedf602012-07-31 10:03:53 +1000706 pending = atomic_read(&rdev->nr_pending);
NeilBrown76073052011-05-11 14:34:56 +1000707 dist = abs(this_sector - conf->mirrors[disk].head_position);
Shaohua Li12cee5a2012-07-31 10:03:53 +1000708 if (choose_first) {
NeilBrown76073052011-05-11 14:34:56 +1000709 best_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 break;
711 }
Shaohua Li12cee5a2012-07-31 10:03:53 +1000712 /* Don't change to another disk for sequential reads */
713 if (conf->mirrors[disk].next_seq_sect == this_sector
714 || dist == 0) {
715 int opt_iosize = bdev_io_opt(rdev->bdev) >> 9;
716 struct raid1_info *mirror = &conf->mirrors[disk];
717
718 best_disk = disk;
719 /*
720 * If buffered sequential IO size exceeds optimal
721 * iosize, check if there is idle disk. If yes, choose
722 * the idle disk. read_balance could already choose an
723 * idle disk before noticing it's a sequential IO in
724 * this disk. This doesn't matter because this disk
725 * will idle, next time it will be utilized after the
726 * first disk has IO size exceeds optimal iosize. In
727 * this way, iosize of the first disk will be optimal
728 * iosize at least. iosize of the second disk might be
729 * small, but not a big deal since when the second disk
730 * starts IO, the first disk is likely still busy.
731 */
732 if (nonrot && opt_iosize > 0 &&
733 mirror->seq_start != MaxSector &&
734 mirror->next_seq_sect > opt_iosize &&
735 mirror->next_seq_sect - opt_iosize >=
736 mirror->seq_start) {
737 choose_next_idle = 1;
738 continue;
739 }
740 break;
741 }
Shaohua Li12cee5a2012-07-31 10:03:53 +1000742
743 if (choose_next_idle)
744 continue;
Shaohua Li9dedf602012-07-31 10:03:53 +1000745
746 if (min_pending > pending) {
747 min_pending = pending;
748 best_pending_disk = disk;
749 }
750
NeilBrown76073052011-05-11 14:34:56 +1000751 if (dist < best_dist) {
752 best_dist = dist;
Shaohua Li9dedf602012-07-31 10:03:53 +1000753 best_dist_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Shaohua Li9dedf602012-07-31 10:03:53 +1000757 /*
758 * If all disks are rotational, choose the closest disk. If any disk is
759 * non-rotational, choose the disk with less pending request even the
760 * disk is rotational, which might/might not be optimal for raids with
761 * mixed ratation/non-rotational disks depending on workload.
762 */
763 if (best_disk == -1) {
NeilBrown2e52d442016-11-18 16:16:12 +1100764 if (has_nonrot_disk || min_pending == 0)
Shaohua Li9dedf602012-07-31 10:03:53 +1000765 best_disk = best_pending_disk;
766 else
767 best_disk = best_dist_disk;
768 }
769
NeilBrown76073052011-05-11 14:34:56 +1000770 if (best_disk >= 0) {
771 rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700772 if (!rdev)
773 goto retry;
774 atomic_inc(&rdev->nr_pending);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000775 sectors = best_good_sectors;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000776
777 if (conf->mirrors[best_disk].next_seq_sect != this_sector)
778 conf->mirrors[best_disk].seq_start = this_sector;
779
Shaohua Libe4d3282012-07-31 10:03:53 +1000780 conf->mirrors[best_disk].next_seq_sect = this_sector + sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782 rcu_read_unlock();
NeilBrownd2eb35a2011-07-28 11:31:48 +1000783 *max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
NeilBrown76073052011-05-11 14:34:56 +1000785 return best_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
NeilBrown5c675f82014-12-15 12:56:56 +1100788static int raid1_congested(struct mddev *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700789{
NeilBrowne8096362011-10-11 16:49:05 +1100790 struct r1conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700791 int i, ret = 0;
792
Tejun Heo44522262015-05-22 17:13:26 -0400793 if ((bits & (1 << WB_async_congested)) &&
NeilBrown34db0cd2011-10-11 16:50:01 +1100794 conf->pending_count >= max_queued_requests)
795 return 1;
796
NeilBrown0d129222006-10-03 01:15:54 -0700797 rcu_read_lock();
NeilBrownf53e29f2012-02-13 14:24:05 +1100798 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100799 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700800 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200801 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700802
Jonathan Brassow1ed72422011-06-07 17:50:35 -0500803 BUG_ON(!q);
804
NeilBrown0d129222006-10-03 01:15:54 -0700805 /* Note the '|| 1' - when read_balance prefers
806 * non-congested targets, it can be removed
807 */
Tejun Heo44522262015-05-22 17:13:26 -0400808 if ((bits & (1 << WB_async_congested)) || 1)
Jan Karadc3b17c2017-02-02 15:56:50 +0100809 ret |= bdi_congested(q->backing_dev_info, bits);
NeilBrown0d129222006-10-03 01:15:54 -0700810 else
Jan Karadc3b17c2017-02-02 15:56:50 +0100811 ret &= bdi_congested(q->backing_dev_info, bits);
NeilBrown0d129222006-10-03 01:15:54 -0700812 }
813 }
814 rcu_read_unlock();
815 return ret;
816}
NeilBrown0d129222006-10-03 01:15:54 -0700817
NeilBrown673ca682017-04-05 14:05:51 +1000818static void flush_bio_list(struct r1conf *conf, struct bio *bio)
819{
820 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -0700821 md_bitmap_unplug(conf->mddev->bitmap);
NeilBrown673ca682017-04-05 14:05:51 +1000822 wake_up(&conf->wait_barrier);
823
824 while (bio) { /* submit pending writes */
825 struct bio *next = bio->bi_next;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200826 struct md_rdev *rdev = (void *)bio->bi_disk;
NeilBrown673ca682017-04-05 14:05:51 +1000827 bio->bi_next = NULL;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200828 bio_set_dev(bio, rdev->bdev);
NeilBrown673ca682017-04-05 14:05:51 +1000829 if (test_bit(Faulty, &rdev->flags)) {
Guoqing Jiang6308d8e2017-07-21 16:33:44 +0800830 bio_io_error(bio);
NeilBrown673ca682017-04-05 14:05:51 +1000831 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
Christoph Hellwig74d46992017-08-23 19:10:32 +0200832 !blk_queue_discard(bio->bi_disk->queue)))
NeilBrown673ca682017-04-05 14:05:51 +1000833 /* Just ignore it */
834 bio_endio(bio);
835 else
836 generic_make_request(bio);
837 bio = next;
Hannes Reinecke5fa4f8b2019-10-25 09:08:56 +0200838 cond_resched();
NeilBrown673ca682017-04-05 14:05:51 +1000839 }
840}
841
NeilBrowne8096362011-10-11 16:49:05 +1100842static void flush_pending_writes(struct r1conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800843{
844 /* Any writes that have been queued but are awaiting
845 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800846 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800847 spin_lock_irq(&conf->device_lock);
848
849 if (conf->pending_bio_list.head) {
Shaohua Li18022a12017-12-01 12:12:34 -0800850 struct blk_plug plug;
NeilBrowna35e63e2008-03-04 14:29:29 -0800851 struct bio *bio;
Shaohua Li18022a12017-12-01 12:12:34 -0800852
NeilBrowna35e63e2008-03-04 14:29:29 -0800853 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100854 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800855 spin_unlock_irq(&conf->device_lock);
NeilBrown474beb52017-12-04 08:21:04 +1100856
857 /*
858 * As this is called in a wait_event() loop (see freeze_array),
859 * current->state might be TASK_UNINTERRUPTIBLE which will
860 * cause a warning when we prepare to wait again. As it is
861 * rare that this path is taken, it is perfectly safe to force
862 * us to go around the wait_event() loop again, so the warning
863 * is a false-positive. Silence the warning by resetting
864 * thread state
865 */
866 __set_current_state(TASK_RUNNING);
Shaohua Li18022a12017-12-01 12:12:34 -0800867 blk_start_plug(&plug);
NeilBrown673ca682017-04-05 14:05:51 +1000868 flush_bio_list(conf, bio);
Shaohua Li18022a12017-12-01 12:12:34 -0800869 blk_finish_plug(&plug);
NeilBrowna35e63e2008-03-04 14:29:29 -0800870 } else
871 spin_unlock_irq(&conf->device_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100872}
873
NeilBrown17999be2006-01-06 00:20:12 -0800874/* Barriers....
875 * Sometimes we need to suspend IO while we do something else,
876 * either some resync/recovery, or reconfigure the array.
877 * To do this we raise a 'barrier'.
878 * The 'barrier' is a counter that can be raised multiple times
879 * to count how many activities are happening which preclude
880 * normal IO.
881 * We can only raise the barrier if there is no pending IO.
882 * i.e. if nr_pending == 0.
883 * We choose only to raise the barrier if no-one is waiting for the
884 * barrier to go down. This means that as soon as an IO request
885 * is ready, no other operations which require a barrier will start
886 * until the IO request has had a chance.
887 *
888 * So: regular IO calls 'wait_barrier'. When that returns there
889 * is no backgroup IO happening, It must arrange to call
890 * allow_barrier when it has finished its IO.
891 * backgroup IO calls must call raise_barrier. Once that returns
892 * there is no normal IO happeing. It must arrange to call
893 * lower_barrier when the particular background IO completes.
Hou Tao46757192019-07-02 22:35:48 +0800894 *
895 * If resync/recovery is interrupted, returns -EINTR;
896 * Otherwise, returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 */
Hou Tao46757192019-07-02 22:35:48 +0800898static int raise_barrier(struct r1conf *conf, sector_t sector_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
colyli@suse.defd768632017-02-18 03:05:56 +0800900 int idx = sector_to_idx(sector_nr);
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 spin_lock_irq(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800903
904 /* Wait until no block IO is waiting */
colyli@suse.de824e47d2017-02-18 03:05:57 +0800905 wait_event_lock_irq(conf->wait_barrier,
906 !atomic_read(&conf->nr_waiting[idx]),
Lukas Czernereed8c022012-11-30 11:42:40 +0100907 conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800908
909 /* block any new IO from starting */
colyli@suse.de824e47d2017-02-18 03:05:57 +0800910 atomic_inc(&conf->barrier[idx]);
911 /*
912 * In raise_barrier() we firstly increase conf->barrier[idx] then
913 * check conf->nr_pending[idx]. In _wait_barrier() we firstly
914 * increase conf->nr_pending[idx] then check conf->barrier[idx].
915 * A memory barrier here to make sure conf->nr_pending[idx] won't
916 * be fetched before conf->barrier[idx] is increased. Otherwise
917 * there will be a race between raise_barrier() and _wait_barrier().
918 */
919 smp_mb__after_atomic();
NeilBrown17999be2006-01-06 00:20:12 -0800920
majianpeng79ef3a82013-11-15 14:55:02 +0800921 /* For these conditions we must wait:
922 * A: while the array is in frozen state
colyli@suse.defd768632017-02-18 03:05:56 +0800923 * B: while conf->nr_pending[idx] is not 0, meaning regular I/O
924 * existing in corresponding I/O barrier bucket.
925 * C: while conf->barrier[idx] >= RESYNC_DEPTH, meaning reaches
926 * max resync count which allowed on current I/O barrier bucket.
majianpeng79ef3a82013-11-15 14:55:02 +0800927 */
NeilBrown17999be2006-01-06 00:20:12 -0800928 wait_event_lock_irq(conf->wait_barrier,
Yufen Yu8c242592018-04-09 09:50:44 +0800929 (!conf->array_frozen &&
colyli@suse.de824e47d2017-02-18 03:05:57 +0800930 !atomic_read(&conf->nr_pending[idx]) &&
Yufen Yu8c242592018-04-09 09:50:44 +0800931 atomic_read(&conf->barrier[idx]) < RESYNC_DEPTH) ||
932 test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery),
Lukas Czernereed8c022012-11-30 11:42:40 +0100933 conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800934
Yufen Yu8c242592018-04-09 09:50:44 +0800935 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
936 atomic_dec(&conf->barrier[idx]);
937 spin_unlock_irq(&conf->resync_lock);
938 wake_up(&conf->wait_barrier);
939 return -EINTR;
940 }
941
Xiao Ni43ac9b82017-04-27 16:28:49 +0800942 atomic_inc(&conf->nr_sync_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 spin_unlock_irq(&conf->resync_lock);
Yufen Yu8c242592018-04-09 09:50:44 +0800944
945 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
colyli@suse.defd768632017-02-18 03:05:56 +0800948static void lower_barrier(struct r1conf *conf, sector_t sector_nr)
NeilBrown17999be2006-01-06 00:20:12 -0800949{
colyli@suse.defd768632017-02-18 03:05:56 +0800950 int idx = sector_to_idx(sector_nr);
951
colyli@suse.de824e47d2017-02-18 03:05:57 +0800952 BUG_ON(atomic_read(&conf->barrier[idx]) <= 0);
colyli@suse.defd768632017-02-18 03:05:56 +0800953
colyli@suse.de824e47d2017-02-18 03:05:57 +0800954 atomic_dec(&conf->barrier[idx]);
Xiao Ni43ac9b82017-04-27 16:28:49 +0800955 atomic_dec(&conf->nr_sync_pending);
NeilBrown17999be2006-01-06 00:20:12 -0800956 wake_up(&conf->wait_barrier);
957}
958
colyli@suse.defd768632017-02-18 03:05:56 +0800959static void _wait_barrier(struct r1conf *conf, int idx)
NeilBrown17999be2006-01-06 00:20:12 -0800960{
colyli@suse.de824e47d2017-02-18 03:05:57 +0800961 /*
962 * We need to increase conf->nr_pending[idx] very early here,
963 * then raise_barrier() can be blocked when it waits for
964 * conf->nr_pending[idx] to be 0. Then we can avoid holding
965 * conf->resync_lock when there is no barrier raised in same
966 * barrier unit bucket. Also if the array is frozen, I/O
967 * should be blocked until array is unfrozen.
968 */
969 atomic_inc(&conf->nr_pending[idx]);
970 /*
971 * In _wait_barrier() we firstly increase conf->nr_pending[idx], then
972 * check conf->barrier[idx]. In raise_barrier() we firstly increase
973 * conf->barrier[idx], then check conf->nr_pending[idx]. A memory
974 * barrier is necessary here to make sure conf->barrier[idx] won't be
975 * fetched before conf->nr_pending[idx] is increased. Otherwise there
976 * will be a race between _wait_barrier() and raise_barrier().
977 */
978 smp_mb__after_atomic();
majianpeng79ef3a82013-11-15 14:55:02 +0800979
colyli@suse.de824e47d2017-02-18 03:05:57 +0800980 /*
981 * Don't worry about checking two atomic_t variables at same time
982 * here. If during we check conf->barrier[idx], the array is
983 * frozen (conf->array_frozen is 1), and chonf->barrier[idx] is
984 * 0, it is safe to return and make the I/O continue. Because the
985 * array is frozen, all I/O returned here will eventually complete
986 * or be queued, no race will happen. See code comment in
987 * frozen_array().
988 */
989 if (!READ_ONCE(conf->array_frozen) &&
990 !atomic_read(&conf->barrier[idx]))
991 return;
majianpeng79ef3a82013-11-15 14:55:02 +0800992
colyli@suse.de824e47d2017-02-18 03:05:57 +0800993 /*
994 * After holding conf->resync_lock, conf->nr_pending[idx]
995 * should be decreased before waiting for barrier to drop.
996 * Otherwise, we may encounter a race condition because
997 * raise_barrer() might be waiting for conf->nr_pending[idx]
998 * to be 0 at same time.
999 */
1000 spin_lock_irq(&conf->resync_lock);
1001 atomic_inc(&conf->nr_waiting[idx]);
1002 atomic_dec(&conf->nr_pending[idx]);
1003 /*
1004 * In case freeze_array() is waiting for
1005 * get_unqueued_pending() == extra
1006 */
1007 wake_up(&conf->wait_barrier);
1008 /* Wait for the barrier in same barrier unit bucket to drop. */
1009 wait_event_lock_irq(conf->wait_barrier,
1010 !conf->array_frozen &&
1011 !atomic_read(&conf->barrier[idx]),
1012 conf->resync_lock);
1013 atomic_inc(&conf->nr_pending[idx]);
1014 atomic_dec(&conf->nr_waiting[idx]);
colyli@suse.defd768632017-02-18 03:05:56 +08001015 spin_unlock_irq(&conf->resync_lock);
majianpeng79ef3a82013-11-15 14:55:02 +08001016}
1017
colyli@suse.defd768632017-02-18 03:05:56 +08001018static void wait_read_barrier(struct r1conf *conf, sector_t sector_nr)
majianpeng79ef3a82013-11-15 14:55:02 +08001019{
colyli@suse.defd768632017-02-18 03:05:56 +08001020 int idx = sector_to_idx(sector_nr);
majianpeng79ef3a82013-11-15 14:55:02 +08001021
colyli@suse.de824e47d2017-02-18 03:05:57 +08001022 /*
1023 * Very similar to _wait_barrier(). The difference is, for read
1024 * I/O we don't need wait for sync I/O, but if the whole array
1025 * is frozen, the read I/O still has to wait until the array is
1026 * unfrozen. Since there is no ordering requirement with
1027 * conf->barrier[idx] here, memory barrier is unnecessary as well.
1028 */
1029 atomic_inc(&conf->nr_pending[idx]);
majianpeng79ef3a82013-11-15 14:55:02 +08001030
colyli@suse.de824e47d2017-02-18 03:05:57 +08001031 if (!READ_ONCE(conf->array_frozen))
1032 return;
NeilBrown17999be2006-01-06 00:20:12 -08001033
1034 spin_lock_irq(&conf->resync_lock);
colyli@suse.de824e47d2017-02-18 03:05:57 +08001035 atomic_inc(&conf->nr_waiting[idx]);
1036 atomic_dec(&conf->nr_pending[idx]);
1037 /*
1038 * In case freeze_array() is waiting for
1039 * get_unqueued_pending() == extra
1040 */
1041 wake_up(&conf->wait_barrier);
1042 /* Wait for array to be unfrozen */
1043 wait_event_lock_irq(conf->wait_barrier,
1044 !conf->array_frozen,
1045 conf->resync_lock);
1046 atomic_inc(&conf->nr_pending[idx]);
1047 atomic_dec(&conf->nr_waiting[idx]);
NeilBrown17999be2006-01-06 00:20:12 -08001048 spin_unlock_irq(&conf->resync_lock);
1049}
1050
colyli@suse.defd768632017-02-18 03:05:56 +08001051static void wait_barrier(struct r1conf *conf, sector_t sector_nr)
NeilBrown17999be2006-01-06 00:20:12 -08001052{
colyli@suse.defd768632017-02-18 03:05:56 +08001053 int idx = sector_to_idx(sector_nr);
majianpeng79ef3a82013-11-15 14:55:02 +08001054
colyli@suse.defd768632017-02-18 03:05:56 +08001055 _wait_barrier(conf, idx);
1056}
majianpeng79ef3a82013-11-15 14:55:02 +08001057
colyli@suse.defd768632017-02-18 03:05:56 +08001058static void _allow_barrier(struct r1conf *conf, int idx)
NeilBrown17999be2006-01-06 00:20:12 -08001059{
colyli@suse.de824e47d2017-02-18 03:05:57 +08001060 atomic_dec(&conf->nr_pending[idx]);
NeilBrown17999be2006-01-06 00:20:12 -08001061 wake_up(&conf->wait_barrier);
1062}
1063
colyli@suse.defd768632017-02-18 03:05:56 +08001064static void allow_barrier(struct r1conf *conf, sector_t sector_nr)
1065{
1066 int idx = sector_to_idx(sector_nr);
1067
1068 _allow_barrier(conf, idx);
1069}
1070
colyli@suse.defd768632017-02-18 03:05:56 +08001071/* conf->resync_lock should be held */
1072static int get_unqueued_pending(struct r1conf *conf)
1073{
1074 int idx, ret;
1075
Xiao Ni43ac9b82017-04-27 16:28:49 +08001076 ret = atomic_read(&conf->nr_sync_pending);
1077 for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++)
colyli@suse.de824e47d2017-02-18 03:05:57 +08001078 ret += atomic_read(&conf->nr_pending[idx]) -
1079 atomic_read(&conf->nr_queued[idx]);
colyli@suse.defd768632017-02-18 03:05:56 +08001080
1081 return ret;
1082}
1083
NeilBrowne2d59922013-06-12 11:01:22 +10001084static void freeze_array(struct r1conf *conf, int extra)
NeilBrownddaf22a2006-01-06 00:20:19 -08001085{
colyli@suse.defd768632017-02-18 03:05:56 +08001086 /* Stop sync I/O and normal I/O and wait for everything to
Zhilong Liu11353b92017-03-14 15:52:26 +08001087 * go quiet.
colyli@suse.defd768632017-02-18 03:05:56 +08001088 * This is called in two situations:
1089 * 1) management command handlers (reshape, remove disk, quiesce).
1090 * 2) one normal I/O request failed.
1091
1092 * After array_frozen is set to 1, new sync IO will be blocked at
1093 * raise_barrier(), and new normal I/O will blocked at _wait_barrier()
1094 * or wait_read_barrier(). The flying I/Os will either complete or be
1095 * queued. When everything goes quite, there are only queued I/Os left.
1096
1097 * Every flying I/O contributes to a conf->nr_pending[idx], idx is the
1098 * barrier bucket index which this I/O request hits. When all sync and
1099 * normal I/O are queued, sum of all conf->nr_pending[] will match sum
1100 * of all conf->nr_queued[]. But normal I/O failure is an exception,
1101 * in handle_read_error(), we may call freeze_array() before trying to
1102 * fix the read error. In this case, the error read I/O is not queued,
1103 * so get_unqueued_pending() == 1.
1104 *
1105 * Therefore before this function returns, we need to wait until
1106 * get_unqueued_pendings(conf) gets equal to extra. For
1107 * normal I/O context, extra is 1, in rested situations extra is 0.
NeilBrownddaf22a2006-01-06 00:20:19 -08001108 */
1109 spin_lock_irq(&conf->resync_lock);
majianpengb364e3d2013-11-14 15:16:18 +11001110 conf->array_frozen = 1;
NeilBrown578b54a2016-11-14 16:30:21 +11001111 raid1_log(conf->mddev, "wait freeze");
colyli@suse.defd768632017-02-18 03:05:56 +08001112 wait_event_lock_irq_cmd(
1113 conf->wait_barrier,
1114 get_unqueued_pending(conf) == extra,
1115 conf->resync_lock,
1116 flush_pending_writes(conf));
NeilBrownddaf22a2006-01-06 00:20:19 -08001117 spin_unlock_irq(&conf->resync_lock);
1118}
NeilBrowne8096362011-10-11 16:49:05 +11001119static void unfreeze_array(struct r1conf *conf)
NeilBrownddaf22a2006-01-06 00:20:19 -08001120{
1121 /* reverse the effect of the freeze */
1122 spin_lock_irq(&conf->resync_lock);
majianpengb364e3d2013-11-14 15:16:18 +11001123 conf->array_frozen = 0;
NeilBrownddaf22a2006-01-06 00:20:19 -08001124 spin_unlock_irq(&conf->resync_lock);
colyli@suse.de824e47d2017-02-18 03:05:57 +08001125 wake_up(&conf->wait_barrier);
NeilBrownddaf22a2006-01-06 00:20:19 -08001126}
1127
Shaohua Li16d56e22017-07-17 14:33:48 -07001128static void alloc_behind_master_bio(struct r1bio *r1_bio,
NeilBrowncb83efc2017-04-05 14:05:50 +10001129 struct bio *bio)
NeilBrown4b6d2872005-09-09 16:23:47 -07001130{
NeilBrowncb83efc2017-04-05 14:05:50 +10001131 int size = bio->bi_iter.bi_size;
Ming Lei841c1312017-03-17 00:12:31 +08001132 unsigned vcnt = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1133 int i = 0;
1134 struct bio *behind_bio = NULL;
NeilBrown4b6d2872005-09-09 16:23:47 -07001135
Ming Lei841c1312017-03-17 00:12:31 +08001136 behind_bio = bio_alloc_mddev(GFP_NOIO, vcnt, r1_bio->mddev);
1137 if (!behind_bio)
Shaohua Li16d56e22017-07-17 14:33:48 -07001138 return;
Ming Lei841c1312017-03-17 00:12:31 +08001139
Shaohua Li41743c12017-03-24 15:20:47 -07001140 /* discard op, we don't support writezero/writesame yet */
Shaohua Li16d56e22017-07-17 14:33:48 -07001141 if (!bio_has_data(bio)) {
1142 behind_bio->bi_iter.bi_size = size;
Shaohua Li41743c12017-03-24 15:20:47 -07001143 goto skip_copy;
Shaohua Li16d56e22017-07-17 14:33:48 -07001144 }
Shaohua Li41743c12017-03-24 15:20:47 -07001145
Mariusz Dabrowskidba40d42018-02-14 14:23:30 +01001146 behind_bio->bi_write_hint = bio->bi_write_hint;
1147
Ming Lei841c1312017-03-17 00:12:31 +08001148 while (i < vcnt && size) {
1149 struct page *page;
1150 int len = min_t(int, PAGE_SIZE, size);
1151
1152 page = alloc_page(GFP_NOIO);
1153 if (unlikely(!page))
1154 goto free_pages;
1155
1156 bio_add_page(behind_bio, page, len, 0);
1157
1158 size -= len;
1159 i++;
NeilBrown4b6d2872005-09-09 16:23:47 -07001160 }
NeilBrown4b6d2872005-09-09 16:23:47 -07001161
NeilBrowncb83efc2017-04-05 14:05:50 +10001162 bio_copy_data(behind_bio, bio);
Shaohua Li41743c12017-03-24 15:20:47 -07001163skip_copy:
Luis de Bethencourt56a64c12018-01-17 13:38:02 +00001164 r1_bio->behind_master_bio = behind_bio;
Ming Lei841c1312017-03-17 00:12:31 +08001165 set_bit(R1BIO_BehindIO, &r1_bio->state);
1166
Shaohua Li16d56e22017-07-17 14:33:48 -07001167 return;
Ming Lei841c1312017-03-17 00:12:31 +08001168
1169free_pages:
Kent Overstreet4f024f32013-10-11 15:44:27 -07001170 pr_debug("%dB behind alloc failed, doing sync I/O\n",
1171 bio->bi_iter.bi_size);
Ming Lei841c1312017-03-17 00:12:31 +08001172 bio_free_pages(behind_bio);
Shaohua Li16d56e22017-07-17 14:33:48 -07001173 bio_put(behind_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -07001174}
1175
NeilBrownf54a9d02012-08-02 08:33:20 +10001176struct raid1_plug_cb {
1177 struct blk_plug_cb cb;
1178 struct bio_list pending;
1179 int pending_cnt;
1180};
1181
1182static void raid1_unplug(struct blk_plug_cb *cb, bool from_schedule)
1183{
1184 struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb,
1185 cb);
1186 struct mddev *mddev = plug->cb.data;
1187 struct r1conf *conf = mddev->private;
1188 struct bio *bio;
1189
NeilBrown874807a2012-11-27 12:14:40 +11001190 if (from_schedule || current->bio_list) {
NeilBrownf54a9d02012-08-02 08:33:20 +10001191 spin_lock_irq(&conf->device_lock);
1192 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1193 conf->pending_count += plug->pending_cnt;
1194 spin_unlock_irq(&conf->device_lock);
NeilBrownee0b0242013-02-25 12:38:29 +11001195 wake_up(&conf->wait_barrier);
NeilBrownf54a9d02012-08-02 08:33:20 +10001196 md_wakeup_thread(mddev->thread);
1197 kfree(plug);
1198 return;
1199 }
1200
1201 /* we aren't scheduling, so we can do the write-out directly. */
1202 bio = bio_list_get(&plug->pending);
NeilBrown673ca682017-04-05 14:05:51 +10001203 flush_bio_list(conf, bio);
NeilBrownf54a9d02012-08-02 08:33:20 +10001204 kfree(plug);
1205}
1206
NeilBrown689389a2017-04-05 14:05:50 +10001207static void init_r1bio(struct r1bio *r1_bio, struct mddev *mddev, struct bio *bio)
1208{
1209 r1_bio->master_bio = bio;
1210 r1_bio->sectors = bio_sectors(bio);
1211 r1_bio->state = 0;
1212 r1_bio->mddev = mddev;
1213 r1_bio->sector = bio->bi_iter.bi_sector;
1214}
1215
colyli@suse.defd768632017-02-18 03:05:56 +08001216static inline struct r1bio *
NeilBrown689389a2017-04-05 14:05:50 +10001217alloc_r1bio(struct mddev *mddev, struct bio *bio)
colyli@suse.defd768632017-02-18 03:05:56 +08001218{
1219 struct r1conf *conf = mddev->private;
1220 struct r1bio *r1_bio;
1221
Kent Overstreetafeee512018-05-20 18:25:52 -04001222 r1_bio = mempool_alloc(&conf->r1bio_pool, GFP_NOIO);
NeilBrown689389a2017-04-05 14:05:50 +10001223 /* Ensure no bio records IO_BLOCKED */
1224 memset(r1_bio->bios, 0, conf->raid_disks * sizeof(r1_bio->bios[0]));
1225 init_r1bio(r1_bio, mddev, bio);
colyli@suse.defd768632017-02-18 03:05:56 +08001226 return r1_bio;
1227}
1228
NeilBrownc230e7e2017-04-05 14:05:50 +10001229static void raid1_read_request(struct mddev *mddev, struct bio *bio,
NeilBrown689389a2017-04-05 14:05:50 +10001230 int max_read_sectors, struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
NeilBrowne8096362011-10-11 16:49:05 +11001232 struct r1conf *conf = mddev->private;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001233 struct raid1_info *mirror;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 struct bio *read_bio;
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001235 struct bitmap *bitmap = mddev->bitmap;
1236 const int op = bio_op(bio);
1237 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001238 int max_sectors;
1239 int rdisk;
NeilBrown689389a2017-04-05 14:05:50 +10001240 bool print_msg = !!r1_bio;
1241 char b[BDEVNAME_SIZE];
1242
1243 /*
1244 * If r1_bio is set, we are blocking the raid1d thread
1245 * so there is a tiny risk of deadlock. So ask for
1246 * emergency memory if needed.
1247 */
1248 gfp_t gfp = r1_bio ? (GFP_NOIO | __GFP_HIGH) : GFP_NOIO;
1249
1250 if (print_msg) {
1251 /* Need to get the block device name carefully */
1252 struct md_rdev *rdev;
1253 rcu_read_lock();
1254 rdev = rcu_dereference(conf->mirrors[r1_bio->read_disk].rdev);
1255 if (rdev)
1256 bdevname(rdev->bdev, b);
1257 else
1258 strcpy(b, "???");
1259 rcu_read_unlock();
1260 }
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001261
colyli@suse.defd768632017-02-18 03:05:56 +08001262 /*
1263 * Still need barrier for READ in case that whole
1264 * array is frozen.
1265 */
1266 wait_read_barrier(conf, bio->bi_iter.bi_sector);
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001267
NeilBrown689389a2017-04-05 14:05:50 +10001268 if (!r1_bio)
1269 r1_bio = alloc_r1bio(mddev, bio);
1270 else
1271 init_r1bio(r1_bio, mddev, bio);
NeilBrownc230e7e2017-04-05 14:05:50 +10001272 r1_bio->sectors = max_read_sectors;
colyli@suse.defd768632017-02-18 03:05:56 +08001273
1274 /*
1275 * make_request() can abort the operation when read-ahead is being
1276 * used and no empty request is available.
1277 */
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001278 rdisk = read_balance(conf, r1_bio, &max_sectors);
1279
1280 if (rdisk < 0) {
1281 /* couldn't find anywhere to read from */
NeilBrown689389a2017-04-05 14:05:50 +10001282 if (print_msg) {
1283 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
1284 mdname(mddev),
1285 b,
1286 (unsigned long long)r1_bio->sector);
1287 }
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001288 raid_end_bio_io(r1_bio);
1289 return;
1290 }
1291 mirror = conf->mirrors + rdisk;
1292
NeilBrown689389a2017-04-05 14:05:50 +10001293 if (print_msg)
1294 pr_info_ratelimited("md/raid1:%s: redirecting sector %llu to other mirror: %s\n",
1295 mdname(mddev),
1296 (unsigned long long)r1_bio->sector,
1297 bdevname(mirror->rdev->bdev, b));
1298
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001299 if (test_bit(WriteMostly, &mirror->rdev->flags) &&
1300 bitmap) {
1301 /*
1302 * Reading from a write-mostly device must take care not to
1303 * over-take any writes that are 'behind'
1304 */
1305 raid1_log(mddev, "wait behind writes");
1306 wait_event(bitmap->behind_wait,
1307 atomic_read(&bitmap->behind_writes) == 0);
1308 }
NeilBrownc230e7e2017-04-05 14:05:50 +10001309
1310 if (max_sectors < bio_sectors(bio)) {
1311 struct bio *split = bio_split(bio, max_sectors,
Kent Overstreetafeee512018-05-20 18:25:52 -04001312 gfp, &conf->bio_split);
NeilBrownc230e7e2017-04-05 14:05:50 +10001313 bio_chain(split, bio);
1314 generic_make_request(bio);
1315 bio = split;
1316 r1_bio->master_bio = bio;
1317 r1_bio->sectors = max_sectors;
1318 }
1319
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001320 r1_bio->read_disk = rdisk;
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001321
Kent Overstreetafeee512018-05-20 18:25:52 -04001322 read_bio = bio_clone_fast(bio, gfp, &mddev->bio_set);
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001323
1324 r1_bio->bios[rdisk] = read_bio;
1325
1326 read_bio->bi_iter.bi_sector = r1_bio->sector +
1327 mirror->rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001328 bio_set_dev(read_bio, mirror->rdev->bdev);
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001329 read_bio->bi_end_io = raid1_end_read_request;
1330 bio_set_op_attrs(read_bio, op, do_sync);
1331 if (test_bit(FailFast, &mirror->rdev->flags) &&
1332 test_bit(R1BIO_FailFast, &r1_bio->state))
1333 read_bio->bi_opf |= MD_FAILFAST;
1334 read_bio->bi_private = r1_bio;
1335
1336 if (mddev->gendisk)
Christoph Hellwig74d46992017-08-23 19:10:32 +02001337 trace_block_bio_remap(read_bio->bi_disk->queue, read_bio,
1338 disk_devt(mddev->gendisk), r1_bio->sector);
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001339
NeilBrownc230e7e2017-04-05 14:05:50 +10001340 generic_make_request(read_bio);
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001341}
1342
NeilBrownc230e7e2017-04-05 14:05:50 +10001343static void raid1_write_request(struct mddev *mddev, struct bio *bio,
1344 int max_write_sectors)
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001345{
1346 struct r1conf *conf = mddev->private;
colyli@suse.defd768632017-02-18 03:05:56 +08001347 struct r1bio *r1_bio;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001348 int i, disks;
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001349 struct bitmap *bitmap = mddev->bitmap;
NeilBrown191ea9b2005-06-21 17:17:23 -07001350 unsigned long flags;
NeilBrown3cb03002011-10-11 16:45:26 +11001351 struct md_rdev *blocked_rdev;
NeilBrownf54a9d02012-08-02 08:33:20 +10001352 struct blk_plug_cb *cb;
1353 struct raid1_plug_cb *plug = NULL;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001354 int first_clone;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001355 int max_sectors;
NeilBrown191ea9b2005-06-21 17:17:23 -07001356
NeilBrownb3143b92017-10-17 13:46:43 +11001357 if (mddev_is_clustered(mddev) &&
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -05001358 md_cluster_ops->area_resyncing(mddev, WRITE,
NeilBrownb3143b92017-10-17 13:46:43 +11001359 bio->bi_iter.bi_sector, bio_end_sector(bio))) {
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001360
NeilBrown6eef4b22009-12-14 12:49:51 +11001361 DEFINE_WAIT(w);
1362 for (;;) {
NeilBrown6eef4b22009-12-14 12:49:51 +11001363 prepare_to_wait(&conf->wait_barrier,
Mikulas Patockaae89fd32017-10-18 19:01:11 -04001364 &w, TASK_IDLE);
Guoqing Jiangf81f7302017-10-24 15:33:33 +08001365 if (!md_cluster_ops->area_resyncing(mddev, WRITE,
Guoqing Jiang385f4d72017-09-29 09:16:43 +08001366 bio->bi_iter.bi_sector,
NeilBrownb3143b92017-10-17 13:46:43 +11001367 bio_end_sector(bio)))
NeilBrown6eef4b22009-12-14 12:49:51 +11001368 break;
1369 schedule();
1370 }
1371 finish_wait(&conf->wait_barrier, &w);
1372 }
Guoqing Jiangf81f7302017-10-24 15:33:33 +08001373
1374 /*
1375 * Register the new request and wait if the reconstruction
1376 * thread has put up a bar for new requests.
1377 * Continue immediately if no resync is active currently.
1378 */
colyli@suse.defd768632017-02-18 03:05:56 +08001379 wait_barrier(conf, bio->bi_iter.bi_sector);
1380
NeilBrown689389a2017-04-05 14:05:50 +10001381 r1_bio = alloc_r1bio(mddev, bio);
NeilBrownc230e7e2017-04-05 14:05:50 +10001382 r1_bio->sectors = max_write_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
NeilBrown34db0cd2011-10-11 16:50:01 +11001384 if (conf->pending_count >= max_queued_requests) {
1385 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001386 raid1_log(mddev, "wait queued");
NeilBrown34db0cd2011-10-11 16:50:01 +11001387 wait_event(conf->wait_barrier,
1388 conf->pending_count < max_queued_requests);
1389 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001390 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 * inc refcount on their rdev. Record them by setting
1392 * bios[x] to bio
NeilBrown1f68f0c2011-07-28 11:31:48 +10001393 * If there are known/acknowledged bad blocks on any device on
1394 * which we have seen a write error, we want to avoid writing those
1395 * blocks.
1396 * This potentially requires several writes to write around
1397 * the bad blocks. Each set of writes gets it's own r1bio
1398 * with a set of bios attached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001400
NeilBrown8f19ccb2011-12-23 10:17:56 +11001401 disks = conf->raid_disks * 2;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001402 retry_write:
1403 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 rcu_read_lock();
NeilBrown1f68f0c2011-07-28 11:31:48 +10001405 max_sectors = r1_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 for (i = 0; i < disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001407 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001408 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1409 atomic_inc(&rdev->nr_pending);
1410 blocked_rdev = rdev;
1411 break;
1412 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001413 r1_bio->bios[i] = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001414 if (!rdev || test_bit(Faulty, &rdev->flags)) {
NeilBrown8f19ccb2011-12-23 10:17:56 +11001415 if (i < conf->raid_disks)
1416 set_bit(R1BIO_Degraded, &r1_bio->state);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001417 continue;
1418 }
1419
1420 atomic_inc(&rdev->nr_pending);
1421 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1422 sector_t first_bad;
1423 int bad_sectors;
1424 int is_bad;
1425
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001426 is_bad = is_badblock(rdev, r1_bio->sector, max_sectors,
NeilBrown1f68f0c2011-07-28 11:31:48 +10001427 &first_bad, &bad_sectors);
1428 if (is_bad < 0) {
1429 /* mustn't write here until the bad block is
1430 * acknowledged*/
1431 set_bit(BlockedBadBlocks, &rdev->flags);
1432 blocked_rdev = rdev;
1433 break;
NeilBrown964147d2010-05-18 15:27:13 +10001434 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001435 if (is_bad && first_bad <= r1_bio->sector) {
1436 /* Cannot write here at all */
1437 bad_sectors -= (r1_bio->sector - first_bad);
1438 if (bad_sectors < max_sectors)
1439 /* mustn't write more than bad_sectors
1440 * to other devices yet
1441 */
1442 max_sectors = bad_sectors;
1443 rdev_dec_pending(rdev, mddev);
1444 /* We don't set R1BIO_Degraded as that
1445 * only applies if the disk is
1446 * missing, so it might be re-added,
1447 * and we want to know to recover this
1448 * chunk.
1449 * In this case the device is here,
1450 * and the fact that this chunk is not
1451 * in-sync is recorded in the bad
1452 * block log
1453 */
1454 continue;
1455 }
1456 if (is_bad) {
1457 int good_sectors = first_bad - r1_bio->sector;
1458 if (good_sectors < max_sectors)
1459 max_sectors = good_sectors;
1460 }
1461 }
1462 r1_bio->bios[i] = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464 rcu_read_unlock();
1465
Dan Williams6bfe0b42008-04-30 00:52:32 -07001466 if (unlikely(blocked_rdev)) {
1467 /* Wait for this device to become unblocked */
1468 int j;
1469
1470 for (j = 0; j < i; j++)
1471 if (r1_bio->bios[j])
1472 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001473 r1_bio->state = 0;
colyli@suse.defd768632017-02-18 03:05:56 +08001474 allow_barrier(conf, bio->bi_iter.bi_sector);
NeilBrown578b54a2016-11-14 16:30:21 +11001475 raid1_log(mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001476 md_wait_for_blocked_rdev(blocked_rdev, mddev);
colyli@suse.defd768632017-02-18 03:05:56 +08001477 wait_barrier(conf, bio->bi_iter.bi_sector);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001478 goto retry_write;
1479 }
1480
NeilBrownc230e7e2017-04-05 14:05:50 +10001481 if (max_sectors < bio_sectors(bio)) {
1482 struct bio *split = bio_split(bio, max_sectors,
Kent Overstreetafeee512018-05-20 18:25:52 -04001483 GFP_NOIO, &conf->bio_split);
NeilBrownc230e7e2017-04-05 14:05:50 +10001484 bio_chain(split, bio);
1485 generic_make_request(bio);
1486 bio = split;
1487 r1_bio->master_bio = bio;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001488 r1_bio->sectors = max_sectors;
NeilBrown191ea9b2005-06-21 17:17:23 -07001489 }
NeilBrown4b6d2872005-09-09 16:23:47 -07001490
NeilBrown4e780642010-10-19 12:54:01 +11001491 atomic_set(&r1_bio->remaining, 1);
NeilBrown4b6d2872005-09-09 16:23:47 -07001492 atomic_set(&r1_bio->behind_remaining, 0);
NeilBrown191ea9b2005-06-21 17:17:23 -07001493
NeilBrown1f68f0c2011-07-28 11:31:48 +10001494 first_clone = 1;
Ming Leid8c84c42017-03-17 00:12:30 +08001495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 for (i = 0; i < disks; i++) {
Ming Lei8e58e322017-02-14 23:29:01 +08001497 struct bio *mbio = NULL;
Guoqing Jiang69df9cf2019-12-23 10:48:58 +01001498 struct md_rdev *rdev = conf->mirrors[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (!r1_bio->bios[i])
1500 continue;
1501
NeilBrown1f68f0c2011-07-28 11:31:48 +10001502 if (first_clone) {
1503 /* do behind I/O ?
1504 * Not if there are too many, or cannot
1505 * allocate memory, or a reader on WriteMostly
1506 * is waiting for behind writes to flush */
1507 if (bitmap &&
1508 (atomic_read(&bitmap->behind_writes)
1509 < mddev->bitmap_info.max_write_behind) &&
Ming Lei8e58e322017-02-14 23:29:01 +08001510 !waitqueue_active(&bitmap->behind_wait)) {
Shaohua Li16d56e22017-07-17 14:33:48 -07001511 alloc_behind_master_bio(r1_bio, bio);
Ming Lei8e58e322017-02-14 23:29:01 +08001512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07001514 md_bitmap_startwrite(bitmap, r1_bio->sector, r1_bio->sectors,
1515 test_bit(R1BIO_BehindIO, &r1_bio->state));
NeilBrown1f68f0c2011-07-28 11:31:48 +10001516 first_clone = 0;
1517 }
Ming Lei8e58e322017-02-14 23:29:01 +08001518
Shaohua Li16d56e22017-07-17 14:33:48 -07001519 if (r1_bio->behind_master_bio)
1520 mbio = bio_clone_fast(r1_bio->behind_master_bio,
Kent Overstreetafeee512018-05-20 18:25:52 -04001521 GFP_NOIO, &mddev->bio_set);
Shaohua Li16d56e22017-07-17 14:33:48 -07001522 else
Kent Overstreetafeee512018-05-20 18:25:52 -04001523 mbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set);
Ming Lei8e58e322017-02-14 23:29:01 +08001524
Ming Lei841c1312017-03-17 00:12:31 +08001525 if (r1_bio->behind_master_bio) {
Guoqing Jiang69df9cf2019-12-23 10:48:58 +01001526 if (test_bit(CollisionCheck, &rdev->flags))
Guoqing Jiangd0d2d8b2019-12-23 10:49:02 +01001527 wait_for_serialization(rdev, r1_bio);
Guoqing Jiang3e148a32019-06-19 17:30:46 +08001528 if (test_bit(WriteMostly, &rdev->flags))
NeilBrown4b6d2872005-09-09 16:23:47 -07001529 atomic_inc(&r1_bio->behind_remaining);
Guoqing Jiang69df9cf2019-12-23 10:48:58 +01001530 } else if (mddev->serialize_policy)
Guoqing Jiangd0d2d8b2019-12-23 10:49:02 +01001531 wait_for_serialization(rdev, r1_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -07001532
NeilBrown1f68f0c2011-07-28 11:31:48 +10001533 r1_bio->bios[i] = mbio;
1534
Kent Overstreet4f024f32013-10-11 15:44:27 -07001535 mbio->bi_iter.bi_sector = (r1_bio->sector +
NeilBrown1f68f0c2011-07-28 11:31:48 +10001536 conf->mirrors[i].rdev->data_offset);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001537 bio_set_dev(mbio, conf->mirrors[i].rdev->bdev);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001538 mbio->bi_end_io = raid1_end_write_request;
Linus Torvaldsa682e002017-02-24 14:42:19 -08001539 mbio->bi_opf = bio_op(bio) | (bio->bi_opf & (REQ_SYNC | REQ_FUA));
NeilBrown212e7eb2016-11-18 16:16:12 +11001540 if (test_bit(FailFast, &conf->mirrors[i].rdev->flags) &&
1541 !test_bit(WriteMostly, &conf->mirrors[i].rdev->flags) &&
1542 conf->raid_disks - mddev->degraded > 1)
1543 mbio->bi_opf |= MD_FAILFAST;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001544 mbio->bi_private = r1_bio;
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 atomic_inc(&r1_bio->remaining);
NeilBrownf54a9d02012-08-02 08:33:20 +10001547
NeilBrown109e3762016-11-18 13:22:04 +11001548 if (mddev->gendisk)
Christoph Hellwig74d46992017-08-23 19:10:32 +02001549 trace_block_bio_remap(mbio->bi_disk->queue,
NeilBrown109e3762016-11-18 13:22:04 +11001550 mbio, disk_devt(mddev->gendisk),
1551 r1_bio->sector);
1552 /* flush_pending_writes() needs access to the rdev so...*/
Christoph Hellwig74d46992017-08-23 19:10:32 +02001553 mbio->bi_disk = (void *)conf->mirrors[i].rdev;
NeilBrown109e3762016-11-18 13:22:04 +11001554
NeilBrownf54a9d02012-08-02 08:33:20 +10001555 cb = blk_check_plugged(raid1_unplug, mddev, sizeof(*plug));
1556 if (cb)
1557 plug = container_of(cb, struct raid1_plug_cb, cb);
1558 else
1559 plug = NULL;
NeilBrownf54a9d02012-08-02 08:33:20 +10001560 if (plug) {
1561 bio_list_add(&plug->pending, mbio);
1562 plug->pending_cnt++;
1563 } else {
Shaohua Li23b245c2017-05-10 08:47:11 -07001564 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownf54a9d02012-08-02 08:33:20 +10001565 bio_list_add(&conf->pending_bio_list, mbio);
1566 conf->pending_count++;
Shaohua Li23b245c2017-05-10 08:47:11 -07001567 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrownb357f042012-07-03 17:45:31 +10001568 md_wakeup_thread(mddev->thread);
Shaohua Li23b245c2017-05-10 08:47:11 -07001569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001571
NeilBrown079fa162011-09-10 17:21:23 +10001572 r1_bio_write_done(r1_bio);
1573
1574 /* In case raid1d snuck in to freeze_array */
1575 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
NeilBrowncc27b0c2017-06-05 16:49:39 +10001578static bool raid1_make_request(struct mddev *mddev, struct bio *bio)
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001579{
colyli@suse.defd768632017-02-18 03:05:56 +08001580 sector_t sectors;
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001581
David Jeffery775d7832019-09-16 13:15:14 -04001582 if (unlikely(bio->bi_opf & REQ_PREFLUSH)
1583 && md_flush_request(mddev, bio))
NeilBrowncc27b0c2017-06-05 16:49:39 +10001584 return true;
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001585
NeilBrownc230e7e2017-04-05 14:05:50 +10001586 /*
1587 * There is a limit to the maximum size, but
1588 * the read/write handler might find a lower limit
1589 * due to bad blocks. To avoid multiple splits,
1590 * we pass the maximum number of sectors down
1591 * and let the lower level perform the split.
1592 */
1593 sectors = align_to_barrier_unit_end(
1594 bio->bi_iter.bi_sector, bio_sectors(bio));
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001595
NeilBrownc230e7e2017-04-05 14:05:50 +10001596 if (bio_data_dir(bio) == READ)
NeilBrown689389a2017-04-05 14:05:50 +10001597 raid1_read_request(mddev, bio, sectors, NULL);
NeilBrowncc27b0c2017-06-05 16:49:39 +10001598 else {
1599 if (!md_write_start(mddev,bio))
1600 return false;
NeilBrownc230e7e2017-04-05 14:05:50 +10001601 raid1_write_request(mddev, bio, sectors);
NeilBrowncc27b0c2017-06-05 16:49:39 +10001602 }
1603 return true;
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001604}
1605
Shaohua Li849674e2016-01-20 13:52:20 -08001606static void raid1_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
NeilBrowne8096362011-10-11 16:49:05 +11001608 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 int i;
1610
1611 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown11ce99e2006-10-03 01:15:52 -07001612 conf->raid_disks - mddev->degraded);
NeilBrownddac7c72006-08-31 21:27:36 -07001613 rcu_read_lock();
1614 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001615 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 seq_printf(seq, "%s",
NeilBrownddac7c72006-08-31 21:27:36 -07001617 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1618 }
1619 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 seq_printf(seq, "]");
1621}
1622
Shaohua Li849674e2016-01-20 13:52:20 -08001623static void raid1_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
1625 char b[BDEVNAME_SIZE];
NeilBrowne8096362011-10-11 16:49:05 +11001626 struct r1conf *conf = mddev->private;
NeilBrown423f04d2015-07-27 11:48:52 +10001627 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 /*
1630 * If it is not operational, then we have already marked it as dead
Guoqing Jiang9a567842019-07-24 11:09:19 +02001631 * else if it is the last working disks with "fail_last_dev == false",
1632 * ignore the error, let the next level up know.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 * else mark the drive as failed
1634 */
NeilBrown2e52d442016-11-18 16:16:12 +11001635 spin_lock_irqsave(&conf->device_lock, flags);
Guoqing Jiang9a567842019-07-24 11:09:19 +02001636 if (test_bit(In_sync, &rdev->flags) && !mddev->fail_last_dev
NeilBrown4044ba52009-01-09 08:31:11 +11001637 && (conf->raid_disks - mddev->degraded) == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 /*
1639 * Don't fail the drive, act as though we were just a
NeilBrown4044ba52009-01-09 08:31:11 +11001640 * normal single drive.
1641 * However don't try a recovery from this drive as
1642 * it is very likely to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 */
NeilBrown53890422011-07-27 11:00:36 +10001644 conf->recovery_disabled = mddev->recovery_disabled;
NeilBrown2e52d442016-11-18 16:16:12 +11001645 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return;
NeilBrown4044ba52009-01-09 08:31:11 +11001647 }
NeilBrownde393cd2011-07-28 11:31:48 +10001648 set_bit(Blocked, &rdev->flags);
Yufen Yuebda52f2019-02-01 10:45:01 +08001649 if (test_and_clear_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 mddev->degraded++;
Yufen Yuebda52f2019-02-01 10:45:01 +08001651 set_bit(Faulty, &rdev->flags);
NeilBrown423f04d2015-07-27 11:48:52 +10001652 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown2446dba2014-07-31 10:16:29 +10001653 /*
1654 * if recovery is running, make sure it aborts.
1655 */
1656 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08001657 set_mask_bits(&mddev->sb_flags, 0,
1658 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown1d41c212016-11-02 14:16:50 +11001659 pr_crit("md/raid1:%s: Disk failure on %s, disabling device.\n"
1660 "md/raid1:%s: Operation continuing on %d devices.\n",
1661 mdname(mddev), bdevname(rdev->bdev, b),
1662 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
NeilBrowne8096362011-10-11 16:49:05 +11001665static void print_conf(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
NeilBrown1d41c212016-11-02 14:16:50 +11001669 pr_debug("RAID1 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (!conf) {
NeilBrown1d41c212016-11-02 14:16:50 +11001671 pr_debug("(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return;
1673 }
NeilBrown1d41c212016-11-02 14:16:50 +11001674 pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1675 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
NeilBrownddac7c72006-08-31 21:27:36 -07001677 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 for (i = 0; i < conf->raid_disks; i++) {
1679 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11001680 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownddac7c72006-08-31 21:27:36 -07001681 if (rdev)
NeilBrown1d41c212016-11-02 14:16:50 +11001682 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1683 i, !test_bit(In_sync, &rdev->flags),
1684 !test_bit(Faulty, &rdev->flags),
1685 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
NeilBrownddac7c72006-08-31 21:27:36 -07001687 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
NeilBrowne8096362011-10-11 16:49:05 +11001690static void close_sync(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Nate Daileyf6eca2d2017-10-17 08:17:03 -04001692 int idx;
1693
1694 for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++) {
1695 _wait_barrier(conf, idx);
1696 _allow_barrier(conf, idx);
1697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Kent Overstreetafeee512018-05-20 18:25:52 -04001699 mempool_exit(&conf->r1buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
NeilBrownfd01b882011-10-11 16:47:53 +11001702static int raid1_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
1704 int i;
NeilBrowne8096362011-10-11 16:49:05 +11001705 struct r1conf *conf = mddev->private;
NeilBrown6b965622010-08-18 11:56:59 +10001706 int count = 0;
1707 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 /*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001710 * Find all failed disks within the RAID1 configuration
NeilBrownddac7c72006-08-31 21:27:36 -07001711 * and mark them readable.
1712 * Called under mddev lock, so rcu protection not needed.
NeilBrown423f04d2015-07-27 11:48:52 +10001713 * device_lock used to avoid races with raid1_end_read_request
1714 * which expects 'In_sync' flags and ->degraded to be consistent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 */
NeilBrown423f04d2015-07-27 11:48:52 +10001716 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001718 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrown8c7a2c22011-12-23 10:17:57 +11001719 struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev;
1720 if (repl
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001721 && !test_bit(Candidate, &repl->flags)
NeilBrown8c7a2c22011-12-23 10:17:57 +11001722 && repl->recovery_offset == MaxSector
1723 && !test_bit(Faulty, &repl->flags)
1724 && !test_and_set_bit(In_sync, &repl->flags)) {
1725 /* replacement has just become active */
1726 if (!rdev ||
1727 !test_and_clear_bit(In_sync, &rdev->flags))
1728 count++;
1729 if (rdev) {
1730 /* Replaced device not technically
1731 * faulty, but we need to be sure
1732 * it gets removed and never re-added
1733 */
1734 set_bit(Faulty, &rdev->flags);
1735 sysfs_notify_dirent_safe(
1736 rdev->sysfs_state);
1737 }
1738 }
NeilBrownddac7c72006-08-31 21:27:36 -07001739 if (rdev
Lukasz Dorau61e49472013-10-24 12:55:17 +11001740 && rdev->recovery_offset == MaxSector
NeilBrownddac7c72006-08-31 21:27:36 -07001741 && !test_bit(Faulty, &rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001742 && !test_and_set_bit(In_sync, &rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001743 count++;
Jonathan Brassow654e8b52011-07-27 11:00:36 +10001744 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746 }
NeilBrown6b965622010-08-18 11:56:59 +10001747 mddev->degraded -= count;
1748 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001751 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
NeilBrownfd01b882011-10-11 16:47:53 +11001754static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
NeilBrowne8096362011-10-11 16:49:05 +11001756 struct r1conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001757 int err = -EEXIST;
NeilBrown41158c72005-06-21 17:17:25 -07001758 int mirror = 0;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001759 struct raid1_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10001760 int first = 0;
NeilBrown30194632011-12-23 10:17:56 +11001761 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
NeilBrown53890422011-07-27 11:00:36 +10001763 if (mddev->recovery_disabled == conf->recovery_disabled)
1764 return -EBUSY;
1765
Dan Williams1501efa2016-01-13 16:00:07 -08001766 if (md_integrity_add_rdev(rdev, mddev))
1767 return -ENXIO;
1768
Neil Brown6c2fce22008-06-28 08:31:31 +10001769 if (rdev->raid_disk >= 0)
1770 first = last = rdev->raid_disk;
1771
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05001772 /*
1773 * find the disk ... but prefer rdev->saved_raid_disk
1774 * if possible.
1775 */
1776 if (rdev->saved_raid_disk >= 0 &&
1777 rdev->saved_raid_disk >= first &&
Shaohua Li9e753ba2018-10-14 17:05:07 -07001778 rdev->saved_raid_disk < conf->raid_disks &&
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05001779 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1780 first = last = rdev->saved_raid_disk;
1781
NeilBrown7ef449d2011-12-23 10:17:57 +11001782 for (mirror = first; mirror <= last; mirror++) {
Yufen Yuebfeb442019-06-14 15:41:08 -07001783 p = conf->mirrors + mirror;
NeilBrown7ef449d2011-12-23 10:17:57 +11001784 if (!p->rdev) {
Jonathan Brassow9092c022013-05-02 14:19:24 -05001785 if (mddev->gendisk)
1786 disk_stack_limits(mddev->gendisk, rdev->bdev,
1787 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 p->head_position = 0;
1790 rdev->raid_disk = mirror;
Neil Brown199050e2008-06-28 08:31:33 +10001791 err = 0;
NeilBrown6aea114a2005-11-28 13:44:13 -08001792 /* As all devices are equivalent, we don't need a full recovery
1793 * if this was recently any drive of the array
1794 */
1795 if (rdev->saved_raid_disk < 0)
NeilBrown41158c72005-06-21 17:17:25 -07001796 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001797 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 break;
1799 }
NeilBrown7ef449d2011-12-23 10:17:57 +11001800 if (test_bit(WantReplacement, &p->rdev->flags) &&
1801 p[conf->raid_disks].rdev == NULL) {
1802 /* Add this device as a replacement */
1803 clear_bit(In_sync, &rdev->flags);
1804 set_bit(Replacement, &rdev->flags);
1805 rdev->raid_disk = mirror;
1806 err = 0;
1807 conf->fullsync = 1;
1808 rcu_assign_pointer(p[conf->raid_disks].rdev, rdev);
1809 break;
1810 }
1811 }
Jonathan Brassow9092c022013-05-02 14:19:24 -05001812 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
Bart Van Assche8b904b52018-03-07 17:10:10 -08001813 blk_queue_flag_set(QUEUE_FLAG_DISCARD, mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001815 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
NeilBrownb8321b62011-12-23 10:17:51 +11001818static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
NeilBrowne8096362011-10-11 16:49:05 +11001820 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001822 int number = rdev->raid_disk;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001823 struct raid1_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
NeilBrownb014f142011-12-23 10:17:56 +11001825 if (rdev != p->rdev)
1826 p = conf->mirrors + conf->raid_disks + number;
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 print_conf(conf);
NeilBrownb8321b62011-12-23 10:17:51 +11001829 if (rdev == p->rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001830 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 atomic_read(&rdev->nr_pending)) {
1832 err = -EBUSY;
1833 goto abort;
1834 }
NeilBrown046abee2010-10-26 15:46:20 +11001835 /* Only remove non-faulty devices if recovery
NeilBrowndfc70642008-05-23 13:04:39 -07001836 * is not possible.
1837 */
1838 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown53890422011-07-27 11:00:36 +10001839 mddev->recovery_disabled != conf->recovery_disabled &&
NeilBrowndfc70642008-05-23 13:04:39 -07001840 mddev->degraded < conf->raid_disks) {
1841 err = -EBUSY;
1842 goto abort;
1843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 p->rdev = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10001845 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1846 synchronize_rcu();
1847 if (atomic_read(&rdev->nr_pending)) {
1848 /* lost the race, try later */
1849 err = -EBUSY;
1850 p->rdev = rdev;
1851 goto abort;
1852 }
1853 }
1854 if (conf->mirrors[conf->raid_disks + number].rdev) {
NeilBrown8c7a2c22011-12-23 10:17:57 +11001855 /* We just removed a device that is being replaced.
1856 * Move down the replacement. We drain all IO before
1857 * doing this to avoid confusion.
1858 */
1859 struct md_rdev *repl =
1860 conf->mirrors[conf->raid_disks + number].rdev;
NeilBrowne2d59922013-06-12 11:01:22 +10001861 freeze_array(conf, 0);
Yufen Yu3de59bb2018-02-24 12:05:56 +08001862 if (atomic_read(&repl->nr_pending)) {
1863 /* It means that some queued IO of retry_list
1864 * hold repl. Thus, we cannot set replacement
1865 * as NULL, avoiding rdev NULL pointer
1866 * dereference in sync_request_write and
1867 * handle_write_finished.
1868 */
1869 err = -EBUSY;
1870 unfreeze_array(conf);
1871 goto abort;
1872 }
NeilBrown8c7a2c22011-12-23 10:17:57 +11001873 clear_bit(Replacement, &repl->flags);
1874 p->rdev = repl;
1875 conf->mirrors[conf->raid_disks + number].rdev = NULL;
NeilBrowne2d59922013-06-12 11:01:22 +10001876 unfreeze_array(conf);
Guoqing Jiange5bc9c32017-04-24 15:58:04 +08001877 }
1878
1879 clear_bit(WantReplacement, &rdev->flags);
Martin K. Petersena91a2782011-03-17 11:11:05 +01001880 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
1882abort:
1883
1884 print_conf(conf);
1885 return err;
1886}
1887
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001888static void end_sync_read(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Ming Lei98d30c52017-03-17 00:12:26 +08001890 struct r1bio *r1_bio = get_resync_r1bio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
NeilBrown0fc280f2011-10-07 14:22:55 +11001892 update_head_pos(r1_bio->read_disk, r1_bio);
Namhyung Kimba3ae3b2011-10-07 14:22:53 +11001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 /*
1895 * we have read a block, now it needs to be re-written,
1896 * or re-read if the read failed.
1897 * We don't do much here, just schedule handling by raid1d
1898 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001899 if (!bio->bi_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrownd11c1712006-01-06 00:20:26 -08001901
1902 if (atomic_dec_and_test(&r1_bio->remaining))
1903 reschedule_retry(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
1905
Nate Daileydfcc34c2019-02-07 14:19:01 -05001906static void abort_sync_write(struct mddev *mddev, struct r1bio *r1_bio)
1907{
1908 sector_t sync_blocks = 0;
1909 sector_t s = r1_bio->sector;
1910 long sectors_to_go = r1_bio->sectors;
1911
1912 /* make sure these bits don't get cleared. */
1913 do {
1914 md_bitmap_end_sync(mddev->bitmap, s, &sync_blocks, 1);
1915 s += sync_blocks;
1916 sectors_to_go -= sync_blocks;
1917 } while (sectors_to_go > 0);
1918}
1919
Hou Tao449808a2019-07-27 14:02:58 +08001920static void put_sync_write_buf(struct r1bio *r1_bio, int uptodate)
1921{
1922 if (atomic_dec_and_test(&r1_bio->remaining)) {
1923 struct mddev *mddev = r1_bio->mddev;
1924 int s = r1_bio->sectors;
1925
1926 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
1927 test_bit(R1BIO_WriteError, &r1_bio->state))
1928 reschedule_retry(r1_bio);
1929 else {
1930 put_buf(r1_bio);
1931 md_done_sync(mddev, s, uptodate);
1932 }
1933 }
1934}
1935
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001936static void end_sync_write(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001938 int uptodate = !bio->bi_status;
Ming Lei98d30c52017-03-17 00:12:26 +08001939 struct r1bio *r1_bio = get_resync_r1bio(bio);
NeilBrownfd01b882011-10-11 16:47:53 +11001940 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11001941 struct r1conf *conf = mddev->private;
NeilBrown4367af52011-07-28 11:31:49 +10001942 sector_t first_bad;
1943 int bad_sectors;
NeilBrown854abd72016-06-02 16:19:52 +10001944 struct md_rdev *rdev = conf->mirrors[find_bio_disk(r1_bio, bio)].rdev;
Namhyung Kimba3ae3b2011-10-07 14:22:53 +11001945
NeilBrown6b1117d2006-03-31 02:31:57 -08001946 if (!uptodate) {
Nate Daileydfcc34c2019-02-07 14:19:01 -05001947 abort_sync_write(mddev, r1_bio);
NeilBrown854abd72016-06-02 16:19:52 +10001948 set_bit(WriteErrorSeen, &rdev->flags);
1949 if (!test_and_set_bit(WantReplacement, &rdev->flags))
NeilBrown19d67162011-12-23 10:17:57 +11001950 set_bit(MD_RECOVERY_NEEDED, &
1951 mddev->recovery);
NeilBrownd8f05d22011-07-28 11:33:00 +10001952 set_bit(R1BIO_WriteError, &r1_bio->state);
NeilBrown854abd72016-06-02 16:19:52 +10001953 } else if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
NeilBrown3a9f28a2011-07-28 11:33:42 +10001954 &first_bad, &bad_sectors) &&
1955 !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
1956 r1_bio->sector,
1957 r1_bio->sectors,
1958 &first_bad, &bad_sectors)
1959 )
NeilBrown4367af52011-07-28 11:31:49 +10001960 set_bit(R1BIO_MadeGood, &r1_bio->state);
NeilBrowne3b97032005-08-04 12:53:34 -07001961
Hou Tao449808a2019-07-27 14:02:58 +08001962 put_sync_write_buf(r1_bio, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
NeilBrown3cb03002011-10-11 16:45:26 +11001965static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrownd8f05d22011-07-28 11:33:00 +10001966 int sectors, struct page *page, int rw)
1967{
Mike Christie796a5cf2016-06-05 14:32:07 -05001968 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
NeilBrownd8f05d22011-07-28 11:33:00 +10001969 /* success */
1970 return 1;
NeilBrown19d67162011-12-23 10:17:57 +11001971 if (rw == WRITE) {
NeilBrownd8f05d22011-07-28 11:33:00 +10001972 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrown19d67162011-12-23 10:17:57 +11001973 if (!test_and_set_bit(WantReplacement,
1974 &rdev->flags))
1975 set_bit(MD_RECOVERY_NEEDED, &
1976 rdev->mddev->recovery);
1977 }
NeilBrownd8f05d22011-07-28 11:33:00 +10001978 /* need to record an error - either for the block or the device */
1979 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1980 md_error(rdev->mddev, rdev);
1981 return 0;
1982}
1983
NeilBrown9f2c9d12011-10-11 16:48:43 +11001984static int fix_sync_read_error(struct r1bio *r1_bio)
NeilBrowna68e5872011-05-11 14:40:44 +10001985{
1986 /* Try some synchronous reads of other devices to get
1987 * good data, much like with normal read errors. Only
1988 * read into the pages we already have so we don't
1989 * need to re-issue the read request.
1990 * We don't need to freeze the array, because being in an
1991 * active sync request, there is no normal IO, and
1992 * no overlapping syncs.
NeilBrown06f60382011-07-28 11:31:48 +10001993 * We don't need to check is_badblock() again as we
1994 * made sure that anything with a bad block in range
1995 * will have bi_end_io clear.
NeilBrowna68e5872011-05-11 14:40:44 +10001996 */
NeilBrownfd01b882011-10-11 16:47:53 +11001997 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11001998 struct r1conf *conf = mddev->private;
NeilBrowna68e5872011-05-11 14:40:44 +10001999 struct bio *bio = r1_bio->bios[r1_bio->read_disk];
Ming Lei44cf0f42017-03-17 00:12:27 +08002000 struct page **pages = get_resync_pages(bio)->pages;
NeilBrowna68e5872011-05-11 14:40:44 +10002001 sector_t sect = r1_bio->sector;
2002 int sectors = r1_bio->sectors;
2003 int idx = 0;
NeilBrown2e52d442016-11-18 16:16:12 +11002004 struct md_rdev *rdev;
2005
2006 rdev = conf->mirrors[r1_bio->read_disk].rdev;
2007 if (test_bit(FailFast, &rdev->flags)) {
2008 /* Don't try recovering from here - just fail it
2009 * ... unless it is the last working device of course */
2010 md_error(mddev, rdev);
2011 if (test_bit(Faulty, &rdev->flags))
2012 /* Don't try to read from here, but make sure
2013 * put_buf does it's thing
2014 */
2015 bio->bi_end_io = end_sync_write;
2016 }
NeilBrowna68e5872011-05-11 14:40:44 +10002017
2018 while(sectors) {
2019 int s = sectors;
2020 int d = r1_bio->read_disk;
2021 int success = 0;
NeilBrown78d7f5f2011-05-11 14:48:56 +10002022 int start;
NeilBrowna68e5872011-05-11 14:40:44 +10002023
2024 if (s > (PAGE_SIZE>>9))
2025 s = PAGE_SIZE >> 9;
2026 do {
2027 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
2028 /* No rcu protection needed here devices
2029 * can only be removed when no resync is
2030 * active, and resync is currently active
2031 */
2032 rdev = conf->mirrors[d].rdev;
Namhyung Kim9d3d8012011-07-27 11:00:36 +10002033 if (sync_page_io(rdev, sect, s<<9,
Ming Lei44cf0f42017-03-17 00:12:27 +08002034 pages[idx],
Mike Christie796a5cf2016-06-05 14:32:07 -05002035 REQ_OP_READ, 0, false)) {
NeilBrowna68e5872011-05-11 14:40:44 +10002036 success = 1;
2037 break;
2038 }
2039 }
2040 d++;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002041 if (d == conf->raid_disks * 2)
NeilBrowna68e5872011-05-11 14:40:44 +10002042 d = 0;
2043 } while (!success && d != r1_bio->read_disk);
2044
NeilBrown78d7f5f2011-05-11 14:48:56 +10002045 if (!success) {
NeilBrowna68e5872011-05-11 14:40:44 +10002046 char b[BDEVNAME_SIZE];
NeilBrown3a9f28a2011-07-28 11:33:42 +10002047 int abort = 0;
2048 /* Cannot read from anywhere, this block is lost.
2049 * Record a bad block on each device. If that doesn't
2050 * work just disable and interrupt the recovery.
2051 * Don't fail devices as that won't really help.
2052 */
NeilBrown1d41c212016-11-02 14:16:50 +11002053 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
Christoph Hellwig74d46992017-08-23 19:10:32 +02002054 mdname(mddev), bio_devname(bio, b),
NeilBrown1d41c212016-11-02 14:16:50 +11002055 (unsigned long long)r1_bio->sector);
NeilBrown8f19ccb2011-12-23 10:17:56 +11002056 for (d = 0; d < conf->raid_disks * 2; d++) {
NeilBrown3a9f28a2011-07-28 11:33:42 +10002057 rdev = conf->mirrors[d].rdev;
2058 if (!rdev || test_bit(Faulty, &rdev->flags))
2059 continue;
2060 if (!rdev_set_badblocks(rdev, sect, s, 0))
2061 abort = 1;
2062 }
2063 if (abort) {
NeilBrownd890fa22011-10-26 11:54:39 +11002064 conf->recovery_disabled =
2065 mddev->recovery_disabled;
NeilBrown3a9f28a2011-07-28 11:33:42 +10002066 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2067 md_done_sync(mddev, r1_bio->sectors, 0);
2068 put_buf(r1_bio);
2069 return 0;
2070 }
2071 /* Try next page */
2072 sectors -= s;
2073 sect += s;
2074 idx++;
2075 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10002076 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10002077
2078 start = d;
2079 /* write it back and re-read */
2080 while (d != r1_bio->read_disk) {
2081 if (d == 0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002082 d = conf->raid_disks * 2;
NeilBrown78d7f5f2011-05-11 14:48:56 +10002083 d--;
2084 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
2085 continue;
2086 rdev = conf->mirrors[d].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10002087 if (r1_sync_page_io(rdev, sect, s,
Ming Lei44cf0f42017-03-17 00:12:27 +08002088 pages[idx],
NeilBrownd8f05d22011-07-28 11:33:00 +10002089 WRITE) == 0) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10002090 r1_bio->bios[d]->bi_end_io = NULL;
2091 rdev_dec_pending(rdev, mddev);
Namhyung Kim9d3d8012011-07-27 11:00:36 +10002092 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10002093 }
2094 d = start;
2095 while (d != r1_bio->read_disk) {
2096 if (d == 0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002097 d = conf->raid_disks * 2;
NeilBrown78d7f5f2011-05-11 14:48:56 +10002098 d--;
2099 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
2100 continue;
2101 rdev = conf->mirrors[d].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10002102 if (r1_sync_page_io(rdev, sect, s,
Ming Lei44cf0f42017-03-17 00:12:27 +08002103 pages[idx],
NeilBrownd8f05d22011-07-28 11:33:00 +10002104 READ) != 0)
Namhyung Kim9d3d8012011-07-27 11:00:36 +10002105 atomic_add(s, &rdev->corrected_errors);
NeilBrown78d7f5f2011-05-11 14:48:56 +10002106 }
NeilBrowna68e5872011-05-11 14:40:44 +10002107 sectors -= s;
2108 sect += s;
2109 idx ++;
2110 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10002111 set_bit(R1BIO_Uptodate, &r1_bio->state);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002112 bio->bi_status = 0;
NeilBrowna68e5872011-05-11 14:40:44 +10002113 return 1;
2114}
2115
NeilBrownc95e6382014-09-09 13:54:11 +10002116static void process_checks(struct r1bio *r1_bio)
NeilBrowna68e5872011-05-11 14:40:44 +10002117{
2118 /* We have read all readable devices. If we haven't
2119 * got the block, then there is no hope left.
2120 * If we have, then we want to do a comparison
2121 * and skip the write if everything is the same.
2122 * If any blocks failed to read, then we need to
2123 * attempt an over-write
2124 */
NeilBrownfd01b882011-10-11 16:47:53 +11002125 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11002126 struct r1conf *conf = mddev->private;
NeilBrowna68e5872011-05-11 14:40:44 +10002127 int primary;
2128 int i;
majianpengf4380a92012-04-12 16:04:47 +10002129 int vcnt;
NeilBrowna68e5872011-05-11 14:40:44 +10002130
NeilBrown30bc9b52013-07-17 15:19:29 +10002131 /* Fix variable parts of all bios */
2132 vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9);
2133 for (i = 0; i < conf->raid_disks * 2; i++) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002134 blk_status_t status;
NeilBrown30bc9b52013-07-17 15:19:29 +10002135 struct bio *b = r1_bio->bios[i];
Ming Lei98d30c52017-03-17 00:12:26 +08002136 struct resync_pages *rp = get_resync_pages(b);
NeilBrown30bc9b52013-07-17 15:19:29 +10002137 if (b->bi_end_io != end_sync_read)
2138 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002139 /* fixup the bio for reuse, but preserve errno */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002140 status = b->bi_status;
NeilBrown30bc9b52013-07-17 15:19:29 +10002141 bio_reset(b);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002142 b->bi_status = status;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002143 b->bi_iter.bi_sector = r1_bio->sector +
NeilBrown30bc9b52013-07-17 15:19:29 +10002144 conf->mirrors[i].rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002145 bio_set_dev(b, conf->mirrors[i].rdev->bdev);
NeilBrown30bc9b52013-07-17 15:19:29 +10002146 b->bi_end_io = end_sync_read;
Ming Lei98d30c52017-03-17 00:12:26 +08002147 rp->raid_bio = r1_bio;
2148 b->bi_private = rp;
NeilBrown30bc9b52013-07-17 15:19:29 +10002149
Ming Leifb0eb5d2017-07-14 16:14:43 +08002150 /* initialize bvec table again */
2151 md_bio_reset_resync_pages(b, rp, r1_bio->sectors << 9);
NeilBrown30bc9b52013-07-17 15:19:29 +10002152 }
NeilBrown8f19ccb2011-12-23 10:17:56 +11002153 for (primary = 0; primary < conf->raid_disks * 2; primary++)
NeilBrowna68e5872011-05-11 14:40:44 +10002154 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002155 !r1_bio->bios[primary]->bi_status) {
NeilBrowna68e5872011-05-11 14:40:44 +10002156 r1_bio->bios[primary]->bi_end_io = NULL;
2157 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
2158 break;
2159 }
2160 r1_bio->read_disk = primary;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002161 for (i = 0; i < conf->raid_disks * 2; i++) {
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02002162 int j = 0;
NeilBrown78d7f5f2011-05-11 14:48:56 +10002163 struct bio *pbio = r1_bio->bios[primary];
2164 struct bio *sbio = r1_bio->bios[i];
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002165 blk_status_t status = sbio->bi_status;
Ming Lei44cf0f42017-03-17 00:12:27 +08002166 struct page **ppages = get_resync_pages(pbio)->pages;
2167 struct page **spages = get_resync_pages(sbio)->pages;
Ming Lei60928a92017-03-17 00:12:28 +08002168 struct bio_vec *bi;
Ming Lei8fc04e6e2017-03-28 16:17:55 +08002169 int page_len[RESYNC_PAGES] = { 0 };
Ming Lei6dc4f102019-02-15 19:13:19 +08002170 struct bvec_iter_all iter_all;
NeilBrowna68e5872011-05-11 14:40:44 +10002171
Kent Overstreet2aabaa62012-09-11 11:26:12 -07002172 if (sbio->bi_end_io != end_sync_read)
NeilBrown78d7f5f2011-05-11 14:48:56 +10002173 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002174 /* Now we can 'fixup' the error value */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002175 sbio->bi_status = 0;
NeilBrowna68e5872011-05-11 14:40:44 +10002176
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02002177 bio_for_each_segment_all(bi, sbio, iter_all)
2178 page_len[j++] = bi->bv_len;
Ming Lei60928a92017-03-17 00:12:28 +08002179
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002180 if (!status) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10002181 for (j = vcnt; j-- ; ) {
Ming Lei44cf0f42017-03-17 00:12:27 +08002182 if (memcmp(page_address(ppages[j]),
2183 page_address(spages[j]),
Ming Lei60928a92017-03-17 00:12:28 +08002184 page_len[j]))
NeilBrown78d7f5f2011-05-11 14:48:56 +10002185 break;
NeilBrowna68e5872011-05-11 14:40:44 +10002186 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10002187 } else
2188 j = 0;
2189 if (j >= 0)
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002190 atomic64_add(r1_bio->sectors, &mddev->resync_mismatches);
NeilBrown78d7f5f2011-05-11 14:48:56 +10002191 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002192 && !status)) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10002193 /* No need to write to this device. */
2194 sbio->bi_end_io = NULL;
2195 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
2196 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10002197 }
Kent Overstreetd3b45c22012-09-10 13:49:33 -07002198
2199 bio_copy_data(sbio, pbio);
NeilBrown78d7f5f2011-05-11 14:48:56 +10002200 }
NeilBrowna68e5872011-05-11 14:40:44 +10002201}
2202
NeilBrown9f2c9d12011-10-11 16:48:43 +11002203static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
NeilBrowne8096362011-10-11 16:49:05 +11002205 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 int i;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002207 int disks = conf->raid_disks * 2;
Guoqing Jiang037d2ff2017-06-15 17:00:25 +08002208 struct bio *wbio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
NeilBrowna68e5872011-05-11 14:40:44 +10002210 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
2211 /* ouch - failed to read all of that. */
2212 if (!fix_sync_read_error(r1_bio))
2213 return;
NeilBrown7ca78d52011-05-11 14:50:37 +10002214
2215 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrownc95e6382014-09-09 13:54:11 +10002216 process_checks(r1_bio);
2217
NeilBrownd11c1712006-01-06 00:20:26 -08002218 /*
2219 * schedule writes
2220 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 atomic_set(&r1_bio->remaining, 1);
2222 for (i = 0; i < disks ; i++) {
2223 wbio = r1_bio->bios[i];
NeilBrown3e198f72006-01-06 00:20:21 -08002224 if (wbio->bi_end_io == NULL ||
2225 (wbio->bi_end_io == end_sync_read &&
2226 (i == r1_bio->read_disk ||
2227 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 continue;
Nate Daileydfcc34c2019-02-07 14:19:01 -05002229 if (test_bit(Faulty, &conf->mirrors[i].rdev->flags)) {
2230 abort_sync_write(mddev, r1_bio);
NeilBrown0c9d5b12017-04-06 12:06:37 +10002231 continue;
Nate Daileydfcc34c2019-02-07 14:19:01 -05002232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Mike Christie796a5cf2016-06-05 14:32:07 -05002234 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
NeilBrown212e7eb2016-11-18 16:16:12 +11002235 if (test_bit(FailFast, &conf->mirrors[i].rdev->flags))
2236 wbio->bi_opf |= MD_FAILFAST;
2237
NeilBrown3e198f72006-01-06 00:20:21 -08002238 wbio->bi_end_io = end_sync_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 atomic_inc(&r1_bio->remaining);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002240 md_sync_acct(conf->mirrors[i].rdev->bdev, bio_sectors(wbio));
NeilBrown191ea9b2005-06-21 17:17:23 -07002241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 generic_make_request(wbio);
2243 }
2244
Hou Tao449808a2019-07-27 14:02:58 +08002245 put_sync_write_buf(r1_bio, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
2248/*
2249 * This is a kernel thread which:
2250 *
2251 * 1. Retries failed read operations on working mirrors.
2252 * 2. Updates the raid superblock when problems encounter.
NeilBrownd2eb35a2011-07-28 11:31:48 +10002253 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 */
2255
NeilBrowne8096362011-10-11 16:49:05 +11002256static void fix_read_error(struct r1conf *conf, int read_disk,
NeilBrown867868f2006-10-03 01:15:51 -07002257 sector_t sect, int sectors)
2258{
NeilBrownfd01b882011-10-11 16:47:53 +11002259 struct mddev *mddev = conf->mddev;
NeilBrown867868f2006-10-03 01:15:51 -07002260 while(sectors) {
2261 int s = sectors;
2262 int d = read_disk;
2263 int success = 0;
2264 int start;
NeilBrown3cb03002011-10-11 16:45:26 +11002265 struct md_rdev *rdev;
NeilBrown867868f2006-10-03 01:15:51 -07002266
2267 if (s > (PAGE_SIZE>>9))
2268 s = PAGE_SIZE >> 9;
2269
2270 do {
NeilBrownd2eb35a2011-07-28 11:31:48 +10002271 sector_t first_bad;
2272 int bad_sectors;
2273
NeilBrown707a6a42016-06-02 16:19:52 +10002274 rcu_read_lock();
2275 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002276 if (rdev &&
majianpengda8840a2012-05-22 13:55:03 +10002277 (test_bit(In_sync, &rdev->flags) ||
2278 (!test_bit(Faulty, &rdev->flags) &&
2279 rdev->recovery_offset >= sect + s)) &&
NeilBrownd2eb35a2011-07-28 11:31:48 +10002280 is_badblock(rdev, sect, s,
NeilBrown707a6a42016-06-02 16:19:52 +10002281 &first_bad, &bad_sectors) == 0) {
2282 atomic_inc(&rdev->nr_pending);
2283 rcu_read_unlock();
2284 if (sync_page_io(rdev, sect, s<<9,
Mike Christie796a5cf2016-06-05 14:32:07 -05002285 conf->tmppage, REQ_OP_READ, 0, false))
NeilBrown707a6a42016-06-02 16:19:52 +10002286 success = 1;
2287 rdev_dec_pending(rdev, mddev);
2288 if (success)
2289 break;
2290 } else
2291 rcu_read_unlock();
2292 d++;
2293 if (d == conf->raid_disks * 2)
2294 d = 0;
NeilBrown867868f2006-10-03 01:15:51 -07002295 } while (!success && d != read_disk);
2296
2297 if (!success) {
NeilBrownd8f05d22011-07-28 11:33:00 +10002298 /* Cannot read from anywhere - mark it bad */
NeilBrown3cb03002011-10-11 16:45:26 +11002299 struct md_rdev *rdev = conf->mirrors[read_disk].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10002300 if (!rdev_set_badblocks(rdev, sect, s, 0))
2301 md_error(mddev, rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002302 break;
2303 }
2304 /* write it back and re-read */
2305 start = d;
2306 while (d != read_disk) {
2307 if (d==0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002308 d = conf->raid_disks * 2;
NeilBrown867868f2006-10-03 01:15:51 -07002309 d--;
NeilBrown707a6a42016-06-02 16:19:52 +10002310 rcu_read_lock();
2311 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002312 if (rdev &&
NeilBrown707a6a42016-06-02 16:19:52 +10002313 !test_bit(Faulty, &rdev->flags)) {
2314 atomic_inc(&rdev->nr_pending);
2315 rcu_read_unlock();
NeilBrownd8f05d22011-07-28 11:33:00 +10002316 r1_sync_page_io(rdev, sect, s,
2317 conf->tmppage, WRITE);
NeilBrown707a6a42016-06-02 16:19:52 +10002318 rdev_dec_pending(rdev, mddev);
2319 } else
2320 rcu_read_unlock();
NeilBrown867868f2006-10-03 01:15:51 -07002321 }
2322 d = start;
2323 while (d != read_disk) {
2324 char b[BDEVNAME_SIZE];
2325 if (d==0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002326 d = conf->raid_disks * 2;
NeilBrown867868f2006-10-03 01:15:51 -07002327 d--;
NeilBrown707a6a42016-06-02 16:19:52 +10002328 rcu_read_lock();
2329 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002330 if (rdev &&
NeilBrownb8cb6b42014-09-18 11:09:04 +10002331 !test_bit(Faulty, &rdev->flags)) {
NeilBrown707a6a42016-06-02 16:19:52 +10002332 atomic_inc(&rdev->nr_pending);
2333 rcu_read_unlock();
NeilBrownd8f05d22011-07-28 11:33:00 +10002334 if (r1_sync_page_io(rdev, sect, s,
2335 conf->tmppage, READ)) {
NeilBrown867868f2006-10-03 01:15:51 -07002336 atomic_add(s, &rdev->corrected_errors);
NeilBrown1d41c212016-11-02 14:16:50 +11002337 pr_info("md/raid1:%s: read error corrected (%d sectors at %llu on %s)\n",
2338 mdname(mddev), s,
2339 (unsigned long long)(sect +
2340 rdev->data_offset),
2341 bdevname(rdev->bdev, b));
NeilBrown867868f2006-10-03 01:15:51 -07002342 }
NeilBrown707a6a42016-06-02 16:19:52 +10002343 rdev_dec_pending(rdev, mddev);
2344 } else
2345 rcu_read_unlock();
NeilBrown867868f2006-10-03 01:15:51 -07002346 }
2347 sectors -= s;
2348 sect += s;
2349 }
2350}
2351
NeilBrown9f2c9d12011-10-11 16:48:43 +11002352static int narrow_write_error(struct r1bio *r1_bio, int i)
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002353{
NeilBrownfd01b882011-10-11 16:47:53 +11002354 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11002355 struct r1conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002356 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002357
2358 /* bio has the data to be written to device 'i' where
2359 * we just recently had a write error.
2360 * We repeatedly clone the bio and trim down to one block,
2361 * then try the write. Where the write fails we record
2362 * a bad block.
2363 * It is conceivable that the bio doesn't exactly align with
2364 * blocks. We must handle this somehow.
2365 *
2366 * We currently own a reference on the rdev.
2367 */
2368
2369 int block_sectors;
2370 sector_t sector;
2371 int sectors;
2372 int sect_to_write = r1_bio->sectors;
2373 int ok = 1;
2374
2375 if (rdev->badblocks.shift < 0)
2376 return 0;
2377
Nate Daileyab713cd2015-02-12 12:02:09 -05002378 block_sectors = roundup(1 << rdev->badblocks.shift,
2379 bdev_logical_block_size(rdev->bdev) >> 9);
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002380 sector = r1_bio->sector;
2381 sectors = ((sector + block_sectors)
2382 & ~(sector_t)(block_sectors - 1))
2383 - sector;
2384
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002385 while (sect_to_write) {
2386 struct bio *wbio;
2387 if (sectors > sect_to_write)
2388 sectors = sect_to_write;
2389 /* Write at 'sector' for 'sectors'*/
2390
Kent Overstreetb7838632012-09-10 15:17:11 -07002391 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
Ming Lei841c1312017-03-17 00:12:31 +08002392 wbio = bio_clone_fast(r1_bio->behind_master_bio,
2393 GFP_NOIO,
Kent Overstreetafeee512018-05-20 18:25:52 -04002394 &mddev->bio_set);
Kent Overstreetb7838632012-09-10 15:17:11 -07002395 } else {
Ming Leid7a10302017-02-14 23:29:03 +08002396 wbio = bio_clone_fast(r1_bio->master_bio, GFP_NOIO,
Kent Overstreetafeee512018-05-20 18:25:52 -04002397 &mddev->bio_set);
Kent Overstreetb7838632012-09-10 15:17:11 -07002398 }
2399
Mike Christie796a5cf2016-06-05 14:32:07 -05002400 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002401 wbio->bi_iter.bi_sector = r1_bio->sector;
2402 wbio->bi_iter.bi_size = r1_bio->sectors << 9;
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002403
Kent Overstreet6678d832013-08-07 11:14:32 -07002404 bio_trim(wbio, sector - r1_bio->sector, sectors);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002405 wbio->bi_iter.bi_sector += rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002406 bio_set_dev(wbio, rdev->bdev);
Mike Christie4e49ea42016-06-05 14:31:41 -05002407
2408 if (submit_bio_wait(wbio) < 0)
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002409 /* failure! */
2410 ok = rdev_set_badblocks(rdev, sector,
2411 sectors, 0)
2412 && ok;
2413
2414 bio_put(wbio);
2415 sect_to_write -= sectors;
2416 sector += sectors;
2417 sectors = block_sectors;
2418 }
2419 return ok;
2420}
2421
NeilBrowne8096362011-10-11 16:49:05 +11002422static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002423{
2424 int m;
2425 int s = r1_bio->sectors;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002426 for (m = 0; m < conf->raid_disks * 2 ; m++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002427 struct md_rdev *rdev = conf->mirrors[m].rdev;
NeilBrown62096bc2011-07-28 11:38:13 +10002428 struct bio *bio = r1_bio->bios[m];
2429 if (bio->bi_end_io == NULL)
2430 continue;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002431 if (!bio->bi_status &&
NeilBrown62096bc2011-07-28 11:38:13 +10002432 test_bit(R1BIO_MadeGood, &r1_bio->state)) {
NeilBrownc6563a82012-05-21 09:27:00 +10002433 rdev_clear_badblocks(rdev, r1_bio->sector, s, 0);
NeilBrown62096bc2011-07-28 11:38:13 +10002434 }
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002435 if (bio->bi_status &&
NeilBrown62096bc2011-07-28 11:38:13 +10002436 test_bit(R1BIO_WriteError, &r1_bio->state)) {
2437 if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
2438 md_error(conf->mddev, rdev);
2439 }
2440 }
2441 put_buf(r1_bio);
2442 md_done_sync(conf->mddev, s, 1);
2443}
2444
NeilBrowne8096362011-10-11 16:49:05 +11002445static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002446{
colyli@suse.defd768632017-02-18 03:05:56 +08002447 int m, idx;
NeilBrown55ce74d2015-08-14 11:11:10 +10002448 bool fail = false;
colyli@suse.defd768632017-02-18 03:05:56 +08002449
NeilBrown8f19ccb2011-12-23 10:17:56 +11002450 for (m = 0; m < conf->raid_disks * 2 ; m++)
NeilBrown62096bc2011-07-28 11:38:13 +10002451 if (r1_bio->bios[m] == IO_MADE_GOOD) {
NeilBrown3cb03002011-10-11 16:45:26 +11002452 struct md_rdev *rdev = conf->mirrors[m].rdev;
NeilBrown62096bc2011-07-28 11:38:13 +10002453 rdev_clear_badblocks(rdev,
2454 r1_bio->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10002455 r1_bio->sectors, 0);
NeilBrown62096bc2011-07-28 11:38:13 +10002456 rdev_dec_pending(rdev, conf->mddev);
2457 } else if (r1_bio->bios[m] != NULL) {
2458 /* This drive got a write error. We need to
2459 * narrow down and record precise write
2460 * errors.
2461 */
NeilBrown55ce74d2015-08-14 11:11:10 +10002462 fail = true;
NeilBrown62096bc2011-07-28 11:38:13 +10002463 if (!narrow_write_error(r1_bio, m)) {
2464 md_error(conf->mddev,
2465 conf->mirrors[m].rdev);
2466 /* an I/O failed, we can't clear the bitmap */
2467 set_bit(R1BIO_Degraded, &r1_bio->state);
2468 }
2469 rdev_dec_pending(conf->mirrors[m].rdev,
2470 conf->mddev);
2471 }
NeilBrown55ce74d2015-08-14 11:11:10 +10002472 if (fail) {
2473 spin_lock_irq(&conf->device_lock);
2474 list_add(&r1_bio->retry_list, &conf->bio_end_io_list);
colyli@suse.defd768632017-02-18 03:05:56 +08002475 idx = sector_to_idx(r1_bio->sector);
colyli@suse.de824e47d2017-02-18 03:05:57 +08002476 atomic_inc(&conf->nr_queued[idx]);
NeilBrown55ce74d2015-08-14 11:11:10 +10002477 spin_unlock_irq(&conf->device_lock);
colyli@suse.de824e47d2017-02-18 03:05:57 +08002478 /*
2479 * In case freeze_array() is waiting for condition
2480 * get_unqueued_pending() == extra to be true.
2481 */
2482 wake_up(&conf->wait_barrier);
NeilBrown55ce74d2015-08-14 11:11:10 +10002483 md_wakeup_thread(conf->mddev->thread);
NeilBrownbd8688a2015-10-24 16:02:16 +11002484 } else {
2485 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2486 close_write(r1_bio);
NeilBrown55ce74d2015-08-14 11:11:10 +10002487 raid_end_bio_io(r1_bio);
NeilBrownbd8688a2015-10-24 16:02:16 +11002488 }
NeilBrown62096bc2011-07-28 11:38:13 +10002489}
2490
NeilBrowne8096362011-10-11 16:49:05 +11002491static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002492{
NeilBrownfd01b882011-10-11 16:47:53 +11002493 struct mddev *mddev = conf->mddev;
NeilBrown62096bc2011-07-28 11:38:13 +10002494 struct bio *bio;
NeilBrown3cb03002011-10-11 16:45:26 +11002495 struct md_rdev *rdev;
NeilBrown62096bc2011-07-28 11:38:13 +10002496
2497 clear_bit(R1BIO_ReadError, &r1_bio->state);
2498 /* we got a read error. Maybe the drive is bad. Maybe just
2499 * the block and we can fix it.
2500 * We freeze all other IO, and try reading the block from
2501 * other devices. When we find one, we re-write
2502 * and check it that fixes the read error.
2503 * This is all done synchronously while the array is
2504 * frozen
2505 */
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002506
2507 bio = r1_bio->bios[r1_bio->read_disk];
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002508 bio_put(bio);
2509 r1_bio->bios[r1_bio->read_disk] = NULL;
2510
NeilBrown2e52d442016-11-18 16:16:12 +11002511 rdev = conf->mirrors[r1_bio->read_disk].rdev;
2512 if (mddev->ro == 0
2513 && !test_bit(FailFast, &rdev->flags)) {
NeilBrowne2d59922013-06-12 11:01:22 +10002514 freeze_array(conf, 1);
NeilBrown62096bc2011-07-28 11:38:13 +10002515 fix_read_error(conf, r1_bio->read_disk,
2516 r1_bio->sector, r1_bio->sectors);
2517 unfreeze_array(conf);
Gioh Kimb33d1062018-05-02 13:08:11 +02002518 } else if (mddev->ro == 0 && test_bit(FailFast, &rdev->flags)) {
2519 md_error(mddev, rdev);
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002520 } else {
2521 r1_bio->bios[r1_bio->read_disk] = IO_BLOCKED;
2522 }
2523
NeilBrown2e52d442016-11-18 16:16:12 +11002524 rdev_dec_pending(rdev, conf->mddev);
NeilBrown689389a2017-04-05 14:05:50 +10002525 allow_barrier(conf, r1_bio->sector);
2526 bio = r1_bio->master_bio;
NeilBrown62096bc2011-07-28 11:38:13 +10002527
NeilBrown689389a2017-04-05 14:05:50 +10002528 /* Reuse the old r1_bio so that the IO_BLOCKED settings are preserved */
2529 r1_bio->state = 0;
2530 raid1_read_request(mddev, bio, r1_bio->sectors, r1_bio);
NeilBrown62096bc2011-07-28 11:38:13 +10002531}
2532
Shaohua Li4ed87312012-10-11 13:34:00 +11002533static void raid1d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Shaohua Li4ed87312012-10-11 13:34:00 +11002535 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002536 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 unsigned long flags;
NeilBrowne8096362011-10-11 16:49:05 +11002538 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002540 struct blk_plug plug;
colyli@suse.defd768632017-02-18 03:05:56 +08002541 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
2543 md_check_recovery(mddev);
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002544
NeilBrown55ce74d2015-08-14 11:11:10 +10002545 if (!list_empty_careful(&conf->bio_end_io_list) &&
Shaohua Li29530792016-12-08 15:48:19 -08002546 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrown55ce74d2015-08-14 11:11:10 +10002547 LIST_HEAD(tmp);
2548 spin_lock_irqsave(&conf->device_lock, flags);
colyli@suse.defd768632017-02-18 03:05:56 +08002549 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
2550 list_splice_init(&conf->bio_end_io_list, &tmp);
NeilBrown55ce74d2015-08-14 11:11:10 +10002551 spin_unlock_irqrestore(&conf->device_lock, flags);
2552 while (!list_empty(&tmp)) {
Mikulas Patockaa4527442015-10-01 15:17:43 -04002553 r1_bio = list_first_entry(&tmp, struct r1bio,
2554 retry_list);
NeilBrown55ce74d2015-08-14 11:11:10 +10002555 list_del(&r1_bio->retry_list);
colyli@suse.defd768632017-02-18 03:05:56 +08002556 idx = sector_to_idx(r1_bio->sector);
colyli@suse.de824e47d2017-02-18 03:05:57 +08002557 atomic_dec(&conf->nr_queued[idx]);
NeilBrownbd8688a2015-10-24 16:02:16 +11002558 if (mddev->degraded)
2559 set_bit(R1BIO_Degraded, &r1_bio->state);
2560 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2561 close_write(r1_bio);
NeilBrown55ce74d2015-08-14 11:11:10 +10002562 raid_end_bio_io(r1_bio);
2563 }
2564 }
2565
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002566 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002568
NeilBrown0021b7b2012-07-31 09:08:14 +02002569 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002572 if (list_empty(head)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002573 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002575 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002576 r1_bio = list_entry(head->prev, struct r1bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 list_del(head->prev);
colyli@suse.defd768632017-02-18 03:05:56 +08002578 idx = sector_to_idx(r1_bio->sector);
colyli@suse.de824e47d2017-02-18 03:05:57 +08002579 atomic_dec(&conf->nr_queued[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 spin_unlock_irqrestore(&conf->device_lock, flags);
2581
2582 mddev = r1_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002583 conf = mddev->private;
NeilBrown4367af52011-07-28 11:31:49 +10002584 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
NeilBrownd8f05d22011-07-28 11:33:00 +10002585 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
NeilBrown62096bc2011-07-28 11:38:13 +10002586 test_bit(R1BIO_WriteError, &r1_bio->state))
2587 handle_sync_write_finished(conf, r1_bio);
2588 else
NeilBrown4367af52011-07-28 11:31:49 +10002589 sync_request_write(mddev, r1_bio);
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002590 } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
NeilBrown62096bc2011-07-28 11:38:13 +10002591 test_bit(R1BIO_WriteError, &r1_bio->state))
2592 handle_write_finished(conf, r1_bio);
2593 else if (test_bit(R1BIO_ReadError, &r1_bio->state))
2594 handle_read_error(conf, r1_bio);
2595 else
NeilBrownc230e7e2017-04-05 14:05:50 +10002596 WARN_ON_ONCE(1);
NeilBrown62096bc2011-07-28 11:38:13 +10002597
NeilBrown1d9d5242009-10-16 15:55:32 +11002598 cond_resched();
Shaohua Li29530792016-12-08 15:48:19 -08002599 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
NeilBrownde393cd2011-07-28 11:31:48 +10002600 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002602 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603}
2604
NeilBrowne8096362011-10-11 16:49:05 +11002605static int init_resync(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
2607 int buffs;
2608
2609 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Kent Overstreetafeee512018-05-20 18:25:52 -04002610 BUG_ON(mempool_initialized(&conf->r1buf_pool));
2611
2612 return mempool_init(&conf->r1buf_pool, buffs, r1buf_pool_alloc,
2613 r1buf_pool_free, conf->poolinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
2615
Shaohua Li208410b2017-08-24 17:50:40 -07002616static struct r1bio *raid1_alloc_init_r1buf(struct r1conf *conf)
2617{
Kent Overstreetafeee512018-05-20 18:25:52 -04002618 struct r1bio *r1bio = mempool_alloc(&conf->r1buf_pool, GFP_NOIO);
Shaohua Li208410b2017-08-24 17:50:40 -07002619 struct resync_pages *rps;
2620 struct bio *bio;
2621 int i;
2622
2623 for (i = conf->poolinfo->raid_disks; i--; ) {
2624 bio = r1bio->bios[i];
2625 rps = bio->bi_private;
2626 bio_reset(bio);
2627 bio->bi_private = rps;
2628 }
2629 r1bio->master_bio = NULL;
2630 return r1bio;
2631}
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633/*
2634 * perform a "sync" on one "block"
2635 *
2636 * We need to make sure that no normal I/O request - particularly write
2637 * requests - conflict with active sync requests.
2638 *
2639 * This is achieved by tracking pending requests and a 'barrier' concept
2640 * that can be installed to exclude normal IO requests.
2641 */
2642
Shaohua Li849674e2016-01-20 13:52:20 -08002643static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr,
2644 int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645{
NeilBrowne8096362011-10-11 16:49:05 +11002646 struct r1conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002647 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 struct bio *bio;
2649 sector_t max_sector, nr_sectors;
NeilBrown3e198f72006-01-06 00:20:21 -08002650 int disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 int i;
NeilBrown3e198f72006-01-06 00:20:21 -08002652 int wonly = -1;
2653 int write_targets = 0, read_targets = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11002654 sector_t sync_blocks;
NeilBrowne3b97032005-08-04 12:53:34 -07002655 int still_degraded = 0;
NeilBrown06f60382011-07-28 11:31:48 +10002656 int good_sectors = RESYNC_SECTORS;
2657 int min_bad = 0; /* number of sectors that are bad in all devices */
colyli@suse.defd768632017-02-18 03:05:56 +08002658 int idx = sector_to_idx(sector_nr);
Ming Lei022e5102017-07-14 16:14:42 +08002659 int page_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Kent Overstreetafeee512018-05-20 18:25:52 -04002661 if (!mempool_initialized(&conf->r1buf_pool))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Andre Noll58c0fed2009-03-31 14:33:13 +11002665 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 if (sector_nr >= max_sector) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002667 /* If we aborted, we need to abort the
2668 * sync on the 'current' bitmap chunk (there will
2669 * only be one in raid1 resync.
2670 * We can find the current addess in mddev->curr_resync
2671 */
NeilBrown6a806c52005-07-15 03:56:35 -07002672 if (mddev->curr_resync < max_sector) /* aborted */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002673 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2674 &sync_blocks, 1);
NeilBrown6a806c52005-07-15 03:56:35 -07002675 else /* completed sync */
NeilBrown191ea9b2005-06-21 17:17:23 -07002676 conf->fullsync = 0;
NeilBrown6a806c52005-07-15 03:56:35 -07002677
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002678 md_bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 close_sync(conf);
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002680
2681 if (mddev_is_clustered(mddev)) {
2682 conf->cluster_sync_low = 0;
2683 conf->cluster_sync_high = 0;
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 return 0;
2686 }
2687
NeilBrown07d84d102006-06-26 00:27:56 -07002688 if (mddev->bitmap == NULL &&
2689 mddev->recovery_cp == MaxSector &&
NeilBrown6394cca2006-08-27 01:23:50 -07002690 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown07d84d102006-06-26 00:27:56 -07002691 conf->fullsync == 0) {
2692 *skipped = 1;
2693 return max_sector - sector_nr;
2694 }
NeilBrown6394cca2006-08-27 01:23:50 -07002695 /* before building a request, check if we can skip these blocks..
2696 * This call the bitmap_start_sync doesn't actually record anything
2697 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002698 if (!md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrowne5de485f2005-11-08 21:39:38 -08002699 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002700 /* We can skip this block, and probably several more */
2701 *skipped = 1;
2702 return sync_blocks;
2703 }
NeilBrown17999be2006-01-06 00:20:12 -08002704
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02002705 /*
2706 * If there is non-resync activity waiting for a turn, then let it
2707 * though before starting on this new sync request.
2708 */
colyli@suse.de824e47d2017-02-18 03:05:57 +08002709 if (atomic_read(&conf->nr_waiting[idx]))
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02002710 schedule_timeout_uninterruptible(1);
2711
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002712 /* we are incrementing sector_nr below. To be safe, we check against
2713 * sector_nr + two times RESYNC_SECTORS
2714 */
2715
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002716 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr,
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002717 mddev_is_clustered(mddev) && (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
NeilBrown17999be2006-01-06 00:20:12 -08002718
Yufen Yu8c242592018-04-09 09:50:44 +08002719
2720 if (raise_barrier(conf, sector_nr))
2721 return 0;
2722
2723 r1_bio = raid1_alloc_init_r1buf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
NeilBrown3e198f72006-01-06 00:20:21 -08002725 rcu_read_lock();
2726 /*
2727 * If we get a correctably read error during resync or recovery,
2728 * we might want to read from a different device. So we
2729 * flag all drives that could conceivably be read from for READ,
2730 * and any others (which will be non-In_sync devices) for WRITE.
2731 * If a read fails, we try reading from something else for which READ
2732 * is OK.
2733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 r1_bio->mddev = mddev;
2736 r1_bio->sector = sector_nr;
NeilBrown191ea9b2005-06-21 17:17:23 -07002737 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 set_bit(R1BIO_IsSync, &r1_bio->state);
colyli@suse.defd768632017-02-18 03:05:56 +08002739 /* make sure good_sectors won't go across barrier unit boundary */
2740 good_sectors = align_to_barrier_unit_end(sector_nr, good_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
NeilBrown8f19ccb2011-12-23 10:17:56 +11002742 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002743 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 bio = r1_bio->bios[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
NeilBrown3e198f72006-01-06 00:20:21 -08002746 rdev = rcu_dereference(conf->mirrors[i].rdev);
2747 if (rdev == NULL ||
NeilBrown06f60382011-07-28 11:31:48 +10002748 test_bit(Faulty, &rdev->flags)) {
NeilBrown8f19ccb2011-12-23 10:17:56 +11002749 if (i < conf->raid_disks)
2750 still_degraded = 1;
NeilBrown3e198f72006-01-06 00:20:21 -08002751 } else if (!test_bit(In_sync, &rdev->flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05002752 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 bio->bi_end_io = end_sync_write;
2754 write_targets ++;
NeilBrown3e198f72006-01-06 00:20:21 -08002755 } else {
2756 /* may need to read from here */
NeilBrown06f60382011-07-28 11:31:48 +10002757 sector_t first_bad = MaxSector;
2758 int bad_sectors;
2759
2760 if (is_badblock(rdev, sector_nr, good_sectors,
2761 &first_bad, &bad_sectors)) {
2762 if (first_bad > sector_nr)
2763 good_sectors = first_bad - sector_nr;
2764 else {
2765 bad_sectors -= (sector_nr - first_bad);
2766 if (min_bad == 0 ||
2767 min_bad > bad_sectors)
2768 min_bad = bad_sectors;
2769 }
NeilBrown3e198f72006-01-06 00:20:21 -08002770 }
NeilBrown06f60382011-07-28 11:31:48 +10002771 if (sector_nr < first_bad) {
2772 if (test_bit(WriteMostly, &rdev->flags)) {
2773 if (wonly < 0)
2774 wonly = i;
2775 } else {
2776 if (disk < 0)
2777 disk = i;
2778 }
Mike Christie796a5cf2016-06-05 14:32:07 -05002779 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown06f60382011-07-28 11:31:48 +10002780 bio->bi_end_io = end_sync_read;
2781 read_targets++;
Alexander Lyakasd57368a2012-07-17 13:17:55 +03002782 } else if (!test_bit(WriteErrorSeen, &rdev->flags) &&
2783 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
2784 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
2785 /*
2786 * The device is suitable for reading (InSync),
2787 * but has bad block(s) here. Let's try to correct them,
2788 * if we are doing resync or repair. Otherwise, leave
2789 * this device alone for this sync request.
2790 */
Mike Christie796a5cf2016-06-05 14:32:07 -05002791 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Alexander Lyakasd57368a2012-07-17 13:17:55 +03002792 bio->bi_end_io = end_sync_write;
2793 write_targets++;
NeilBrown06f60382011-07-28 11:31:48 +10002794 }
NeilBrown3e198f72006-01-06 00:20:21 -08002795 }
Zhiqiang Liu028288d2019-12-10 10:42:25 +08002796 if (rdev && bio->bi_end_io) {
NeilBrown06f60382011-07-28 11:31:48 +10002797 atomic_inc(&rdev->nr_pending);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002798 bio->bi_iter.bi_sector = sector_nr + rdev->data_offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002799 bio_set_dev(bio, rdev->bdev);
NeilBrown2e52d442016-11-18 16:16:12 +11002800 if (test_bit(FailFast, &rdev->flags))
2801 bio->bi_opf |= MD_FAILFAST;
NeilBrown06f60382011-07-28 11:31:48 +10002802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 }
NeilBrown3e198f72006-01-06 00:20:21 -08002804 rcu_read_unlock();
2805 if (disk < 0)
2806 disk = wonly;
2807 r1_bio->read_disk = disk;
NeilBrown191ea9b2005-06-21 17:17:23 -07002808
NeilBrown06f60382011-07-28 11:31:48 +10002809 if (read_targets == 0 && min_bad > 0) {
2810 /* These sectors are bad on all InSync devices, so we
2811 * need to mark them bad on all write targets
2812 */
2813 int ok = 1;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002814 for (i = 0 ; i < conf->raid_disks * 2 ; i++)
NeilBrown06f60382011-07-28 11:31:48 +10002815 if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
majianpenga42f9d82012-04-02 01:04:19 +10002816 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrown06f60382011-07-28 11:31:48 +10002817 ok = rdev_set_badblocks(rdev, sector_nr,
2818 min_bad, 0
2819 ) && ok;
2820 }
Shaohua Li29530792016-12-08 15:48:19 -08002821 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown06f60382011-07-28 11:31:48 +10002822 *skipped = 1;
2823 put_buf(r1_bio);
2824
2825 if (!ok) {
2826 /* Cannot record the badblocks, so need to
2827 * abort the resync.
2828 * If there are multiple read targets, could just
2829 * fail the really bad ones ???
2830 */
2831 conf->recovery_disabled = mddev->recovery_disabled;
2832 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2833 return 0;
2834 } else
2835 return min_bad;
2836
2837 }
2838 if (min_bad > 0 && min_bad < good_sectors) {
2839 /* only resync enough to reach the next bad->good
2840 * transition */
2841 good_sectors = min_bad;
2842 }
2843
NeilBrown3e198f72006-01-06 00:20:21 -08002844 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
2845 /* extra read targets are also write targets */
2846 write_targets += read_targets-1;
2847
2848 if (write_targets == 0 || read_targets == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 /* There is nowhere to write, so all non-sync
2850 * drives must be failed - so we are finished
2851 */
NeilBrownb7219cc2012-07-31 10:05:34 +10002852 sector_t rv;
2853 if (min_bad > 0)
2854 max_sector = sector_nr + min_bad;
2855 rv = max_sector - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07002856 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 put_buf(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 return rv;
2859 }
2860
NeilBrownc6207272008-02-06 01:39:52 -08002861 if (max_sector > mddev->resync_max)
2862 max_sector = mddev->resync_max; /* Don't do IO beyond here */
NeilBrown06f60382011-07-28 11:31:48 +10002863 if (max_sector > sector_nr + good_sectors)
2864 max_sector = sector_nr + good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 nr_sectors = 0;
NeilBrown289e99e2005-06-21 17:17:24 -07002866 sync_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 do {
2868 struct page *page;
2869 int len = PAGE_SIZE;
2870 if (sector_nr + (len>>9) > max_sector)
2871 len = (max_sector - sector_nr) << 9;
2872 if (len == 0)
2873 break;
NeilBrown6a806c52005-07-15 03:56:35 -07002874 if (sync_blocks == 0) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002875 if (!md_bitmap_start_sync(mddev->bitmap, sector_nr,
2876 &sync_blocks, still_degraded) &&
NeilBrowne5de485f2005-11-08 21:39:38 -08002877 !conf->fullsync &&
2878 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrown6a806c52005-07-15 03:56:35 -07002879 break;
NeilBrown7571ae82010-10-07 11:54:46 +11002880 if ((len >> 9) > sync_blocks)
NeilBrown6a806c52005-07-15 03:56:35 -07002881 len = sync_blocks<<9;
NeilBrownab7a30c2005-06-21 17:17:23 -07002882 }
NeilBrown191ea9b2005-06-21 17:17:23 -07002883
NeilBrown8f19ccb2011-12-23 10:17:56 +11002884 for (i = 0 ; i < conf->raid_disks * 2; i++) {
Ming Lei98d30c52017-03-17 00:12:26 +08002885 struct resync_pages *rp;
2886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 bio = r1_bio->bios[i];
Ming Lei98d30c52017-03-17 00:12:26 +08002888 rp = get_resync_pages(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (bio->bi_end_io) {
Ming Lei022e5102017-07-14 16:14:42 +08002890 page = resync_fetch_page(rp, page_idx);
Ming Leic85ba142017-03-17 00:12:22 +08002891
2892 /*
2893 * won't fail because the vec table is big
2894 * enough to hold all these pages
2895 */
2896 bio_add_page(bio, page, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 }
2898 }
2899 nr_sectors += len>>9;
2900 sector_nr += len>>9;
NeilBrown191ea9b2005-06-21 17:17:23 -07002901 sync_blocks -= (len>>9);
Ming Lei022e5102017-07-14 16:14:42 +08002902 } while (++page_idx < RESYNC_PAGES);
Ming Lei98d30c52017-03-17 00:12:26 +08002903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 r1_bio->sectors = nr_sectors;
2905
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002906 if (mddev_is_clustered(mddev) &&
2907 conf->cluster_sync_high < sector_nr + nr_sectors) {
2908 conf->cluster_sync_low = mddev->curr_resync_completed;
2909 conf->cluster_sync_high = conf->cluster_sync_low + CLUSTER_RESYNC_WINDOW_SECTORS;
2910 /* Send resync message */
2911 md_cluster_ops->resync_info_update(mddev,
2912 conf->cluster_sync_low,
2913 conf->cluster_sync_high);
2914 }
2915
NeilBrownd11c1712006-01-06 00:20:26 -08002916 /* For a user-requested sync, we read all readable devices and do a
2917 * compare
2918 */
2919 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2920 atomic_set(&r1_bio->remaining, read_targets);
NeilBrown2d4f4f32012-07-09 11:34:13 +10002921 for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) {
NeilBrownd11c1712006-01-06 00:20:26 -08002922 bio = r1_bio->bios[i];
2923 if (bio->bi_end_io == end_sync_read) {
NeilBrown2d4f4f32012-07-09 11:34:13 +10002924 read_targets--;
Christoph Hellwig74d46992017-08-23 19:10:32 +02002925 md_sync_acct_bio(bio, nr_sectors);
NeilBrown2e52d442016-11-18 16:16:12 +11002926 if (read_targets == 1)
2927 bio->bi_opf &= ~MD_FAILFAST;
NeilBrownd11c1712006-01-06 00:20:26 -08002928 generic_make_request(bio);
2929 }
2930 }
2931 } else {
2932 atomic_set(&r1_bio->remaining, 1);
2933 bio = r1_bio->bios[r1_bio->read_disk];
Christoph Hellwig74d46992017-08-23 19:10:32 +02002934 md_sync_acct_bio(bio, nr_sectors);
NeilBrown2e52d442016-11-18 16:16:12 +11002935 if (read_targets == 1)
2936 bio->bi_opf &= ~MD_FAILFAST;
NeilBrownd11c1712006-01-06 00:20:26 -08002937 generic_make_request(bio);
NeilBrownd11c1712006-01-06 00:20:26 -08002938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 return nr_sectors;
2940}
2941
NeilBrownfd01b882011-10-11 16:47:53 +11002942static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07002943{
2944 if (sectors)
2945 return sectors;
2946
2947 return mddev->dev_sectors;
2948}
2949
NeilBrowne8096362011-10-11 16:49:05 +11002950static struct r1conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951{
NeilBrowne8096362011-10-11 16:49:05 +11002952 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11002953 int i;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10002954 struct raid1_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11002955 struct md_rdev *rdev;
NeilBrown709ae482009-12-14 12:49:51 +11002956 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
NeilBrowne8096362011-10-11 16:49:05 +11002958 conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 if (!conf)
NeilBrown709ae482009-12-14 12:49:51 +11002960 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
colyli@suse.defd768632017-02-18 03:05:56 +08002962 conf->nr_pending = kcalloc(BARRIER_BUCKETS_NR,
colyli@suse.de824e47d2017-02-18 03:05:57 +08002963 sizeof(atomic_t), GFP_KERNEL);
colyli@suse.defd768632017-02-18 03:05:56 +08002964 if (!conf->nr_pending)
2965 goto abort;
2966
2967 conf->nr_waiting = kcalloc(BARRIER_BUCKETS_NR,
colyli@suse.de824e47d2017-02-18 03:05:57 +08002968 sizeof(atomic_t), GFP_KERNEL);
colyli@suse.defd768632017-02-18 03:05:56 +08002969 if (!conf->nr_waiting)
2970 goto abort;
2971
2972 conf->nr_queued = kcalloc(BARRIER_BUCKETS_NR,
colyli@suse.de824e47d2017-02-18 03:05:57 +08002973 sizeof(atomic_t), GFP_KERNEL);
colyli@suse.defd768632017-02-18 03:05:56 +08002974 if (!conf->nr_queued)
2975 goto abort;
2976
2977 conf->barrier = kcalloc(BARRIER_BUCKETS_NR,
colyli@suse.de824e47d2017-02-18 03:05:57 +08002978 sizeof(atomic_t), GFP_KERNEL);
colyli@suse.defd768632017-02-18 03:05:56 +08002979 if (!conf->barrier)
2980 goto abort;
2981
Kees Cook6396bb22018-06-12 14:03:40 -07002982 conf->mirrors = kzalloc(array3_size(sizeof(struct raid1_info),
2983 mddev->raid_disks, 2),
2984 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 if (!conf->mirrors)
NeilBrown709ae482009-12-14 12:49:51 +11002986 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
NeilBrownddaf22a2006-01-06 00:20:19 -08002988 conf->tmppage = alloc_page(GFP_KERNEL);
2989 if (!conf->tmppage)
NeilBrown709ae482009-12-14 12:49:51 +11002990 goto abort;
NeilBrownddaf22a2006-01-06 00:20:19 -08002991
NeilBrown709ae482009-12-14 12:49:51 +11002992 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 if (!conf->poolinfo)
NeilBrown709ae482009-12-14 12:49:51 +11002994 goto abort;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002995 conf->poolinfo->raid_disks = mddev->raid_disks * 2;
Marcos Paulo de Souza3f677f92019-06-14 15:41:04 -07002996 err = mempool_init(&conf->r1bio_pool, NR_RAID_BIOS, r1bio_pool_alloc,
Marcos Paulo de Souzac7afa802019-06-14 15:41:10 -07002997 rbio_pool_free, conf->poolinfo);
Kent Overstreetafeee512018-05-20 18:25:52 -04002998 if (err)
NeilBrown709ae482009-12-14 12:49:51 +11002999 goto abort;
3000
Kent Overstreetafeee512018-05-20 18:25:52 -04003001 err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
3002 if (err)
NeilBrownc230e7e2017-04-05 14:05:50 +10003003 goto abort;
3004
NeilBrowned9bfdf2009-10-16 15:55:44 +11003005 conf->poolinfo->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
NeilBrownc19d5792011-12-23 10:17:57 +11003007 err = -EINVAL;
Neil Browne7e72bf2008-05-14 16:05:54 -07003008 spin_lock_init(&conf->device_lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11003009 rdev_for_each(rdev, mddev) {
NeilBrown709ae482009-12-14 12:49:51 +11003010 int disk_idx = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 if (disk_idx >= mddev->raid_disks
3012 || disk_idx < 0)
3013 continue;
NeilBrownc19d5792011-12-23 10:17:57 +11003014 if (test_bit(Replacement, &rdev->flags))
NeilBrown02b898f2012-10-31 11:42:03 +11003015 disk = conf->mirrors + mddev->raid_disks + disk_idx;
NeilBrownc19d5792011-12-23 10:17:57 +11003016 else
3017 disk = conf->mirrors + disk_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
NeilBrownc19d5792011-12-23 10:17:57 +11003019 if (disk->rdev)
3020 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 disk->rdev = rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 disk->head_position = 0;
Shaohua Li12cee5a2012-07-31 10:03:53 +10003023 disk->seq_start = MaxSector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 }
3025 conf->raid_disks = mddev->raid_disks;
3026 conf->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 INIT_LIST_HEAD(&conf->retry_list);
NeilBrown55ce74d2015-08-14 11:11:10 +10003028 INIT_LIST_HEAD(&conf->bio_end_io_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
3030 spin_lock_init(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -08003031 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
NeilBrown191ea9b2005-06-21 17:17:23 -07003033 bio_list_init(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +11003034 conf->pending_count = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11003035 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown191ea9b2005-06-21 17:17:23 -07003036
NeilBrownc19d5792011-12-23 10:17:57 +11003037 err = -EIO;
NeilBrown8f19ccb2011-12-23 10:17:56 +11003038 for (i = 0; i < conf->raid_disks * 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
3040 disk = conf->mirrors + i;
3041
NeilBrownc19d5792011-12-23 10:17:57 +11003042 if (i < conf->raid_disks &&
3043 disk[conf->raid_disks].rdev) {
3044 /* This slot has a replacement. */
3045 if (!disk->rdev) {
3046 /* No original, just make the replacement
3047 * a recovering spare
3048 */
3049 disk->rdev =
3050 disk[conf->raid_disks].rdev;
3051 disk[conf->raid_disks].rdev = NULL;
3052 } else if (!test_bit(In_sync, &disk->rdev->flags))
3053 /* Original is not in_sync - bad */
3054 goto abort;
3055 }
3056
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003057 if (!disk->rdev ||
3058 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 disk->head_position = 0;
Jonathan Brassow4f0a5e02012-05-22 13:55:31 +10003060 if (disk->rdev &&
3061 (disk->rdev->saved_raid_disk < 0))
NeilBrown918f0232007-08-22 14:01:52 -07003062 conf->fullsync = 1;
Shaohua Libe4d3282012-07-31 10:03:53 +10003063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 }
NeilBrown709ae482009-12-14 12:49:51 +11003065
NeilBrown709ae482009-12-14 12:49:51 +11003066 err = -ENOMEM;
NeilBrown02326052012-07-03 15:56:52 +10003067 conf->thread = md_register_thread(raid1d, mddev, "raid1");
NeilBrown1d41c212016-11-02 14:16:50 +11003068 if (!conf->thread)
NeilBrown709ae482009-12-14 12:49:51 +11003069 goto abort;
NeilBrown191ea9b2005-06-21 17:17:23 -07003070
NeilBrown709ae482009-12-14 12:49:51 +11003071 return conf;
3072
3073 abort:
3074 if (conf) {
Kent Overstreetafeee512018-05-20 18:25:52 -04003075 mempool_exit(&conf->r1bio_pool);
NeilBrown709ae482009-12-14 12:49:51 +11003076 kfree(conf->mirrors);
3077 safe_put_page(conf->tmppage);
3078 kfree(conf->poolinfo);
colyli@suse.defd768632017-02-18 03:05:56 +08003079 kfree(conf->nr_pending);
3080 kfree(conf->nr_waiting);
3081 kfree(conf->nr_queued);
3082 kfree(conf->barrier);
Kent Overstreetafeee512018-05-20 18:25:52 -04003083 bioset_exit(&conf->bio_split);
NeilBrown709ae482009-12-14 12:49:51 +11003084 kfree(conf);
3085 }
3086 return ERR_PTR(err);
3087}
3088
NeilBrownafa0f552014-12-15 12:56:58 +11003089static void raid1_free(struct mddev *mddev, void *priv);
Shaohua Li849674e2016-01-20 13:52:20 -08003090static int raid1_run(struct mddev *mddev)
NeilBrown709ae482009-12-14 12:49:51 +11003091{
NeilBrowne8096362011-10-11 16:49:05 +11003092 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11003093 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003094 struct md_rdev *rdev;
majianpeng5220ea12012-04-02 09:48:38 +10003095 int ret;
Shaohua Li2ff8cc22012-10-11 13:28:54 +11003096 bool discard_supported = false;
NeilBrown709ae482009-12-14 12:49:51 +11003097
3098 if (mddev->level != 1) {
NeilBrown1d41c212016-11-02 14:16:50 +11003099 pr_warn("md/raid1:%s: raid level not set to mirroring (%d)\n",
3100 mdname(mddev), mddev->level);
NeilBrown709ae482009-12-14 12:49:51 +11003101 return -EIO;
3102 }
3103 if (mddev->reshape_position != MaxSector) {
NeilBrown1d41c212016-11-02 14:16:50 +11003104 pr_warn("md/raid1:%s: reshape_position set but not supported\n",
3105 mdname(mddev));
NeilBrown709ae482009-12-14 12:49:51 +11003106 return -EIO;
3107 }
NeilBrowna415c0f2017-06-05 16:05:13 +10003108 if (mddev_init_writes_pending(mddev) < 0)
3109 return -ENOMEM;
NeilBrown709ae482009-12-14 12:49:51 +11003110 /*
3111 * copy the already verified devices into our private RAID1
3112 * bookkeeping area. [whatever we allocate in run(),
NeilBrownafa0f552014-12-15 12:56:58 +11003113 * should be freed in raid1_free()]
NeilBrown709ae482009-12-14 12:49:51 +11003114 */
3115 if (mddev->private == NULL)
3116 conf = setup_conf(mddev);
3117 else
3118 conf = mddev->private;
3119
3120 if (IS_ERR(conf))
3121 return PTR_ERR(conf);
3122
Christoph Hellwig3deff1a2017-04-05 19:21:03 +02003123 if (mddev->queue) {
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07003124 blk_queue_max_write_same_sectors(mddev->queue, 0);
Christoph Hellwig3deff1a2017-04-05 19:21:03 +02003125 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
3126 }
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07003127
NeilBrowndafb20f2012-03-19 12:46:39 +11003128 rdev_for_each(rdev, mddev) {
Jonathan Brassow1ed72422011-06-07 17:50:35 -05003129 if (!mddev->gendisk)
3130 continue;
NeilBrown709ae482009-12-14 12:49:51 +11003131 disk_stack_limits(mddev->gendisk, rdev->bdev,
3132 rdev->data_offset << 9);
Shaohua Li2ff8cc22012-10-11 13:28:54 +11003133 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3134 discard_supported = true;
NeilBrown709ae482009-12-14 12:49:51 +11003135 }
3136
3137 mddev->degraded = 0;
Yufen Yuebfeb442019-06-14 15:41:08 -07003138 for (i = 0; i < conf->raid_disks; i++)
NeilBrown709ae482009-12-14 12:49:51 +11003139 if (conf->mirrors[i].rdev == NULL ||
3140 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
3141 test_bit(Faulty, &conf->mirrors[i].rdev->flags))
3142 mddev->degraded++;
Yufen Yu07f1a682019-09-03 21:12:41 +08003143 /*
3144 * RAID1 needs at least one disk in active
3145 */
3146 if (conf->raid_disks - mddev->degraded < 1) {
3147 ret = -EINVAL;
3148 goto abort;
3149 }
NeilBrown709ae482009-12-14 12:49:51 +11003150
3151 if (conf->raid_disks - mddev->degraded == 1)
3152 mddev->recovery_cp = MaxSector;
3153
Andre Noll8c6ac8682009-06-18 08:48:06 +10003154 if (mddev->recovery_cp != MaxSector)
NeilBrown1d41c212016-11-02 14:16:50 +11003155 pr_info("md/raid1:%s: not clean -- starting background reconstruction\n",
3156 mdname(mddev));
3157 pr_info("md/raid1:%s: active with %d out of %d mirrors\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10003158 mdname(mddev), mddev->raid_disks - mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 mddev->raid_disks);
NeilBrown709ae482009-12-14 12:49:51 +11003160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 /*
3162 * Ok, everything is just fine now
3163 */
NeilBrown709ae482009-12-14 12:49:51 +11003164 mddev->thread = conf->thread;
3165 conf->thread = NULL;
3166 mddev->private = conf;
NeilBrown46533ff2016-11-18 16:16:11 +11003167 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
NeilBrown709ae482009-12-14 12:49:51 +11003168
Dan Williams1f403622009-03-31 14:59:03 +11003169 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Jonathan Brassow1ed72422011-06-07 17:50:35 -05003171 if (mddev->queue) {
Shaohua Li2ff8cc22012-10-11 13:28:54 +11003172 if (discard_supported)
Bart Van Assche8b904b52018-03-07 17:10:10 -08003173 blk_queue_flag_set(QUEUE_FLAG_DISCARD,
Shaohua Li2ff8cc22012-10-11 13:28:54 +11003174 mddev->queue);
3175 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08003176 blk_queue_flag_clear(QUEUE_FLAG_DISCARD,
Shaohua Li2ff8cc22012-10-11 13:28:54 +11003177 mddev->queue);
Jonathan Brassow1ed72422011-06-07 17:50:35 -05003178 }
majianpeng5220ea12012-04-02 09:48:38 +10003179
Yufen Yuebfeb442019-06-14 15:41:08 -07003180 ret = md_integrity_register(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003181 if (ret) {
3182 md_unregister_thread(&mddev->thread);
Yufen Yu07f1a682019-09-03 21:12:41 +08003183 goto abort;
NeilBrown5aa61f42014-12-15 12:56:57 +11003184 }
Yufen Yu07f1a682019-09-03 21:12:41 +08003185 return 0;
3186
3187abort:
3188 raid1_free(mddev, conf);
majianpeng5220ea12012-04-02 09:48:38 +10003189 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190}
3191
NeilBrownafa0f552014-12-15 12:56:58 +11003192static void raid1_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
NeilBrownafa0f552014-12-15 12:56:58 +11003194 struct r1conf *conf = priv;
NeilBrown4b6d2872005-09-09 16:23:47 -07003195
Kent Overstreetafeee512018-05-20 18:25:52 -04003196 mempool_exit(&conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003197 kfree(conf->mirrors);
Hirokazu Takahashi0fea7ed2013-04-24 11:42:44 +10003198 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003199 kfree(conf->poolinfo);
colyli@suse.defd768632017-02-18 03:05:56 +08003200 kfree(conf->nr_pending);
3201 kfree(conf->nr_waiting);
3202 kfree(conf->nr_queued);
3203 kfree(conf->barrier);
Kent Overstreetafeee512018-05-20 18:25:52 -04003204 bioset_exit(&conf->bio_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206}
3207
NeilBrownfd01b882011-10-11 16:47:53 +11003208static int raid1_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209{
3210 /* no resync is happening, and there is enough space
3211 * on all devices, so we can resize.
3212 * We need to make sure resync covers any new space.
3213 * If the array is shrinking we should possibly wait until
3214 * any io in the removed space completes, but it hardly seems
3215 * worth it.
3216 */
NeilBrowna4a61252012-05-22 13:55:27 +10003217 sector_t newsize = raid1_size(mddev, sectors, 0);
3218 if (mddev->external_size &&
3219 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11003220 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10003221 if (mddev->bitmap) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003222 int ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10003223 if (ret)
3224 return ret;
3225 }
3226 md_set_array_sectors(mddev, newsize);
Dan Williamsb522adc2009-03-31 15:00:31 +11003227 if (sectors > mddev->dev_sectors &&
NeilBrownb0986362011-05-11 15:52:21 +10003228 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003229 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3231 }
Dan Williamsb522adc2009-03-31 15:00:31 +11003232 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07003233 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return 0;
3235}
3236
NeilBrownfd01b882011-10-11 16:47:53 +11003237static int raid1_reshape(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
3239 /* We need to:
3240 * 1/ resize the r1bio_pool
3241 * 2/ resize conf->mirrors
3242 *
3243 * We allocate a new r1bio_pool if we can.
3244 * Then raise a device barrier and wait until all IO stops.
3245 * Then resize conf->mirrors and swap in the new r1bio pool.
NeilBrown6ea9c072005-06-21 17:17:09 -07003246 *
3247 * At the same time, we "pack" the devices so that all the missing
3248 * devices have the higher raid_disk numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 */
Kent Overstreetafeee512018-05-20 18:25:52 -04003250 mempool_t newpool, oldpool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 struct pool_info *newpoolinfo;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10003252 struct raid1_info *newmirrors;
NeilBrowne8096362011-10-11 16:49:05 +11003253 struct r1conf *conf = mddev->private;
NeilBrown63c70c42006-03-27 01:18:13 -08003254 int cnt, raid_disks;
NeilBrownc04be0a2006-10-03 01:15:53 -07003255 unsigned long flags;
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02003256 int d, d2;
Kent Overstreetafeee512018-05-20 18:25:52 -04003257 int ret;
3258
3259 memset(&newpool, 0, sizeof(newpool));
3260 memset(&oldpool, 0, sizeof(oldpool));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
NeilBrown63c70c42006-03-27 01:18:13 -08003262 /* Cannot change chunk_size, layout, or level */
Andre Noll664e7c42009-06-18 08:45:27 +10003263 if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
NeilBrown63c70c42006-03-27 01:18:13 -08003264 mddev->layout != mddev->new_layout ||
3265 mddev->level != mddev->new_level) {
Andre Noll664e7c42009-06-18 08:45:27 +10003266 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown63c70c42006-03-27 01:18:13 -08003267 mddev->new_layout = mddev->layout;
3268 mddev->new_level = mddev->level;
3269 return -EINVAL;
3270 }
3271
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02003272 if (!mddev_is_clustered(mddev))
3273 md_allow_write(mddev);
NeilBrown2a2275d2007-01-26 00:57:11 -08003274
NeilBrown63c70c42006-03-27 01:18:13 -08003275 raid_disks = mddev->raid_disks + mddev->delta_disks;
3276
NeilBrown6ea9c072005-06-21 17:17:09 -07003277 if (raid_disks < conf->raid_disks) {
3278 cnt=0;
3279 for (d= 0; d < conf->raid_disks; d++)
3280 if (conf->mirrors[d].rdev)
3281 cnt++;
3282 if (cnt > raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 return -EBUSY;
NeilBrown6ea9c072005-06-21 17:17:09 -07003284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
3286 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
3287 if (!newpoolinfo)
3288 return -ENOMEM;
3289 newpoolinfo->mddev = mddev;
NeilBrown8f19ccb2011-12-23 10:17:56 +11003290 newpoolinfo->raid_disks = raid_disks * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Marcos Paulo de Souza3f677f92019-06-14 15:41:04 -07003292 ret = mempool_init(&newpool, NR_RAID_BIOS, r1bio_pool_alloc,
Marcos Paulo de Souzac7afa802019-06-14 15:41:10 -07003293 rbio_pool_free, newpoolinfo);
Kent Overstreetafeee512018-05-20 18:25:52 -04003294 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 kfree(newpoolinfo);
Kent Overstreetafeee512018-05-20 18:25:52 -04003296 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 }
Kees Cook6396bb22018-06-12 14:03:40 -07003298 newmirrors = kzalloc(array3_size(sizeof(struct raid1_info),
3299 raid_disks, 2),
NeilBrown8f19ccb2011-12-23 10:17:56 +11003300 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 if (!newmirrors) {
3302 kfree(newpoolinfo);
Kent Overstreetafeee512018-05-20 18:25:52 -04003303 mempool_exit(&newpool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 return -ENOMEM;
3305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
NeilBrowne2d59922013-06-12 11:01:22 +10003307 freeze_array(conf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
3309 /* ok, everything is stopped */
3310 oldpool = conf->r1bio_pool;
3311 conf->r1bio_pool = newpool;
NeilBrown6ea9c072005-06-21 17:17:09 -07003312
NeilBrowna88aa782007-08-22 14:01:53 -07003313 for (d = d2 = 0; d < conf->raid_disks; d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11003314 struct md_rdev *rdev = conf->mirrors[d].rdev;
NeilBrowna88aa782007-08-22 14:01:53 -07003315 if (rdev && rdev->raid_disk != d2) {
Namhyung Kim36fad852011-07-27 11:00:36 +10003316 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna88aa782007-08-22 14:01:53 -07003317 rdev->raid_disk = d2;
Namhyung Kim36fad852011-07-27 11:00:36 +10003318 sysfs_unlink_rdev(mddev, rdev);
3319 if (sysfs_link_rdev(mddev, rdev))
NeilBrown1d41c212016-11-02 14:16:50 +11003320 pr_warn("md/raid1:%s: cannot register rd%d\n",
3321 mdname(mddev), rdev->raid_disk);
NeilBrown6ea9c072005-06-21 17:17:09 -07003322 }
NeilBrowna88aa782007-08-22 14:01:53 -07003323 if (rdev)
3324 newmirrors[d2++].rdev = rdev;
3325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 kfree(conf->mirrors);
3327 conf->mirrors = newmirrors;
3328 kfree(conf->poolinfo);
3329 conf->poolinfo = newpoolinfo;
3330
NeilBrownc04be0a2006-10-03 01:15:53 -07003331 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 mddev->degraded += (raid_disks - conf->raid_disks);
NeilBrownc04be0a2006-10-03 01:15:53 -07003333 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 conf->raid_disks = mddev->raid_disks = raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08003335 mddev->delta_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
NeilBrowne2d59922013-06-12 11:01:22 +10003337 unfreeze_array(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
NeilBrown985ca972015-07-06 12:26:57 +10003339 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3341 md_wakeup_thread(mddev->thread);
3342
Kent Overstreetafeee512018-05-20 18:25:52 -04003343 mempool_exit(&oldpool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 return 0;
3345}
3346
NeilBrownb03e0cc2017-10-19 12:49:15 +11003347static void raid1_quiesce(struct mddev *mddev, int quiesce)
NeilBrown36fa3062005-09-09 16:23:45 -07003348{
NeilBrowne8096362011-10-11 16:49:05 +11003349 struct r1conf *conf = mddev->private;
NeilBrown36fa3062005-09-09 16:23:45 -07003350
NeilBrownb03e0cc2017-10-19 12:49:15 +11003351 if (quiesce)
majianpeng07169fd2013-11-14 15:16:18 +11003352 freeze_array(conf, 0);
NeilBrownb03e0cc2017-10-19 12:49:15 +11003353 else
majianpeng07169fd2013-11-14 15:16:18 +11003354 unfreeze_array(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07003355}
3356
NeilBrownfd01b882011-10-11 16:47:53 +11003357static void *raid1_takeover(struct mddev *mddev)
NeilBrown709ae482009-12-14 12:49:51 +11003358{
3359 /* raid1 can take over:
3360 * raid5 with 2 devices, any layout or chunk size
3361 */
3362 if (mddev->level == 5 && mddev->raid_disks == 2) {
NeilBrowne8096362011-10-11 16:49:05 +11003363 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11003364 mddev->new_level = 1;
3365 mddev->new_layout = 0;
3366 mddev->new_chunk_sectors = 0;
3367 conf = setup_conf(mddev);
Shaohua Li6995f0b2016-12-08 15:48:17 -08003368 if (!IS_ERR(conf)) {
majianpeng07169fd2013-11-14 15:16:18 +11003369 /* Array must appear to be quiesced */
3370 conf->array_frozen = 1;
Shaohua Li394ed8e2017-01-04 16:10:19 -08003371 mddev_clear_unsupported_flags(mddev,
3372 UNSUPPORTED_MDDEV_FLAGS);
Shaohua Li6995f0b2016-12-08 15:48:17 -08003373 }
NeilBrown709ae482009-12-14 12:49:51 +11003374 return conf;
3375 }
3376 return ERR_PTR(-EINVAL);
3377}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
NeilBrown84fc4b52011-10-11 16:49:58 +11003379static struct md_personality raid1_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380{
3381 .name = "raid1",
NeilBrown2604b702006-01-06 00:20:36 -08003382 .level = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08003384 .make_request = raid1_make_request,
3385 .run = raid1_run,
NeilBrownafa0f552014-12-15 12:56:58 +11003386 .free = raid1_free,
Shaohua Li849674e2016-01-20 13:52:20 -08003387 .status = raid1_status,
3388 .error_handler = raid1_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 .hot_add_disk = raid1_add_disk,
3390 .hot_remove_disk= raid1_remove_disk,
3391 .spare_active = raid1_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08003392 .sync_request = raid1_sync_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 .resize = raid1_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07003394 .size = raid1_size,
NeilBrown63c70c42006-03-27 01:18:13 -08003395 .check_reshape = raid1_reshape,
NeilBrown36fa3062005-09-09 16:23:45 -07003396 .quiesce = raid1_quiesce,
NeilBrown709ae482009-12-14 12:49:51 +11003397 .takeover = raid1_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11003398 .congested = raid1_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399};
3400
3401static int __init raid_init(void)
3402{
NeilBrown2604b702006-01-06 00:20:36 -08003403 return register_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404}
3405
3406static void raid_exit(void)
3407{
NeilBrown2604b702006-01-06 00:20:36 -08003408 unregister_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409}
3410
3411module_init(raid_init);
3412module_exit(raid_exit);
3413MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11003414MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415MODULE_ALIAS("md-personality-3"); /* RAID1 */
NeilBrownd9d166c2006-01-06 00:20:51 -08003416MODULE_ALIAS("md-raid1");
NeilBrown2604b702006-01-06 00:20:36 -08003417MODULE_ALIAS("md-level-1");
NeilBrown34db0cd2011-10-11 16:50:01 +11003418
3419module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);