blob: 941f810638912581a3baae127fc83e845e18b76c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid1.c : Multiple Devices driver for Linux
3 *
4 * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
5 *
6 * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
7 *
8 * RAID-1 management functions.
9 *
10 * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
11 *
Jan Engelhardt96de0e22007-10-19 23:21:04 +020012 * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
14 *
NeilBrown191ea9b2005-06-21 17:17:23 -070015 * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support
16 * bitmapped intelligence in resync:
17 *
18 * - bitmap marked during normal i/o
19 * - bitmap used to skip nondirty blocks during sync
20 *
21 * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
22 * - persistent bitmap code
23 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2, or (at your option)
27 * any later version.
28 *
29 * You should have received a copy of the GNU General Public License
30 * (for example /usr/src/linux/COPYING); if not, write to the Free
31 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 */
33
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110035#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040037#include <linux/module.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100039#include <linux/ratelimit.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010040#include <linux/sched/signal.h>
41
NeilBrown109e3762016-11-18 13:22:04 +110042#include <trace/events/block.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010043
NeilBrown43b2e5d2009-03-31 14:33:13 +110044#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110045#include "raid1.h"
46#include "bitmap.h"
NeilBrown191ea9b2005-06-21 17:17:23 -070047
Shaohua Li394ed8e2017-01-04 16:10:19 -080048#define UNSUPPORTED_MDDEV_FLAGS \
49 ((1L << MD_HAS_JOURNAL) | \
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +010050 (1L << MD_JOURNAL_CLEAN) | \
51 (1L << MD_HAS_PPL))
Shaohua Li394ed8e2017-01-04 16:10:19 -080052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
54 * Number of guaranteed r1bios in case of extreme VM load:
55 */
56#define NR_RAID1_BIOS 256
57
Jonathan Brassow473e87c2012-07-31 10:03:52 +100058/* when we get a read error on a read-only array, we redirect to another
59 * device without failing the first device, or trying to over-write to
60 * correct the read error. To keep track of bad blocks on a per-bio
61 * level, we store IO_BLOCKED in the appropriate 'bios' pointer
62 */
63#define IO_BLOCKED ((struct bio *)1)
64/* When we successfully write to a known bad-block, we need to remove the
65 * bad-block marking which must be done from process context. So we record
66 * the success by setting devs[n].bio to IO_MADE_GOOD
67 */
68#define IO_MADE_GOOD ((struct bio *)2)
69
70#define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
71
NeilBrown34db0cd2011-10-11 16:50:01 +110072/* When there are this many requests queue to be written by
73 * the raid1 thread, we become 'congested' to provide back-pressure
74 * for writeback.
75 */
76static int max_queued_requests = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
colyli@suse.defd768632017-02-18 03:05:56 +080078static void allow_barrier(struct r1conf *conf, sector_t sector_nr);
79static void lower_barrier(struct r1conf *conf, sector_t sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
NeilBrown578b54a2016-11-14 16:30:21 +110081#define raid1_log(md, fmt, args...) \
82 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid1 " fmt, ##args); } while (0)
83
Al Virodd0fc662005-10-07 07:46:04 +010084static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 struct pool_info *pi = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +110087 int size = offsetof(struct r1bio, bios[pi->raid_disks]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 /* allocate a r1bio with room for raid_disks entries in the bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +010090 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93static void r1bio_pool_free(void *r1_bio, void *data)
94{
95 kfree(r1_bio);
96}
97
98#define RESYNC_BLOCK_SIZE (64*1024)
majianpeng8e005f72013-11-14 15:16:18 +110099#define RESYNC_DEPTH 32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
101#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
majianpeng8e005f72013-11-14 15:16:18 +1100102#define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH)
103#define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9)
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +1000104#define CLUSTER_RESYNC_WINDOW (16 * RESYNC_WINDOW)
105#define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Al Virodd0fc662005-10-07 07:46:04 +0100107static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 struct pool_info *pi = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100110 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 struct bio *bio;
NeilBrownda1aab32014-04-09 12:25:43 +1000112 int need_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 int i, j;
114
115 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100116 if (!r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 /*
120 * Allocate bios : 1 for reading, n-1 for writing
121 */
122 for (j = pi->raid_disks ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100123 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (!bio)
125 goto out_free_bio;
126 r1_bio->bios[j] = bio;
127 }
128 /*
129 * Allocate RESYNC_PAGES data pages and attach them to
NeilBrownd11c1712006-01-06 00:20:26 -0800130 * the first bio.
131 * If this is a user-requested check/repair, allocate
132 * RESYNC_PAGES for each bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
NeilBrownd11c1712006-01-06 00:20:26 -0800134 if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
NeilBrownda1aab32014-04-09 12:25:43 +1000135 need_pages = pi->raid_disks;
NeilBrownd11c1712006-01-06 00:20:26 -0800136 else
NeilBrownda1aab32014-04-09 12:25:43 +1000137 need_pages = 1;
138 for (j = 0; j < need_pages; j++) {
NeilBrownd11c1712006-01-06 00:20:26 -0800139 bio = r1_bio->bios[j];
Kent Overstreeta0787602012-09-10 14:03:28 -0700140 bio->bi_vcnt = RESYNC_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Kent Overstreeta0787602012-09-10 14:03:28 -0700142 if (bio_alloc_pages(bio, gfp_flags))
NeilBrownda1aab32014-04-09 12:25:43 +1000143 goto out_free_pages;
NeilBrownd11c1712006-01-06 00:20:26 -0800144 }
145 /* If not user-requests, copy the page pointers to all bios */
146 if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) {
147 for (i=0; i<RESYNC_PAGES ; i++)
148 for (j=1; j<pi->raid_disks; j++)
149 r1_bio->bios[j]->bi_io_vec[i].bv_page =
150 r1_bio->bios[0]->bi_io_vec[i].bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152
153 r1_bio->master_bio = NULL;
154
155 return r1_bio;
156
NeilBrownda1aab32014-04-09 12:25:43 +1000157out_free_pages:
Guoqing Jiang491221f2016-09-22 03:10:01 -0400158 while (--j >= 0)
159 bio_free_pages(r1_bio->bios[j]);
NeilBrownda1aab32014-04-09 12:25:43 +1000160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161out_free_bio:
NeilBrown8f19ccb2011-12-23 10:17:56 +1100162 while (++j < pi->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 bio_put(r1_bio->bios[j]);
164 r1bio_pool_free(r1_bio, data);
165 return NULL;
166}
167
168static void r1buf_pool_free(void *__r1_bio, void *data)
169{
170 struct pool_info *pi = data;
NeilBrownd11c1712006-01-06 00:20:26 -0800171 int i,j;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100172 struct r1bio *r1bio = __r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
NeilBrownd11c1712006-01-06 00:20:26 -0800174 for (i = 0; i < RESYNC_PAGES; i++)
175 for (j = pi->raid_disks; j-- ;) {
176 if (j == 0 ||
177 r1bio->bios[j]->bi_io_vec[i].bv_page !=
178 r1bio->bios[0]->bi_io_vec[i].bv_page)
NeilBrown1345b1d2006-01-06 00:20:40 -0800179 safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page);
NeilBrownd11c1712006-01-06 00:20:26 -0800180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 for (i=0 ; i < pi->raid_disks; i++)
182 bio_put(r1bio->bios[i]);
183
184 r1bio_pool_free(r1bio, data);
185}
186
NeilBrowne8096362011-10-11 16:49:05 +1100187static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 int i;
190
NeilBrown8f19ccb2011-12-23 10:17:56 +1100191 for (i = 0; i < conf->raid_disks * 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct bio **bio = r1_bio->bios + i;
NeilBrown4367af52011-07-28 11:31:49 +1000193 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 bio_put(*bio);
195 *bio = NULL;
196 }
197}
198
NeilBrown9f2c9d12011-10-11 16:48:43 +1100199static void free_r1bio(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
NeilBrowne8096362011-10-11 16:49:05 +1100201 struct r1conf *conf = r1_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 put_all_bios(conf, r1_bio);
204 mempool_free(r1_bio, conf->r1bio_pool);
205}
206
NeilBrown9f2c9d12011-10-11 16:48:43 +1100207static void put_buf(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
NeilBrowne8096362011-10-11 16:49:05 +1100209 struct r1conf *conf = r1_bio->mddev->private;
Shaohua Liaf5f42a2017-02-19 22:41:27 -0800210 sector_t sect = r1_bio->sector;
NeilBrown3e198f72006-01-06 00:20:21 -0800211 int i;
212
NeilBrown8f19ccb2011-12-23 10:17:56 +1100213 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown3e198f72006-01-06 00:20:21 -0800214 struct bio *bio = r1_bio->bios[i];
215 if (bio->bi_end_io)
216 rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 mempool_free(r1_bio, conf->r1buf_pool);
220
Shaohua Liaf5f42a2017-02-19 22:41:27 -0800221 lower_barrier(conf, sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
NeilBrown9f2c9d12011-10-11 16:48:43 +1100224static void reschedule_retry(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100227 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +1100228 struct r1conf *conf = mddev->private;
colyli@suse.defd768632017-02-18 03:05:56 +0800229 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
colyli@suse.defd768632017-02-18 03:05:56 +0800231 idx = sector_to_idx(r1_bio->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 spin_lock_irqsave(&conf->device_lock, flags);
233 list_add(&r1_bio->retry_list, &conf->retry_list);
colyli@suse.de824e47d2017-02-18 03:05:57 +0800234 atomic_inc(&conf->nr_queued[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 spin_unlock_irqrestore(&conf->device_lock, flags);
236
NeilBrown17999be2006-01-06 00:20:12 -0800237 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 md_wakeup_thread(mddev->thread);
239}
240
241/*
242 * raid_end_bio_io() is called when we have finished servicing a mirrored
243 * operation and are ready to return a success/failure code to the buffer
244 * cache layer.
245 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100246static void call_bio_endio(struct r1bio *r1_bio)
NeilBrownd2eb35a2011-07-28 11:31:48 +1000247{
248 struct bio *bio = r1_bio->master_bio;
NeilBrowne8096362011-10-11 16:49:05 +1100249 struct r1conf *conf = r1_bio->mddev->private;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000250
251 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200252 bio->bi_error = -EIO;
253
NeilBrown37011e32017-03-15 14:05:14 +1100254 bio_endio(bio);
255 /*
256 * Wake up any possible resync thread that waits for the device
257 * to go idle.
258 */
259 allow_barrier(conf, r1_bio->sector);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000260}
261
NeilBrown9f2c9d12011-10-11 16:48:43 +1100262static void raid_end_bio_io(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 struct bio *bio = r1_bio->master_bio;
265
NeilBrown4b6d2872005-09-09 16:23:47 -0700266 /* if nobody has done the final endio yet, do it now */
267 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
NeilBrown36a4e1f2011-10-07 14:23:17 +1100268 pr_debug("raid1: sync end %s on sectors %llu-%llu\n",
269 (bio_data_dir(bio) == WRITE) ? "write" : "read",
Kent Overstreet4f024f32013-10-11 15:44:27 -0700270 (unsigned long long) bio->bi_iter.bi_sector,
271 (unsigned long long) bio_end_sector(bio) - 1);
NeilBrown4b6d2872005-09-09 16:23:47 -0700272
NeilBrownd2eb35a2011-07-28 11:31:48 +1000273 call_bio_endio(r1_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -0700274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 free_r1bio(r1_bio);
276}
277
278/*
279 * Update disk head position estimator based on IRQ completion info.
280 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100281static inline void update_head_pos(int disk, struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
NeilBrowne8096362011-10-11 16:49:05 +1100283 struct r1conf *conf = r1_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 conf->mirrors[disk].head_position =
286 r1_bio->sector + (r1_bio->sectors);
287}
288
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100289/*
290 * Find the disk number which triggered given bio
291 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100292static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio)
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100293{
294 int mirror;
NeilBrown30194632011-12-23 10:17:56 +1100295 struct r1conf *conf = r1_bio->mddev->private;
296 int raid_disks = conf->raid_disks;
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100297
NeilBrown8f19ccb2011-12-23 10:17:56 +1100298 for (mirror = 0; mirror < raid_disks * 2; mirror++)
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100299 if (r1_bio->bios[mirror] == bio)
300 break;
301
NeilBrown8f19ccb2011-12-23 10:17:56 +1100302 BUG_ON(mirror == raid_disks * 2);
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100303 update_head_pos(mirror, r1_bio);
304
305 return mirror;
306}
307
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200308static void raid1_end_read_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200310 int uptodate = !bio->bi_error;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100311 struct r1bio *r1_bio = bio->bi_private;
NeilBrowne8096362011-10-11 16:49:05 +1100312 struct r1conf *conf = r1_bio->mddev->private;
NeilBrowne5872d52016-06-02 16:19:52 +1000313 struct md_rdev *rdev = conf->mirrors[r1_bio->read_disk].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /*
316 * this branch is our 'one mirror IO has finished' event handler:
317 */
NeilBrowne5872d52016-06-02 16:19:52 +1000318 update_head_pos(r1_bio->read_disk, r1_bio);
NeilBrownddaf22a2006-01-06 00:20:19 -0800319
NeilBrowndd00a992007-05-10 03:15:50 -0700320 if (uptodate)
321 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrown2e52d442016-11-18 16:16:12 +1100322 else if (test_bit(FailFast, &rdev->flags) &&
323 test_bit(R1BIO_FailFast, &r1_bio->state))
324 /* This was a fail-fast read so we definitely
325 * want to retry */
326 ;
NeilBrowndd00a992007-05-10 03:15:50 -0700327 else {
328 /* If all other devices have failed, we want to return
329 * the error upwards rather than fail the last device.
330 * Here we redefine "uptodate" to mean "Don't want to retry"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
NeilBrowndd00a992007-05-10 03:15:50 -0700332 unsigned long flags;
333 spin_lock_irqsave(&conf->device_lock, flags);
334 if (r1_bio->mddev->degraded == conf->raid_disks ||
335 (r1_bio->mddev->degraded == conf->raid_disks-1 &&
NeilBrowne5872d52016-06-02 16:19:52 +1000336 test_bit(In_sync, &rdev->flags)))
NeilBrowndd00a992007-05-10 03:15:50 -0700337 uptodate = 1;
338 spin_unlock_irqrestore(&conf->device_lock, flags);
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
NeilBrown7ad4d4a2012-10-11 13:44:30 +1100341 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 raid_end_bio_io(r1_bio);
NeilBrowne5872d52016-06-02 16:19:52 +1000343 rdev_dec_pending(rdev, conf->mddev);
NeilBrown7ad4d4a2012-10-11 13:44:30 +1100344 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /*
346 * oops, read error:
347 */
348 char b[BDEVNAME_SIZE];
NeilBrown1d41c212016-11-02 14:16:50 +1100349 pr_err_ratelimited("md/raid1:%s: %s: rescheduling sector %llu\n",
350 mdname(conf->mddev),
351 bdevname(rdev->bdev, b),
352 (unsigned long long)r1_bio->sector);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000353 set_bit(R1BIO_ReadError, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 reschedule_retry(r1_bio);
NeilBrown7ad4d4a2012-10-11 13:44:30 +1100355 /* don't drop the reference on read_disk yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
NeilBrown9f2c9d12011-10-11 16:48:43 +1100359static void close_write(struct r1bio *r1_bio)
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000360{
361 /* it really is the end of this request */
362 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
363 /* free extra copy of the data pages */
364 int i = r1_bio->behind_page_count;
365 while (i--)
366 safe_put_page(r1_bio->behind_bvecs[i].bv_page);
367 kfree(r1_bio->behind_bvecs);
368 r1_bio->behind_bvecs = NULL;
369 }
370 /* clear the bitmap if all writes complete successfully */
371 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
372 r1_bio->sectors,
373 !test_bit(R1BIO_Degraded, &r1_bio->state),
374 test_bit(R1BIO_BehindIO, &r1_bio->state));
375 md_write_end(r1_bio->mddev);
376}
377
NeilBrown9f2c9d12011-10-11 16:48:43 +1100378static void r1_bio_write_done(struct r1bio *r1_bio)
NeilBrown4e780642010-10-19 12:54:01 +1100379{
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000380 if (!atomic_dec_and_test(&r1_bio->remaining))
381 return;
382
383 if (test_bit(R1BIO_WriteError, &r1_bio->state))
384 reschedule_retry(r1_bio);
385 else {
386 close_write(r1_bio);
NeilBrown4367af52011-07-28 11:31:49 +1000387 if (test_bit(R1BIO_MadeGood, &r1_bio->state))
388 reschedule_retry(r1_bio);
389 else
390 raid_end_bio_io(r1_bio);
NeilBrown4e780642010-10-19 12:54:01 +1100391 }
392}
393
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200394static void raid1_end_write_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
NeilBrown9f2c9d12011-10-11 16:48:43 +1100396 struct r1bio *r1_bio = bio->bi_private;
NeilBrowne5872d52016-06-02 16:19:52 +1000397 int behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
NeilBrowne8096362011-10-11 16:49:05 +1100398 struct r1conf *conf = r1_bio->mddev->private;
NeilBrown04b857f2006-03-09 17:33:46 -0800399 struct bio *to_put = NULL;
NeilBrowne5872d52016-06-02 16:19:52 +1000400 int mirror = find_bio_disk(r1_bio, bio);
401 struct md_rdev *rdev = conf->mirrors[mirror].rdev;
Shaohua Lie3f948c2016-10-06 14:09:16 -0700402 bool discard_error;
403
404 discard_error = bio->bi_error && bio_op(bio) == REQ_OP_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Tejun Heoe9c74692010-09-03 11:56:18 +0200406 /*
407 * 'one mirror IO has finished' event handler:
408 */
Shaohua Lie3f948c2016-10-06 14:09:16 -0700409 if (bio->bi_error && !discard_error) {
NeilBrowne5872d52016-06-02 16:19:52 +1000410 set_bit(WriteErrorSeen, &rdev->flags);
411 if (!test_and_set_bit(WantReplacement, &rdev->flags))
NeilBrown19d67162011-12-23 10:17:57 +1100412 set_bit(MD_RECOVERY_NEEDED, &
413 conf->mddev->recovery);
414
NeilBrown212e7eb2016-11-18 16:16:12 +1100415 if (test_bit(FailFast, &rdev->flags) &&
416 (bio->bi_opf & MD_FAILFAST) &&
417 /* We never try FailFast to WriteMostly devices */
418 !test_bit(WriteMostly, &rdev->flags)) {
419 md_error(r1_bio->mddev, rdev);
420 if (!test_bit(Faulty, &rdev->flags))
421 /* This is the only remaining device,
422 * We need to retry the write without
423 * FailFast
424 */
425 set_bit(R1BIO_WriteError, &r1_bio->state);
426 else {
427 /* Finished with this branch */
428 r1_bio->bios[mirror] = NULL;
429 to_put = bio;
430 }
431 } else
432 set_bit(R1BIO_WriteError, &r1_bio->state);
NeilBrown4367af52011-07-28 11:31:49 +1000433 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /*
Tejun Heoe9c74692010-09-03 11:56:18 +0200435 * Set R1BIO_Uptodate in our master bio, so that we
436 * will return a good error code for to the higher
437 * levels even if IO on some other mirrored buffer
438 * fails.
439 *
440 * The 'master' represents the composite IO operation
441 * to user-side. So if something waits for IO, then it
442 * will wait for the 'master' bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 */
NeilBrown4367af52011-07-28 11:31:49 +1000444 sector_t first_bad;
445 int bad_sectors;
446
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000447 r1_bio->bios[mirror] = NULL;
448 to_put = bio;
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300449 /*
450 * Do not set R1BIO_Uptodate if the current device is
451 * rebuilding or Faulty. This is because we cannot use
452 * such device for properly reading the data back (we could
453 * potentially use it, if the current write would have felt
454 * before rdev->recovery_offset, but for simplicity we don't
455 * check this here.
456 */
NeilBrowne5872d52016-06-02 16:19:52 +1000457 if (test_bit(In_sync, &rdev->flags) &&
458 !test_bit(Faulty, &rdev->flags))
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300459 set_bit(R1BIO_Uptodate, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
NeilBrown4367af52011-07-28 11:31:49 +1000461 /* Maybe we can clear some bad blocks. */
NeilBrowne5872d52016-06-02 16:19:52 +1000462 if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
Shaohua Lie3f948c2016-10-06 14:09:16 -0700463 &first_bad, &bad_sectors) && !discard_error) {
NeilBrown4367af52011-07-28 11:31:49 +1000464 r1_bio->bios[mirror] = IO_MADE_GOOD;
465 set_bit(R1BIO_MadeGood, &r1_bio->state);
466 }
467 }
468
Tejun Heoe9c74692010-09-03 11:56:18 +0200469 if (behind) {
NeilBrowne5872d52016-06-02 16:19:52 +1000470 if (test_bit(WriteMostly, &rdev->flags))
Tejun Heoe9c74692010-09-03 11:56:18 +0200471 atomic_dec(&r1_bio->behind_remaining);
NeilBrown4b6d2872005-09-09 16:23:47 -0700472
Tejun Heoe9c74692010-09-03 11:56:18 +0200473 /*
474 * In behind mode, we ACK the master bio once the I/O
475 * has safely reached all non-writemostly
476 * disks. Setting the Returned bit ensures that this
477 * gets done only once -- we don't ever want to return
478 * -EIO here, instead we'll wait
479 */
480 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
481 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
482 /* Maybe we can return now */
483 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
484 struct bio *mbio = r1_bio->master_bio;
NeilBrown36a4e1f2011-10-07 14:23:17 +1100485 pr_debug("raid1: behind end write sectors"
486 " %llu-%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -0700487 (unsigned long long) mbio->bi_iter.bi_sector,
488 (unsigned long long) bio_end_sector(mbio) - 1);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000489 call_bio_endio(r1_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -0700490 }
491 }
492 }
NeilBrown4367af52011-07-28 11:31:49 +1000493 if (r1_bio->bios[mirror] == NULL)
NeilBrowne5872d52016-06-02 16:19:52 +1000494 rdev_dec_pending(rdev, conf->mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 * Let's see if all mirrored write operations have finished
498 * already.
499 */
NeilBrownaf6d7b72011-05-11 14:51:19 +1000500 r1_bio_write_done(r1_bio);
NeilBrownc70810b2006-06-26 00:27:35 -0700501
NeilBrown04b857f2006-03-09 17:33:46 -0800502 if (to_put)
503 bio_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
colyli@suse.defd768632017-02-18 03:05:56 +0800506static sector_t align_to_barrier_unit_end(sector_t start_sector,
507 sector_t sectors)
508{
509 sector_t len;
510
511 WARN_ON(sectors == 0);
512 /*
513 * len is the number of sectors from start_sector to end of the
514 * barrier unit which start_sector belongs to.
515 */
516 len = round_up(start_sector + 1, BARRIER_UNIT_SECTOR_SIZE) -
517 start_sector;
518
519 if (len > sectors)
520 len = sectors;
521
522 return len;
523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/*
526 * This routine returns the disk from which the requested read should
527 * be done. There is a per-array 'next expected sequential IO' sector
528 * number - if this matches on the next IO then we use the last disk.
529 * There is also a per-disk 'last know head position' sector that is
530 * maintained from IRQ contexts, both the normal and the resync IO
531 * completion handlers update this position correctly. If there is no
532 * perfect sequential match then we pick the disk whose head is closest.
533 *
534 * If there are 2 mirrors in the same 2 devices, performance degrades
535 * because position is mirror, not device based.
536 *
537 * The rdev for the device selected will have nr_pending incremented.
538 */
NeilBrowne8096362011-10-11 16:49:05 +1100539static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000541 const sector_t this_sector = r1_bio->sector;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000542 int sectors;
543 int best_good_sectors;
Shaohua Li9dedf602012-07-31 10:03:53 +1000544 int best_disk, best_dist_disk, best_pending_disk;
545 int has_nonrot_disk;
Shaohua Libe4d3282012-07-31 10:03:53 +1000546 int disk;
NeilBrown76073052011-05-11 14:34:56 +1000547 sector_t best_dist;
Shaohua Li9dedf602012-07-31 10:03:53 +1000548 unsigned int min_pending;
NeilBrown3cb03002011-10-11 16:45:26 +1100549 struct md_rdev *rdev;
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000550 int choose_first;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000551 int choose_next_idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 rcu_read_lock();
554 /*
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700555 * Check if we can balance. We can balance on the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * device if no resync is going on, or below the resync window.
557 * We take the first readable disk when above the resync window.
558 */
559 retry:
NeilBrownd2eb35a2011-07-28 11:31:48 +1000560 sectors = r1_bio->sectors;
NeilBrown76073052011-05-11 14:34:56 +1000561 best_disk = -1;
Shaohua Li9dedf602012-07-31 10:03:53 +1000562 best_dist_disk = -1;
NeilBrown76073052011-05-11 14:34:56 +1000563 best_dist = MaxSector;
Shaohua Li9dedf602012-07-31 10:03:53 +1000564 best_pending_disk = -1;
565 min_pending = UINT_MAX;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000566 best_good_sectors = 0;
Shaohua Li9dedf602012-07-31 10:03:53 +1000567 has_nonrot_disk = 0;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000568 choose_next_idle = 0;
NeilBrown2e52d442016-11-18 16:16:12 +1100569 clear_bit(R1BIO_FailFast, &r1_bio->state);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000570
Goldwyn Rodrigues7d49ffc2014-08-12 10:13:19 -0500571 if ((conf->mddev->recovery_cp < this_sector + sectors) ||
572 (mddev_is_clustered(conf->mddev) &&
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500573 md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
Goldwyn Rodrigues7d49ffc2014-08-12 10:13:19 -0500574 this_sector + sectors)))
575 choose_first = 1;
576 else
577 choose_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Shaohua Libe4d3282012-07-31 10:03:53 +1000579 for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
NeilBrown76073052011-05-11 14:34:56 +1000580 sector_t dist;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000581 sector_t first_bad;
582 int bad_sectors;
Shaohua Li9dedf602012-07-31 10:03:53 +1000583 unsigned int pending;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000584 bool nonrot;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000585
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000586 rdev = rcu_dereference(conf->mirrors[disk].rdev);
587 if (r1_bio->bios[disk] == IO_BLOCKED
588 || rdev == NULL
NeilBrown76073052011-05-11 14:34:56 +1000589 || test_bit(Faulty, &rdev->flags))
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000590 continue;
NeilBrown76073052011-05-11 14:34:56 +1000591 if (!test_bit(In_sync, &rdev->flags) &&
592 rdev->recovery_offset < this_sector + sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 continue;
NeilBrown76073052011-05-11 14:34:56 +1000594 if (test_bit(WriteMostly, &rdev->flags)) {
595 /* Don't balance among write-mostly, just
596 * use the first as a last resort */
Tomáš Hodekd1901ef2015-02-23 11:00:38 +1100597 if (best_dist_disk < 0) {
NeilBrown307729c2012-01-09 01:41:51 +1100598 if (is_badblock(rdev, this_sector, sectors,
599 &first_bad, &bad_sectors)) {
Wei Fang816b0ac2016-03-21 19:18:32 +0800600 if (first_bad <= this_sector)
NeilBrown307729c2012-01-09 01:41:51 +1100601 /* Cannot use this */
602 continue;
603 best_good_sectors = first_bad - this_sector;
604 } else
605 best_good_sectors = sectors;
Tomáš Hodekd1901ef2015-02-23 11:00:38 +1100606 best_dist_disk = disk;
607 best_pending_disk = disk;
NeilBrown307729c2012-01-09 01:41:51 +1100608 }
NeilBrown76073052011-05-11 14:34:56 +1000609 continue;
610 }
611 /* This is a reasonable device to use. It might
612 * even be best.
613 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000614 if (is_badblock(rdev, this_sector, sectors,
615 &first_bad, &bad_sectors)) {
616 if (best_dist < MaxSector)
617 /* already have a better device */
618 continue;
619 if (first_bad <= this_sector) {
620 /* cannot read here. If this is the 'primary'
621 * device, then we must not read beyond
622 * bad_sectors from another device..
623 */
624 bad_sectors -= (this_sector - first_bad);
625 if (choose_first && sectors > bad_sectors)
626 sectors = bad_sectors;
627 if (best_good_sectors > sectors)
628 best_good_sectors = sectors;
629
630 } else {
631 sector_t good_sectors = first_bad - this_sector;
632 if (good_sectors > best_good_sectors) {
633 best_good_sectors = good_sectors;
634 best_disk = disk;
635 }
636 if (choose_first)
637 break;
638 }
639 continue;
640 } else
641 best_good_sectors = sectors;
642
NeilBrown2e52d442016-11-18 16:16:12 +1100643 if (best_disk >= 0)
644 /* At least two disks to choose from so failfast is OK */
645 set_bit(R1BIO_FailFast, &r1_bio->state);
646
Shaohua Li12cee5a2012-07-31 10:03:53 +1000647 nonrot = blk_queue_nonrot(bdev_get_queue(rdev->bdev));
648 has_nonrot_disk |= nonrot;
Shaohua Li9dedf602012-07-31 10:03:53 +1000649 pending = atomic_read(&rdev->nr_pending);
NeilBrown76073052011-05-11 14:34:56 +1000650 dist = abs(this_sector - conf->mirrors[disk].head_position);
Shaohua Li12cee5a2012-07-31 10:03:53 +1000651 if (choose_first) {
NeilBrown76073052011-05-11 14:34:56 +1000652 best_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 break;
654 }
Shaohua Li12cee5a2012-07-31 10:03:53 +1000655 /* Don't change to another disk for sequential reads */
656 if (conf->mirrors[disk].next_seq_sect == this_sector
657 || dist == 0) {
658 int opt_iosize = bdev_io_opt(rdev->bdev) >> 9;
659 struct raid1_info *mirror = &conf->mirrors[disk];
660
661 best_disk = disk;
662 /*
663 * If buffered sequential IO size exceeds optimal
664 * iosize, check if there is idle disk. If yes, choose
665 * the idle disk. read_balance could already choose an
666 * idle disk before noticing it's a sequential IO in
667 * this disk. This doesn't matter because this disk
668 * will idle, next time it will be utilized after the
669 * first disk has IO size exceeds optimal iosize. In
670 * this way, iosize of the first disk will be optimal
671 * iosize at least. iosize of the second disk might be
672 * small, but not a big deal since when the second disk
673 * starts IO, the first disk is likely still busy.
674 */
675 if (nonrot && opt_iosize > 0 &&
676 mirror->seq_start != MaxSector &&
677 mirror->next_seq_sect > opt_iosize &&
678 mirror->next_seq_sect - opt_iosize >=
679 mirror->seq_start) {
680 choose_next_idle = 1;
681 continue;
682 }
683 break;
684 }
Shaohua Li12cee5a2012-07-31 10:03:53 +1000685
686 if (choose_next_idle)
687 continue;
Shaohua Li9dedf602012-07-31 10:03:53 +1000688
689 if (min_pending > pending) {
690 min_pending = pending;
691 best_pending_disk = disk;
692 }
693
NeilBrown76073052011-05-11 14:34:56 +1000694 if (dist < best_dist) {
695 best_dist = dist;
Shaohua Li9dedf602012-07-31 10:03:53 +1000696 best_dist_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Shaohua Li9dedf602012-07-31 10:03:53 +1000700 /*
701 * If all disks are rotational, choose the closest disk. If any disk is
702 * non-rotational, choose the disk with less pending request even the
703 * disk is rotational, which might/might not be optimal for raids with
704 * mixed ratation/non-rotational disks depending on workload.
705 */
706 if (best_disk == -1) {
NeilBrown2e52d442016-11-18 16:16:12 +1100707 if (has_nonrot_disk || min_pending == 0)
Shaohua Li9dedf602012-07-31 10:03:53 +1000708 best_disk = best_pending_disk;
709 else
710 best_disk = best_dist_disk;
711 }
712
NeilBrown76073052011-05-11 14:34:56 +1000713 if (best_disk >= 0) {
714 rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700715 if (!rdev)
716 goto retry;
717 atomic_inc(&rdev->nr_pending);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000718 sectors = best_good_sectors;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000719
720 if (conf->mirrors[best_disk].next_seq_sect != this_sector)
721 conf->mirrors[best_disk].seq_start = this_sector;
722
Shaohua Libe4d3282012-07-31 10:03:53 +1000723 conf->mirrors[best_disk].next_seq_sect = this_sector + sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725 rcu_read_unlock();
NeilBrownd2eb35a2011-07-28 11:31:48 +1000726 *max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
NeilBrown76073052011-05-11 14:34:56 +1000728 return best_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
NeilBrown5c675f82014-12-15 12:56:56 +1100731static int raid1_congested(struct mddev *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700732{
NeilBrowne8096362011-10-11 16:49:05 +1100733 struct r1conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700734 int i, ret = 0;
735
Tejun Heo44522262015-05-22 17:13:26 -0400736 if ((bits & (1 << WB_async_congested)) &&
NeilBrown34db0cd2011-10-11 16:50:01 +1100737 conf->pending_count >= max_queued_requests)
738 return 1;
739
NeilBrown0d129222006-10-03 01:15:54 -0700740 rcu_read_lock();
NeilBrownf53e29f2012-02-13 14:24:05 +1100741 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100742 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700743 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200744 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700745
Jonathan Brassow1ed72422011-06-07 17:50:35 -0500746 BUG_ON(!q);
747
NeilBrown0d129222006-10-03 01:15:54 -0700748 /* Note the '|| 1' - when read_balance prefers
749 * non-congested targets, it can be removed
750 */
Tejun Heo44522262015-05-22 17:13:26 -0400751 if ((bits & (1 << WB_async_congested)) || 1)
Jan Karadc3b17c2017-02-02 15:56:50 +0100752 ret |= bdi_congested(q->backing_dev_info, bits);
NeilBrown0d129222006-10-03 01:15:54 -0700753 else
Jan Karadc3b17c2017-02-02 15:56:50 +0100754 ret &= bdi_congested(q->backing_dev_info, bits);
NeilBrown0d129222006-10-03 01:15:54 -0700755 }
756 }
757 rcu_read_unlock();
758 return ret;
759}
NeilBrown0d129222006-10-03 01:15:54 -0700760
NeilBrowne8096362011-10-11 16:49:05 +1100761static void flush_pending_writes(struct r1conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800762{
763 /* Any writes that have been queued but are awaiting
764 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800765 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800766 spin_lock_irq(&conf->device_lock);
767
768 if (conf->pending_bio_list.head) {
769 struct bio *bio;
770 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100771 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800772 spin_unlock_irq(&conf->device_lock);
773 /* flush any pending bitmap writes to
774 * disk before proceeding w/ I/O */
775 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100776 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800777
778 while (bio) { /* submit pending writes */
779 struct bio *next = bio->bi_next;
NeilBrown5e2c7a32016-11-04 16:46:03 +1100780 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrowna35e63e2008-03-04 14:29:29 -0800781 bio->bi_next = NULL;
NeilBrown5e2c7a32016-11-04 16:46:03 +1100782 bio->bi_bdev = rdev->bdev;
783 if (test_bit(Faulty, &rdev->flags)) {
784 bio->bi_error = -EIO;
785 bio_endio(bio);
786 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
787 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li2ff8cc22012-10-11 13:28:54 +1100788 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200789 bio_endio(bio);
Shaohua Li2ff8cc22012-10-11 13:28:54 +1100790 else
791 generic_make_request(bio);
NeilBrowna35e63e2008-03-04 14:29:29 -0800792 bio = next;
793 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800794 } else
795 spin_unlock_irq(&conf->device_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100796}
797
NeilBrown17999be2006-01-06 00:20:12 -0800798/* Barriers....
799 * Sometimes we need to suspend IO while we do something else,
800 * either some resync/recovery, or reconfigure the array.
801 * To do this we raise a 'barrier'.
802 * The 'barrier' is a counter that can be raised multiple times
803 * to count how many activities are happening which preclude
804 * normal IO.
805 * We can only raise the barrier if there is no pending IO.
806 * i.e. if nr_pending == 0.
807 * We choose only to raise the barrier if no-one is waiting for the
808 * barrier to go down. This means that as soon as an IO request
809 * is ready, no other operations which require a barrier will start
810 * until the IO request has had a chance.
811 *
812 * So: regular IO calls 'wait_barrier'. When that returns there
813 * is no backgroup IO happening, It must arrange to call
814 * allow_barrier when it has finished its IO.
815 * backgroup IO calls must call raise_barrier. Once that returns
816 * there is no normal IO happeing. It must arrange to call
817 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 */
NeilBrownc2fd4c92014-09-10 16:01:24 +1000819static void raise_barrier(struct r1conf *conf, sector_t sector_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
colyli@suse.defd768632017-02-18 03:05:56 +0800821 int idx = sector_to_idx(sector_nr);
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 spin_lock_irq(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800824
825 /* Wait until no block IO is waiting */
colyli@suse.de824e47d2017-02-18 03:05:57 +0800826 wait_event_lock_irq(conf->wait_barrier,
827 !atomic_read(&conf->nr_waiting[idx]),
Lukas Czernereed8c022012-11-30 11:42:40 +0100828 conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800829
830 /* block any new IO from starting */
colyli@suse.de824e47d2017-02-18 03:05:57 +0800831 atomic_inc(&conf->barrier[idx]);
832 /*
833 * In raise_barrier() we firstly increase conf->barrier[idx] then
834 * check conf->nr_pending[idx]. In _wait_barrier() we firstly
835 * increase conf->nr_pending[idx] then check conf->barrier[idx].
836 * A memory barrier here to make sure conf->nr_pending[idx] won't
837 * be fetched before conf->barrier[idx] is increased. Otherwise
838 * there will be a race between raise_barrier() and _wait_barrier().
839 */
840 smp_mb__after_atomic();
NeilBrown17999be2006-01-06 00:20:12 -0800841
majianpeng79ef3a82013-11-15 14:55:02 +0800842 /* For these conditions we must wait:
843 * A: while the array is in frozen state
colyli@suse.defd768632017-02-18 03:05:56 +0800844 * B: while conf->nr_pending[idx] is not 0, meaning regular I/O
845 * existing in corresponding I/O barrier bucket.
846 * C: while conf->barrier[idx] >= RESYNC_DEPTH, meaning reaches
847 * max resync count which allowed on current I/O barrier bucket.
majianpeng79ef3a82013-11-15 14:55:02 +0800848 */
NeilBrown17999be2006-01-06 00:20:12 -0800849 wait_event_lock_irq(conf->wait_barrier,
majianpengb364e3d2013-11-14 15:16:18 +1100850 !conf->array_frozen &&
colyli@suse.de824e47d2017-02-18 03:05:57 +0800851 !atomic_read(&conf->nr_pending[idx]) &&
852 atomic_read(&conf->barrier[idx]) < RESYNC_DEPTH,
Lukas Czernereed8c022012-11-30 11:42:40 +0100853 conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800854
colyli@suse.de824e47d2017-02-18 03:05:57 +0800855 atomic_inc(&conf->nr_pending[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 spin_unlock_irq(&conf->resync_lock);
857}
858
colyli@suse.defd768632017-02-18 03:05:56 +0800859static void lower_barrier(struct r1conf *conf, sector_t sector_nr)
NeilBrown17999be2006-01-06 00:20:12 -0800860{
colyli@suse.defd768632017-02-18 03:05:56 +0800861 int idx = sector_to_idx(sector_nr);
862
colyli@suse.de824e47d2017-02-18 03:05:57 +0800863 BUG_ON(atomic_read(&conf->barrier[idx]) <= 0);
colyli@suse.defd768632017-02-18 03:05:56 +0800864
colyli@suse.de824e47d2017-02-18 03:05:57 +0800865 atomic_dec(&conf->barrier[idx]);
866 atomic_dec(&conf->nr_pending[idx]);
NeilBrown17999be2006-01-06 00:20:12 -0800867 wake_up(&conf->wait_barrier);
868}
869
colyli@suse.defd768632017-02-18 03:05:56 +0800870static void _wait_barrier(struct r1conf *conf, int idx)
NeilBrown17999be2006-01-06 00:20:12 -0800871{
colyli@suse.de824e47d2017-02-18 03:05:57 +0800872 /*
873 * We need to increase conf->nr_pending[idx] very early here,
874 * then raise_barrier() can be blocked when it waits for
875 * conf->nr_pending[idx] to be 0. Then we can avoid holding
876 * conf->resync_lock when there is no barrier raised in same
877 * barrier unit bucket. Also if the array is frozen, I/O
878 * should be blocked until array is unfrozen.
879 */
880 atomic_inc(&conf->nr_pending[idx]);
881 /*
882 * In _wait_barrier() we firstly increase conf->nr_pending[idx], then
883 * check conf->barrier[idx]. In raise_barrier() we firstly increase
884 * conf->barrier[idx], then check conf->nr_pending[idx]. A memory
885 * barrier is necessary here to make sure conf->barrier[idx] won't be
886 * fetched before conf->nr_pending[idx] is increased. Otherwise there
887 * will be a race between _wait_barrier() and raise_barrier().
888 */
889 smp_mb__after_atomic();
majianpeng79ef3a82013-11-15 14:55:02 +0800890
colyli@suse.de824e47d2017-02-18 03:05:57 +0800891 /*
892 * Don't worry about checking two atomic_t variables at same time
893 * here. If during we check conf->barrier[idx], the array is
894 * frozen (conf->array_frozen is 1), and chonf->barrier[idx] is
895 * 0, it is safe to return and make the I/O continue. Because the
896 * array is frozen, all I/O returned here will eventually complete
897 * or be queued, no race will happen. See code comment in
898 * frozen_array().
899 */
900 if (!READ_ONCE(conf->array_frozen) &&
901 !atomic_read(&conf->barrier[idx]))
902 return;
majianpeng79ef3a82013-11-15 14:55:02 +0800903
colyli@suse.de824e47d2017-02-18 03:05:57 +0800904 /*
905 * After holding conf->resync_lock, conf->nr_pending[idx]
906 * should be decreased before waiting for barrier to drop.
907 * Otherwise, we may encounter a race condition because
908 * raise_barrer() might be waiting for conf->nr_pending[idx]
909 * to be 0 at same time.
910 */
911 spin_lock_irq(&conf->resync_lock);
912 atomic_inc(&conf->nr_waiting[idx]);
913 atomic_dec(&conf->nr_pending[idx]);
914 /*
915 * In case freeze_array() is waiting for
916 * get_unqueued_pending() == extra
917 */
918 wake_up(&conf->wait_barrier);
919 /* Wait for the barrier in same barrier unit bucket to drop. */
920 wait_event_lock_irq(conf->wait_barrier,
921 !conf->array_frozen &&
922 !atomic_read(&conf->barrier[idx]),
923 conf->resync_lock);
924 atomic_inc(&conf->nr_pending[idx]);
925 atomic_dec(&conf->nr_waiting[idx]);
colyli@suse.defd768632017-02-18 03:05:56 +0800926 spin_unlock_irq(&conf->resync_lock);
majianpeng79ef3a82013-11-15 14:55:02 +0800927}
928
colyli@suse.defd768632017-02-18 03:05:56 +0800929static void wait_read_barrier(struct r1conf *conf, sector_t sector_nr)
majianpeng79ef3a82013-11-15 14:55:02 +0800930{
colyli@suse.defd768632017-02-18 03:05:56 +0800931 int idx = sector_to_idx(sector_nr);
majianpeng79ef3a82013-11-15 14:55:02 +0800932
colyli@suse.de824e47d2017-02-18 03:05:57 +0800933 /*
934 * Very similar to _wait_barrier(). The difference is, for read
935 * I/O we don't need wait for sync I/O, but if the whole array
936 * is frozen, the read I/O still has to wait until the array is
937 * unfrozen. Since there is no ordering requirement with
938 * conf->barrier[idx] here, memory barrier is unnecessary as well.
939 */
940 atomic_inc(&conf->nr_pending[idx]);
majianpeng79ef3a82013-11-15 14:55:02 +0800941
colyli@suse.de824e47d2017-02-18 03:05:57 +0800942 if (!READ_ONCE(conf->array_frozen))
943 return;
NeilBrown17999be2006-01-06 00:20:12 -0800944
945 spin_lock_irq(&conf->resync_lock);
colyli@suse.de824e47d2017-02-18 03:05:57 +0800946 atomic_inc(&conf->nr_waiting[idx]);
947 atomic_dec(&conf->nr_pending[idx]);
948 /*
949 * In case freeze_array() is waiting for
950 * get_unqueued_pending() == extra
951 */
952 wake_up(&conf->wait_barrier);
953 /* Wait for array to be unfrozen */
954 wait_event_lock_irq(conf->wait_barrier,
955 !conf->array_frozen,
956 conf->resync_lock);
957 atomic_inc(&conf->nr_pending[idx]);
958 atomic_dec(&conf->nr_waiting[idx]);
NeilBrown17999be2006-01-06 00:20:12 -0800959 spin_unlock_irq(&conf->resync_lock);
960}
961
NeilBrown37011e32017-03-15 14:05:14 +1100962static void inc_pending(struct r1conf *conf, sector_t bi_sector)
963{
964 /* The current request requires multiple r1_bio, so
965 * we need to increment the pending count, and the corresponding
966 * window count.
967 */
968 int idx = sector_to_idx(bi_sector);
969 atomic_inc(&conf->nr_pending[idx]);
970}
971
colyli@suse.defd768632017-02-18 03:05:56 +0800972static void wait_barrier(struct r1conf *conf, sector_t sector_nr)
NeilBrown17999be2006-01-06 00:20:12 -0800973{
colyli@suse.defd768632017-02-18 03:05:56 +0800974 int idx = sector_to_idx(sector_nr);
majianpeng79ef3a82013-11-15 14:55:02 +0800975
colyli@suse.defd768632017-02-18 03:05:56 +0800976 _wait_barrier(conf, idx);
977}
majianpeng79ef3a82013-11-15 14:55:02 +0800978
colyli@suse.defd768632017-02-18 03:05:56 +0800979static void wait_all_barriers(struct r1conf *conf)
980{
981 int idx;
982
983 for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++)
984 _wait_barrier(conf, idx);
985}
986
987static void _allow_barrier(struct r1conf *conf, int idx)
NeilBrown17999be2006-01-06 00:20:12 -0800988{
colyli@suse.de824e47d2017-02-18 03:05:57 +0800989 atomic_dec(&conf->nr_pending[idx]);
NeilBrown17999be2006-01-06 00:20:12 -0800990 wake_up(&conf->wait_barrier);
991}
992
colyli@suse.defd768632017-02-18 03:05:56 +0800993static void allow_barrier(struct r1conf *conf, sector_t sector_nr)
994{
995 int idx = sector_to_idx(sector_nr);
996
997 _allow_barrier(conf, idx);
998}
999
1000static void allow_all_barriers(struct r1conf *conf)
1001{
1002 int idx;
1003
1004 for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++)
1005 _allow_barrier(conf, idx);
1006}
1007
1008/* conf->resync_lock should be held */
1009static int get_unqueued_pending(struct r1conf *conf)
1010{
1011 int idx, ret;
1012
1013 for (ret = 0, idx = 0; idx < BARRIER_BUCKETS_NR; idx++)
colyli@suse.de824e47d2017-02-18 03:05:57 +08001014 ret += atomic_read(&conf->nr_pending[idx]) -
1015 atomic_read(&conf->nr_queued[idx]);
colyli@suse.defd768632017-02-18 03:05:56 +08001016
1017 return ret;
1018}
1019
NeilBrowne2d59922013-06-12 11:01:22 +10001020static void freeze_array(struct r1conf *conf, int extra)
NeilBrownddaf22a2006-01-06 00:20:19 -08001021{
colyli@suse.defd768632017-02-18 03:05:56 +08001022 /* Stop sync I/O and normal I/O and wait for everything to
Zhilong Liu11353b92017-03-14 15:52:26 +08001023 * go quiet.
colyli@suse.defd768632017-02-18 03:05:56 +08001024 * This is called in two situations:
1025 * 1) management command handlers (reshape, remove disk, quiesce).
1026 * 2) one normal I/O request failed.
1027
1028 * After array_frozen is set to 1, new sync IO will be blocked at
1029 * raise_barrier(), and new normal I/O will blocked at _wait_barrier()
1030 * or wait_read_barrier(). The flying I/Os will either complete or be
1031 * queued. When everything goes quite, there are only queued I/Os left.
1032
1033 * Every flying I/O contributes to a conf->nr_pending[idx], idx is the
1034 * barrier bucket index which this I/O request hits. When all sync and
1035 * normal I/O are queued, sum of all conf->nr_pending[] will match sum
1036 * of all conf->nr_queued[]. But normal I/O failure is an exception,
1037 * in handle_read_error(), we may call freeze_array() before trying to
1038 * fix the read error. In this case, the error read I/O is not queued,
1039 * so get_unqueued_pending() == 1.
1040 *
1041 * Therefore before this function returns, we need to wait until
1042 * get_unqueued_pendings(conf) gets equal to extra. For
1043 * normal I/O context, extra is 1, in rested situations extra is 0.
NeilBrownddaf22a2006-01-06 00:20:19 -08001044 */
1045 spin_lock_irq(&conf->resync_lock);
majianpengb364e3d2013-11-14 15:16:18 +11001046 conf->array_frozen = 1;
NeilBrown578b54a2016-11-14 16:30:21 +11001047 raid1_log(conf->mddev, "wait freeze");
colyli@suse.defd768632017-02-18 03:05:56 +08001048 wait_event_lock_irq_cmd(
1049 conf->wait_barrier,
1050 get_unqueued_pending(conf) == extra,
1051 conf->resync_lock,
1052 flush_pending_writes(conf));
NeilBrownddaf22a2006-01-06 00:20:19 -08001053 spin_unlock_irq(&conf->resync_lock);
1054}
NeilBrowne8096362011-10-11 16:49:05 +11001055static void unfreeze_array(struct r1conf *conf)
NeilBrownddaf22a2006-01-06 00:20:19 -08001056{
1057 /* reverse the effect of the freeze */
1058 spin_lock_irq(&conf->resync_lock);
majianpengb364e3d2013-11-14 15:16:18 +11001059 conf->array_frozen = 0;
NeilBrownddaf22a2006-01-06 00:20:19 -08001060 spin_unlock_irq(&conf->resync_lock);
colyli@suse.de824e47d2017-02-18 03:05:57 +08001061 wake_up(&conf->wait_barrier);
NeilBrownddaf22a2006-01-06 00:20:19 -08001062}
1063
NeilBrownf72ffdd2014-09-30 14:23:59 +10001064/* duplicate the data pages for behind I/O
NeilBrown4e780642010-10-19 12:54:01 +11001065 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001066static void alloc_behind_pages(struct bio *bio, struct r1bio *r1_bio)
NeilBrown4b6d2872005-09-09 16:23:47 -07001067{
1068 int i;
1069 struct bio_vec *bvec;
NeilBrown2ca68f52011-07-28 11:32:10 +10001070 struct bio_vec *bvecs = kzalloc(bio->bi_vcnt * sizeof(struct bio_vec),
NeilBrown4b6d2872005-09-09 16:23:47 -07001071 GFP_NOIO);
NeilBrown2ca68f52011-07-28 11:32:10 +10001072 if (unlikely(!bvecs))
NeilBrownaf6d7b72011-05-11 14:51:19 +10001073 return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001074
Kent Overstreetcb34e052012-09-05 15:22:02 -07001075 bio_for_each_segment_all(bvec, bio, i) {
NeilBrown2ca68f52011-07-28 11:32:10 +10001076 bvecs[i] = *bvec;
1077 bvecs[i].bv_page = alloc_page(GFP_NOIO);
1078 if (unlikely(!bvecs[i].bv_page))
NeilBrown4b6d2872005-09-09 16:23:47 -07001079 goto do_sync_io;
NeilBrown2ca68f52011-07-28 11:32:10 +10001080 memcpy(kmap(bvecs[i].bv_page) + bvec->bv_offset,
1081 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
1082 kunmap(bvecs[i].bv_page);
NeilBrown4b6d2872005-09-09 16:23:47 -07001083 kunmap(bvec->bv_page);
1084 }
NeilBrown2ca68f52011-07-28 11:32:10 +10001085 r1_bio->behind_bvecs = bvecs;
NeilBrownaf6d7b72011-05-11 14:51:19 +10001086 r1_bio->behind_page_count = bio->bi_vcnt;
1087 set_bit(R1BIO_BehindIO, &r1_bio->state);
1088 return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001089
1090do_sync_io:
NeilBrownaf6d7b72011-05-11 14:51:19 +10001091 for (i = 0; i < bio->bi_vcnt; i++)
NeilBrown2ca68f52011-07-28 11:32:10 +10001092 if (bvecs[i].bv_page)
1093 put_page(bvecs[i].bv_page);
1094 kfree(bvecs);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001095 pr_debug("%dB behind alloc failed, doing sync I/O\n",
1096 bio->bi_iter.bi_size);
NeilBrown4b6d2872005-09-09 16:23:47 -07001097}
1098
NeilBrownf54a9d02012-08-02 08:33:20 +10001099struct raid1_plug_cb {
1100 struct blk_plug_cb cb;
1101 struct bio_list pending;
1102 int pending_cnt;
1103};
1104
1105static void raid1_unplug(struct blk_plug_cb *cb, bool from_schedule)
1106{
1107 struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb,
1108 cb);
1109 struct mddev *mddev = plug->cb.data;
1110 struct r1conf *conf = mddev->private;
1111 struct bio *bio;
1112
NeilBrown874807a2012-11-27 12:14:40 +11001113 if (from_schedule || current->bio_list) {
NeilBrownf54a9d02012-08-02 08:33:20 +10001114 spin_lock_irq(&conf->device_lock);
1115 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1116 conf->pending_count += plug->pending_cnt;
1117 spin_unlock_irq(&conf->device_lock);
NeilBrownee0b0242013-02-25 12:38:29 +11001118 wake_up(&conf->wait_barrier);
NeilBrownf54a9d02012-08-02 08:33:20 +10001119 md_wakeup_thread(mddev->thread);
1120 kfree(plug);
1121 return;
1122 }
1123
1124 /* we aren't scheduling, so we can do the write-out directly. */
1125 bio = bio_list_get(&plug->pending);
1126 bitmap_unplug(mddev->bitmap);
1127 wake_up(&conf->wait_barrier);
1128
1129 while (bio) { /* submit pending writes */
1130 struct bio *next = bio->bi_next;
NeilBrown5e2c7a32016-11-04 16:46:03 +11001131 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrownf54a9d02012-08-02 08:33:20 +10001132 bio->bi_next = NULL;
NeilBrown5e2c7a32016-11-04 16:46:03 +11001133 bio->bi_bdev = rdev->bdev;
1134 if (test_bit(Faulty, &rdev->flags)) {
1135 bio->bi_error = -EIO;
1136 bio_endio(bio);
1137 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1138 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li32f9f572013-04-28 18:26:38 +08001139 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001140 bio_endio(bio);
Shaohua Li32f9f572013-04-28 18:26:38 +08001141 else
1142 generic_make_request(bio);
NeilBrownf54a9d02012-08-02 08:33:20 +10001143 bio = next;
1144 }
1145 kfree(plug);
1146}
1147
colyli@suse.defd768632017-02-18 03:05:56 +08001148static inline struct r1bio *
1149alloc_r1bio(struct mddev *mddev, struct bio *bio, sector_t sectors_handled)
1150{
1151 struct r1conf *conf = mddev->private;
1152 struct r1bio *r1_bio;
1153
1154 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1155
1156 r1_bio->master_bio = bio;
1157 r1_bio->sectors = bio_sectors(bio) - sectors_handled;
1158 r1_bio->state = 0;
1159 r1_bio->mddev = mddev;
1160 r1_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
1161
1162 return r1_bio;
1163}
1164
1165static void raid1_read_request(struct mddev *mddev, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
NeilBrowne8096362011-10-11 16:49:05 +11001167 struct r1conf *conf = mddev->private;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001168 struct raid1_info *mirror;
colyli@suse.defd768632017-02-18 03:05:56 +08001169 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 struct bio *read_bio;
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001171 struct bitmap *bitmap = mddev->bitmap;
1172 const int op = bio_op(bio);
1173 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1174 int sectors_handled;
1175 int max_sectors;
1176 int rdisk;
1177
colyli@suse.defd768632017-02-18 03:05:56 +08001178 /*
1179 * Still need barrier for READ in case that whole
1180 * array is frozen.
1181 */
1182 wait_read_barrier(conf, bio->bi_iter.bi_sector);
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001183
colyli@suse.defd768632017-02-18 03:05:56 +08001184 r1_bio = alloc_r1bio(mddev, bio, 0);
1185
1186 /*
colyli@suse.defd768632017-02-18 03:05:56 +08001187 * make_request() can abort the operation when read-ahead is being
1188 * used and no empty request is available.
1189 */
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001190read_again:
1191 rdisk = read_balance(conf, r1_bio, &max_sectors);
1192
1193 if (rdisk < 0) {
1194 /* couldn't find anywhere to read from */
1195 raid_end_bio_io(r1_bio);
1196 return;
1197 }
1198 mirror = conf->mirrors + rdisk;
1199
1200 if (test_bit(WriteMostly, &mirror->rdev->flags) &&
1201 bitmap) {
1202 /*
1203 * Reading from a write-mostly device must take care not to
1204 * over-take any writes that are 'behind'
1205 */
1206 raid1_log(mddev, "wait behind writes");
1207 wait_event(bitmap->behind_wait,
1208 atomic_read(&bitmap->behind_writes) == 0);
1209 }
1210 r1_bio->read_disk = rdisk;
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001211
Ming Leid7a10302017-02-14 23:29:03 +08001212 read_bio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001213 bio_trim(read_bio, r1_bio->sector - bio->bi_iter.bi_sector,
1214 max_sectors);
1215
1216 r1_bio->bios[rdisk] = read_bio;
1217
1218 read_bio->bi_iter.bi_sector = r1_bio->sector +
1219 mirror->rdev->data_offset;
1220 read_bio->bi_bdev = mirror->rdev->bdev;
1221 read_bio->bi_end_io = raid1_end_read_request;
1222 bio_set_op_attrs(read_bio, op, do_sync);
1223 if (test_bit(FailFast, &mirror->rdev->flags) &&
1224 test_bit(R1BIO_FailFast, &r1_bio->state))
1225 read_bio->bi_opf |= MD_FAILFAST;
1226 read_bio->bi_private = r1_bio;
1227
1228 if (mddev->gendisk)
1229 trace_block_bio_remap(bdev_get_queue(read_bio->bi_bdev),
1230 read_bio, disk_devt(mddev->gendisk),
1231 r1_bio->sector);
1232
1233 if (max_sectors < r1_bio->sectors) {
1234 /*
1235 * could not read all from this device, so we will need another
1236 * r1_bio.
1237 */
1238 sectors_handled = (r1_bio->sector + max_sectors
1239 - bio->bi_iter.bi_sector);
1240 r1_bio->sectors = max_sectors;
NeilBrown37011e32017-03-15 14:05:14 +11001241 bio_inc_remaining(bio);
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001242
1243 /*
1244 * Cannot call generic_make_request directly as that will be
1245 * queued in __make_request and subsequent mempool_alloc might
1246 * block waiting for it. So hand bio over to raid1d.
1247 */
1248 reschedule_retry(r1_bio);
1249
colyli@suse.defd768632017-02-18 03:05:56 +08001250 r1_bio = alloc_r1bio(mddev, bio, sectors_handled);
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001251 goto read_again;
1252 } else
1253 generic_make_request(read_bio);
1254}
1255
colyli@suse.defd768632017-02-18 03:05:56 +08001256static void raid1_write_request(struct mddev *mddev, struct bio *bio)
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001257{
1258 struct r1conf *conf = mddev->private;
colyli@suse.defd768632017-02-18 03:05:56 +08001259 struct r1bio *r1_bio;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001260 int i, disks;
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001261 struct bitmap *bitmap = mddev->bitmap;
NeilBrown191ea9b2005-06-21 17:17:23 -07001262 unsigned long flags;
NeilBrown3cb03002011-10-11 16:45:26 +11001263 struct md_rdev *blocked_rdev;
NeilBrownf54a9d02012-08-02 08:33:20 +10001264 struct blk_plug_cb *cb;
1265 struct raid1_plug_cb *plug = NULL;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001266 int first_clone;
1267 int sectors_handled;
1268 int max_sectors;
NeilBrown191ea9b2005-06-21 17:17:23 -07001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /*
1271 * Register the new request and wait if the reconstruction
1272 * thread has put up a bar for new requests.
1273 * Continue immediately if no resync is active currently.
1274 */
NeilBrown62de6082006-05-01 12:15:47 -07001275
NeilBrown3d310eb2005-06-21 17:17:26 -07001276 md_write_start(mddev, bio); /* wait on superblock update early */
1277
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001278 if ((bio_end_sector(bio) > mddev->suspend_lo &&
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001279 bio->bi_iter.bi_sector < mddev->suspend_hi) ||
1280 (mddev_is_clustered(mddev) &&
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -05001281 md_cluster_ops->area_resyncing(mddev, WRITE,
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001282 bio->bi_iter.bi_sector, bio_end_sector(bio)))) {
1283
1284 /*
1285 * As the suspend_* range is controlled by userspace, we want
1286 * an interruptible wait.
NeilBrown6eef4b22009-12-14 12:49:51 +11001287 */
1288 DEFINE_WAIT(w);
1289 for (;;) {
1290 flush_signals(current);
1291 prepare_to_wait(&conf->wait_barrier,
1292 &w, TASK_INTERRUPTIBLE);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07001293 if (bio_end_sector(bio) <= mddev->suspend_lo ||
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001294 bio->bi_iter.bi_sector >= mddev->suspend_hi ||
1295 (mddev_is_clustered(mddev) &&
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -05001296 !md_cluster_ops->area_resyncing(mddev, WRITE,
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001297 bio->bi_iter.bi_sector,
1298 bio_end_sector(bio))))
NeilBrown6eef4b22009-12-14 12:49:51 +11001299 break;
1300 schedule();
1301 }
1302 finish_wait(&conf->wait_barrier, &w);
1303 }
colyli@suse.defd768632017-02-18 03:05:56 +08001304 wait_barrier(conf, bio->bi_iter.bi_sector);
1305
1306 r1_bio = alloc_r1bio(mddev, bio, 0);
1307
NeilBrown34db0cd2011-10-11 16:50:01 +11001308 if (conf->pending_count >= max_queued_requests) {
1309 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001310 raid1_log(mddev, "wait queued");
NeilBrown34db0cd2011-10-11 16:50:01 +11001311 wait_event(conf->wait_barrier,
1312 conf->pending_count < max_queued_requests);
1313 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001314 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 * inc refcount on their rdev. Record them by setting
1316 * bios[x] to bio
NeilBrown1f68f0c2011-07-28 11:31:48 +10001317 * If there are known/acknowledged bad blocks on any device on
1318 * which we have seen a write error, we want to avoid writing those
1319 * blocks.
1320 * This potentially requires several writes to write around
1321 * the bad blocks. Each set of writes gets it's own r1bio
1322 * with a set of bios attached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001324
NeilBrown8f19ccb2011-12-23 10:17:56 +11001325 disks = conf->raid_disks * 2;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001326 retry_write:
1327 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 rcu_read_lock();
NeilBrown1f68f0c2011-07-28 11:31:48 +10001329 max_sectors = r1_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 for (i = 0; i < disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001331 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001332 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1333 atomic_inc(&rdev->nr_pending);
1334 blocked_rdev = rdev;
1335 break;
1336 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001337 r1_bio->bios[i] = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001338 if (!rdev || test_bit(Faulty, &rdev->flags)) {
NeilBrown8f19ccb2011-12-23 10:17:56 +11001339 if (i < conf->raid_disks)
1340 set_bit(R1BIO_Degraded, &r1_bio->state);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001341 continue;
1342 }
1343
1344 atomic_inc(&rdev->nr_pending);
1345 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1346 sector_t first_bad;
1347 int bad_sectors;
1348 int is_bad;
1349
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001350 is_bad = is_badblock(rdev, r1_bio->sector, max_sectors,
NeilBrown1f68f0c2011-07-28 11:31:48 +10001351 &first_bad, &bad_sectors);
1352 if (is_bad < 0) {
1353 /* mustn't write here until the bad block is
1354 * acknowledged*/
1355 set_bit(BlockedBadBlocks, &rdev->flags);
1356 blocked_rdev = rdev;
1357 break;
NeilBrown964147d2010-05-18 15:27:13 +10001358 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001359 if (is_bad && first_bad <= r1_bio->sector) {
1360 /* Cannot write here at all */
1361 bad_sectors -= (r1_bio->sector - first_bad);
1362 if (bad_sectors < max_sectors)
1363 /* mustn't write more than bad_sectors
1364 * to other devices yet
1365 */
1366 max_sectors = bad_sectors;
1367 rdev_dec_pending(rdev, mddev);
1368 /* We don't set R1BIO_Degraded as that
1369 * only applies if the disk is
1370 * missing, so it might be re-added,
1371 * and we want to know to recover this
1372 * chunk.
1373 * In this case the device is here,
1374 * and the fact that this chunk is not
1375 * in-sync is recorded in the bad
1376 * block log
1377 */
1378 continue;
1379 }
1380 if (is_bad) {
1381 int good_sectors = first_bad - r1_bio->sector;
1382 if (good_sectors < max_sectors)
1383 max_sectors = good_sectors;
1384 }
1385 }
1386 r1_bio->bios[i] = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388 rcu_read_unlock();
1389
Dan Williams6bfe0b42008-04-30 00:52:32 -07001390 if (unlikely(blocked_rdev)) {
1391 /* Wait for this device to become unblocked */
1392 int j;
1393
1394 for (j = 0; j < i; j++)
1395 if (r1_bio->bios[j])
1396 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001397 r1_bio->state = 0;
colyli@suse.defd768632017-02-18 03:05:56 +08001398 allow_barrier(conf, bio->bi_iter.bi_sector);
NeilBrown578b54a2016-11-14 16:30:21 +11001399 raid1_log(mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001400 md_wait_for_blocked_rdev(blocked_rdev, mddev);
colyli@suse.defd768632017-02-18 03:05:56 +08001401 wait_barrier(conf, bio->bi_iter.bi_sector);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001402 goto retry_write;
1403 }
1404
NeilBrown6b6c8112017-03-15 14:05:13 +11001405 if (max_sectors < r1_bio->sectors)
NeilBrown1f68f0c2011-07-28 11:31:48 +10001406 r1_bio->sectors = max_sectors;
NeilBrown6b6c8112017-03-15 14:05:13 +11001407
Kent Overstreet4f024f32013-10-11 15:44:27 -07001408 sectors_handled = r1_bio->sector + max_sectors - bio->bi_iter.bi_sector;
NeilBrown4b6d2872005-09-09 16:23:47 -07001409
NeilBrown4e780642010-10-19 12:54:01 +11001410 atomic_set(&r1_bio->remaining, 1);
NeilBrown4b6d2872005-09-09 16:23:47 -07001411 atomic_set(&r1_bio->behind_remaining, 0);
NeilBrown191ea9b2005-06-21 17:17:23 -07001412
NeilBrown1f68f0c2011-07-28 11:31:48 +10001413 first_clone = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 for (i = 0; i < disks; i++) {
Ming Lei8e58e322017-02-14 23:29:01 +08001415 struct bio *mbio = NULL;
1416 sector_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (!r1_bio->bios[i])
1418 continue;
1419
Ming Lei8e58e322017-02-14 23:29:01 +08001420 offset = r1_bio->sector - bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
NeilBrown1f68f0c2011-07-28 11:31:48 +10001422 if (first_clone) {
1423 /* do behind I/O ?
1424 * Not if there are too many, or cannot
1425 * allocate memory, or a reader on WriteMostly
1426 * is waiting for behind writes to flush */
1427 if (bitmap &&
1428 (atomic_read(&bitmap->behind_writes)
1429 < mddev->bitmap_info.max_write_behind) &&
Ming Lei8e58e322017-02-14 23:29:01 +08001430 !waitqueue_active(&bitmap->behind_wait)) {
1431 mbio = bio_clone_bioset_partial(bio, GFP_NOIO,
1432 mddev->bio_set,
Shaohua Li1ec49222017-02-21 14:27:57 -08001433 offset << 9,
1434 max_sectors << 9);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001435 alloc_behind_pages(mbio, r1_bio);
Ming Lei8e58e322017-02-14 23:29:01 +08001436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
NeilBrown1f68f0c2011-07-28 11:31:48 +10001438 bitmap_startwrite(bitmap, r1_bio->sector,
1439 r1_bio->sectors,
1440 test_bit(R1BIO_BehindIO,
1441 &r1_bio->state));
1442 first_clone = 0;
1443 }
Ming Lei8e58e322017-02-14 23:29:01 +08001444
1445 if (!mbio) {
Shaohua Li1ec49222017-02-21 14:27:57 -08001446 if (r1_bio->behind_bvecs)
1447 mbio = bio_clone_bioset_partial(bio, GFP_NOIO,
1448 mddev->bio_set,
1449 offset << 9,
1450 max_sectors << 9);
1451 else {
1452 mbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
1453 bio_trim(mbio, offset, max_sectors);
1454 }
Ming Lei8e58e322017-02-14 23:29:01 +08001455 }
1456
NeilBrown2ca68f52011-07-28 11:32:10 +10001457 if (r1_bio->behind_bvecs) {
NeilBrown4b6d2872005-09-09 16:23:47 -07001458 struct bio_vec *bvec;
1459 int j;
1460
Kent Overstreetcb34e052012-09-05 15:22:02 -07001461 /*
1462 * We trimmed the bio, so _all is legit
NeilBrown4b6d2872005-09-09 16:23:47 -07001463 */
Kent Overstreetd74c6d52013-02-06 12:23:11 -08001464 bio_for_each_segment_all(bvec, mbio, j)
NeilBrown2ca68f52011-07-28 11:32:10 +10001465 bvec->bv_page = r1_bio->behind_bvecs[j].bv_page;
NeilBrown4b6d2872005-09-09 16:23:47 -07001466 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
1467 atomic_inc(&r1_bio->behind_remaining);
1468 }
1469
NeilBrown1f68f0c2011-07-28 11:31:48 +10001470 r1_bio->bios[i] = mbio;
1471
Kent Overstreet4f024f32013-10-11 15:44:27 -07001472 mbio->bi_iter.bi_sector = (r1_bio->sector +
NeilBrown1f68f0c2011-07-28 11:31:48 +10001473 conf->mirrors[i].rdev->data_offset);
NeilBrown109e3762016-11-18 13:22:04 +11001474 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001475 mbio->bi_end_io = raid1_end_write_request;
Linus Torvaldsa682e002017-02-24 14:42:19 -08001476 mbio->bi_opf = bio_op(bio) | (bio->bi_opf & (REQ_SYNC | REQ_FUA));
NeilBrown212e7eb2016-11-18 16:16:12 +11001477 if (test_bit(FailFast, &conf->mirrors[i].rdev->flags) &&
1478 !test_bit(WriteMostly, &conf->mirrors[i].rdev->flags) &&
1479 conf->raid_disks - mddev->degraded > 1)
1480 mbio->bi_opf |= MD_FAILFAST;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001481 mbio->bi_private = r1_bio;
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 atomic_inc(&r1_bio->remaining);
NeilBrownf54a9d02012-08-02 08:33:20 +10001484
NeilBrown109e3762016-11-18 13:22:04 +11001485 if (mddev->gendisk)
1486 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1487 mbio, disk_devt(mddev->gendisk),
1488 r1_bio->sector);
1489 /* flush_pending_writes() needs access to the rdev so...*/
1490 mbio->bi_bdev = (void*)conf->mirrors[i].rdev;
1491
NeilBrownf54a9d02012-08-02 08:33:20 +10001492 cb = blk_check_plugged(raid1_unplug, mddev, sizeof(*plug));
1493 if (cb)
1494 plug = container_of(cb, struct raid1_plug_cb, cb);
1495 else
1496 plug = NULL;
NeilBrown4e780642010-10-19 12:54:01 +11001497 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownf54a9d02012-08-02 08:33:20 +10001498 if (plug) {
1499 bio_list_add(&plug->pending, mbio);
1500 plug->pending_cnt++;
1501 } else {
1502 bio_list_add(&conf->pending_bio_list, mbio);
1503 conf->pending_count++;
1504 }
NeilBrown4e780642010-10-19 12:54:01 +11001505 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrownf54a9d02012-08-02 08:33:20 +10001506 if (!plug)
NeilBrownb357f042012-07-03 17:45:31 +10001507 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
NeilBrown079fa162011-09-10 17:21:23 +10001509 /* Mustn't call r1_bio_write_done before this next test,
1510 * as it could result in the bio being freed.
1511 */
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001512 if (sectors_handled < bio_sectors(bio)) {
NeilBrown37011e32017-03-15 14:05:14 +11001513 /* We need another r1_bio, which must be counted */
1514 sector_t sect = bio->bi_iter.bi_sector + sectors_handled;
NeilBrown6b6c8112017-03-15 14:05:13 +11001515
NeilBrown37011e32017-03-15 14:05:14 +11001516 inc_pending(conf, sect);
1517 bio_inc_remaining(bio);
NeilBrown6b6c8112017-03-15 14:05:13 +11001518 r1_bio_write_done(r1_bio);
colyli@suse.defd768632017-02-18 03:05:56 +08001519 r1_bio = alloc_r1bio(mddev, bio, sectors_handled);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001520 goto retry_write;
1521 }
1522
NeilBrown079fa162011-09-10 17:21:23 +10001523 r1_bio_write_done(r1_bio);
1524
1525 /* In case raid1d snuck in to freeze_array */
1526 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001529static void raid1_make_request(struct mddev *mddev, struct bio *bio)
1530{
colyli@suse.defd768632017-02-18 03:05:56 +08001531 struct bio *split;
1532 sector_t sectors;
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001533
Shaohua Liaff8da02017-02-21 10:56:19 -08001534 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1535 md_flush_request(mddev, bio);
1536 return;
1537 }
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001538
colyli@suse.defd768632017-02-18 03:05:56 +08001539 /* if bio exceeds barrier unit boundary, split it */
1540 do {
1541 sectors = align_to_barrier_unit_end(
1542 bio->bi_iter.bi_sector, bio_sectors(bio));
1543 if (sectors < bio_sectors(bio)) {
1544 split = bio_split(bio, sectors, GFP_NOIO, fs_bio_set);
1545 bio_chain(split, bio);
1546 } else {
1547 split = bio;
1548 }
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001549
Shaohua Li61eb2b42017-02-28 13:00:20 -08001550 if (bio_data_dir(split) == READ) {
colyli@suse.defd768632017-02-18 03:05:56 +08001551 raid1_read_request(mddev, split);
Shaohua Li61eb2b42017-02-28 13:00:20 -08001552
1553 /*
1554 * If a bio is splitted, the first part of bio will
1555 * pass barrier but the bio is queued in
1556 * current->bio_list (see generic_make_request). If
1557 * there is a raise_barrier() called here, the second
1558 * part of bio can't pass barrier. But since the first
1559 * part bio isn't dispatched to underlaying disks yet,
1560 * the barrier is never released, hence raise_barrier
1561 * will alays wait. We have a deadlock.
1562 * Note, this only happens in read path. For write
1563 * path, the first part of bio is dispatched in a
1564 * schedule() call (because of blk plug) or offloaded
1565 * to raid10d.
1566 * Quitting from the function immediately can change
1567 * the bio order queued in bio_list and avoid the deadlock.
1568 */
1569 if (split != bio) {
1570 generic_make_request(bio);
1571 break;
1572 }
1573 } else
colyli@suse.defd768632017-02-18 03:05:56 +08001574 raid1_write_request(mddev, split);
1575 } while (split != bio);
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001576}
1577
Shaohua Li849674e2016-01-20 13:52:20 -08001578static void raid1_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
NeilBrowne8096362011-10-11 16:49:05 +11001580 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 int i;
1582
1583 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown11ce99e2006-10-03 01:15:52 -07001584 conf->raid_disks - mddev->degraded);
NeilBrownddac7c72006-08-31 21:27:36 -07001585 rcu_read_lock();
1586 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001587 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 seq_printf(seq, "%s",
NeilBrownddac7c72006-08-31 21:27:36 -07001589 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1590 }
1591 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 seq_printf(seq, "]");
1593}
1594
Shaohua Li849674e2016-01-20 13:52:20 -08001595static void raid1_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
1597 char b[BDEVNAME_SIZE];
NeilBrowne8096362011-10-11 16:49:05 +11001598 struct r1conf *conf = mddev->private;
NeilBrown423f04d2015-07-27 11:48:52 +10001599 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 /*
1602 * If it is not operational, then we have already marked it as dead
1603 * else if it is the last working disks, ignore the error, let the
1604 * next level up know.
1605 * else mark the drive as failed
1606 */
NeilBrown2e52d442016-11-18 16:16:12 +11001607 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001608 if (test_bit(In_sync, &rdev->flags)
NeilBrown4044ba52009-01-09 08:31:11 +11001609 && (conf->raid_disks - mddev->degraded) == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /*
1611 * Don't fail the drive, act as though we were just a
NeilBrown4044ba52009-01-09 08:31:11 +11001612 * normal single drive.
1613 * However don't try a recovery from this drive as
1614 * it is very likely to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 */
NeilBrown53890422011-07-27 11:00:36 +10001616 conf->recovery_disabled = mddev->recovery_disabled;
NeilBrown2e52d442016-11-18 16:16:12 +11001617 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return;
NeilBrown4044ba52009-01-09 08:31:11 +11001619 }
NeilBrownde393cd2011-07-28 11:31:48 +10001620 set_bit(Blocked, &rdev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001621 if (test_and_clear_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 mddev->degraded++;
NeilBrowndd00a992007-05-10 03:15:50 -07001623 set_bit(Faulty, &rdev->flags);
NeilBrowndd00a992007-05-10 03:15:50 -07001624 } else
1625 set_bit(Faulty, &rdev->flags);
NeilBrown423f04d2015-07-27 11:48:52 +10001626 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown2446dba2014-07-31 10:16:29 +10001627 /*
1628 * if recovery is running, make sure it aborts.
1629 */
1630 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08001631 set_mask_bits(&mddev->sb_flags, 0,
1632 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown1d41c212016-11-02 14:16:50 +11001633 pr_crit("md/raid1:%s: Disk failure on %s, disabling device.\n"
1634 "md/raid1:%s: Operation continuing on %d devices.\n",
1635 mdname(mddev), bdevname(rdev->bdev, b),
1636 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
NeilBrowne8096362011-10-11 16:49:05 +11001639static void print_conf(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
NeilBrown1d41c212016-11-02 14:16:50 +11001643 pr_debug("RAID1 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (!conf) {
NeilBrown1d41c212016-11-02 14:16:50 +11001645 pr_debug("(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return;
1647 }
NeilBrown1d41c212016-11-02 14:16:50 +11001648 pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1649 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
NeilBrownddac7c72006-08-31 21:27:36 -07001651 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 for (i = 0; i < conf->raid_disks; i++) {
1653 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11001654 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownddac7c72006-08-31 21:27:36 -07001655 if (rdev)
NeilBrown1d41c212016-11-02 14:16:50 +11001656 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1657 i, !test_bit(In_sync, &rdev->flags),
1658 !test_bit(Faulty, &rdev->flags),
1659 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
NeilBrownddac7c72006-08-31 21:27:36 -07001661 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
1663
NeilBrowne8096362011-10-11 16:49:05 +11001664static void close_sync(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
colyli@suse.defd768632017-02-18 03:05:56 +08001666 wait_all_barriers(conf);
1667 allow_all_barriers(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 mempool_destroy(conf->r1buf_pool);
1670 conf->r1buf_pool = NULL;
1671}
1672
NeilBrownfd01b882011-10-11 16:47:53 +11001673static int raid1_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
1675 int i;
NeilBrowne8096362011-10-11 16:49:05 +11001676 struct r1conf *conf = mddev->private;
NeilBrown6b965622010-08-18 11:56:59 +10001677 int count = 0;
1678 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 /*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001681 * Find all failed disks within the RAID1 configuration
NeilBrownddac7c72006-08-31 21:27:36 -07001682 * and mark them readable.
1683 * Called under mddev lock, so rcu protection not needed.
NeilBrown423f04d2015-07-27 11:48:52 +10001684 * device_lock used to avoid races with raid1_end_read_request
1685 * which expects 'In_sync' flags and ->degraded to be consistent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 */
NeilBrown423f04d2015-07-27 11:48:52 +10001687 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001689 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrown8c7a2c22011-12-23 10:17:57 +11001690 struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev;
1691 if (repl
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001692 && !test_bit(Candidate, &repl->flags)
NeilBrown8c7a2c22011-12-23 10:17:57 +11001693 && repl->recovery_offset == MaxSector
1694 && !test_bit(Faulty, &repl->flags)
1695 && !test_and_set_bit(In_sync, &repl->flags)) {
1696 /* replacement has just become active */
1697 if (!rdev ||
1698 !test_and_clear_bit(In_sync, &rdev->flags))
1699 count++;
1700 if (rdev) {
1701 /* Replaced device not technically
1702 * faulty, but we need to be sure
1703 * it gets removed and never re-added
1704 */
1705 set_bit(Faulty, &rdev->flags);
1706 sysfs_notify_dirent_safe(
1707 rdev->sysfs_state);
1708 }
1709 }
NeilBrownddac7c72006-08-31 21:27:36 -07001710 if (rdev
Lukasz Dorau61e49472013-10-24 12:55:17 +11001711 && rdev->recovery_offset == MaxSector
NeilBrownddac7c72006-08-31 21:27:36 -07001712 && !test_bit(Faulty, &rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001713 && !test_and_set_bit(In_sync, &rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001714 count++;
Jonathan Brassow654e8b52011-07-27 11:00:36 +10001715 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717 }
NeilBrown6b965622010-08-18 11:56:59 +10001718 mddev->degraded -= count;
1719 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001722 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
NeilBrownfd01b882011-10-11 16:47:53 +11001725static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
NeilBrowne8096362011-10-11 16:49:05 +11001727 struct r1conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001728 int err = -EEXIST;
NeilBrown41158c72005-06-21 17:17:25 -07001729 int mirror = 0;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001730 struct raid1_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10001731 int first = 0;
NeilBrown30194632011-12-23 10:17:56 +11001732 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
NeilBrown53890422011-07-27 11:00:36 +10001734 if (mddev->recovery_disabled == conf->recovery_disabled)
1735 return -EBUSY;
1736
Dan Williams1501efa2016-01-13 16:00:07 -08001737 if (md_integrity_add_rdev(rdev, mddev))
1738 return -ENXIO;
1739
Neil Brown6c2fce22008-06-28 08:31:31 +10001740 if (rdev->raid_disk >= 0)
1741 first = last = rdev->raid_disk;
1742
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05001743 /*
1744 * find the disk ... but prefer rdev->saved_raid_disk
1745 * if possible.
1746 */
1747 if (rdev->saved_raid_disk >= 0 &&
1748 rdev->saved_raid_disk >= first &&
1749 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1750 first = last = rdev->saved_raid_disk;
1751
NeilBrown7ef449d2011-12-23 10:17:57 +11001752 for (mirror = first; mirror <= last; mirror++) {
1753 p = conf->mirrors+mirror;
1754 if (!p->rdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Jonathan Brassow9092c022013-05-02 14:19:24 -05001756 if (mddev->gendisk)
1757 disk_stack_limits(mddev->gendisk, rdev->bdev,
1758 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 p->head_position = 0;
1761 rdev->raid_disk = mirror;
Neil Brown199050e2008-06-28 08:31:33 +10001762 err = 0;
NeilBrown6aea114a2005-11-28 13:44:13 -08001763 /* As all devices are equivalent, we don't need a full recovery
1764 * if this was recently any drive of the array
1765 */
1766 if (rdev->saved_raid_disk < 0)
NeilBrown41158c72005-06-21 17:17:25 -07001767 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001768 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 break;
1770 }
NeilBrown7ef449d2011-12-23 10:17:57 +11001771 if (test_bit(WantReplacement, &p->rdev->flags) &&
1772 p[conf->raid_disks].rdev == NULL) {
1773 /* Add this device as a replacement */
1774 clear_bit(In_sync, &rdev->flags);
1775 set_bit(Replacement, &rdev->flags);
1776 rdev->raid_disk = mirror;
1777 err = 0;
1778 conf->fullsync = 1;
1779 rcu_assign_pointer(p[conf->raid_disks].rdev, rdev);
1780 break;
1781 }
1782 }
Jonathan Brassow9092c022013-05-02 14:19:24 -05001783 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
Shaohua Li2ff8cc22012-10-11 13:28:54 +11001784 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001786 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
NeilBrownb8321b62011-12-23 10:17:51 +11001789static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
NeilBrowne8096362011-10-11 16:49:05 +11001791 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001793 int number = rdev->raid_disk;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001794 struct raid1_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
NeilBrownb014f142011-12-23 10:17:56 +11001796 if (rdev != p->rdev)
1797 p = conf->mirrors + conf->raid_disks + number;
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 print_conf(conf);
NeilBrownb8321b62011-12-23 10:17:51 +11001800 if (rdev == p->rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001801 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 atomic_read(&rdev->nr_pending)) {
1803 err = -EBUSY;
1804 goto abort;
1805 }
NeilBrown046abee2010-10-26 15:46:20 +11001806 /* Only remove non-faulty devices if recovery
NeilBrowndfc70642008-05-23 13:04:39 -07001807 * is not possible.
1808 */
1809 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown53890422011-07-27 11:00:36 +10001810 mddev->recovery_disabled != conf->recovery_disabled &&
NeilBrowndfc70642008-05-23 13:04:39 -07001811 mddev->degraded < conf->raid_disks) {
1812 err = -EBUSY;
1813 goto abort;
1814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 p->rdev = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10001816 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1817 synchronize_rcu();
1818 if (atomic_read(&rdev->nr_pending)) {
1819 /* lost the race, try later */
1820 err = -EBUSY;
1821 p->rdev = rdev;
1822 goto abort;
1823 }
1824 }
1825 if (conf->mirrors[conf->raid_disks + number].rdev) {
NeilBrown8c7a2c22011-12-23 10:17:57 +11001826 /* We just removed a device that is being replaced.
1827 * Move down the replacement. We drain all IO before
1828 * doing this to avoid confusion.
1829 */
1830 struct md_rdev *repl =
1831 conf->mirrors[conf->raid_disks + number].rdev;
NeilBrowne2d59922013-06-12 11:01:22 +10001832 freeze_array(conf, 0);
NeilBrown8c7a2c22011-12-23 10:17:57 +11001833 clear_bit(Replacement, &repl->flags);
1834 p->rdev = repl;
1835 conf->mirrors[conf->raid_disks + number].rdev = NULL;
NeilBrowne2d59922013-06-12 11:01:22 +10001836 unfreeze_array(conf);
NeilBrownb014f142011-12-23 10:17:56 +11001837 clear_bit(WantReplacement, &rdev->flags);
NeilBrown8c7a2c22011-12-23 10:17:57 +11001838 } else
1839 clear_bit(WantReplacement, &rdev->flags);
Martin K. Petersena91a2782011-03-17 11:11:05 +01001840 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
1842abort:
1843
1844 print_conf(conf);
1845 return err;
1846}
1847
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001848static void end_sync_read(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001850 struct r1bio *r1_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
NeilBrown0fc280f2011-10-07 14:22:55 +11001852 update_head_pos(r1_bio->read_disk, r1_bio);
Namhyung Kimba3ae3b2011-10-07 14:22:53 +11001853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 /*
1855 * we have read a block, now it needs to be re-written,
1856 * or re-read if the read failed.
1857 * We don't do much here, just schedule handling by raid1d
1858 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001859 if (!bio->bi_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrownd11c1712006-01-06 00:20:26 -08001861
1862 if (atomic_dec_and_test(&r1_bio->remaining))
1863 reschedule_retry(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
1865
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001866static void end_sync_write(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001868 int uptodate = !bio->bi_error;
NeilBrown9f2c9d12011-10-11 16:48:43 +11001869 struct r1bio *r1_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001870 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11001871 struct r1conf *conf = mddev->private;
NeilBrown4367af52011-07-28 11:31:49 +10001872 sector_t first_bad;
1873 int bad_sectors;
NeilBrown854abd72016-06-02 16:19:52 +10001874 struct md_rdev *rdev = conf->mirrors[find_bio_disk(r1_bio, bio)].rdev;
Namhyung Kimba3ae3b2011-10-07 14:22:53 +11001875
NeilBrown6b1117d2006-03-31 02:31:57 -08001876 if (!uptodate) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001877 sector_t sync_blocks = 0;
NeilBrown6b1117d2006-03-31 02:31:57 -08001878 sector_t s = r1_bio->sector;
1879 long sectors_to_go = r1_bio->sectors;
1880 /* make sure these bits doesn't get cleared. */
1881 do {
NeilBrown5e3db642006-07-10 04:44:18 -07001882 bitmap_end_sync(mddev->bitmap, s,
NeilBrown6b1117d2006-03-31 02:31:57 -08001883 &sync_blocks, 1);
1884 s += sync_blocks;
1885 sectors_to_go -= sync_blocks;
1886 } while (sectors_to_go > 0);
NeilBrown854abd72016-06-02 16:19:52 +10001887 set_bit(WriteErrorSeen, &rdev->flags);
1888 if (!test_and_set_bit(WantReplacement, &rdev->flags))
NeilBrown19d67162011-12-23 10:17:57 +11001889 set_bit(MD_RECOVERY_NEEDED, &
1890 mddev->recovery);
NeilBrownd8f05d22011-07-28 11:33:00 +10001891 set_bit(R1BIO_WriteError, &r1_bio->state);
NeilBrown854abd72016-06-02 16:19:52 +10001892 } else if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
NeilBrown3a9f28a2011-07-28 11:33:42 +10001893 &first_bad, &bad_sectors) &&
1894 !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
1895 r1_bio->sector,
1896 r1_bio->sectors,
1897 &first_bad, &bad_sectors)
1898 )
NeilBrown4367af52011-07-28 11:31:49 +10001899 set_bit(R1BIO_MadeGood, &r1_bio->state);
NeilBrowne3b97032005-08-04 12:53:34 -07001900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown4367af52011-07-28 11:31:49 +10001902 int s = r1_bio->sectors;
NeilBrownd8f05d22011-07-28 11:33:00 +10001903 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
1904 test_bit(R1BIO_WriteError, &r1_bio->state))
NeilBrown4367af52011-07-28 11:31:49 +10001905 reschedule_retry(r1_bio);
1906 else {
1907 put_buf(r1_bio);
1908 md_done_sync(mddev, s, uptodate);
1909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
NeilBrown3cb03002011-10-11 16:45:26 +11001913static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrownd8f05d22011-07-28 11:33:00 +10001914 int sectors, struct page *page, int rw)
1915{
Mike Christie796a5cf2016-06-05 14:32:07 -05001916 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
NeilBrownd8f05d22011-07-28 11:33:00 +10001917 /* success */
1918 return 1;
NeilBrown19d67162011-12-23 10:17:57 +11001919 if (rw == WRITE) {
NeilBrownd8f05d22011-07-28 11:33:00 +10001920 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrown19d67162011-12-23 10:17:57 +11001921 if (!test_and_set_bit(WantReplacement,
1922 &rdev->flags))
1923 set_bit(MD_RECOVERY_NEEDED, &
1924 rdev->mddev->recovery);
1925 }
NeilBrownd8f05d22011-07-28 11:33:00 +10001926 /* need to record an error - either for the block or the device */
1927 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1928 md_error(rdev->mddev, rdev);
1929 return 0;
1930}
1931
NeilBrown9f2c9d12011-10-11 16:48:43 +11001932static int fix_sync_read_error(struct r1bio *r1_bio)
NeilBrowna68e5872011-05-11 14:40:44 +10001933{
1934 /* Try some synchronous reads of other devices to get
1935 * good data, much like with normal read errors. Only
1936 * read into the pages we already have so we don't
1937 * need to re-issue the read request.
1938 * We don't need to freeze the array, because being in an
1939 * active sync request, there is no normal IO, and
1940 * no overlapping syncs.
NeilBrown06f60382011-07-28 11:31:48 +10001941 * We don't need to check is_badblock() again as we
1942 * made sure that anything with a bad block in range
1943 * will have bi_end_io clear.
NeilBrowna68e5872011-05-11 14:40:44 +10001944 */
NeilBrownfd01b882011-10-11 16:47:53 +11001945 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11001946 struct r1conf *conf = mddev->private;
NeilBrowna68e5872011-05-11 14:40:44 +10001947 struct bio *bio = r1_bio->bios[r1_bio->read_disk];
1948 sector_t sect = r1_bio->sector;
1949 int sectors = r1_bio->sectors;
1950 int idx = 0;
NeilBrown2e52d442016-11-18 16:16:12 +11001951 struct md_rdev *rdev;
1952
1953 rdev = conf->mirrors[r1_bio->read_disk].rdev;
1954 if (test_bit(FailFast, &rdev->flags)) {
1955 /* Don't try recovering from here - just fail it
1956 * ... unless it is the last working device of course */
1957 md_error(mddev, rdev);
1958 if (test_bit(Faulty, &rdev->flags))
1959 /* Don't try to read from here, but make sure
1960 * put_buf does it's thing
1961 */
1962 bio->bi_end_io = end_sync_write;
1963 }
NeilBrowna68e5872011-05-11 14:40:44 +10001964
1965 while(sectors) {
1966 int s = sectors;
1967 int d = r1_bio->read_disk;
1968 int success = 0;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001969 int start;
NeilBrowna68e5872011-05-11 14:40:44 +10001970
1971 if (s > (PAGE_SIZE>>9))
1972 s = PAGE_SIZE >> 9;
1973 do {
1974 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1975 /* No rcu protection needed here devices
1976 * can only be removed when no resync is
1977 * active, and resync is currently active
1978 */
1979 rdev = conf->mirrors[d].rdev;
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001980 if (sync_page_io(rdev, sect, s<<9,
NeilBrowna68e5872011-05-11 14:40:44 +10001981 bio->bi_io_vec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05001982 REQ_OP_READ, 0, false)) {
NeilBrowna68e5872011-05-11 14:40:44 +10001983 success = 1;
1984 break;
1985 }
1986 }
1987 d++;
NeilBrown8f19ccb2011-12-23 10:17:56 +11001988 if (d == conf->raid_disks * 2)
NeilBrowna68e5872011-05-11 14:40:44 +10001989 d = 0;
1990 } while (!success && d != r1_bio->read_disk);
1991
NeilBrown78d7f5f2011-05-11 14:48:56 +10001992 if (!success) {
NeilBrowna68e5872011-05-11 14:40:44 +10001993 char b[BDEVNAME_SIZE];
NeilBrown3a9f28a2011-07-28 11:33:42 +10001994 int abort = 0;
1995 /* Cannot read from anywhere, this block is lost.
1996 * Record a bad block on each device. If that doesn't
1997 * work just disable and interrupt the recovery.
1998 * Don't fail devices as that won't really help.
1999 */
NeilBrown1d41c212016-11-02 14:16:50 +11002000 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
2001 mdname(mddev),
2002 bdevname(bio->bi_bdev, b),
2003 (unsigned long long)r1_bio->sector);
NeilBrown8f19ccb2011-12-23 10:17:56 +11002004 for (d = 0; d < conf->raid_disks * 2; d++) {
NeilBrown3a9f28a2011-07-28 11:33:42 +10002005 rdev = conf->mirrors[d].rdev;
2006 if (!rdev || test_bit(Faulty, &rdev->flags))
2007 continue;
2008 if (!rdev_set_badblocks(rdev, sect, s, 0))
2009 abort = 1;
2010 }
2011 if (abort) {
NeilBrownd890fa22011-10-26 11:54:39 +11002012 conf->recovery_disabled =
2013 mddev->recovery_disabled;
NeilBrown3a9f28a2011-07-28 11:33:42 +10002014 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2015 md_done_sync(mddev, r1_bio->sectors, 0);
2016 put_buf(r1_bio);
2017 return 0;
2018 }
2019 /* Try next page */
2020 sectors -= s;
2021 sect += s;
2022 idx++;
2023 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10002024 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10002025
2026 start = d;
2027 /* write it back and re-read */
2028 while (d != r1_bio->read_disk) {
2029 if (d == 0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002030 d = conf->raid_disks * 2;
NeilBrown78d7f5f2011-05-11 14:48:56 +10002031 d--;
2032 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
2033 continue;
2034 rdev = conf->mirrors[d].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10002035 if (r1_sync_page_io(rdev, sect, s,
2036 bio->bi_io_vec[idx].bv_page,
2037 WRITE) == 0) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10002038 r1_bio->bios[d]->bi_end_io = NULL;
2039 rdev_dec_pending(rdev, mddev);
Namhyung Kim9d3d8012011-07-27 11:00:36 +10002040 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10002041 }
2042 d = start;
2043 while (d != r1_bio->read_disk) {
2044 if (d == 0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002045 d = conf->raid_disks * 2;
NeilBrown78d7f5f2011-05-11 14:48:56 +10002046 d--;
2047 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
2048 continue;
2049 rdev = conf->mirrors[d].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10002050 if (r1_sync_page_io(rdev, sect, s,
2051 bio->bi_io_vec[idx].bv_page,
2052 READ) != 0)
Namhyung Kim9d3d8012011-07-27 11:00:36 +10002053 atomic_add(s, &rdev->corrected_errors);
NeilBrown78d7f5f2011-05-11 14:48:56 +10002054 }
NeilBrowna68e5872011-05-11 14:40:44 +10002055 sectors -= s;
2056 sect += s;
2057 idx ++;
2058 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10002059 set_bit(R1BIO_Uptodate, &r1_bio->state);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002060 bio->bi_error = 0;
NeilBrowna68e5872011-05-11 14:40:44 +10002061 return 1;
2062}
2063
NeilBrownc95e6382014-09-09 13:54:11 +10002064static void process_checks(struct r1bio *r1_bio)
NeilBrowna68e5872011-05-11 14:40:44 +10002065{
2066 /* We have read all readable devices. If we haven't
2067 * got the block, then there is no hope left.
2068 * If we have, then we want to do a comparison
2069 * and skip the write if everything is the same.
2070 * If any blocks failed to read, then we need to
2071 * attempt an over-write
2072 */
NeilBrownfd01b882011-10-11 16:47:53 +11002073 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11002074 struct r1conf *conf = mddev->private;
NeilBrowna68e5872011-05-11 14:40:44 +10002075 int primary;
2076 int i;
majianpengf4380a92012-04-12 16:04:47 +10002077 int vcnt;
NeilBrowna68e5872011-05-11 14:40:44 +10002078
NeilBrown30bc9b52013-07-17 15:19:29 +10002079 /* Fix variable parts of all bios */
2080 vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9);
2081 for (i = 0; i < conf->raid_disks * 2; i++) {
2082 int j;
2083 int size;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002084 int error;
NeilBrown30bc9b52013-07-17 15:19:29 +10002085 struct bio *b = r1_bio->bios[i];
2086 if (b->bi_end_io != end_sync_read)
2087 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002088 /* fixup the bio for reuse, but preserve errno */
2089 error = b->bi_error;
NeilBrown30bc9b52013-07-17 15:19:29 +10002090 bio_reset(b);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002091 b->bi_error = error;
NeilBrown30bc9b52013-07-17 15:19:29 +10002092 b->bi_vcnt = vcnt;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002093 b->bi_iter.bi_size = r1_bio->sectors << 9;
2094 b->bi_iter.bi_sector = r1_bio->sector +
NeilBrown30bc9b52013-07-17 15:19:29 +10002095 conf->mirrors[i].rdev->data_offset;
2096 b->bi_bdev = conf->mirrors[i].rdev->bdev;
2097 b->bi_end_io = end_sync_read;
2098 b->bi_private = r1_bio;
2099
Kent Overstreet4f024f32013-10-11 15:44:27 -07002100 size = b->bi_iter.bi_size;
NeilBrown30bc9b52013-07-17 15:19:29 +10002101 for (j = 0; j < vcnt ; j++) {
2102 struct bio_vec *bi;
2103 bi = &b->bi_io_vec[j];
2104 bi->bv_offset = 0;
2105 if (size > PAGE_SIZE)
2106 bi->bv_len = PAGE_SIZE;
2107 else
2108 bi->bv_len = size;
2109 size -= PAGE_SIZE;
2110 }
2111 }
NeilBrown8f19ccb2011-12-23 10:17:56 +11002112 for (primary = 0; primary < conf->raid_disks * 2; primary++)
NeilBrowna68e5872011-05-11 14:40:44 +10002113 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002114 !r1_bio->bios[primary]->bi_error) {
NeilBrowna68e5872011-05-11 14:40:44 +10002115 r1_bio->bios[primary]->bi_end_io = NULL;
2116 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
2117 break;
2118 }
2119 r1_bio->read_disk = primary;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002120 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10002121 int j;
NeilBrown78d7f5f2011-05-11 14:48:56 +10002122 struct bio *pbio = r1_bio->bios[primary];
2123 struct bio *sbio = r1_bio->bios[i];
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002124 int error = sbio->bi_error;
NeilBrowna68e5872011-05-11 14:40:44 +10002125
Kent Overstreet2aabaa62012-09-11 11:26:12 -07002126 if (sbio->bi_end_io != end_sync_read)
NeilBrown78d7f5f2011-05-11 14:48:56 +10002127 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002128 /* Now we can 'fixup' the error value */
2129 sbio->bi_error = 0;
NeilBrowna68e5872011-05-11 14:40:44 +10002130
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002131 if (!error) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10002132 for (j = vcnt; j-- ; ) {
2133 struct page *p, *s;
2134 p = pbio->bi_io_vec[j].bv_page;
2135 s = sbio->bi_io_vec[j].bv_page;
2136 if (memcmp(page_address(p),
2137 page_address(s),
NeilBrown5020ad72012-04-02 01:39:05 +10002138 sbio->bi_io_vec[j].bv_len))
NeilBrown78d7f5f2011-05-11 14:48:56 +10002139 break;
NeilBrowna68e5872011-05-11 14:40:44 +10002140 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10002141 } else
2142 j = 0;
2143 if (j >= 0)
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002144 atomic64_add(r1_bio->sectors, &mddev->resync_mismatches);
NeilBrown78d7f5f2011-05-11 14:48:56 +10002145 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002146 && !error)) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10002147 /* No need to write to this device. */
2148 sbio->bi_end_io = NULL;
2149 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
2150 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10002151 }
Kent Overstreetd3b45c22012-09-10 13:49:33 -07002152
2153 bio_copy_data(sbio, pbio);
NeilBrown78d7f5f2011-05-11 14:48:56 +10002154 }
NeilBrowna68e5872011-05-11 14:40:44 +10002155}
2156
NeilBrown9f2c9d12011-10-11 16:48:43 +11002157static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
NeilBrowne8096362011-10-11 16:49:05 +11002159 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 int i;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002161 int disks = conf->raid_disks * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 struct bio *bio, *wbio;
2163
2164 bio = r1_bio->bios[r1_bio->read_disk];
2165
NeilBrowna68e5872011-05-11 14:40:44 +10002166 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
2167 /* ouch - failed to read all of that. */
2168 if (!fix_sync_read_error(r1_bio))
2169 return;
NeilBrown7ca78d52011-05-11 14:50:37 +10002170
2171 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrownc95e6382014-09-09 13:54:11 +10002172 process_checks(r1_bio);
2173
NeilBrownd11c1712006-01-06 00:20:26 -08002174 /*
2175 * schedule writes
2176 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 atomic_set(&r1_bio->remaining, 1);
2178 for (i = 0; i < disks ; i++) {
2179 wbio = r1_bio->bios[i];
NeilBrown3e198f72006-01-06 00:20:21 -08002180 if (wbio->bi_end_io == NULL ||
2181 (wbio->bi_end_io == end_sync_read &&
2182 (i == r1_bio->read_disk ||
2183 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 continue;
2185
Mike Christie796a5cf2016-06-05 14:32:07 -05002186 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
NeilBrown212e7eb2016-11-18 16:16:12 +11002187 if (test_bit(FailFast, &conf->mirrors[i].rdev->flags))
2188 wbio->bi_opf |= MD_FAILFAST;
2189
NeilBrown3e198f72006-01-06 00:20:21 -08002190 wbio->bi_end_io = end_sync_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 atomic_inc(&r1_bio->remaining);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002192 md_sync_acct(conf->mirrors[i].rdev->bdev, bio_sectors(wbio));
NeilBrown191ea9b2005-06-21 17:17:23 -07002193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 generic_make_request(wbio);
2195 }
2196
2197 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002198 /* if we're here, all write(s) have completed, so clean up */
NeilBrown58e94ae2012-07-19 15:59:18 +10002199 int s = r1_bio->sectors;
2200 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2201 test_bit(R1BIO_WriteError, &r1_bio->state))
2202 reschedule_retry(r1_bio);
2203 else {
2204 put_buf(r1_bio);
2205 md_done_sync(mddev, s, 1);
2206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
2208}
2209
2210/*
2211 * This is a kernel thread which:
2212 *
2213 * 1. Retries failed read operations on working mirrors.
2214 * 2. Updates the raid superblock when problems encounter.
NeilBrownd2eb35a2011-07-28 11:31:48 +10002215 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 */
2217
NeilBrowne8096362011-10-11 16:49:05 +11002218static void fix_read_error(struct r1conf *conf, int read_disk,
NeilBrown867868f2006-10-03 01:15:51 -07002219 sector_t sect, int sectors)
2220{
NeilBrownfd01b882011-10-11 16:47:53 +11002221 struct mddev *mddev = conf->mddev;
NeilBrown867868f2006-10-03 01:15:51 -07002222 while(sectors) {
2223 int s = sectors;
2224 int d = read_disk;
2225 int success = 0;
2226 int start;
NeilBrown3cb03002011-10-11 16:45:26 +11002227 struct md_rdev *rdev;
NeilBrown867868f2006-10-03 01:15:51 -07002228
2229 if (s > (PAGE_SIZE>>9))
2230 s = PAGE_SIZE >> 9;
2231
2232 do {
NeilBrownd2eb35a2011-07-28 11:31:48 +10002233 sector_t first_bad;
2234 int bad_sectors;
2235
NeilBrown707a6a42016-06-02 16:19:52 +10002236 rcu_read_lock();
2237 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002238 if (rdev &&
majianpengda8840a2012-05-22 13:55:03 +10002239 (test_bit(In_sync, &rdev->flags) ||
2240 (!test_bit(Faulty, &rdev->flags) &&
2241 rdev->recovery_offset >= sect + s)) &&
NeilBrownd2eb35a2011-07-28 11:31:48 +10002242 is_badblock(rdev, sect, s,
NeilBrown707a6a42016-06-02 16:19:52 +10002243 &first_bad, &bad_sectors) == 0) {
2244 atomic_inc(&rdev->nr_pending);
2245 rcu_read_unlock();
2246 if (sync_page_io(rdev, sect, s<<9,
Mike Christie796a5cf2016-06-05 14:32:07 -05002247 conf->tmppage, REQ_OP_READ, 0, false))
NeilBrown707a6a42016-06-02 16:19:52 +10002248 success = 1;
2249 rdev_dec_pending(rdev, mddev);
2250 if (success)
2251 break;
2252 } else
2253 rcu_read_unlock();
2254 d++;
2255 if (d == conf->raid_disks * 2)
2256 d = 0;
NeilBrown867868f2006-10-03 01:15:51 -07002257 } while (!success && d != read_disk);
2258
2259 if (!success) {
NeilBrownd8f05d22011-07-28 11:33:00 +10002260 /* Cannot read from anywhere - mark it bad */
NeilBrown3cb03002011-10-11 16:45:26 +11002261 struct md_rdev *rdev = conf->mirrors[read_disk].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10002262 if (!rdev_set_badblocks(rdev, sect, s, 0))
2263 md_error(mddev, rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002264 break;
2265 }
2266 /* write it back and re-read */
2267 start = d;
2268 while (d != read_disk) {
2269 if (d==0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002270 d = conf->raid_disks * 2;
NeilBrown867868f2006-10-03 01:15:51 -07002271 d--;
NeilBrown707a6a42016-06-02 16:19:52 +10002272 rcu_read_lock();
2273 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002274 if (rdev &&
NeilBrown707a6a42016-06-02 16:19:52 +10002275 !test_bit(Faulty, &rdev->flags)) {
2276 atomic_inc(&rdev->nr_pending);
2277 rcu_read_unlock();
NeilBrownd8f05d22011-07-28 11:33:00 +10002278 r1_sync_page_io(rdev, sect, s,
2279 conf->tmppage, WRITE);
NeilBrown707a6a42016-06-02 16:19:52 +10002280 rdev_dec_pending(rdev, mddev);
2281 } else
2282 rcu_read_unlock();
NeilBrown867868f2006-10-03 01:15:51 -07002283 }
2284 d = start;
2285 while (d != read_disk) {
2286 char b[BDEVNAME_SIZE];
2287 if (d==0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002288 d = conf->raid_disks * 2;
NeilBrown867868f2006-10-03 01:15:51 -07002289 d--;
NeilBrown707a6a42016-06-02 16:19:52 +10002290 rcu_read_lock();
2291 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002292 if (rdev &&
NeilBrownb8cb6b42014-09-18 11:09:04 +10002293 !test_bit(Faulty, &rdev->flags)) {
NeilBrown707a6a42016-06-02 16:19:52 +10002294 atomic_inc(&rdev->nr_pending);
2295 rcu_read_unlock();
NeilBrownd8f05d22011-07-28 11:33:00 +10002296 if (r1_sync_page_io(rdev, sect, s,
2297 conf->tmppage, READ)) {
NeilBrown867868f2006-10-03 01:15:51 -07002298 atomic_add(s, &rdev->corrected_errors);
NeilBrown1d41c212016-11-02 14:16:50 +11002299 pr_info("md/raid1:%s: read error corrected (%d sectors at %llu on %s)\n",
2300 mdname(mddev), s,
2301 (unsigned long long)(sect +
2302 rdev->data_offset),
2303 bdevname(rdev->bdev, b));
NeilBrown867868f2006-10-03 01:15:51 -07002304 }
NeilBrown707a6a42016-06-02 16:19:52 +10002305 rdev_dec_pending(rdev, mddev);
2306 } else
2307 rcu_read_unlock();
NeilBrown867868f2006-10-03 01:15:51 -07002308 }
2309 sectors -= s;
2310 sect += s;
2311 }
2312}
2313
NeilBrown9f2c9d12011-10-11 16:48:43 +11002314static int narrow_write_error(struct r1bio *r1_bio, int i)
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002315{
NeilBrownfd01b882011-10-11 16:47:53 +11002316 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11002317 struct r1conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002318 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002319
2320 /* bio has the data to be written to device 'i' where
2321 * we just recently had a write error.
2322 * We repeatedly clone the bio and trim down to one block,
2323 * then try the write. Where the write fails we record
2324 * a bad block.
2325 * It is conceivable that the bio doesn't exactly align with
2326 * blocks. We must handle this somehow.
2327 *
2328 * We currently own a reference on the rdev.
2329 */
2330
2331 int block_sectors;
2332 sector_t sector;
2333 int sectors;
2334 int sect_to_write = r1_bio->sectors;
2335 int ok = 1;
2336
2337 if (rdev->badblocks.shift < 0)
2338 return 0;
2339
Nate Daileyab713cd2015-02-12 12:02:09 -05002340 block_sectors = roundup(1 << rdev->badblocks.shift,
2341 bdev_logical_block_size(rdev->bdev) >> 9);
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002342 sector = r1_bio->sector;
2343 sectors = ((sector + block_sectors)
2344 & ~(sector_t)(block_sectors - 1))
2345 - sector;
2346
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002347 while (sect_to_write) {
2348 struct bio *wbio;
2349 if (sectors > sect_to_write)
2350 sectors = sect_to_write;
2351 /* Write at 'sector' for 'sectors'*/
2352
Kent Overstreetb7838632012-09-10 15:17:11 -07002353 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
2354 unsigned vcnt = r1_bio->behind_page_count;
2355 struct bio_vec *vec = r1_bio->behind_bvecs;
2356
2357 while (!vec->bv_page) {
2358 vec++;
2359 vcnt--;
2360 }
2361
2362 wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev);
2363 memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec));
2364
2365 wbio->bi_vcnt = vcnt;
2366 } else {
Ming Leid7a10302017-02-14 23:29:03 +08002367 wbio = bio_clone_fast(r1_bio->master_bio, GFP_NOIO,
2368 mddev->bio_set);
Kent Overstreetb7838632012-09-10 15:17:11 -07002369 }
2370
Mike Christie796a5cf2016-06-05 14:32:07 -05002371 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002372 wbio->bi_iter.bi_sector = r1_bio->sector;
2373 wbio->bi_iter.bi_size = r1_bio->sectors << 9;
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002374
Kent Overstreet6678d832013-08-07 11:14:32 -07002375 bio_trim(wbio, sector - r1_bio->sector, sectors);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002376 wbio->bi_iter.bi_sector += rdev->data_offset;
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002377 wbio->bi_bdev = rdev->bdev;
Mike Christie4e49ea42016-06-05 14:31:41 -05002378
2379 if (submit_bio_wait(wbio) < 0)
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002380 /* failure! */
2381 ok = rdev_set_badblocks(rdev, sector,
2382 sectors, 0)
2383 && ok;
2384
2385 bio_put(wbio);
2386 sect_to_write -= sectors;
2387 sector += sectors;
2388 sectors = block_sectors;
2389 }
2390 return ok;
2391}
2392
NeilBrowne8096362011-10-11 16:49:05 +11002393static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002394{
2395 int m;
2396 int s = r1_bio->sectors;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002397 for (m = 0; m < conf->raid_disks * 2 ; m++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002398 struct md_rdev *rdev = conf->mirrors[m].rdev;
NeilBrown62096bc2011-07-28 11:38:13 +10002399 struct bio *bio = r1_bio->bios[m];
2400 if (bio->bi_end_io == NULL)
2401 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002402 if (!bio->bi_error &&
NeilBrown62096bc2011-07-28 11:38:13 +10002403 test_bit(R1BIO_MadeGood, &r1_bio->state)) {
NeilBrownc6563a82012-05-21 09:27:00 +10002404 rdev_clear_badblocks(rdev, r1_bio->sector, s, 0);
NeilBrown62096bc2011-07-28 11:38:13 +10002405 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002406 if (bio->bi_error &&
NeilBrown62096bc2011-07-28 11:38:13 +10002407 test_bit(R1BIO_WriteError, &r1_bio->state)) {
2408 if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
2409 md_error(conf->mddev, rdev);
2410 }
2411 }
2412 put_buf(r1_bio);
2413 md_done_sync(conf->mddev, s, 1);
2414}
2415
NeilBrowne8096362011-10-11 16:49:05 +11002416static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002417{
colyli@suse.defd768632017-02-18 03:05:56 +08002418 int m, idx;
NeilBrown55ce74d2015-08-14 11:11:10 +10002419 bool fail = false;
colyli@suse.defd768632017-02-18 03:05:56 +08002420
NeilBrown8f19ccb2011-12-23 10:17:56 +11002421 for (m = 0; m < conf->raid_disks * 2 ; m++)
NeilBrown62096bc2011-07-28 11:38:13 +10002422 if (r1_bio->bios[m] == IO_MADE_GOOD) {
NeilBrown3cb03002011-10-11 16:45:26 +11002423 struct md_rdev *rdev = conf->mirrors[m].rdev;
NeilBrown62096bc2011-07-28 11:38:13 +10002424 rdev_clear_badblocks(rdev,
2425 r1_bio->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10002426 r1_bio->sectors, 0);
NeilBrown62096bc2011-07-28 11:38:13 +10002427 rdev_dec_pending(rdev, conf->mddev);
2428 } else if (r1_bio->bios[m] != NULL) {
2429 /* This drive got a write error. We need to
2430 * narrow down and record precise write
2431 * errors.
2432 */
NeilBrown55ce74d2015-08-14 11:11:10 +10002433 fail = true;
NeilBrown62096bc2011-07-28 11:38:13 +10002434 if (!narrow_write_error(r1_bio, m)) {
2435 md_error(conf->mddev,
2436 conf->mirrors[m].rdev);
2437 /* an I/O failed, we can't clear the bitmap */
2438 set_bit(R1BIO_Degraded, &r1_bio->state);
2439 }
2440 rdev_dec_pending(conf->mirrors[m].rdev,
2441 conf->mddev);
2442 }
NeilBrown55ce74d2015-08-14 11:11:10 +10002443 if (fail) {
2444 spin_lock_irq(&conf->device_lock);
2445 list_add(&r1_bio->retry_list, &conf->bio_end_io_list);
colyli@suse.defd768632017-02-18 03:05:56 +08002446 idx = sector_to_idx(r1_bio->sector);
colyli@suse.de824e47d2017-02-18 03:05:57 +08002447 atomic_inc(&conf->nr_queued[idx]);
NeilBrown55ce74d2015-08-14 11:11:10 +10002448 spin_unlock_irq(&conf->device_lock);
colyli@suse.de824e47d2017-02-18 03:05:57 +08002449 /*
2450 * In case freeze_array() is waiting for condition
2451 * get_unqueued_pending() == extra to be true.
2452 */
2453 wake_up(&conf->wait_barrier);
NeilBrown55ce74d2015-08-14 11:11:10 +10002454 md_wakeup_thread(conf->mddev->thread);
NeilBrownbd8688a2015-10-24 16:02:16 +11002455 } else {
2456 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2457 close_write(r1_bio);
NeilBrown55ce74d2015-08-14 11:11:10 +10002458 raid_end_bio_io(r1_bio);
NeilBrownbd8688a2015-10-24 16:02:16 +11002459 }
NeilBrown62096bc2011-07-28 11:38:13 +10002460}
2461
NeilBrowne8096362011-10-11 16:49:05 +11002462static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002463{
2464 int disk;
2465 int max_sectors;
NeilBrownfd01b882011-10-11 16:47:53 +11002466 struct mddev *mddev = conf->mddev;
NeilBrown62096bc2011-07-28 11:38:13 +10002467 struct bio *bio;
2468 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11002469 struct md_rdev *rdev;
NeilBrown109e3762016-11-18 13:22:04 +11002470 dev_t bio_dev;
2471 sector_t bio_sector;
NeilBrown62096bc2011-07-28 11:38:13 +10002472
2473 clear_bit(R1BIO_ReadError, &r1_bio->state);
2474 /* we got a read error. Maybe the drive is bad. Maybe just
2475 * the block and we can fix it.
2476 * We freeze all other IO, and try reading the block from
2477 * other devices. When we find one, we re-write
2478 * and check it that fixes the read error.
2479 * This is all done synchronously while the array is
2480 * frozen
2481 */
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002482
2483 bio = r1_bio->bios[r1_bio->read_disk];
2484 bdevname(bio->bi_bdev, b);
NeilBrown109e3762016-11-18 13:22:04 +11002485 bio_dev = bio->bi_bdev->bd_dev;
2486 bio_sector = conf->mirrors[r1_bio->read_disk].rdev->data_offset + r1_bio->sector;
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002487 bio_put(bio);
2488 r1_bio->bios[r1_bio->read_disk] = NULL;
2489
NeilBrown2e52d442016-11-18 16:16:12 +11002490 rdev = conf->mirrors[r1_bio->read_disk].rdev;
2491 if (mddev->ro == 0
2492 && !test_bit(FailFast, &rdev->flags)) {
NeilBrowne2d59922013-06-12 11:01:22 +10002493 freeze_array(conf, 1);
NeilBrown62096bc2011-07-28 11:38:13 +10002494 fix_read_error(conf, r1_bio->read_disk,
2495 r1_bio->sector, r1_bio->sectors);
2496 unfreeze_array(conf);
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002497 } else {
2498 r1_bio->bios[r1_bio->read_disk] = IO_BLOCKED;
2499 }
2500
NeilBrown2e52d442016-11-18 16:16:12 +11002501 rdev_dec_pending(rdev, conf->mddev);
NeilBrown62096bc2011-07-28 11:38:13 +10002502
NeilBrown62096bc2011-07-28 11:38:13 +10002503read_more:
2504 disk = read_balance(conf, r1_bio, &max_sectors);
2505 if (disk == -1) {
NeilBrown1d41c212016-11-02 14:16:50 +11002506 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
2507 mdname(mddev), b, (unsigned long long)r1_bio->sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002508 raid_end_bio_io(r1_bio);
2509 } else {
2510 const unsigned long do_sync
Jens Axboe1eff9d32016-08-05 15:35:16 -06002511 = r1_bio->master_bio->bi_opf & REQ_SYNC;
NeilBrown62096bc2011-07-28 11:38:13 +10002512 r1_bio->read_disk = disk;
Ming Leid7a10302017-02-14 23:29:03 +08002513 bio = bio_clone_fast(r1_bio->master_bio, GFP_NOIO,
2514 mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002515 bio_trim(bio, r1_bio->sector - bio->bi_iter.bi_sector,
2516 max_sectors);
NeilBrown62096bc2011-07-28 11:38:13 +10002517 r1_bio->bios[r1_bio->read_disk] = bio;
2518 rdev = conf->mirrors[disk].rdev;
NeilBrown1d41c212016-11-02 14:16:50 +11002519 pr_info_ratelimited("md/raid1:%s: redirecting sector %llu to other mirror: %s\n",
2520 mdname(mddev),
2521 (unsigned long long)r1_bio->sector,
2522 bdevname(rdev->bdev, b));
Kent Overstreet4f024f32013-10-11 15:44:27 -07002523 bio->bi_iter.bi_sector = r1_bio->sector + rdev->data_offset;
NeilBrown62096bc2011-07-28 11:38:13 +10002524 bio->bi_bdev = rdev->bdev;
2525 bio->bi_end_io = raid1_end_read_request;
Mike Christie796a5cf2016-06-05 14:32:07 -05002526 bio_set_op_attrs(bio, REQ_OP_READ, do_sync);
NeilBrown2e52d442016-11-18 16:16:12 +11002527 if (test_bit(FailFast, &rdev->flags) &&
2528 test_bit(R1BIO_FailFast, &r1_bio->state))
2529 bio->bi_opf |= MD_FAILFAST;
NeilBrown62096bc2011-07-28 11:38:13 +10002530 bio->bi_private = r1_bio;
2531 if (max_sectors < r1_bio->sectors) {
2532 /* Drat - have to split this up more */
2533 struct bio *mbio = r1_bio->master_bio;
2534 int sectors_handled = (r1_bio->sector + max_sectors
Kent Overstreet4f024f32013-10-11 15:44:27 -07002535 - mbio->bi_iter.bi_sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002536 r1_bio->sectors = max_sectors;
NeilBrown37011e32017-03-15 14:05:14 +11002537 bio_inc_remaining(mbio);
NeilBrown109e3762016-11-18 13:22:04 +11002538 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2539 bio, bio_dev, bio_sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002540 generic_make_request(bio);
2541 bio = NULL;
2542
colyli@suse.defd768632017-02-18 03:05:56 +08002543 r1_bio = alloc_r1bio(mddev, mbio, sectors_handled);
NeilBrown62096bc2011-07-28 11:38:13 +10002544 set_bit(R1BIO_ReadError, &r1_bio->state);
NeilBrown37011e32017-03-15 14:05:14 +11002545 inc_pending(conf, r1_bio->sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002546
2547 goto read_more;
NeilBrown109e3762016-11-18 13:22:04 +11002548 } else {
2549 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2550 bio, bio_dev, bio_sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002551 generic_make_request(bio);
NeilBrown109e3762016-11-18 13:22:04 +11002552 }
NeilBrown62096bc2011-07-28 11:38:13 +10002553 }
2554}
2555
Shaohua Li4ed87312012-10-11 13:34:00 +11002556static void raid1d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557{
Shaohua Li4ed87312012-10-11 13:34:00 +11002558 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002559 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 unsigned long flags;
NeilBrowne8096362011-10-11 16:49:05 +11002561 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002563 struct blk_plug plug;
colyli@suse.defd768632017-02-18 03:05:56 +08002564 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
2566 md_check_recovery(mddev);
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002567
NeilBrown55ce74d2015-08-14 11:11:10 +10002568 if (!list_empty_careful(&conf->bio_end_io_list) &&
Shaohua Li29530792016-12-08 15:48:19 -08002569 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrown55ce74d2015-08-14 11:11:10 +10002570 LIST_HEAD(tmp);
2571 spin_lock_irqsave(&conf->device_lock, flags);
colyli@suse.defd768632017-02-18 03:05:56 +08002572 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
2573 list_splice_init(&conf->bio_end_io_list, &tmp);
NeilBrown55ce74d2015-08-14 11:11:10 +10002574 spin_unlock_irqrestore(&conf->device_lock, flags);
2575 while (!list_empty(&tmp)) {
Mikulas Patockaa4527442015-10-01 15:17:43 -04002576 r1_bio = list_first_entry(&tmp, struct r1bio,
2577 retry_list);
NeilBrown55ce74d2015-08-14 11:11:10 +10002578 list_del(&r1_bio->retry_list);
colyli@suse.defd768632017-02-18 03:05:56 +08002579 idx = sector_to_idx(r1_bio->sector);
colyli@suse.de824e47d2017-02-18 03:05:57 +08002580 atomic_dec(&conf->nr_queued[idx]);
NeilBrownbd8688a2015-10-24 16:02:16 +11002581 if (mddev->degraded)
2582 set_bit(R1BIO_Degraded, &r1_bio->state);
2583 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2584 close_write(r1_bio);
NeilBrown55ce74d2015-08-14 11:11:10 +10002585 raid_end_bio_io(r1_bio);
2586 }
2587 }
2588
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002589 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002591
NeilBrown0021b7b2012-07-31 09:08:14 +02002592 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002595 if (list_empty(head)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002596 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002598 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002599 r1_bio = list_entry(head->prev, struct r1bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 list_del(head->prev);
colyli@suse.defd768632017-02-18 03:05:56 +08002601 idx = sector_to_idx(r1_bio->sector);
colyli@suse.de824e47d2017-02-18 03:05:57 +08002602 atomic_dec(&conf->nr_queued[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 spin_unlock_irqrestore(&conf->device_lock, flags);
2604
2605 mddev = r1_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002606 conf = mddev->private;
NeilBrown4367af52011-07-28 11:31:49 +10002607 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
NeilBrownd8f05d22011-07-28 11:33:00 +10002608 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
NeilBrown62096bc2011-07-28 11:38:13 +10002609 test_bit(R1BIO_WriteError, &r1_bio->state))
2610 handle_sync_write_finished(conf, r1_bio);
2611 else
NeilBrown4367af52011-07-28 11:31:49 +10002612 sync_request_write(mddev, r1_bio);
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002613 } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
NeilBrown62096bc2011-07-28 11:38:13 +10002614 test_bit(R1BIO_WriteError, &r1_bio->state))
2615 handle_write_finished(conf, r1_bio);
2616 else if (test_bit(R1BIO_ReadError, &r1_bio->state))
2617 handle_read_error(conf, r1_bio);
2618 else
NeilBrownd2eb35a2011-07-28 11:31:48 +10002619 /* just a partial read to be scheduled from separate
2620 * context
2621 */
2622 generic_make_request(r1_bio->bios[r1_bio->read_disk]);
NeilBrown62096bc2011-07-28 11:38:13 +10002623
NeilBrown1d9d5242009-10-16 15:55:32 +11002624 cond_resched();
Shaohua Li29530792016-12-08 15:48:19 -08002625 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
NeilBrownde393cd2011-07-28 11:31:48 +10002626 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002628 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
2630
NeilBrowne8096362011-10-11 16:49:05 +11002631static int init_resync(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
2633 int buffs;
2634
2635 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhenn9e77c482006-04-01 01:08:49 +02002636 BUG_ON(conf->r1buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
2638 conf->poolinfo);
2639 if (!conf->r1buf_pool)
2640 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 return 0;
2642}
2643
2644/*
2645 * perform a "sync" on one "block"
2646 *
2647 * We need to make sure that no normal I/O request - particularly write
2648 * requests - conflict with active sync requests.
2649 *
2650 * This is achieved by tracking pending requests and a 'barrier' concept
2651 * that can be installed to exclude normal IO requests.
2652 */
2653
Shaohua Li849674e2016-01-20 13:52:20 -08002654static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr,
2655 int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
NeilBrowne8096362011-10-11 16:49:05 +11002657 struct r1conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002658 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 struct bio *bio;
2660 sector_t max_sector, nr_sectors;
NeilBrown3e198f72006-01-06 00:20:21 -08002661 int disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 int i;
NeilBrown3e198f72006-01-06 00:20:21 -08002663 int wonly = -1;
2664 int write_targets = 0, read_targets = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11002665 sector_t sync_blocks;
NeilBrowne3b97032005-08-04 12:53:34 -07002666 int still_degraded = 0;
NeilBrown06f60382011-07-28 11:31:48 +10002667 int good_sectors = RESYNC_SECTORS;
2668 int min_bad = 0; /* number of sectors that are bad in all devices */
colyli@suse.defd768632017-02-18 03:05:56 +08002669 int idx = sector_to_idx(sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
2671 if (!conf->r1buf_pool)
2672 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Andre Noll58c0fed2009-03-31 14:33:13 +11002675 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 if (sector_nr >= max_sector) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002677 /* If we aborted, we need to abort the
2678 * sync on the 'current' bitmap chunk (there will
2679 * only be one in raid1 resync.
2680 * We can find the current addess in mddev->curr_resync
2681 */
NeilBrown6a806c52005-07-15 03:56:35 -07002682 if (mddev->curr_resync < max_sector) /* aborted */
2683 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
NeilBrown191ea9b2005-06-21 17:17:23 -07002684 &sync_blocks, 1);
NeilBrown6a806c52005-07-15 03:56:35 -07002685 else /* completed sync */
NeilBrown191ea9b2005-06-21 17:17:23 -07002686 conf->fullsync = 0;
NeilBrown6a806c52005-07-15 03:56:35 -07002687
2688 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 close_sync(conf);
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002690
2691 if (mddev_is_clustered(mddev)) {
2692 conf->cluster_sync_low = 0;
2693 conf->cluster_sync_high = 0;
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 return 0;
2696 }
2697
NeilBrown07d84d102006-06-26 00:27:56 -07002698 if (mddev->bitmap == NULL &&
2699 mddev->recovery_cp == MaxSector &&
NeilBrown6394cca2006-08-27 01:23:50 -07002700 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown07d84d102006-06-26 00:27:56 -07002701 conf->fullsync == 0) {
2702 *skipped = 1;
2703 return max_sector - sector_nr;
2704 }
NeilBrown6394cca2006-08-27 01:23:50 -07002705 /* before building a request, check if we can skip these blocks..
2706 * This call the bitmap_start_sync doesn't actually record anything
2707 */
NeilBrowne3b97032005-08-04 12:53:34 -07002708 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrowne5de485f2005-11-08 21:39:38 -08002709 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002710 /* We can skip this block, and probably several more */
2711 *skipped = 1;
2712 return sync_blocks;
2713 }
NeilBrown17999be2006-01-06 00:20:12 -08002714
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02002715 /*
2716 * If there is non-resync activity waiting for a turn, then let it
2717 * though before starting on this new sync request.
2718 */
colyli@suse.de824e47d2017-02-18 03:05:57 +08002719 if (atomic_read(&conf->nr_waiting[idx]))
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02002720 schedule_timeout_uninterruptible(1);
2721
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002722 /* we are incrementing sector_nr below. To be safe, we check against
2723 * sector_nr + two times RESYNC_SECTORS
2724 */
2725
2726 bitmap_cond_end_sync(mddev->bitmap, sector_nr,
2727 mddev_is_clustered(mddev) && (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
NeilBrown1c4588e2010-10-26 17:41:22 +11002728 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
NeilBrown17999be2006-01-06 00:20:12 -08002729
NeilBrownc2fd4c92014-09-10 16:01:24 +10002730 raise_barrier(conf, sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
NeilBrown3e198f72006-01-06 00:20:21 -08002732 rcu_read_lock();
2733 /*
2734 * If we get a correctably read error during resync or recovery,
2735 * we might want to read from a different device. So we
2736 * flag all drives that could conceivably be read from for READ,
2737 * and any others (which will be non-In_sync devices) for WRITE.
2738 * If a read fails, we try reading from something else for which READ
2739 * is OK.
2740 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 r1_bio->mddev = mddev;
2743 r1_bio->sector = sector_nr;
NeilBrown191ea9b2005-06-21 17:17:23 -07002744 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 set_bit(R1BIO_IsSync, &r1_bio->state);
colyli@suse.defd768632017-02-18 03:05:56 +08002746 /* make sure good_sectors won't go across barrier unit boundary */
2747 good_sectors = align_to_barrier_unit_end(sector_nr, good_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
NeilBrown8f19ccb2011-12-23 10:17:56 +11002749 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002750 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 bio = r1_bio->bios[i];
Kent Overstreet2aabaa62012-09-11 11:26:12 -07002752 bio_reset(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
NeilBrown3e198f72006-01-06 00:20:21 -08002754 rdev = rcu_dereference(conf->mirrors[i].rdev);
2755 if (rdev == NULL ||
NeilBrown06f60382011-07-28 11:31:48 +10002756 test_bit(Faulty, &rdev->flags)) {
NeilBrown8f19ccb2011-12-23 10:17:56 +11002757 if (i < conf->raid_disks)
2758 still_degraded = 1;
NeilBrown3e198f72006-01-06 00:20:21 -08002759 } else if (!test_bit(In_sync, &rdev->flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05002760 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 bio->bi_end_io = end_sync_write;
2762 write_targets ++;
NeilBrown3e198f72006-01-06 00:20:21 -08002763 } else {
2764 /* may need to read from here */
NeilBrown06f60382011-07-28 11:31:48 +10002765 sector_t first_bad = MaxSector;
2766 int bad_sectors;
2767
2768 if (is_badblock(rdev, sector_nr, good_sectors,
2769 &first_bad, &bad_sectors)) {
2770 if (first_bad > sector_nr)
2771 good_sectors = first_bad - sector_nr;
2772 else {
2773 bad_sectors -= (sector_nr - first_bad);
2774 if (min_bad == 0 ||
2775 min_bad > bad_sectors)
2776 min_bad = bad_sectors;
2777 }
NeilBrown3e198f72006-01-06 00:20:21 -08002778 }
NeilBrown06f60382011-07-28 11:31:48 +10002779 if (sector_nr < first_bad) {
2780 if (test_bit(WriteMostly, &rdev->flags)) {
2781 if (wonly < 0)
2782 wonly = i;
2783 } else {
2784 if (disk < 0)
2785 disk = i;
2786 }
Mike Christie796a5cf2016-06-05 14:32:07 -05002787 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown06f60382011-07-28 11:31:48 +10002788 bio->bi_end_io = end_sync_read;
2789 read_targets++;
Alexander Lyakasd57368a2012-07-17 13:17:55 +03002790 } else if (!test_bit(WriteErrorSeen, &rdev->flags) &&
2791 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
2792 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
2793 /*
2794 * The device is suitable for reading (InSync),
2795 * but has bad block(s) here. Let's try to correct them,
2796 * if we are doing resync or repair. Otherwise, leave
2797 * this device alone for this sync request.
2798 */
Mike Christie796a5cf2016-06-05 14:32:07 -05002799 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Alexander Lyakasd57368a2012-07-17 13:17:55 +03002800 bio->bi_end_io = end_sync_write;
2801 write_targets++;
NeilBrown06f60382011-07-28 11:31:48 +10002802 }
NeilBrown3e198f72006-01-06 00:20:21 -08002803 }
NeilBrown06f60382011-07-28 11:31:48 +10002804 if (bio->bi_end_io) {
2805 atomic_inc(&rdev->nr_pending);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002806 bio->bi_iter.bi_sector = sector_nr + rdev->data_offset;
NeilBrown06f60382011-07-28 11:31:48 +10002807 bio->bi_bdev = rdev->bdev;
2808 bio->bi_private = r1_bio;
NeilBrown2e52d442016-11-18 16:16:12 +11002809 if (test_bit(FailFast, &rdev->flags))
2810 bio->bi_opf |= MD_FAILFAST;
NeilBrown06f60382011-07-28 11:31:48 +10002811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
NeilBrown3e198f72006-01-06 00:20:21 -08002813 rcu_read_unlock();
2814 if (disk < 0)
2815 disk = wonly;
2816 r1_bio->read_disk = disk;
NeilBrown191ea9b2005-06-21 17:17:23 -07002817
NeilBrown06f60382011-07-28 11:31:48 +10002818 if (read_targets == 0 && min_bad > 0) {
2819 /* These sectors are bad on all InSync devices, so we
2820 * need to mark them bad on all write targets
2821 */
2822 int ok = 1;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002823 for (i = 0 ; i < conf->raid_disks * 2 ; i++)
NeilBrown06f60382011-07-28 11:31:48 +10002824 if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
majianpenga42f9d82012-04-02 01:04:19 +10002825 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrown06f60382011-07-28 11:31:48 +10002826 ok = rdev_set_badblocks(rdev, sector_nr,
2827 min_bad, 0
2828 ) && ok;
2829 }
Shaohua Li29530792016-12-08 15:48:19 -08002830 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown06f60382011-07-28 11:31:48 +10002831 *skipped = 1;
2832 put_buf(r1_bio);
2833
2834 if (!ok) {
2835 /* Cannot record the badblocks, so need to
2836 * abort the resync.
2837 * If there are multiple read targets, could just
2838 * fail the really bad ones ???
2839 */
2840 conf->recovery_disabled = mddev->recovery_disabled;
2841 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2842 return 0;
2843 } else
2844 return min_bad;
2845
2846 }
2847 if (min_bad > 0 && min_bad < good_sectors) {
2848 /* only resync enough to reach the next bad->good
2849 * transition */
2850 good_sectors = min_bad;
2851 }
2852
NeilBrown3e198f72006-01-06 00:20:21 -08002853 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
2854 /* extra read targets are also write targets */
2855 write_targets += read_targets-1;
2856
2857 if (write_targets == 0 || read_targets == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 /* There is nowhere to write, so all non-sync
2859 * drives must be failed - so we are finished
2860 */
NeilBrownb7219cc2012-07-31 10:05:34 +10002861 sector_t rv;
2862 if (min_bad > 0)
2863 max_sector = sector_nr + min_bad;
2864 rv = max_sector - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07002865 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 put_buf(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 return rv;
2868 }
2869
NeilBrownc6207272008-02-06 01:39:52 -08002870 if (max_sector > mddev->resync_max)
2871 max_sector = mddev->resync_max; /* Don't do IO beyond here */
NeilBrown06f60382011-07-28 11:31:48 +10002872 if (max_sector > sector_nr + good_sectors)
2873 max_sector = sector_nr + good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 nr_sectors = 0;
NeilBrown289e99e2005-06-21 17:17:24 -07002875 sync_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 do {
2877 struct page *page;
2878 int len = PAGE_SIZE;
2879 if (sector_nr + (len>>9) > max_sector)
2880 len = (max_sector - sector_nr) << 9;
2881 if (len == 0)
2882 break;
NeilBrown6a806c52005-07-15 03:56:35 -07002883 if (sync_blocks == 0) {
2884 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
NeilBrowne5de485f2005-11-08 21:39:38 -08002885 &sync_blocks, still_degraded) &&
2886 !conf->fullsync &&
2887 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrown6a806c52005-07-15 03:56:35 -07002888 break;
NeilBrown7571ae82010-10-07 11:54:46 +11002889 if ((len >> 9) > sync_blocks)
NeilBrown6a806c52005-07-15 03:56:35 -07002890 len = sync_blocks<<9;
NeilBrownab7a30c2005-06-21 17:17:23 -07002891 }
NeilBrown191ea9b2005-06-21 17:17:23 -07002892
NeilBrown8f19ccb2011-12-23 10:17:56 +11002893 for (i = 0 ; i < conf->raid_disks * 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 bio = r1_bio->bios[i];
2895 if (bio->bi_end_io) {
NeilBrownd11c1712006-01-06 00:20:26 -08002896 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 if (bio_add_page(bio, page, len, 0) == 0) {
2898 /* stop here */
NeilBrownd11c1712006-01-06 00:20:26 -08002899 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 while (i > 0) {
2901 i--;
2902 bio = r1_bio->bios[i];
NeilBrown6a806c52005-07-15 03:56:35 -07002903 if (bio->bi_end_io==NULL)
2904 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 /* remove last page from this bio */
2906 bio->bi_vcnt--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002907 bio->bi_iter.bi_size -= len;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06002908 bio_clear_flag(bio, BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 }
2910 goto bio_full;
2911 }
2912 }
2913 }
2914 nr_sectors += len>>9;
2915 sector_nr += len>>9;
NeilBrown191ea9b2005-06-21 17:17:23 -07002916 sync_blocks -= (len>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
2918 bio_full:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 r1_bio->sectors = nr_sectors;
2920
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002921 if (mddev_is_clustered(mddev) &&
2922 conf->cluster_sync_high < sector_nr + nr_sectors) {
2923 conf->cluster_sync_low = mddev->curr_resync_completed;
2924 conf->cluster_sync_high = conf->cluster_sync_low + CLUSTER_RESYNC_WINDOW_SECTORS;
2925 /* Send resync message */
2926 md_cluster_ops->resync_info_update(mddev,
2927 conf->cluster_sync_low,
2928 conf->cluster_sync_high);
2929 }
2930
NeilBrownd11c1712006-01-06 00:20:26 -08002931 /* For a user-requested sync, we read all readable devices and do a
2932 * compare
2933 */
2934 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2935 atomic_set(&r1_bio->remaining, read_targets);
NeilBrown2d4f4f32012-07-09 11:34:13 +10002936 for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) {
NeilBrownd11c1712006-01-06 00:20:26 -08002937 bio = r1_bio->bios[i];
2938 if (bio->bi_end_io == end_sync_read) {
NeilBrown2d4f4f32012-07-09 11:34:13 +10002939 read_targets--;
NeilBrownddac7c72006-08-31 21:27:36 -07002940 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrown2e52d442016-11-18 16:16:12 +11002941 if (read_targets == 1)
2942 bio->bi_opf &= ~MD_FAILFAST;
NeilBrownd11c1712006-01-06 00:20:26 -08002943 generic_make_request(bio);
2944 }
2945 }
2946 } else {
2947 atomic_set(&r1_bio->remaining, 1);
2948 bio = r1_bio->bios[r1_bio->read_disk];
NeilBrownddac7c72006-08-31 21:27:36 -07002949 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrown2e52d442016-11-18 16:16:12 +11002950 if (read_targets == 1)
2951 bio->bi_opf &= ~MD_FAILFAST;
NeilBrownd11c1712006-01-06 00:20:26 -08002952 generic_make_request(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
NeilBrownd11c1712006-01-06 00:20:26 -08002954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 return nr_sectors;
2956}
2957
NeilBrownfd01b882011-10-11 16:47:53 +11002958static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07002959{
2960 if (sectors)
2961 return sectors;
2962
2963 return mddev->dev_sectors;
2964}
2965
NeilBrowne8096362011-10-11 16:49:05 +11002966static struct r1conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
NeilBrowne8096362011-10-11 16:49:05 +11002968 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11002969 int i;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10002970 struct raid1_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11002971 struct md_rdev *rdev;
NeilBrown709ae482009-12-14 12:49:51 +11002972 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
NeilBrowne8096362011-10-11 16:49:05 +11002974 conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 if (!conf)
NeilBrown709ae482009-12-14 12:49:51 +11002976 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
colyli@suse.defd768632017-02-18 03:05:56 +08002978 conf->nr_pending = kcalloc(BARRIER_BUCKETS_NR,
colyli@suse.de824e47d2017-02-18 03:05:57 +08002979 sizeof(atomic_t), GFP_KERNEL);
colyli@suse.defd768632017-02-18 03:05:56 +08002980 if (!conf->nr_pending)
2981 goto abort;
2982
2983 conf->nr_waiting = kcalloc(BARRIER_BUCKETS_NR,
colyli@suse.de824e47d2017-02-18 03:05:57 +08002984 sizeof(atomic_t), GFP_KERNEL);
colyli@suse.defd768632017-02-18 03:05:56 +08002985 if (!conf->nr_waiting)
2986 goto abort;
2987
2988 conf->nr_queued = kcalloc(BARRIER_BUCKETS_NR,
colyli@suse.de824e47d2017-02-18 03:05:57 +08002989 sizeof(atomic_t), GFP_KERNEL);
colyli@suse.defd768632017-02-18 03:05:56 +08002990 if (!conf->nr_queued)
2991 goto abort;
2992
2993 conf->barrier = kcalloc(BARRIER_BUCKETS_NR,
colyli@suse.de824e47d2017-02-18 03:05:57 +08002994 sizeof(atomic_t), GFP_KERNEL);
colyli@suse.defd768632017-02-18 03:05:56 +08002995 if (!conf->barrier)
2996 goto abort;
2997
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10002998 conf->mirrors = kzalloc(sizeof(struct raid1_info)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002999 * mddev->raid_disks * 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 GFP_KERNEL);
3001 if (!conf->mirrors)
NeilBrown709ae482009-12-14 12:49:51 +11003002 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
NeilBrownddaf22a2006-01-06 00:20:19 -08003004 conf->tmppage = alloc_page(GFP_KERNEL);
3005 if (!conf->tmppage)
NeilBrown709ae482009-12-14 12:49:51 +11003006 goto abort;
NeilBrownddaf22a2006-01-06 00:20:19 -08003007
NeilBrown709ae482009-12-14 12:49:51 +11003008 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 if (!conf->poolinfo)
NeilBrown709ae482009-12-14 12:49:51 +11003010 goto abort;
NeilBrown8f19ccb2011-12-23 10:17:56 +11003011 conf->poolinfo->raid_disks = mddev->raid_disks * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
3013 r1bio_pool_free,
3014 conf->poolinfo);
3015 if (!conf->r1bio_pool)
NeilBrown709ae482009-12-14 12:49:51 +11003016 goto abort;
3017
NeilBrowned9bfdf2009-10-16 15:55:44 +11003018 conf->poolinfo->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
NeilBrownc19d5792011-12-23 10:17:57 +11003020 err = -EINVAL;
Neil Browne7e72bf2008-05-14 16:05:54 -07003021 spin_lock_init(&conf->device_lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11003022 rdev_for_each(rdev, mddev) {
NeilBrownaba336b2012-05-31 15:39:11 +10003023 struct request_queue *q;
NeilBrown709ae482009-12-14 12:49:51 +11003024 int disk_idx = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 if (disk_idx >= mddev->raid_disks
3026 || disk_idx < 0)
3027 continue;
NeilBrownc19d5792011-12-23 10:17:57 +11003028 if (test_bit(Replacement, &rdev->flags))
NeilBrown02b898f2012-10-31 11:42:03 +11003029 disk = conf->mirrors + mddev->raid_disks + disk_idx;
NeilBrownc19d5792011-12-23 10:17:57 +11003030 else
3031 disk = conf->mirrors + disk_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
NeilBrownc19d5792011-12-23 10:17:57 +11003033 if (disk->rdev)
3034 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 disk->rdev = rdev;
NeilBrownaba336b2012-05-31 15:39:11 +10003036 q = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
3038 disk->head_position = 0;
Shaohua Li12cee5a2012-07-31 10:03:53 +10003039 disk->seq_start = MaxSector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 }
3041 conf->raid_disks = mddev->raid_disks;
3042 conf->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 INIT_LIST_HEAD(&conf->retry_list);
NeilBrown55ce74d2015-08-14 11:11:10 +10003044 INIT_LIST_HEAD(&conf->bio_end_io_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
3046 spin_lock_init(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -08003047 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
NeilBrown191ea9b2005-06-21 17:17:23 -07003049 bio_list_init(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +11003050 conf->pending_count = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11003051 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown191ea9b2005-06-21 17:17:23 -07003052
NeilBrownc19d5792011-12-23 10:17:57 +11003053 err = -EIO;
NeilBrown8f19ccb2011-12-23 10:17:56 +11003054 for (i = 0; i < conf->raid_disks * 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
3056 disk = conf->mirrors + i;
3057
NeilBrownc19d5792011-12-23 10:17:57 +11003058 if (i < conf->raid_disks &&
3059 disk[conf->raid_disks].rdev) {
3060 /* This slot has a replacement. */
3061 if (!disk->rdev) {
3062 /* No original, just make the replacement
3063 * a recovering spare
3064 */
3065 disk->rdev =
3066 disk[conf->raid_disks].rdev;
3067 disk[conf->raid_disks].rdev = NULL;
3068 } else if (!test_bit(In_sync, &disk->rdev->flags))
3069 /* Original is not in_sync - bad */
3070 goto abort;
3071 }
3072
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003073 if (!disk->rdev ||
3074 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 disk->head_position = 0;
Jonathan Brassow4f0a5e02012-05-22 13:55:31 +10003076 if (disk->rdev &&
3077 (disk->rdev->saved_raid_disk < 0))
NeilBrown918f0232007-08-22 14:01:52 -07003078 conf->fullsync = 1;
Shaohua Libe4d3282012-07-31 10:03:53 +10003079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 }
NeilBrown709ae482009-12-14 12:49:51 +11003081
NeilBrown709ae482009-12-14 12:49:51 +11003082 err = -ENOMEM;
NeilBrown02326052012-07-03 15:56:52 +10003083 conf->thread = md_register_thread(raid1d, mddev, "raid1");
NeilBrown1d41c212016-11-02 14:16:50 +11003084 if (!conf->thread)
NeilBrown709ae482009-12-14 12:49:51 +11003085 goto abort;
NeilBrown191ea9b2005-06-21 17:17:23 -07003086
NeilBrown709ae482009-12-14 12:49:51 +11003087 return conf;
3088
3089 abort:
3090 if (conf) {
Julia Lawall644df1a2015-09-13 14:15:10 +02003091 mempool_destroy(conf->r1bio_pool);
NeilBrown709ae482009-12-14 12:49:51 +11003092 kfree(conf->mirrors);
3093 safe_put_page(conf->tmppage);
3094 kfree(conf->poolinfo);
colyli@suse.defd768632017-02-18 03:05:56 +08003095 kfree(conf->nr_pending);
3096 kfree(conf->nr_waiting);
3097 kfree(conf->nr_queued);
3098 kfree(conf->barrier);
NeilBrown709ae482009-12-14 12:49:51 +11003099 kfree(conf);
3100 }
3101 return ERR_PTR(err);
3102}
3103
NeilBrownafa0f552014-12-15 12:56:58 +11003104static void raid1_free(struct mddev *mddev, void *priv);
Shaohua Li849674e2016-01-20 13:52:20 -08003105static int raid1_run(struct mddev *mddev)
NeilBrown709ae482009-12-14 12:49:51 +11003106{
NeilBrowne8096362011-10-11 16:49:05 +11003107 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11003108 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003109 struct md_rdev *rdev;
majianpeng5220ea12012-04-02 09:48:38 +10003110 int ret;
Shaohua Li2ff8cc22012-10-11 13:28:54 +11003111 bool discard_supported = false;
NeilBrown709ae482009-12-14 12:49:51 +11003112
3113 if (mddev->level != 1) {
NeilBrown1d41c212016-11-02 14:16:50 +11003114 pr_warn("md/raid1:%s: raid level not set to mirroring (%d)\n",
3115 mdname(mddev), mddev->level);
NeilBrown709ae482009-12-14 12:49:51 +11003116 return -EIO;
3117 }
3118 if (mddev->reshape_position != MaxSector) {
NeilBrown1d41c212016-11-02 14:16:50 +11003119 pr_warn("md/raid1:%s: reshape_position set but not supported\n",
3120 mdname(mddev));
NeilBrown709ae482009-12-14 12:49:51 +11003121 return -EIO;
3122 }
3123 /*
3124 * copy the already verified devices into our private RAID1
3125 * bookkeeping area. [whatever we allocate in run(),
NeilBrownafa0f552014-12-15 12:56:58 +11003126 * should be freed in raid1_free()]
NeilBrown709ae482009-12-14 12:49:51 +11003127 */
3128 if (mddev->private == NULL)
3129 conf = setup_conf(mddev);
3130 else
3131 conf = mddev->private;
3132
3133 if (IS_ERR(conf))
3134 return PTR_ERR(conf);
3135
Joe Lawrencec8dc9c62013-02-21 13:28:09 +11003136 if (mddev->queue)
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07003137 blk_queue_max_write_same_sectors(mddev->queue, 0);
3138
NeilBrowndafb20f2012-03-19 12:46:39 +11003139 rdev_for_each(rdev, mddev) {
Jonathan Brassow1ed72422011-06-07 17:50:35 -05003140 if (!mddev->gendisk)
3141 continue;
NeilBrown709ae482009-12-14 12:49:51 +11003142 disk_stack_limits(mddev->gendisk, rdev->bdev,
3143 rdev->data_offset << 9);
Shaohua Li2ff8cc22012-10-11 13:28:54 +11003144 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3145 discard_supported = true;
NeilBrown709ae482009-12-14 12:49:51 +11003146 }
3147
3148 mddev->degraded = 0;
3149 for (i=0; i < conf->raid_disks; i++)
3150 if (conf->mirrors[i].rdev == NULL ||
3151 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
3152 test_bit(Faulty, &conf->mirrors[i].rdev->flags))
3153 mddev->degraded++;
3154
3155 if (conf->raid_disks - mddev->degraded == 1)
3156 mddev->recovery_cp = MaxSector;
3157
Andre Noll8c6ac8682009-06-18 08:48:06 +10003158 if (mddev->recovery_cp != MaxSector)
NeilBrown1d41c212016-11-02 14:16:50 +11003159 pr_info("md/raid1:%s: not clean -- starting background reconstruction\n",
3160 mdname(mddev));
3161 pr_info("md/raid1:%s: active with %d out of %d mirrors\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10003162 mdname(mddev), mddev->raid_disks - mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 mddev->raid_disks);
NeilBrown709ae482009-12-14 12:49:51 +11003164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 /*
3166 * Ok, everything is just fine now
3167 */
NeilBrown709ae482009-12-14 12:49:51 +11003168 mddev->thread = conf->thread;
3169 conf->thread = NULL;
3170 mddev->private = conf;
NeilBrown46533ff2016-11-18 16:16:11 +11003171 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
NeilBrown709ae482009-12-14 12:49:51 +11003172
Dan Williams1f403622009-03-31 14:59:03 +11003173 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Jonathan Brassow1ed72422011-06-07 17:50:35 -05003175 if (mddev->queue) {
Shaohua Li2ff8cc22012-10-11 13:28:54 +11003176 if (discard_supported)
3177 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3178 mddev->queue);
3179 else
3180 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3181 mddev->queue);
Jonathan Brassow1ed72422011-06-07 17:50:35 -05003182 }
majianpeng5220ea12012-04-02 09:48:38 +10003183
3184 ret = md_integrity_register(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003185 if (ret) {
3186 md_unregister_thread(&mddev->thread);
NeilBrownafa0f552014-12-15 12:56:58 +11003187 raid1_free(mddev, conf);
NeilBrown5aa61f42014-12-15 12:56:57 +11003188 }
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
Julia Lawall644df1a2015-09-13 14:15:10 +02003196 mempool_destroy(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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
NeilBrownfd01b882011-10-11 16:47:53 +11003207static int raid1_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
3209 /* no resync is happening, and there is enough space
3210 * on all devices, so we can resize.
3211 * We need to make sure resync covers any new space.
3212 * If the array is shrinking we should possibly wait until
3213 * any io in the removed space completes, but it hardly seems
3214 * worth it.
3215 */
NeilBrowna4a61252012-05-22 13:55:27 +10003216 sector_t newsize = raid1_size(mddev, sectors, 0);
3217 if (mddev->external_size &&
3218 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11003219 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10003220 if (mddev->bitmap) {
3221 int ret = bitmap_resize(mddev->bitmap, newsize, 0, 0);
3222 if (ret)
3223 return ret;
3224 }
3225 md_set_array_sectors(mddev, newsize);
Dan Williamsb522adc2009-03-31 15:00:31 +11003226 if (sectors > mddev->dev_sectors &&
NeilBrownb0986362011-05-11 15:52:21 +10003227 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003228 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3230 }
Dan Williamsb522adc2009-03-31 15:00:31 +11003231 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07003232 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 return 0;
3234}
3235
NeilBrownfd01b882011-10-11 16:47:53 +11003236static int raid1_reshape(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237{
3238 /* We need to:
3239 * 1/ resize the r1bio_pool
3240 * 2/ resize conf->mirrors
3241 *
3242 * We allocate a new r1bio_pool if we can.
3243 * Then raise a device barrier and wait until all IO stops.
3244 * Then resize conf->mirrors and swap in the new r1bio pool.
NeilBrown6ea9c072005-06-21 17:17:09 -07003245 *
3246 * At the same time, we "pack" the devices so that all the missing
3247 * devices have the higher raid_disk numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 */
3249 mempool_t *newpool, *oldpool;
3250 struct pool_info *newpoolinfo;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10003251 struct raid1_info *newmirrors;
NeilBrowne8096362011-10-11 16:49:05 +11003252 struct r1conf *conf = mddev->private;
NeilBrown63c70c42006-03-27 01:18:13 -08003253 int cnt, raid_disks;
NeilBrownc04be0a2006-10-03 01:15:53 -07003254 unsigned long flags;
Dan Williamsb5470dc2008-06-27 21:44:04 -07003255 int d, d2, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
NeilBrown63c70c42006-03-27 01:18:13 -08003257 /* Cannot change chunk_size, layout, or level */
Andre Noll664e7c42009-06-18 08:45:27 +10003258 if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
NeilBrown63c70c42006-03-27 01:18:13 -08003259 mddev->layout != mddev->new_layout ||
3260 mddev->level != mddev->new_level) {
Andre Noll664e7c42009-06-18 08:45:27 +10003261 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown63c70c42006-03-27 01:18:13 -08003262 mddev->new_layout = mddev->layout;
3263 mddev->new_level = mddev->level;
3264 return -EINVAL;
3265 }
3266
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003267 if (!mddev_is_clustered(mddev)) {
3268 err = md_allow_write(mddev);
3269 if (err)
3270 return err;
3271 }
NeilBrown2a2275d2007-01-26 00:57:11 -08003272
NeilBrown63c70c42006-03-27 01:18:13 -08003273 raid_disks = mddev->raid_disks + mddev->delta_disks;
3274
NeilBrown6ea9c072005-06-21 17:17:09 -07003275 if (raid_disks < conf->raid_disks) {
3276 cnt=0;
3277 for (d= 0; d < conf->raid_disks; d++)
3278 if (conf->mirrors[d].rdev)
3279 cnt++;
3280 if (cnt > raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 return -EBUSY;
NeilBrown6ea9c072005-06-21 17:17:09 -07003282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
3284 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
3285 if (!newpoolinfo)
3286 return -ENOMEM;
3287 newpoolinfo->mddev = mddev;
NeilBrown8f19ccb2011-12-23 10:17:56 +11003288 newpoolinfo->raid_disks = raid_disks * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
3290 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
3291 r1bio_pool_free, newpoolinfo);
3292 if (!newpool) {
3293 kfree(newpoolinfo);
3294 return -ENOMEM;
3295 }
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10003296 newmirrors = kzalloc(sizeof(struct raid1_info) * raid_disks * 2,
NeilBrown8f19ccb2011-12-23 10:17:56 +11003297 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 if (!newmirrors) {
3299 kfree(newpoolinfo);
3300 mempool_destroy(newpool);
3301 return -ENOMEM;
3302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
NeilBrowne2d59922013-06-12 11:01:22 +10003304 freeze_array(conf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
3306 /* ok, everything is stopped */
3307 oldpool = conf->r1bio_pool;
3308 conf->r1bio_pool = newpool;
NeilBrown6ea9c072005-06-21 17:17:09 -07003309
NeilBrowna88aa782007-08-22 14:01:53 -07003310 for (d = d2 = 0; d < conf->raid_disks; d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11003311 struct md_rdev *rdev = conf->mirrors[d].rdev;
NeilBrowna88aa782007-08-22 14:01:53 -07003312 if (rdev && rdev->raid_disk != d2) {
Namhyung Kim36fad852011-07-27 11:00:36 +10003313 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna88aa782007-08-22 14:01:53 -07003314 rdev->raid_disk = d2;
Namhyung Kim36fad852011-07-27 11:00:36 +10003315 sysfs_unlink_rdev(mddev, rdev);
3316 if (sysfs_link_rdev(mddev, rdev))
NeilBrown1d41c212016-11-02 14:16:50 +11003317 pr_warn("md/raid1:%s: cannot register rd%d\n",
3318 mdname(mddev), rdev->raid_disk);
NeilBrown6ea9c072005-06-21 17:17:09 -07003319 }
NeilBrowna88aa782007-08-22 14:01:53 -07003320 if (rdev)
3321 newmirrors[d2++].rdev = rdev;
3322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 kfree(conf->mirrors);
3324 conf->mirrors = newmirrors;
3325 kfree(conf->poolinfo);
3326 conf->poolinfo = newpoolinfo;
3327
NeilBrownc04be0a2006-10-03 01:15:53 -07003328 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 mddev->degraded += (raid_disks - conf->raid_disks);
NeilBrownc04be0a2006-10-03 01:15:53 -07003330 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 conf->raid_disks = mddev->raid_disks = raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08003332 mddev->delta_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
NeilBrowne2d59922013-06-12 11:01:22 +10003334 unfreeze_array(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
NeilBrown985ca972015-07-06 12:26:57 +10003336 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3338 md_wakeup_thread(mddev->thread);
3339
3340 mempool_destroy(oldpool);
3341 return 0;
3342}
3343
NeilBrownfd01b882011-10-11 16:47:53 +11003344static void raid1_quiesce(struct mddev *mddev, int state)
NeilBrown36fa3062005-09-09 16:23:45 -07003345{
NeilBrowne8096362011-10-11 16:49:05 +11003346 struct r1conf *conf = mddev->private;
NeilBrown36fa3062005-09-09 16:23:45 -07003347
3348 switch(state) {
NeilBrown6eef4b22009-12-14 12:49:51 +11003349 case 2: /* wake for suspend */
3350 wake_up(&conf->wait_barrier);
3351 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07003352 case 1:
majianpeng07169fd2013-11-14 15:16:18 +11003353 freeze_array(conf, 0);
NeilBrown36fa3062005-09-09 16:23:45 -07003354 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07003355 case 0:
majianpeng07169fd2013-11-14 15:16:18 +11003356 unfreeze_array(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07003357 break;
3358 }
NeilBrown36fa3062005-09-09 16:23:45 -07003359}
3360
NeilBrownfd01b882011-10-11 16:47:53 +11003361static void *raid1_takeover(struct mddev *mddev)
NeilBrown709ae482009-12-14 12:49:51 +11003362{
3363 /* raid1 can take over:
3364 * raid5 with 2 devices, any layout or chunk size
3365 */
3366 if (mddev->level == 5 && mddev->raid_disks == 2) {
NeilBrowne8096362011-10-11 16:49:05 +11003367 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11003368 mddev->new_level = 1;
3369 mddev->new_layout = 0;
3370 mddev->new_chunk_sectors = 0;
3371 conf = setup_conf(mddev);
Shaohua Li6995f0b2016-12-08 15:48:17 -08003372 if (!IS_ERR(conf)) {
majianpeng07169fd2013-11-14 15:16:18 +11003373 /* Array must appear to be quiesced */
3374 conf->array_frozen = 1;
Shaohua Li394ed8e2017-01-04 16:10:19 -08003375 mddev_clear_unsupported_flags(mddev,
3376 UNSUPPORTED_MDDEV_FLAGS);
Shaohua Li6995f0b2016-12-08 15:48:17 -08003377 }
NeilBrown709ae482009-12-14 12:49:51 +11003378 return conf;
3379 }
3380 return ERR_PTR(-EINVAL);
3381}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
NeilBrown84fc4b52011-10-11 16:49:58 +11003383static struct md_personality raid1_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384{
3385 .name = "raid1",
NeilBrown2604b702006-01-06 00:20:36 -08003386 .level = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08003388 .make_request = raid1_make_request,
3389 .run = raid1_run,
NeilBrownafa0f552014-12-15 12:56:58 +11003390 .free = raid1_free,
Shaohua Li849674e2016-01-20 13:52:20 -08003391 .status = raid1_status,
3392 .error_handler = raid1_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 .hot_add_disk = raid1_add_disk,
3394 .hot_remove_disk= raid1_remove_disk,
3395 .spare_active = raid1_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08003396 .sync_request = raid1_sync_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 .resize = raid1_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07003398 .size = raid1_size,
NeilBrown63c70c42006-03-27 01:18:13 -08003399 .check_reshape = raid1_reshape,
NeilBrown36fa3062005-09-09 16:23:45 -07003400 .quiesce = raid1_quiesce,
NeilBrown709ae482009-12-14 12:49:51 +11003401 .takeover = raid1_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11003402 .congested = raid1_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403};
3404
3405static int __init raid_init(void)
3406{
NeilBrown2604b702006-01-06 00:20:36 -08003407 return register_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408}
3409
3410static void raid_exit(void)
3411{
NeilBrown2604b702006-01-06 00:20:36 -08003412 unregister_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413}
3414
3415module_init(raid_init);
3416module_exit(raid_exit);
3417MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11003418MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419MODULE_ALIAS("md-personality-3"); /* RAID1 */
NeilBrownd9d166c2006-01-06 00:20:51 -08003420MODULE_ALIAS("md-raid1");
NeilBrown2604b702006-01-06 00:20:36 -08003421MODULE_ALIAS("md-level-1");
NeilBrown34db0cd2011-10-11 16:50:01 +11003422
3423module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);