blob: fefbbfdb440b4199ecd2a79fb90517df0693156c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid1.c : Multiple Devices driver for Linux
3 *
4 * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
5 *
6 * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
7 *
8 * RAID-1 management functions.
9 *
10 * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
11 *
Jan Engelhardt96de0e22007-10-19 23:21:04 +020012 * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
14 *
NeilBrown191ea9b2005-06-21 17:17:23 -070015 * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support
16 * bitmapped intelligence in resync:
17 *
18 * - bitmap marked during normal i/o
19 * - bitmap used to skip nondirty blocks during sync
20 *
21 * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
22 * - persistent bitmap code
23 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2, or (at your option)
27 * any later version.
28 *
29 * You should have received a copy of the GNU General Public License
30 * (for example /usr/src/linux/COPYING); if not, write to the Free
31 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 */
33
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110035#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040037#include <linux/module.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100039#include <linux/ratelimit.h>
NeilBrown109e3762016-11-18 13:22:04 +110040#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110041#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110042#include "raid1.h"
43#include "bitmap.h"
NeilBrown191ea9b2005-06-21 17:17:23 -070044
Shaohua Li394ed8e2017-01-04 16:10:19 -080045#define UNSUPPORTED_MDDEV_FLAGS \
46 ((1L << MD_HAS_JOURNAL) | \
47 (1L << MD_JOURNAL_CLEAN))
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
50 * Number of guaranteed r1bios in case of extreme VM load:
51 */
52#define NR_RAID1_BIOS 256
53
Jonathan Brassow473e87c2012-07-31 10:03:52 +100054/* when we get a read error on a read-only array, we redirect to another
55 * device without failing the first device, or trying to over-write to
56 * correct the read error. To keep track of bad blocks on a per-bio
57 * level, we store IO_BLOCKED in the appropriate 'bios' pointer
58 */
59#define IO_BLOCKED ((struct bio *)1)
60/* When we successfully write to a known bad-block, we need to remove the
61 * bad-block marking which must be done from process context. So we record
62 * the success by setting devs[n].bio to IO_MADE_GOOD
63 */
64#define IO_MADE_GOOD ((struct bio *)2)
65
66#define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
67
NeilBrown34db0cd2011-10-11 16:50:01 +110068/* When there are this many requests queue to be written by
69 * the raid1 thread, we become 'congested' to provide back-pressure
70 * for writeback.
71 */
72static int max_queued_requests = 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
colyli@suse.defd768632017-02-18 03:05:56 +080074static void allow_barrier(struct r1conf *conf, sector_t sector_nr);
75static void lower_barrier(struct r1conf *conf, sector_t sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
NeilBrown578b54a2016-11-14 16:30:21 +110077#define raid1_log(md, fmt, args...) \
78 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid1 " fmt, ##args); } while (0)
79
Al Virodd0fc662005-10-07 07:46:04 +010080static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 struct pool_info *pi = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +110083 int size = offsetof(struct r1bio, bios[pi->raid_disks]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 /* allocate a r1bio with room for raid_disks entries in the bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +010086 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89static void r1bio_pool_free(void *r1_bio, void *data)
90{
91 kfree(r1_bio);
92}
93
94#define RESYNC_BLOCK_SIZE (64*1024)
majianpeng8e005f72013-11-14 15:16:18 +110095#define RESYNC_DEPTH 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
97#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
majianpeng8e005f72013-11-14 15:16:18 +110098#define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH)
99#define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9)
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +1000100#define CLUSTER_RESYNC_WINDOW (16 * RESYNC_WINDOW)
101#define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Al Virodd0fc662005-10-07 07:46:04 +0100103static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 struct pool_info *pi = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100106 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct bio *bio;
NeilBrownda1aab32014-04-09 12:25:43 +1000108 int need_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int i, j;
110
111 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100112 if (!r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 /*
116 * Allocate bios : 1 for reading, n-1 for writing
117 */
118 for (j = pi->raid_disks ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100119 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (!bio)
121 goto out_free_bio;
122 r1_bio->bios[j] = bio;
123 }
124 /*
125 * Allocate RESYNC_PAGES data pages and attach them to
NeilBrownd11c1712006-01-06 00:20:26 -0800126 * the first bio.
127 * If this is a user-requested check/repair, allocate
128 * RESYNC_PAGES for each bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 */
NeilBrownd11c1712006-01-06 00:20:26 -0800130 if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
NeilBrownda1aab32014-04-09 12:25:43 +1000131 need_pages = pi->raid_disks;
NeilBrownd11c1712006-01-06 00:20:26 -0800132 else
NeilBrownda1aab32014-04-09 12:25:43 +1000133 need_pages = 1;
134 for (j = 0; j < need_pages; j++) {
NeilBrownd11c1712006-01-06 00:20:26 -0800135 bio = r1_bio->bios[j];
Kent Overstreeta0787602012-09-10 14:03:28 -0700136 bio->bi_vcnt = RESYNC_PAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Kent Overstreeta0787602012-09-10 14:03:28 -0700138 if (bio_alloc_pages(bio, gfp_flags))
NeilBrownda1aab32014-04-09 12:25:43 +1000139 goto out_free_pages;
NeilBrownd11c1712006-01-06 00:20:26 -0800140 }
141 /* If not user-requests, copy the page pointers to all bios */
142 if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) {
143 for (i=0; i<RESYNC_PAGES ; i++)
144 for (j=1; j<pi->raid_disks; j++)
145 r1_bio->bios[j]->bi_io_vec[i].bv_page =
146 r1_bio->bios[0]->bi_io_vec[i].bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148
149 r1_bio->master_bio = NULL;
150
151 return r1_bio;
152
NeilBrownda1aab32014-04-09 12:25:43 +1000153out_free_pages:
Guoqing Jiang491221f2016-09-22 03:10:01 -0400154 while (--j >= 0)
155 bio_free_pages(r1_bio->bios[j]);
NeilBrownda1aab32014-04-09 12:25:43 +1000156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157out_free_bio:
NeilBrown8f19ccb2011-12-23 10:17:56 +1100158 while (++j < pi->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 bio_put(r1_bio->bios[j]);
160 r1bio_pool_free(r1_bio, data);
161 return NULL;
162}
163
164static void r1buf_pool_free(void *__r1_bio, void *data)
165{
166 struct pool_info *pi = data;
NeilBrownd11c1712006-01-06 00:20:26 -0800167 int i,j;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100168 struct r1bio *r1bio = __r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
NeilBrownd11c1712006-01-06 00:20:26 -0800170 for (i = 0; i < RESYNC_PAGES; i++)
171 for (j = pi->raid_disks; j-- ;) {
172 if (j == 0 ||
173 r1bio->bios[j]->bi_io_vec[i].bv_page !=
174 r1bio->bios[0]->bi_io_vec[i].bv_page)
NeilBrown1345b1d2006-01-06 00:20:40 -0800175 safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page);
NeilBrownd11c1712006-01-06 00:20:26 -0800176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 for (i=0 ; i < pi->raid_disks; i++)
178 bio_put(r1bio->bios[i]);
179
180 r1bio_pool_free(r1bio, data);
181}
182
NeilBrowne8096362011-10-11 16:49:05 +1100183static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int i;
186
NeilBrown8f19ccb2011-12-23 10:17:56 +1100187 for (i = 0; i < conf->raid_disks * 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct bio **bio = r1_bio->bios + i;
NeilBrown4367af52011-07-28 11:31:49 +1000189 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 bio_put(*bio);
191 *bio = NULL;
192 }
193}
194
NeilBrown9f2c9d12011-10-11 16:48:43 +1100195static void free_r1bio(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
NeilBrowne8096362011-10-11 16:49:05 +1100197 struct r1conf *conf = r1_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 put_all_bios(conf, r1_bio);
200 mempool_free(r1_bio, conf->r1bio_pool);
201}
202
NeilBrown9f2c9d12011-10-11 16:48:43 +1100203static void put_buf(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
NeilBrowne8096362011-10-11 16:49:05 +1100205 struct r1conf *conf = r1_bio->mddev->private;
NeilBrown3e198f72006-01-06 00:20:21 -0800206 int i;
207
NeilBrown8f19ccb2011-12-23 10:17:56 +1100208 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown3e198f72006-01-06 00:20:21 -0800209 struct bio *bio = r1_bio->bios[i];
210 if (bio->bi_end_io)
211 rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 mempool_free(r1_bio, conf->r1buf_pool);
215
colyli@suse.defd768632017-02-18 03:05:56 +0800216 lower_barrier(conf, r1_bio->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
NeilBrown9f2c9d12011-10-11 16:48:43 +1100219static void reschedule_retry(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100222 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +1100223 struct r1conf *conf = mddev->private;
colyli@suse.defd768632017-02-18 03:05:56 +0800224 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
colyli@suse.defd768632017-02-18 03:05:56 +0800226 idx = sector_to_idx(r1_bio->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 spin_lock_irqsave(&conf->device_lock, flags);
228 list_add(&r1_bio->retry_list, &conf->retry_list);
colyli@suse.de824e47d2017-02-18 03:05:57 +0800229 atomic_inc(&conf->nr_queued[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 spin_unlock_irqrestore(&conf->device_lock, flags);
231
NeilBrown17999be2006-01-06 00:20:12 -0800232 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 md_wakeup_thread(mddev->thread);
234}
235
236/*
237 * raid_end_bio_io() is called when we have finished servicing a mirrored
238 * operation and are ready to return a success/failure code to the buffer
239 * cache layer.
240 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100241static void call_bio_endio(struct r1bio *r1_bio)
NeilBrownd2eb35a2011-07-28 11:31:48 +1000242{
243 struct bio *bio = r1_bio->master_bio;
244 int done;
NeilBrowne8096362011-10-11 16:49:05 +1100245 struct r1conf *conf = r1_bio->mddev->private;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700246 sector_t bi_sector = bio->bi_iter.bi_sector;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000247
248 if (bio->bi_phys_segments) {
249 unsigned long flags;
250 spin_lock_irqsave(&conf->device_lock, flags);
251 bio->bi_phys_segments--;
252 done = (bio->bi_phys_segments == 0);
253 spin_unlock_irqrestore(&conf->device_lock, flags);
majianpeng79ef3a82013-11-15 14:55:02 +0800254 /*
255 * make_request() might be waiting for
256 * bi_phys_segments to decrease
257 */
258 wake_up(&conf->wait_barrier);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000259 } else
260 done = 1;
261
262 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200263 bio->bi_error = -EIO;
264
NeilBrownd2eb35a2011-07-28 11:31:48 +1000265 if (done) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200266 bio_endio(bio);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000267 /*
268 * Wake up any possible resync thread that waits for the device
269 * to go idle.
270 */
colyli@suse.defd768632017-02-18 03:05:56 +0800271 allow_barrier(conf, bi_sector);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000272 }
273}
274
NeilBrown9f2c9d12011-10-11 16:48:43 +1100275static void raid_end_bio_io(struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct bio *bio = r1_bio->master_bio;
278
NeilBrown4b6d2872005-09-09 16:23:47 -0700279 /* if nobody has done the final endio yet, do it now */
280 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
NeilBrown36a4e1f2011-10-07 14:23:17 +1100281 pr_debug("raid1: sync end %s on sectors %llu-%llu\n",
282 (bio_data_dir(bio) == WRITE) ? "write" : "read",
Kent Overstreet4f024f32013-10-11 15:44:27 -0700283 (unsigned long long) bio->bi_iter.bi_sector,
284 (unsigned long long) bio_end_sector(bio) - 1);
NeilBrown4b6d2872005-09-09 16:23:47 -0700285
NeilBrownd2eb35a2011-07-28 11:31:48 +1000286 call_bio_endio(r1_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -0700287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 free_r1bio(r1_bio);
289}
290
291/*
292 * Update disk head position estimator based on IRQ completion info.
293 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100294static inline void update_head_pos(int disk, struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
NeilBrowne8096362011-10-11 16:49:05 +1100296 struct r1conf *conf = r1_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 conf->mirrors[disk].head_position =
299 r1_bio->sector + (r1_bio->sectors);
300}
301
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100302/*
303 * Find the disk number which triggered given bio
304 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100305static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio)
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100306{
307 int mirror;
NeilBrown30194632011-12-23 10:17:56 +1100308 struct r1conf *conf = r1_bio->mddev->private;
309 int raid_disks = conf->raid_disks;
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100310
NeilBrown8f19ccb2011-12-23 10:17:56 +1100311 for (mirror = 0; mirror < raid_disks * 2; mirror++)
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100312 if (r1_bio->bios[mirror] == bio)
313 break;
314
NeilBrown8f19ccb2011-12-23 10:17:56 +1100315 BUG_ON(mirror == raid_disks * 2);
Namhyung Kimba3ae3b2011-10-07 14:22:53 +1100316 update_head_pos(mirror, r1_bio);
317
318 return mirror;
319}
320
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200321static void raid1_end_read_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200323 int uptodate = !bio->bi_error;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100324 struct r1bio *r1_bio = bio->bi_private;
NeilBrowne8096362011-10-11 16:49:05 +1100325 struct r1conf *conf = r1_bio->mddev->private;
NeilBrowne5872d52016-06-02 16:19:52 +1000326 struct md_rdev *rdev = conf->mirrors[r1_bio->read_disk].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /*
329 * this branch is our 'one mirror IO has finished' event handler:
330 */
NeilBrowne5872d52016-06-02 16:19:52 +1000331 update_head_pos(r1_bio->read_disk, r1_bio);
NeilBrownddaf22a2006-01-06 00:20:19 -0800332
NeilBrowndd00a992007-05-10 03:15:50 -0700333 if (uptodate)
334 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrown2e52d442016-11-18 16:16:12 +1100335 else if (test_bit(FailFast, &rdev->flags) &&
336 test_bit(R1BIO_FailFast, &r1_bio->state))
337 /* This was a fail-fast read so we definitely
338 * want to retry */
339 ;
NeilBrowndd00a992007-05-10 03:15:50 -0700340 else {
341 /* If all other devices have failed, we want to return
342 * the error upwards rather than fail the last device.
343 * Here we redefine "uptodate" to mean "Don't want to retry"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 */
NeilBrowndd00a992007-05-10 03:15:50 -0700345 unsigned long flags;
346 spin_lock_irqsave(&conf->device_lock, flags);
347 if (r1_bio->mddev->degraded == conf->raid_disks ||
348 (r1_bio->mddev->degraded == conf->raid_disks-1 &&
NeilBrowne5872d52016-06-02 16:19:52 +1000349 test_bit(In_sync, &rdev->flags)))
NeilBrowndd00a992007-05-10 03:15:50 -0700350 uptodate = 1;
351 spin_unlock_irqrestore(&conf->device_lock, flags);
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
NeilBrown7ad4d4a2012-10-11 13:44:30 +1100354 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 raid_end_bio_io(r1_bio);
NeilBrowne5872d52016-06-02 16:19:52 +1000356 rdev_dec_pending(rdev, conf->mddev);
NeilBrown7ad4d4a2012-10-11 13:44:30 +1100357 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /*
359 * oops, read error:
360 */
361 char b[BDEVNAME_SIZE];
NeilBrown1d41c212016-11-02 14:16:50 +1100362 pr_err_ratelimited("md/raid1:%s: %s: rescheduling sector %llu\n",
363 mdname(conf->mddev),
364 bdevname(rdev->bdev, b),
365 (unsigned long long)r1_bio->sector);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000366 set_bit(R1BIO_ReadError, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 reschedule_retry(r1_bio);
NeilBrown7ad4d4a2012-10-11 13:44:30 +1100368 /* don't drop the reference on read_disk yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
NeilBrown9f2c9d12011-10-11 16:48:43 +1100372static void close_write(struct r1bio *r1_bio)
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000373{
374 /* it really is the end of this request */
375 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
376 /* free extra copy of the data pages */
377 int i = r1_bio->behind_page_count;
378 while (i--)
379 safe_put_page(r1_bio->behind_bvecs[i].bv_page);
380 kfree(r1_bio->behind_bvecs);
381 r1_bio->behind_bvecs = NULL;
382 }
383 /* clear the bitmap if all writes complete successfully */
384 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
385 r1_bio->sectors,
386 !test_bit(R1BIO_Degraded, &r1_bio->state),
387 test_bit(R1BIO_BehindIO, &r1_bio->state));
388 md_write_end(r1_bio->mddev);
389}
390
NeilBrown9f2c9d12011-10-11 16:48:43 +1100391static void r1_bio_write_done(struct r1bio *r1_bio)
NeilBrown4e780642010-10-19 12:54:01 +1100392{
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000393 if (!atomic_dec_and_test(&r1_bio->remaining))
394 return;
395
396 if (test_bit(R1BIO_WriteError, &r1_bio->state))
397 reschedule_retry(r1_bio);
398 else {
399 close_write(r1_bio);
NeilBrown4367af52011-07-28 11:31:49 +1000400 if (test_bit(R1BIO_MadeGood, &r1_bio->state))
401 reschedule_retry(r1_bio);
402 else
403 raid_end_bio_io(r1_bio);
NeilBrown4e780642010-10-19 12:54:01 +1100404 }
405}
406
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200407static void raid1_end_write_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
NeilBrown9f2c9d12011-10-11 16:48:43 +1100409 struct r1bio *r1_bio = bio->bi_private;
NeilBrowne5872d52016-06-02 16:19:52 +1000410 int behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
NeilBrowne8096362011-10-11 16:49:05 +1100411 struct r1conf *conf = r1_bio->mddev->private;
NeilBrown04b857f2006-03-09 17:33:46 -0800412 struct bio *to_put = NULL;
NeilBrowne5872d52016-06-02 16:19:52 +1000413 int mirror = find_bio_disk(r1_bio, bio);
414 struct md_rdev *rdev = conf->mirrors[mirror].rdev;
Shaohua Lie3f948c2016-10-06 14:09:16 -0700415 bool discard_error;
416
417 discard_error = bio->bi_error && bio_op(bio) == REQ_OP_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Tejun Heoe9c74692010-09-03 11:56:18 +0200419 /*
420 * 'one mirror IO has finished' event handler:
421 */
Shaohua Lie3f948c2016-10-06 14:09:16 -0700422 if (bio->bi_error && !discard_error) {
NeilBrowne5872d52016-06-02 16:19:52 +1000423 set_bit(WriteErrorSeen, &rdev->flags);
424 if (!test_and_set_bit(WantReplacement, &rdev->flags))
NeilBrown19d67162011-12-23 10:17:57 +1100425 set_bit(MD_RECOVERY_NEEDED, &
426 conf->mddev->recovery);
427
NeilBrown212e7eb2016-11-18 16:16:12 +1100428 if (test_bit(FailFast, &rdev->flags) &&
429 (bio->bi_opf & MD_FAILFAST) &&
430 /* We never try FailFast to WriteMostly devices */
431 !test_bit(WriteMostly, &rdev->flags)) {
432 md_error(r1_bio->mddev, rdev);
433 if (!test_bit(Faulty, &rdev->flags))
434 /* This is the only remaining device,
435 * We need to retry the write without
436 * FailFast
437 */
438 set_bit(R1BIO_WriteError, &r1_bio->state);
439 else {
440 /* Finished with this branch */
441 r1_bio->bios[mirror] = NULL;
442 to_put = bio;
443 }
444 } else
445 set_bit(R1BIO_WriteError, &r1_bio->state);
NeilBrown4367af52011-07-28 11:31:49 +1000446 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /*
Tejun Heoe9c74692010-09-03 11:56:18 +0200448 * Set R1BIO_Uptodate in our master bio, so that we
449 * will return a good error code for to the higher
450 * levels even if IO on some other mirrored buffer
451 * fails.
452 *
453 * The 'master' represents the composite IO operation
454 * to user-side. So if something waits for IO, then it
455 * will wait for the 'master' bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 */
NeilBrown4367af52011-07-28 11:31:49 +1000457 sector_t first_bad;
458 int bad_sectors;
459
NeilBrowncd5ff9a12011-07-28 11:32:41 +1000460 r1_bio->bios[mirror] = NULL;
461 to_put = bio;
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300462 /*
463 * Do not set R1BIO_Uptodate if the current device is
464 * rebuilding or Faulty. This is because we cannot use
465 * such device for properly reading the data back (we could
466 * potentially use it, if the current write would have felt
467 * before rdev->recovery_offset, but for simplicity we don't
468 * check this here.
469 */
NeilBrowne5872d52016-06-02 16:19:52 +1000470 if (test_bit(In_sync, &rdev->flags) &&
471 !test_bit(Faulty, &rdev->flags))
Alex Lyakas3056e3a2013-06-04 20:42:21 +0300472 set_bit(R1BIO_Uptodate, &r1_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
NeilBrown4367af52011-07-28 11:31:49 +1000474 /* Maybe we can clear some bad blocks. */
NeilBrowne5872d52016-06-02 16:19:52 +1000475 if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
Shaohua Lie3f948c2016-10-06 14:09:16 -0700476 &first_bad, &bad_sectors) && !discard_error) {
NeilBrown4367af52011-07-28 11:31:49 +1000477 r1_bio->bios[mirror] = IO_MADE_GOOD;
478 set_bit(R1BIO_MadeGood, &r1_bio->state);
479 }
480 }
481
Tejun Heoe9c74692010-09-03 11:56:18 +0200482 if (behind) {
NeilBrowne5872d52016-06-02 16:19:52 +1000483 if (test_bit(WriteMostly, &rdev->flags))
Tejun Heoe9c74692010-09-03 11:56:18 +0200484 atomic_dec(&r1_bio->behind_remaining);
NeilBrown4b6d2872005-09-09 16:23:47 -0700485
Tejun Heoe9c74692010-09-03 11:56:18 +0200486 /*
487 * In behind mode, we ACK the master bio once the I/O
488 * has safely reached all non-writemostly
489 * disks. Setting the Returned bit ensures that this
490 * gets done only once -- we don't ever want to return
491 * -EIO here, instead we'll wait
492 */
493 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
494 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
495 /* Maybe we can return now */
496 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
497 struct bio *mbio = r1_bio->master_bio;
NeilBrown36a4e1f2011-10-07 14:23:17 +1100498 pr_debug("raid1: behind end write sectors"
499 " %llu-%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -0700500 (unsigned long long) mbio->bi_iter.bi_sector,
501 (unsigned long long) bio_end_sector(mbio) - 1);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000502 call_bio_endio(r1_bio);
NeilBrown4b6d2872005-09-09 16:23:47 -0700503 }
504 }
505 }
NeilBrown4367af52011-07-28 11:31:49 +1000506 if (r1_bio->bios[mirror] == NULL)
NeilBrowne5872d52016-06-02 16:19:52 +1000507 rdev_dec_pending(rdev, conf->mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 * Let's see if all mirrored write operations have finished
511 * already.
512 */
NeilBrownaf6d7b72011-05-11 14:51:19 +1000513 r1_bio_write_done(r1_bio);
NeilBrownc70810b2006-06-26 00:27:35 -0700514
NeilBrown04b857f2006-03-09 17:33:46 -0800515 if (to_put)
516 bio_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
colyli@suse.defd768632017-02-18 03:05:56 +0800519static sector_t align_to_barrier_unit_end(sector_t start_sector,
520 sector_t sectors)
521{
522 sector_t len;
523
524 WARN_ON(sectors == 0);
525 /*
526 * len is the number of sectors from start_sector to end of the
527 * barrier unit which start_sector belongs to.
528 */
529 len = round_up(start_sector + 1, BARRIER_UNIT_SECTOR_SIZE) -
530 start_sector;
531
532 if (len > sectors)
533 len = sectors;
534
535 return len;
536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538/*
539 * This routine returns the disk from which the requested read should
540 * be done. There is a per-array 'next expected sequential IO' sector
541 * number - if this matches on the next IO then we use the last disk.
542 * There is also a per-disk 'last know head position' sector that is
543 * maintained from IRQ contexts, both the normal and the resync IO
544 * completion handlers update this position correctly. If there is no
545 * perfect sequential match then we pick the disk whose head is closest.
546 *
547 * If there are 2 mirrors in the same 2 devices, performance degrades
548 * because position is mirror, not device based.
549 *
550 * The rdev for the device selected will have nr_pending incremented.
551 */
NeilBrowne8096362011-10-11 16:49:05 +1100552static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000554 const sector_t this_sector = r1_bio->sector;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000555 int sectors;
556 int best_good_sectors;
Shaohua Li9dedf602012-07-31 10:03:53 +1000557 int best_disk, best_dist_disk, best_pending_disk;
558 int has_nonrot_disk;
Shaohua Libe4d3282012-07-31 10:03:53 +1000559 int disk;
NeilBrown76073052011-05-11 14:34:56 +1000560 sector_t best_dist;
Shaohua Li9dedf602012-07-31 10:03:53 +1000561 unsigned int min_pending;
NeilBrown3cb03002011-10-11 16:45:26 +1100562 struct md_rdev *rdev;
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000563 int choose_first;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000564 int choose_next_idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 rcu_read_lock();
567 /*
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700568 * Check if we can balance. We can balance on the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 * device if no resync is going on, or below the resync window.
570 * We take the first readable disk when above the resync window.
571 */
572 retry:
NeilBrownd2eb35a2011-07-28 11:31:48 +1000573 sectors = r1_bio->sectors;
NeilBrown76073052011-05-11 14:34:56 +1000574 best_disk = -1;
Shaohua Li9dedf602012-07-31 10:03:53 +1000575 best_dist_disk = -1;
NeilBrown76073052011-05-11 14:34:56 +1000576 best_dist = MaxSector;
Shaohua Li9dedf602012-07-31 10:03:53 +1000577 best_pending_disk = -1;
578 min_pending = UINT_MAX;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000579 best_good_sectors = 0;
Shaohua Li9dedf602012-07-31 10:03:53 +1000580 has_nonrot_disk = 0;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000581 choose_next_idle = 0;
NeilBrown2e52d442016-11-18 16:16:12 +1100582 clear_bit(R1BIO_FailFast, &r1_bio->state);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000583
Goldwyn Rodrigues7d49ffc2014-08-12 10:13:19 -0500584 if ((conf->mddev->recovery_cp < this_sector + sectors) ||
585 (mddev_is_clustered(conf->mddev) &&
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -0500586 md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
Goldwyn Rodrigues7d49ffc2014-08-12 10:13:19 -0500587 this_sector + sectors)))
588 choose_first = 1;
589 else
590 choose_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Shaohua Libe4d3282012-07-31 10:03:53 +1000592 for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
NeilBrown76073052011-05-11 14:34:56 +1000593 sector_t dist;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000594 sector_t first_bad;
595 int bad_sectors;
Shaohua Li9dedf602012-07-31 10:03:53 +1000596 unsigned int pending;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000597 bool nonrot;
NeilBrownd2eb35a2011-07-28 11:31:48 +1000598
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000599 rdev = rcu_dereference(conf->mirrors[disk].rdev);
600 if (r1_bio->bios[disk] == IO_BLOCKED
601 || rdev == NULL
NeilBrown76073052011-05-11 14:34:56 +1000602 || test_bit(Faulty, &rdev->flags))
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000603 continue;
NeilBrown76073052011-05-11 14:34:56 +1000604 if (!test_bit(In_sync, &rdev->flags) &&
605 rdev->recovery_offset < this_sector + sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 continue;
NeilBrown76073052011-05-11 14:34:56 +1000607 if (test_bit(WriteMostly, &rdev->flags)) {
608 /* Don't balance among write-mostly, just
609 * use the first as a last resort */
Tomáš Hodekd1901ef2015-02-23 11:00:38 +1100610 if (best_dist_disk < 0) {
NeilBrown307729c2012-01-09 01:41:51 +1100611 if (is_badblock(rdev, this_sector, sectors,
612 &first_bad, &bad_sectors)) {
Wei Fang816b0ac2016-03-21 19:18:32 +0800613 if (first_bad <= this_sector)
NeilBrown307729c2012-01-09 01:41:51 +1100614 /* Cannot use this */
615 continue;
616 best_good_sectors = first_bad - this_sector;
617 } else
618 best_good_sectors = sectors;
Tomáš Hodekd1901ef2015-02-23 11:00:38 +1100619 best_dist_disk = disk;
620 best_pending_disk = disk;
NeilBrown307729c2012-01-09 01:41:51 +1100621 }
NeilBrown76073052011-05-11 14:34:56 +1000622 continue;
623 }
624 /* This is a reasonable device to use. It might
625 * even be best.
626 */
NeilBrownd2eb35a2011-07-28 11:31:48 +1000627 if (is_badblock(rdev, this_sector, sectors,
628 &first_bad, &bad_sectors)) {
629 if (best_dist < MaxSector)
630 /* already have a better device */
631 continue;
632 if (first_bad <= this_sector) {
633 /* cannot read here. If this is the 'primary'
634 * device, then we must not read beyond
635 * bad_sectors from another device..
636 */
637 bad_sectors -= (this_sector - first_bad);
638 if (choose_first && sectors > bad_sectors)
639 sectors = bad_sectors;
640 if (best_good_sectors > sectors)
641 best_good_sectors = sectors;
642
643 } else {
644 sector_t good_sectors = first_bad - this_sector;
645 if (good_sectors > best_good_sectors) {
646 best_good_sectors = good_sectors;
647 best_disk = disk;
648 }
649 if (choose_first)
650 break;
651 }
652 continue;
653 } else
654 best_good_sectors = sectors;
655
NeilBrown2e52d442016-11-18 16:16:12 +1100656 if (best_disk >= 0)
657 /* At least two disks to choose from so failfast is OK */
658 set_bit(R1BIO_FailFast, &r1_bio->state);
659
Shaohua Li12cee5a2012-07-31 10:03:53 +1000660 nonrot = blk_queue_nonrot(bdev_get_queue(rdev->bdev));
661 has_nonrot_disk |= nonrot;
Shaohua Li9dedf602012-07-31 10:03:53 +1000662 pending = atomic_read(&rdev->nr_pending);
NeilBrown76073052011-05-11 14:34:56 +1000663 dist = abs(this_sector - conf->mirrors[disk].head_position);
Shaohua Li12cee5a2012-07-31 10:03:53 +1000664 if (choose_first) {
NeilBrown76073052011-05-11 14:34:56 +1000665 best_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 break;
667 }
Shaohua Li12cee5a2012-07-31 10:03:53 +1000668 /* Don't change to another disk for sequential reads */
669 if (conf->mirrors[disk].next_seq_sect == this_sector
670 || dist == 0) {
671 int opt_iosize = bdev_io_opt(rdev->bdev) >> 9;
672 struct raid1_info *mirror = &conf->mirrors[disk];
673
674 best_disk = disk;
675 /*
676 * If buffered sequential IO size exceeds optimal
677 * iosize, check if there is idle disk. If yes, choose
678 * the idle disk. read_balance could already choose an
679 * idle disk before noticing it's a sequential IO in
680 * this disk. This doesn't matter because this disk
681 * will idle, next time it will be utilized after the
682 * first disk has IO size exceeds optimal iosize. In
683 * this way, iosize of the first disk will be optimal
684 * iosize at least. iosize of the second disk might be
685 * small, but not a big deal since when the second disk
686 * starts IO, the first disk is likely still busy.
687 */
688 if (nonrot && opt_iosize > 0 &&
689 mirror->seq_start != MaxSector &&
690 mirror->next_seq_sect > opt_iosize &&
691 mirror->next_seq_sect - opt_iosize >=
692 mirror->seq_start) {
693 choose_next_idle = 1;
694 continue;
695 }
696 break;
697 }
Shaohua Li12cee5a2012-07-31 10:03:53 +1000698
699 if (choose_next_idle)
700 continue;
Shaohua Li9dedf602012-07-31 10:03:53 +1000701
702 if (min_pending > pending) {
703 min_pending = pending;
704 best_pending_disk = disk;
705 }
706
NeilBrown76073052011-05-11 14:34:56 +1000707 if (dist < best_dist) {
708 best_dist = dist;
Shaohua Li9dedf602012-07-31 10:03:53 +1000709 best_dist_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
NeilBrownf3ac8bf2010-09-06 14:10:08 +1000711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Shaohua Li9dedf602012-07-31 10:03:53 +1000713 /*
714 * If all disks are rotational, choose the closest disk. If any disk is
715 * non-rotational, choose the disk with less pending request even the
716 * disk is rotational, which might/might not be optimal for raids with
717 * mixed ratation/non-rotational disks depending on workload.
718 */
719 if (best_disk == -1) {
NeilBrown2e52d442016-11-18 16:16:12 +1100720 if (has_nonrot_disk || min_pending == 0)
Shaohua Li9dedf602012-07-31 10:03:53 +1000721 best_disk = best_pending_disk;
722 else
723 best_disk = best_dist_disk;
724 }
725
NeilBrown76073052011-05-11 14:34:56 +1000726 if (best_disk >= 0) {
727 rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700728 if (!rdev)
729 goto retry;
730 atomic_inc(&rdev->nr_pending);
NeilBrownd2eb35a2011-07-28 11:31:48 +1000731 sectors = best_good_sectors;
Shaohua Li12cee5a2012-07-31 10:03:53 +1000732
733 if (conf->mirrors[best_disk].next_seq_sect != this_sector)
734 conf->mirrors[best_disk].seq_start = this_sector;
735
Shaohua Libe4d3282012-07-31 10:03:53 +1000736 conf->mirrors[best_disk].next_seq_sect = this_sector + sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738 rcu_read_unlock();
NeilBrownd2eb35a2011-07-28 11:31:48 +1000739 *max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
NeilBrown76073052011-05-11 14:34:56 +1000741 return best_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
NeilBrown5c675f82014-12-15 12:56:56 +1100744static int raid1_congested(struct mddev *mddev, int bits)
NeilBrown0d129222006-10-03 01:15:54 -0700745{
NeilBrowne8096362011-10-11 16:49:05 +1100746 struct r1conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700747 int i, ret = 0;
748
Tejun Heo44522262015-05-22 17:13:26 -0400749 if ((bits & (1 << WB_async_congested)) &&
NeilBrown34db0cd2011-10-11 16:50:01 +1100750 conf->pending_count >= max_queued_requests)
751 return 1;
752
NeilBrown0d129222006-10-03 01:15:54 -0700753 rcu_read_lock();
NeilBrownf53e29f2012-02-13 14:24:05 +1100754 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100755 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700756 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200757 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700758
Jonathan Brassow1ed72422011-06-07 17:50:35 -0500759 BUG_ON(!q);
760
NeilBrown0d129222006-10-03 01:15:54 -0700761 /* Note the '|| 1' - when read_balance prefers
762 * non-congested targets, it can be removed
763 */
Tejun Heo44522262015-05-22 17:13:26 -0400764 if ((bits & (1 << WB_async_congested)) || 1)
NeilBrown0d129222006-10-03 01:15:54 -0700765 ret |= bdi_congested(&q->backing_dev_info, bits);
766 else
767 ret &= bdi_congested(&q->backing_dev_info, bits);
768 }
769 }
770 rcu_read_unlock();
771 return ret;
772}
NeilBrown0d129222006-10-03 01:15:54 -0700773
NeilBrowne8096362011-10-11 16:49:05 +1100774static void flush_pending_writes(struct r1conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800775{
776 /* Any writes that have been queued but are awaiting
777 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800778 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800779 spin_lock_irq(&conf->device_lock);
780
781 if (conf->pending_bio_list.head) {
782 struct bio *bio;
783 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100784 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800785 spin_unlock_irq(&conf->device_lock);
786 /* flush any pending bitmap writes to
787 * disk before proceeding w/ I/O */
788 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100789 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800790
791 while (bio) { /* submit pending writes */
792 struct bio *next = bio->bi_next;
NeilBrown5e2c7a32016-11-04 16:46:03 +1100793 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrowna35e63e2008-03-04 14:29:29 -0800794 bio->bi_next = NULL;
NeilBrown5e2c7a32016-11-04 16:46:03 +1100795 bio->bi_bdev = rdev->bdev;
796 if (test_bit(Faulty, &rdev->flags)) {
797 bio->bi_error = -EIO;
798 bio_endio(bio);
799 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
800 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li2ff8cc22012-10-11 13:28:54 +1100801 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200802 bio_endio(bio);
Shaohua Li2ff8cc22012-10-11 13:28:54 +1100803 else
804 generic_make_request(bio);
NeilBrowna35e63e2008-03-04 14:29:29 -0800805 bio = next;
806 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800807 } else
808 spin_unlock_irq(&conf->device_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100809}
810
NeilBrown17999be2006-01-06 00:20:12 -0800811/* Barriers....
812 * Sometimes we need to suspend IO while we do something else,
813 * either some resync/recovery, or reconfigure the array.
814 * To do this we raise a 'barrier'.
815 * The 'barrier' is a counter that can be raised multiple times
816 * to count how many activities are happening which preclude
817 * normal IO.
818 * We can only raise the barrier if there is no pending IO.
819 * i.e. if nr_pending == 0.
820 * We choose only to raise the barrier if no-one is waiting for the
821 * barrier to go down. This means that as soon as an IO request
822 * is ready, no other operations which require a barrier will start
823 * until the IO request has had a chance.
824 *
825 * So: regular IO calls 'wait_barrier'. When that returns there
826 * is no backgroup IO happening, It must arrange to call
827 * allow_barrier when it has finished its IO.
828 * backgroup IO calls must call raise_barrier. Once that returns
829 * there is no normal IO happeing. It must arrange to call
830 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 */
NeilBrownc2fd4c92014-09-10 16:01:24 +1000832static void raise_barrier(struct r1conf *conf, sector_t sector_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
colyli@suse.defd768632017-02-18 03:05:56 +0800834 int idx = sector_to_idx(sector_nr);
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 spin_lock_irq(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800837
838 /* Wait until no block IO is waiting */
colyli@suse.de824e47d2017-02-18 03:05:57 +0800839 wait_event_lock_irq(conf->wait_barrier,
840 !atomic_read(&conf->nr_waiting[idx]),
Lukas Czernereed8c022012-11-30 11:42:40 +0100841 conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800842
843 /* block any new IO from starting */
colyli@suse.de824e47d2017-02-18 03:05:57 +0800844 atomic_inc(&conf->barrier[idx]);
845 /*
846 * In raise_barrier() we firstly increase conf->barrier[idx] then
847 * check conf->nr_pending[idx]. In _wait_barrier() we firstly
848 * increase conf->nr_pending[idx] then check conf->barrier[idx].
849 * A memory barrier here to make sure conf->nr_pending[idx] won't
850 * be fetched before conf->barrier[idx] is increased. Otherwise
851 * there will be a race between raise_barrier() and _wait_barrier().
852 */
853 smp_mb__after_atomic();
NeilBrown17999be2006-01-06 00:20:12 -0800854
majianpeng79ef3a82013-11-15 14:55:02 +0800855 /* For these conditions we must wait:
856 * A: while the array is in frozen state
colyli@suse.defd768632017-02-18 03:05:56 +0800857 * B: while conf->nr_pending[idx] is not 0, meaning regular I/O
858 * existing in corresponding I/O barrier bucket.
859 * C: while conf->barrier[idx] >= RESYNC_DEPTH, meaning reaches
860 * max resync count which allowed on current I/O barrier bucket.
majianpeng79ef3a82013-11-15 14:55:02 +0800861 */
NeilBrown17999be2006-01-06 00:20:12 -0800862 wait_event_lock_irq(conf->wait_barrier,
majianpengb364e3d2013-11-14 15:16:18 +1100863 !conf->array_frozen &&
colyli@suse.de824e47d2017-02-18 03:05:57 +0800864 !atomic_read(&conf->nr_pending[idx]) &&
865 atomic_read(&conf->barrier[idx]) < RESYNC_DEPTH,
Lukas Czernereed8c022012-11-30 11:42:40 +0100866 conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -0800867
colyli@suse.de824e47d2017-02-18 03:05:57 +0800868 atomic_inc(&conf->nr_pending[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 spin_unlock_irq(&conf->resync_lock);
870}
871
colyli@suse.defd768632017-02-18 03:05:56 +0800872static void lower_barrier(struct r1conf *conf, sector_t sector_nr)
NeilBrown17999be2006-01-06 00:20:12 -0800873{
colyli@suse.defd768632017-02-18 03:05:56 +0800874 int idx = sector_to_idx(sector_nr);
875
colyli@suse.de824e47d2017-02-18 03:05:57 +0800876 BUG_ON(atomic_read(&conf->barrier[idx]) <= 0);
colyli@suse.defd768632017-02-18 03:05:56 +0800877
colyli@suse.de824e47d2017-02-18 03:05:57 +0800878 atomic_dec(&conf->barrier[idx]);
879 atomic_dec(&conf->nr_pending[idx]);
NeilBrown17999be2006-01-06 00:20:12 -0800880 wake_up(&conf->wait_barrier);
881}
882
colyli@suse.defd768632017-02-18 03:05:56 +0800883static void _wait_barrier(struct r1conf *conf, int idx)
NeilBrown17999be2006-01-06 00:20:12 -0800884{
colyli@suse.de824e47d2017-02-18 03:05:57 +0800885 /*
886 * We need to increase conf->nr_pending[idx] very early here,
887 * then raise_barrier() can be blocked when it waits for
888 * conf->nr_pending[idx] to be 0. Then we can avoid holding
889 * conf->resync_lock when there is no barrier raised in same
890 * barrier unit bucket. Also if the array is frozen, I/O
891 * should be blocked until array is unfrozen.
892 */
893 atomic_inc(&conf->nr_pending[idx]);
894 /*
895 * In _wait_barrier() we firstly increase conf->nr_pending[idx], then
896 * check conf->barrier[idx]. In raise_barrier() we firstly increase
897 * conf->barrier[idx], then check conf->nr_pending[idx]. A memory
898 * barrier is necessary here to make sure conf->barrier[idx] won't be
899 * fetched before conf->nr_pending[idx] is increased. Otherwise there
900 * will be a race between _wait_barrier() and raise_barrier().
901 */
902 smp_mb__after_atomic();
majianpeng79ef3a82013-11-15 14:55:02 +0800903
colyli@suse.de824e47d2017-02-18 03:05:57 +0800904 /*
905 * Don't worry about checking two atomic_t variables at same time
906 * here. If during we check conf->barrier[idx], the array is
907 * frozen (conf->array_frozen is 1), and chonf->barrier[idx] is
908 * 0, it is safe to return and make the I/O continue. Because the
909 * array is frozen, all I/O returned here will eventually complete
910 * or be queued, no race will happen. See code comment in
911 * frozen_array().
912 */
913 if (!READ_ONCE(conf->array_frozen) &&
914 !atomic_read(&conf->barrier[idx]))
915 return;
916
917 /*
918 * After holding conf->resync_lock, conf->nr_pending[idx]
919 * should be decreased before waiting for barrier to drop.
920 * Otherwise, we may encounter a race condition because
921 * raise_barrer() might be waiting for conf->nr_pending[idx]
922 * to be 0 at same time.
923 */
924 spin_lock_irq(&conf->resync_lock);
925 atomic_inc(&conf->nr_waiting[idx]);
926 atomic_dec(&conf->nr_pending[idx]);
927 /*
928 * In case freeze_array() is waiting for
929 * get_unqueued_pending() == extra
930 */
931 wake_up(&conf->wait_barrier);
932 /* Wait for the barrier in same barrier unit bucket to drop. */
933 wait_event_lock_irq(conf->wait_barrier,
934 !conf->array_frozen &&
935 !atomic_read(&conf->barrier[idx]),
936 conf->resync_lock);
937 atomic_inc(&conf->nr_pending[idx]);
938 atomic_dec(&conf->nr_waiting[idx]);
colyli@suse.defd768632017-02-18 03:05:56 +0800939 spin_unlock_irq(&conf->resync_lock);
majianpeng79ef3a82013-11-15 14:55:02 +0800940}
941
colyli@suse.defd768632017-02-18 03:05:56 +0800942static void wait_read_barrier(struct r1conf *conf, sector_t sector_nr)
majianpeng79ef3a82013-11-15 14:55:02 +0800943{
colyli@suse.defd768632017-02-18 03:05:56 +0800944 int idx = sector_to_idx(sector_nr);
majianpeng79ef3a82013-11-15 14:55:02 +0800945
colyli@suse.de824e47d2017-02-18 03:05:57 +0800946 /*
947 * Very similar to _wait_barrier(). The difference is, for read
948 * I/O we don't need wait for sync I/O, but if the whole array
949 * is frozen, the read I/O still has to wait until the array is
950 * unfrozen. Since there is no ordering requirement with
951 * conf->barrier[idx] here, memory barrier is unnecessary as well.
952 */
953 atomic_inc(&conf->nr_pending[idx]);
majianpeng79ef3a82013-11-15 14:55:02 +0800954
colyli@suse.de824e47d2017-02-18 03:05:57 +0800955 if (!READ_ONCE(conf->array_frozen))
956 return;
957
958 spin_lock_irq(&conf->resync_lock);
959 atomic_inc(&conf->nr_waiting[idx]);
960 atomic_dec(&conf->nr_pending[idx]);
961 /*
962 * In case freeze_array() is waiting for
963 * get_unqueued_pending() == extra
964 */
965 wake_up(&conf->wait_barrier);
966 /* Wait for array to be unfrozen */
967 wait_event_lock_irq(conf->wait_barrier,
968 !conf->array_frozen,
969 conf->resync_lock);
970 atomic_inc(&conf->nr_pending[idx]);
971 atomic_dec(&conf->nr_waiting[idx]);
NeilBrown17999be2006-01-06 00:20:12 -0800972 spin_unlock_irq(&conf->resync_lock);
973}
974
colyli@suse.defd768632017-02-18 03:05:56 +0800975static void wait_barrier(struct r1conf *conf, sector_t sector_nr)
976{
977 int idx = sector_to_idx(sector_nr);
978
979 _wait_barrier(conf, idx);
980}
981
982static void wait_all_barriers(struct r1conf *conf)
983{
984 int idx;
985
986 for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++)
987 _wait_barrier(conf, idx);
988}
989
990static void _allow_barrier(struct r1conf *conf, int idx)
NeilBrown17999be2006-01-06 00:20:12 -0800991{
colyli@suse.de824e47d2017-02-18 03:05:57 +0800992 atomic_dec(&conf->nr_pending[idx]);
NeilBrown17999be2006-01-06 00:20:12 -0800993 wake_up(&conf->wait_barrier);
994}
995
colyli@suse.defd768632017-02-18 03:05:56 +0800996static void allow_barrier(struct r1conf *conf, sector_t sector_nr)
997{
998 int idx = sector_to_idx(sector_nr);
999
1000 _allow_barrier(conf, idx);
1001}
1002
1003static void allow_all_barriers(struct r1conf *conf)
1004{
1005 int idx;
1006
1007 for (idx = 0; idx < BARRIER_BUCKETS_NR; idx++)
1008 _allow_barrier(conf, idx);
1009}
1010
1011/* conf->resync_lock should be held */
1012static int get_unqueued_pending(struct r1conf *conf)
1013{
1014 int idx, ret;
1015
1016 for (ret = 0, idx = 0; idx < BARRIER_BUCKETS_NR; idx++)
colyli@suse.de824e47d2017-02-18 03:05:57 +08001017 ret += atomic_read(&conf->nr_pending[idx]) -
1018 atomic_read(&conf->nr_queued[idx]);
colyli@suse.defd768632017-02-18 03:05:56 +08001019
1020 return ret;
1021}
1022
NeilBrowne2d59922013-06-12 11:01:22 +10001023static void freeze_array(struct r1conf *conf, int extra)
NeilBrownddaf22a2006-01-06 00:20:19 -08001024{
colyli@suse.defd768632017-02-18 03:05:56 +08001025 /* Stop sync I/O and normal I/O and wait for everything to
NeilBrownddaf22a2006-01-06 00:20:19 -08001026 * go quite.
colyli@suse.defd768632017-02-18 03:05:56 +08001027 * This is called in two situations:
1028 * 1) management command handlers (reshape, remove disk, quiesce).
1029 * 2) one normal I/O request failed.
1030
1031 * After array_frozen is set to 1, new sync IO will be blocked at
1032 * raise_barrier(), and new normal I/O will blocked at _wait_barrier()
1033 * or wait_read_barrier(). The flying I/Os will either complete or be
1034 * queued. When everything goes quite, there are only queued I/Os left.
1035
1036 * Every flying I/O contributes to a conf->nr_pending[idx], idx is the
1037 * barrier bucket index which this I/O request hits. When all sync and
1038 * normal I/O are queued, sum of all conf->nr_pending[] will match sum
1039 * of all conf->nr_queued[]. But normal I/O failure is an exception,
1040 * in handle_read_error(), we may call freeze_array() before trying to
1041 * fix the read error. In this case, the error read I/O is not queued,
1042 * so get_unqueued_pending() == 1.
1043 *
1044 * Therefore before this function returns, we need to wait until
1045 * get_unqueued_pendings(conf) gets equal to extra. For
1046 * normal I/O context, extra is 1, in rested situations extra is 0.
NeilBrownddaf22a2006-01-06 00:20:19 -08001047 */
1048 spin_lock_irq(&conf->resync_lock);
majianpengb364e3d2013-11-14 15:16:18 +11001049 conf->array_frozen = 1;
NeilBrown578b54a2016-11-14 16:30:21 +11001050 raid1_log(conf->mddev, "wait freeze");
colyli@suse.defd768632017-02-18 03:05:56 +08001051 wait_event_lock_irq_cmd(
1052 conf->wait_barrier,
1053 get_unqueued_pending(conf) == extra,
1054 conf->resync_lock,
1055 flush_pending_writes(conf));
NeilBrownddaf22a2006-01-06 00:20:19 -08001056 spin_unlock_irq(&conf->resync_lock);
1057}
NeilBrowne8096362011-10-11 16:49:05 +11001058static void unfreeze_array(struct r1conf *conf)
NeilBrownddaf22a2006-01-06 00:20:19 -08001059{
1060 /* reverse the effect of the freeze */
1061 spin_lock_irq(&conf->resync_lock);
majianpengb364e3d2013-11-14 15:16:18 +11001062 conf->array_frozen = 0;
NeilBrownddaf22a2006-01-06 00:20:19 -08001063 spin_unlock_irq(&conf->resync_lock);
colyli@suse.de824e47d2017-02-18 03:05:57 +08001064 wake_up(&conf->wait_barrier);
NeilBrownddaf22a2006-01-06 00:20:19 -08001065}
1066
NeilBrownf72ffdd2014-09-30 14:23:59 +10001067/* duplicate the data pages for behind I/O
NeilBrown4e780642010-10-19 12:54:01 +11001068 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001069static void alloc_behind_pages(struct bio *bio, struct r1bio *r1_bio)
NeilBrown4b6d2872005-09-09 16:23:47 -07001070{
1071 int i;
1072 struct bio_vec *bvec;
NeilBrown2ca68f52011-07-28 11:32:10 +10001073 struct bio_vec *bvecs = kzalloc(bio->bi_vcnt * sizeof(struct bio_vec),
NeilBrown4b6d2872005-09-09 16:23:47 -07001074 GFP_NOIO);
NeilBrown2ca68f52011-07-28 11:32:10 +10001075 if (unlikely(!bvecs))
NeilBrownaf6d7b72011-05-11 14:51:19 +10001076 return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001077
Kent Overstreetcb34e052012-09-05 15:22:02 -07001078 bio_for_each_segment_all(bvec, bio, i) {
NeilBrown2ca68f52011-07-28 11:32:10 +10001079 bvecs[i] = *bvec;
1080 bvecs[i].bv_page = alloc_page(GFP_NOIO);
1081 if (unlikely(!bvecs[i].bv_page))
NeilBrown4b6d2872005-09-09 16:23:47 -07001082 goto do_sync_io;
NeilBrown2ca68f52011-07-28 11:32:10 +10001083 memcpy(kmap(bvecs[i].bv_page) + bvec->bv_offset,
1084 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
1085 kunmap(bvecs[i].bv_page);
NeilBrown4b6d2872005-09-09 16:23:47 -07001086 kunmap(bvec->bv_page);
1087 }
NeilBrown2ca68f52011-07-28 11:32:10 +10001088 r1_bio->behind_bvecs = bvecs;
NeilBrownaf6d7b72011-05-11 14:51:19 +10001089 r1_bio->behind_page_count = bio->bi_vcnt;
1090 set_bit(R1BIO_BehindIO, &r1_bio->state);
1091 return;
NeilBrown4b6d2872005-09-09 16:23:47 -07001092
1093do_sync_io:
NeilBrownaf6d7b72011-05-11 14:51:19 +10001094 for (i = 0; i < bio->bi_vcnt; i++)
NeilBrown2ca68f52011-07-28 11:32:10 +10001095 if (bvecs[i].bv_page)
1096 put_page(bvecs[i].bv_page);
1097 kfree(bvecs);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001098 pr_debug("%dB behind alloc failed, doing sync I/O\n",
1099 bio->bi_iter.bi_size);
NeilBrown4b6d2872005-09-09 16:23:47 -07001100}
1101
NeilBrownf54a9d02012-08-02 08:33:20 +10001102struct raid1_plug_cb {
1103 struct blk_plug_cb cb;
1104 struct bio_list pending;
1105 int pending_cnt;
1106};
1107
1108static void raid1_unplug(struct blk_plug_cb *cb, bool from_schedule)
1109{
1110 struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb,
1111 cb);
1112 struct mddev *mddev = plug->cb.data;
1113 struct r1conf *conf = mddev->private;
1114 struct bio *bio;
1115
NeilBrown874807a2012-11-27 12:14:40 +11001116 if (from_schedule || current->bio_list) {
NeilBrownf54a9d02012-08-02 08:33:20 +10001117 spin_lock_irq(&conf->device_lock);
1118 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1119 conf->pending_count += plug->pending_cnt;
1120 spin_unlock_irq(&conf->device_lock);
NeilBrownee0b0242013-02-25 12:38:29 +11001121 wake_up(&conf->wait_barrier);
NeilBrownf54a9d02012-08-02 08:33:20 +10001122 md_wakeup_thread(mddev->thread);
1123 kfree(plug);
1124 return;
1125 }
1126
1127 /* we aren't scheduling, so we can do the write-out directly. */
1128 bio = bio_list_get(&plug->pending);
1129 bitmap_unplug(mddev->bitmap);
1130 wake_up(&conf->wait_barrier);
1131
1132 while (bio) { /* submit pending writes */
1133 struct bio *next = bio->bi_next;
NeilBrown5e2c7a32016-11-04 16:46:03 +11001134 struct md_rdev *rdev = (void*)bio->bi_bdev;
NeilBrownf54a9d02012-08-02 08:33:20 +10001135 bio->bi_next = NULL;
NeilBrown5e2c7a32016-11-04 16:46:03 +11001136 bio->bi_bdev = rdev->bdev;
1137 if (test_bit(Faulty, &rdev->flags)) {
1138 bio->bi_error = -EIO;
1139 bio_endio(bio);
1140 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1141 !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
Shaohua Li32f9f572013-04-28 18:26:38 +08001142 /* Just ignore it */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001143 bio_endio(bio);
Shaohua Li32f9f572013-04-28 18:26:38 +08001144 else
1145 generic_make_request(bio);
NeilBrownf54a9d02012-08-02 08:33:20 +10001146 bio = next;
1147 }
1148 kfree(plug);
1149}
1150
colyli@suse.defd768632017-02-18 03:05:56 +08001151static inline struct r1bio *
1152alloc_r1bio(struct mddev *mddev, struct bio *bio, sector_t sectors_handled)
1153{
1154 struct r1conf *conf = mddev->private;
1155 struct r1bio *r1_bio;
1156
1157 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1158
1159 r1_bio->master_bio = bio;
1160 r1_bio->sectors = bio_sectors(bio) - sectors_handled;
1161 r1_bio->state = 0;
1162 r1_bio->mddev = mddev;
1163 r1_bio->sector = bio->bi_iter.bi_sector + sectors_handled;
1164
1165 return r1_bio;
1166}
1167
1168static void raid1_read_request(struct mddev *mddev, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
NeilBrowne8096362011-10-11 16:49:05 +11001170 struct r1conf *conf = mddev->private;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001171 struct raid1_info *mirror;
colyli@suse.defd768632017-02-18 03:05:56 +08001172 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 struct bio *read_bio;
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001174 struct bitmap *bitmap = mddev->bitmap;
1175 const int op = bio_op(bio);
1176 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1177 int sectors_handled;
1178 int max_sectors;
1179 int rdisk;
1180
colyli@suse.defd768632017-02-18 03:05:56 +08001181 /*
1182 * Still need barrier for READ in case that whole
1183 * array is frozen.
1184 */
1185 wait_read_barrier(conf, bio->bi_iter.bi_sector);
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001186
colyli@suse.defd768632017-02-18 03:05:56 +08001187 r1_bio = alloc_r1bio(mddev, bio, 0);
1188
1189 /*
1190 * We might need to issue multiple reads to different
1191 * devices if there are bad blocks around, so we keep
1192 * track of the number of reads in bio->bi_phys_segments.
1193 * If this is 0, there is only one r1_bio and no locking
1194 * will be needed when requests complete. If it is
1195 * non-zero, then it is the number of not-completed requests.
1196 */
1197 bio->bi_phys_segments = 0;
1198 bio_clear_flag(bio, BIO_SEG_VALID);
1199
1200 /*
1201 * make_request() can abort the operation when read-ahead is being
1202 * used and no empty request is available.
1203 */
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001204read_again:
1205 rdisk = read_balance(conf, r1_bio, &max_sectors);
1206
1207 if (rdisk < 0) {
1208 /* couldn't find anywhere to read from */
1209 raid_end_bio_io(r1_bio);
1210 return;
1211 }
1212 mirror = conf->mirrors + rdisk;
1213
1214 if (test_bit(WriteMostly, &mirror->rdev->flags) &&
1215 bitmap) {
1216 /*
1217 * Reading from a write-mostly device must take care not to
1218 * over-take any writes that are 'behind'
1219 */
1220 raid1_log(mddev, "wait behind writes");
1221 wait_event(bitmap->behind_wait,
1222 atomic_read(&bitmap->behind_writes) == 0);
1223 }
1224 r1_bio->read_disk = rdisk;
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001225
Ming Leid7a10302017-02-14 23:29:03 +08001226 read_bio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001227 bio_trim(read_bio, r1_bio->sector - bio->bi_iter.bi_sector,
1228 max_sectors);
1229
1230 r1_bio->bios[rdisk] = read_bio;
1231
1232 read_bio->bi_iter.bi_sector = r1_bio->sector +
1233 mirror->rdev->data_offset;
1234 read_bio->bi_bdev = mirror->rdev->bdev;
1235 read_bio->bi_end_io = raid1_end_read_request;
1236 bio_set_op_attrs(read_bio, op, do_sync);
1237 if (test_bit(FailFast, &mirror->rdev->flags) &&
1238 test_bit(R1BIO_FailFast, &r1_bio->state))
1239 read_bio->bi_opf |= MD_FAILFAST;
1240 read_bio->bi_private = r1_bio;
1241
1242 if (mddev->gendisk)
1243 trace_block_bio_remap(bdev_get_queue(read_bio->bi_bdev),
1244 read_bio, disk_devt(mddev->gendisk),
1245 r1_bio->sector);
1246
1247 if (max_sectors < r1_bio->sectors) {
1248 /*
1249 * could not read all from this device, so we will need another
1250 * r1_bio.
1251 */
1252 sectors_handled = (r1_bio->sector + max_sectors
1253 - bio->bi_iter.bi_sector);
1254 r1_bio->sectors = max_sectors;
1255 spin_lock_irq(&conf->device_lock);
1256 if (bio->bi_phys_segments == 0)
1257 bio->bi_phys_segments = 2;
1258 else
1259 bio->bi_phys_segments++;
1260 spin_unlock_irq(&conf->device_lock);
1261
1262 /*
1263 * Cannot call generic_make_request directly as that will be
1264 * queued in __make_request and subsequent mempool_alloc might
1265 * block waiting for it. So hand bio over to raid1d.
1266 */
1267 reschedule_retry(r1_bio);
1268
colyli@suse.defd768632017-02-18 03:05:56 +08001269 r1_bio = alloc_r1bio(mddev, bio, sectors_handled);
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001270 goto read_again;
1271 } else
1272 generic_make_request(read_bio);
1273}
1274
colyli@suse.defd768632017-02-18 03:05:56 +08001275static void raid1_write_request(struct mddev *mddev, struct bio *bio)
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001276{
1277 struct r1conf *conf = mddev->private;
colyli@suse.defd768632017-02-18 03:05:56 +08001278 struct r1bio *r1_bio;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001279 int i, disks;
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001280 struct bitmap *bitmap = mddev->bitmap;
NeilBrown191ea9b2005-06-21 17:17:23 -07001281 unsigned long flags;
Mike Christie796a5cf2016-06-05 14:32:07 -05001282 const int op = bio_op(bio);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001283 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1284 const unsigned long do_flush_fua = (bio->bi_opf &
Mike Christie28a8f0d2016-06-05 14:32:25 -05001285 (REQ_PREFLUSH | REQ_FUA));
NeilBrown3cb03002011-10-11 16:45:26 +11001286 struct md_rdev *blocked_rdev;
NeilBrownf54a9d02012-08-02 08:33:20 +10001287 struct blk_plug_cb *cb;
1288 struct raid1_plug_cb *plug = NULL;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001289 int first_clone;
1290 int sectors_handled;
1291 int max_sectors;
NeilBrown191ea9b2005-06-21 17:17:23 -07001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 /*
1294 * Register the new request and wait if the reconstruction
1295 * thread has put up a bar for new requests.
1296 * Continue immediately if no resync is active currently.
1297 */
NeilBrown62de6082006-05-01 12:15:47 -07001298
NeilBrown3d310eb2005-06-21 17:17:26 -07001299 md_write_start(mddev, bio); /* wait on superblock update early */
1300
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001301 if ((bio_end_sector(bio) > mddev->suspend_lo &&
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001302 bio->bi_iter.bi_sector < mddev->suspend_hi) ||
1303 (mddev_is_clustered(mddev) &&
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -05001304 md_cluster_ops->area_resyncing(mddev, WRITE,
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001305 bio->bi_iter.bi_sector, bio_end_sector(bio)))) {
1306
1307 /*
1308 * As the suspend_* range is controlled by userspace, we want
1309 * an interruptible wait.
NeilBrown6eef4b22009-12-14 12:49:51 +11001310 */
1311 DEFINE_WAIT(w);
1312 for (;;) {
1313 flush_signals(current);
1314 prepare_to_wait(&conf->wait_barrier,
1315 &w, TASK_INTERRUPTIBLE);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07001316 if (bio_end_sector(bio) <= mddev->suspend_lo ||
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -05001317 bio->bi_iter.bi_sector >= mddev->suspend_hi ||
1318 (mddev_is_clustered(mddev) &&
Goldwyn Rodrigues90382ed2015-06-24 09:30:32 -05001319 !md_cluster_ops->area_resyncing(mddev, WRITE,
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001320 bio->bi_iter.bi_sector,
1321 bio_end_sector(bio))))
NeilBrown6eef4b22009-12-14 12:49:51 +11001322 break;
1323 schedule();
1324 }
1325 finish_wait(&conf->wait_barrier, &w);
1326 }
colyli@suse.defd768632017-02-18 03:05:56 +08001327 wait_barrier(conf, bio->bi_iter.bi_sector);
1328
1329 r1_bio = alloc_r1bio(mddev, bio, 0);
1330
1331 /* We might need to issue multiple writes to different
1332 * devices if there are bad blocks around, so we keep
1333 * track of the number of writes in bio->bi_phys_segments.
1334 * If this is 0, there is only one r1_bio and no locking
1335 * will be needed when requests complete. If it is
1336 * non-zero, then it is the number of not-completed requests.
1337 */
1338 bio->bi_phys_segments = 0;
1339 bio_clear_flag(bio, BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
NeilBrown34db0cd2011-10-11 16:50:01 +11001341 if (conf->pending_count >= max_queued_requests) {
1342 md_wakeup_thread(mddev->thread);
NeilBrown578b54a2016-11-14 16:30:21 +11001343 raid1_log(mddev, "wait queued");
NeilBrown34db0cd2011-10-11 16:50:01 +11001344 wait_event(conf->wait_barrier,
1345 conf->pending_count < max_queued_requests);
1346 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001347 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 * inc refcount on their rdev. Record them by setting
1349 * bios[x] to bio
NeilBrown1f68f0c2011-07-28 11:31:48 +10001350 * If there are known/acknowledged bad blocks on any device on
1351 * which we have seen a write error, we want to avoid writing those
1352 * blocks.
1353 * This potentially requires several writes to write around
1354 * the bad blocks. Each set of writes gets it's own r1bio
1355 * with a set of bios attached.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001357
NeilBrown8f19ccb2011-12-23 10:17:56 +11001358 disks = conf->raid_disks * 2;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001359 retry_write:
1360 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 rcu_read_lock();
NeilBrown1f68f0c2011-07-28 11:31:48 +10001362 max_sectors = r1_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 for (i = 0; i < disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001364 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001365 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1366 atomic_inc(&rdev->nr_pending);
1367 blocked_rdev = rdev;
1368 break;
1369 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001370 r1_bio->bios[i] = NULL;
Kent Overstreet8ae12662015-04-27 23:48:34 -07001371 if (!rdev || test_bit(Faulty, &rdev->flags)) {
NeilBrown8f19ccb2011-12-23 10:17:56 +11001372 if (i < conf->raid_disks)
1373 set_bit(R1BIO_Degraded, &r1_bio->state);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001374 continue;
1375 }
1376
1377 atomic_inc(&rdev->nr_pending);
1378 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1379 sector_t first_bad;
1380 int bad_sectors;
1381 int is_bad;
1382
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001383 is_bad = is_badblock(rdev, r1_bio->sector, max_sectors,
NeilBrown1f68f0c2011-07-28 11:31:48 +10001384 &first_bad, &bad_sectors);
1385 if (is_bad < 0) {
1386 /* mustn't write here until the bad block is
1387 * acknowledged*/
1388 set_bit(BlockedBadBlocks, &rdev->flags);
1389 blocked_rdev = rdev;
1390 break;
NeilBrown964147d2010-05-18 15:27:13 +10001391 }
NeilBrown1f68f0c2011-07-28 11:31:48 +10001392 if (is_bad && first_bad <= r1_bio->sector) {
1393 /* Cannot write here at all */
1394 bad_sectors -= (r1_bio->sector - first_bad);
1395 if (bad_sectors < max_sectors)
1396 /* mustn't write more than bad_sectors
1397 * to other devices yet
1398 */
1399 max_sectors = bad_sectors;
1400 rdev_dec_pending(rdev, mddev);
1401 /* We don't set R1BIO_Degraded as that
1402 * only applies if the disk is
1403 * missing, so it might be re-added,
1404 * and we want to know to recover this
1405 * chunk.
1406 * In this case the device is here,
1407 * and the fact that this chunk is not
1408 * in-sync is recorded in the bad
1409 * block log
1410 */
1411 continue;
1412 }
1413 if (is_bad) {
1414 int good_sectors = first_bad - r1_bio->sector;
1415 if (good_sectors < max_sectors)
1416 max_sectors = good_sectors;
1417 }
1418 }
1419 r1_bio->bios[i] = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421 rcu_read_unlock();
1422
Dan Williams6bfe0b42008-04-30 00:52:32 -07001423 if (unlikely(blocked_rdev)) {
1424 /* Wait for this device to become unblocked */
1425 int j;
1426
1427 for (j = 0; j < i; j++)
1428 if (r1_bio->bios[j])
1429 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001430 r1_bio->state = 0;
colyli@suse.defd768632017-02-18 03:05:56 +08001431 allow_barrier(conf, bio->bi_iter.bi_sector);
NeilBrown578b54a2016-11-14 16:30:21 +11001432 raid1_log(mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001433 md_wait_for_blocked_rdev(blocked_rdev, mddev);
colyli@suse.defd768632017-02-18 03:05:56 +08001434 wait_barrier(conf, bio->bi_iter.bi_sector);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001435 goto retry_write;
1436 }
1437
NeilBrown1f68f0c2011-07-28 11:31:48 +10001438 if (max_sectors < r1_bio->sectors) {
1439 /* We are splitting this write into multiple parts, so
1440 * we need to prepare for allocating another r1_bio.
1441 */
1442 r1_bio->sectors = max_sectors;
1443 spin_lock_irq(&conf->device_lock);
1444 if (bio->bi_phys_segments == 0)
1445 bio->bi_phys_segments = 2;
1446 else
1447 bio->bi_phys_segments++;
1448 spin_unlock_irq(&conf->device_lock);
NeilBrown191ea9b2005-06-21 17:17:23 -07001449 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07001450 sectors_handled = r1_bio->sector + max_sectors - bio->bi_iter.bi_sector;
NeilBrown4b6d2872005-09-09 16:23:47 -07001451
NeilBrown4e780642010-10-19 12:54:01 +11001452 atomic_set(&r1_bio->remaining, 1);
NeilBrown4b6d2872005-09-09 16:23:47 -07001453 atomic_set(&r1_bio->behind_remaining, 0);
NeilBrown191ea9b2005-06-21 17:17:23 -07001454
NeilBrown1f68f0c2011-07-28 11:31:48 +10001455 first_clone = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 for (i = 0; i < disks; i++) {
Ming Lei8e58e322017-02-14 23:29:01 +08001457 struct bio *mbio = NULL;
1458 sector_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (!r1_bio->bios[i])
1460 continue;
1461
Ming Lei8e58e322017-02-14 23:29:01 +08001462 offset = r1_bio->sector - bio->bi_iter.bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
NeilBrown1f68f0c2011-07-28 11:31:48 +10001464 if (first_clone) {
1465 /* do behind I/O ?
1466 * Not if there are too many, or cannot
1467 * allocate memory, or a reader on WriteMostly
1468 * is waiting for behind writes to flush */
1469 if (bitmap &&
1470 (atomic_read(&bitmap->behind_writes)
1471 < mddev->bitmap_info.max_write_behind) &&
Ming Lei8e58e322017-02-14 23:29:01 +08001472 !waitqueue_active(&bitmap->behind_wait)) {
1473 mbio = bio_clone_bioset_partial(bio, GFP_NOIO,
1474 mddev->bio_set,
1475 offset,
1476 max_sectors);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001477 alloc_behind_pages(mbio, r1_bio);
Ming Lei8e58e322017-02-14 23:29:01 +08001478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
NeilBrown1f68f0c2011-07-28 11:31:48 +10001480 bitmap_startwrite(bitmap, r1_bio->sector,
1481 r1_bio->sectors,
1482 test_bit(R1BIO_BehindIO,
1483 &r1_bio->state));
1484 first_clone = 0;
1485 }
Ming Lei8e58e322017-02-14 23:29:01 +08001486
1487 if (!mbio) {
Ming Leid7a10302017-02-14 23:29:03 +08001488 mbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
Ming Lei8e58e322017-02-14 23:29:01 +08001489 bio_trim(mbio, offset, max_sectors);
1490 }
1491
NeilBrown2ca68f52011-07-28 11:32:10 +10001492 if (r1_bio->behind_bvecs) {
NeilBrown4b6d2872005-09-09 16:23:47 -07001493 struct bio_vec *bvec;
1494 int j;
1495
Kent Overstreetcb34e052012-09-05 15:22:02 -07001496 /*
1497 * We trimmed the bio, so _all is legit
NeilBrown4b6d2872005-09-09 16:23:47 -07001498 */
Kent Overstreetd74c6d52013-02-06 12:23:11 -08001499 bio_for_each_segment_all(bvec, mbio, j)
NeilBrown2ca68f52011-07-28 11:32:10 +10001500 bvec->bv_page = r1_bio->behind_bvecs[j].bv_page;
NeilBrown4b6d2872005-09-09 16:23:47 -07001501 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
1502 atomic_inc(&r1_bio->behind_remaining);
1503 }
1504
NeilBrown1f68f0c2011-07-28 11:31:48 +10001505 r1_bio->bios[i] = mbio;
1506
Kent Overstreet4f024f32013-10-11 15:44:27 -07001507 mbio->bi_iter.bi_sector = (r1_bio->sector +
NeilBrown1f68f0c2011-07-28 11:31:48 +10001508 conf->mirrors[i].rdev->data_offset);
NeilBrown109e3762016-11-18 13:22:04 +11001509 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001510 mbio->bi_end_io = raid1_end_write_request;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001511 bio_set_op_attrs(mbio, op, do_flush_fua | do_sync);
NeilBrown212e7eb2016-11-18 16:16:12 +11001512 if (test_bit(FailFast, &conf->mirrors[i].rdev->flags) &&
1513 !test_bit(WriteMostly, &conf->mirrors[i].rdev->flags) &&
1514 conf->raid_disks - mddev->degraded > 1)
1515 mbio->bi_opf |= MD_FAILFAST;
NeilBrown1f68f0c2011-07-28 11:31:48 +10001516 mbio->bi_private = r1_bio;
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 atomic_inc(&r1_bio->remaining);
NeilBrownf54a9d02012-08-02 08:33:20 +10001519
NeilBrown109e3762016-11-18 13:22:04 +11001520 if (mddev->gendisk)
1521 trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev),
1522 mbio, disk_devt(mddev->gendisk),
1523 r1_bio->sector);
1524 /* flush_pending_writes() needs access to the rdev so...*/
1525 mbio->bi_bdev = (void*)conf->mirrors[i].rdev;
1526
NeilBrownf54a9d02012-08-02 08:33:20 +10001527 cb = blk_check_plugged(raid1_unplug, mddev, sizeof(*plug));
1528 if (cb)
1529 plug = container_of(cb, struct raid1_plug_cb, cb);
1530 else
1531 plug = NULL;
NeilBrown4e780642010-10-19 12:54:01 +11001532 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownf54a9d02012-08-02 08:33:20 +10001533 if (plug) {
1534 bio_list_add(&plug->pending, mbio);
1535 plug->pending_cnt++;
1536 } else {
1537 bio_list_add(&conf->pending_bio_list, mbio);
1538 conf->pending_count++;
1539 }
NeilBrown4e780642010-10-19 12:54:01 +11001540 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrownf54a9d02012-08-02 08:33:20 +10001541 if (!plug)
NeilBrownb357f042012-07-03 17:45:31 +10001542 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
NeilBrown079fa162011-09-10 17:21:23 +10001544 /* Mustn't call r1_bio_write_done before this next test,
1545 * as it could result in the bio being freed.
1546 */
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08001547 if (sectors_handled < bio_sectors(bio)) {
NeilBrown079fa162011-09-10 17:21:23 +10001548 r1_bio_write_done(r1_bio);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001549 /* We need another r1_bio. It has already been counted
1550 * in bio->bi_phys_segments
1551 */
colyli@suse.defd768632017-02-18 03:05:56 +08001552 r1_bio = alloc_r1bio(mddev, bio, sectors_handled);
NeilBrown1f68f0c2011-07-28 11:31:48 +10001553 goto retry_write;
1554 }
1555
NeilBrown079fa162011-09-10 17:21:23 +10001556 r1_bio_write_done(r1_bio);
1557
1558 /* In case raid1d snuck in to freeze_array */
1559 wake_up(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001562static void raid1_make_request(struct mddev *mddev, struct bio *bio)
1563{
colyli@suse.defd768632017-02-18 03:05:56 +08001564 struct bio *split;
1565 sector_t sectors;
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001566
colyli@suse.defd768632017-02-18 03:05:56 +08001567 /* if bio exceeds barrier unit boundary, split it */
1568 do {
1569 sectors = align_to_barrier_unit_end(
1570 bio->bi_iter.bi_sector, bio_sectors(bio));
1571 if (sectors < bio_sectors(bio)) {
1572 split = bio_split(bio, sectors, GFP_NOIO, fs_bio_set);
1573 bio_chain(split, bio);
1574 } else {
1575 split = bio;
1576 }
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001577
colyli@suse.defd768632017-02-18 03:05:56 +08001578 if (bio_data_dir(split) == READ)
1579 raid1_read_request(mddev, split);
1580 else
1581 raid1_write_request(mddev, split);
1582 } while (split != bio);
Robert LeBlanc3b046a92016-12-05 13:02:57 -07001583}
1584
Shaohua Li849674e2016-01-20 13:52:20 -08001585static void raid1_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
NeilBrowne8096362011-10-11 16:49:05 +11001587 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 int i;
1589
1590 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown11ce99e2006-10-03 01:15:52 -07001591 conf->raid_disks - mddev->degraded);
NeilBrownddac7c72006-08-31 21:27:36 -07001592 rcu_read_lock();
1593 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001594 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 seq_printf(seq, "%s",
NeilBrownddac7c72006-08-31 21:27:36 -07001596 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1597 }
1598 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 seq_printf(seq, "]");
1600}
1601
Shaohua Li849674e2016-01-20 13:52:20 -08001602static void raid1_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 char b[BDEVNAME_SIZE];
NeilBrowne8096362011-10-11 16:49:05 +11001605 struct r1conf *conf = mddev->private;
NeilBrown423f04d2015-07-27 11:48:52 +10001606 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 /*
1609 * If it is not operational, then we have already marked it as dead
1610 * else if it is the last working disks, ignore the error, let the
1611 * next level up know.
1612 * else mark the drive as failed
1613 */
NeilBrown2e52d442016-11-18 16:16:12 +11001614 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001615 if (test_bit(In_sync, &rdev->flags)
NeilBrown4044ba52009-01-09 08:31:11 +11001616 && (conf->raid_disks - mddev->degraded) == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 /*
1618 * Don't fail the drive, act as though we were just a
NeilBrown4044ba52009-01-09 08:31:11 +11001619 * normal single drive.
1620 * However don't try a recovery from this drive as
1621 * it is very likely to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 */
NeilBrown53890422011-07-27 11:00:36 +10001623 conf->recovery_disabled = mddev->recovery_disabled;
NeilBrown2e52d442016-11-18 16:16:12 +11001624 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return;
NeilBrown4044ba52009-01-09 08:31:11 +11001626 }
NeilBrownde393cd2011-07-28 11:31:48 +10001627 set_bit(Blocked, &rdev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001628 if (test_and_clear_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 mddev->degraded++;
NeilBrowndd00a992007-05-10 03:15:50 -07001630 set_bit(Faulty, &rdev->flags);
NeilBrowndd00a992007-05-10 03:15:50 -07001631 } else
1632 set_bit(Faulty, &rdev->flags);
NeilBrown423f04d2015-07-27 11:48:52 +10001633 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown2446dba2014-07-31 10:16:29 +10001634 /*
1635 * if recovery is running, make sure it aborts.
1636 */
1637 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08001638 set_mask_bits(&mddev->sb_flags, 0,
1639 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown1d41c212016-11-02 14:16:50 +11001640 pr_crit("md/raid1:%s: Disk failure on %s, disabling device.\n"
1641 "md/raid1:%s: Operation continuing on %d devices.\n",
1642 mdname(mddev), bdevname(rdev->bdev, b),
1643 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
NeilBrowne8096362011-10-11 16:49:05 +11001646static void print_conf(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
1648 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
NeilBrown1d41c212016-11-02 14:16:50 +11001650 pr_debug("RAID1 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (!conf) {
NeilBrown1d41c212016-11-02 14:16:50 +11001652 pr_debug("(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 return;
1654 }
NeilBrown1d41c212016-11-02 14:16:50 +11001655 pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1656 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
NeilBrownddac7c72006-08-31 21:27:36 -07001658 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 for (i = 0; i < conf->raid_disks; i++) {
1660 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11001661 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownddac7c72006-08-31 21:27:36 -07001662 if (rdev)
NeilBrown1d41c212016-11-02 14:16:50 +11001663 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1664 i, !test_bit(In_sync, &rdev->flags),
1665 !test_bit(Faulty, &rdev->flags),
1666 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
NeilBrownddac7c72006-08-31 21:27:36 -07001668 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
NeilBrowne8096362011-10-11 16:49:05 +11001671static void close_sync(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
colyli@suse.defd768632017-02-18 03:05:56 +08001673 wait_all_barriers(conf);
1674 allow_all_barriers(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 mempool_destroy(conf->r1buf_pool);
1677 conf->r1buf_pool = NULL;
1678}
1679
NeilBrownfd01b882011-10-11 16:47:53 +11001680static int raid1_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 int i;
NeilBrowne8096362011-10-11 16:49:05 +11001683 struct r1conf *conf = mddev->private;
NeilBrown6b965622010-08-18 11:56:59 +10001684 int count = 0;
1685 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 /*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001688 * Find all failed disks within the RAID1 configuration
NeilBrownddac7c72006-08-31 21:27:36 -07001689 * and mark them readable.
1690 * Called under mddev lock, so rcu protection not needed.
NeilBrown423f04d2015-07-27 11:48:52 +10001691 * device_lock used to avoid races with raid1_end_read_request
1692 * which expects 'In_sync' flags and ->degraded to be consistent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 */
NeilBrown423f04d2015-07-27 11:48:52 +10001694 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11001696 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrown8c7a2c22011-12-23 10:17:57 +11001697 struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev;
1698 if (repl
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05001699 && !test_bit(Candidate, &repl->flags)
NeilBrown8c7a2c22011-12-23 10:17:57 +11001700 && repl->recovery_offset == MaxSector
1701 && !test_bit(Faulty, &repl->flags)
1702 && !test_and_set_bit(In_sync, &repl->flags)) {
1703 /* replacement has just become active */
1704 if (!rdev ||
1705 !test_and_clear_bit(In_sync, &rdev->flags))
1706 count++;
1707 if (rdev) {
1708 /* Replaced device not technically
1709 * faulty, but we need to be sure
1710 * it gets removed and never re-added
1711 */
1712 set_bit(Faulty, &rdev->flags);
1713 sysfs_notify_dirent_safe(
1714 rdev->sysfs_state);
1715 }
1716 }
NeilBrownddac7c72006-08-31 21:27:36 -07001717 if (rdev
Lukasz Dorau61e49472013-10-24 12:55:17 +11001718 && rdev->recovery_offset == MaxSector
NeilBrownddac7c72006-08-31 21:27:36 -07001719 && !test_bit(Faulty, &rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001720 && !test_and_set_bit(In_sync, &rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001721 count++;
Jonathan Brassow654e8b52011-07-27 11:00:36 +10001722 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
1724 }
NeilBrown6b965622010-08-18 11:56:59 +10001725 mddev->degraded -= count;
1726 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001729 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
NeilBrownfd01b882011-10-11 16:47:53 +11001732static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
NeilBrowne8096362011-10-11 16:49:05 +11001734 struct r1conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001735 int err = -EEXIST;
NeilBrown41158c72005-06-21 17:17:25 -07001736 int mirror = 0;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001737 struct raid1_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10001738 int first = 0;
NeilBrown30194632011-12-23 10:17:56 +11001739 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
NeilBrown53890422011-07-27 11:00:36 +10001741 if (mddev->recovery_disabled == conf->recovery_disabled)
1742 return -EBUSY;
1743
Dan Williams1501efa2016-01-13 16:00:07 -08001744 if (md_integrity_add_rdev(rdev, mddev))
1745 return -ENXIO;
1746
Neil Brown6c2fce22008-06-28 08:31:31 +10001747 if (rdev->raid_disk >= 0)
1748 first = last = rdev->raid_disk;
1749
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05001750 /*
1751 * find the disk ... but prefer rdev->saved_raid_disk
1752 * if possible.
1753 */
1754 if (rdev->saved_raid_disk >= 0 &&
1755 rdev->saved_raid_disk >= first &&
1756 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1757 first = last = rdev->saved_raid_disk;
1758
NeilBrown7ef449d2011-12-23 10:17:57 +11001759 for (mirror = first; mirror <= last; mirror++) {
1760 p = conf->mirrors+mirror;
1761 if (!p->rdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Jonathan Brassow9092c022013-05-02 14:19:24 -05001763 if (mddev->gendisk)
1764 disk_stack_limits(mddev->gendisk, rdev->bdev,
1765 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 p->head_position = 0;
1768 rdev->raid_disk = mirror;
Neil Brown199050e2008-06-28 08:31:33 +10001769 err = 0;
NeilBrown6aea114a2005-11-28 13:44:13 -08001770 /* As all devices are equivalent, we don't need a full recovery
1771 * if this was recently any drive of the array
1772 */
1773 if (rdev->saved_raid_disk < 0)
NeilBrown41158c72005-06-21 17:17:25 -07001774 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001775 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 break;
1777 }
NeilBrown7ef449d2011-12-23 10:17:57 +11001778 if (test_bit(WantReplacement, &p->rdev->flags) &&
1779 p[conf->raid_disks].rdev == NULL) {
1780 /* Add this device as a replacement */
1781 clear_bit(In_sync, &rdev->flags);
1782 set_bit(Replacement, &rdev->flags);
1783 rdev->raid_disk = mirror;
1784 err = 0;
1785 conf->fullsync = 1;
1786 rcu_assign_pointer(p[conf->raid_disks].rdev, rdev);
1787 break;
1788 }
1789 }
Jonathan Brassow9092c022013-05-02 14:19:24 -05001790 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
Shaohua Li2ff8cc22012-10-11 13:28:54 +11001791 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001793 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
NeilBrownb8321b62011-12-23 10:17:51 +11001796static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
NeilBrowne8096362011-10-11 16:49:05 +11001798 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11001800 int number = rdev->raid_disk;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10001801 struct raid1_info *p = conf->mirrors + number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
NeilBrownb014f142011-12-23 10:17:56 +11001803 if (rdev != p->rdev)
1804 p = conf->mirrors + conf->raid_disks + number;
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 print_conf(conf);
NeilBrownb8321b62011-12-23 10:17:51 +11001807 if (rdev == p->rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001808 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 atomic_read(&rdev->nr_pending)) {
1810 err = -EBUSY;
1811 goto abort;
1812 }
NeilBrown046abee2010-10-26 15:46:20 +11001813 /* Only remove non-faulty devices if recovery
NeilBrowndfc70642008-05-23 13:04:39 -07001814 * is not possible.
1815 */
1816 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown53890422011-07-27 11:00:36 +10001817 mddev->recovery_disabled != conf->recovery_disabled &&
NeilBrowndfc70642008-05-23 13:04:39 -07001818 mddev->degraded < conf->raid_disks) {
1819 err = -EBUSY;
1820 goto abort;
1821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 p->rdev = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10001823 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1824 synchronize_rcu();
1825 if (atomic_read(&rdev->nr_pending)) {
1826 /* lost the race, try later */
1827 err = -EBUSY;
1828 p->rdev = rdev;
1829 goto abort;
1830 }
1831 }
1832 if (conf->mirrors[conf->raid_disks + number].rdev) {
NeilBrown8c7a2c22011-12-23 10:17:57 +11001833 /* We just removed a device that is being replaced.
1834 * Move down the replacement. We drain all IO before
1835 * doing this to avoid confusion.
1836 */
1837 struct md_rdev *repl =
1838 conf->mirrors[conf->raid_disks + number].rdev;
NeilBrowne2d59922013-06-12 11:01:22 +10001839 freeze_array(conf, 0);
NeilBrown8c7a2c22011-12-23 10:17:57 +11001840 clear_bit(Replacement, &repl->flags);
1841 p->rdev = repl;
1842 conf->mirrors[conf->raid_disks + number].rdev = NULL;
NeilBrowne2d59922013-06-12 11:01:22 +10001843 unfreeze_array(conf);
NeilBrownb014f142011-12-23 10:17:56 +11001844 clear_bit(WantReplacement, &rdev->flags);
NeilBrown8c7a2c22011-12-23 10:17:57 +11001845 } else
1846 clear_bit(WantReplacement, &rdev->flags);
Martin K. Petersena91a2782011-03-17 11:11:05 +01001847 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
1849abort:
1850
1851 print_conf(conf);
1852 return err;
1853}
1854
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001855static void end_sync_read(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001857 struct r1bio *r1_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
NeilBrown0fc280f2011-10-07 14:22:55 +11001859 update_head_pos(r1_bio->read_disk, r1_bio);
Namhyung Kimba3ae3b2011-10-07 14:22:53 +11001860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 /*
1862 * we have read a block, now it needs to be re-written,
1863 * or re-read if the read failed.
1864 * We don't do much here, just schedule handling by raid1d
1865 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001866 if (!bio->bi_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 set_bit(R1BIO_Uptodate, &r1_bio->state);
NeilBrownd11c1712006-01-06 00:20:26 -08001868
1869 if (atomic_dec_and_test(&r1_bio->remaining))
1870 reschedule_retry(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001873static void end_sync_write(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001875 int uptodate = !bio->bi_error;
NeilBrown9f2c9d12011-10-11 16:48:43 +11001876 struct r1bio *r1_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001877 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11001878 struct r1conf *conf = mddev->private;
NeilBrown4367af52011-07-28 11:31:49 +10001879 sector_t first_bad;
1880 int bad_sectors;
NeilBrown854abd72016-06-02 16:19:52 +10001881 struct md_rdev *rdev = conf->mirrors[find_bio_disk(r1_bio, bio)].rdev;
Namhyung Kimba3ae3b2011-10-07 14:22:53 +11001882
NeilBrown6b1117d2006-03-31 02:31:57 -08001883 if (!uptodate) {
NeilBrown57dab0b2010-10-19 10:03:39 +11001884 sector_t sync_blocks = 0;
NeilBrown6b1117d2006-03-31 02:31:57 -08001885 sector_t s = r1_bio->sector;
1886 long sectors_to_go = r1_bio->sectors;
1887 /* make sure these bits doesn't get cleared. */
1888 do {
NeilBrown5e3db642006-07-10 04:44:18 -07001889 bitmap_end_sync(mddev->bitmap, s,
NeilBrown6b1117d2006-03-31 02:31:57 -08001890 &sync_blocks, 1);
1891 s += sync_blocks;
1892 sectors_to_go -= sync_blocks;
1893 } while (sectors_to_go > 0);
NeilBrown854abd72016-06-02 16:19:52 +10001894 set_bit(WriteErrorSeen, &rdev->flags);
1895 if (!test_and_set_bit(WantReplacement, &rdev->flags))
NeilBrown19d67162011-12-23 10:17:57 +11001896 set_bit(MD_RECOVERY_NEEDED, &
1897 mddev->recovery);
NeilBrownd8f05d22011-07-28 11:33:00 +10001898 set_bit(R1BIO_WriteError, &r1_bio->state);
NeilBrown854abd72016-06-02 16:19:52 +10001899 } else if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors,
NeilBrown3a9f28a2011-07-28 11:33:42 +10001900 &first_bad, &bad_sectors) &&
1901 !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
1902 r1_bio->sector,
1903 r1_bio->sectors,
1904 &first_bad, &bad_sectors)
1905 )
NeilBrown4367af52011-07-28 11:31:49 +10001906 set_bit(R1BIO_MadeGood, &r1_bio->state);
NeilBrowne3b97032005-08-04 12:53:34 -07001907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown4367af52011-07-28 11:31:49 +10001909 int s = r1_bio->sectors;
NeilBrownd8f05d22011-07-28 11:33:00 +10001910 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
1911 test_bit(R1BIO_WriteError, &r1_bio->state))
NeilBrown4367af52011-07-28 11:31:49 +10001912 reschedule_retry(r1_bio);
1913 else {
1914 put_buf(r1_bio);
1915 md_done_sync(mddev, s, uptodate);
1916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
NeilBrown3cb03002011-10-11 16:45:26 +11001920static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrownd8f05d22011-07-28 11:33:00 +10001921 int sectors, struct page *page, int rw)
1922{
Mike Christie796a5cf2016-06-05 14:32:07 -05001923 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
NeilBrownd8f05d22011-07-28 11:33:00 +10001924 /* success */
1925 return 1;
NeilBrown19d67162011-12-23 10:17:57 +11001926 if (rw == WRITE) {
NeilBrownd8f05d22011-07-28 11:33:00 +10001927 set_bit(WriteErrorSeen, &rdev->flags);
NeilBrown19d67162011-12-23 10:17:57 +11001928 if (!test_and_set_bit(WantReplacement,
1929 &rdev->flags))
1930 set_bit(MD_RECOVERY_NEEDED, &
1931 rdev->mddev->recovery);
1932 }
NeilBrownd8f05d22011-07-28 11:33:00 +10001933 /* need to record an error - either for the block or the device */
1934 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1935 md_error(rdev->mddev, rdev);
1936 return 0;
1937}
1938
NeilBrown9f2c9d12011-10-11 16:48:43 +11001939static int fix_sync_read_error(struct r1bio *r1_bio)
NeilBrowna68e5872011-05-11 14:40:44 +10001940{
1941 /* Try some synchronous reads of other devices to get
1942 * good data, much like with normal read errors. Only
1943 * read into the pages we already have so we don't
1944 * need to re-issue the read request.
1945 * We don't need to freeze the array, because being in an
1946 * active sync request, there is no normal IO, and
1947 * no overlapping syncs.
NeilBrown06f60382011-07-28 11:31:48 +10001948 * We don't need to check is_badblock() again as we
1949 * made sure that anything with a bad block in range
1950 * will have bi_end_io clear.
NeilBrowna68e5872011-05-11 14:40:44 +10001951 */
NeilBrownfd01b882011-10-11 16:47:53 +11001952 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11001953 struct r1conf *conf = mddev->private;
NeilBrowna68e5872011-05-11 14:40:44 +10001954 struct bio *bio = r1_bio->bios[r1_bio->read_disk];
1955 sector_t sect = r1_bio->sector;
1956 int sectors = r1_bio->sectors;
1957 int idx = 0;
NeilBrown2e52d442016-11-18 16:16:12 +11001958 struct md_rdev *rdev;
1959
1960 rdev = conf->mirrors[r1_bio->read_disk].rdev;
1961 if (test_bit(FailFast, &rdev->flags)) {
1962 /* Don't try recovering from here - just fail it
1963 * ... unless it is the last working device of course */
1964 md_error(mddev, rdev);
1965 if (test_bit(Faulty, &rdev->flags))
1966 /* Don't try to read from here, but make sure
1967 * put_buf does it's thing
1968 */
1969 bio->bi_end_io = end_sync_write;
1970 }
NeilBrowna68e5872011-05-11 14:40:44 +10001971
1972 while(sectors) {
1973 int s = sectors;
1974 int d = r1_bio->read_disk;
1975 int success = 0;
NeilBrown78d7f5f2011-05-11 14:48:56 +10001976 int start;
NeilBrowna68e5872011-05-11 14:40:44 +10001977
1978 if (s > (PAGE_SIZE>>9))
1979 s = PAGE_SIZE >> 9;
1980 do {
1981 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1982 /* No rcu protection needed here devices
1983 * can only be removed when no resync is
1984 * active, and resync is currently active
1985 */
1986 rdev = conf->mirrors[d].rdev;
Namhyung Kim9d3d8012011-07-27 11:00:36 +10001987 if (sync_page_io(rdev, sect, s<<9,
NeilBrowna68e5872011-05-11 14:40:44 +10001988 bio->bi_io_vec[idx].bv_page,
Mike Christie796a5cf2016-06-05 14:32:07 -05001989 REQ_OP_READ, 0, false)) {
NeilBrowna68e5872011-05-11 14:40:44 +10001990 success = 1;
1991 break;
1992 }
1993 }
1994 d++;
NeilBrown8f19ccb2011-12-23 10:17:56 +11001995 if (d == conf->raid_disks * 2)
NeilBrowna68e5872011-05-11 14:40:44 +10001996 d = 0;
1997 } while (!success && d != r1_bio->read_disk);
1998
NeilBrown78d7f5f2011-05-11 14:48:56 +10001999 if (!success) {
NeilBrowna68e5872011-05-11 14:40:44 +10002000 char b[BDEVNAME_SIZE];
NeilBrown3a9f28a2011-07-28 11:33:42 +10002001 int abort = 0;
2002 /* Cannot read from anywhere, this block is lost.
2003 * Record a bad block on each device. If that doesn't
2004 * work just disable and interrupt the recovery.
2005 * Don't fail devices as that won't really help.
2006 */
NeilBrown1d41c212016-11-02 14:16:50 +11002007 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
2008 mdname(mddev),
2009 bdevname(bio->bi_bdev, b),
2010 (unsigned long long)r1_bio->sector);
NeilBrown8f19ccb2011-12-23 10:17:56 +11002011 for (d = 0; d < conf->raid_disks * 2; d++) {
NeilBrown3a9f28a2011-07-28 11:33:42 +10002012 rdev = conf->mirrors[d].rdev;
2013 if (!rdev || test_bit(Faulty, &rdev->flags))
2014 continue;
2015 if (!rdev_set_badblocks(rdev, sect, s, 0))
2016 abort = 1;
2017 }
2018 if (abort) {
NeilBrownd890fa22011-10-26 11:54:39 +11002019 conf->recovery_disabled =
2020 mddev->recovery_disabled;
NeilBrown3a9f28a2011-07-28 11:33:42 +10002021 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2022 md_done_sync(mddev, r1_bio->sectors, 0);
2023 put_buf(r1_bio);
2024 return 0;
2025 }
2026 /* Try next page */
2027 sectors -= s;
2028 sect += s;
2029 idx++;
2030 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10002031 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10002032
2033 start = d;
2034 /* write it back and re-read */
2035 while (d != r1_bio->read_disk) {
2036 if (d == 0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002037 d = conf->raid_disks * 2;
NeilBrown78d7f5f2011-05-11 14:48:56 +10002038 d--;
2039 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
2040 continue;
2041 rdev = conf->mirrors[d].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10002042 if (r1_sync_page_io(rdev, sect, s,
2043 bio->bi_io_vec[idx].bv_page,
2044 WRITE) == 0) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10002045 r1_bio->bios[d]->bi_end_io = NULL;
2046 rdev_dec_pending(rdev, mddev);
Namhyung Kim9d3d8012011-07-27 11:00:36 +10002047 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10002048 }
2049 d = start;
2050 while (d != r1_bio->read_disk) {
2051 if (d == 0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002052 d = conf->raid_disks * 2;
NeilBrown78d7f5f2011-05-11 14:48:56 +10002053 d--;
2054 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
2055 continue;
2056 rdev = conf->mirrors[d].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10002057 if (r1_sync_page_io(rdev, sect, s,
2058 bio->bi_io_vec[idx].bv_page,
2059 READ) != 0)
Namhyung Kim9d3d8012011-07-27 11:00:36 +10002060 atomic_add(s, &rdev->corrected_errors);
NeilBrown78d7f5f2011-05-11 14:48:56 +10002061 }
NeilBrowna68e5872011-05-11 14:40:44 +10002062 sectors -= s;
2063 sect += s;
2064 idx ++;
2065 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10002066 set_bit(R1BIO_Uptodate, &r1_bio->state);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002067 bio->bi_error = 0;
NeilBrowna68e5872011-05-11 14:40:44 +10002068 return 1;
2069}
2070
NeilBrownc95e6382014-09-09 13:54:11 +10002071static void process_checks(struct r1bio *r1_bio)
NeilBrowna68e5872011-05-11 14:40:44 +10002072{
2073 /* We have read all readable devices. If we haven't
2074 * got the block, then there is no hope left.
2075 * If we have, then we want to do a comparison
2076 * and skip the write if everything is the same.
2077 * If any blocks failed to read, then we need to
2078 * attempt an over-write
2079 */
NeilBrownfd01b882011-10-11 16:47:53 +11002080 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11002081 struct r1conf *conf = mddev->private;
NeilBrowna68e5872011-05-11 14:40:44 +10002082 int primary;
2083 int i;
majianpengf4380a92012-04-12 16:04:47 +10002084 int vcnt;
NeilBrowna68e5872011-05-11 14:40:44 +10002085
NeilBrown30bc9b52013-07-17 15:19:29 +10002086 /* Fix variable parts of all bios */
2087 vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9);
2088 for (i = 0; i < conf->raid_disks * 2; i++) {
2089 int j;
2090 int size;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002091 int error;
NeilBrown30bc9b52013-07-17 15:19:29 +10002092 struct bio *b = r1_bio->bios[i];
2093 if (b->bi_end_io != end_sync_read)
2094 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002095 /* fixup the bio for reuse, but preserve errno */
2096 error = b->bi_error;
NeilBrown30bc9b52013-07-17 15:19:29 +10002097 bio_reset(b);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002098 b->bi_error = error;
NeilBrown30bc9b52013-07-17 15:19:29 +10002099 b->bi_vcnt = vcnt;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002100 b->bi_iter.bi_size = r1_bio->sectors << 9;
2101 b->bi_iter.bi_sector = r1_bio->sector +
NeilBrown30bc9b52013-07-17 15:19:29 +10002102 conf->mirrors[i].rdev->data_offset;
2103 b->bi_bdev = conf->mirrors[i].rdev->bdev;
2104 b->bi_end_io = end_sync_read;
2105 b->bi_private = r1_bio;
2106
Kent Overstreet4f024f32013-10-11 15:44:27 -07002107 size = b->bi_iter.bi_size;
NeilBrown30bc9b52013-07-17 15:19:29 +10002108 for (j = 0; j < vcnt ; j++) {
2109 struct bio_vec *bi;
2110 bi = &b->bi_io_vec[j];
2111 bi->bv_offset = 0;
2112 if (size > PAGE_SIZE)
2113 bi->bv_len = PAGE_SIZE;
2114 else
2115 bi->bv_len = size;
2116 size -= PAGE_SIZE;
2117 }
2118 }
NeilBrown8f19ccb2011-12-23 10:17:56 +11002119 for (primary = 0; primary < conf->raid_disks * 2; primary++)
NeilBrowna68e5872011-05-11 14:40:44 +10002120 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002121 !r1_bio->bios[primary]->bi_error) {
NeilBrowna68e5872011-05-11 14:40:44 +10002122 r1_bio->bios[primary]->bi_end_io = NULL;
2123 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
2124 break;
2125 }
2126 r1_bio->read_disk = primary;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002127 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10002128 int j;
NeilBrown78d7f5f2011-05-11 14:48:56 +10002129 struct bio *pbio = r1_bio->bios[primary];
2130 struct bio *sbio = r1_bio->bios[i];
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002131 int error = sbio->bi_error;
NeilBrowna68e5872011-05-11 14:40:44 +10002132
Kent Overstreet2aabaa62012-09-11 11:26:12 -07002133 if (sbio->bi_end_io != end_sync_read)
NeilBrown78d7f5f2011-05-11 14:48:56 +10002134 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002135 /* Now we can 'fixup' the error value */
2136 sbio->bi_error = 0;
NeilBrowna68e5872011-05-11 14:40:44 +10002137
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002138 if (!error) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10002139 for (j = vcnt; j-- ; ) {
2140 struct page *p, *s;
2141 p = pbio->bi_io_vec[j].bv_page;
2142 s = sbio->bi_io_vec[j].bv_page;
2143 if (memcmp(page_address(p),
2144 page_address(s),
NeilBrown5020ad72012-04-02 01:39:05 +10002145 sbio->bi_io_vec[j].bv_len))
NeilBrown78d7f5f2011-05-11 14:48:56 +10002146 break;
NeilBrowna68e5872011-05-11 14:40:44 +10002147 }
NeilBrown78d7f5f2011-05-11 14:48:56 +10002148 } else
2149 j = 0;
2150 if (j >= 0)
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002151 atomic64_add(r1_bio->sectors, &mddev->resync_mismatches);
NeilBrown78d7f5f2011-05-11 14:48:56 +10002152 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002153 && !error)) {
NeilBrown78d7f5f2011-05-11 14:48:56 +10002154 /* No need to write to this device. */
2155 sbio->bi_end_io = NULL;
2156 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
2157 continue;
NeilBrowna68e5872011-05-11 14:40:44 +10002158 }
Kent Overstreetd3b45c22012-09-10 13:49:33 -07002159
2160 bio_copy_data(sbio, pbio);
NeilBrown78d7f5f2011-05-11 14:48:56 +10002161 }
NeilBrowna68e5872011-05-11 14:40:44 +10002162}
2163
NeilBrown9f2c9d12011-10-11 16:48:43 +11002164static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
NeilBrowne8096362011-10-11 16:49:05 +11002166 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 int i;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002168 int disks = conf->raid_disks * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 struct bio *bio, *wbio;
2170
2171 bio = r1_bio->bios[r1_bio->read_disk];
2172
NeilBrowna68e5872011-05-11 14:40:44 +10002173 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
2174 /* ouch - failed to read all of that. */
2175 if (!fix_sync_read_error(r1_bio))
2176 return;
NeilBrown7ca78d52011-05-11 14:50:37 +10002177
2178 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrownc95e6382014-09-09 13:54:11 +10002179 process_checks(r1_bio);
2180
NeilBrownd11c1712006-01-06 00:20:26 -08002181 /*
2182 * schedule writes
2183 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 atomic_set(&r1_bio->remaining, 1);
2185 for (i = 0; i < disks ; i++) {
2186 wbio = r1_bio->bios[i];
NeilBrown3e198f72006-01-06 00:20:21 -08002187 if (wbio->bi_end_io == NULL ||
2188 (wbio->bi_end_io == end_sync_read &&
2189 (i == r1_bio->read_disk ||
2190 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 continue;
2192
Mike Christie796a5cf2016-06-05 14:32:07 -05002193 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
NeilBrown212e7eb2016-11-18 16:16:12 +11002194 if (test_bit(FailFast, &conf->mirrors[i].rdev->flags))
2195 wbio->bi_opf |= MD_FAILFAST;
2196
NeilBrown3e198f72006-01-06 00:20:21 -08002197 wbio->bi_end_io = end_sync_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 atomic_inc(&r1_bio->remaining);
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08002199 md_sync_acct(conf->mirrors[i].rdev->bdev, bio_sectors(wbio));
NeilBrown191ea9b2005-06-21 17:17:23 -07002200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 generic_make_request(wbio);
2202 }
2203
2204 if (atomic_dec_and_test(&r1_bio->remaining)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002205 /* if we're here, all write(s) have completed, so clean up */
NeilBrown58e94ae2012-07-19 15:59:18 +10002206 int s = r1_bio->sectors;
2207 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2208 test_bit(R1BIO_WriteError, &r1_bio->state))
2209 reschedule_retry(r1_bio);
2210 else {
2211 put_buf(r1_bio);
2212 md_done_sync(mddev, s, 1);
2213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215}
2216
2217/*
2218 * This is a kernel thread which:
2219 *
2220 * 1. Retries failed read operations on working mirrors.
2221 * 2. Updates the raid superblock when problems encounter.
NeilBrownd2eb35a2011-07-28 11:31:48 +10002222 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 */
2224
NeilBrowne8096362011-10-11 16:49:05 +11002225static void fix_read_error(struct r1conf *conf, int read_disk,
NeilBrown867868f2006-10-03 01:15:51 -07002226 sector_t sect, int sectors)
2227{
NeilBrownfd01b882011-10-11 16:47:53 +11002228 struct mddev *mddev = conf->mddev;
NeilBrown867868f2006-10-03 01:15:51 -07002229 while(sectors) {
2230 int s = sectors;
2231 int d = read_disk;
2232 int success = 0;
2233 int start;
NeilBrown3cb03002011-10-11 16:45:26 +11002234 struct md_rdev *rdev;
NeilBrown867868f2006-10-03 01:15:51 -07002235
2236 if (s > (PAGE_SIZE>>9))
2237 s = PAGE_SIZE >> 9;
2238
2239 do {
NeilBrownd2eb35a2011-07-28 11:31:48 +10002240 sector_t first_bad;
2241 int bad_sectors;
2242
NeilBrown707a6a42016-06-02 16:19:52 +10002243 rcu_read_lock();
2244 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002245 if (rdev &&
majianpengda8840a2012-05-22 13:55:03 +10002246 (test_bit(In_sync, &rdev->flags) ||
2247 (!test_bit(Faulty, &rdev->flags) &&
2248 rdev->recovery_offset >= sect + s)) &&
NeilBrownd2eb35a2011-07-28 11:31:48 +10002249 is_badblock(rdev, sect, s,
NeilBrown707a6a42016-06-02 16:19:52 +10002250 &first_bad, &bad_sectors) == 0) {
2251 atomic_inc(&rdev->nr_pending);
2252 rcu_read_unlock();
2253 if (sync_page_io(rdev, sect, s<<9,
Mike Christie796a5cf2016-06-05 14:32:07 -05002254 conf->tmppage, REQ_OP_READ, 0, false))
NeilBrown707a6a42016-06-02 16:19:52 +10002255 success = 1;
2256 rdev_dec_pending(rdev, mddev);
2257 if (success)
2258 break;
2259 } else
2260 rcu_read_unlock();
2261 d++;
2262 if (d == conf->raid_disks * 2)
2263 d = 0;
NeilBrown867868f2006-10-03 01:15:51 -07002264 } while (!success && d != read_disk);
2265
2266 if (!success) {
NeilBrownd8f05d22011-07-28 11:33:00 +10002267 /* Cannot read from anywhere - mark it bad */
NeilBrown3cb03002011-10-11 16:45:26 +11002268 struct md_rdev *rdev = conf->mirrors[read_disk].rdev;
NeilBrownd8f05d22011-07-28 11:33:00 +10002269 if (!rdev_set_badblocks(rdev, sect, s, 0))
2270 md_error(mddev, rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002271 break;
2272 }
2273 /* write it back and re-read */
2274 start = d;
2275 while (d != read_disk) {
2276 if (d==0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002277 d = conf->raid_disks * 2;
NeilBrown867868f2006-10-03 01:15:51 -07002278 d--;
NeilBrown707a6a42016-06-02 16:19:52 +10002279 rcu_read_lock();
2280 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002281 if (rdev &&
NeilBrown707a6a42016-06-02 16:19:52 +10002282 !test_bit(Faulty, &rdev->flags)) {
2283 atomic_inc(&rdev->nr_pending);
2284 rcu_read_unlock();
NeilBrownd8f05d22011-07-28 11:33:00 +10002285 r1_sync_page_io(rdev, sect, s,
2286 conf->tmppage, WRITE);
NeilBrown707a6a42016-06-02 16:19:52 +10002287 rdev_dec_pending(rdev, mddev);
2288 } else
2289 rcu_read_unlock();
NeilBrown867868f2006-10-03 01:15:51 -07002290 }
2291 d = start;
2292 while (d != read_disk) {
2293 char b[BDEVNAME_SIZE];
2294 if (d==0)
NeilBrown8f19ccb2011-12-23 10:17:56 +11002295 d = conf->raid_disks * 2;
NeilBrown867868f2006-10-03 01:15:51 -07002296 d--;
NeilBrown707a6a42016-06-02 16:19:52 +10002297 rcu_read_lock();
2298 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown867868f2006-10-03 01:15:51 -07002299 if (rdev &&
NeilBrownb8cb6b42014-09-18 11:09:04 +10002300 !test_bit(Faulty, &rdev->flags)) {
NeilBrown707a6a42016-06-02 16:19:52 +10002301 atomic_inc(&rdev->nr_pending);
2302 rcu_read_unlock();
NeilBrownd8f05d22011-07-28 11:33:00 +10002303 if (r1_sync_page_io(rdev, sect, s,
2304 conf->tmppage, READ)) {
NeilBrown867868f2006-10-03 01:15:51 -07002305 atomic_add(s, &rdev->corrected_errors);
NeilBrown1d41c212016-11-02 14:16:50 +11002306 pr_info("md/raid1:%s: read error corrected (%d sectors at %llu on %s)\n",
2307 mdname(mddev), s,
2308 (unsigned long long)(sect +
2309 rdev->data_offset),
2310 bdevname(rdev->bdev, b));
NeilBrown867868f2006-10-03 01:15:51 -07002311 }
NeilBrown707a6a42016-06-02 16:19:52 +10002312 rdev_dec_pending(rdev, mddev);
2313 } else
2314 rcu_read_unlock();
NeilBrown867868f2006-10-03 01:15:51 -07002315 }
2316 sectors -= s;
2317 sect += s;
2318 }
2319}
2320
NeilBrown9f2c9d12011-10-11 16:48:43 +11002321static int narrow_write_error(struct r1bio *r1_bio, int i)
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002322{
NeilBrownfd01b882011-10-11 16:47:53 +11002323 struct mddev *mddev = r1_bio->mddev;
NeilBrowne8096362011-10-11 16:49:05 +11002324 struct r1conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002325 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002326
2327 /* bio has the data to be written to device 'i' where
2328 * we just recently had a write error.
2329 * We repeatedly clone the bio and trim down to one block,
2330 * then try the write. Where the write fails we record
2331 * a bad block.
2332 * It is conceivable that the bio doesn't exactly align with
2333 * blocks. We must handle this somehow.
2334 *
2335 * We currently own a reference on the rdev.
2336 */
2337
2338 int block_sectors;
2339 sector_t sector;
2340 int sectors;
2341 int sect_to_write = r1_bio->sectors;
2342 int ok = 1;
2343
2344 if (rdev->badblocks.shift < 0)
2345 return 0;
2346
Nate Daileyab713cd2015-02-12 12:02:09 -05002347 block_sectors = roundup(1 << rdev->badblocks.shift,
2348 bdev_logical_block_size(rdev->bdev) >> 9);
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002349 sector = r1_bio->sector;
2350 sectors = ((sector + block_sectors)
2351 & ~(sector_t)(block_sectors - 1))
2352 - sector;
2353
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002354 while (sect_to_write) {
2355 struct bio *wbio;
2356 if (sectors > sect_to_write)
2357 sectors = sect_to_write;
2358 /* Write at 'sector' for 'sectors'*/
2359
Kent Overstreetb7838632012-09-10 15:17:11 -07002360 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
2361 unsigned vcnt = r1_bio->behind_page_count;
2362 struct bio_vec *vec = r1_bio->behind_bvecs;
2363
2364 while (!vec->bv_page) {
2365 vec++;
2366 vcnt--;
2367 }
2368
2369 wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev);
2370 memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec));
2371
2372 wbio->bi_vcnt = vcnt;
2373 } else {
Ming Leid7a10302017-02-14 23:29:03 +08002374 wbio = bio_clone_fast(r1_bio->master_bio, GFP_NOIO,
2375 mddev->bio_set);
Kent Overstreetb7838632012-09-10 15:17:11 -07002376 }
2377
Mike Christie796a5cf2016-06-05 14:32:07 -05002378 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002379 wbio->bi_iter.bi_sector = r1_bio->sector;
2380 wbio->bi_iter.bi_size = r1_bio->sectors << 9;
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002381
Kent Overstreet6678d832013-08-07 11:14:32 -07002382 bio_trim(wbio, sector - r1_bio->sector, sectors);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002383 wbio->bi_iter.bi_sector += rdev->data_offset;
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002384 wbio->bi_bdev = rdev->bdev;
Mike Christie4e49ea42016-06-05 14:31:41 -05002385
2386 if (submit_bio_wait(wbio) < 0)
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002387 /* failure! */
2388 ok = rdev_set_badblocks(rdev, sector,
2389 sectors, 0)
2390 && ok;
2391
2392 bio_put(wbio);
2393 sect_to_write -= sectors;
2394 sector += sectors;
2395 sectors = block_sectors;
2396 }
2397 return ok;
2398}
2399
NeilBrowne8096362011-10-11 16:49:05 +11002400static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002401{
2402 int m;
2403 int s = r1_bio->sectors;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002404 for (m = 0; m < conf->raid_disks * 2 ; m++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002405 struct md_rdev *rdev = conf->mirrors[m].rdev;
NeilBrown62096bc2011-07-28 11:38:13 +10002406 struct bio *bio = r1_bio->bios[m];
2407 if (bio->bi_end_io == NULL)
2408 continue;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002409 if (!bio->bi_error &&
NeilBrown62096bc2011-07-28 11:38:13 +10002410 test_bit(R1BIO_MadeGood, &r1_bio->state)) {
NeilBrownc6563a82012-05-21 09:27:00 +10002411 rdev_clear_badblocks(rdev, r1_bio->sector, s, 0);
NeilBrown62096bc2011-07-28 11:38:13 +10002412 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002413 if (bio->bi_error &&
NeilBrown62096bc2011-07-28 11:38:13 +10002414 test_bit(R1BIO_WriteError, &r1_bio->state)) {
2415 if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
2416 md_error(conf->mddev, rdev);
2417 }
2418 }
2419 put_buf(r1_bio);
2420 md_done_sync(conf->mddev, s, 1);
2421}
2422
NeilBrowne8096362011-10-11 16:49:05 +11002423static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002424{
colyli@suse.defd768632017-02-18 03:05:56 +08002425 int m, idx;
NeilBrown55ce74d2015-08-14 11:11:10 +10002426 bool fail = false;
colyli@suse.defd768632017-02-18 03:05:56 +08002427
NeilBrown8f19ccb2011-12-23 10:17:56 +11002428 for (m = 0; m < conf->raid_disks * 2 ; m++)
NeilBrown62096bc2011-07-28 11:38:13 +10002429 if (r1_bio->bios[m] == IO_MADE_GOOD) {
NeilBrown3cb03002011-10-11 16:45:26 +11002430 struct md_rdev *rdev = conf->mirrors[m].rdev;
NeilBrown62096bc2011-07-28 11:38:13 +10002431 rdev_clear_badblocks(rdev,
2432 r1_bio->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10002433 r1_bio->sectors, 0);
NeilBrown62096bc2011-07-28 11:38:13 +10002434 rdev_dec_pending(rdev, conf->mddev);
2435 } else if (r1_bio->bios[m] != NULL) {
2436 /* This drive got a write error. We need to
2437 * narrow down and record precise write
2438 * errors.
2439 */
NeilBrown55ce74d2015-08-14 11:11:10 +10002440 fail = true;
NeilBrown62096bc2011-07-28 11:38:13 +10002441 if (!narrow_write_error(r1_bio, m)) {
2442 md_error(conf->mddev,
2443 conf->mirrors[m].rdev);
2444 /* an I/O failed, we can't clear the bitmap */
2445 set_bit(R1BIO_Degraded, &r1_bio->state);
2446 }
2447 rdev_dec_pending(conf->mirrors[m].rdev,
2448 conf->mddev);
2449 }
NeilBrown55ce74d2015-08-14 11:11:10 +10002450 if (fail) {
2451 spin_lock_irq(&conf->device_lock);
2452 list_add(&r1_bio->retry_list, &conf->bio_end_io_list);
colyli@suse.defd768632017-02-18 03:05:56 +08002453 idx = sector_to_idx(r1_bio->sector);
colyli@suse.de824e47d2017-02-18 03:05:57 +08002454 atomic_inc(&conf->nr_queued[idx]);
NeilBrown55ce74d2015-08-14 11:11:10 +10002455 spin_unlock_irq(&conf->device_lock);
colyli@suse.de824e47d2017-02-18 03:05:57 +08002456 /*
2457 * In case freeze_array() is waiting for condition
2458 * get_unqueued_pending() == extra to be true.
2459 */
2460 wake_up(&conf->wait_barrier);
NeilBrown55ce74d2015-08-14 11:11:10 +10002461 md_wakeup_thread(conf->mddev->thread);
NeilBrownbd8688a2015-10-24 16:02:16 +11002462 } else {
2463 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2464 close_write(r1_bio);
NeilBrown55ce74d2015-08-14 11:11:10 +10002465 raid_end_bio_io(r1_bio);
NeilBrownbd8688a2015-10-24 16:02:16 +11002466 }
NeilBrown62096bc2011-07-28 11:38:13 +10002467}
2468
NeilBrowne8096362011-10-11 16:49:05 +11002469static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
NeilBrown62096bc2011-07-28 11:38:13 +10002470{
2471 int disk;
2472 int max_sectors;
NeilBrownfd01b882011-10-11 16:47:53 +11002473 struct mddev *mddev = conf->mddev;
NeilBrown62096bc2011-07-28 11:38:13 +10002474 struct bio *bio;
2475 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11002476 struct md_rdev *rdev;
NeilBrown109e3762016-11-18 13:22:04 +11002477 dev_t bio_dev;
2478 sector_t bio_sector;
NeilBrown62096bc2011-07-28 11:38:13 +10002479
2480 clear_bit(R1BIO_ReadError, &r1_bio->state);
2481 /* we got a read error. Maybe the drive is bad. Maybe just
2482 * the block and we can fix it.
2483 * We freeze all other IO, and try reading the block from
2484 * other devices. When we find one, we re-write
2485 * and check it that fixes the read error.
2486 * This is all done synchronously while the array is
2487 * frozen
2488 */
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002489
2490 bio = r1_bio->bios[r1_bio->read_disk];
2491 bdevname(bio->bi_bdev, b);
NeilBrown109e3762016-11-18 13:22:04 +11002492 bio_dev = bio->bi_bdev->bd_dev;
2493 bio_sector = conf->mirrors[r1_bio->read_disk].rdev->data_offset + r1_bio->sector;
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002494 bio_put(bio);
2495 r1_bio->bios[r1_bio->read_disk] = NULL;
2496
NeilBrown2e52d442016-11-18 16:16:12 +11002497 rdev = conf->mirrors[r1_bio->read_disk].rdev;
2498 if (mddev->ro == 0
2499 && !test_bit(FailFast, &rdev->flags)) {
NeilBrowne2d59922013-06-12 11:01:22 +10002500 freeze_array(conf, 1);
NeilBrown62096bc2011-07-28 11:38:13 +10002501 fix_read_error(conf, r1_bio->read_disk,
2502 r1_bio->sector, r1_bio->sectors);
2503 unfreeze_array(conf);
Tomasz Majchrzak7449f692016-10-28 14:45:58 +02002504 } else {
2505 r1_bio->bios[r1_bio->read_disk] = IO_BLOCKED;
2506 }
2507
NeilBrown2e52d442016-11-18 16:16:12 +11002508 rdev_dec_pending(rdev, conf->mddev);
NeilBrown62096bc2011-07-28 11:38:13 +10002509
NeilBrown62096bc2011-07-28 11:38:13 +10002510read_more:
2511 disk = read_balance(conf, r1_bio, &max_sectors);
2512 if (disk == -1) {
NeilBrown1d41c212016-11-02 14:16:50 +11002513 pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n",
2514 mdname(mddev), b, (unsigned long long)r1_bio->sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002515 raid_end_bio_io(r1_bio);
2516 } else {
2517 const unsigned long do_sync
Jens Axboe1eff9d32016-08-05 15:35:16 -06002518 = r1_bio->master_bio->bi_opf & REQ_SYNC;
NeilBrown62096bc2011-07-28 11:38:13 +10002519 r1_bio->read_disk = disk;
Ming Leid7a10302017-02-14 23:29:03 +08002520 bio = bio_clone_fast(r1_bio->master_bio, GFP_NOIO,
2521 mddev->bio_set);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002522 bio_trim(bio, r1_bio->sector - bio->bi_iter.bi_sector,
2523 max_sectors);
NeilBrown62096bc2011-07-28 11:38:13 +10002524 r1_bio->bios[r1_bio->read_disk] = bio;
2525 rdev = conf->mirrors[disk].rdev;
NeilBrown1d41c212016-11-02 14:16:50 +11002526 pr_info_ratelimited("md/raid1:%s: redirecting sector %llu to other mirror: %s\n",
2527 mdname(mddev),
2528 (unsigned long long)r1_bio->sector,
2529 bdevname(rdev->bdev, b));
Kent Overstreet4f024f32013-10-11 15:44:27 -07002530 bio->bi_iter.bi_sector = r1_bio->sector + rdev->data_offset;
NeilBrown62096bc2011-07-28 11:38:13 +10002531 bio->bi_bdev = rdev->bdev;
2532 bio->bi_end_io = raid1_end_read_request;
Mike Christie796a5cf2016-06-05 14:32:07 -05002533 bio_set_op_attrs(bio, REQ_OP_READ, do_sync);
NeilBrown2e52d442016-11-18 16:16:12 +11002534 if (test_bit(FailFast, &rdev->flags) &&
2535 test_bit(R1BIO_FailFast, &r1_bio->state))
2536 bio->bi_opf |= MD_FAILFAST;
NeilBrown62096bc2011-07-28 11:38:13 +10002537 bio->bi_private = r1_bio;
2538 if (max_sectors < r1_bio->sectors) {
2539 /* Drat - have to split this up more */
2540 struct bio *mbio = r1_bio->master_bio;
2541 int sectors_handled = (r1_bio->sector + max_sectors
Kent Overstreet4f024f32013-10-11 15:44:27 -07002542 - mbio->bi_iter.bi_sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002543 r1_bio->sectors = max_sectors;
2544 spin_lock_irq(&conf->device_lock);
2545 if (mbio->bi_phys_segments == 0)
2546 mbio->bi_phys_segments = 2;
2547 else
2548 mbio->bi_phys_segments++;
2549 spin_unlock_irq(&conf->device_lock);
NeilBrown109e3762016-11-18 13:22:04 +11002550 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2551 bio, bio_dev, bio_sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002552 generic_make_request(bio);
2553 bio = NULL;
2554
colyli@suse.defd768632017-02-18 03:05:56 +08002555 r1_bio = alloc_r1bio(mddev, mbio, sectors_handled);
NeilBrown62096bc2011-07-28 11:38:13 +10002556 set_bit(R1BIO_ReadError, &r1_bio->state);
NeilBrown62096bc2011-07-28 11:38:13 +10002557
2558 goto read_more;
NeilBrown109e3762016-11-18 13:22:04 +11002559 } else {
2560 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev),
2561 bio, bio_dev, bio_sector);
NeilBrown62096bc2011-07-28 11:38:13 +10002562 generic_make_request(bio);
NeilBrown109e3762016-11-18 13:22:04 +11002563 }
NeilBrown62096bc2011-07-28 11:38:13 +10002564 }
2565}
2566
Shaohua Li4ed87312012-10-11 13:34:00 +11002567static void raid1d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
Shaohua Li4ed87312012-10-11 13:34:00 +11002569 struct mddev *mddev = thread->mddev;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002570 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 unsigned long flags;
NeilBrowne8096362011-10-11 16:49:05 +11002572 struct r1conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002574 struct blk_plug plug;
colyli@suse.defd768632017-02-18 03:05:56 +08002575 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 md_check_recovery(mddev);
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002578
NeilBrown55ce74d2015-08-14 11:11:10 +10002579 if (!list_empty_careful(&conf->bio_end_io_list) &&
Shaohua Li29530792016-12-08 15:48:19 -08002580 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrown55ce74d2015-08-14 11:11:10 +10002581 LIST_HEAD(tmp);
2582 spin_lock_irqsave(&conf->device_lock, flags);
colyli@suse.defd768632017-02-18 03:05:56 +08002583 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
2584 list_splice_init(&conf->bio_end_io_list, &tmp);
NeilBrown55ce74d2015-08-14 11:11:10 +10002585 spin_unlock_irqrestore(&conf->device_lock, flags);
2586 while (!list_empty(&tmp)) {
Mikulas Patockaa4527442015-10-01 15:17:43 -04002587 r1_bio = list_first_entry(&tmp, struct r1bio,
2588 retry_list);
NeilBrown55ce74d2015-08-14 11:11:10 +10002589 list_del(&r1_bio->retry_list);
colyli@suse.defd768632017-02-18 03:05:56 +08002590 idx = sector_to_idx(r1_bio->sector);
colyli@suse.de824e47d2017-02-18 03:05:57 +08002591 atomic_dec(&conf->nr_queued[idx]);
NeilBrownbd8688a2015-10-24 16:02:16 +11002592 if (mddev->degraded)
2593 set_bit(R1BIO_Degraded, &r1_bio->state);
2594 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2595 close_write(r1_bio);
NeilBrown55ce74d2015-08-14 11:11:10 +10002596 raid_end_bio_io(r1_bio);
2597 }
2598 }
2599
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002600 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002602
NeilBrown0021b7b2012-07-31 09:08:14 +02002603 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002606 if (list_empty(head)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002607 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002609 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002610 r1_bio = list_entry(head->prev, struct r1bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 list_del(head->prev);
colyli@suse.defd768632017-02-18 03:05:56 +08002612 idx = sector_to_idx(r1_bio->sector);
colyli@suse.de824e47d2017-02-18 03:05:57 +08002613 atomic_dec(&conf->nr_queued[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 spin_unlock_irqrestore(&conf->device_lock, flags);
2615
2616 mddev = r1_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002617 conf = mddev->private;
NeilBrown4367af52011-07-28 11:31:49 +10002618 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
NeilBrownd8f05d22011-07-28 11:33:00 +10002619 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
NeilBrown62096bc2011-07-28 11:38:13 +10002620 test_bit(R1BIO_WriteError, &r1_bio->state))
2621 handle_sync_write_finished(conf, r1_bio);
2622 else
NeilBrown4367af52011-07-28 11:31:49 +10002623 sync_request_write(mddev, r1_bio);
NeilBrowncd5ff9a12011-07-28 11:32:41 +10002624 } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
NeilBrown62096bc2011-07-28 11:38:13 +10002625 test_bit(R1BIO_WriteError, &r1_bio->state))
2626 handle_write_finished(conf, r1_bio);
2627 else if (test_bit(R1BIO_ReadError, &r1_bio->state))
2628 handle_read_error(conf, r1_bio);
2629 else
NeilBrownd2eb35a2011-07-28 11:31:48 +10002630 /* just a partial read to be scheduled from separate
2631 * context
2632 */
2633 generic_make_request(r1_bio->bios[r1_bio->read_disk]);
NeilBrown62096bc2011-07-28 11:38:13 +10002634
NeilBrown1d9d5242009-10-16 15:55:32 +11002635 cond_resched();
Shaohua Li29530792016-12-08 15:48:19 -08002636 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
NeilBrownde393cd2011-07-28 11:31:48 +10002637 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002639 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640}
2641
NeilBrowne8096362011-10-11 16:49:05 +11002642static int init_resync(struct r1conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643{
2644 int buffs;
2645
2646 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhenn9e77c482006-04-01 01:08:49 +02002647 BUG_ON(conf->r1buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
2649 conf->poolinfo);
2650 if (!conf->r1buf_pool)
2651 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 return 0;
2653}
2654
2655/*
2656 * perform a "sync" on one "block"
2657 *
2658 * We need to make sure that no normal I/O request - particularly write
2659 * requests - conflict with active sync requests.
2660 *
2661 * This is achieved by tracking pending requests and a 'barrier' concept
2662 * that can be installed to exclude normal IO requests.
2663 */
2664
Shaohua Li849674e2016-01-20 13:52:20 -08002665static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr,
2666 int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
NeilBrowne8096362011-10-11 16:49:05 +11002668 struct r1conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002669 struct r1bio *r1_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 struct bio *bio;
2671 sector_t max_sector, nr_sectors;
NeilBrown3e198f72006-01-06 00:20:21 -08002672 int disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 int i;
NeilBrown3e198f72006-01-06 00:20:21 -08002674 int wonly = -1;
2675 int write_targets = 0, read_targets = 0;
NeilBrown57dab0b2010-10-19 10:03:39 +11002676 sector_t sync_blocks;
NeilBrowne3b97032005-08-04 12:53:34 -07002677 int still_degraded = 0;
NeilBrown06f60382011-07-28 11:31:48 +10002678 int good_sectors = RESYNC_SECTORS;
2679 int min_bad = 0; /* number of sectors that are bad in all devices */
colyli@suse.defd768632017-02-18 03:05:56 +08002680 int idx = sector_to_idx(sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
2682 if (!conf->r1buf_pool)
2683 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Andre Noll58c0fed2009-03-31 14:33:13 +11002686 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 if (sector_nr >= max_sector) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002688 /* If we aborted, we need to abort the
2689 * sync on the 'current' bitmap chunk (there will
2690 * only be one in raid1 resync.
2691 * We can find the current addess in mddev->curr_resync
2692 */
NeilBrown6a806c52005-07-15 03:56:35 -07002693 if (mddev->curr_resync < max_sector) /* aborted */
2694 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
NeilBrown191ea9b2005-06-21 17:17:23 -07002695 &sync_blocks, 1);
NeilBrown6a806c52005-07-15 03:56:35 -07002696 else /* completed sync */
NeilBrown191ea9b2005-06-21 17:17:23 -07002697 conf->fullsync = 0;
NeilBrown6a806c52005-07-15 03:56:35 -07002698
2699 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 close_sync(conf);
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002701
2702 if (mddev_is_clustered(mddev)) {
2703 conf->cluster_sync_low = 0;
2704 conf->cluster_sync_high = 0;
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 return 0;
2707 }
2708
NeilBrown07d84d102006-06-26 00:27:56 -07002709 if (mddev->bitmap == NULL &&
2710 mddev->recovery_cp == MaxSector &&
NeilBrown6394cca2006-08-27 01:23:50 -07002711 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown07d84d102006-06-26 00:27:56 -07002712 conf->fullsync == 0) {
2713 *skipped = 1;
2714 return max_sector - sector_nr;
2715 }
NeilBrown6394cca2006-08-27 01:23:50 -07002716 /* before building a request, check if we can skip these blocks..
2717 * This call the bitmap_start_sync doesn't actually record anything
2718 */
NeilBrowne3b97032005-08-04 12:53:34 -07002719 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrowne5de485f2005-11-08 21:39:38 -08002720 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown191ea9b2005-06-21 17:17:23 -07002721 /* We can skip this block, and probably several more */
2722 *skipped = 1;
2723 return sync_blocks;
2724 }
NeilBrown17999be2006-01-06 00:20:12 -08002725
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02002726 /*
2727 * If there is non-resync activity waiting for a turn, then let it
2728 * though before starting on this new sync request.
2729 */
colyli@suse.de824e47d2017-02-18 03:05:57 +08002730 if (atomic_read(&conf->nr_waiting[idx]))
Tomasz Majchrzak7ac50442016-06-13 15:51:19 +02002731 schedule_timeout_uninterruptible(1);
2732
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002733 /* we are incrementing sector_nr below. To be safe, we check against
2734 * sector_nr + two times RESYNC_SECTORS
2735 */
2736
2737 bitmap_cond_end_sync(mddev->bitmap, sector_nr,
2738 mddev_is_clustered(mddev) && (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
NeilBrown1c4588e2010-10-26 17:41:22 +11002739 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
NeilBrown17999be2006-01-06 00:20:12 -08002740
NeilBrownc2fd4c92014-09-10 16:01:24 +10002741 raise_barrier(conf, sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
NeilBrown3e198f72006-01-06 00:20:21 -08002743 rcu_read_lock();
2744 /*
2745 * If we get a correctably read error during resync or recovery,
2746 * we might want to read from a different device. So we
2747 * flag all drives that could conceivably be read from for READ,
2748 * and any others (which will be non-In_sync devices) for WRITE.
2749 * If a read fails, we try reading from something else for which READ
2750 * is OK.
2751 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 r1_bio->mddev = mddev;
2754 r1_bio->sector = sector_nr;
NeilBrown191ea9b2005-06-21 17:17:23 -07002755 r1_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 set_bit(R1BIO_IsSync, &r1_bio->state);
colyli@suse.defd768632017-02-18 03:05:56 +08002757 /* make sure good_sectors won't go across barrier unit boundary */
2758 good_sectors = align_to_barrier_unit_end(sector_nr, good_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
NeilBrown8f19ccb2011-12-23 10:17:56 +11002760 for (i = 0; i < conf->raid_disks * 2; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002761 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 bio = r1_bio->bios[i];
Kent Overstreet2aabaa62012-09-11 11:26:12 -07002763 bio_reset(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
NeilBrown3e198f72006-01-06 00:20:21 -08002765 rdev = rcu_dereference(conf->mirrors[i].rdev);
2766 if (rdev == NULL ||
NeilBrown06f60382011-07-28 11:31:48 +10002767 test_bit(Faulty, &rdev->flags)) {
NeilBrown8f19ccb2011-12-23 10:17:56 +11002768 if (i < conf->raid_disks)
2769 still_degraded = 1;
NeilBrown3e198f72006-01-06 00:20:21 -08002770 } else if (!test_bit(In_sync, &rdev->flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05002771 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 bio->bi_end_io = end_sync_write;
2773 write_targets ++;
NeilBrown3e198f72006-01-06 00:20:21 -08002774 } else {
2775 /* may need to read from here */
NeilBrown06f60382011-07-28 11:31:48 +10002776 sector_t first_bad = MaxSector;
2777 int bad_sectors;
2778
2779 if (is_badblock(rdev, sector_nr, good_sectors,
2780 &first_bad, &bad_sectors)) {
2781 if (first_bad > sector_nr)
2782 good_sectors = first_bad - sector_nr;
2783 else {
2784 bad_sectors -= (sector_nr - first_bad);
2785 if (min_bad == 0 ||
2786 min_bad > bad_sectors)
2787 min_bad = bad_sectors;
2788 }
NeilBrown3e198f72006-01-06 00:20:21 -08002789 }
NeilBrown06f60382011-07-28 11:31:48 +10002790 if (sector_nr < first_bad) {
2791 if (test_bit(WriteMostly, &rdev->flags)) {
2792 if (wonly < 0)
2793 wonly = i;
2794 } else {
2795 if (disk < 0)
2796 disk = i;
2797 }
Mike Christie796a5cf2016-06-05 14:32:07 -05002798 bio_set_op_attrs(bio, REQ_OP_READ, 0);
NeilBrown06f60382011-07-28 11:31:48 +10002799 bio->bi_end_io = end_sync_read;
2800 read_targets++;
Alexander Lyakasd57368a2012-07-17 13:17:55 +03002801 } else if (!test_bit(WriteErrorSeen, &rdev->flags) &&
2802 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
2803 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
2804 /*
2805 * The device is suitable for reading (InSync),
2806 * but has bad block(s) here. Let's try to correct them,
2807 * if we are doing resync or repair. Otherwise, leave
2808 * this device alone for this sync request.
2809 */
Mike Christie796a5cf2016-06-05 14:32:07 -05002810 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Alexander Lyakasd57368a2012-07-17 13:17:55 +03002811 bio->bi_end_io = end_sync_write;
2812 write_targets++;
NeilBrown06f60382011-07-28 11:31:48 +10002813 }
NeilBrown3e198f72006-01-06 00:20:21 -08002814 }
NeilBrown06f60382011-07-28 11:31:48 +10002815 if (bio->bi_end_io) {
2816 atomic_inc(&rdev->nr_pending);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002817 bio->bi_iter.bi_sector = sector_nr + rdev->data_offset;
NeilBrown06f60382011-07-28 11:31:48 +10002818 bio->bi_bdev = rdev->bdev;
2819 bio->bi_private = r1_bio;
NeilBrown2e52d442016-11-18 16:16:12 +11002820 if (test_bit(FailFast, &rdev->flags))
2821 bio->bi_opf |= MD_FAILFAST;
NeilBrown06f60382011-07-28 11:31:48 +10002822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 }
NeilBrown3e198f72006-01-06 00:20:21 -08002824 rcu_read_unlock();
2825 if (disk < 0)
2826 disk = wonly;
2827 r1_bio->read_disk = disk;
NeilBrown191ea9b2005-06-21 17:17:23 -07002828
NeilBrown06f60382011-07-28 11:31:48 +10002829 if (read_targets == 0 && min_bad > 0) {
2830 /* These sectors are bad on all InSync devices, so we
2831 * need to mark them bad on all write targets
2832 */
2833 int ok = 1;
NeilBrown8f19ccb2011-12-23 10:17:56 +11002834 for (i = 0 ; i < conf->raid_disks * 2 ; i++)
NeilBrown06f60382011-07-28 11:31:48 +10002835 if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
majianpenga42f9d82012-04-02 01:04:19 +10002836 struct md_rdev *rdev = conf->mirrors[i].rdev;
NeilBrown06f60382011-07-28 11:31:48 +10002837 ok = rdev_set_badblocks(rdev, sector_nr,
2838 min_bad, 0
2839 ) && ok;
2840 }
Shaohua Li29530792016-12-08 15:48:19 -08002841 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown06f60382011-07-28 11:31:48 +10002842 *skipped = 1;
2843 put_buf(r1_bio);
2844
2845 if (!ok) {
2846 /* Cannot record the badblocks, so need to
2847 * abort the resync.
2848 * If there are multiple read targets, could just
2849 * fail the really bad ones ???
2850 */
2851 conf->recovery_disabled = mddev->recovery_disabled;
2852 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2853 return 0;
2854 } else
2855 return min_bad;
2856
2857 }
2858 if (min_bad > 0 && min_bad < good_sectors) {
2859 /* only resync enough to reach the next bad->good
2860 * transition */
2861 good_sectors = min_bad;
2862 }
2863
NeilBrown3e198f72006-01-06 00:20:21 -08002864 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
2865 /* extra read targets are also write targets */
2866 write_targets += read_targets-1;
2867
2868 if (write_targets == 0 || read_targets == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 /* There is nowhere to write, so all non-sync
2870 * drives must be failed - so we are finished
2871 */
NeilBrownb7219cc2012-07-31 10:05:34 +10002872 sector_t rv;
2873 if (min_bad > 0)
2874 max_sector = sector_nr + min_bad;
2875 rv = max_sector - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07002876 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 put_buf(r1_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return rv;
2879 }
2880
NeilBrownc6207272008-02-06 01:39:52 -08002881 if (max_sector > mddev->resync_max)
2882 max_sector = mddev->resync_max; /* Don't do IO beyond here */
NeilBrown06f60382011-07-28 11:31:48 +10002883 if (max_sector > sector_nr + good_sectors)
2884 max_sector = sector_nr + good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 nr_sectors = 0;
NeilBrown289e99e2005-06-21 17:17:24 -07002886 sync_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 do {
2888 struct page *page;
2889 int len = PAGE_SIZE;
2890 if (sector_nr + (len>>9) > max_sector)
2891 len = (max_sector - sector_nr) << 9;
2892 if (len == 0)
2893 break;
NeilBrown6a806c52005-07-15 03:56:35 -07002894 if (sync_blocks == 0) {
2895 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
NeilBrowne5de485f2005-11-08 21:39:38 -08002896 &sync_blocks, still_degraded) &&
2897 !conf->fullsync &&
2898 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
NeilBrown6a806c52005-07-15 03:56:35 -07002899 break;
NeilBrown7571ae82010-10-07 11:54:46 +11002900 if ((len >> 9) > sync_blocks)
NeilBrown6a806c52005-07-15 03:56:35 -07002901 len = sync_blocks<<9;
NeilBrownab7a30c2005-06-21 17:17:23 -07002902 }
NeilBrown191ea9b2005-06-21 17:17:23 -07002903
NeilBrown8f19ccb2011-12-23 10:17:56 +11002904 for (i = 0 ; i < conf->raid_disks * 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 bio = r1_bio->bios[i];
2906 if (bio->bi_end_io) {
NeilBrownd11c1712006-01-06 00:20:26 -08002907 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (bio_add_page(bio, page, len, 0) == 0) {
2909 /* stop here */
NeilBrownd11c1712006-01-06 00:20:26 -08002910 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 while (i > 0) {
2912 i--;
2913 bio = r1_bio->bios[i];
NeilBrown6a806c52005-07-15 03:56:35 -07002914 if (bio->bi_end_io==NULL)
2915 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 /* remove last page from this bio */
2917 bio->bi_vcnt--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002918 bio->bi_iter.bi_size -= len;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06002919 bio_clear_flag(bio, BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 }
2921 goto bio_full;
2922 }
2923 }
2924 }
2925 nr_sectors += len>>9;
2926 sector_nr += len>>9;
NeilBrown191ea9b2005-06-21 17:17:23 -07002927 sync_blocks -= (len>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
2929 bio_full:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 r1_bio->sectors = nr_sectors;
2931
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10002932 if (mddev_is_clustered(mddev) &&
2933 conf->cluster_sync_high < sector_nr + nr_sectors) {
2934 conf->cluster_sync_low = mddev->curr_resync_completed;
2935 conf->cluster_sync_high = conf->cluster_sync_low + CLUSTER_RESYNC_WINDOW_SECTORS;
2936 /* Send resync message */
2937 md_cluster_ops->resync_info_update(mddev,
2938 conf->cluster_sync_low,
2939 conf->cluster_sync_high);
2940 }
2941
NeilBrownd11c1712006-01-06 00:20:26 -08002942 /* For a user-requested sync, we read all readable devices and do a
2943 * compare
2944 */
2945 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2946 atomic_set(&r1_bio->remaining, read_targets);
NeilBrown2d4f4f32012-07-09 11:34:13 +10002947 for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) {
NeilBrownd11c1712006-01-06 00:20:26 -08002948 bio = r1_bio->bios[i];
2949 if (bio->bi_end_io == end_sync_read) {
NeilBrown2d4f4f32012-07-09 11:34:13 +10002950 read_targets--;
NeilBrownddac7c72006-08-31 21:27:36 -07002951 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrown2e52d442016-11-18 16:16:12 +11002952 if (read_targets == 1)
2953 bio->bi_opf &= ~MD_FAILFAST;
NeilBrownd11c1712006-01-06 00:20:26 -08002954 generic_make_request(bio);
2955 }
2956 }
2957 } else {
2958 atomic_set(&r1_bio->remaining, 1);
2959 bio = r1_bio->bios[r1_bio->read_disk];
NeilBrownddac7c72006-08-31 21:27:36 -07002960 md_sync_acct(bio->bi_bdev, nr_sectors);
NeilBrown2e52d442016-11-18 16:16:12 +11002961 if (read_targets == 1)
2962 bio->bi_opf &= ~MD_FAILFAST;
NeilBrownd11c1712006-01-06 00:20:26 -08002963 generic_make_request(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
NeilBrownd11c1712006-01-06 00:20:26 -08002965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 return nr_sectors;
2967}
2968
NeilBrownfd01b882011-10-11 16:47:53 +11002969static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07002970{
2971 if (sectors)
2972 return sectors;
2973
2974 return mddev->dev_sectors;
2975}
2976
NeilBrowne8096362011-10-11 16:49:05 +11002977static struct r1conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
NeilBrowne8096362011-10-11 16:49:05 +11002979 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11002980 int i;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10002981 struct raid1_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11002982 struct md_rdev *rdev;
NeilBrown709ae482009-12-14 12:49:51 +11002983 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
NeilBrowne8096362011-10-11 16:49:05 +11002985 conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 if (!conf)
NeilBrown709ae482009-12-14 12:49:51 +11002987 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
colyli@suse.defd768632017-02-18 03:05:56 +08002989 conf->nr_pending = kcalloc(BARRIER_BUCKETS_NR,
colyli@suse.de824e47d2017-02-18 03:05:57 +08002990 sizeof(atomic_t), GFP_KERNEL);
colyli@suse.defd768632017-02-18 03:05:56 +08002991 if (!conf->nr_pending)
2992 goto abort;
2993
2994 conf->nr_waiting = kcalloc(BARRIER_BUCKETS_NR,
colyli@suse.de824e47d2017-02-18 03:05:57 +08002995 sizeof(atomic_t), GFP_KERNEL);
colyli@suse.defd768632017-02-18 03:05:56 +08002996 if (!conf->nr_waiting)
2997 goto abort;
2998
2999 conf->nr_queued = kcalloc(BARRIER_BUCKETS_NR,
colyli@suse.de824e47d2017-02-18 03:05:57 +08003000 sizeof(atomic_t), GFP_KERNEL);
colyli@suse.defd768632017-02-18 03:05:56 +08003001 if (!conf->nr_queued)
3002 goto abort;
3003
3004 conf->barrier = kcalloc(BARRIER_BUCKETS_NR,
colyli@suse.de824e47d2017-02-18 03:05:57 +08003005 sizeof(atomic_t), GFP_KERNEL);
colyli@suse.defd768632017-02-18 03:05:56 +08003006 if (!conf->barrier)
3007 goto abort;
3008
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10003009 conf->mirrors = kzalloc(sizeof(struct raid1_info)
NeilBrown8f19ccb2011-12-23 10:17:56 +11003010 * mddev->raid_disks * 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 GFP_KERNEL);
3012 if (!conf->mirrors)
NeilBrown709ae482009-12-14 12:49:51 +11003013 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
NeilBrownddaf22a2006-01-06 00:20:19 -08003015 conf->tmppage = alloc_page(GFP_KERNEL);
3016 if (!conf->tmppage)
NeilBrown709ae482009-12-14 12:49:51 +11003017 goto abort;
NeilBrownddaf22a2006-01-06 00:20:19 -08003018
NeilBrown709ae482009-12-14 12:49:51 +11003019 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 if (!conf->poolinfo)
NeilBrown709ae482009-12-14 12:49:51 +11003021 goto abort;
NeilBrown8f19ccb2011-12-23 10:17:56 +11003022 conf->poolinfo->raid_disks = mddev->raid_disks * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
3024 r1bio_pool_free,
3025 conf->poolinfo);
3026 if (!conf->r1bio_pool)
NeilBrown709ae482009-12-14 12:49:51 +11003027 goto abort;
3028
NeilBrowned9bfdf2009-10-16 15:55:44 +11003029 conf->poolinfo->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
NeilBrownc19d5792011-12-23 10:17:57 +11003031 err = -EINVAL;
Neil Browne7e72bf2008-05-14 16:05:54 -07003032 spin_lock_init(&conf->device_lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11003033 rdev_for_each(rdev, mddev) {
NeilBrownaba336b2012-05-31 15:39:11 +10003034 struct request_queue *q;
NeilBrown709ae482009-12-14 12:49:51 +11003035 int disk_idx = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 if (disk_idx >= mddev->raid_disks
3037 || disk_idx < 0)
3038 continue;
NeilBrownc19d5792011-12-23 10:17:57 +11003039 if (test_bit(Replacement, &rdev->flags))
NeilBrown02b898f2012-10-31 11:42:03 +11003040 disk = conf->mirrors + mddev->raid_disks + disk_idx;
NeilBrownc19d5792011-12-23 10:17:57 +11003041 else
3042 disk = conf->mirrors + disk_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
NeilBrownc19d5792011-12-23 10:17:57 +11003044 if (disk->rdev)
3045 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 disk->rdev = rdev;
NeilBrownaba336b2012-05-31 15:39:11 +10003047 q = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
3049 disk->head_position = 0;
Shaohua Li12cee5a2012-07-31 10:03:53 +10003050 disk->seq_start = MaxSector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 }
3052 conf->raid_disks = mddev->raid_disks;
3053 conf->mddev = mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 INIT_LIST_HEAD(&conf->retry_list);
NeilBrown55ce74d2015-08-14 11:11:10 +10003055 INIT_LIST_HEAD(&conf->bio_end_io_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
3057 spin_lock_init(&conf->resync_lock);
NeilBrown17999be2006-01-06 00:20:12 -08003058 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
NeilBrown191ea9b2005-06-21 17:17:23 -07003060 bio_list_init(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +11003061 conf->pending_count = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11003062 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown191ea9b2005-06-21 17:17:23 -07003063
NeilBrownc19d5792011-12-23 10:17:57 +11003064 err = -EIO;
NeilBrown8f19ccb2011-12-23 10:17:56 +11003065 for (i = 0; i < conf->raid_disks * 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
3067 disk = conf->mirrors + i;
3068
NeilBrownc19d5792011-12-23 10:17:57 +11003069 if (i < conf->raid_disks &&
3070 disk[conf->raid_disks].rdev) {
3071 /* This slot has a replacement. */
3072 if (!disk->rdev) {
3073 /* No original, just make the replacement
3074 * a recovering spare
3075 */
3076 disk->rdev =
3077 disk[conf->raid_disks].rdev;
3078 disk[conf->raid_disks].rdev = NULL;
3079 } else if (!test_bit(In_sync, &disk->rdev->flags))
3080 /* Original is not in_sync - bad */
3081 goto abort;
3082 }
3083
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003084 if (!disk->rdev ||
3085 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 disk->head_position = 0;
Jonathan Brassow4f0a5e02012-05-22 13:55:31 +10003087 if (disk->rdev &&
3088 (disk->rdev->saved_raid_disk < 0))
NeilBrown918f0232007-08-22 14:01:52 -07003089 conf->fullsync = 1;
Shaohua Libe4d3282012-07-31 10:03:53 +10003090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
NeilBrown709ae482009-12-14 12:49:51 +11003092
NeilBrown709ae482009-12-14 12:49:51 +11003093 err = -ENOMEM;
NeilBrown02326052012-07-03 15:56:52 +10003094 conf->thread = md_register_thread(raid1d, mddev, "raid1");
NeilBrown1d41c212016-11-02 14:16:50 +11003095 if (!conf->thread)
NeilBrown709ae482009-12-14 12:49:51 +11003096 goto abort;
NeilBrown191ea9b2005-06-21 17:17:23 -07003097
NeilBrown709ae482009-12-14 12:49:51 +11003098 return conf;
3099
3100 abort:
3101 if (conf) {
Julia Lawall644df1a2015-09-13 14:15:10 +02003102 mempool_destroy(conf->r1bio_pool);
NeilBrown709ae482009-12-14 12:49:51 +11003103 kfree(conf->mirrors);
3104 safe_put_page(conf->tmppage);
3105 kfree(conf->poolinfo);
colyli@suse.defd768632017-02-18 03:05:56 +08003106 kfree(conf->nr_pending);
3107 kfree(conf->nr_waiting);
3108 kfree(conf->nr_queued);
3109 kfree(conf->barrier);
NeilBrown709ae482009-12-14 12:49:51 +11003110 kfree(conf);
3111 }
3112 return ERR_PTR(err);
3113}
3114
NeilBrownafa0f552014-12-15 12:56:58 +11003115static void raid1_free(struct mddev *mddev, void *priv);
Shaohua Li849674e2016-01-20 13:52:20 -08003116static int raid1_run(struct mddev *mddev)
NeilBrown709ae482009-12-14 12:49:51 +11003117{
NeilBrowne8096362011-10-11 16:49:05 +11003118 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11003119 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003120 struct md_rdev *rdev;
majianpeng5220ea12012-04-02 09:48:38 +10003121 int ret;
Shaohua Li2ff8cc22012-10-11 13:28:54 +11003122 bool discard_supported = false;
NeilBrown709ae482009-12-14 12:49:51 +11003123
3124 if (mddev->level != 1) {
NeilBrown1d41c212016-11-02 14:16:50 +11003125 pr_warn("md/raid1:%s: raid level not set to mirroring (%d)\n",
3126 mdname(mddev), mddev->level);
NeilBrown709ae482009-12-14 12:49:51 +11003127 return -EIO;
3128 }
3129 if (mddev->reshape_position != MaxSector) {
NeilBrown1d41c212016-11-02 14:16:50 +11003130 pr_warn("md/raid1:%s: reshape_position set but not supported\n",
3131 mdname(mddev));
NeilBrown709ae482009-12-14 12:49:51 +11003132 return -EIO;
3133 }
3134 /*
3135 * copy the already verified devices into our private RAID1
3136 * bookkeeping area. [whatever we allocate in run(),
NeilBrownafa0f552014-12-15 12:56:58 +11003137 * should be freed in raid1_free()]
NeilBrown709ae482009-12-14 12:49:51 +11003138 */
3139 if (mddev->private == NULL)
3140 conf = setup_conf(mddev);
3141 else
3142 conf = mddev->private;
3143
3144 if (IS_ERR(conf))
3145 return PTR_ERR(conf);
3146
Joe Lawrencec8dc9c62013-02-21 13:28:09 +11003147 if (mddev->queue)
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07003148 blk_queue_max_write_same_sectors(mddev->queue, 0);
3149
NeilBrowndafb20f2012-03-19 12:46:39 +11003150 rdev_for_each(rdev, mddev) {
Jonathan Brassow1ed72422011-06-07 17:50:35 -05003151 if (!mddev->gendisk)
3152 continue;
NeilBrown709ae482009-12-14 12:49:51 +11003153 disk_stack_limits(mddev->gendisk, rdev->bdev,
3154 rdev->data_offset << 9);
Shaohua Li2ff8cc22012-10-11 13:28:54 +11003155 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3156 discard_supported = true;
NeilBrown709ae482009-12-14 12:49:51 +11003157 }
3158
3159 mddev->degraded = 0;
3160 for (i=0; i < conf->raid_disks; i++)
3161 if (conf->mirrors[i].rdev == NULL ||
3162 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
3163 test_bit(Faulty, &conf->mirrors[i].rdev->flags))
3164 mddev->degraded++;
3165
3166 if (conf->raid_disks - mddev->degraded == 1)
3167 mddev->recovery_cp = MaxSector;
3168
Andre Noll8c6ac862009-06-18 08:48:06 +10003169 if (mddev->recovery_cp != MaxSector)
NeilBrown1d41c212016-11-02 14:16:50 +11003170 pr_info("md/raid1:%s: not clean -- starting background reconstruction\n",
3171 mdname(mddev));
3172 pr_info("md/raid1:%s: active with %d out of %d mirrors\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10003173 mdname(mddev), mddev->raid_disks - mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 mddev->raid_disks);
NeilBrown709ae482009-12-14 12:49:51 +11003175
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 /*
3177 * Ok, everything is just fine now
3178 */
NeilBrown709ae482009-12-14 12:49:51 +11003179 mddev->thread = conf->thread;
3180 conf->thread = NULL;
3181 mddev->private = conf;
NeilBrown46533ff2016-11-18 16:16:11 +11003182 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
NeilBrown709ae482009-12-14 12:49:51 +11003183
Dan Williams1f403622009-03-31 14:59:03 +11003184 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Jonathan Brassow1ed72422011-06-07 17:50:35 -05003186 if (mddev->queue) {
Shaohua Li2ff8cc22012-10-11 13:28:54 +11003187 if (discard_supported)
3188 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3189 mddev->queue);
3190 else
3191 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3192 mddev->queue);
Jonathan Brassow1ed72422011-06-07 17:50:35 -05003193 }
majianpeng5220ea12012-04-02 09:48:38 +10003194
3195 ret = md_integrity_register(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003196 if (ret) {
3197 md_unregister_thread(&mddev->thread);
NeilBrownafa0f552014-12-15 12:56:58 +11003198 raid1_free(mddev, conf);
NeilBrown5aa61f42014-12-15 12:56:57 +11003199 }
majianpeng5220ea12012-04-02 09:48:38 +10003200 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201}
3202
NeilBrownafa0f552014-12-15 12:56:58 +11003203static void raid1_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
NeilBrownafa0f552014-12-15 12:56:58 +11003205 struct r1conf *conf = priv;
NeilBrown4b6d2872005-09-09 16:23:47 -07003206
Julia Lawall644df1a2015-09-13 14:15:10 +02003207 mempool_destroy(conf->r1bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003208 kfree(conf->mirrors);
Hirokazu Takahashi0fea7ed2013-04-24 11:42:44 +10003209 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07003210 kfree(conf->poolinfo);
colyli@suse.defd768632017-02-18 03:05:56 +08003211 kfree(conf->nr_pending);
3212 kfree(conf->nr_waiting);
3213 kfree(conf->nr_queued);
3214 kfree(conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216}
3217
NeilBrownfd01b882011-10-11 16:47:53 +11003218static int raid1_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219{
3220 /* no resync is happening, and there is enough space
3221 * on all devices, so we can resize.
3222 * We need to make sure resync covers any new space.
3223 * If the array is shrinking we should possibly wait until
3224 * any io in the removed space completes, but it hardly seems
3225 * worth it.
3226 */
NeilBrowna4a61252012-05-22 13:55:27 +10003227 sector_t newsize = raid1_size(mddev, sectors, 0);
3228 if (mddev->external_size &&
3229 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11003230 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10003231 if (mddev->bitmap) {
3232 int ret = bitmap_resize(mddev->bitmap, newsize, 0, 0);
3233 if (ret)
3234 return ret;
3235 }
3236 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10003237 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003238 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003239 if (sectors > mddev->dev_sectors &&
NeilBrownb0986362011-05-11 15:52:21 +10003240 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003241 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3243 }
Dan Williamsb522adc2009-03-31 15:00:31 +11003244 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07003245 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 return 0;
3247}
3248
NeilBrownfd01b882011-10-11 16:47:53 +11003249static int raid1_reshape(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
3251 /* We need to:
3252 * 1/ resize the r1bio_pool
3253 * 2/ resize conf->mirrors
3254 *
3255 * We allocate a new r1bio_pool if we can.
3256 * Then raise a device barrier and wait until all IO stops.
3257 * Then resize conf->mirrors and swap in the new r1bio pool.
NeilBrown6ea9c072005-06-21 17:17:09 -07003258 *
3259 * At the same time, we "pack" the devices so that all the missing
3260 * devices have the higher raid_disk numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 */
3262 mempool_t *newpool, *oldpool;
3263 struct pool_info *newpoolinfo;
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10003264 struct raid1_info *newmirrors;
NeilBrowne8096362011-10-11 16:49:05 +11003265 struct r1conf *conf = mddev->private;
NeilBrown63c70c42006-03-27 01:18:13 -08003266 int cnt, raid_disks;
NeilBrownc04be0a2006-10-03 01:15:53 -07003267 unsigned long flags;
Dan Williamsb5470dc2008-06-27 21:44:04 -07003268 int d, d2, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
NeilBrown63c70c42006-03-27 01:18:13 -08003270 /* Cannot change chunk_size, layout, or level */
Andre Noll664e7c42009-06-18 08:45:27 +10003271 if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
NeilBrown63c70c42006-03-27 01:18:13 -08003272 mddev->layout != mddev->new_layout ||
3273 mddev->level != mddev->new_level) {
Andre Noll664e7c42009-06-18 08:45:27 +10003274 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown63c70c42006-03-27 01:18:13 -08003275 mddev->new_layout = mddev->layout;
3276 mddev->new_level = mddev->level;
3277 return -EINVAL;
3278 }
3279
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003280 if (!mddev_is_clustered(mddev)) {
3281 err = md_allow_write(mddev);
3282 if (err)
3283 return err;
3284 }
NeilBrown2a2275d2007-01-26 00:57:11 -08003285
NeilBrown63c70c42006-03-27 01:18:13 -08003286 raid_disks = mddev->raid_disks + mddev->delta_disks;
3287
NeilBrown6ea9c072005-06-21 17:17:09 -07003288 if (raid_disks < conf->raid_disks) {
3289 cnt=0;
3290 for (d= 0; d < conf->raid_disks; d++)
3291 if (conf->mirrors[d].rdev)
3292 cnt++;
3293 if (cnt > raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 return -EBUSY;
NeilBrown6ea9c072005-06-21 17:17:09 -07003295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
3297 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
3298 if (!newpoolinfo)
3299 return -ENOMEM;
3300 newpoolinfo->mddev = mddev;
NeilBrown8f19ccb2011-12-23 10:17:56 +11003301 newpoolinfo->raid_disks = raid_disks * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
3303 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
3304 r1bio_pool_free, newpoolinfo);
3305 if (!newpool) {
3306 kfree(newpoolinfo);
3307 return -ENOMEM;
3308 }
Jonathan Brassow0eaf8222012-07-31 10:03:52 +10003309 newmirrors = kzalloc(sizeof(struct raid1_info) * raid_disks * 2,
NeilBrown8f19ccb2011-12-23 10:17:56 +11003310 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 if (!newmirrors) {
3312 kfree(newpoolinfo);
3313 mempool_destroy(newpool);
3314 return -ENOMEM;
3315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
NeilBrowne2d59922013-06-12 11:01:22 +10003317 freeze_array(conf, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
3319 /* ok, everything is stopped */
3320 oldpool = conf->r1bio_pool;
3321 conf->r1bio_pool = newpool;
NeilBrown6ea9c072005-06-21 17:17:09 -07003322
NeilBrowna88aa782007-08-22 14:01:53 -07003323 for (d = d2 = 0; d < conf->raid_disks; d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11003324 struct md_rdev *rdev = conf->mirrors[d].rdev;
NeilBrowna88aa782007-08-22 14:01:53 -07003325 if (rdev && rdev->raid_disk != d2) {
Namhyung Kim36fad852011-07-27 11:00:36 +10003326 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna88aa782007-08-22 14:01:53 -07003327 rdev->raid_disk = d2;
Namhyung Kim36fad852011-07-27 11:00:36 +10003328 sysfs_unlink_rdev(mddev, rdev);
3329 if (sysfs_link_rdev(mddev, rdev))
NeilBrown1d41c212016-11-02 14:16:50 +11003330 pr_warn("md/raid1:%s: cannot register rd%d\n",
3331 mdname(mddev), rdev->raid_disk);
NeilBrown6ea9c072005-06-21 17:17:09 -07003332 }
NeilBrowna88aa782007-08-22 14:01:53 -07003333 if (rdev)
3334 newmirrors[d2++].rdev = rdev;
3335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 kfree(conf->mirrors);
3337 conf->mirrors = newmirrors;
3338 kfree(conf->poolinfo);
3339 conf->poolinfo = newpoolinfo;
3340
NeilBrownc04be0a2006-10-03 01:15:53 -07003341 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 mddev->degraded += (raid_disks - conf->raid_disks);
NeilBrownc04be0a2006-10-03 01:15:53 -07003343 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 conf->raid_disks = mddev->raid_disks = raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08003345 mddev->delta_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
NeilBrowne2d59922013-06-12 11:01:22 +10003347 unfreeze_array(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
NeilBrown985ca972015-07-06 12:26:57 +10003349 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3351 md_wakeup_thread(mddev->thread);
3352
3353 mempool_destroy(oldpool);
3354 return 0;
3355}
3356
NeilBrownfd01b882011-10-11 16:47:53 +11003357static void raid1_quiesce(struct mddev *mddev, int state)
NeilBrown36fa3062005-09-09 16:23:45 -07003358{
NeilBrowne8096362011-10-11 16:49:05 +11003359 struct r1conf *conf = mddev->private;
NeilBrown36fa3062005-09-09 16:23:45 -07003360
3361 switch(state) {
NeilBrown6eef4b22009-12-14 12:49:51 +11003362 case 2: /* wake for suspend */
3363 wake_up(&conf->wait_barrier);
3364 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07003365 case 1:
majianpeng07169fd2013-11-14 15:16:18 +11003366 freeze_array(conf, 0);
NeilBrown36fa3062005-09-09 16:23:45 -07003367 break;
NeilBrown9e6603d2005-09-09 16:23:48 -07003368 case 0:
majianpeng07169fd2013-11-14 15:16:18 +11003369 unfreeze_array(conf);
NeilBrown36fa3062005-09-09 16:23:45 -07003370 break;
3371 }
NeilBrown36fa3062005-09-09 16:23:45 -07003372}
3373
NeilBrownfd01b882011-10-11 16:47:53 +11003374static void *raid1_takeover(struct mddev *mddev)
NeilBrown709ae482009-12-14 12:49:51 +11003375{
3376 /* raid1 can take over:
3377 * raid5 with 2 devices, any layout or chunk size
3378 */
3379 if (mddev->level == 5 && mddev->raid_disks == 2) {
NeilBrowne8096362011-10-11 16:49:05 +11003380 struct r1conf *conf;
NeilBrown709ae482009-12-14 12:49:51 +11003381 mddev->new_level = 1;
3382 mddev->new_layout = 0;
3383 mddev->new_chunk_sectors = 0;
3384 conf = setup_conf(mddev);
Shaohua Li6995f0b2016-12-08 15:48:17 -08003385 if (!IS_ERR(conf)) {
majianpeng07169fd2013-11-14 15:16:18 +11003386 /* Array must appear to be quiesced */
3387 conf->array_frozen = 1;
Shaohua Li394ed8e2017-01-04 16:10:19 -08003388 mddev_clear_unsupported_flags(mddev,
3389 UNSUPPORTED_MDDEV_FLAGS);
Shaohua Li6995f0b2016-12-08 15:48:17 -08003390 }
NeilBrown709ae482009-12-14 12:49:51 +11003391 return conf;
3392 }
3393 return ERR_PTR(-EINVAL);
3394}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
NeilBrown84fc4b52011-10-11 16:49:58 +11003396static struct md_personality raid1_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397{
3398 .name = "raid1",
NeilBrown2604b702006-01-06 00:20:36 -08003399 .level = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08003401 .make_request = raid1_make_request,
3402 .run = raid1_run,
NeilBrownafa0f552014-12-15 12:56:58 +11003403 .free = raid1_free,
Shaohua Li849674e2016-01-20 13:52:20 -08003404 .status = raid1_status,
3405 .error_handler = raid1_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 .hot_add_disk = raid1_add_disk,
3407 .hot_remove_disk= raid1_remove_disk,
3408 .spare_active = raid1_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08003409 .sync_request = raid1_sync_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 .resize = raid1_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07003411 .size = raid1_size,
NeilBrown63c70c42006-03-27 01:18:13 -08003412 .check_reshape = raid1_reshape,
NeilBrown36fa3062005-09-09 16:23:45 -07003413 .quiesce = raid1_quiesce,
NeilBrown709ae482009-12-14 12:49:51 +11003414 .takeover = raid1_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11003415 .congested = raid1_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416};
3417
3418static int __init raid_init(void)
3419{
NeilBrown2604b702006-01-06 00:20:36 -08003420 return register_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421}
3422
3423static void raid_exit(void)
3424{
NeilBrown2604b702006-01-06 00:20:36 -08003425 unregister_md_personality(&raid1_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
3428module_init(raid_init);
3429module_exit(raid_exit);
3430MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11003431MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432MODULE_ALIAS("md-personality-3"); /* RAID1 */
NeilBrownd9d166c2006-01-06 00:20:51 -08003433MODULE_ALIAS("md-raid1");
NeilBrown2604b702006-01-06 00:20:36 -08003434MODULE_ALIAS("md-level-1");
NeilBrown34db0cd2011-10-11 16:50:01 +11003435
3436module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);