blob: 98bac4f304ae262411944252088276bf60b86d41 [file] [log] [blame]
Thomas Gleixneraf1a8892019-05-20 19:08:12 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 md.c : Multiple Devices driver for Linux
NeilBrownf72ffdd2014-09-30 14:23:59 +10004 Copyright (C) 1998, 1999, 2000 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
6 completely rewritten, based on the MD driver code from Marc Zyngier
7
8 Changes:
9
10 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
11 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
12 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
13 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
14 - kmod support by: Cyrus Durgin
15 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
16 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17
18 - lots of fixes and improvements to the RAID1/RAID5 and generic
19 RAID code (such as request based resynchronization):
20
21 Neil Brown <neilb@cse.unsw.edu.au>.
22
NeilBrown32a76272005-06-21 17:17:14 -070023 - persistent bitmap code
24 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25
NeilBrown9d487392016-11-02 14:16:49 +110026
27 Errors, Warnings, etc.
28 Please use:
29 pr_crit() for error conditions that risk data loss
30 pr_err() for error conditions that are unexpected, like an IO error
31 or internal inconsistency
32 pr_warn() for error conditions that could have been predicated, like
33 adding a device to an array when it has incompatible metadata
34 pr_info() for every interesting, very rare events, like an array starting
35 or stopping, or resync starting or stopping
36 pr_debug() for everything else.
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038*/
39
Guoqing Jiang963c5552019-06-14 17:10:36 +080040#include <linux/sched/mm.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010041#include <linux/sched/signal.h>
NeilBrowna6fb0932005-09-09 16:23:56 -070042#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110043#include <linux/blkdev.h>
Vishal Vermafc974ee2015-12-24 19:20:34 -070044#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040047#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080048#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070049#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080050#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110051#include <linux/hdreg.h>
52#include <linux/proc_fs.h>
53#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040054#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110055#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070056#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110057#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110058#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110059#include <linux/raid/md_p.h>
60#include <linux/raid/md_u.h>
Christoph Hellwig74cc979c2020-03-24 08:25:19 +010061#include <linux/raid/detect.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090062#include <linux/slab.h>
NeilBrown4ad23a972017-03-15 14:05:14 +110063#include <linux/percpu-refcount.h>
Christoph Hellwigc6a564ff2020-03-25 16:48:42 +010064#include <linux/part_stat.h>
NeilBrown4ad23a972017-03-15 14:05:14 +110065
Shaohua Li504634f2016-11-18 09:44:08 -080066#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110067#include "md.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040068#include "md-bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050069#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
NeilBrown01f96c02011-09-21 15:30:20 +100071/* pers_list is a list of registered personalities protected
72 * by pers_lock.
73 * pers_lock does extra service to protect accesses to
74 * mddev->thread when the mutex cannot be held.
75 */
NeilBrown2604b702006-01-06 00:20:36 -080076static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static DEFINE_SPINLOCK(pers_lock);
78
Kent Overstreet28dec872018-06-07 20:52:54 -040079static struct kobj_type md_ktype;
80
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050081struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050082EXPORT_SYMBOL(md_cluster_ops);
Christoph Hellwig2b598ee2019-04-04 18:56:14 +020083static struct module *md_cluster_mod;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050084
Bernd Schubert90b08712008-05-23 13:04:38 -070085static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020086static struct workqueue_struct *md_wq;
87static struct workqueue_struct *md_misc_wq;
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +020088static struct workqueue_struct *md_rdev_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070089
NeilBrown746d3202013-04-24 11:42:41 +100090static int remove_and_add_spares(struct mddev *mddev,
91 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110092static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*
Robert Becker1e509152009-12-14 12:49:58 +110095 * Default number of read corrections we'll attempt on an rdev
96 * before ejecting it from the array. We divide the read error
97 * count by 2 for every hour elapsed between read errors.
98 */
99#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
Zhao Heming7c9d5c52020-07-21 02:08:52 +0800100/* Default safemode delay: 200 msec */
101#define DEFAULT_SAFEMODE_DELAY ((200 * HZ)/1000 +1)
Robert Becker1e509152009-12-14 12:49:58 +1100102/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
104 * is 1000 KB/sec, so the extra system load does not show up that much.
105 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -0700106 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * subsystem is idle. There is also an 'absolute maximum' reconstruction
108 * speed limit - in case reconstruction slows down your system despite
109 * idle IO detection.
110 *
111 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -0800112 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
114
115static int sysctl_speed_limit_min = 1000;
116static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100117static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800118{
119 return mddev->sync_speed_min ?
120 mddev->sync_speed_min : sysctl_speed_limit_min;
121}
122
NeilBrownfd01b882011-10-11 16:47:53 +1100123static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800124{
125 return mddev->sync_speed_max ?
126 mddev->sync_speed_max : sysctl_speed_limit_max;
127}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100129static void rdev_uninit_serial(struct md_rdev *rdev)
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800130{
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100131 if (!test_and_clear_bit(CollisionCheck, &rdev->flags))
132 return;
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800133
Guoqing Jiang025471f2019-12-23 10:49:01 +0100134 kvfree(rdev->serial);
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100135 rdev->serial = NULL;
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800136}
137
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100138static void rdevs_uninit_serial(struct mddev *mddev)
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100139{
140 struct md_rdev *rdev;
141
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100142 rdev_for_each(rdev, mddev)
143 rdev_uninit_serial(rdev);
144}
145
146static int rdev_init_serial(struct md_rdev *rdev)
147{
Guoqing Jiang025471f2019-12-23 10:49:01 +0100148 /* serial_nums equals with BARRIER_BUCKETS_NR */
149 int i, serial_nums = 1 << ((PAGE_SHIFT - ilog2(sizeof(atomic_t))));
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100150 struct serial_in_rdev *serial = NULL;
151
152 if (test_bit(CollisionCheck, &rdev->flags))
153 return 0;
154
Guoqing Jiang025471f2019-12-23 10:49:01 +0100155 serial = kvmalloc(sizeof(struct serial_in_rdev) * serial_nums,
156 GFP_KERNEL);
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100157 if (!serial)
158 return -ENOMEM;
159
Guoqing Jiang025471f2019-12-23 10:49:01 +0100160 for (i = 0; i < serial_nums; i++) {
161 struct serial_in_rdev *serial_tmp = &serial[i];
162
163 spin_lock_init(&serial_tmp->serial_lock);
164 serial_tmp->serial_rb = RB_ROOT_CACHED;
165 init_waitqueue_head(&serial_tmp->serial_io_wait);
166 }
167
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100168 rdev->serial = serial;
169 set_bit(CollisionCheck, &rdev->flags);
170
171 return 0;
172}
173
174static int rdevs_init_serial(struct mddev *mddev)
175{
176 struct md_rdev *rdev;
177 int ret = 0;
178
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100179 rdev_for_each(rdev, mddev) {
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100180 ret = rdev_init_serial(rdev);
181 if (ret)
182 break;
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100183 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100184
185 /* Free all resources if pool is not existed */
186 if (ret && !mddev->serial_info_pool)
187 rdevs_uninit_serial(mddev);
188
189 return ret;
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100190}
191
Guoqing Jiang963c5552019-06-14 17:10:36 +0800192/*
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100193 * rdev needs to enable serial stuffs if it meets the conditions:
194 * 1. it is multi-queue device flaged with writemostly.
195 * 2. the write-behind mode is enabled.
196 */
197static int rdev_need_serial(struct md_rdev *rdev)
198{
199 return (rdev && rdev->mddev->bitmap_info.max_write_behind > 0 &&
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200200 rdev->bdev->bd_disk->queue->nr_hw_queues != 1 &&
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100201 test_bit(WriteMostly, &rdev->flags));
202}
203
204/*
205 * Init resource for rdev(s), then create serial_info_pool if:
206 * 1. rdev is the first device which return true from rdev_enable_serial.
207 * 2. rdev is NULL, means we want to enable serialization for all rdevs.
Guoqing Jiang963c5552019-06-14 17:10:36 +0800208 */
Guoqing Jiang404659c2019-12-23 10:48:53 +0100209void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev,
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100210 bool is_suspend)
Guoqing Jiang963c5552019-06-14 17:10:36 +0800211{
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100212 int ret = 0;
213
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100214 if (rdev && !rdev_need_serial(rdev) &&
215 !test_bit(CollisionCheck, &rdev->flags))
Guoqing Jiang963c5552019-06-14 17:10:36 +0800216 return;
217
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100218 if (!is_suspend)
219 mddev_suspend(mddev);
220
221 if (!rdev)
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100222 ret = rdevs_init_serial(mddev);
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100223 else
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100224 ret = rdev_init_serial(rdev);
225 if (ret)
226 goto abort;
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100227
Guoqing Jiang404659c2019-12-23 10:48:53 +0100228 if (mddev->serial_info_pool == NULL) {
Coly Li3024ba22020-04-09 22:17:23 +0800229 /*
230 * already in memalloc noio context by
231 * mddev_suspend()
232 */
Guoqing Jiang404659c2019-12-23 10:48:53 +0100233 mddev->serial_info_pool =
234 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
235 sizeof(struct serial_info));
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100236 if (!mddev->serial_info_pool) {
237 rdevs_uninit_serial(mddev);
Guoqing Jiang404659c2019-12-23 10:48:53 +0100238 pr_err("can't alloc memory pool for serialization\n");
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100239 }
Guoqing Jiang963c5552019-06-14 17:10:36 +0800240 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100241
242abort:
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100243 if (!is_suspend)
244 mddev_resume(mddev);
Guoqing Jiang963c5552019-06-14 17:10:36 +0800245}
Guoqing Jiang963c5552019-06-14 17:10:36 +0800246
247/*
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100248 * Free resource from rdev(s), and destroy serial_info_pool under conditions:
249 * 1. rdev is the last device flaged with CollisionCheck.
250 * 2. when bitmap is destroyed while policy is not enabled.
251 * 3. for disable policy, the pool is destroyed only when no rdev needs it.
Guoqing Jiang963c5552019-06-14 17:10:36 +0800252 */
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100253void mddev_destroy_serial_pool(struct mddev *mddev, struct md_rdev *rdev,
254 bool is_suspend)
Guoqing Jiang963c5552019-06-14 17:10:36 +0800255{
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100256 if (rdev && !test_bit(CollisionCheck, &rdev->flags))
Guoqing Jiang963c5552019-06-14 17:10:36 +0800257 return;
258
Guoqing Jiang404659c2019-12-23 10:48:53 +0100259 if (mddev->serial_info_pool) {
Guoqing Jiang963c5552019-06-14 17:10:36 +0800260 struct md_rdev *temp;
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100261 int num = 0; /* used to track if other rdevs need the pool */
Guoqing Jiang963c5552019-06-14 17:10:36 +0800262
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100263 if (!is_suspend)
264 mddev_suspend(mddev);
265 rdev_for_each(temp, mddev) {
266 if (!rdev) {
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100267 if (!mddev->serialize_policy ||
268 !rdev_need_serial(temp))
269 rdev_uninit_serial(temp);
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100270 else
271 num++;
272 } else if (temp != rdev &&
273 test_bit(CollisionCheck, &temp->flags))
Guoqing Jiang963c5552019-06-14 17:10:36 +0800274 num++;
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100275 }
276
277 if (rdev)
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100278 rdev_uninit_serial(rdev);
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100279
280 if (num)
281 pr_info("The mempool could be used by other devices\n");
282 else {
Guoqing Jiang404659c2019-12-23 10:48:53 +0100283 mempool_destroy(mddev->serial_info_pool);
284 mddev->serial_info_pool = NULL;
Guoqing Jiang963c5552019-06-14 17:10:36 +0800285 }
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100286 if (!is_suspend)
287 mddev_resume(mddev);
Guoqing Jiang963c5552019-06-14 17:10:36 +0800288 }
289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291static struct ctl_table_header *raid_table_header;
292
Joe Perches82592c32013-11-14 15:16:18 +1100293static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 .procname = "speed_limit_min",
296 .data = &sysctl_speed_limit_min,
297 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700298 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800299 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 },
301 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 .procname = "speed_limit_max",
303 .data = &sysctl_speed_limit_max,
304 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700305 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800306 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800308 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309};
310
Joe Perches82592c32013-11-14 15:16:18 +1100311static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 .procname = "raid",
314 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700315 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 .child = raid_table,
317 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800318 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319};
320
Joe Perches82592c32013-11-14 15:16:18 +1100321static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 .procname = "dev",
324 .maxlen = 0,
325 .mode = 0555,
326 .child = raid_dir_table,
327 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800328 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329};
330
NeilBrownf91de922005-11-08 21:39:36 -0800331static int start_readonly;
332
NeilBrown78b63502017-04-12 16:26:13 +1000333/*
334 * The original mechanism for creating an md device is to create
335 * a device node in /dev and to open it. This causes races with device-close.
336 * The preferred method is to write to the "new_array" module parameter.
337 * This can avoid races.
338 * Setting create_on_open to false disables the original mechanism
339 * so all the races disappear.
340 */
341static bool create_on_open = true;
342
NeilBrowna167f662010-10-26 18:31:13 +1100343struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100344 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100345{
Kent Overstreetafeee512018-05-20 18:25:52 -0400346 if (!mddev || !bioset_initialized(&mddev->bio_set))
NeilBrowna167f662010-10-26 18:31:13 +1100347 return bio_alloc(gfp_mask, nr_iovecs);
348
Marcos Paulo de Souza62516912019-01-14 06:31:56 -0700349 return bio_alloc_bioset(gfp_mask, nr_iovecs, &mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100350}
351EXPORT_SYMBOL_GPL(bio_alloc_mddev);
352
NeilBrown5a850712017-06-21 09:12:21 +1000353static struct bio *md_bio_alloc_sync(struct mddev *mddev)
354{
Kent Overstreetafeee512018-05-20 18:25:52 -0400355 if (!mddev || !bioset_initialized(&mddev->sync_set))
NeilBrown5a850712017-06-21 09:12:21 +1000356 return bio_alloc(GFP_NOIO, 1);
357
Kent Overstreetafeee512018-05-20 18:25:52 -0400358 return bio_alloc_bioset(GFP_NOIO, 1, &mddev->sync_set);
NeilBrown5a850712017-06-21 09:12:21 +1000359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361/*
NeilBrownd7603b72006-01-06 00:20:30 -0800362 * We have a system wide 'event count' that is incremented
363 * on any 'interesting' event, and readers of /proc/mdstat
364 * can use 'poll' or 'select' to find out when the event
365 * count increases.
366 *
367 * Events are:
368 * start array, stop array, error, add device, remove device,
369 * start build, activate spare
370 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800371static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800372static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100373void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800374{
375 atomic_inc(&md_event_count);
376 wake_up(&md_event_waiters);
377}
NeilBrown29269552006-03-27 01:18:10 -0800378EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800379
380/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 * Enables to iterate over all existing md arrays
382 * all_mddevs_lock protects this list.
383 */
384static LIST_HEAD(all_mddevs);
385static DEFINE_SPINLOCK(all_mddevs_lock);
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/*
388 * iterates through all used mddevs in the system.
389 * We take care to grab the all_mddevs_lock whenever navigating
390 * the list, and to always hold a refcount when unlocked.
391 * Any code which breaks out of this loop while own
392 * a reference to the current mddev and must mddev_put it.
393 */
NeilBrownfd01b882011-10-11 16:47:53 +1100394#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000396 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100397 _tmp = all_mddevs.next; \
398 _mddev = NULL;}); \
399 ({ if (_tmp != &all_mddevs) \
400 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100402 if (_mddev) mddev_put(_mddev); \
403 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
404 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100406 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 )
408
NeilBrown409c57f2009-03-31 14:39:39 +1100409/* Rather than calling directly into the personality make_request function,
410 * IO requests come here first so that we can check if the device is
411 * being suspended pending a reconfiguration.
412 * We hold a refcount over the call to ->make_request. By the time that
413 * call has finished, the bio has been linked into some internal structure
414 * and so is visible to ->quiesce(), so we don't need the refcount any more.
415 */
NeilBrownb3143b92017-10-17 13:46:43 +1100416static bool is_suspended(struct mddev *mddev, struct bio *bio)
417{
418 if (mddev->suspended)
419 return true;
420 if (bio_data_dir(bio) != WRITE)
421 return false;
422 if (mddev->suspend_lo >= mddev->suspend_hi)
423 return false;
424 if (bio->bi_iter.bi_sector >= mddev->suspend_hi)
425 return false;
426 if (bio_end_sector(bio) < mddev->suspend_lo)
427 return false;
428 return true;
429}
430
Shaohua Li393debc2017-09-21 10:23:35 -0700431void md_handle_request(struct mddev *mddev, struct bio *bio)
432{
433check_suspended:
434 rcu_read_lock();
NeilBrownb3143b92017-10-17 13:46:43 +1100435 if (is_suspended(mddev, bio)) {
Shaohua Li393debc2017-09-21 10:23:35 -0700436 DEFINE_WAIT(__wait);
437 for (;;) {
438 prepare_to_wait(&mddev->sb_wait, &__wait,
439 TASK_UNINTERRUPTIBLE);
NeilBrownb3143b92017-10-17 13:46:43 +1100440 if (!is_suspended(mddev, bio))
Shaohua Li393debc2017-09-21 10:23:35 -0700441 break;
442 rcu_read_unlock();
443 schedule();
444 rcu_read_lock();
445 }
446 finish_wait(&mddev->sb_wait, &__wait);
447 }
448 atomic_inc(&mddev->active_io);
449 rcu_read_unlock();
450
451 if (!mddev->pers->make_request(mddev, bio)) {
452 atomic_dec(&mddev->active_io);
453 wake_up(&mddev->sb_wait);
454 goto check_suspended;
455 }
456
457 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
458 wake_up(&mddev->sb_wait);
459}
460EXPORT_SYMBOL(md_handle_request);
461
Artur Paszkiewicz41d2d842020-07-03 11:13:09 +0200462struct md_io {
463 struct mddev *mddev;
464 bio_end_io_t *orig_bi_end_io;
465 void *orig_bi_private;
466 unsigned long start_time;
Song Liu00fe60e2020-08-31 15:27:24 -0700467 struct hd_struct *part;
Artur Paszkiewicz41d2d842020-07-03 11:13:09 +0200468};
469
470static void md_end_io(struct bio *bio)
471{
472 struct md_io *md_io = bio->bi_private;
473 struct mddev *mddev = md_io->mddev;
474
Song Liu00fe60e2020-08-31 15:27:24 -0700475 part_end_io_acct(md_io->part, bio, md_io->start_time);
Artur Paszkiewicz41d2d842020-07-03 11:13:09 +0200476
477 bio->bi_end_io = md_io->orig_bi_end_io;
478 bio->bi_private = md_io->orig_bi_private;
479
480 mempool_free(md_io, &mddev->md_io_pool);
481
482 if (bio->bi_end_io)
483 bio->bi_end_io(bio);
484}
485
Christoph Hellwigc62b37d2020-07-01 10:59:43 +0200486static blk_qc_t md_submit_bio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
NeilBrown49077322010-03-25 16:20:56 +1100488 const int rw = bio_data_dir(bio);
Christoph Hellwige4fc5a72020-05-08 18:15:14 +0200489 struct mddev *mddev = bio->bi_disk->private_data;
NeilBrown49077322010-03-25 16:20:56 +1100490
Colin Ian King9a5a8592020-07-02 12:35:02 +0100491 if (mddev == NULL || mddev->pers == NULL) {
492 bio_io_error(bio);
493 return BLK_QC_T_NONE;
494 }
NeilBrown409c57f2009-03-31 14:39:39 +1100495
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -0300496 if (unlikely(test_bit(MD_BROKEN, &mddev->flags)) && (rw == WRITE)) {
497 bio_io_error(bio);
498 return BLK_QC_T_NONE;
499 }
500
Christoph Hellwigf695ca32020-07-01 10:59:39 +0200501 blk_queue_split(&bio);
Kent Overstreet54efd502015-04-23 22:37:18 -0700502
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100503 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200504 if (bio_sectors(bio) != 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200505 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200506 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700507 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100508 }
NeilBrown49077322010-03-25 16:20:56 +1100509
Artur Paszkiewicz41d2d842020-07-03 11:13:09 +0200510 if (bio->bi_end_io != md_end_io) {
511 struct md_io *md_io;
512
513 md_io = mempool_alloc(&mddev->md_io_pool, GFP_NOIO);
514 md_io->mddev = mddev;
515 md_io->orig_bi_end_io = bio->bi_end_io;
516 md_io->orig_bi_private = bio->bi_private;
517
518 bio->bi_end_io = md_end_io;
519 bio->bi_private = md_io;
520
Song Liu00fe60e2020-08-31 15:27:24 -0700521 md_io->start_time = part_start_io_acct(mddev->gendisk,
522 &md_io->part, bio);
Artur Paszkiewicz41d2d842020-07-03 11:13:09 +0200523 }
524
Shaohua Li9c573de2016-04-25 16:52:38 -0700525 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600526 bio->bi_opf &= ~REQ_NOMERGE;
Shaohua Li393debc2017-09-21 10:23:35 -0700527
528 md_handle_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100529
Jens Axboedece1632015-11-05 10:41:16 -0700530 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
NeilBrown9e35b992010-04-06 14:23:02 +1000533/* mddev_suspend makes sure no new requests are submitted
534 * to the device, and that any requests that have been submitted
535 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100536 * Once mddev_detach() is called and completes, the module will be
537 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000538 */
NeilBrownfd01b882011-10-11 16:47:53 +1100539void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100540{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200541 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
NeilBrown4d5324f2017-10-19 12:17:16 +1100542 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100543 if (mddev->suspended++)
544 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100545 synchronize_rcu();
NeilBrowncc27b0c2017-06-05 16:49:39 +1000546 wake_up(&mddev->sb_wait);
NeilBrown35bfc522017-10-17 13:46:43 +1100547 set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
548 smp_mb__after_atomic();
NeilBrown409c57f2009-03-31 14:39:39 +1100549 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
550 mddev->pers->quiesce(mddev, 1);
NeilBrown35bfc522017-10-17 13:46:43 +1100551 clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags);
552 wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags));
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500553
554 del_timer_sync(&mddev->safemode_timer);
Coly Li78f57ef2020-04-09 22:17:20 +0800555 /* restrict memory reclaim I/O during raid array is suspend */
556 mddev->noio_flag = memalloc_noio_save();
NeilBrown409c57f2009-03-31 14:39:39 +1100557}
NeilBrown390ee602010-06-01 19:37:27 +1000558EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100559
NeilBrownfd01b882011-10-11 16:47:53 +1100560void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100561{
Coly Li78f57ef2020-04-09 22:17:20 +0800562 /* entred the memalloc scope from mddev_suspend() */
563 memalloc_noio_restore(mddev->noio_flag);
NeilBrown4d5324f2017-10-19 12:17:16 +1100564 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100565 if (--mddev->suspended)
566 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100567 wake_up(&mddev->sb_wait);
568 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500569
Jonathan Brassow47525e52012-05-22 13:55:29 +1000570 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500571 md_wakeup_thread(mddev->thread);
572 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100573}
NeilBrown390ee602010-06-01 19:37:27 +1000574EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100575
NeilBrowna2826aa2009-12-14 12:49:49 +1100576/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200577 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100578 */
NeilBrown4bc034d2019-03-29 10:46:16 -0700579
580static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100581{
NeilBrown4bc034d2019-03-29 10:46:16 -0700582 struct md_rdev *rdev = bio->bi_private;
583 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100584
585 rdev_dec_pending(rdev, mddev);
586
NeilBrown4bc034d2019-03-29 10:46:16 -0700587 if (atomic_dec_and_test(&mddev->flush_pending)) {
588 /* The pre-request flush has finished */
589 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100590 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700591 bio_put(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100592}
593
NeilBrown4bc034d2019-03-29 10:46:16 -0700594static void md_submit_flush_data(struct work_struct *ws);
595
596static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100597{
NeilBrown4bc034d2019-03-29 10:46:16 -0700598 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100599 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100600
NeilBrown2bc13b82019-03-29 10:46:17 -0700601 mddev->start_flush = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700602 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
603 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100604 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100605 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100606 if (rdev->raid_disk >= 0 &&
607 !test_bit(Faulty, &rdev->flags)) {
608 /* Take two references, one is dropped
609 * when request finishes, one after
610 * we reclaim rcu_read_lock
611 */
612 struct bio *bi;
613 atomic_inc(&rdev->nr_pending);
614 atomic_inc(&rdev->nr_pending);
615 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000616 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Xiao Ni5a409b42018-05-21 11:49:54 +0800617 bi->bi_end_io = md_end_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700618 bi->bi_private = rdev;
619 bio_set_dev(bi, rdev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600620 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
NeilBrown4bc034d2019-03-29 10:46:16 -0700621 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500622 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100623 rcu_read_lock();
624 rdev_dec_pending(rdev, mddev);
625 }
626 rcu_read_unlock();
NeilBrown4bc034d2019-03-29 10:46:16 -0700627 if (atomic_dec_and_test(&mddev->flush_pending))
628 queue_work(md_wq, &mddev->flush_work);
629}
NeilBrowna2826aa2009-12-14 12:49:49 +1100630
NeilBrown4bc034d2019-03-29 10:46:16 -0700631static void md_submit_flush_data(struct work_struct *ws)
632{
633 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
634 struct bio *bio = mddev->flush_bio;
635
636 /*
637 * must reset flush_bio before calling into md_handle_request to avoid a
638 * deadlock, because other bios passed md_handle_request suspend check
639 * could wait for this and below md_handle_request could wait for those
640 * bios because of suspend check
641 */
NeilBrown2bc13b82019-03-29 10:46:17 -0700642 mddev->last_flush = mddev->start_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700643 mddev->flush_bio = NULL;
644 wake_up(&mddev->sb_wait);
645
646 if (bio->bi_iter.bi_size == 0) {
647 /* an empty barrier - all done */
648 bio_endio(bio);
649 } else {
650 bio->bi_opf &= ~REQ_PREFLUSH;
651 md_handle_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100652 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100653}
NeilBrown4bc034d2019-03-29 10:46:16 -0700654
David Jeffery775d7832019-09-16 13:15:14 -0400655/*
656 * Manages consolidation of flushes and submitting any flushes needed for
657 * a bio with REQ_PREFLUSH. Returns true if the bio is finished or is
658 * being finished in another context. Returns false if the flushing is
659 * complete but still needs the I/O portion of the bio to be processed.
660 */
661bool md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrown4bc034d2019-03-29 10:46:16 -0700662{
NeilBrown2bc13b82019-03-29 10:46:17 -0700663 ktime_t start = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700664 spin_lock_irq(&mddev->lock);
665 wait_event_lock_irq(mddev->sb_wait,
NeilBrown2bc13b82019-03-29 10:46:17 -0700666 !mddev->flush_bio ||
667 ktime_after(mddev->last_flush, start),
NeilBrown4bc034d2019-03-29 10:46:16 -0700668 mddev->lock);
NeilBrown2bc13b82019-03-29 10:46:17 -0700669 if (!ktime_after(mddev->last_flush, start)) {
670 WARN_ON(mddev->flush_bio);
671 mddev->flush_bio = bio;
672 bio = NULL;
673 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700674 spin_unlock_irq(&mddev->lock);
675
NeilBrown2bc13b82019-03-29 10:46:17 -0700676 if (!bio) {
677 INIT_WORK(&mddev->flush_work, submit_flushes);
678 queue_work(md_wq, &mddev->flush_work);
679 } else {
680 /* flush was performed for some other bio while we waited. */
681 if (bio->bi_iter.bi_size == 0)
682 /* an empty barrier - all done */
683 bio_endio(bio);
684 else {
685 bio->bi_opf &= ~REQ_PREFLUSH;
David Jeffery775d7832019-09-16 13:15:14 -0400686 return false;
NeilBrown2bc13b82019-03-29 10:46:17 -0700687 }
688 }
David Jeffery775d7832019-09-16 13:15:14 -0400689 return true;
NeilBrown4bc034d2019-03-29 10:46:16 -0700690}
Tejun Heoe9c74692010-09-03 11:56:18 +0200691EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100692
NeilBrownfd01b882011-10-11 16:47:53 +1100693static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 atomic_inc(&mddev->active);
696 return mddev;
697}
698
Dan Williams5fd3a172009-03-04 00:57:25 -0700699static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100700
NeilBrownfd01b882011-10-11 16:47:53 +1100701static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
704 return;
NeilBrownd3374822009-01-09 08:31:10 +1100705 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100706 mddev->ctime == 0 && !mddev->hold_active) {
707 /* Array is not configured at all, and not held active,
708 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100709 list_del_init(&mddev->all_mddevs);
Kent Overstreet28dec872018-06-07 20:52:54 -0400710
711 /*
712 * Call queue_work inside the spinlock so that
713 * flush_workqueue() after mddev_find will succeed in waiting
714 * for the work to be done.
715 */
716 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
717 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100718 }
719 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Kees Cook8376d3c2017-10-16 17:01:48 -0700722static void md_safemode_timeout(struct timer_list *t);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400723
NeilBrownfd01b882011-10-11 16:47:53 +1100724void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100725{
Kent Overstreet28dec872018-06-07 20:52:54 -0400726 kobject_init(&mddev->kobj, &md_ktype);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100727 mutex_init(&mddev->open_mutex);
728 mutex_init(&mddev->reconfig_mutex);
729 mutex_init(&mddev->bitmap_info.mutex);
730 INIT_LIST_HEAD(&mddev->disks);
731 INIT_LIST_HEAD(&mddev->all_mddevs);
Kees Cook8376d3c2017-10-16 17:01:48 -0700732 timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100733 atomic_set(&mddev->active, 1);
734 atomic_set(&mddev->openers, 0);
735 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100736 spin_lock_init(&mddev->lock);
NeilBrown4bc034d2019-03-29 10:46:16 -0700737 atomic_set(&mddev->flush_pending, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100738 init_waitqueue_head(&mddev->sb_wait);
739 init_waitqueue_head(&mddev->recovery_wait);
740 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000741 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500742 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100743 mddev->resync_min = 0;
744 mddev->resync_max = MaxSector;
745 mddev->level = LEVEL_NONE;
746}
NeilBrown390ee602010-06-01 19:37:27 +1000747EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100748
NeilBrownf72ffdd2014-09-30 14:23:59 +1000749static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
NeilBrownfd01b882011-10-11 16:47:53 +1100751 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
NeilBrown8f5f02c2011-02-16 13:58:51 +1100753 if (unit && MAJOR(unit) != MD_MAJOR)
754 unit &= ~((1<<MdpMinorShift)-1);
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 retry:
757 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
NeilBrownefeb53c2009-01-09 08:31:10 +1100759 if (unit) {
760 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
761 if (mddev->unit == unit) {
762 mddev_get(mddev);
763 spin_unlock(&all_mddevs_lock);
764 kfree(new);
765 return mddev;
766 }
767
768 if (new) {
769 list_add(&new->all_mddevs, &all_mddevs);
770 spin_unlock(&all_mddevs_lock);
771 new->hold_active = UNTIL_IOCTL;
772 return new;
773 }
774 } else if (new) {
775 /* find an unused unit number */
776 static int next_minor = 512;
777 int start = next_minor;
778 int is_free = 0;
779 int dev = 0;
780 while (!is_free) {
781 dev = MKDEV(MD_MAJOR, next_minor);
782 next_minor++;
783 if (next_minor > MINORMASK)
784 next_minor = 0;
785 if (next_minor == start) {
786 /* Oh dear, all in use. */
787 spin_unlock(&all_mddevs_lock);
788 kfree(new);
789 return NULL;
790 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000791
NeilBrownefeb53c2009-01-09 08:31:10 +1100792 is_free = 1;
793 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
794 if (mddev->unit == dev) {
795 is_free = 0;
796 break;
797 }
798 }
799 new->unit = dev;
800 new->md_minor = MINOR(dev);
801 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 list_add(&new->all_mddevs, &all_mddevs);
803 spin_unlock(&all_mddevs_lock);
804 return new;
805 }
806 spin_unlock(&all_mddevs_lock);
807
NeilBrown9ffae0c2006-01-06 00:20:32 -0800808 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (!new)
810 return NULL;
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 new->unit = unit;
813 if (MAJOR(unit) == MD_MAJOR)
814 new->md_minor = MINOR(unit);
815 else
816 new->md_minor = MINOR(unit) >> MdpMinorShift;
817
NeilBrownfafd7fb2010-04-01 15:55:30 +1100818 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 goto retry;
821}
822
NeilBrownb6eb1272010-04-15 10:13:47 +1000823static struct attribute_group md_redundancy_group;
824
NeilBrown5c47daf2014-12-15 12:57:01 +1100825void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
NeilBrowna64c8762010-04-14 17:15:37 +1000827 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000828 /* These cannot be removed under reconfig_mutex as
829 * an access to the files will try to take reconfig_mutex
830 * while holding the file unremovable, which leads to
831 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000832 * So hold set sysfs_active while the remove in happeing,
833 * and anything else which might set ->to_remove or my
834 * otherwise change the sysfs namespace will fail with
835 * -EBUSY if sysfs_active is still set.
836 * We set sysfs_active under reconfig_mutex and elsewhere
837 * test it under the same mutex to ensure its correct value
838 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000839 */
NeilBrowna64c8762010-04-14 17:15:37 +1000840 struct attribute_group *to_remove = mddev->to_remove;
841 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000842 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000843 mutex_unlock(&mddev->reconfig_mutex);
844
NeilBrown00bcb4a2010-06-01 19:37:23 +1000845 if (mddev->kobj.sd) {
846 if (to_remove != &md_redundancy_group)
847 sysfs_remove_group(&mddev->kobj, to_remove);
848 if (mddev->pers == NULL ||
849 mddev->pers->sync_request == NULL) {
850 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
851 if (mddev->sysfs_action)
852 sysfs_put(mddev->sysfs_action);
Junxiao Bie8efa9b2020-08-04 17:27:18 -0700853 if (mddev->sysfs_completed)
854 sysfs_put(mddev->sysfs_completed);
855 if (mddev->sysfs_degraded)
856 sysfs_put(mddev->sysfs_degraded);
NeilBrown00bcb4a2010-06-01 19:37:23 +1000857 mddev->sysfs_action = NULL;
Junxiao Bie8efa9b2020-08-04 17:27:18 -0700858 mddev->sysfs_completed = NULL;
859 mddev->sysfs_degraded = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +1000860 }
NeilBrowna64c8762010-04-14 17:15:37 +1000861 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000862 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000863 } else
864 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Chris Dunlop751e67c2011-10-19 16:48:26 +1100866 /* As we've dropped the mutex we need a spinlock to
867 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000868 */
869 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700870 md_wakeup_thread(mddev->thread);
NeilBrown4d5324f2017-10-19 12:17:16 +1100871 wake_up(&mddev->sb_wait);
NeilBrown01f96c02011-09-21 15:30:20 +1000872 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
NeilBrown5c47daf2014-12-15 12:57:01 +1100874EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500876struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100877{
878 struct md_rdev *rdev;
879
880 rdev_for_each_rcu(rdev, mddev)
881 if (rdev->desc_nr == nr)
882 return rdev;
883
884 return NULL;
885}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500886EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100887
888static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
NeilBrown3cb03002011-10-11 16:45:26 +1100890 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
NeilBrowndafb20f2012-03-19 12:46:39 +1100892 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (rdev->bdev->bd_dev == dev)
894 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return NULL;
897}
898
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100899struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev)
NeilBrown1ca69c42012-10-11 13:37:33 +1100900{
901 struct md_rdev *rdev;
902
903 rdev_for_each_rcu(rdev, mddev)
904 if (rdev->bdev->bd_dev == dev)
905 return rdev;
906
907 return NULL;
908}
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100909EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100910
NeilBrown84fc4b52011-10-11 16:49:58 +1100911static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800912{
NeilBrown84fc4b52011-10-11 16:49:58 +1100913 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800914 list_for_each_entry(pers, &pers_list, list) {
915 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800916 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800917 if (strcmp(pers->name, clevel)==0)
918 return pers;
919 }
NeilBrown2604b702006-01-06 00:20:36 -0800920 return NULL;
921}
922
Andre Nollb73df2d2008-07-11 22:02:23 +1000923/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100924static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100926 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000927 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
NeilBrownf72ffdd2014-09-30 14:23:59 +1000930static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100933 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000934 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return 0;
936}
937
NeilBrown545c8792012-05-22 13:54:30 +1000938void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800941 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 rdev->sb_loaded = 0;
943 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000944 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100945 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
NeilBrown2699b672011-07-28 11:31:47 +1000947 if (rdev->bb_page) {
948 put_page(rdev->bb_page);
949 rdev->bb_page = NULL;
950 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800951 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
NeilBrown545c8792012-05-22 13:54:30 +1000953EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200955static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700956{
NeilBrown3cb03002011-10-11 16:45:26 +1100957 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100958 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700959
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200960 if (bio->bi_status) {
Guoqing Jiangb3db8a22020-07-28 12:01:41 +0200961 pr_err("md: %s gets error=%d\n", __func__,
962 blk_status_to_errno(bio->bi_status));
NeilBrowna9701a32005-11-08 21:39:34 -0800963 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100964 if (!test_bit(Faulty, &rdev->flags)
965 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800966 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100967 set_bit(LastDev, &rdev->flags);
968 }
969 } else
970 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700971
NeilBrowna9701a32005-11-08 21:39:34 -0800972 if (atomic_dec_and_test(&mddev->pending_writes))
973 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700974 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700975 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700976}
977
NeilBrownfd01b882011-10-11 16:47:53 +1100978void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700979 sector_t sector, int size, struct page *page)
980{
981 /* write first size bytes of page to sector of rdev
982 * Increment mddev->pending_writes before returning
983 * and decrement it on completion, waking up sb_wait
984 * if zero is reached.
985 * If an error occurred, call md_error
986 */
NeilBrown46533ff2016-11-18 16:16:11 +1100987 struct bio *bio;
988 int ff = 0;
989
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +0100990 if (!page)
991 return;
992
NeilBrown46533ff2016-11-18 16:16:11 +1100993 if (test_bit(Faulty, &rdev->flags))
994 return;
995
NeilBrown5a850712017-06-21 09:12:21 +1000996 bio = md_bio_alloc_sync(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700997
Shaohua Lied3b98c2016-03-29 14:00:19 -0700998 atomic_inc(&rdev->nr_pending);
999
Christoph Hellwig74d46992017-08-23 19:10:32 +02001000 bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001001 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001002 bio_add_page(bio, page, size, 0);
1003 bio->bi_private = rdev;
1004 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +11001005
1006 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
1007 test_bit(FailFast, &rdev->flags) &&
1008 !test_bit(LastDev, &rdev->flags))
1009 ff = MD_FAILFAST;
Jan Kara5a8948f2017-05-31 09:44:33 +02001010 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -08001011
NeilBrown7bfa19f2005-06-21 17:17:28 -07001012 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -05001013 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -08001014}
1015
NeilBrown46533ff2016-11-18 16:16:11 +11001016int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -08001017{
Tejun Heoe9c74692010-09-03 11:56:18 +02001018 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +10001019 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -08001020 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +11001021 return -EAGAIN;
1022 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001023}
1024
NeilBrown3cb03002011-10-11 16:45:26 +11001025int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -05001026 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
NeilBrown5a850712017-06-21 09:12:21 +10001028 struct bio *bio = md_bio_alloc_sync(rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 int ret;
1030
Christoph Hellwig74d46992017-08-23 19:10:32 +02001031 if (metadata_op && rdev->meta_bdev)
1032 bio_set_dev(bio, rdev->meta_bdev);
1033 else
1034 bio_set_dev(bio, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -05001035 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001036 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -07001037 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +10001038 else if (rdev->mddev->reshape_position != MaxSector &&
1039 (rdev->mddev->reshape_backwards ==
1040 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001041 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001042 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001043 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -05001045
1046 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001048 ret = !bio->bi_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 bio_put(bio);
1050 return ret;
1051}
NeilBrowna8745db2006-01-06 00:20:34 -08001052EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
NeilBrownf72ffdd2014-09-30 14:23:59 +10001054static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (rdev->sb_loaded)
1059 return 0;
1060
Mike Christie796a5cf2016-06-05 14:32:07 -05001061 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 goto fail;
1063 rdev->sb_loaded = 1;
1064 return 0;
1065
1066fail:
NeilBrown9d487392016-11-02 14:16:49 +11001067 pr_err("md: disabled device %s, could not read superblock.\n",
1068 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return -EINVAL;
1070}
1071
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001072static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
NeilBrownf72ffdd2014-09-30 14:23:59 +10001074 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +10001075 sb1->set_uuid1 == sb2->set_uuid1 &&
1076 sb1->set_uuid2 == sb2->set_uuid2 &&
1077 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001080static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
1082 int ret;
1083 mdp_super_t *tmp1, *tmp2;
1084
1085 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
1086 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
1087
1088 if (!tmp1 || !tmp2) {
1089 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 goto abort;
1091 }
1092
1093 *tmp1 = *sb1;
1094 *tmp2 = *sb2;
1095
1096 /*
1097 * nr_disks is not constant
1098 */
1099 tmp1->nr_disks = 0;
1100 tmp2->nr_disks = 0;
1101
Andre Nollce0c8e02008-07-11 22:02:20 +10001102 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001104 kfree(tmp1);
1105 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return ret;
1107}
1108
NeilBrown4d167f02007-05-09 02:35:37 -07001109static u32 md_csum_fold(u32 csum)
1110{
1111 csum = (csum & 0xffff) + (csum >> 16);
1112 return (csum & 0xffff) + (csum >> 16);
1113}
1114
NeilBrownf72ffdd2014-09-30 14:23:59 +10001115static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
NeilBrown4d167f02007-05-09 02:35:37 -07001117 u64 newcsum = 0;
1118 u32 *sb32 = (u32*)sb;
1119 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 unsigned int disk_csum, csum;
1121
1122 disk_csum = sb->sb_csum;
1123 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -07001124
1125 for (i = 0; i < MD_SB_BYTES/4 ; i++)
1126 newcsum += sb32[i];
1127 csum = (newcsum & 0xffffffff) + (newcsum>>32);
1128
NeilBrown4d167f02007-05-09 02:35:37 -07001129#ifdef CONFIG_ALPHA
1130 /* This used to use csum_partial, which was wrong for several
1131 * reasons including that different results are returned on
1132 * different architectures. It isn't critical that we get exactly
1133 * the same return value as before (we always csum_fold before
1134 * testing, and that removes any differences). However as we
1135 * know that csum_partial always returned a 16bit value on
1136 * alphas, do a fold to maximise conformity to previous behaviour.
1137 */
1138 sb->sb_csum = md_csum_fold(disk_csum);
1139#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -07001141#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return csum;
1143}
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145/*
1146 * Handle superblock details.
1147 * We want to be able to handle multiple superblock formats
1148 * so we have a common interface to them all, and an array of
1149 * different handlers.
1150 * We rely on user-space to write the initial superblock, and support
1151 * reading and updating of superblocks.
1152 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +11001153 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 * loads and validates a superblock on dev.
1155 * if refdev != NULL, compare superblocks on both devices
1156 * Return:
1157 * 0 - dev has a superblock that is compatible with refdev
1158 * 1 - dev has a superblock that is compatible and newer than refdev
1159 * so dev should be used as the refdev in future
1160 * -EINVAL superblock incompatible or invalid
1161 * -othererror e.g. -EIO
1162 *
NeilBrownfd01b882011-10-11 16:47:53 +11001163 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 * Verify that dev is acceptable into mddev.
1165 * The first time, mddev->raid_disks will be 0, and data from
1166 * dev should be merged in. Subsequent calls check that dev
1167 * is new enough. Return 0 or -EINVAL
1168 *
NeilBrownfd01b882011-10-11 16:47:53 +11001169 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 * Update the superblock for rdev with data in mddev
1171 * This does not write to disc.
1172 *
1173 */
1174
1175struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001176 char *name;
1177 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +10001178 int (*load_super)(struct md_rdev *rdev,
1179 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001180 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +10001181 int (*validate_super)(struct mddev *mddev,
1182 struct md_rdev *rdev);
1183 void (*sync_super)(struct mddev *mddev,
1184 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001185 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001186 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +10001187 int (*allow_new_offset)(struct md_rdev *rdev,
1188 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189};
1190
1191/*
Andre Noll0894cc32009-06-18 08:49:23 +10001192 * Check that the given mddev has no bitmap.
1193 *
1194 * This function is called from the run method of all personalities that do not
1195 * support bitmaps. It prints an error message and returns non-zero if mddev
1196 * has a bitmap. Otherwise, it returns 0.
1197 *
1198 */
NeilBrownfd01b882011-10-11 16:47:53 +11001199int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001200{
NeilBrownc3d97142009-12-14 12:49:52 +11001201 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001202 return 0;
NeilBrown9d487392016-11-02 14:16:49 +11001203 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +10001204 mdname(mddev), mddev->pers->name);
1205 return 1;
1206}
1207EXPORT_SYMBOL(md_check_no_bitmap);
1208
1209/*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001210 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 */
NeilBrown3cb03002011-10-11 16:45:26 +11001212static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1215 mdp_super_t *sb;
1216 int ret;
Yufen Yu228fc7d2019-10-30 18:47:02 +08001217 bool spare_disk = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 /*
Andre Noll0f420352008-07-11 22:02:23 +10001220 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 * it's at the end of the disk.
1222 *
1223 * It also happens to be a multiple of 4Kb.
1224 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001225 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
NeilBrown0002b272005-09-09 16:23:53 -07001227 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +11001228 if (ret)
1229 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 ret = -EINVAL;
1232
1233 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001234 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +11001237 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 goto abort;
1239 }
1240
1241 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001242 sb->minor_version < 90 ||
1243 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +11001244 pr_warn("Bad version number %d.%d on %s\n",
1245 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 goto abort;
1247 }
1248
1249 if (sb->raid_disks <= 0)
1250 goto abort;
1251
NeilBrown4d167f02007-05-09 02:35:37 -07001252 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001253 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 goto abort;
1255 }
1256
1257 rdev->preferred_minor = sb->md_minor;
1258 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001259 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001260 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001261 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 if (sb->level == LEVEL_MULTIPATH)
1264 rdev->desc_nr = -1;
1265 else
1266 rdev->desc_nr = sb->this_disk.number;
1267
Yufen Yu228fc7d2019-10-30 18:47:02 +08001268 /* not spare disk, or LEVEL_MULTIPATH */
1269 if (sb->level == LEVEL_MULTIPATH ||
1270 (rdev->desc_nr >= 0 &&
Yufen Yu3b7436c2019-12-10 15:01:29 +08001271 rdev->desc_nr < MD_SB_DISKS &&
Yufen Yu228fc7d2019-10-30 18:47:02 +08001272 sb->disks[rdev->desc_nr].state &
1273 ((1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))))
1274 spare_disk = false;
1275
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001276 if (!refdev) {
Yufen Yu228fc7d2019-10-30 18:47:02 +08001277 if (!spare_disk)
Yufen Yu6a5cb532019-10-16 16:00:03 +08001278 ret = 1;
1279 else
1280 ret = 0;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001281 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001283 mdp_super_t *refsb = page_address(refdev->sb_page);
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001284 if (!md_uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001285 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 b, bdevname(refdev->bdev,b2));
1287 goto abort;
1288 }
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001289 if (!md_sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001290 pr_warn("md: %s has same UUID but different superblock to %s\n",
1291 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 goto abort;
1293 }
1294 ev1 = md_event(sb);
1295 ev2 = md_event(refsb);
Yufen Yu6a5cb532019-10-16 16:00:03 +08001296
Yufen Yu228fc7d2019-10-30 18:47:02 +08001297 if (!spare_disk && ev1 > ev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001299 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 ret = 0;
1301 }
NeilBrown8190e752009-06-18 08:48:58 +10001302 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001303 /* Limit to 4TB as metadata cannot record more than that.
1304 * (not needed for Linear and RAID0 as metadata doesn't
1305 * record this size)
1306 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001307 if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001308 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
NeilBrown27a7b262011-09-10 17:21:28 +10001310 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001311 /* "this cannot possibly happen" ... */
1312 ret = -EINVAL;
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 abort:
1315 return ret;
1316}
1317
1318/*
1319 * validate_super for 0.90.0
1320 */
NeilBrownfd01b882011-10-11 16:47:53 +11001321static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001324 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001325 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
NeilBrown41158c72005-06-21 17:17:25 -07001327 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001328 clear_bit(Faulty, &rdev->flags);
1329 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001330 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001331 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (mddev->raid_disks == 0) {
1334 mddev->major_version = 0;
1335 mddev->minor_version = sb->minor_version;
1336 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001337 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001338 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 mddev->ctime = sb->ctime;
1340 mddev->utime = sb->utime;
1341 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001342 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 mddev->layout = sb->layout;
1344 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001345 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001346 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001347 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001348 mddev->bitmap_info.space = 0;
1349 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001350 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001351 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001352 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
NeilBrownf6705572006-03-27 01:18:11 -08001354 if (mddev->minor_version >= 91) {
1355 mddev->reshape_position = sb->reshape_position;
1356 mddev->delta_disks = sb->delta_disks;
1357 mddev->new_level = sb->new_level;
1358 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001359 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001360 if (mddev->delta_disks < 0)
1361 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001362 } else {
1363 mddev->reshape_position = MaxSector;
1364 mddev->delta_disks = 0;
1365 mddev->new_level = mddev->level;
1366 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001367 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001368 }
NeilBrown33f2c352019-09-09 16:52:29 +10001369 if (mddev->level == 0)
1370 mddev->layout = -1;
NeilBrownf6705572006-03-27 01:18:11 -08001371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (sb->state & (1<<MD_SB_CLEAN))
1373 mddev->recovery_cp = MaxSector;
1374 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001375 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 sb->events_lo == sb->cp_events_lo) {
1377 mddev->recovery_cp = sb->recovery_cp;
1378 } else
1379 mddev->recovery_cp = 0;
1380 }
1381
1382 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1383 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1384 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1385 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1386
1387 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001388
1389 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001390 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001391 mddev->bitmap_info.offset =
1392 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001393 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001394 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001395 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001396
NeilBrown41158c72005-06-21 17:17:25 -07001397 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001398 /* Insist on good event counter while assembling, except
1399 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001401 if (sb->disks[rdev->desc_nr].state & (
1402 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001403 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001404 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001405 } else if (mddev->bitmap) {
1406 /* if adding to array with a bitmap, then we can accept an
1407 * older device ... but not too old.
1408 */
NeilBrown41158c72005-06-21 17:17:25 -07001409 if (ev1 < mddev->bitmap->events_cleared)
1410 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001411 if (ev1 < mddev->events)
1412 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001413 } else {
1414 if (ev1 < mddev->events)
1415 /* just a hot-add of a new device, leave raid_disk at -1 */
1416 return 0;
1417 }
NeilBrown41158c72005-06-21 17:17:25 -07001418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 desc = sb->disks + rdev->desc_nr;
1421
1422 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001423 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001424 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1425 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001426 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001428 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001429 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1430 /* active but not in sync implies recovery up to
1431 * reshape position. We don't know exactly where
1432 * that is, so set to zero for now */
1433 if (mddev->minor_version >= 91) {
1434 rdev->recovery_offset = 0;
1435 rdev->raid_disk = desc->raid_disk;
1436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001438 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1439 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001440 if (desc->state & (1<<MD_DISK_FAILFAST))
1441 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001442 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001443 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return 0;
1445}
1446
1447/*
1448 * sync_super for 0.90.0
1449 */
NeilBrownfd01b882011-10-11 16:47:53 +11001450static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
1452 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001453 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 /* make rdev->sb match mddev data..
1457 *
1458 * 1/ zero out disks
1459 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1460 * 3/ any empty disks < next_spare become removed
1461 *
1462 * disks[0] gets initialised to REMOVED because
1463 * we cannot be sure from other fields if it has
1464 * been initialised or not.
1465 */
1466 int i;
1467 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1468
NeilBrown61181562005-09-09 16:24:02 -07001469 rdev->sb_size = MD_SB_BYTES;
1470
Namhyung Kim65a06f062011-07-27 11:00:36 +10001471 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 memset(sb, 0, sizeof(*sb));
1474
1475 sb->md_magic = MD_SB_MAGIC;
1476 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 sb->patch_version = mddev->patch_version;
1478 sb->gvalid_words = 0; /* ignored */
1479 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1480 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1481 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1482 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1483
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001484 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001486 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 sb->raid_disks = mddev->raid_disks;
1488 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001489 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001490 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 sb->state = 0;
1492 sb->events_hi = (mddev->events>>32);
1493 sb->events_lo = (u32)mddev->events;
1494
NeilBrownf6705572006-03-27 01:18:11 -08001495 if (mddev->reshape_position == MaxSector)
1496 sb->minor_version = 90;
1497 else {
1498 sb->minor_version = 91;
1499 sb->reshape_position = mddev->reshape_position;
1500 sb->new_level = mddev->new_level;
1501 sb->delta_disks = mddev->delta_disks;
1502 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001503 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001504 }
1505 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (mddev->in_sync)
1507 {
1508 sb->recovery_cp = mddev->recovery_cp;
1509 sb->cp_events_hi = (mddev->events>>32);
1510 sb->cp_events_lo = (u32)mddev->events;
1511 if (mddev->recovery_cp == MaxSector)
1512 sb->state = (1<< MD_SB_CLEAN);
1513 } else
1514 sb->recovery_cp = 0;
1515
1516 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001517 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
NeilBrownc3d97142009-12-14 12:49:52 +11001519 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001520 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001523 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001525 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001526 int is_active = test_bit(In_sync, &rdev2->flags);
1527
1528 if (rdev2->raid_disk >= 0 &&
1529 sb->minor_version >= 91)
1530 /* we have nowhere to store the recovery_offset,
1531 * but if it is not below the reshape_position,
1532 * we can piggy-back on that.
1533 */
1534 is_active = 1;
1535 if (rdev2->raid_disk < 0 ||
1536 test_bit(Faulty, &rdev2->flags))
1537 is_active = 0;
1538 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001539 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001541 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001542 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 d = &sb->disks[rdev2->desc_nr];
1544 nr_disks++;
1545 d->number = rdev2->desc_nr;
1546 d->major = MAJOR(rdev2->bdev->bd_dev);
1547 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001548 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 d->raid_disk = rdev2->raid_disk;
1550 else
1551 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001552 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001554 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001556 if (test_bit(In_sync, &rdev2->flags))
1557 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 active++;
1559 working++;
1560 } else {
1561 d->state = 0;
1562 spare++;
1563 working++;
1564 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001565 if (test_bit(WriteMostly, &rdev2->flags))
1566 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001567 if (test_bit(FailFast, &rdev2->flags))
1568 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /* now set the "removed" and "faulty" bits on any missing devices */
1571 for (i=0 ; i < mddev->raid_disks ; i++) {
1572 mdp_disk_t *d = &sb->disks[i];
1573 if (d->state == 0 && d->number == 0) {
1574 d->number = i;
1575 d->raid_disk = i;
1576 d->state = (1<<MD_DISK_REMOVED);
1577 d->state |= (1<<MD_DISK_FAULTY);
1578 failed++;
1579 }
1580 }
1581 sb->nr_disks = nr_disks;
1582 sb->active_disks = active;
1583 sb->working_disks = working;
1584 sb->failed_disks = failed;
1585 sb->spare_disks = spare;
1586
1587 sb->this_disk = sb->disks[rdev->desc_nr];
1588 sb->sb_csum = calc_sb_csum(sb);
1589}
1590
1591/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001592 * rdev_size_change for 0.90.0
1593 */
1594static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001595super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001596{
Andre Noll58c0fed2009-03-31 14:33:13 +11001597 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001598 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001599 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001600 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001601 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001602 if (!num_sectors || num_sectors > rdev->sb_start)
1603 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001604 /* Limit to 4TB as metadata cannot record more than that.
1605 * 4TB == 2^32 KB, or 2*2^32 sectors.
1606 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001607 if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001608 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001609 do {
1610 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001611 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001612 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001613 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001614}
1615
NeilBrownc6563a82012-05-21 09:27:00 +10001616static int
1617super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1618{
1619 /* non-zero offset changes not possible with v0.90 */
1620 return new_offset == 0;
1621}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001622
1623/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 * version 1 superblock
1625 */
1626
NeilBrownf72ffdd2014-09-30 14:23:59 +10001627static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001629 __le32 disk_csum;
1630 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 unsigned long long newcsum;
1632 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001633 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 disk_csum = sb->sb_csum;
1636 sb->sb_csum = 0;
1637 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001638 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 newcsum += le32_to_cpu(*isuper++);
1640
1641 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001642 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1645 sb->sb_csum = disk_csum;
1646 return cpu_to_le32(csum);
1647}
1648
NeilBrown3cb03002011-10-11 16:45:26 +11001649static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 struct mdp_superblock_1 *sb;
1652 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001653 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001654 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001656 int bmask;
Yufen Yu228fc7d2019-10-30 18:47:02 +08001657 bool spare_disk = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 /*
Andre Noll0f420352008-07-11 22:02:23 +10001660 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 * It is always aligned to a 4K boundary and
1662 * depeding on minor_version, it can be:
1663 * 0: At least 8K, but less than 12K, from end of device
1664 * 1: At start of device
1665 * 2: 4K from start of device.
1666 */
1667 switch(minor_version) {
1668 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001669 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001670 sb_start -= 8*2;
1671 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 break;
1673 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001674 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 break;
1676 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001677 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 break;
1679 default:
1680 return -EINVAL;
1681 }
Andre Noll0f420352008-07-11 22:02:23 +10001682 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
NeilBrown0002b272005-09-09 16:23:53 -07001684 /* superblock is rarely larger than 1K, but it can be larger,
1685 * and it is safe to read 4k, so we do that
1686 */
1687 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (ret) return ret;
1689
Namhyung Kim65a06f062011-07-27 11:00:36 +10001690 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1693 sb->major_version != cpu_to_le32(1) ||
1694 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001695 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001696 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return -EINVAL;
1698
1699 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001700 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 bdevname(rdev->bdev,b));
1702 return -EINVAL;
1703 }
1704 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001705 pr_warn("md: data_size too small on %s\n",
1706 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return -EINVAL;
1708 }
NeilBrownc6563a82012-05-21 09:27:00 +10001709 if (sb->pad0 ||
1710 sb->pad3[0] ||
1711 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1712 /* Some padding is non-zero, might be a new feature */
1713 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 rdev->preferred_minor = 0xffff;
1716 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001717 rdev->new_data_offset = rdev->data_offset;
1718 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1719 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1720 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001721 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
NeilBrown0002b272005-09-09 16:23:53 -07001723 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001724 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001725 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001726 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1727
1728 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001729 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001730 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001731 if (minor_version
1732 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1733 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001734
NeilBrown31b65a02006-07-10 04:44:14 -07001735 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1736 rdev->desc_nr = -1;
1737 else
1738 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1739
NeilBrown2699b672011-07-28 11:31:47 +10001740 if (!rdev->bb_page) {
1741 rdev->bb_page = alloc_page(GFP_KERNEL);
1742 if (!rdev->bb_page)
1743 return -ENOMEM;
1744 }
1745 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1746 rdev->badblocks.count == 0) {
1747 /* need to load the bad block list.
1748 * Currently we limit it to one page.
1749 */
1750 s32 offset;
1751 sector_t bb_sector;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001752 __le64 *bbp;
NeilBrown2699b672011-07-28 11:31:47 +10001753 int i;
1754 int sectors = le16_to_cpu(sb->bblog_size);
1755 if (sectors > (PAGE_SIZE / 512))
1756 return -EINVAL;
1757 offset = le32_to_cpu(sb->bblog_offset);
1758 if (offset == 0)
1759 return -EINVAL;
1760 bb_sector = (long long)offset;
1761 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001762 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001763 return -EIO;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001764 bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10001765 rdev->badblocks.shift = sb->bblog_shift;
1766 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1767 u64 bb = le64_to_cpu(*bbp);
1768 int count = bb & (0x3ff);
1769 u64 sector = bb >> 10;
1770 sector <<= sb->bblog_shift;
1771 count <<= sb->bblog_shift;
1772 if (bb + 1 == 0)
1773 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001774 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001775 return -EINVAL;
1776 }
NeilBrown486adf72013-04-24 11:42:44 +10001777 } else if (sb->bblog_offset != 0)
1778 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001779
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001780 if ((le32_to_cpu(sb->feature_map) &
1781 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001782 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1783 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1784 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1785 }
1786
NeilBrown33f2c352019-09-09 16:52:29 +10001787 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT) &&
1788 sb->level != 0)
1789 return -EINVAL;
1790
Yufen Yu228fc7d2019-10-30 18:47:02 +08001791 /* not spare disk, or LEVEL_MULTIPATH */
1792 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH) ||
1793 (rdev->desc_nr >= 0 &&
1794 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1795 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1796 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)))
1797 spare_disk = false;
Yufen Yu6a5cb532019-10-16 16:00:03 +08001798
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001799 if (!refdev) {
Yufen Yu228fc7d2019-10-30 18:47:02 +08001800 if (!spare_disk)
Yufen Yu6a5cb532019-10-16 16:00:03 +08001801 ret = 1;
1802 else
1803 ret = 0;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001804 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001806 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1809 sb->level != refsb->level ||
1810 sb->layout != refsb->layout ||
1811 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001812 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 bdevname(rdev->bdev,b),
1814 bdevname(refdev->bdev,b2));
1815 return -EINVAL;
1816 }
1817 ev1 = le64_to_cpu(sb->events);
1818 ev2 = le64_to_cpu(refsb->events);
1819
Yufen Yu228fc7d2019-10-30 18:47:02 +08001820 if (!spare_disk && ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001821 ret = 1;
1822 else
1823 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
NeilBrownc6563a82012-05-21 09:27:00 +10001825 if (minor_version) {
1826 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1827 sectors -= rdev->data_offset;
1828 } else
1829 sectors = rdev->sb_start;
1830 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001832 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001833 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
NeilBrownfd01b882011-10-11 16:47:53 +11001836static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001838 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001839 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
NeilBrown41158c72005-06-21 17:17:25 -07001841 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001842 clear_bit(Faulty, &rdev->flags);
1843 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001844 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001845 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (mddev->raid_disks == 0) {
1848 mddev->major_version = 1;
1849 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001850 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001851 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001852 mddev->ctime = le64_to_cpu(sb->ctime);
1853 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001855 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 mddev->layout = le32_to_cpu(sb->layout);
1857 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001858 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001859 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001860 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001861 mddev->bitmap_info.space = 0;
1862 /* Default location for bitmap is 1K after superblock
1863 * using 3K - total of 4K
1864 */
NeilBrownc3d97142009-12-14 12:49:52 +11001865 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001866 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001867 mddev->reshape_backwards = 0;
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1870 memcpy(mddev->uuid, sb->set_uuid, 16);
1871
1872 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001873
NeilBrown71c08052005-09-09 16:23:51 -07001874 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001875 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001876 mddev->bitmap_info.offset =
1877 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001878 /* Metadata doesn't record how much space is available.
1879 * For 1.0, we assume we can use up to the superblock
1880 * if before, else to 4K beyond superblock.
1881 * For others, assume no change is possible.
1882 */
1883 if (mddev->minor_version > 0)
1884 mddev->bitmap_info.space = 0;
1885 else if (mddev->bitmap_info.offset > 0)
1886 mddev->bitmap_info.space =
1887 8 - mddev->bitmap_info.offset;
1888 else
1889 mddev->bitmap_info.space =
1890 -mddev->bitmap_info.offset;
1891 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001892
NeilBrownf6705572006-03-27 01:18:11 -08001893 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1894 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1895 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1896 mddev->new_level = le32_to_cpu(sb->new_level);
1897 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001898 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001899 if (mddev->delta_disks < 0 ||
1900 (mddev->delta_disks == 0 &&
1901 (le32_to_cpu(sb->feature_map)
1902 & MD_FEATURE_RESHAPE_BACKWARDS)))
1903 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001904 } else {
1905 mddev->reshape_position = MaxSector;
1906 mddev->delta_disks = 0;
1907 mddev->new_level = mddev->level;
1908 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001909 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001910 }
1911
NeilBrown33f2c352019-09-09 16:52:29 +10001912 if (mddev->level == 0 &&
1913 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT))
1914 mddev->layout = -1;
1915
Song Liu486b0f72016-08-19 15:34:01 -07001916 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001917 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001918
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001919 if (le32_to_cpu(sb->feature_map) &
1920 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001921 if (le32_to_cpu(sb->feature_map) &
1922 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1923 return -EINVAL;
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001924 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1925 (le32_to_cpu(sb->feature_map) &
1926 MD_FEATURE_MULTIPLE_PPLS))
1927 return -EINVAL;
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001928 set_bit(MD_HAS_PPL, &mddev->flags);
1929 }
NeilBrown41158c72005-06-21 17:17:25 -07001930 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001931 /* Insist of good event counter while assembling, except for
1932 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001934 if (rdev->desc_nr >= 0 &&
1935 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001936 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1937 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001938 if (ev1 < mddev->events)
1939 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001940 } else if (mddev->bitmap) {
1941 /* If adding to array with a bitmap, then we can accept an
1942 * older device, but not too old.
1943 */
NeilBrown41158c72005-06-21 17:17:25 -07001944 if (ev1 < mddev->bitmap->events_cleared)
1945 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001946 if (ev1 < mddev->events)
1947 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001948 } else {
1949 if (ev1 < mddev->events)
1950 /* just a hot-add of a new device, leave raid_disk at -1 */
1951 return 0;
1952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (mddev->level != LEVEL_MULTIPATH) {
1954 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001955 if (rdev->desc_nr < 0 ||
1956 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001957 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001958 rdev->desc_nr = -1;
1959 } else
1960 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001962 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001964 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001965 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 break;
Song Liubac624f2015-08-13 14:31:55 -07001967 case MD_DISK_ROLE_JOURNAL: /* journal device */
1968 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1969 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001970 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001971 return -EINVAL;
1972 }
1973 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001974 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001975 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001976 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 default:
NeilBrownf4667222013-12-09 12:04:56 +11001978 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001979 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001980 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001981 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001982 if (!(le32_to_cpu(sb->feature_map) &
1983 MD_FEATURE_RECOVERY_BITMAP))
1984 rdev->saved_raid_disk = -1;
Guoqing Jiang062f5b2a2019-07-24 11:09:20 +02001985 } else {
1986 /*
1987 * If the array is FROZEN, then the device can't
1988 * be in_sync with rest of array.
1989 */
1990 if (!test_bit(MD_RECOVERY_FROZEN,
1991 &mddev->recovery))
1992 set_bit(In_sync, &rdev->flags);
1993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 rdev->raid_disk = role;
1995 break;
1996 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001997 if (sb->devflags & WriteMostly1)
1998 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001999 if (sb->devflags & FailFast1)
2000 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002001 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
2002 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07002003 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08002004 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07002005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return 0;
2007}
2008
NeilBrownfd01b882011-10-11 16:47:53 +11002009static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
2011 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11002012 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 int max_dev, i;
2014 /* make rdev->sb match mddev and rdev data. */
2015
Namhyung Kim65a06f062011-07-27 11:00:36 +10002016 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 sb->feature_map = 0;
2019 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002020 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 memset(sb->pad3, 0, sizeof(sb->pad3));
2022
2023 sb->utime = cpu_to_le64((__u64)mddev->utime);
2024 sb->events = cpu_to_le64(mddev->events);
2025 if (mddev->in_sync)
2026 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07002027 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
2028 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 else
2030 sb->resync_offset = cpu_to_le64(0);
2031
NeilBrown1c05b4b2006-10-21 10:24:08 -07002032 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08002033
NeilBrownf0ca3402006-02-02 14:28:04 -08002034 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11002035 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10002036 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10002037 sb->level = cpu_to_le32(mddev->level);
2038 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11002039 if (test_bit(FailFast, &rdev->flags))
2040 sb->devflags |= FailFast1;
2041 else
2042 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08002043
NeilBrownaeb9b2112011-08-25 14:43:08 +10002044 if (test_bit(WriteMostly, &rdev->flags))
2045 sb->devflags |= WriteMostly1;
2046 else
2047 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10002048 sb->data_offset = cpu_to_le64(rdev->data_offset);
2049 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10002050
NeilBrownc3d97142009-12-14 12:49:52 +11002051 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
2052 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07002053 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07002054 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002055
Shaohua Lif2076e72015-10-08 21:54:12 -07002056 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11002057 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11002058 sb->feature_map |=
2059 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
2060 sb->recovery_offset =
2061 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11002062 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
2063 sb->feature_map |=
2064 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002065 }
Shaohua Li3069aa82015-08-13 14:31:56 -07002066 /* Note: recovery_offset and journal_tail share space */
2067 if (test_bit(Journal, &rdev->flags))
2068 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002069 if (test_bit(Replacement, &rdev->flags))
2070 sb->feature_map |=
2071 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002072
NeilBrownf6705572006-03-27 01:18:11 -08002073 if (mddev->reshape_position != MaxSector) {
2074 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
2075 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
2076 sb->new_layout = cpu_to_le32(mddev->new_layout);
2077 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
2078 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10002079 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10002080 if (mddev->delta_disks == 0 &&
2081 mddev->reshape_backwards)
2082 sb->feature_map
2083 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10002084 if (rdev->new_data_offset != rdev->data_offset) {
2085 sb->feature_map
2086 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
2087 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
2088 - rdev->data_offset));
2089 }
NeilBrownf6705572006-03-27 01:18:11 -08002090 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002091
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10002092 if (mddev_is_clustered(mddev))
2093 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
2094
NeilBrown2699b672011-07-28 11:31:47 +10002095 if (rdev->badblocks.count == 0)
2096 /* Nothing to do for bad blocks*/ ;
2097 else if (sb->bblog_offset == 0)
2098 /* Cannot record bad blocks on this device */
2099 md_error(mddev, rdev);
2100 else {
2101 struct badblocks *bb = &rdev->badblocks;
Christoph Hellwigae506402019-04-04 18:56:13 +02002102 __le64 *bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10002103 u64 *p = bb->page;
2104 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
2105 if (bb->changed) {
2106 unsigned seq;
2107
2108retry:
2109 seq = read_seqbegin(&bb->lock);
2110
2111 memset(bbp, 0xff, PAGE_SIZE);
2112
2113 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08002114 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10002115 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
2116 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08002117 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10002118 }
NeilBrownd0962932012-03-19 12:46:41 +11002119 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002120 if (read_seqretry(&bb->lock, seq))
2121 goto retry;
2122
2123 bb->sector = (rdev->sb_start +
2124 (int)le32_to_cpu(sb->bblog_offset));
2125 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10002126 }
2127 }
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11002130 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (rdev2->desc_nr+1 > max_dev)
2132 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07002133
NeilBrown70471da2009-08-03 10:59:57 +10002134 if (max_dev > le32_to_cpu(sb->max_dev)) {
2135 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07002136 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10002137 rdev->sb_size = max_dev * 2 + 256;
2138 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
2139 if (rdev->sb_size & bmask)
2140 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10002141 } else
2142 max_dev = le32_to_cpu(sb->max_dev);
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 for (i=0; i<max_dev;i++)
Lidong Zhong8df72022017-06-12 10:45:55 +08002145 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
NeilBrownf72ffdd2014-09-30 14:23:59 +10002146
Song Liua97b7892015-10-08 21:54:09 -07002147 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
2148 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01002150 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
Pawel Baldysiakddc08822017-08-16 17:13:45 +02002151 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
2152 sb->feature_map |=
2153 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
2154 else
2155 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01002156 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
2157 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
2158 }
2159
NeilBrowndafb20f2012-03-19 12:46:39 +11002160 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002162 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07002163 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08002164 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07002166 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07002167 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11002168 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002169 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 else
Song Liuc4d4c912015-08-13 14:31:54 -07002171 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 sb->sb_csum = calc_sb_1_csum(sb);
2175}
2176
Xiao Nid9c0fa52020-06-30 15:55:36 +08002177static sector_t super_1_choose_bm_space(sector_t dev_size)
2178{
2179 sector_t bm_space;
2180
2181 /* if the device is bigger than 8Gig, save 64k for bitmap
2182 * usage, if bigger than 200Gig, save 128k
2183 */
2184 if (dev_size < 64*2)
2185 bm_space = 0;
2186 else if (dev_size - 64*2 >= 200*1024*1024*2)
2187 bm_space = 128*2;
2188 else if (dev_size - 4*2 > 8*1024*1024*2)
2189 bm_space = 64*2;
2190 else
2191 bm_space = 4*2;
2192 return bm_space;
2193}
2194
Chris Webb0cd17fe2008-06-28 08:31:46 +10002195static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11002196super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002197{
2198 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002199 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11002200 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002201 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10002202 if (rdev->data_offset != rdev->new_data_offset)
2203 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10002204 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10002205 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01002206 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002207 max_sectors -= rdev->data_offset;
2208 if (!num_sectors || num_sectors > max_sectors)
2209 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11002210 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10002211 /* minor version 0 with bitmap we can't move */
2212 return 0;
2213 } else {
2214 /* minor version 0; superblock after data */
Xiao Nid9c0fa52020-06-30 15:55:36 +08002215 sector_t sb_start, bm_space;
2216 sector_t dev_size = i_size_read(rdev->bdev->bd_inode) >> 9;
2217
2218 /* 8K is for superblock */
2219 sb_start = dev_size - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10002220 sb_start &= ~(sector_t)(4*2 - 1);
Xiao Nid9c0fa52020-06-30 15:55:36 +08002221
2222 bm_space = super_1_choose_bm_space(dev_size);
2223
2224 /* Space that can be used to store date needs to decrease
2225 * superblock bitmap space and bad block space(4K)
2226 */
2227 max_sectors = sb_start - bm_space - 4*2;
2228
Andre Noll15f4a5f2008-07-21 14:42:12 +10002229 if (!num_sectors || num_sectors > max_sectors)
2230 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002231 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10002232 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10002233 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08002234 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10002235 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11002236 do {
2237 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
2238 rdev->sb_page);
2239 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11002240 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10002241
2242}
2243
2244static int
2245super_1_allow_new_offset(struct md_rdev *rdev,
2246 unsigned long long new_offset)
2247{
2248 /* All necessary checks on new >= old have been done */
2249 struct bitmap *bitmap;
2250 if (new_offset >= rdev->data_offset)
2251 return 1;
2252
2253 /* with 1.0 metadata, there is no metadata to tread on
2254 * so we can always move back */
2255 if (rdev->mddev->minor_version == 0)
2256 return 1;
2257
2258 /* otherwise we must be sure not to step on
2259 * any metadata, so stay:
2260 * 36K beyond start of superblock
2261 * beyond end of badblocks
2262 * beyond write-intent bitmap
2263 */
2264 if (rdev->sb_start + (32+4)*2 > new_offset)
2265 return 0;
2266 bitmap = rdev->mddev->bitmap;
2267 if (bitmap && !rdev->mddev->bitmap_info.file &&
2268 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10002269 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10002270 return 0;
2271 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2272 return 0;
2273
2274 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002275}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Adrian Bunk75c96f82005-05-05 16:16:09 -07002277static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 [0] = {
2279 .name = "0.90.0",
2280 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002281 .load_super = super_90_load,
2282 .validate_super = super_90_validate,
2283 .sync_super = super_90_sync,
2284 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002285 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 },
2287 [1] = {
2288 .name = "md-1",
2289 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002290 .load_super = super_1_load,
2291 .validate_super = super_1_validate,
2292 .sync_super = super_1_sync,
2293 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002294 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 },
2296};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
NeilBrownfd01b882011-10-11 16:47:53 +11002298static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05002299{
2300 if (mddev->sync_super) {
2301 mddev->sync_super(mddev, rdev);
2302 return;
2303 }
2304
2305 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2306
2307 super_types[mddev->major_version].sync_super(mddev, rdev);
2308}
2309
NeilBrownfd01b882011-10-11 16:47:53 +11002310static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
NeilBrown3cb03002011-10-11 16:45:26 +11002312 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
NeilBrown4b809912008-07-21 17:05:25 +10002314 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07002315 rdev_for_each_rcu(rdev, mddev1) {
2316 if (test_bit(Faulty, &rdev->flags) ||
2317 test_bit(Journal, &rdev->flags) ||
2318 rdev->raid_disk == -1)
2319 continue;
2320 rdev_for_each_rcu(rdev2, mddev2) {
2321 if (test_bit(Faulty, &rdev2->flags) ||
2322 test_bit(Journal, &rdev2->flags) ||
2323 rdev2->raid_disk == -1)
2324 continue;
Christoph Hellwig61a27e1f2020-09-03 07:40:58 +02002325 if (rdev->bdev->bd_disk == rdev2->bdev->bd_disk) {
NeilBrown4b809912008-07-21 17:05:25 +10002326 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002327 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002328 }
Song Liu0b020e82015-09-03 23:00:35 -07002329 }
2330 }
NeilBrown4b809912008-07-21 17:05:25 +10002331 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return 0;
2333}
2334
2335static LIST_HEAD(pending_raid_disks);
2336
Andre Nollac5e7112009-08-03 10:59:47 +10002337/*
2338 * Try to register data integrity profile for an mddev
2339 *
2340 * This is called when an array is started and after a disk has been kicked
2341 * from the array. It only succeeds if all working and active component devices
2342 * are integrity capable with matching profiles.
2343 */
NeilBrownfd01b882011-10-11 16:47:53 +11002344int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002345{
NeilBrown3cb03002011-10-11 16:45:26 +11002346 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002347
Andre Nollac5e7112009-08-03 10:59:47 +10002348 if (list_empty(&mddev->disks))
2349 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002350 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2351 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002352 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002353 /* skip spares and non-functional disks */
2354 if (test_bit(Faulty, &rdev->flags))
2355 continue;
2356 if (rdev->raid_disk < 0)
2357 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002358 if (!reference) {
2359 /* Use the first rdev as the reference */
2360 reference = rdev;
2361 continue;
2362 }
2363 /* does this rdev's profile match the reference profile? */
2364 if (blk_integrity_compare(reference->bdev->bd_disk,
2365 rdev->bdev->bd_disk) < 0)
2366 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002367 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002368 if (!reference || !bdev_get_integrity(reference->bdev))
2369 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002370 /*
2371 * All component devices are integrity capable and have matching
2372 * profiles, register the common profile for the md device.
2373 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002374 blk_integrity_register(mddev->gendisk,
2375 bdev_get_integrity(reference->bdev));
2376
NeilBrown9d487392016-11-02 14:16:49 +11002377 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Kent Overstreetafeee512018-05-20 18:25:52 -04002378 if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002379 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002380 mdname(mddev));
2381 return -EINVAL;
2382 }
Andre Nollac5e7112009-08-03 10:59:47 +10002383 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002384}
Andre Nollac5e7112009-08-03 10:59:47 +10002385EXPORT_SYMBOL(md_integrity_register);
2386
Dan Williams1501efa2016-01-13 16:00:07 -08002387/*
2388 * Attempt to add an rdev, but only if it is consistent with the current
2389 * integrity profile
2390 */
2391int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002392{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002393 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002394 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002395
2396 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002397 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002398
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002399 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002400
2401 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002402 return 0;
2403
2404 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002405 pr_err("%s: incompatible integrity profile for %s\n",
2406 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002407 return -ENXIO;
2408 }
2409
2410 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002411}
2412EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002413
NeilBrownf72ffdd2014-09-30 14:23:59 +10002414static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002416 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002417 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002418 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Dan Williams11e2ede2008-04-30 00:52:32 -07002420 /* prevent duplicates */
2421 if (find_rdev(mddev, rdev->bdev->bd_dev))
2422 return -EEXIST;
2423
NeilBrown97b20ef2017-04-13 08:53:48 +10002424 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2425 mddev->pers)
2426 return -EROFS;
2427
Andre Nolldd8ac332009-03-31 14:33:13 +11002428 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002429 if (!test_bit(Journal, &rdev->flags) &&
2430 rdev->sectors &&
2431 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002432 if (mddev->pers) {
2433 /* Cannot change size, so fail
2434 * If mddev->level <= 0, then we don't care
2435 * about aligning sizes (e.g. linear)
2436 */
2437 if (mddev->level > 0)
2438 return -ENOSPC;
2439 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002440 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 /* Verify rdev->desc_nr is unique.
2444 * If it is -1, assign a free number, else
2445 * check number is not in use
2446 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002447 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (rdev->desc_nr < 0) {
2449 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002450 if (mddev->pers)
2451 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002452 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 choice++;
2454 rdev->desc_nr = choice;
2455 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002456 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002457 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002461 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002462 if (!test_bit(Journal, &rdev->flags) &&
2463 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002464 pr_warn("md: %s: array is limited to %d devices\n",
2465 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002466 return -EBUSY;
2467 }
NeilBrown19133a42005-11-08 21:39:35 -08002468 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002469 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002472 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002473
Guoqing Jiang963c5552019-06-14 17:10:36 +08002474 if (mddev->raid_disks)
Guoqing Jiang404659c2019-12-23 10:48:53 +01002475 mddev_create_serial_pool(mddev, rdev, false);
Guoqing Jiang963c5552019-06-14 17:10:36 +08002476
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002477 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002478 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002479
Tejun Heo0762b8b2008-08-25 19:56:12 +09002480 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
Damien Le Moal5e3b8a82020-07-16 13:54:40 +09002481 /* failure here is OK */
2482 err = sysfs_create_link(&rdev->kobj, ko, "block");
NeilBrown00bcb4a2010-06-01 19:37:23 +10002483 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
Junxiao Bie1a86db2020-07-14 16:10:26 -07002484 rdev->sysfs_unack_badblocks =
2485 sysfs_get_dirent_safe(rdev->kobj.sd, "unacknowledged_bad_blocks");
2486 rdev->sysfs_badblocks =
2487 sysfs_get_dirent_safe(rdev->kobj.sd, "bad_blocks");
NeilBrown3c0ee632008-10-21 13:25:28 +11002488
NeilBrown4b809912008-07-21 17:05:25 +10002489 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002490 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002491
2492 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002493 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002496
2497 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002498 pr_warn("md: failed to register dev-%s for %s\n",
2499 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002500 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002503static void rdev_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002504{
NeilBrown3cb03002011-10-11 16:45:26 +11002505 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002506 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002507 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002508}
2509
NeilBrownf72ffdd2014-09-30 14:23:59 +10002510static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
2512 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002513
Tejun Heo49731ba2011-01-14 18:43:57 +01002514 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002515 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002516 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +01002517 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002519 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002520 sysfs_put(rdev->sysfs_state);
Junxiao Bie1a86db2020-07-14 16:10:26 -07002521 sysfs_put(rdev->sysfs_unack_badblocks);
2522 sysfs_put(rdev->sysfs_badblocks);
NeilBrown3c0ee632008-10-21 13:25:28 +11002523 rdev->sysfs_state = NULL;
Junxiao Bie1a86db2020-07-14 16:10:26 -07002524 rdev->sysfs_unack_badblocks = NULL;
2525 rdev->sysfs_badblocks = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002526 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002527 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002528 * writing to 'remove' to "dev/state". We also need
2529 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002530 */
NeilBrown4b809912008-07-21 17:05:25 +10002531 synchronize_rcu();
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002532 INIT_WORK(&rdev->del_work, rdev_delayed_delete);
NeilBrown177a99b2008-02-06 01:39:56 -08002533 kobject_get(&rdev->kobj);
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002534 queue_work(md_rdev_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
2536
2537/*
2538 * prevent the device from being mounted, repartitioned or
2539 * otherwise reused by a RAID array (or any other kernel
2540 * subsystem), by bd_claiming the device.
2541 */
NeilBrown3cb03002011-10-11 16:45:26 +11002542static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
2544 int err = 0;
2545 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Tejun Heod4d77622010-11-13 11:55:18 +01002547 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002548 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 if (IS_ERR(bdev)) {
Christoph Hellwigea3edd42020-03-24 08:25:11 +01002550 pr_warn("md: could not open device unknown-block(%u,%u).\n",
2551 MAJOR(dev), MINOR(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 return PTR_ERR(bdev);
2553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 rdev->bdev = bdev;
2555 return err;
2556}
2557
NeilBrown3cb03002011-10-11 16:45:26 +11002558static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
2560 struct block_device *bdev = rdev->bdev;
2561 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002562 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563}
2564
2565void md_autodetect_dev(dev_t dev);
2566
NeilBrownf72ffdd2014-09-30 14:23:59 +10002567static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
2569 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002570
NeilBrown9d487392016-11-02 14:16:49 +11002571 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002572 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002574 if (test_bit(AutoDetected, &rdev->flags))
2575 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576#endif
2577 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002578 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579}
2580
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002581void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582{
2583 unbind_rdev_from_array(rdev);
2584 export_rdev(rdev);
2585}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002586EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
NeilBrownfd01b882011-10-11 16:47:53 +11002588static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
NeilBrown0638bb02014-09-25 17:43:47 +10002590 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
NeilBrown0638bb02014-09-25 17:43:47 +10002592 while (!list_empty(&mddev->disks)) {
2593 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2594 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002595 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 mddev->raid_disks = 0;
2598 mddev->major_version = 0;
2599}
2600
NeilBrown6497709b2017-03-15 14:05:14 +11002601static bool set_in_sync(struct mddev *mddev)
2602{
Shaohua Liefa4b772017-10-18 22:08:13 -07002603 lockdep_assert_held(&mddev->lock);
NeilBrown4ad23a972017-03-15 14:05:14 +11002604 if (!mddev->in_sync) {
2605 mddev->sync_checkers++;
2606 spin_unlock(&mddev->lock);
2607 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2608 spin_lock(&mddev->lock);
2609 if (!mddev->in_sync &&
2610 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002611 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002612 /*
2613 * Ensure ->in_sync is visible before we clear
2614 * ->sync_checkers.
2615 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002616 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002617 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2618 sysfs_notify_dirent_safe(mddev->sysfs_state);
2619 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002620 if (--mddev->sync_checkers == 0)
2621 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002622 }
2623 if (mddev->safemode == 1)
2624 mddev->safemode = 0;
2625 return mddev->in_sync;
2626}
2627
NeilBrownf72ffdd2014-09-30 14:23:59 +10002628static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
NeilBrown42543762006-06-26 00:27:57 -07002630 /* Update each superblock (in-memory image), but
2631 * if we are allowed to, skip spares which already
2632 * have the right event counter, or have one earlier
2633 * (which would mean they aren't being marked as dirty
2634 * with the rest of the array)
2635 */
NeilBrown3cb03002011-10-11 16:45:26 +11002636 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002637 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002638 if (rdev->sb_events == mddev->events ||
2639 (nospares &&
2640 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002641 rdev->sb_events+1 == mddev->events)) {
2642 /* Don't update this superblock */
2643 rdev->sb_loaded = 2;
2644 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002645 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002646 rdev->sb_loaded = 1;
2647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
2649}
2650
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002651static bool does_sb_need_changing(struct mddev *mddev)
2652{
2653 struct md_rdev *rdev;
2654 struct mdp_superblock_1 *sb;
2655 int role;
2656
2657 /* Find a good rdev */
2658 rdev_for_each(rdev, mddev)
2659 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2660 break;
2661
2662 /* No good device found. */
2663 if (!rdev)
2664 return false;
2665
2666 sb = page_address(rdev->sb_page);
2667 /* Check if a device has become faulty or a spare become active */
2668 rdev_for_each(rdev, mddev) {
2669 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2670 /* Device activated? */
2671 if (role == 0xffff && rdev->raid_disk >=0 &&
2672 !test_bit(Faulty, &rdev->flags))
2673 return true;
2674 /* Device turned faulty? */
2675 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2676 return true;
2677 }
2678
2679 /* Check if any mddev parameters have changed */
2680 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2681 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002682 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002683 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2684 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2685 return true;
2686
2687 return false;
2688}
2689
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002690void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
NeilBrown3cb03002011-10-11 16:45:26 +11002692 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002693 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002694 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002695 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002696 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
NeilBrownd87f0642013-04-24 11:42:40 +10002698 if (mddev->ro) {
2699 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002700 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002701 return;
2702 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002703
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002704repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002705 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002706 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002707 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002708 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002709 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002710 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002711 /* Has someone else has updated the sb */
2712 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002713 if (ret == 0)
2714 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002715 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2716 BIT(MD_SB_CHANGE_DEVS) |
2717 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002718 return;
2719 }
2720 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002721
NeilBrowndb0505d2017-10-17 16:18:36 +11002722 /*
2723 * First make sure individual recovery_offsets are correct
2724 * curr_resync_completed can only be used during recovery.
2725 * During reshape/resync it might use array-addresses rather
2726 * that device addresses.
2727 */
NeilBrowndafb20f2012-03-19 12:46:39 +11002728 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002729 if (rdev->raid_disk >= 0 &&
2730 mddev->delta_disks >= 0 &&
NeilBrowndb0505d2017-10-17 16:18:36 +11002731 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2732 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2733 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002734 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002735 !test_bit(In_sync, &rdev->flags) &&
2736 mddev->curr_resync_completed > rdev->recovery_offset)
2737 rdev->recovery_offset = mddev->curr_resync_completed;
2738
NeilBrownf72ffdd2014-09-30 14:23:59 +10002739 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002740 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002741 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2742 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002743 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002744 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002745 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002746 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002747 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002748 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002749 md_error(mddev, rdev);
2750 }
2751 clear_bit(Blocked, &rdev->flags);
2752 clear_bit(BlockedBadBlocks, &rdev->flags);
2753 wake_up(&rdev->blocked_wait);
2754 }
2755 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002756 wake_up(&mddev->sb_wait);
2757 return;
2758 }
2759
NeilBrown85572d72014-12-15 12:56:56 +11002760 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002761
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002762 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002763
Shaohua Li29530792016-12-08 15:48:19 -08002764 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002765 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002766 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002767 /* just a clean<-> dirty transition, possibly leave spares alone,
2768 * though if events isn't the right even/odd, we will have to do
2769 * spares after all
2770 */
2771 nospares = 1;
2772 if (force_change)
2773 nospares = 0;
2774 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002775 /* If the array is degraded, then skipping spares is both
2776 * dangerous and fairly pointless.
2777 * Dangerous because a device that was removed from the array
2778 * might have a event_count that still looks up-to-date,
2779 * so it can be re-added without a resync.
2780 * Pointless because if there are any spares to skip,
2781 * then a recovery will happen and soon that array won't
2782 * be degraded any more and the spare can go back to sleep then.
2783 */
NeilBrown850b2b422006-10-03 01:15:46 -07002784 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002785
NeilBrown06d91a52005-06-21 17:17:12 -07002786 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002787
2788 /* If this is just a dirty<->clean transition, and the array is clean
2789 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002790 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002791 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002792 && mddev->can_decrease_events
2793 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002794 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002795 mddev->can_decrease_events = 0;
2796 } else {
NeilBrown42543762006-06-26 00:27:57 -07002797 /* otherwise we have to go forward and ... */
2798 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002799 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
NeilBrown403df472014-09-30 15:52:29 +10002802 /*
2803 * This 64-bit counter should never wrap.
2804 * Either we are in around ~1 trillion A.C., assuming
2805 * 1 reboot per second, or we have a bug...
2806 */
2807 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002808
NeilBrowndafb20f2012-03-19 12:46:39 +11002809 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002810 if (rdev->badblocks.changed)
2811 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002812 if (test_bit(Faulty, &rdev->flags))
2813 set_bit(FaultRecorded, &rdev->flags);
2814 }
NeilBrown2699b672011-07-28 11:31:47 +10002815
NeilBrowne6910632008-02-06 01:39:51 -08002816 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002817 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
NeilBrown36a4e1f2011-10-07 14:23:17 +11002819 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2820 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Shaohua Li504634f2016-11-18 09:44:08 -08002822 if (mddev->queue)
2823 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002824rewrite:
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002825 md_bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002826 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002828
NeilBrown42543762006-06-26 00:27:57 -07002829 if (rdev->sb_loaded != 1)
2830 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
NeilBrownf4667222013-12-09 12:04:56 +11002832 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002833 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002834 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002835 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002836 pr_debug("md: (write) %s's sb offset: %llu\n",
2837 bdevname(rdev->bdev, b),
2838 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002839 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002840 if (rdev->badblocks.size) {
2841 md_super_write(mddev, rdev,
2842 rdev->badblocks.sector,
2843 rdev->badblocks.size << 9,
2844 rdev->bb_page);
2845 rdev->badblocks.size = 0;
2846 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002847
NeilBrownf4667222013-12-09 12:04:56 +11002848 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002849 pr_debug("md: %s (skipping faulty)\n",
2850 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002851
NeilBrown7bfa19f2005-06-21 17:17:28 -07002852 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 /* only need to write one superblock... */
2854 break;
2855 }
NeilBrown46533ff2016-11-18 16:16:11 +11002856 if (md_super_wait(mddev) < 0)
2857 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002858 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002859
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002860 if (mddev_is_clustered(mddev) && ret == 0)
2861 md_cluster_ops->metadata_update_finish(mddev);
2862
NeilBrown850b2b422006-10-03 01:15:46 -07002863 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002864 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2865 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002866 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002867 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002868 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002869 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
Junxiao Bie1a86db2020-07-14 16:10:26 -07002870 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown06d91a52005-06-21 17:17:12 -07002871
NeilBrowndafb20f2012-03-19 12:46:39 +11002872 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002873 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2874 clear_bit(Blocked, &rdev->flags);
2875
2876 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002877 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002878 clear_bit(BlockedBadBlocks, &rdev->flags);
2879 wake_up(&rdev->blocked_wait);
2880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002882EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002884static int add_bound_rdev(struct md_rdev *rdev)
2885{
2886 struct mddev *mddev = rdev->mddev;
2887 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002888 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002889
Shaohua Li87d4d912016-01-06 14:37:14 -08002890 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002891 /* If there is hot_add_disk but no hot_remove_disk
2892 * then added disks for geometry changes,
2893 * and should be added immediately.
2894 */
2895 super_types[mddev->major_version].
2896 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002897 if (add_journal)
2898 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002899 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002900 if (add_journal)
2901 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002902 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002903 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002904 return err;
2905 }
2906 }
2907 sysfs_notify_dirent_safe(rdev->sysfs_state);
2908
Shaohua Li29530792016-12-08 15:48:19 -08002909 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002910 if (mddev->degraded)
2911 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2912 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2913 md_new_event(mddev);
2914 md_wakeup_thread(mddev->thread);
2915 return 0;
2916}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Andre Noll7f6ce762008-03-23 18:34:54 +01002918/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002919 * We want to accept with case. For this we use cmd_match.
2920 */
2921static int cmd_match(const char *cmd, const char *str)
2922{
2923 /* See if cmd, written into a sysfs file, matches
2924 * str. They must either be the same, or cmd can
2925 * have a trailing newline
2926 */
2927 while (*cmd && *str && *cmd == *str) {
2928 cmd++;
2929 str++;
2930 }
2931 if (*cmd == '\n')
2932 cmd++;
2933 if (*str || *cmd)
2934 return 0;
2935 return 1;
2936}
2937
NeilBrown86e6ffd2005-11-08 21:39:24 -08002938struct rdev_sysfs_entry {
2939 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002940 ssize_t (*show)(struct md_rdev *, char *);
2941 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002942};
2943
2944static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002945state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002946{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002947 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002948 size_t len = 0;
Mark Rutland6aa7de02017-10-23 14:07:29 -07002949 unsigned long flags = READ_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002950
NeilBrown758bfc82014-12-15 12:56:59 +11002951 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002952 (!test_bit(ExternalBbl, &flags) &&
2953 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002954 len += sprintf(page+len, "faulty%s", sep);
2955 if (test_bit(In_sync, &flags))
2956 len += sprintf(page+len, "in_sync%s", sep);
2957 if (test_bit(Journal, &flags))
2958 len += sprintf(page+len, "journal%s", sep);
2959 if (test_bit(WriteMostly, &flags))
2960 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002961 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002962 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002963 && !test_bit(Faulty, &flags)))
2964 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002965 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002966 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002967 !test_bit(In_sync, &flags))
2968 len += sprintf(page+len, "spare%s", sep);
2969 if (test_bit(WriteErrorSeen, &flags))
2970 len += sprintf(page+len, "write_error%s", sep);
2971 if (test_bit(WantReplacement, &flags))
2972 len += sprintf(page+len, "want_replacement%s", sep);
2973 if (test_bit(Replacement, &flags))
2974 len += sprintf(page+len, "replacement%s", sep);
2975 if (test_bit(ExternalBbl, &flags))
2976 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002977 if (test_bit(FailFast, &flags))
2978 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002979
2980 if (len)
2981 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002982
NeilBrown86e6ffd2005-11-08 21:39:24 -08002983 return len+sprintf(page+len, "\n");
2984}
2985
NeilBrown45dc2de2006-06-26 00:27:58 -07002986static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002987state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002988{
2989 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002990 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002991 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002992 * writemostly - sets write_mostly
2993 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002994 * blocked - sets the Blocked flags
2995 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002996 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002997 * -insync - clear Insync for a device with a slot assigned,
2998 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002999 * write_error - sets WriteErrorSeen
3000 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11003001 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07003002 */
3003 int err = -EINVAL;
3004 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
3005 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10003006 if (test_bit(Faulty, &rdev->flags))
3007 err = 0;
3008 else
3009 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07003010 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07003011 if (rdev->mddev->pers) {
3012 clear_bit(Blocked, &rdev->flags);
3013 remove_and_add_spares(rdev->mddev, rdev);
3014 }
NeilBrown45dc2de2006-06-26 00:27:58 -07003015 if (rdev->raid_disk >= 0)
3016 err = -EBUSY;
3017 else {
NeilBrownfd01b882011-10-11 16:47:53 +11003018 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07003019 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08003020 if (mddev_is_clustered(mddev))
3021 err = md_cluster_ops->remove_disk(mddev, rdev);
3022
3023 if (err == 0) {
3024 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11003025 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08003026 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11003027 md_wakeup_thread(mddev->thread);
3028 }
Guoqing Jianga9720902015-10-12 17:21:27 +08003029 md_new_event(mddev);
3030 }
NeilBrown45dc2de2006-06-26 00:27:58 -07003031 }
NeilBrownf6556752006-06-26 00:28:01 -07003032 } else if (cmd_match(buf, "writemostly")) {
3033 set_bit(WriteMostly, &rdev->flags);
Guoqing Jiang404659c2019-12-23 10:48:53 +01003034 mddev_create_serial_pool(rdev->mddev, rdev, false);
NeilBrownf6556752006-06-26 00:28:01 -07003035 err = 0;
3036 } else if (cmd_match(buf, "-writemostly")) {
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +01003037 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
NeilBrownf6556752006-06-26 00:28:01 -07003038 clear_bit(WriteMostly, &rdev->flags);
3039 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003040 } else if (cmd_match(buf, "blocked")) {
3041 set_bit(Blocked, &rdev->flags);
3042 err = 0;
3043 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10003044 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02003045 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10003046 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10003047 /* metadata handler doesn't understand badblocks,
3048 * so we need to fail the device
3049 */
3050 md_error(rdev->mddev, rdev);
3051 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003052 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10003053 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003054 wake_up(&rdev->blocked_wait);
3055 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3056 md_wakeup_thread(rdev->mddev->thread);
3057
3058 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10003059 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
3060 set_bit(In_sync, &rdev->flags);
3061 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11003062 } else if (cmd_match(buf, "failfast")) {
3063 set_bit(FailFast, &rdev->flags);
3064 err = 0;
3065 } else if (cmd_match(buf, "-failfast")) {
3066 clear_bit(FailFast, &rdev->flags);
3067 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07003068 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
3069 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10003070 if (rdev->mddev->pers == NULL) {
3071 clear_bit(In_sync, &rdev->flags);
3072 rdev->saved_raid_disk = rdev->raid_disk;
3073 rdev->raid_disk = -1;
3074 err = 0;
3075 }
NeilBrownd7a9d442011-07-28 11:31:48 +10003076 } else if (cmd_match(buf, "write_error")) {
3077 set_bit(WriteErrorSeen, &rdev->flags);
3078 err = 0;
3079 } else if (cmd_match(buf, "-write_error")) {
3080 clear_bit(WriteErrorSeen, &rdev->flags);
3081 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11003082 } else if (cmd_match(buf, "want_replacement")) {
3083 /* Any non-spare device that is not a replacement can
3084 * become want_replacement at any time, but we then need to
3085 * check if recovery is needed.
3086 */
3087 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07003088 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11003089 !test_bit(Replacement, &rdev->flags))
3090 set_bit(WantReplacement, &rdev->flags);
3091 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3092 md_wakeup_thread(rdev->mddev->thread);
3093 err = 0;
3094 } else if (cmd_match(buf, "-want_replacement")) {
3095 /* Clearing 'want_replacement' is always allowed.
3096 * Once replacements starts it is too late though.
3097 */
3098 err = 0;
3099 clear_bit(WantReplacement, &rdev->flags);
3100 } else if (cmd_match(buf, "replacement")) {
3101 /* Can only set a device as a replacement when array has not
3102 * yet been started. Once running, replacement is automatic
3103 * from spares, or by assigning 'slot'.
3104 */
3105 if (rdev->mddev->pers)
3106 err = -EBUSY;
3107 else {
3108 set_bit(Replacement, &rdev->flags);
3109 err = 0;
3110 }
3111 } else if (cmd_match(buf, "-replacement")) {
3112 /* Similarly, can only clear Replacement before start */
3113 if (rdev->mddev->pers)
3114 err = -EBUSY;
3115 else {
3116 clear_bit(Replacement, &rdev->flags);
3117 err = 0;
3118 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05003119 } else if (cmd_match(buf, "re-add")) {
Yufen Yuee37e622019-04-02 14:22:14 +08003120 if (!rdev->mddev->pers)
3121 err = -EINVAL;
3122 else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) &&
3123 rdev->saved_raid_disk >= 0) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05003124 /* clear_bit is performed _after_ all the devices
3125 * have their local Faulty bit cleared. If any writes
3126 * happen in the meantime in the local node, they
3127 * will land in the local bitmap, which will be synced
3128 * by this node eventually
3129 */
3130 if (!mddev_is_clustered(rdev->mddev) ||
3131 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
3132 clear_bit(Faulty, &rdev->flags);
3133 err = add_bound_rdev(rdev);
3134 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05003135 } else
3136 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02003137 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
3138 set_bit(ExternalBbl, &rdev->flags);
3139 rdev->badblocks.shift = 0;
3140 err = 0;
3141 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
3142 clear_bit(ExternalBbl, &rdev->flags);
3143 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07003144 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10003145 if (!err)
3146 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07003147 return err ? err : len;
3148}
NeilBrown80ca3a42006-07-10 04:44:18 -07003149static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10003150__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003151
3152static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003153errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08003154{
3155 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
3156}
3157
3158static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003159errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08003160{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003161 unsigned int n;
3162 int rv;
3163
3164 rv = kstrtouint(buf, 10, &n);
3165 if (rv < 0)
3166 return rv;
3167 atomic_set(&rdev->corrected_errors, n);
3168 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08003169}
3170static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07003171__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08003172
NeilBrown014236d2006-01-06 00:20:55 -08003173static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003174slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08003175{
Shaohua Lif2076e72015-10-08 21:54:12 -07003176 if (test_bit(Journal, &rdev->flags))
3177 return sprintf(page, "journal\n");
3178 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08003179 return sprintf(page, "none\n");
3180 else
3181 return sprintf(page, "%d\n", rdev->raid_disk);
3182}
3183
3184static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003185slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08003186{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003187 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08003188 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003189
Shaohua Lif2076e72015-10-08 21:54:12 -07003190 if (test_bit(Journal, &rdev->flags))
3191 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08003192 if (strncmp(buf, "none", 4)==0)
3193 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003194 else {
3195 err = kstrtouint(buf, 10, (unsigned int *)&slot);
3196 if (err < 0)
3197 return err;
3198 }
Neil Brown6c2fce22008-06-28 08:31:31 +10003199 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08003200 /* Setting 'slot' on an active array requires also
3201 * updating the 'rd%d' link, and communicating
3202 * with the personality with ->hot_*_disk.
3203 * For now we only support removing
3204 * failed/spare devices. This normally happens automatically,
3205 * but not when the metadata is externally managed.
3206 */
NeilBrownc303da62008-02-06 01:39:51 -08003207 if (rdev->raid_disk == -1)
3208 return -EEXIST;
3209 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10003210 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08003211 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10003212 clear_bit(Blocked, &rdev->flags);
3213 remove_and_add_spares(rdev->mddev, rdev);
3214 if (rdev->raid_disk >= 0)
3215 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08003216 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3217 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10003218 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10003219 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10003220 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10003221 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11003222 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10003223
3224 if (rdev->raid_disk != -1)
3225 return -EBUSY;
3226
NeilBrownc6751b22011-02-02 11:57:13 +11003227 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
3228 return -EBUSY;
3229
Neil Brown6c2fce22008-06-28 08:31:31 +10003230 if (rdev->mddev->pers->hot_add_disk == NULL)
3231 return -EINVAL;
3232
NeilBrownba1b41b2011-01-14 09:14:34 +11003233 if (slot >= rdev->mddev->raid_disks &&
3234 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
3235 return -ENOSPC;
3236
Neil Brown6c2fce22008-06-28 08:31:31 +10003237 rdev->raid_disk = slot;
3238 if (test_bit(In_sync, &rdev->flags))
3239 rdev->saved_raid_disk = slot;
3240 else
3241 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11003242 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11003243 clear_bit(Bitmap_sync, &rdev->flags);
Guoqing Jiang3f79cc22020-04-04 23:57:11 +02003244 err = rdev->mddev->pers->hot_add_disk(rdev->mddev, rdev);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11003245 if (err) {
3246 rdev->raid_disk = -1;
3247 return err;
3248 } else
3249 sysfs_notify_dirent_safe(rdev->sysfs_state);
Damien Le Moal5e3b8a82020-07-16 13:54:40 +09003250 /* failure here is OK */;
3251 sysfs_link_rdev(rdev->mddev, rdev);
Neil Brown6c2fce22008-06-28 08:31:31 +10003252 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08003253 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11003254 if (slot >= rdev->mddev->raid_disks &&
3255 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08003256 return -ENOSPC;
3257 rdev->raid_disk = slot;
3258 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08003259 clear_bit(Faulty, &rdev->flags);
3260 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08003261 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003262 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08003263 }
NeilBrown014236d2006-01-06 00:20:55 -08003264 return len;
3265}
3266
NeilBrown014236d2006-01-06 00:20:55 -08003267static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07003268__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08003269
NeilBrown93c8cad2006-01-06 00:20:56 -08003270static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003271offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08003272{
Andrew Morton6961ece2006-01-06 00:20:59 -08003273 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08003274}
3275
3276static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003277offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08003278{
NeilBrownc6563a82012-05-21 09:27:00 +10003279 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003280 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003281 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10003282 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003283 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003284 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08003285 /* Must set offset before size, so overlap checks
3286 * can be sane */
3287 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08003288 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10003289 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08003290 return len;
3291}
3292
3293static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07003294__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08003295
NeilBrownc6563a82012-05-21 09:27:00 +10003296static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3297{
3298 return sprintf(page, "%llu\n",
3299 (unsigned long long)rdev->new_data_offset);
3300}
3301
3302static ssize_t new_offset_store(struct md_rdev *rdev,
3303 const char *buf, size_t len)
3304{
3305 unsigned long long new_offset;
3306 struct mddev *mddev = rdev->mddev;
3307
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003308 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10003309 return -EINVAL;
3310
NeilBrownf851b602014-12-11 10:02:10 +11003311 if (mddev->sync_thread ||
3312 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10003313 return -EBUSY;
3314 if (new_offset == rdev->data_offset)
3315 /* reset is always permitted */
3316 ;
3317 else if (new_offset > rdev->data_offset) {
3318 /* must not push array size beyond rdev_sectors */
3319 if (new_offset - rdev->data_offset
3320 + mddev->dev_sectors > rdev->sectors)
3321 return -E2BIG;
3322 }
3323 /* Metadata worries about other space details. */
3324
3325 /* decreasing the offset is inconsistent with a backwards
3326 * reshape.
3327 */
3328 if (new_offset < rdev->data_offset &&
3329 mddev->reshape_backwards)
3330 return -EINVAL;
3331 /* Increasing offset is inconsistent with forwards
3332 * reshape. reshape_direction should be set to
3333 * 'backwards' first.
3334 */
3335 if (new_offset > rdev->data_offset &&
3336 !mddev->reshape_backwards)
3337 return -EINVAL;
3338
3339 if (mddev->pers && mddev->persistent &&
3340 !super_types[mddev->major_version]
3341 .allow_new_offset(rdev, new_offset))
3342 return -E2BIG;
3343 rdev->new_data_offset = new_offset;
3344 if (new_offset > rdev->data_offset)
3345 mddev->reshape_backwards = 1;
3346 else if (new_offset < rdev->data_offset)
3347 mddev->reshape_backwards = 0;
3348
3349 return len;
3350}
3351static struct rdev_sysfs_entry rdev_new_offset =
3352__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3353
NeilBrown83303b62006-01-06 00:21:06 -08003354static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003355rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003356{
Andre Nolldd8ac332009-03-31 14:33:13 +11003357 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003358}
3359
NeilBrownc5d79ad2008-02-06 01:39:54 -08003360static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3361{
3362 /* check if two start/length pairs overlap */
3363 if (s1+l1 <= s2)
3364 return 0;
3365 if (s2+l2 <= s1)
3366 return 0;
3367 return 1;
3368}
3369
Dan Williamsb522adc2009-03-31 15:00:31 +11003370static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3371{
3372 unsigned long long blocks;
3373 sector_t new;
3374
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003375 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003376 return -EINVAL;
3377
3378 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3379 return -EINVAL; /* sector conversion overflow */
3380
3381 new = blocks * 2;
3382 if (new != blocks * 2)
3383 return -EINVAL; /* unsigned long long to sector_t overflow */
3384
3385 *sectors = new;
3386 return 0;
3387}
3388
NeilBrown83303b62006-01-06 00:21:06 -08003389static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003390rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003391{
NeilBrownfd01b882011-10-11 16:47:53 +11003392 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003393 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003394 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003395
Shaohua Lif2076e72015-10-08 21:54:12 -07003396 if (test_bit(Journal, &rdev->flags))
3397 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003398 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003399 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003400 if (rdev->data_offset != rdev->new_data_offset)
3401 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003402 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003403 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003404 sectors = super_types[my_mddev->major_version].
3405 rdev_size_change(rdev, sectors);
3406 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003407 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003408 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003409 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003410 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003411 if (!my_mddev->pers->resize)
3412 /* Cannot change size for RAID0 or Linear etc */
3413 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003414 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003415 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003416 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003417
Andre Nolldd8ac332009-03-31 14:33:13 +11003418 rdev->sectors = sectors;
3419 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003420 /* Need to check that all other rdevs with the same
3421 * ->bdev do not overlap. 'rcu' is sufficient to walk
3422 * the rdev lists safely.
3423 * This check does not provide a hard guarantee, it
3424 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003425 */
NeilBrownfd01b882011-10-11 16:47:53 +11003426 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003427 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003428 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003429
NeilBrown8b1afc32014-09-29 15:33:20 +10003430 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003431 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003432 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003433
NeilBrowndafb20f2012-03-19 12:46:39 +11003434 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003435 if (rdev->bdev == rdev2->bdev &&
3436 rdev != rdev2 &&
3437 overlaps(rdev->data_offset, rdev->sectors,
3438 rdev2->data_offset,
3439 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003440 overlap = 1;
3441 break;
3442 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003443 if (overlap) {
3444 mddev_put(mddev);
3445 break;
3446 }
3447 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003448 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003449 if (overlap) {
3450 /* Someone else could have slipped in a size
3451 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003452 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003453 * safe, and trust userspace not to race with
3454 * itself
3455 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003456 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003457 return -EBUSY;
3458 }
3459 }
NeilBrown83303b62006-01-06 00:21:06 -08003460 return len;
3461}
3462
3463static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003464__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003465
NeilBrown3cb03002011-10-11 16:45:26 +11003466static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003467{
3468 unsigned long long recovery_start = rdev->recovery_offset;
3469
3470 if (test_bit(In_sync, &rdev->flags) ||
3471 recovery_start == MaxSector)
3472 return sprintf(page, "none\n");
3473
3474 return sprintf(page, "%llu\n", recovery_start);
3475}
3476
NeilBrown3cb03002011-10-11 16:45:26 +11003477static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003478{
3479 unsigned long long recovery_start;
3480
3481 if (cmd_match(buf, "none"))
3482 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003483 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003484 return -EINVAL;
3485
3486 if (rdev->mddev->pers &&
3487 rdev->raid_disk >= 0)
3488 return -EBUSY;
3489
3490 rdev->recovery_offset = recovery_start;
3491 if (recovery_start == MaxSector)
3492 set_bit(In_sync, &rdev->flags);
3493 else
3494 clear_bit(In_sync, &rdev->flags);
3495 return len;
3496}
3497
3498static struct rdev_sysfs_entry rdev_recovery_start =
3499__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3500
Vishal Vermafc974ee2015-12-24 19:20:34 -07003501/* sysfs access to bad-blocks list.
3502 * We present two files.
3503 * 'bad-blocks' lists sector numbers and lengths of ranges that
3504 * are recorded as bad. The list is truncated to fit within
3505 * the one-page limit of sysfs.
3506 * Writing "sector length" to this file adds an acknowledged
3507 * bad block list.
3508 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3509 * been acknowledged. Writing to this file adds bad blocks
3510 * without acknowledging them. This is largely for testing.
3511 */
NeilBrown3cb03002011-10-11 16:45:26 +11003512static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003513{
3514 return badblocks_show(&rdev->badblocks, page, 0);
3515}
NeilBrown3cb03002011-10-11 16:45:26 +11003516static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003517{
NeilBrownde393cd2011-07-28 11:31:48 +10003518 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3519 /* Maybe that ack was all we needed */
3520 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3521 wake_up(&rdev->blocked_wait);
3522 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003523}
3524static struct rdev_sysfs_entry rdev_bad_blocks =
3525__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3526
NeilBrown3cb03002011-10-11 16:45:26 +11003527static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003528{
3529 return badblocks_show(&rdev->badblocks, page, 1);
3530}
NeilBrown3cb03002011-10-11 16:45:26 +11003531static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003532{
3533 return badblocks_store(&rdev->badblocks, page, len, 1);
3534}
3535static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3536__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3537
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003538static ssize_t
3539ppl_sector_show(struct md_rdev *rdev, char *page)
3540{
3541 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3542}
3543
3544static ssize_t
3545ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3546{
3547 unsigned long long sector;
3548
3549 if (kstrtoull(buf, 10, &sector) < 0)
3550 return -EINVAL;
3551 if (sector != (sector_t)sector)
3552 return -EINVAL;
3553
3554 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3555 rdev->raid_disk >= 0)
3556 return -EBUSY;
3557
3558 if (rdev->mddev->persistent) {
3559 if (rdev->mddev->major_version == 0)
3560 return -EINVAL;
3561 if ((sector > rdev->sb_start &&
3562 sector - rdev->sb_start > S16_MAX) ||
3563 (sector < rdev->sb_start &&
3564 rdev->sb_start - sector > -S16_MIN))
3565 return -EINVAL;
3566 rdev->ppl.offset = sector - rdev->sb_start;
3567 } else if (!rdev->mddev->external) {
3568 return -EBUSY;
3569 }
3570 rdev->ppl.sector = sector;
3571 return len;
3572}
3573
3574static struct rdev_sysfs_entry rdev_ppl_sector =
3575__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3576
3577static ssize_t
3578ppl_size_show(struct md_rdev *rdev, char *page)
3579{
3580 return sprintf(page, "%u\n", rdev->ppl.size);
3581}
3582
3583static ssize_t
3584ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3585{
3586 unsigned int size;
3587
3588 if (kstrtouint(buf, 10, &size) < 0)
3589 return -EINVAL;
3590
3591 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3592 rdev->raid_disk >= 0)
3593 return -EBUSY;
3594
3595 if (rdev->mddev->persistent) {
3596 if (rdev->mddev->major_version == 0)
3597 return -EINVAL;
3598 if (size > U16_MAX)
3599 return -EINVAL;
3600 } else if (!rdev->mddev->external) {
3601 return -EBUSY;
3602 }
3603 rdev->ppl.size = size;
3604 return len;
3605}
3606
3607static struct rdev_sysfs_entry rdev_ppl_size =
3608__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3609
NeilBrown86e6ffd2005-11-08 21:39:24 -08003610static struct attribute *rdev_default_attrs[] = {
3611 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003612 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003613 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003614 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003615 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003616 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003617 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003618 &rdev_bad_blocks.attr,
3619 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003620 &rdev_ppl_sector.attr,
3621 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003622 NULL,
3623};
3624static ssize_t
3625rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3626{
3627 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003628 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003629
3630 if (!entry->show)
3631 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003632 if (!rdev->mddev)
Marcos Paulo de Souza168b3052019-06-14 15:41:06 -07003633 return -ENODEV;
NeilBrown758bfc82014-12-15 12:56:59 +11003634 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003635}
3636
3637static ssize_t
3638rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3639 const char *page, size_t length)
3640{
3641 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003642 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003643 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003644 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003645
3646 if (!entry->store)
3647 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003648 if (!capable(CAP_SYS_ADMIN))
3649 return -EACCES;
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003650 rv = mddev ? mddev_lock(mddev) : -ENODEV;
NeilBrownca388052008-02-06 01:39:55 -08003651 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003652 if (rdev->mddev == NULL)
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003653 rv = -ENODEV;
NeilBrown27c529b2008-03-04 14:29:33 -08003654 else
3655 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003656 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003657 }
3658 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003659}
3660
3661static void rdev_free(struct kobject *ko)
3662{
NeilBrown3cb03002011-10-11 16:45:26 +11003663 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003664 kfree(rdev);
3665}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003666static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003667 .show = rdev_attr_show,
3668 .store = rdev_attr_store,
3669};
3670static struct kobj_type rdev_ktype = {
3671 .release = rdev_free,
3672 .sysfs_ops = &rdev_sysfs_ops,
3673 .default_attrs = rdev_default_attrs,
3674};
3675
NeilBrown3cb03002011-10-11 16:45:26 +11003676int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003677{
3678 rdev->desc_nr = -1;
3679 rdev->saved_raid_disk = -1;
3680 rdev->raid_disk = -1;
3681 rdev->flags = 0;
3682 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003683 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003684 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003685 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003686 rdev->sb_loaded = 0;
3687 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003688 atomic_set(&rdev->nr_pending, 0);
3689 atomic_set(&rdev->read_errors, 0);
3690 atomic_set(&rdev->corrected_errors, 0);
3691
3692 INIT_LIST_HEAD(&rdev->same_set);
3693 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003694
3695 /* Add space to store bad block list.
3696 * This reserves the space even on arrays where it cannot
3697 * be used - I wonder if that matters
3698 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003699 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003700}
3701EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702/*
3703 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3704 *
3705 * mark the device faulty if:
3706 *
3707 * - the device is nonexistent (zero size)
3708 * - the device has no valid superblock
3709 *
3710 * a faulty rdev _never_ has rdev->sb set.
3711 */
NeilBrown3cb03002011-10-11 16:45:26 +11003712static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713{
3714 char b[BDEVNAME_SIZE];
3715 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003716 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 sector_t size;
3718
NeilBrown9ffae0c2006-01-06 00:20:32 -08003719 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003720 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
NeilBrown2230dfe2011-07-28 11:31:46 +10003723 err = md_rdev_init(rdev);
3724 if (err)
3725 goto abort_free;
3726 err = alloc_disk_sb(rdev);
3727 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 goto abort_free;
3729
NeilBrownc5d79ad2008-02-06 01:39:54 -08003730 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 if (err)
3732 goto abort_free;
3733
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003734 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003735
Mike Snitzer77304d22010-11-08 14:39:12 +01003736 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003738 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 bdevname(rdev->bdev,b));
3740 err = -EINVAL;
3741 goto abort_free;
3742 }
3743
3744 if (super_format >= 0) {
3745 err = super_types[super_format].
3746 load_super(rdev, NULL, super_minor);
3747 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003748 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003749 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003750 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 goto abort_free;
3752 }
3753 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003754 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 bdevname(rdev->bdev,b));
3756 goto abort_free;
3757 }
3758 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003759
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 return rdev;
3761
3762abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003763 if (rdev->bdev)
3764 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003765 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 kfree(rdev);
3767 return ERR_PTR(err);
3768}
3769
3770/*
3771 * Check a full RAID array for plausibility
3772 */
3773
Yufen Yu6a5cb532019-10-16 16:00:03 +08003774static int analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775{
3776 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003777 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 char b[BDEVNAME_SIZE];
3779
3780 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003781 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 switch (super_types[mddev->major_version].
3783 load_super(rdev, freshest, mddev->minor_version)) {
3784 case 1:
3785 freshest = rdev;
3786 break;
3787 case 0:
3788 break;
3789 default:
NeilBrown9d487392016-11-02 14:16:49 +11003790 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003792 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 }
3794
Yufen Yu6a5cb532019-10-16 16:00:03 +08003795 /* Cannot find a valid fresh disk */
3796 if (!freshest) {
3797 pr_warn("md: cannot find a valid disk\n");
3798 return -EINVAL;
3799 }
3800
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 super_types[mddev->major_version].
3802 validate_super(mddev, freshest);
3803
3804 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003805 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003806 if (mddev->max_disks &&
3807 (rdev->desc_nr >= mddev->max_disks ||
3808 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003809 pr_warn("md: %s: %s: only %d devices permitted\n",
3810 mdname(mddev), bdevname(rdev->bdev, b),
3811 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003812 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003813 continue;
3814 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003815 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 if (super_types[mddev->major_version].
3817 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003818 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003820 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 continue;
3822 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 if (mddev->level == LEVEL_MULTIPATH) {
3825 rdev->desc_nr = i++;
3826 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003827 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003828 } else if (rdev->raid_disk >=
3829 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3830 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003831 rdev->raid_disk = -1;
3832 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 }
3834 }
Yufen Yu6a5cb532019-10-16 16:00:03 +08003835
3836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837}
3838
NeilBrown72e02072009-12-14 12:49:55 +11003839/* Read a fixed-point number.
3840 * Numbers in sysfs attributes should be in "standard" units where
3841 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003842 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003843 * milliseconds or jiffies.
3844 * This function takes a decimal number with a possible fractional
3845 * component, and produces an integer which is the result of
3846 * multiplying that number by 10^'scale'.
3847 * all without any floating-point arithmetic.
3848 */
3849int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3850{
3851 unsigned long result = 0;
3852 long decimals = -1;
3853 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3854 if (*cp == '.')
3855 decimals = 0;
3856 else if (decimals < scale) {
3857 unsigned int value;
3858 value = *cp - '0';
3859 result = result * 10 + value;
3860 if (decimals >= 0)
3861 decimals++;
3862 }
3863 cp++;
3864 }
3865 if (*cp == '\n')
3866 cp++;
3867 if (*cp)
3868 return -EINVAL;
3869 if (decimals < 0)
3870 decimals = 0;
Andy Shevchenkocf891602019-07-23 23:41:55 +03003871 *res = result * int_pow(10, scale - decimals);
NeilBrown72e02072009-12-14 12:49:55 +11003872 return 0;
3873}
3874
NeilBrowneae17012005-11-08 21:39:23 -08003875static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003876safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003877{
3878 int msec = (mddev->safemode_delay*1000)/HZ;
3879 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3880}
3881static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003882safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003883{
NeilBrown16f17b32006-06-26 00:27:37 -07003884 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003885
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003886 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003887 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003888 return -EINVAL;
3889 }
3890
NeilBrown72e02072009-12-14 12:49:55 +11003891 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003892 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003893 if (msec == 0)
3894 mddev->safemode_delay = 0;
3895 else {
NeilBrown19052c02008-08-05 15:54:13 +10003896 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003897 unsigned long new_delay = (msec*HZ)/1000;
3898
3899 if (new_delay == 0)
3900 new_delay = 1;
3901 mddev->safemode_delay = new_delay;
3902 if (new_delay < old_delay || old_delay == 0)
3903 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003904 }
3905 return len;
3906}
3907static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003908__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003909
3910static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003911level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003912{
NeilBrown36d091f2014-12-15 12:56:58 +11003913 struct md_personality *p;
3914 int ret;
3915 spin_lock(&mddev->lock);
3916 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003917 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003918 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003919 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003920 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003921 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003922 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003923 else
NeilBrown36d091f2014-12-15 12:56:58 +11003924 ret = 0;
3925 spin_unlock(&mddev->lock);
3926 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003927}
3928
NeilBrownd9d166c2006-01-06 00:20:51 -08003929static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003930level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003931{
Dan Williamsf2859af2010-05-02 10:04:16 -07003932 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003933 ssize_t rv;
3934 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003935 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003936 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003937 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003938 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003939
NeilBrown67918752014-12-15 12:57:01 +11003940 if (slen == 0 || slen >= sizeof(clevel))
3941 return -EINVAL;
3942
3943 rv = mddev_lock(mddev);
3944 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003945 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003946
3947 if (mddev->pers == NULL) {
3948 strncpy(mddev->clevel, buf, slen);
3949 if (mddev->clevel[slen-1] == '\n')
3950 slen--;
3951 mddev->clevel[slen] = 0;
3952 mddev->level = LEVEL_NONE;
3953 rv = len;
3954 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003955 }
NeilBrown67918752014-12-15 12:57:01 +11003956 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003957 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003958 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003959
3960 /* request to change the personality. Need to ensure:
3961 * - array is not engaged in resync/recovery/reshape
3962 * - old personality can be suspended
3963 * - new personality will access other array.
3964 */
3965
NeilBrown67918752014-12-15 12:57:01 +11003966 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003967 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003968 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003969 mddev->reshape_position != MaxSector ||
3970 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003971 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003972
NeilBrown67918752014-12-15 12:57:01 +11003973 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003974 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003975 pr_warn("md: %s: %s does not support online personality change\n",
3976 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003977 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003978 }
3979
3980 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003981 strncpy(clevel, buf, slen);
3982 if (clevel[slen-1] == '\n')
3983 slen--;
3984 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003985 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003986 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003987
Dan Williamsf2859af2010-05-02 10:04:16 -07003988 if (request_module("md-%s", clevel) != 0)
3989 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003990 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003991 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003992 if (!pers || !try_module_get(pers->owner)) {
3993 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003994 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003995 rv = -EINVAL;
3996 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003997 }
3998 spin_unlock(&pers_lock);
3999
4000 if (pers == mddev->pers) {
4001 /* Nothing to do! */
4002 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11004003 rv = len;
4004 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11004005 }
4006 if (!pers->takeover) {
4007 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11004008 pr_warn("md: %s: %s does not support personality takeover\n",
4009 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11004010 rv = -EINVAL;
4011 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11004012 }
4013
NeilBrowndafb20f2012-03-19 12:46:39 +11004014 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01004015 rdev->new_raid_disk = rdev->raid_disk;
4016
NeilBrown245f46c2009-03-31 14:39:39 +11004017 /* ->takeover must set new_* and/or delta_disks
4018 * if it succeeds, and may set them when it fails.
4019 */
4020 priv = pers->takeover(mddev);
4021 if (IS_ERR(priv)) {
4022 mddev->new_level = mddev->level;
4023 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004024 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11004025 mddev->raid_disks -= mddev->delta_disks;
4026 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004027 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11004028 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11004029 pr_warn("md: %s: %s would not accept array\n",
4030 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11004031 rv = PTR_ERR(priv);
4032 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11004033 }
4034
4035 /* Looks like we have a winner */
4036 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11004037 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11004038
4039 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11004040 oldpers = mddev->pers;
4041 oldpriv = mddev->private;
4042 mddev->pers = pers;
4043 mddev->private = priv;
4044 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4045 mddev->level = mddev->new_level;
4046 mddev->layout = mddev->new_layout;
4047 mddev->chunk_sectors = mddev->new_chunk_sectors;
4048 mddev->delta_disks = 0;
4049 mddev->reshape_backwards = 0;
4050 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11004051 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10004052
NeilBrowndb721d32014-12-15 12:56:58 +11004053 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11004054 mddev->external) {
4055 /* We are converting from a no-redundancy array
4056 * to a redundancy array and metadata is managed
4057 * externally so we need to be sure that writes
4058 * won't block due to a need to transition
4059 * clean->dirty
4060 * until external management is started.
4061 */
4062 mddev->in_sync = 0;
4063 mddev->safemode_delay = 0;
4064 mddev->safemode = 0;
4065 }
4066
NeilBrowndb721d32014-12-15 12:56:58 +11004067 oldpers->free(mddev, oldpriv);
4068
4069 if (oldpers->sync_request == NULL &&
4070 pers->sync_request != NULL) {
4071 /* need to add the md_redundancy_group */
4072 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11004073 pr_warn("md: cannot register extra attributes for %s\n",
4074 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11004075 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
Junxiao Bie8efa9b2020-08-04 17:27:18 -07004076 mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed");
4077 mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded");
NeilBrowndb721d32014-12-15 12:56:58 +11004078 }
4079 if (oldpers->sync_request != NULL &&
4080 pers->sync_request == NULL) {
4081 /* need to remove the md_redundancy_group */
4082 if (mddev->to_remove == NULL)
4083 mddev->to_remove = &md_redundancy_group;
4084 }
4085
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02004086 module_put(oldpers->owner);
4087
NeilBrowndafb20f2012-03-19 12:46:39 +11004088 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004089 if (rdev->raid_disk < 0)
4090 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11004091 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01004092 rdev->new_raid_disk = -1;
4093 if (rdev->new_raid_disk == rdev->raid_disk)
4094 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10004095 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01004096 }
NeilBrowndafb20f2012-03-19 12:46:39 +11004097 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004098 if (rdev->raid_disk < 0)
4099 continue;
4100 if (rdev->new_raid_disk == rdev->raid_disk)
4101 continue;
4102 rdev->raid_disk = rdev->new_raid_disk;
4103 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10004104 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01004105 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10004106 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11004107 pr_warn("md: cannot register rd%d for %s after level change\n",
4108 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10004109 }
NeilBrowne93f68a2010-06-15 09:36:03 +01004110 }
4111
NeilBrowndb721d32014-12-15 12:56:58 +11004112 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11004113 /* this is now an array without redundancy, so
4114 * it must always be in_sync
4115 */
4116 mddev->in_sync = 1;
4117 del_timer_sync(&mddev->safemode_timer);
4118 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11004119 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11004120 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004121 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10004122 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11004123 if (!mddev->thread)
4124 md_update_sb(mddev, 1);
Junxiao Bie1a86db2020-07-14 16:10:26 -07004125 sysfs_notify_dirent_safe(mddev->sysfs_level);
Dan Williamsbb7f8d22010-05-01 18:14:57 -07004126 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004127 rv = len;
4128out_unlock:
4129 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08004130 return rv;
4131}
4132
4133static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07004134__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08004135
NeilBrownd4dbd022006-06-26 00:27:59 -07004136static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004137layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07004138{
4139 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07004140 if (mddev->reshape_position != MaxSector &&
4141 mddev->layout != mddev->new_layout)
4142 return sprintf(page, "%d (%d)\n",
4143 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07004144 return sprintf(page, "%d\n", mddev->layout);
4145}
4146
4147static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004148layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07004149{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004150 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11004151 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07004152
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004153 err = kstrtouint(buf, 10, &n);
4154 if (err < 0)
4155 return err;
NeilBrown67918752014-12-15 12:57:01 +11004156 err = mddev_lock(mddev);
4157 if (err)
4158 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07004159
NeilBrownb3546032009-03-31 14:56:41 +11004160 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10004161 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004162 err = -EBUSY;
4163 else if (mddev->ro)
4164 err = -EROFS;
4165 else {
4166 mddev->new_layout = n;
4167 err = mddev->pers->check_reshape(mddev);
4168 if (err)
4169 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10004170 }
NeilBrownb3546032009-03-31 14:56:41 +11004171 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07004172 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11004173 if (mddev->reshape_position == MaxSector)
4174 mddev->layout = n;
4175 }
NeilBrown67918752014-12-15 12:57:01 +11004176 mddev_unlock(mddev);
4177 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07004178}
4179static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07004180__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07004181
NeilBrowneae17012005-11-08 21:39:23 -08004182static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004183raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08004184{
NeilBrownbb636542005-11-08 21:39:45 -08004185 if (mddev->raid_disks == 0)
4186 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004187 if (mddev->reshape_position != MaxSector &&
4188 mddev->delta_disks != 0)
4189 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
4190 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08004191 return sprintf(page, "%d\n", mddev->raid_disks);
4192}
4193
NeilBrownfd01b882011-10-11 16:47:53 +11004194static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08004195
4196static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004197raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08004198{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004199 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11004200 int err;
NeilBrownda943b992006-01-06 00:20:54 -08004201
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004202 err = kstrtouint(buf, 10, &n);
4203 if (err < 0)
4204 return err;
NeilBrownda943b992006-01-06 00:20:54 -08004205
NeilBrown67918752014-12-15 12:57:01 +11004206 err = mddev_lock(mddev);
4207 if (err)
4208 return err;
NeilBrownda943b992006-01-06 00:20:54 -08004209 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11004210 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07004211 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10004212 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004213 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10004214
NeilBrown67918752014-12-15 12:57:01 +11004215 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10004216 rdev_for_each(rdev, mddev) {
4217 if (olddisks < n &&
4218 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11004219 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10004220 if (olddisks > n &&
4221 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11004222 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10004223 }
NeilBrown67918752014-12-15 12:57:01 +11004224 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004225 mddev->delta_disks = n - olddisks;
4226 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10004227 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07004228 } else
NeilBrownda943b992006-01-06 00:20:54 -08004229 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11004230out_unlock:
4231 mddev_unlock(mddev);
4232 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08004233}
4234static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07004235__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08004236
NeilBrown24dd4692005-11-08 21:39:26 -08004237static ssize_t
Sebastian Parschauerec164d072020-07-28 12:01:39 +02004238uuid_show(struct mddev *mddev, char *page)
4239{
4240 return sprintf(page, "%pU\n", mddev->uuid);
4241}
4242static struct md_sysfs_entry md_uuid =
4243__ATTR(uuid, S_IRUGO, uuid_show, NULL);
4244
4245static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004246chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08004247{
NeilBrown08a02ec2007-05-09 02:35:38 -07004248 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10004249 mddev->chunk_sectors != mddev->new_chunk_sectors)
4250 return sprintf(page, "%d (%d)\n",
4251 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10004252 mddev->chunk_sectors << 9);
4253 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08004254}
4255
4256static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004257chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08004258{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004259 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11004260 int err;
NeilBrown3b343802006-01-06 00:20:47 -08004261
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004262 err = kstrtoul(buf, 10, &n);
4263 if (err < 0)
4264 return err;
NeilBrown3b343802006-01-06 00:20:47 -08004265
NeilBrown67918752014-12-15 12:57:01 +11004266 err = mddev_lock(mddev);
4267 if (err)
4268 return err;
NeilBrownb3546032009-03-31 14:56:41 +11004269 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10004270 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004271 err = -EBUSY;
4272 else if (mddev->ro)
4273 err = -EROFS;
4274 else {
4275 mddev->new_chunk_sectors = n >> 9;
4276 err = mddev->pers->check_reshape(mddev);
4277 if (err)
4278 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10004279 }
NeilBrownb3546032009-03-31 14:56:41 +11004280 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10004281 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004282 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10004283 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004284 }
NeilBrown67918752014-12-15 12:57:01 +11004285 mddev_unlock(mddev);
4286 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08004287}
4288static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004289__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08004290
NeilBrowna94213b2006-06-26 00:28:00 -07004291static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004292resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07004293{
NeilBrownd1a7c502009-03-31 15:24:32 +11004294 if (mddev->recovery_cp == MaxSector)
4295 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07004296 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4297}
4298
4299static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004300resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07004301{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004302 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11004303 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004304
4305 if (cmd_match(buf, "none"))
4306 n = MaxSector;
4307 else {
4308 err = kstrtoull(buf, 10, &n);
4309 if (err < 0)
4310 return err;
4311 if (n != (sector_t)n)
4312 return -EINVAL;
4313 }
NeilBrowna94213b2006-06-26 00:28:00 -07004314
NeilBrown67918752014-12-15 12:57:01 +11004315 err = mddev_lock(mddev);
4316 if (err)
4317 return err;
NeilBrownb0986362011-05-11 15:52:21 +10004318 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11004319 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07004320
NeilBrown67918752014-12-15 12:57:01 +11004321 if (!err) {
4322 mddev->recovery_cp = n;
4323 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08004324 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11004325 }
4326 mddev_unlock(mddev);
4327 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07004328}
4329static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10004330__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4331 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07004332
NeilBrown9e653b62006-06-26 00:27:58 -07004333/*
4334 * The array state can be:
4335 *
4336 * clear
4337 * No devices, no size, no level
4338 * Equivalent to STOP_ARRAY ioctl
4339 * inactive
4340 * May have some settings, but array is not active
4341 * all IO results in error
4342 * When written, doesn't tear down array, but just stops it
4343 * suspended (not supported yet)
4344 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01004345 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07004346 * readonly
4347 * no resync can happen. no superblocks get written.
4348 * write requests fail
4349 * read-auto
4350 * like readonly, but behaves like 'clean' on a write request.
4351 *
4352 * clean - no pending writes, but otherwise active.
4353 * When written to inactive array, starts without resync
4354 * If a write request arrives then
4355 * if metadata is known, mark 'dirty' and switch to 'active'.
4356 * if not known, block and switch to write-pending
4357 * If written to an active array that has pending writes, then fails.
4358 * active
4359 * fully active: IO and resync can be happening.
4360 * When written to inactive array, starts with resync
4361 *
4362 * write-pending
4363 * clean, but writes are blocked waiting for 'active' to be written.
4364 *
4365 * active-idle
4366 * like active, but no writes have been seen for a while (100msec).
4367 *
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004368 * broken
4369 * RAID0/LINEAR-only: same as clean, but array is missing a member.
4370 * It's useful because RAID0/LINEAR mounted-arrays aren't stopped
4371 * when a member is gone, so this state will at least alert the
4372 * user that something is wrong.
NeilBrown9e653b62006-06-26 00:27:58 -07004373 */
4374enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004375 write_pending, active_idle, broken, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004376static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004377 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004378 "write-pending", "active-idle", "broken", NULL };
NeilBrown9e653b62006-06-26 00:27:58 -07004379
4380static int match_word(const char *word, char **list)
4381{
4382 int n;
4383 for (n=0; list[n]; n++)
4384 if (cmd_match(word, list[n]))
4385 break;
4386 return n;
4387}
4388
4389static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004390array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004391{
4392 enum array_state st = inactive;
4393
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004394 if (mddev->pers && !test_bit(MD_NOT_READY, &mddev->flags)) {
NeilBrown9e653b62006-06-26 00:27:58 -07004395 switch(mddev->ro) {
4396 case 1:
4397 st = readonly;
4398 break;
4399 case 2:
4400 st = read_auto;
4401 break;
4402 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004403 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004404 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004405 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004406 else if (mddev->in_sync)
4407 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004408 else if (mddev->safemode)
4409 st = active_idle;
4410 else
4411 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004412 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004413 }
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004414
4415 if (test_bit(MD_BROKEN, &mddev->flags) && st == clean)
4416 st = broken;
4417 } else {
NeilBrown9e653b62006-06-26 00:27:58 -07004418 if (list_empty(&mddev->disks) &&
4419 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004420 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004421 st = clear;
4422 else
4423 st = inactive;
4424 }
4425 return sprintf(page, "%s\n", array_states[st]);
4426}
4427
NeilBrownf72ffdd2014-09-30 14:23:59 +10004428static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4429static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
NeilBrownfd01b882011-10-11 16:47:53 +11004430static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004431
4432static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004433array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004434{
NeilBrown6497709b2017-03-15 14:05:14 +11004435 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004436 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004437
4438 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4439 /* don't take reconfig_mutex when toggling between
4440 * clean and active
4441 */
4442 spin_lock(&mddev->lock);
4443 if (st == active) {
4444 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004445 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004446 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004447 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004448 } else /* st == clean */ {
4449 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004450 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004451 err = -EBUSY;
4452 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004453 if (!err)
4454 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004455 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004456 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004457 }
4458 err = mddev_lock(mddev);
4459 if (err)
4460 return err;
4461 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004462 switch(st) {
4463 case bad_word:
4464 break;
4465 case clear:
4466 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004467 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004468 break;
4469 case inactive:
4470 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004471 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004472 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004473 else
NeilBrowne6910632008-02-06 01:39:51 -08004474 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004475 break;
4476 case suspended:
4477 break; /* not supported yet */
4478 case readonly:
4479 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004480 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004481 else {
4482 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004483 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004484 err = do_md_run(mddev);
4485 }
4486 break;
4487 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004488 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004489 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004490 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004491 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004492 err = restart_array(mddev);
4493 if (err == 0) {
4494 mddev->ro = 2;
4495 set_disk_ro(mddev->gendisk, 0);
4496 }
NeilBrown9e653b62006-06-26 00:27:58 -07004497 } else {
4498 mddev->ro = 2;
4499 err = do_md_run(mddev);
4500 }
4501 break;
4502 case clean:
4503 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004504 err = restart_array(mddev);
4505 if (err)
4506 break;
NeilBrown85572d72014-12-15 12:56:56 +11004507 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004508 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004509 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004510 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004511 } else
4512 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004513 break;
4514 case active:
4515 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004516 err = restart_array(mddev);
4517 if (err)
4518 break;
Shaohua Li29530792016-12-08 15:48:19 -08004519 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004520 wake_up(&mddev->sb_wait);
4521 err = 0;
4522 } else {
4523 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004524 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004525 err = do_md_run(mddev);
4526 }
4527 break;
4528 case write_pending:
4529 case active_idle:
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004530 case broken:
NeilBrown9e653b62006-06-26 00:27:58 -07004531 /* these cannot be set */
4532 break;
4533 }
NeilBrown67918752014-12-15 12:57:01 +11004534
4535 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004536 if (mddev->hold_active == UNTIL_IOCTL)
4537 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004538 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004539 }
NeilBrown67918752014-12-15 12:57:01 +11004540 mddev_unlock(mddev);
4541 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004542}
NeilBrown80ca3a42006-07-10 04:44:18 -07004543static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004544__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004545
NeilBrown6d7ff7382006-01-06 00:21:16 -08004546static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004547max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004548 return sprintf(page, "%d\n",
4549 atomic_read(&mddev->max_corr_read_errors));
4550}
4551
4552static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004553max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004554{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004555 unsigned int n;
4556 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004557
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004558 rv = kstrtouint(buf, 10, &n);
4559 if (rv < 0)
4560 return rv;
4561 atomic_set(&mddev->max_corr_read_errors, n);
4562 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004563}
4564
4565static struct md_sysfs_entry max_corr_read_errors =
4566__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4567 max_corrected_read_errors_store);
4568
4569static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004570null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004571{
4572 return -EINVAL;
4573}
4574
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004575/* need to ensure rdev_delayed_delete() has completed */
4576static void flush_rdev_wq(struct mddev *mddev)
4577{
4578 struct md_rdev *rdev;
4579
4580 rcu_read_lock();
4581 rdev_for_each_rcu(rdev, mddev)
4582 if (work_pending(&rdev->del_work)) {
4583 flush_workqueue(md_rdev_misc_wq);
4584 break;
4585 }
4586 rcu_read_unlock();
4587}
4588
NeilBrown6d7ff7382006-01-06 00:21:16 -08004589static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004590new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004591{
4592 /* buf must be %d:%d\n? giving major and minor numbers */
4593 /* The new device is added to the array.
4594 * If the array has a persistent superblock, we read the
4595 * superblock to initialise info and check validity.
4596 * Otherwise, only checking done is that in bind_rdev_to_array,
4597 * which mainly checks size.
4598 */
4599 char *e;
4600 int major = simple_strtoul(buf, &e, 10);
4601 int minor;
4602 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004603 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004604 int err;
4605
4606 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4607 return -EINVAL;
4608 minor = simple_strtoul(e+1, &e, 10);
4609 if (*e && *e != '\n')
4610 return -EINVAL;
4611 dev = MKDEV(major, minor);
4612 if (major != MAJOR(dev) ||
4613 minor != MINOR(dev))
4614 return -EOVERFLOW;
4615
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004616 flush_rdev_wq(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004617 err = mddev_lock(mddev);
4618 if (err)
4619 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004620 if (mddev->persistent) {
4621 rdev = md_import_device(dev, mddev->major_version,
4622 mddev->minor_version);
4623 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004624 struct md_rdev *rdev0
4625 = list_entry(mddev->disks.next,
4626 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004627 err = super_types[mddev->major_version]
4628 .load_super(rdev, rdev0, mddev->minor_version);
4629 if (err < 0)
4630 goto out;
4631 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004632 } else if (mddev->external)
4633 rdev = md_import_device(dev, -2, -1);
4634 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004635 rdev = md_import_device(dev, -1, -1);
4636
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004637 if (IS_ERR(rdev)) {
4638 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004639 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004640 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004641 err = bind_rdev_to_array(rdev, mddev);
4642 out:
4643 if (err)
4644 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004645 mddev_unlock(mddev);
Alexey Obitotskiy5492c462017-07-28 15:49:25 +02004646 if (!err)
4647 md_new_event(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004648 return err ? err : len;
4649}
4650
4651static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004652__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004653
4654static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004655bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004656{
4657 char *end;
4658 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004659 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004660
NeilBrown67918752014-12-15 12:57:01 +11004661 err = mddev_lock(mddev);
4662 if (err)
4663 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004664 if (!mddev->bitmap)
4665 goto out;
4666 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4667 while (*buf) {
4668 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4669 if (buf == end) break;
4670 if (*end == '-') { /* range */
4671 buf = end + 1;
4672 end_chunk = simple_strtoul(buf, &end, 0);
4673 if (buf == end) break;
4674 }
4675 if (*end && !isspace(*end)) break;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004676 md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004677 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004678 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004679 md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
Paul Clements9b1d1da2006-10-03 01:15:49 -07004680out:
NeilBrown67918752014-12-15 12:57:01 +11004681 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004682 return len;
4683}
4684
4685static struct md_sysfs_entry md_bitmap =
4686__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4687
4688static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004689size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004690{
Andre Noll58c0fed2009-03-31 14:33:13 +11004691 return sprintf(page, "%llu\n",
4692 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004693}
4694
NeilBrownfd01b882011-10-11 16:47:53 +11004695static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004696
4697static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004698size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004699{
4700 /* If array is inactive, we can reduce the component size, but
4701 * not increase it (except from 0).
4702 * If array is active, we can try an on-line resize
4703 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004704 sector_t sectors;
4705 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004706
Andre Noll58c0fed2009-03-31 14:33:13 +11004707 if (err < 0)
4708 return err;
NeilBrown67918752014-12-15 12:57:01 +11004709 err = mddev_lock(mddev);
4710 if (err)
4711 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004712 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004713 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004714 if (err == 0)
4715 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004716 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004717 if (mddev->dev_sectors == 0 ||
4718 mddev->dev_sectors > sectors)
4719 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004720 else
4721 err = -ENOSPC;
4722 }
NeilBrown67918752014-12-15 12:57:01 +11004723 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004724 return err ? err : len;
4725}
4726
4727static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004728__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004729
Masanari Iida83f0d772012-10-30 00:18:08 +09004730/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004731 * This is one of
4732 * 'none' for arrays with no metadata (good luck...)
4733 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004734 * or N.M for internally known formats
4735 */
4736static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004737metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004738{
4739 if (mddev->persistent)
4740 return sprintf(page, "%d.%d\n",
4741 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004742 else if (mddev->external)
4743 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004744 else
4745 return sprintf(page, "none\n");
4746}
4747
4748static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004749metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004750{
4751 int major, minor;
4752 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004753 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004754 /* Changing the details of 'external' metadata is
4755 * always permitted. Otherwise there must be
4756 * no devices attached to the array.
4757 */
NeilBrown67918752014-12-15 12:57:01 +11004758
4759 err = mddev_lock(mddev);
4760 if (err)
4761 return err;
4762 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004763 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4764 ;
4765 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004766 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004767
NeilBrown67918752014-12-15 12:57:01 +11004768 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004769 if (cmd_match(buf, "none")) {
4770 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004771 mddev->external = 0;
4772 mddev->major_version = 0;
4773 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004774 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004775 }
4776 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004777 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004778 if (namelen >= sizeof(mddev->metadata_type))
4779 namelen = sizeof(mddev->metadata_type)-1;
4780 strncpy(mddev->metadata_type, buf+9, namelen);
4781 mddev->metadata_type[namelen] = 0;
4782 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4783 mddev->metadata_type[--namelen] = 0;
4784 mddev->persistent = 0;
4785 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004786 mddev->major_version = 0;
4787 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004788 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004789 }
4790 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004791 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004792 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004793 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004794 buf = e+1;
4795 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004796 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004797 goto out_unlock;
4798 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004799 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004800 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004801 mddev->major_version = major;
4802 mddev->minor_version = minor;
4803 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004804 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004805 err = 0;
4806out_unlock:
4807 mddev_unlock(mddev);
4808 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004809}
4810
4811static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004812__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004813
NeilBrowna35b0d62006-01-06 00:20:49 -08004814static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004815action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004816{
NeilBrown7eec3142005-11-08 21:39:44 -08004817 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004818 unsigned long recovery = mddev->recovery;
4819 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004820 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004821 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4822 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4823 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004824 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004825 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4826 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004827 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004828 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004829 type = "check";
4830 else
4831 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004832 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004833 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004834 else if (mddev->reshape_position != MaxSector)
4835 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004836 }
4837 return sprintf(page, "%s\n", type);
4838}
4839
4840static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004841action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004842{
NeilBrown7eec3142005-11-08 21:39:44 -08004843 if (!mddev->pers || !mddev->pers->sync_request)
4844 return -EINVAL;
4845
NeilBrownb6a9ce62009-05-26 09:41:17 +10004846
4847 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004848 if (cmd_match(page, "frozen"))
4849 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4850 else
4851 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004852 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4853 mddev_lock(mddev) == 0) {
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004854 if (work_pending(&mddev->del_work))
4855 flush_workqueue(md_misc_wq);
NeilBrown8e8e2512015-06-12 19:51:27 +10004856 if (mddev->sync_thread) {
4857 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004858 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004859 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004860 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004861 }
NeilBrown312045e2015-12-21 11:01:21 +11004862 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004863 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004864 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004865 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004866 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004867 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004868 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004869 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004870 int err;
4871 if (mddev->pers->start_reshape == NULL)
4872 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004873 err = mddev_lock(mddev);
4874 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004875 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4876 err = -EBUSY;
4877 else {
4878 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4879 err = mddev->pers->start_reshape(mddev);
4880 }
NeilBrown67918752014-12-15 12:57:01 +11004881 mddev_unlock(mddev);
4882 }
NeilBrown16484bf2006-03-27 01:18:13 -08004883 if (err)
4884 return err;
Junxiao Bie1a86db2020-07-14 16:10:26 -07004885 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
NeilBrown16484bf2006-03-27 01:18:13 -08004886 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004887 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004888 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004889 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004890 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004891 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004892 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4893 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004894 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004895 if (mddev->ro == 2) {
4896 /* A write to sync_action is enough to justify
4897 * canceling read-auto mode
4898 */
4899 mddev->ro = 0;
4900 md_wakeup_thread(mddev->sync_thread);
4901 }
NeilBrown03c902e2006-01-06 00:20:46 -08004902 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004903 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004904 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004905 return len;
4906}
4907
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004908static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004909__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004910
4911static ssize_t
4912last_sync_action_show(struct mddev *mddev, char *page)
4913{
4914 return sprintf(page, "%s\n", mddev->last_sync_action);
4915}
4916
4917static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4918
NeilBrown9d888832005-11-08 21:39:26 -08004919static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004920mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004921{
4922 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004923 (unsigned long long)
4924 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004925}
4926
NeilBrown80ca3a42006-07-10 04:44:18 -07004927static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004928
NeilBrown88202a02006-01-06 00:21:36 -08004929static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004930sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004931{
4932 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4933 mddev->sync_speed_min ? "local": "system");
4934}
4935
4936static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004937sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004938{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004939 unsigned int min;
4940 int rv;
4941
NeilBrown88202a02006-01-06 00:21:36 -08004942 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004943 min = 0;
4944 } else {
4945 rv = kstrtouint(buf, 10, &min);
4946 if (rv < 0)
4947 return rv;
4948 if (min == 0)
4949 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004950 }
NeilBrown88202a02006-01-06 00:21:36 -08004951 mddev->sync_speed_min = min;
4952 return len;
4953}
4954
4955static struct md_sysfs_entry md_sync_min =
4956__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4957
4958static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004959sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004960{
4961 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4962 mddev->sync_speed_max ? "local": "system");
4963}
4964
4965static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004966sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004967{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004968 unsigned int max;
4969 int rv;
4970
NeilBrown88202a02006-01-06 00:21:36 -08004971 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004972 max = 0;
4973 } else {
4974 rv = kstrtouint(buf, 10, &max);
4975 if (rv < 0)
4976 return rv;
4977 if (max == 0)
4978 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004979 }
NeilBrown88202a02006-01-06 00:21:36 -08004980 mddev->sync_speed_max = max;
4981 return len;
4982}
4983
4984static struct md_sysfs_entry md_sync_max =
4985__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4986
Iustin Popd7f3d292007-10-16 23:30:54 -07004987static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004988degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004989{
4990 return sprintf(page, "%d\n", mddev->degraded);
4991}
4992static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004993
4994static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004995sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004996{
4997 return sprintf(page, "%d\n", mddev->parallel_resync);
4998}
4999
5000static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005001sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07005002{
5003 long n;
5004
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005005 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07005006 return -EINVAL;
5007
5008 if (n != 0 && n != 1)
5009 return -EINVAL;
5010
5011 mddev->parallel_resync = n;
5012
5013 if (mddev->sync_thread)
5014 wake_up(&resync_wait);
5015
5016 return len;
5017}
5018
5019/* force parallel resync, even with shared block devices */
5020static struct md_sysfs_entry md_sync_force_parallel =
5021__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
5022 sync_force_parallel_show, sync_force_parallel_store);
5023
5024static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005025sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08005026{
5027 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11005028 if (mddev->curr_resync == 0)
5029 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01005030 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
5031 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08005032 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01005033 db = resync - mddev->resync_mark_cnt;
5034 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08005035}
5036
NeilBrown80ca3a42006-07-10 04:44:18 -07005037static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08005038
5039static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005040sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08005041{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11005042 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08005043
NeilBrownacb180b2009-04-14 16:28:34 +10005044 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5045 return sprintf(page, "none\n");
5046
NeilBrown72f36d52012-10-11 14:25:57 +11005047 if (mddev->curr_resync == 1 ||
5048 mddev->curr_resync == 2)
5049 return sprintf(page, "delayed\n");
5050
NeilBrownc804cde2012-05-21 09:28:33 +10005051 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
5052 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11005053 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08005054 else
Andre Noll58c0fed2009-03-31 14:33:13 +11005055 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08005056
NeilBrownacb180b2009-04-14 16:28:34 +10005057 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11005058 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08005059}
5060
NeilBrown750f1992014-09-30 08:53:05 +10005061static struct md_sysfs_entry md_sync_completed =
5062 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08005063
NeilBrowne464eaf2006-03-27 01:18:14 -08005064static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005065min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10005066{
5067 return sprintf(page, "%llu\n",
5068 (unsigned long long)mddev->resync_min);
5069}
5070static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005071min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10005072{
5073 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11005074 int err;
NeilBrown23da4222014-12-15 12:57:01 +11005075
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005076 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10005077 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11005078
5079 spin_lock(&mddev->lock);
5080 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10005081 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11005082 goto out_unlock;
5083
5084 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10005085 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11005086 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10005087
NeilBrown50c37b12015-03-23 17:36:38 +11005088 /* Round down to multiple of 4K for safety */
5089 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11005090 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10005091
NeilBrown23da4222014-12-15 12:57:01 +11005092out_unlock:
5093 spin_unlock(&mddev->lock);
5094 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10005095}
5096
5097static struct md_sysfs_entry md_min_sync =
5098__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
5099
5100static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005101max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08005102{
5103 if (mddev->resync_max == MaxSector)
5104 return sprintf(page, "max\n");
5105 else
5106 return sprintf(page, "%llu\n",
5107 (unsigned long long)mddev->resync_max);
5108}
5109static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005110max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08005111{
NeilBrown23da4222014-12-15 12:57:01 +11005112 int err;
5113 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08005114 if (strncmp(buf, "max", 3) == 0)
5115 mddev->resync_max = MaxSector;
5116 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10005117 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11005118 int chunk;
5119
5120 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005121 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11005122 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10005123 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11005124 goto out_unlock;
5125
5126 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08005127 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10005128 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08005129 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11005130 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08005131
5132 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11005133 chunk = mddev->chunk_sectors;
5134 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10005135 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11005136
5137 err = -EINVAL;
5138 if (sector_div(temp, chunk))
5139 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08005140 }
5141 mddev->resync_max = max;
5142 }
5143 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11005144 err = 0;
5145out_unlock:
5146 spin_unlock(&mddev->lock);
5147 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08005148}
5149
5150static struct md_sysfs_entry md_max_sync =
5151__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
5152
5153static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005154suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08005155{
5156 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
5157}
5158
5159static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005160suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08005161{
NeilBrownb03e0cc2017-10-19 12:49:15 +11005162 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005163 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08005164
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005165 err = kstrtoull(buf, 10, &new);
5166 if (err < 0)
5167 return err;
5168 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08005169 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11005170
NeilBrown67918752014-12-15 12:57:01 +11005171 err = mddev_lock(mddev);
5172 if (err)
5173 return err;
5174 err = -EINVAL;
5175 if (mddev->pers == NULL ||
5176 mddev->pers->quiesce == NULL)
5177 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005178 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11005179 mddev->suspend_lo = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005180 mddev_resume(mddev);
5181
NeilBrown67918752014-12-15 12:57:01 +11005182 err = 0;
5183unlock:
5184 mddev_unlock(mddev);
5185 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08005186}
5187static struct md_sysfs_entry md_suspend_lo =
5188__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
5189
NeilBrowne464eaf2006-03-27 01:18:14 -08005190static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005191suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08005192{
5193 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
5194}
5195
5196static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005197suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08005198{
NeilBrownb03e0cc2017-10-19 12:49:15 +11005199 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005200 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08005201
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005202 err = kstrtoull(buf, 10, &new);
5203 if (err < 0)
5204 return err;
5205 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08005206 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11005207
NeilBrown67918752014-12-15 12:57:01 +11005208 err = mddev_lock(mddev);
5209 if (err)
5210 return err;
5211 err = -EINVAL;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005212 if (mddev->pers == NULL)
NeilBrown67918752014-12-15 12:57:01 +11005213 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005214
5215 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11005216 mddev->suspend_hi = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005217 mddev_resume(mddev);
5218
NeilBrown67918752014-12-15 12:57:01 +11005219 err = 0;
5220unlock:
5221 mddev_unlock(mddev);
5222 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08005223}
5224static struct md_sysfs_entry md_suspend_hi =
5225__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
5226
NeilBrown08a02ec2007-05-09 02:35:38 -07005227static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005228reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07005229{
5230 if (mddev->reshape_position != MaxSector)
5231 return sprintf(page, "%llu\n",
5232 (unsigned long long)mddev->reshape_position);
5233 strcpy(page, "none\n");
5234 return 5;
5235}
5236
5237static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005238reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07005239{
NeilBrownc6563a82012-05-21 09:27:00 +10005240 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005241 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005242 int err;
NeilBrown67918752014-12-15 12:57:01 +11005243
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005244 err = kstrtoull(buf, 10, &new);
5245 if (err < 0)
5246 return err;
5247 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07005248 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11005249 err = mddev_lock(mddev);
5250 if (err)
5251 return err;
5252 err = -EBUSY;
5253 if (mddev->pers)
5254 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07005255 mddev->reshape_position = new;
5256 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005257 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07005258 mddev->new_level = mddev->level;
5259 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10005260 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10005261 rdev_for_each(rdev, mddev)
5262 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11005263 err = 0;
5264unlock:
5265 mddev_unlock(mddev);
5266 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07005267}
5268
5269static struct md_sysfs_entry md_reshape_position =
5270__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
5271 reshape_position_store);
5272
Dan Williamsb522adc2009-03-31 15:00:31 +11005273static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10005274reshape_direction_show(struct mddev *mddev, char *page)
5275{
5276 return sprintf(page, "%s\n",
5277 mddev->reshape_backwards ? "backwards" : "forwards");
5278}
5279
5280static ssize_t
5281reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
5282{
5283 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11005284 int err;
5285
NeilBrown2c810cd2012-05-21 09:27:00 +10005286 if (cmd_match(buf, "forwards"))
5287 backwards = 0;
5288 else if (cmd_match(buf, "backwards"))
5289 backwards = 1;
5290 else
5291 return -EINVAL;
5292 if (mddev->reshape_backwards == backwards)
5293 return len;
5294
NeilBrown67918752014-12-15 12:57:01 +11005295 err = mddev_lock(mddev);
5296 if (err)
5297 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10005298 /* check if we are allowed to change */
5299 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11005300 err = -EBUSY;
5301 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10005302 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11005303 err = -EINVAL;
5304 else
5305 mddev->reshape_backwards = backwards;
5306 mddev_unlock(mddev);
5307 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10005308}
5309
5310static struct md_sysfs_entry md_reshape_direction =
5311__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5312 reshape_direction_store);
5313
5314static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005315array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11005316{
5317 if (mddev->external_size)
5318 return sprintf(page, "%llu\n",
5319 (unsigned long long)mddev->array_sectors/2);
5320 else
5321 return sprintf(page, "default\n");
5322}
5323
5324static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005325array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11005326{
5327 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11005328 int err;
5329
5330 err = mddev_lock(mddev);
5331 if (err)
5332 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11005333
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005334 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08005335 if (mddev_is_clustered(mddev)) {
5336 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005337 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08005338 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005339
Dan Williamsb522adc2009-03-31 15:00:31 +11005340 if (strncmp(buf, "default", 7) == 0) {
5341 if (mddev->pers)
5342 sectors = mddev->pers->size(mddev, 0, 0);
5343 else
5344 sectors = mddev->array_sectors;
5345
5346 mddev->external_size = 0;
5347 } else {
5348 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11005349 err = -EINVAL;
5350 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5351 err = -E2BIG;
5352 else
5353 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11005354 }
5355
NeilBrown67918752014-12-15 12:57:01 +11005356 if (!err) {
5357 mddev->array_sectors = sectors;
5358 if (mddev->pers) {
5359 set_capacity(mddev->gendisk, mddev->array_sectors);
Christoph Hellwig659e56b2020-09-01 17:57:43 +02005360 revalidate_disk_size(mddev->gendisk, true);
NeilBrown67918752014-12-15 12:57:01 +11005361 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11005362 }
NeilBrown67918752014-12-15 12:57:01 +11005363 mddev_unlock(mddev);
5364 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11005365}
5366
5367static struct md_sysfs_entry md_array_size =
5368__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5369 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005370
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005371static ssize_t
5372consistency_policy_show(struct mddev *mddev, char *page)
5373{
5374 int ret;
5375
5376 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5377 ret = sprintf(page, "journal\n");
5378 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5379 ret = sprintf(page, "ppl\n");
5380 } else if (mddev->bitmap) {
5381 ret = sprintf(page, "bitmap\n");
5382 } else if (mddev->pers) {
5383 if (mddev->pers->sync_request)
5384 ret = sprintf(page, "resync\n");
5385 else
5386 ret = sprintf(page, "none\n");
5387 } else {
5388 ret = sprintf(page, "unknown\n");
5389 }
5390
5391 return ret;
5392}
5393
5394static ssize_t
5395consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5396{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005397 int err = 0;
5398
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005399 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005400 if (mddev->pers->change_consistency_policy)
5401 err = mddev->pers->change_consistency_policy(mddev, buf);
5402 else
5403 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005404 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5405 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005406 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005407 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005408 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005409
5410 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005411}
5412
5413static struct md_sysfs_entry md_consistency_policy =
5414__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5415 consistency_policy_store);
5416
Guoqing Jiang9a567842019-07-24 11:09:19 +02005417static ssize_t fail_last_dev_show(struct mddev *mddev, char *page)
5418{
5419 return sprintf(page, "%d\n", mddev->fail_last_dev);
5420}
5421
5422/*
5423 * Setting fail_last_dev to true to allow last device to be forcibly removed
5424 * from RAID1/RAID10.
5425 */
5426static ssize_t
5427fail_last_dev_store(struct mddev *mddev, const char *buf, size_t len)
5428{
5429 int ret;
5430 bool value;
5431
5432 ret = kstrtobool(buf, &value);
5433 if (ret)
5434 return ret;
5435
5436 if (value != mddev->fail_last_dev)
5437 mddev->fail_last_dev = value;
5438
5439 return len;
5440}
5441static struct md_sysfs_entry md_fail_last_dev =
5442__ATTR(fail_last_dev, S_IRUGO | S_IWUSR, fail_last_dev_show,
5443 fail_last_dev_store);
5444
Guoqing Jiang3938f5f2019-12-23 10:48:56 +01005445static ssize_t serialize_policy_show(struct mddev *mddev, char *page)
5446{
5447 if (mddev->pers == NULL || (mddev->pers->level != 1))
5448 return sprintf(page, "n/a\n");
5449 else
5450 return sprintf(page, "%d\n", mddev->serialize_policy);
5451}
5452
5453/*
5454 * Setting serialize_policy to true to enforce write IO is not reordered
5455 * for raid1.
5456 */
5457static ssize_t
5458serialize_policy_store(struct mddev *mddev, const char *buf, size_t len)
5459{
5460 int err;
5461 bool value;
5462
5463 err = kstrtobool(buf, &value);
5464 if (err)
5465 return err;
5466
5467 if (value == mddev->serialize_policy)
5468 return len;
5469
5470 err = mddev_lock(mddev);
5471 if (err)
5472 return err;
5473 if (mddev->pers == NULL || (mddev->pers->level != 1)) {
5474 pr_err("md: serialize_policy is only effective for raid1\n");
5475 err = -EINVAL;
5476 goto unlock;
5477 }
5478
5479 mddev_suspend(mddev);
5480 if (value)
5481 mddev_create_serial_pool(mddev, NULL, true);
5482 else
5483 mddev_destroy_serial_pool(mddev, NULL, true);
5484 mddev->serialize_policy = value;
5485 mddev_resume(mddev);
5486unlock:
5487 mddev_unlock(mddev);
5488 return err ?: len;
5489}
5490
5491static struct md_sysfs_entry md_serialize_policy =
5492__ATTR(serialize_policy, S_IRUGO | S_IWUSR, serialize_policy_show,
5493 serialize_policy_store);
5494
5495
NeilBrowneae17012005-11-08 21:39:23 -08005496static struct attribute *md_default_attrs[] = {
5497 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005498 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005499 &md_raid_disks.attr,
Sebastian Parschauerec164d072020-07-28 12:01:39 +02005500 &md_uuid.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005501 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005502 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005503 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005504 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005505 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005506 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005507 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005508 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005509 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005510 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005511 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005512 &md_consistency_policy.attr,
Guoqing Jiang9a567842019-07-24 11:09:19 +02005513 &md_fail_last_dev.attr,
Guoqing Jiang3938f5f2019-12-23 10:48:56 +01005514 &md_serialize_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005515 NULL,
5516};
5517
5518static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005519 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005520 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005521 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005522 &md_sync_min.attr,
5523 &md_sync_max.attr,
5524 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005525 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005526 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005527 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005528 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005529 &md_suspend_lo.attr,
5530 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005531 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005532 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005533 NULL,
5534};
NeilBrown411036f2005-11-08 21:39:40 -08005535static struct attribute_group md_redundancy_group = {
5536 .name = NULL,
5537 .attrs = md_redundancy_attrs,
5538};
5539
NeilBrowneae17012005-11-08 21:39:23 -08005540static ssize_t
5541md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5542{
5543 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005544 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005545 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005546
5547 if (!entry->show)
5548 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005549 spin_lock(&all_mddevs_lock);
5550 if (list_empty(&mddev->all_mddevs)) {
5551 spin_unlock(&all_mddevs_lock);
5552 return -EBUSY;
5553 }
5554 mddev_get(mddev);
5555 spin_unlock(&all_mddevs_lock);
5556
NeilBrownb7b17c92014-12-15 12:56:59 +11005557 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005558 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005559 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005560}
5561
5562static ssize_t
5563md_attr_store(struct kobject *kobj, struct attribute *attr,
5564 const char *page, size_t length)
5565{
5566 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005567 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005568 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005569
5570 if (!entry->store)
5571 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005572 if (!capable(CAP_SYS_ADMIN))
5573 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005574 spin_lock(&all_mddevs_lock);
5575 if (list_empty(&mddev->all_mddevs)) {
5576 spin_unlock(&all_mddevs_lock);
5577 return -EBUSY;
5578 }
5579 mddev_get(mddev);
5580 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005581 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005582 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005583 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005584}
5585
5586static void md_free(struct kobject *ko)
5587{
NeilBrownfd01b882011-10-11 16:47:53 +11005588 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005589
5590 if (mddev->sysfs_state)
5591 sysfs_put(mddev->sysfs_state);
Junxiao Bie1a86db2020-07-14 16:10:26 -07005592 if (mddev->sysfs_level)
5593 sysfs_put(mddev->sysfs_level);
5594
Bart Van Assched8115c352018-02-28 10:15:29 -08005595 if (mddev->gendisk)
5596 del_gendisk(mddev->gendisk);
NeilBrown6cd18e72015-04-27 14:12:22 +10005597 if (mddev->queue)
5598 blk_cleanup_queue(mddev->queue);
Bart Van Assched8115c352018-02-28 10:15:29 -08005599 if (mddev->gendisk)
NeilBrowna21d1502009-01-09 08:31:09 +11005600 put_disk(mddev->gendisk);
NeilBrown4ad23a972017-03-15 14:05:14 +11005601 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005602
Kent Overstreet28dec872018-06-07 20:52:54 -04005603 bioset_exit(&mddev->bio_set);
5604 bioset_exit(&mddev->sync_set);
Artur Paszkiewicz41d2d842020-07-03 11:13:09 +02005605 mempool_exit(&mddev->md_io_pool);
NeilBrowneae17012005-11-08 21:39:23 -08005606 kfree(mddev);
5607}
5608
Emese Revfy52cf25d2010-01-19 02:58:23 +01005609static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005610 .show = md_attr_show,
5611 .store = md_attr_store,
5612};
5613static struct kobj_type md_ktype = {
5614 .release = md_free,
5615 .sysfs_ops = &md_sysfs_ops,
5616 .default_attrs = md_default_attrs,
5617};
5618
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619int mdp_major = 0;
5620
Dan Williams5fd3a172009-03-04 00:57:25 -07005621static void mddev_delayed_delete(struct work_struct *ws)
5622{
NeilBrownfd01b882011-10-11 16:47:53 +11005623 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005624
NeilBrown43a70502009-12-14 12:49:55 +11005625 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005626 kobject_del(&mddev->kobj);
5627 kobject_put(&mddev->kobj);
5628}
5629
NeilBrown4ad23a972017-03-15 14:05:14 +11005630static void no_op(struct percpu_ref *r) {}
5631
NeilBrowna415c0f2017-06-05 16:05:13 +10005632int mddev_init_writes_pending(struct mddev *mddev)
5633{
5634 if (mddev->writes_pending.percpu_count_ptr)
5635 return 0;
Roman Gushchinddde2af2019-05-07 10:01:49 -07005636 if (percpu_ref_init(&mddev->writes_pending, no_op,
5637 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL) < 0)
NeilBrowna415c0f2017-06-05 16:05:13 +10005638 return -ENOMEM;
5639 /* We want to start with the refcount at zero */
5640 percpu_ref_put(&mddev->writes_pending);
5641 return 0;
5642}
5643EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5644
NeilBrownefeb53c2009-01-09 08:31:10 +11005645static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646{
NeilBrown039b7222017-04-12 16:26:13 +10005647 /*
5648 * If dev is zero, name is the name of a device to allocate with
5649 * an arbitrary minor number. It will be "md_???"
5650 * If dev is non-zero it must be a device number with a MAJOR of
5651 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5652 * the device is being created by opening a node in /dev.
5653 * If "name" is not NULL, the device is being created by
5654 * writing to /sys/module/md_mod/parameters/new_array.
5655 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005656 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005657 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005659 int partitioned;
5660 int shift;
5661 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005662 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663
5664 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005665 return -ENODEV;
5666
5667 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5668 shift = partitioned ? MdpMinorShift : 0;
5669 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670
Tejun Heoe804ac72010-10-15 15:36:08 +02005671 /* wait for any previous instance of this device to be
5672 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005673 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005674 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005675
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005676 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005677 error = -EEXIST;
5678 if (mddev->gendisk)
5679 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005680
NeilBrown039b7222017-04-12 16:26:13 +10005681 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005682 /* Need to ensure that 'name' is not a duplicate.
5683 */
NeilBrownfd01b882011-10-11 16:47:53 +11005684 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005685 spin_lock(&all_mddevs_lock);
5686
5687 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5688 if (mddev2->gendisk &&
5689 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5690 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005691 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005692 }
5693 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 }
NeilBrown039b7222017-04-12 16:26:13 +10005695 if (name && dev)
5696 /*
5697 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5698 */
5699 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005700
Artur Paszkiewicz41d2d842020-07-03 11:13:09 +02005701 error = mempool_init_kmalloc_pool(&mddev->md_io_pool, BIO_POOL_SIZE,
5702 sizeof(struct md_io));
5703 if (error)
5704 goto abort;
5705
NeilBrown0909dc42009-07-01 12:27:21 +10005706 error = -ENOMEM;
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02005707 mddev->queue = blk_alloc_queue(NUMA_NO_NODE);
NeilBrown0909dc42009-07-01 12:27:21 +10005708 if (!mddev->queue)
5709 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005710
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005711 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005712
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 disk = alloc_disk(1 << shift);
5714 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005715 blk_cleanup_queue(mddev->queue);
5716 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005717 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005719 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005721 if (name)
5722 strcpy(disk->disk_name, name);
5723 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005725 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 disk->fops = &md_fops;
5728 disk->private_data = mddev;
5729 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005730 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005731 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005732 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005733 * remove it now.
5734 */
5735 disk->flags |= GENHD_FL_EXT_DEVT;
Christoph Hellwiga564e232020-07-08 14:25:41 +02005736 disk->events |= DISK_EVENT_MEDIA_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005738 /* As soon as we call add_disk(), another thread could get
5739 * through to md_open, so make sure it doesn't get too far
5740 */
5741 mutex_lock(&mddev->open_mutex);
5742 add_disk(disk);
5743
Kent Overstreet28dec872018-06-07 20:52:54 -04005744 error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005745 if (error) {
5746 /* This isn't possible, but as kobject_init_and_add is marked
5747 * __must_check, we must do something with the result
5748 */
NeilBrown9d487392016-11-02 14:16:49 +11005749 pr_debug("md: cannot register %s/md - name in use\n",
5750 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005751 error = 0;
5752 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005753 if (mddev->kobj.sd &&
5754 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005755 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005756 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005757 abort:
5758 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005759 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005760 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005761 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
Junxiao Bie1a86db2020-07-14 16:10:26 -07005762 mddev->sysfs_level = sysfs_get_dirent_safe(mddev->kobj.sd, "level");
NeilBrownb62b7592008-10-21 13:25:21 +11005763 }
NeilBrownd3374822009-01-09 08:31:10 +11005764 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005765 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005766}
5767
5768static struct kobject *md_probe(dev_t dev, int *part, void *data)
5769{
NeilBrown78b63502017-04-12 16:26:13 +10005770 if (create_on_open)
5771 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 return NULL;
5773}
5774
Kees Cooke4dca7b2017-10-17 19:04:42 -07005775static int add_named_array(const char *val, const struct kernel_param *kp)
NeilBrownefeb53c2009-01-09 08:31:10 +11005776{
NeilBrown039b7222017-04-12 16:26:13 +10005777 /*
5778 * val must be "md_*" or "mdNNN".
5779 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005780 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005781 * For "mdNNN" we allocate an array with the minor number NNN
5782 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005783 */
5784 int len = strlen(val);
5785 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005786 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005787
5788 while (len && val[len-1] == '\n')
5789 len--;
5790 if (len >= DISK_NAME_LEN)
5791 return -E2BIG;
5792 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005793 if (strncmp(buf, "md_", 3) == 0)
5794 return md_alloc(0, buf);
5795 if (strncmp(buf, "md", 2) == 0 &&
5796 isdigit(buf[2]) &&
5797 kstrtoul(buf+2, 10, &devnum) == 0 &&
5798 devnum <= MINORMASK)
5799 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5800
5801 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005802}
5803
Kees Cook8376d3c2017-10-16 17:01:48 -07005804static void md_safemode_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805{
Kees Cook8376d3c2017-10-16 17:01:48 -07005806 struct mddev *mddev = from_timer(mddev, t, safemode_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807
NeilBrown4ad23a972017-03-15 14:05:14 +11005808 mddev->safemode = 1;
5809 if (mddev->external)
5810 sysfs_notify_dirent_safe(mddev->sysfs_state);
5811
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 md_wakeup_thread(mddev->thread);
5813}
5814
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005815static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816
NeilBrownfd01b882011-10-11 16:47:53 +11005817int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818{
NeilBrown2604b702006-01-06 00:20:36 -08005819 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005820 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005821 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822
NeilBrowna757e642005-04-16 15:26:42 -07005823 if (list_empty(&mddev->disks))
5824 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
5827 if (mddev->pers)
5828 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005829 /* Cannot run until previous stop completes properly */
5830 if (mddev->sysfs_active)
5831 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005832
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 /*
5834 * Analyze all RAID superblock(s)
5835 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005836 if (!mddev->raid_disks) {
5837 if (!mddev->persistent)
5838 return -EINVAL;
Yufen Yu6a5cb532019-10-16 16:00:03 +08005839 err = analyze_sbs(mddev);
5840 if (err)
5841 return -EINVAL;
NeilBrown1ec4a932008-02-06 01:39:53 -08005842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
NeilBrownd9d166c2006-01-06 00:20:51 -08005844 if (mddev->level != LEVEL_NONE)
5845 request_module("md-level-%d", mddev->level);
5846 else if (mddev->clevel[0])
5847 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848
5849 /*
5850 * Drop all container device buffers, from now on
5851 * the only valid external interface is through the md
5852 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 */
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005854 mddev->has_superblocks = false;
NeilBrowndafb20f2012-03-19 12:46:39 +11005855 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005856 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 continue;
5858 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005859 invalidate_bdev(rdev->bdev);
NeilBrown97b20ef2017-04-13 08:53:48 +10005860 if (mddev->ro != 1 &&
5861 (bdev_read_only(rdev->bdev) ||
5862 bdev_read_only(rdev->meta_bdev))) {
5863 mddev->ro = 1;
5864 if (mddev->gendisk)
5865 set_disk_ro(mddev->gendisk, 1);
5866 }
NeilBrownf0d76d72007-07-17 04:06:12 -07005867
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005868 if (rdev->sb_page)
5869 mddev->has_superblocks = true;
5870
NeilBrownf0d76d72007-07-17 04:06:12 -07005871 /* perform some consistency tests on the device.
5872 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005873 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005874 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005875 if (rdev->meta_bdev) {
5876 /* Nothing to check */;
5877 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005878 if (mddev->dev_sectors &&
5879 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005880 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005881 pr_warn("md: %s: data overlaps metadata\n",
5882 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005883 return -EINVAL;
5884 }
5885 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005886 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005887 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005888 pr_warn("md: %s: metadata overlaps data\n",
5889 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005890 return -EINVAL;
5891 }
5892 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005893 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 }
5895
Kent Overstreetafeee512018-05-20 18:25:52 -04005896 if (!bioset_initialized(&mddev->bio_set)) {
5897 err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5898 if (err)
5899 return err;
Ming Lei10273172017-02-14 23:29:00 +08005900 }
Kent Overstreetafeee512018-05-20 18:25:52 -04005901 if (!bioset_initialized(&mddev->sync_set)) {
5902 err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5903 if (err)
Kent Overstreet28dec872018-06-07 20:52:54 -04005904 return err;
NeilBrown5a850712017-06-21 09:12:21 +10005905 }
NeilBrowna167f662010-10-26 18:31:13 +11005906
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005908 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005909 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005911 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005912 pr_warn("md: personality for level %d is not loaded!\n",
5913 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005914 else
NeilBrown9d487392016-11-02 14:16:49 +11005915 pr_warn("md: personality for level %s is not loaded!\n",
5916 mddev->clevel);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005917 err = -EINVAL;
5918 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005921 if (mddev->level != pers->level) {
5922 mddev->level = pers->level;
5923 mddev->new_level = pers->level;
5924 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005925 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
NeilBrownf6705572006-03-27 01:18:11 -08005927 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005928 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005929 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005930 module_put(pers->owner);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005931 err = -EINVAL;
5932 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08005933 }
5934
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005935 if (pers->sync_request) {
5936 /* Warn if this is a potentially silly
5937 * configuration.
5938 */
5939 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005940 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005941 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005942
NeilBrowndafb20f2012-03-19 12:46:39 +11005943 rdev_for_each(rdev, mddev)
5944 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005945 if (rdev < rdev2 &&
Christoph Hellwig61a27e1f2020-09-03 07:40:58 +02005946 rdev->bdev->bd_disk ==
5947 rdev2->bdev->bd_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11005948 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5949 mdname(mddev),
5950 bdevname(rdev->bdev,b),
5951 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005952 warned = 1;
5953 }
5954 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005955
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005956 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005957 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005958 }
5959
NeilBrown657390d2005-08-26 18:34:16 -07005960 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005961 /* may be over-ridden by personality */
5962 mddev->resync_max_sectors = mddev->dev_sectors;
5963
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005964 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965
NeilBrown0f9552b52009-12-30 12:08:50 +11005966 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005967 mddev->ro = 2; /* read-only, but switch on first write */
5968
NeilBrown36d091f2014-12-15 12:56:58 +11005969 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005970 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005971 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005972 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005973 WARN_ONCE(!mddev->external_size,
5974 "%s: default size too small, but 'external_size' not in effect?\n",
5975 __func__);
5976 pr_warn("md: invalid array_size %llu > default size %llu\n",
5977 (unsigned long long)mddev->array_sectors / 2,
5978 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005979 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005980 }
NeilBrown36d091f2014-12-15 12:56:58 +11005981 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005982 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005983 struct bitmap *bitmap;
5984
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005985 bitmap = md_bitmap_create(mddev, -1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005986 if (IS_ERR(bitmap)) {
5987 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005988 pr_warn("%s: failed to create bitmap (%d)\n",
5989 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005990 } else
5991 mddev->bitmap = bitmap;
5992
NeilBrownb15c2e52006-01-06 00:20:16 -08005993 }
Guoqing Jiangd4945492019-06-14 17:10:39 +08005994 if (err)
5995 goto bitmap_abort;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005996
5997 if (mddev->bitmap_info.max_write_behind > 0) {
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01005998 bool create_pool = false;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005999
6000 rdev_for_each(rdev, mddev) {
6001 if (test_bit(WriteMostly, &rdev->flags) &&
Guoqing Jiang404659c2019-12-23 10:48:53 +01006002 rdev_init_serial(rdev))
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01006003 create_pool = true;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08006004 }
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01006005 if (create_pool && mddev->serial_info_pool == NULL) {
Guoqing Jiang404659c2019-12-23 10:48:53 +01006006 mddev->serial_info_pool =
6007 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
6008 sizeof(struct serial_info));
6009 if (!mddev->serial_info_pool) {
Guoqing Jiang3e148a32019-06-19 17:30:46 +08006010 err = -ENOMEM;
Guoqing Jiangd4945492019-06-14 17:10:39 +08006011 goto bitmap_abort;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08006012 }
6013 }
6014 }
6015
NeilBrown5c675f82014-12-15 12:56:56 +11006016 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07006017 bool nonrot = true;
6018
6019 rdev_for_each(rdev, mddev) {
6020 if (rdev->raid_disk >= 0 &&
6021 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
6022 nonrot = false;
6023 break;
6024 }
6025 }
6026 if (mddev->degraded)
6027 nonrot = false;
6028 if (nonrot)
Bart Van Assche8b904b52018-03-07 17:10:10 -08006029 blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue);
Shaohua Libb086a82016-09-30 09:45:40 -07006030 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08006031 blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue);
NeilBrown5c675f82014-12-15 12:56:56 +11006032 }
NeilBrown36d091f2014-12-15 12:56:58 +11006033 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10006034 if (mddev->kobj.sd &&
6035 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11006036 pr_warn("md: cannot register extra attributes for %s\n",
6037 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10006038 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
Junxiao Bie8efa9b2020-08-04 17:27:18 -07006039 mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed");
6040 mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded");
NeilBrown5e55e2f2007-03-26 21:32:14 -08006041 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08006042 mddev->ro = 0;
6043
Robert Becker1e509152009-12-14 12:49:58 +11006044 atomic_set(&mddev->max_corr_read_errors,
6045 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006047 if (mddev_is_clustered(mddev))
6048 mddev->safemode_delay = 0;
6049 else
Zhao Heming7c9d5c52020-07-21 02:08:52 +08006050 mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11006052 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11006053 spin_lock(&mddev->lock);
6054 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11006055 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11006056 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10006057 if (rdev->raid_disk >= 0)
Yufen Yue5b521e2019-06-14 15:41:07 -07006058 sysfs_link_rdev(mddev, rdev); /* failure here is OK */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006059
NeilBrowna4a3d262015-07-17 11:57:30 +10006060 if (mddev->degraded && !mddev->ro)
6061 /* This ensures that recovering status is reported immediately
6062 * via sysfs - until a lack of spares is confirmed.
6063 */
6064 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10006066
Shaohua Li29530792016-12-08 15:48:19 -08006067 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07006068 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069
NeilBrownd7603b72006-01-06 00:20:30 -08006070 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 return 0;
Xiao Nib1261942018-01-24 12:17:38 +08006072
Guoqing Jiangd4945492019-06-14 17:10:39 +08006073bitmap_abort:
6074 mddev_detach(mddev);
6075 if (mddev->private)
6076 pers->free(mddev, mddev->private);
6077 mddev->private = NULL;
6078 module_put(pers->owner);
6079 md_bitmap_destroy(mddev);
Xiao Nib1261942018-01-24 12:17:38 +08006080abort:
NeilBrown4bc034d2019-03-29 10:46:16 -07006081 bioset_exit(&mddev->bio_set);
6082 bioset_exit(&mddev->sync_set);
Xiao Nib1261942018-01-24 12:17:38 +08006083 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084}
NeilBrown390ee602010-06-01 19:37:27 +10006085EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
Christoph Hellwig7e0adbf2020-06-07 17:31:19 +02006087int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11006088{
6089 int err;
6090
NeilBrown9d4b45d2019-08-20 10:21:09 +10006091 set_bit(MD_NOT_READY, &mddev->flags);
NeilBrownfe60b012010-03-29 11:10:42 +11006092 err = md_run(mddev);
6093 if (err)
6094 goto out;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006095 err = md_bitmap_load(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006096 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006097 md_bitmap_destroy(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006098 goto out;
6099 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05006100
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006101 if (mddev_is_clustered(mddev))
6102 md_allow_write(mddev);
6103
Song Liud5d885f2017-11-19 22:17:01 -08006104 /* run start up tasks that require md_thread */
6105 md_start(mddev);
6106
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05006107 md_wakeup_thread(mddev->thread);
6108 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
6109
NeilBrownfe60b012010-03-29 11:10:42 +11006110 set_capacity(mddev->gendisk, mddev->array_sectors);
Christoph Hellwig659e56b2020-09-01 17:57:43 +02006111 revalidate_disk_size(mddev->gendisk, true);
NeilBrown9d4b45d2019-08-20 10:21:09 +10006112 clear_bit(MD_NOT_READY, &mddev->flags);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006113 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11006114 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrown9d4b45d2019-08-20 10:21:09 +10006115 sysfs_notify_dirent_safe(mddev->sysfs_state);
6116 sysfs_notify_dirent_safe(mddev->sysfs_action);
Junxiao Bie1a86db2020-07-14 16:10:26 -07006117 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
NeilBrownfe60b012010-03-29 11:10:42 +11006118out:
NeilBrown9d4b45d2019-08-20 10:21:09 +10006119 clear_bit(MD_NOT_READY, &mddev->flags);
NeilBrownfe60b012010-03-29 11:10:42 +11006120 return err;
6121}
6122
Song Liud5d885f2017-11-19 22:17:01 -08006123int md_start(struct mddev *mddev)
6124{
6125 int ret = 0;
6126
6127 if (mddev->pers->start) {
6128 set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6129 md_wakeup_thread(mddev->thread);
6130 ret = mddev->pers->start(mddev);
6131 clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6132 md_wakeup_thread(mddev->sync_thread);
6133 }
6134 return ret;
6135}
6136EXPORT_SYMBOL_GPL(md_start);
6137
NeilBrownfd01b882011-10-11 16:47:53 +11006138static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139{
6140 struct gendisk *disk = mddev->gendisk;
NeilBrown97b20ef2017-04-13 08:53:48 +10006141 struct md_rdev *rdev;
6142 bool has_journal = false;
6143 bool has_readonly = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144
Andre Noll80fab1d2008-07-11 22:02:21 +10006145 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10006147 return -ENXIO;
6148 if (!mddev->pers)
6149 return -EINVAL;
6150 if (!mddev->ro)
6151 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07006152
NeilBrown97b20ef2017-04-13 08:53:48 +10006153 rcu_read_lock();
6154 rdev_for_each_rcu(rdev, mddev) {
6155 if (test_bit(Journal, &rdev->flags) &&
6156 !test_bit(Faulty, &rdev->flags))
6157 has_journal = true;
6158 if (bdev_read_only(rdev->bdev))
6159 has_readonly = true;
Song Liu339421d2015-10-08 21:54:13 -07006160 }
NeilBrown97b20ef2017-04-13 08:53:48 +10006161 rcu_read_unlock();
6162 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
6163 /* Don't restart rw with journal missing/faulty */
6164 return -EINVAL;
6165 if (has_readonly)
6166 return -EROFS;
Song Liu339421d2015-10-08 21:54:13 -07006167
Andre Noll80fab1d2008-07-11 22:02:21 +10006168 mddev->safemode = 0;
6169 mddev->ro = 0;
6170 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11006171 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10006172 /* Kick recovery or resync if necessary */
6173 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6174 md_wakeup_thread(mddev->thread);
6175 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006176 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10006177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178}
6179
NeilBrownfd01b882011-10-11 16:47:53 +11006180static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11006181{
6182 mddev->array_sectors = 0;
6183 mddev->external_size = 0;
6184 mddev->dev_sectors = 0;
6185 mddev->raid_disks = 0;
6186 mddev->recovery_cp = 0;
6187 mddev->resync_min = 0;
6188 mddev->resync_max = MaxSector;
6189 mddev->reshape_position = MaxSector;
6190 mddev->external = 0;
6191 mddev->persistent = 0;
6192 mddev->level = LEVEL_NONE;
6193 mddev->clevel[0] = 0;
6194 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006195 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006196 mddev->ro = 0;
6197 mddev->metadata_type[0] = 0;
6198 mddev->chunk_sectors = 0;
6199 mddev->ctime = mddev->utime = 0;
6200 mddev->layout = 0;
6201 mddev->max_disks = 0;
6202 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10006203 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006204 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006205 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006206 mddev->new_level = LEVEL_NONE;
6207 mddev->new_layout = 0;
6208 mddev->new_chunk_sectors = 0;
6209 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11006210 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11006211 mddev->suspend_lo = mddev->suspend_hi = 0;
6212 mddev->sync_speed_min = mddev->sync_speed_max = 0;
6213 mddev->recovery = 0;
6214 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006215 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006216 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006217 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10006218 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08006219 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11006220 mddev->bitmap_info.offset = 0;
6221 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10006222 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006223 mddev->bitmap_info.chunksize = 0;
6224 mddev->bitmap_info.daemon_sleep = 0;
6225 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08006226 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006227}
6228
NeilBrownfd01b882011-10-11 16:47:53 +11006229static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11006230{
NeilBrown6b6204e2013-05-09 09:48:30 +10006231 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Guoqing Jiang21e09582020-04-04 23:57:07 +02006232 if (work_pending(&mddev->del_work))
6233 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11006234 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11006235 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10006236 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11006237 }
6238
6239 del_timer_sync(&mddev->safemode_timer);
6240
Shaohua Li034e33f2016-11-21 10:29:19 -08006241 if (mddev->pers && mddev->pers->quiesce) {
6242 mddev->pers->quiesce(mddev, 1);
6243 mddev->pers->quiesce(mddev, 0);
6244 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006245 md_bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11006246
NeilBrownb6d428c2013-04-24 11:42:42 +10006247 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006248 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08006249 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11006250 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006251 if (!mddev_is_clustered(mddev))
6252 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11006253 md_update_sb(mddev, 1);
6254 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +01006255 /* disable policy to guarantee rdevs free resources for serialization */
6256 mddev->serialize_policy = 0;
6257 mddev_destroy_serial_pool(mddev, NULL, true);
NeilBrowna047e122010-03-29 12:07:53 +11006258}
NeilBrowndefad612011-01-14 09:14:33 +11006259
NeilBrownfd01b882011-10-11 16:47:53 +11006260void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11006261{
NeilBrown29f097c2013-11-14 17:54:51 +11006262 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11006263 __md_stop_writes(mddev);
6264 mddev_unlock(mddev);
6265}
NeilBrown390ee602010-06-01 19:37:27 +10006266EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11006267
NeilBrown5aa61f42014-12-15 12:56:57 +11006268static void mddev_detach(struct mddev *mddev)
6269{
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006270 md_bitmap_wait_behind_writes(mddev);
Guoqing Jiang6b40bec2020-02-11 11:10:04 +01006271 if (mddev->pers && mddev->pers->quiesce && !mddev->suspended) {
NeilBrown5aa61f42014-12-15 12:56:57 +11006272 mddev->pers->quiesce(mddev, 1);
6273 mddev->pers->quiesce(mddev, 0);
6274 }
6275 md_unregister_thread(&mddev->thread);
6276 if (mddev->queue)
6277 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
6278}
6279
NeilBrown5eff3c42012-11-19 10:47:48 +11006280static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11006281{
NeilBrown36d091f2014-12-15 12:56:58 +11006282 struct md_personality *pers = mddev->pers;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006283 md_bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11006284 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10006285 /* Ensure ->event_work is done */
Guoqing Jiang21e09582020-04-04 23:57:07 +02006286 if (mddev->event_work.func)
6287 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11006288 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11006289 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11006290 spin_unlock(&mddev->lock);
6291 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10006292 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11006293 if (pers->sync_request && mddev->to_remove == NULL)
6294 mddev->to_remove = &md_redundancy_group;
6295 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11006296 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Jack Wang6aaa58c2018-10-19 16:21:31 +02006297}
6298
6299void md_stop(struct mddev *mddev)
6300{
6301 /* stop the array and free an attached data structures.
6302 * This is called from dm-raid
6303 */
6304 __md_stop(mddev);
Kent Overstreetafeee512018-05-20 18:25:52 -04006305 bioset_exit(&mddev->bio_set);
6306 bioset_exit(&mddev->sync_set);
NeilBrown5eff3c42012-11-19 10:47:48 +11006307}
6308
NeilBrown390ee602010-06-01 19:37:27 +10006309EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11006310
NeilBrowna05b7ea2012-07-19 15:59:18 +10006311static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11006312{
6313 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11006314 int did_freeze = 0;
6315
6316 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6317 did_freeze = 1;
6318 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6319 md_wakeup_thread(mddev->thread);
6320 }
NeilBrownf851b602014-12-11 10:02:10 +11006321 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11006322 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006323 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11006324 /* Thread might be blocked waiting for metadata update
6325 * which will now never happen */
6326 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11006327
Shaohua Li29530792016-12-08 15:48:19 -08006328 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10006329 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11006330 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006331 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
6332 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10006333 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08006334 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11006335 mddev_lock_nointr(mddev);
6336
NeilBrowna4bd82d2010-03-29 13:23:10 +11006337 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006338 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006339 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006340 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006341 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11006342 if (did_freeze) {
6343 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006344 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006345 md_wakeup_thread(mddev->thread);
6346 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11006347 err = -EBUSY;
6348 goto out;
6349 }
6350 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11006351 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11006352
6353 err = -ENXIO;
6354 if (mddev->ro==1)
6355 goto out;
6356 mddev->ro = 1;
6357 set_disk_ro(mddev->gendisk, 1);
6358 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006359 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6360 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006361 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11006362 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006363 }
6364out:
6365 mutex_unlock(&mddev->open_mutex);
6366 return err;
6367}
6368
NeilBrown9e653b62006-06-26 00:27:58 -07006369/* mode:
6370 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07006371 * 2 - stop but do not disassemble array
6372 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006373static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10006374 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11006377 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11006378 int did_freeze = 0;
6379
6380 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6381 did_freeze = 1;
6382 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6383 md_wakeup_thread(mddev->thread);
6384 }
NeilBrownf851b602014-12-11 10:02:10 +11006385 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11006386 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006387 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11006388 /* Thread might be blocked waiting for metadata update
6389 * which will now never happen */
6390 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11006391
NeilBrown30b8feb2013-11-14 15:16:17 +11006392 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006393 wait_event(resync_wait, (mddev->sync_thread == NULL &&
6394 !test_bit(MD_RECOVERY_RUNNING,
6395 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11006396 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397
NeilBrownc8c00a62009-08-10 12:50:52 +10006398 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006399 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006400 mddev->sysfs_active ||
6401 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006402 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006403 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10006404 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11006405 if (did_freeze) {
6406 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006407 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006408 md_wakeup_thread(mddev->thread);
6409 }
NeilBrown260fa032013-08-27 16:44:13 +10006410 return -EBUSY;
6411 }
NeilBrown6e17b022010-08-07 21:41:19 +10006412 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11006413 if (mddev->ro)
6414 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11006415
NeilBrowndefad612011-01-14 09:14:33 +11006416 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11006417 __md_stop(mddev);
NeilBrown6177b472010-03-29 11:37:13 +11006418
NeilBrowna4bd82d2010-03-29 13:23:10 +11006419 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10006420 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08006421
NeilBrowndafb20f2012-03-19 12:46:39 +11006422 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10006423 if (rdev->raid_disk >= 0)
6424 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10006425
NeilBrowna4bd82d2010-03-29 13:23:10 +11006426 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10006427 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006428 mddev->changed = 1;
Christoph Hellwig659e56b2020-09-01 17:57:43 +02006429 revalidate_disk_size(disk, true);
NeilBrown0d4ca602006-12-10 02:20:44 -08006430
NeilBrowna4bd82d2010-03-29 13:23:10 +11006431 if (mddev->ro)
6432 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10006433 } else
6434 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 /*
6436 * Free resources if final stop
6437 */
NeilBrown9e653b62006-06-26 00:27:58 -07006438 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006439 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440
NeilBrownc3d97142009-12-14 12:49:52 +11006441 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11006442 struct file *f = mddev->bitmap_info.file;
6443 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11006444 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11006445 spin_unlock(&mddev->lock);
6446 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08006447 }
NeilBrownc3d97142009-12-14 12:49:52 +11006448 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08006449
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 export_array(mddev);
6451
NeilBrown6177b472010-03-29 11:37:13 +11006452 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11006453 if (mddev->hold_active == UNTIL_STOP)
6454 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006455 }
NeilBrownd7603b72006-01-06 00:20:30 -08006456 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006457 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10006458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459}
6460
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006461#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11006462static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463{
NeilBrown3cb03002011-10-11 16:45:26 +11006464 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 int err;
6466
NeilBrowna757e642005-04-16 15:26:42 -07006467 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469
NeilBrown9d487392016-11-02 14:16:49 +11006470 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471
NeilBrowndafb20f2012-03-19 12:46:39 +11006472 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11006474 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 }
NeilBrown9d487392016-11-02 14:16:49 +11006476 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477
NeilBrownd710e132008-10-13 11:55:12 +11006478 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006480 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10006481 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 }
6483}
6484
6485/*
6486 * lets try to run arrays based on all disks that have arrived
6487 * until now. (those are in pending_raid_disks)
6488 *
6489 * the method: pick the first pending disk, collect all disks with
6490 * the same UUID, remove all from the pending list and put them into
6491 * the 'same_array' list. Then order this list based on superblock
6492 * update time (freshest comes first), kick out 'old' disks and
6493 * compare superblocks. If everything's fine then run it.
6494 *
6495 * If "unit" is allocated, then bump its reference count
6496 */
6497static void autorun_devices(int part)
6498{
NeilBrown3cb03002011-10-11 16:45:26 +11006499 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006500 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501 char b[BDEVNAME_SIZE];
6502
NeilBrown9d487392016-11-02 14:16:49 +11006503 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07006505 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08006507 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11006509 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510
NeilBrown9d487392016-11-02 14:16:49 +11006511 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006513 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006515 pr_debug("md: adding %s ...\n",
6516 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 list_move(&rdev->same_set, &candidates);
6518 }
6519 /*
6520 * now we have a set of devices, with all of them having
6521 * mostly sane superblocks. It's time to allocate the
6522 * mddev.
6523 */
NeilBrowne8703fe2006-10-03 01:15:59 -07006524 if (part) {
6525 dev = MKDEV(mdp_major,
6526 rdev0->preferred_minor << MdpMinorShift);
6527 unit = MINOR(dev) >> MdpMinorShift;
6528 } else {
6529 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6530 unit = MINOR(dev);
6531 }
6532 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11006533 pr_warn("md: unit number in %s is bad: %d\n",
6534 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 break;
6536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537
6538 md_probe(dev, NULL, NULL);
6539 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006540 if (!mddev || !mddev->gendisk) {
6541 if (mddev)
6542 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543 break;
6544 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006545 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006546 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 else if (mddev->raid_disks || mddev->major_version
6548 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006549 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 mdname(mddev), bdevname(rdev0->bdev,b));
6551 mddev_unlock(mddev);
6552 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006553 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006554 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006555 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 list_del_init(&rdev->same_set);
6557 if (bind_rdev_to_array(rdev, mddev))
6558 export_rdev(rdev);
6559 }
6560 autorun_array(mddev);
6561 mddev_unlock(mddev);
6562 }
6563 /* on success, candidates will be empty, on error
6564 * it won't...
6565 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006566 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006567 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 mddev_put(mddev);
6571 }
NeilBrown9d487392016-11-02 14:16:49 +11006572 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006574#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575
NeilBrownf72ffdd2014-09-30 14:23:59 +10006576static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577{
6578 mdu_version_t ver;
6579
6580 ver.major = MD_MAJOR_VERSION;
6581 ver.minor = MD_MINOR_VERSION;
6582 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6583
6584 if (copy_to_user(arg, &ver, sizeof(ver)))
6585 return -EFAULT;
6586
6587 return 0;
6588}
6589
NeilBrownf72ffdd2014-09-30 14:23:59 +10006590static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591{
6592 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006593 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006594 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595
NeilBrown1ca69c42012-10-11 13:37:33 +11006596 nr = working = insync = failed = spare = 0;
6597 rcu_read_lock();
6598 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006600 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 failed++;
6602 else {
6603 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006604 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006605 insync++;
Song Liub347af82016-08-11 17:14:45 -07006606 else if (test_bit(Journal, &rdev->flags))
6607 /* TODO: add journal count to md_u.h */
6608 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 else
6610 spare++;
6611 }
6612 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006613 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614
6615 info.major_version = mddev->major_version;
6616 info.minor_version = mddev->minor_version;
6617 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006618 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006620 info.size = mddev->dev_sectors / 2;
6621 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006622 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 info.nr_disks = nr;
6624 info.raid_disks = mddev->raid_disks;
6625 info.md_minor = mddev->md_minor;
6626 info.not_persistent= !mddev->persistent;
6627
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006628 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629 info.state = 0;
6630 if (mddev->in_sync)
6631 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006632 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006633 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006634 if (mddev_is_clustered(mddev))
6635 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006636 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 info.working_disks = working;
6638 info.failed_disks = failed;
6639 info.spare_disks = spare;
6640
6641 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006642 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643
6644 if (copy_to_user(arg, &info, sizeof(info)))
6645 return -EFAULT;
6646
6647 return 0;
6648}
6649
NeilBrownf72ffdd2014-09-30 14:23:59 +10006650static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006651{
6652 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006653 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006654 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006655
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006656 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006657 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006658 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006659
NeilBrown32a76272005-06-21 17:17:14 -07006660 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006661 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006662 /* bitmap enabled */
6663 if (mddev->bitmap_info.file) {
6664 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6665 sizeof(file->pathname));
6666 if (IS_ERR(ptr))
6667 err = PTR_ERR(ptr);
6668 else
6669 memmove(file->pathname, ptr,
6670 sizeof(file->pathname)-(ptr-file->pathname));
6671 }
NeilBrown4af1a042014-12-15 12:57:00 +11006672 spin_unlock(&mddev->lock);
6673
6674 if (err == 0 &&
6675 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006676 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006677
NeilBrown32a76272005-06-21 17:17:14 -07006678 kfree(file);
6679 return err;
6680}
6681
NeilBrownf72ffdd2014-09-30 14:23:59 +10006682static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683{
6684 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006685 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686
6687 if (copy_from_user(&info, arg, sizeof(info)))
6688 return -EFAULT;
6689
NeilBrown1ca69c42012-10-11 13:37:33 +11006690 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006691 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692 if (rdev) {
6693 info.major = MAJOR(rdev->bdev->bd_dev);
6694 info.minor = MINOR(rdev->bdev->bd_dev);
6695 info.raid_disk = rdev->raid_disk;
6696 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006697 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006699 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700 info.state |= (1<<MD_DISK_ACTIVE);
6701 info.state |= (1<<MD_DISK_SYNC);
6702 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006703 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006704 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006705 if (test_bit(WriteMostly, &rdev->flags))
6706 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006707 if (test_bit(FailFast, &rdev->flags))
6708 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 } else {
6710 info.major = info.minor = 0;
6711 info.raid_disk = -1;
6712 info.state = (1<<MD_DISK_REMOVED);
6713 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006714 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715
6716 if (copy_to_user(arg, &info, sizeof(info)))
6717 return -EFAULT;
6718
6719 return 0;
6720}
6721
Christoph Hellwig7e0adbf2020-06-07 17:31:19 +02006722int md_add_new_disk(struct mddev *mddev, struct mdu_disk_info_s *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723{
6724 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006725 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 dev_t dev = MKDEV(info->major,info->minor);
6727
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006728 if (mddev_is_clustered(mddev) &&
6729 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006730 pr_warn("%s: Cannot add to clustered mddev.\n",
6731 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006732 return -EINVAL;
6733 }
6734
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6736 return -EOVERFLOW;
6737
6738 if (!mddev->raid_disks) {
6739 int err;
6740 /* expecting a device which has a superblock */
6741 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6742 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006743 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 PTR_ERR(rdev));
6745 return PTR_ERR(rdev);
6746 }
6747 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006748 struct md_rdev *rdev0
6749 = list_entry(mddev->disks.next,
6750 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006751 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 .load_super(rdev, rdev0, mddev->minor_version);
6753 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006754 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006755 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 bdevname(rdev0->bdev,b2));
6757 export_rdev(rdev);
6758 return -EINVAL;
6759 }
6760 }
6761 err = bind_rdev_to_array(rdev, mddev);
6762 if (err)
6763 export_rdev(rdev);
6764 return err;
6765 }
6766
6767 /*
Christoph Hellwig7e0adbf2020-06-07 17:31:19 +02006768 * md_add_new_disk can be used once the array is assembled
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 * to add "hot spares". They must already have a superblock
6770 * written
6771 */
6772 if (mddev->pers) {
6773 int err;
6774 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006775 pr_warn("%s: personality does not support diskops!\n",
6776 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 return -EINVAL;
6778 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006779 if (mddev->persistent)
6780 rdev = md_import_device(dev, mddev->major_version,
6781 mddev->minor_version);
6782 else
6783 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006785 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 PTR_ERR(rdev));
6787 return PTR_ERR(rdev);
6788 }
NeilBrown1a855a02010-12-09 16:36:28 +11006789 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006790 if (!mddev->persistent) {
6791 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006792 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006793 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006794 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006795 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006796 } else
NeilBrown41158c72005-06-21 17:17:25 -07006797 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006798 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006799 } else
6800 super_types[mddev->major_version].
6801 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006802 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006803 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006804 /* This was a hot-add request, but events doesn't
6805 * match, so reject it.
6806 */
6807 export_rdev(rdev);
6808 return -EINVAL;
6809 }
6810
NeilBrownb2d444d2005-11-08 21:39:31 -08006811 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006812 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6813 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006814 else
6815 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006816 if (info->state & (1<<MD_DISK_FAILFAST))
6817 set_bit(FailFast, &rdev->flags);
6818 else
6819 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006820
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006821 if (info->state & (1<<MD_DISK_JOURNAL)) {
6822 struct md_rdev *rdev2;
6823 bool has_journal = false;
6824
6825 /* make sure no existing journal disk */
6826 rdev_for_each(rdev2, mddev) {
6827 if (test_bit(Journal, &rdev2->flags)) {
6828 has_journal = true;
6829 break;
6830 }
6831 }
NeilBrown230b55f2017-10-17 14:24:09 +11006832 if (has_journal || mddev->bitmap) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006833 export_rdev(rdev);
6834 return -EBUSY;
6835 }
Song Liubac624f2015-08-13 14:31:55 -07006836 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006837 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006838 /*
6839 * check whether the device shows up in other nodes
6840 */
6841 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006842 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006843 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006844 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006845 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006846 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006847 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006848 export_rdev(rdev);
6849 return err;
6850 }
6851 }
6852 }
6853
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 rdev->raid_disk = -1;
6855 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006856
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857 if (err)
6858 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006859
6860 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006861 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6862 if (!err) {
6863 err = md_cluster_ops->new_disk_ack(mddev,
6864 err == 0);
6865 if (err)
6866 md_kick_rdev_from_array(rdev);
6867 }
6868 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006869 if (err)
6870 md_cluster_ops->add_new_disk_cancel(mddev);
6871 else
6872 err = add_bound_rdev(rdev);
6873 }
6874
6875 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006876 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006877
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 return err;
6879 }
6880
Christoph Hellwig7e0adbf2020-06-07 17:31:19 +02006881 /* otherwise, md_add_new_disk is only allowed
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 * for major_version==0 superblocks
6883 */
6884 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006885 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886 return -EINVAL;
6887 }
6888
6889 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6890 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006891 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006893 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 PTR_ERR(rdev));
6895 return PTR_ERR(rdev);
6896 }
6897 rdev->desc_nr = info->number;
6898 if (info->raid_disk < mddev->raid_disks)
6899 rdev->raid_disk = info->raid_disk;
6900 else
6901 rdev->raid_disk = -1;
6902
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006904 if (info->state & (1<<MD_DISK_SYNC))
6905 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006907 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6908 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006909 if (info->state & (1<<MD_DISK_FAILFAST))
6910 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006911
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006913 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006914 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6915 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006916 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006917 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918
NeilBrown2bf071b2006-01-06 00:20:55 -08006919 err = bind_rdev_to_array(rdev, mddev);
6920 if (err) {
6921 export_rdev(rdev);
6922 return err;
6923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924 }
6925
6926 return 0;
6927}
6928
NeilBrownf72ffdd2014-09-30 14:23:59 +10006929static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930{
6931 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006932 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933
Yufen Yuc42a0e22018-05-04 18:08:10 +08006934 if (!mddev->pers)
6935 return -ENODEV;
6936
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937 rdev = find_rdev(mddev, dev);
6938 if (!rdev)
6939 return -ENXIO;
6940
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006941 if (rdev->raid_disk < 0)
6942 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006943
NeilBrown3ea8929d2013-04-24 11:42:41 +10006944 clear_bit(Blocked, &rdev->flags);
6945 remove_and_add_spares(mddev, rdev);
6946
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 if (rdev->raid_disk >= 0)
6948 goto busy;
6949
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006950kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006951 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006952 md_cluster_ops->remove_disk(mddev, rdev);
6953
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006954 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006955 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006956 if (mddev->thread)
6957 md_wakeup_thread(mddev->thread);
6958 else
6959 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006960 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961
6962 return 0;
6963busy:
NeilBrown9d487392016-11-02 14:16:49 +11006964 pr_debug("md: cannot remove active disk %s from %s ...\n",
6965 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966 return -EBUSY;
6967}
6968
NeilBrownf72ffdd2014-09-30 14:23:59 +10006969static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970{
6971 char b[BDEVNAME_SIZE];
6972 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006973 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974
6975 if (!mddev->pers)
6976 return -ENODEV;
6977
6978 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006979 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 mdname(mddev));
6981 return -EINVAL;
6982 }
6983 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006984 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985 mdname(mddev));
6986 return -EINVAL;
6987 }
6988
NeilBrownd710e132008-10-13 11:55:12 +11006989 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006991 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992 PTR_ERR(rdev));
6993 return -EINVAL;
6994 }
6995
6996 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006997 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006999 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000
NeilBrown8190e752009-06-18 08:48:58 +10007001 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002
NeilBrownb2d444d2005-11-08 21:39:31 -08007003 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11007004 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 bdevname(rdev->bdev,b), mdname(mddev));
7006 err = -EINVAL;
7007 goto abort_export;
7008 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007009
NeilBrownb2d444d2005-11-08 21:39:31 -08007010 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07007012 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08007013 err = bind_rdev_to_array(rdev, mddev);
7014 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05007015 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016
7017 /*
7018 * The rest should better be atomic, we can have disk failures
7019 * noticed in interrupt contexts ...
7020 */
7021
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022 rdev->raid_disk = -1;
7023
Shaohua Li29530792016-12-08 15:48:19 -08007024 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11007025 if (!mddev->thread)
7026 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027 /*
7028 * Kick recovery, maybe this spare has to be added to the
7029 * array immediately.
7030 */
7031 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7032 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08007033 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 return 0;
7035
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036abort_export:
7037 export_rdev(rdev);
7038 return err;
7039}
7040
NeilBrownfd01b882011-10-11 16:47:53 +11007041static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07007042{
NeilBrown035328c2014-04-09 12:25:40 +10007043 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07007044
NeilBrown36fa3062005-09-09 16:23:45 -07007045 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10007046 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07007047 return -EBUSY;
7048 if (mddev->recovery || mddev->sync_thread)
7049 return -EBUSY;
7050 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07007051 }
7052
NeilBrown36fa3062005-09-09 16:23:45 -07007053 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10007054 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11007055 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07007056
NeilBrown1e594bb2014-12-15 12:57:00 +11007057 if (mddev->bitmap || mddev->bitmap_info.file)
7058 return -EEXIST; /* cannot add when bitmap is present */
7059 f = fget(fd);
7060
7061 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11007062 pr_warn("%s: error: failed to get bitmap file\n",
7063 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07007064 return -EBADF;
7065 }
7066
NeilBrown1e594bb2014-12-15 12:57:00 +11007067 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10007068 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11007069 pr_warn("%s: error: bitmap file must be a regular file\n",
7070 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10007071 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11007072 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11007073 pr_warn("%s: error: bitmap file must open for write\n",
7074 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10007075 err = -EBADF;
7076 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11007077 pr_warn("%s: error: bitmap file is already in use\n",
7078 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10007079 err = -EBUSY;
7080 }
7081 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11007082 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07007083 return err;
7084 }
NeilBrown1e594bb2014-12-15 12:57:00 +11007085 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11007086 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07007087 } else if (mddev->bitmap == NULL)
7088 return -ENOENT; /* cannot remove what isn't there */
7089 err = 0;
7090 if (mddev->pers) {
NeilBrown69e51b42010-06-01 19:37:35 +10007091 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007092 struct bitmap *bitmap;
7093
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007094 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007095 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007096 if (!IS_ERR(bitmap)) {
7097 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007098 err = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11007099 } else
7100 err = PTR_ERR(bitmap);
NeilBrown52a0d492017-10-17 13:46:43 +11007101 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007102 md_bitmap_destroy(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11007103 fd = -1;
7104 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007105 mddev_resume(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11007106 } else if (fd < 0) {
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007107 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007108 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007109 mddev_resume(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07007110 }
NeilBrownd7375ab2006-06-26 00:27:43 -07007111 }
7112 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11007113 struct file *f = mddev->bitmap_info.file;
7114 if (f) {
7115 spin_lock(&mddev->lock);
7116 mddev->bitmap_info.file = NULL;
7117 spin_unlock(&mddev->lock);
7118 fput(f);
7119 }
NeilBrown36fa3062005-09-09 16:23:45 -07007120 }
7121
NeilBrown32a76272005-06-21 17:17:14 -07007122 return err;
7123}
7124
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125/*
Christoph Hellwig7e0adbf2020-06-07 17:31:19 +02007126 * md_set_array_info is used two different ways
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 * The original usage is when creating a new array.
7128 * In this usage, raid_disks is > 0 and it together with
7129 * level, size, not_persistent,layout,chunksize determine the
7130 * shape of the array.
7131 * This will always create an array with a type-0.90.0 superblock.
7132 * The newer usage is when assembling an array.
7133 * In this case raid_disks will be 0, and the major_version field is
7134 * use to determine which style super-blocks are to be found on the devices.
7135 * The minor and patch _version numbers are also kept incase the
7136 * super_block handler wishes to interpret them.
7137 */
Christoph Hellwig7e0adbf2020-06-07 17:31:19 +02007138int md_set_array_info(struct mddev *mddev, struct mdu_array_info_s *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 if (info->raid_disks == 0) {
7141 /* just setting version number for superblock loading */
7142 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07007143 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144 super_types[info->major_version].name == NULL) {
7145 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11007146 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 info->major_version);
7148 return -EINVAL;
7149 }
7150 mddev->major_version = info->major_version;
7151 mddev->minor_version = info->minor_version;
7152 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08007153 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11007154 /* ensure mddev_put doesn't delete this now that there
7155 * is some minimal configuration.
7156 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11007157 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 return 0;
7159 }
7160 mddev->major_version = MD_MAJOR_VERSION;
7161 mddev->minor_version = MD_MINOR_VERSION;
7162 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11007163 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164
7165 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08007166 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11007167 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168 mddev->raid_disks = info->raid_disks;
7169 /* don't set md_minor, it is determined by which /dev/md* was
7170 * openned
7171 */
7172 if (info->state & (1<<MD_SB_CLEAN))
7173 mddev->recovery_cp = MaxSector;
7174 else
7175 mddev->recovery_cp = 0;
7176 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08007177 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178
7179 mddev->layout = info->layout;
NeilBrown33f2c352019-09-09 16:52:29 +10007180 if (mddev->level == 0)
7181 /* Cannot trust RAID0 layout info here */
7182 mddev->layout = -1;
Andre Noll9d8f0362009-06-18 08:45:01 +10007183 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184
Shaohua Li29530792016-12-08 15:48:19 -08007185 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11007186 mddev->max_disks = MD_SB_DISKS;
7187 mddev->flags = 0;
7188 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08007189 }
7190 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191
NeilBrownc3d97142009-12-14 12:49:52 +11007192 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10007193 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11007194 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08007195
NeilBrownf6705572006-03-27 01:18:11 -08007196 mddev->reshape_position = MaxSector;
7197
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198 /*
7199 * Generate a 128 bit UUID
7200 */
7201 get_random_bytes(mddev->uuid, 16);
7202
NeilBrownf6705572006-03-27 01:18:11 -08007203 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10007204 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08007205 mddev->new_layout = mddev->layout;
7206 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007207 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08007208
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209 return 0;
7210}
7211
NeilBrownfd01b882011-10-11 16:47:53 +11007212void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11007213{
Shaohua Liefa4b772017-10-18 22:08:13 -07007214 lockdep_assert_held(&mddev->reconfig_mutex);
Dan Williamsb522adc2009-03-31 15:00:31 +11007215
7216 if (mddev->external_size)
7217 return;
7218
Dan Williams1f403622009-03-31 14:59:03 +11007219 mddev->array_sectors = array_sectors;
7220}
7221EXPORT_SYMBOL(md_set_array_sectors);
7222
NeilBrownfd01b882011-10-11 16:47:53 +11007223static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08007224{
NeilBrown3cb03002011-10-11 16:45:26 +11007225 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08007226 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10007227 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08007228 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04007229
NeilBrowna35b0d62006-01-06 00:20:49 -08007230 if (mddev->pers->resize == NULL)
7231 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10007232 /* The "num_sectors" is the number of sectors of each device that
7233 * is used. This can only make sense for arrays with redundancy.
7234 * linear and raid0 always use whatever space is available. We can only
7235 * consider changing this number if no resync or reconstruction is
7236 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10007237 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10007238 * of each device. If num_sectors is zero, we find the largest size
7239 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08007240 */
NeilBrownf851b602014-12-11 10:02:10 +11007241 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7242 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08007243 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10007244 if (mddev->ro)
7245 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10007246
NeilBrowndafb20f2012-03-19 12:46:39 +11007247 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11007248 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07007249
Andre Nolld71f9f82008-07-11 22:02:22 +10007250 if (fit && (num_sectors == 0 || num_sectors > avail))
7251 num_sectors = avail;
7252 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08007253 return -ENOSPC;
7254 }
Andre Nolld71f9f82008-07-11 22:02:22 +10007255 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08007256 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08007257 if (mddev_is_clustered(mddev))
7258 md_cluster_ops->update_size(mddev, old_dev_sectors);
7259 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08007260 set_capacity(mddev->gendisk, mddev->array_sectors);
Christoph Hellwig659e56b2020-09-01 17:57:43 +02007261 revalidate_disk_size(mddev->gendisk, true);
Guoqing Jiangc9483632017-02-24 11:15:23 +08007262 }
7263 }
NeilBrowna35b0d62006-01-06 00:20:49 -08007264 return rv;
7265}
7266
NeilBrownfd01b882011-10-11 16:47:53 +11007267static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08007268{
7269 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10007270 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08007271 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08007272 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08007273 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10007274 if (mddev->ro)
7275 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08007276 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10007277 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08007278 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11007279 if (mddev->sync_thread ||
7280 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7281 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08007282 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10007283
7284 rdev_for_each(rdev, mddev) {
7285 if (mddev->raid_disks < raid_disks &&
7286 rdev->data_offset < rdev->new_data_offset)
7287 return -EINVAL;
7288 if (mddev->raid_disks > raid_disks &&
7289 rdev->data_offset > rdev->new_data_offset)
7290 return -EINVAL;
7291 }
7292
NeilBrown63c70c42006-03-27 01:18:13 -08007293 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10007294 if (mddev->delta_disks < 0)
7295 mddev->reshape_backwards = 1;
7296 else if (mddev->delta_disks > 0)
7297 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08007298
7299 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10007300 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11007301 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007302 mddev->reshape_backwards = 0;
7303 }
NeilBrownda943b992006-01-06 00:20:54 -08007304 return rv;
7305}
7306
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307/*
7308 * update_array_info is used to change the configuration of an
7309 * on-line array.
7310 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
7311 * fields in the info are checked against the array.
7312 * Any differences that cannot be handled will cause an error.
7313 * Normally, only one change can be managed at a time.
7314 */
NeilBrownfd01b882011-10-11 16:47:53 +11007315static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316{
7317 int rv = 0;
7318 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007319 int state = 0;
7320
7321 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11007322 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07007323 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324
7325 if (mddev->major_version != info->major_version ||
7326 mddev->minor_version != info->minor_version ||
7327/* mddev->patch_version != info->patch_version || */
7328 mddev->ctime != info->ctime ||
7329 mddev->level != info->level ||
7330/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08007331 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10007332 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07007333 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
7334 ((state^info->state) & 0xfffffe00)
7335 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336 return -EINVAL;
7337 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11007338 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
7339 cnt++;
7340 if (mddev->raid_disks != info->raid_disks)
7341 cnt++;
7342 if (mddev->layout != info->layout)
7343 cnt++;
7344 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
7345 cnt++;
7346 if (cnt == 0)
7347 return 0;
7348 if (cnt > 1)
7349 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350
7351 if (mddev->layout != info->layout) {
7352 /* Change layout
7353 * we don't need to do anything at the md level, the
7354 * personality will take care of it all.
7355 */
NeilBrown50ac1682009-06-18 08:47:55 +10007356 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007358 else {
7359 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10007360 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10007361 if (rv)
7362 mddev->new_layout = mddev->layout;
7363 return rv;
7364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007366 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10007367 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368
NeilBrownda943b992006-01-06 00:20:54 -08007369 if (mddev->raid_disks != info->raid_disks)
7370 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371
NeilBrown36fa3062005-09-09 16:23:45 -07007372 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007373 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
7374 rv = -EINVAL;
7375 goto err;
7376 }
7377 if (mddev->recovery || mddev->sync_thread) {
7378 rv = -EBUSY;
7379 goto err;
7380 }
NeilBrown36fa3062005-09-09 16:23:45 -07007381 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007382 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07007383 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007384 if (mddev->bitmap) {
7385 rv = -EEXIST;
7386 goto err;
7387 }
7388 if (mddev->bitmap_info.default_offset == 0) {
7389 rv = -EINVAL;
7390 goto err;
7391 }
NeilBrownc3d97142009-12-14 12:49:52 +11007392 mddev->bitmap_info.offset =
7393 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10007394 mddev->bitmap_info.space =
7395 mddev->bitmap_info.default_space;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007396 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007397 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007398 if (!IS_ERR(bitmap)) {
7399 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007400 rv = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11007401 } else
7402 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07007403 if (rv)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007404 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007405 mddev_resume(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07007406 } else {
7407 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007408 if (!mddev->bitmap) {
7409 rv = -ENOENT;
7410 goto err;
7411 }
7412 if (mddev->bitmap->storage.file) {
7413 rv = -EINVAL;
7414 goto err;
7415 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11007416 if (mddev->bitmap_info.nodes) {
7417 /* hold PW on all the bitmap lock */
7418 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11007419 pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n");
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11007420 rv = -EPERM;
7421 md_cluster_ops->unlock_all_bitmaps(mddev);
7422 goto err;
7423 }
7424
7425 mddev->bitmap_info.nodes = 0;
7426 md_cluster_ops->leave(mddev);
Zhao Hemingedee9df2020-07-21 02:08:53 +08007427 module_put(md_cluster_mod);
Zhao Heming7c9d5c52020-07-21 02:08:52 +08007428 mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY;
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11007429 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007430 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007431 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007432 mddev_resume(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11007433 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007434 }
7435 }
NeilBrown850b2b422006-10-03 01:15:46 -07007436 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007437 return rv;
7438err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439 return rv;
7440}
7441
NeilBrownfd01b882011-10-11 16:47:53 +11007442static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443{
NeilBrown3cb03002011-10-11 16:45:26 +11007444 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11007445 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446
7447 if (mddev->pers == NULL)
7448 return -ENODEV;
7449
NeilBrown1ca69c42012-10-11 13:37:33 +11007450 rcu_read_lock();
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01007451 rdev = md_find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11007453 err = -ENODEV;
7454 else {
7455 md_error(mddev, rdev);
7456 if (!test_bit(Faulty, &rdev->flags))
7457 err = -EBUSY;
7458 }
7459 rcu_read_unlock();
7460 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461}
7462
Andre Noll2f9618c2008-04-25 18:57:58 +02007463/*
7464 * We have a problem here : there is no easy way to give a CHS
7465 * virtual geometry. We currently pretend that we have a 2 heads
7466 * 4 sectors (with a BIG number of cylinders...). This drives
7467 * dosfs just mad... ;-)
7468 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007469static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7470{
NeilBrownfd01b882011-10-11 16:47:53 +11007471 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007472
7473 geo->heads = 2;
7474 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11007475 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007476 return 0;
7477}
7478
Nicolas Schichancb335f82014-01-15 16:58:52 +01007479static inline bool md_ioctl_valid(unsigned int cmd)
7480{
7481 switch (cmd) {
7482 case ADD_NEW_DISK:
7483 case BLKROSET:
7484 case GET_ARRAY_INFO:
7485 case GET_BITMAP_FILE:
7486 case GET_DISK_INFO:
7487 case HOT_ADD_DISK:
7488 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007489 case RAID_VERSION:
7490 case RESTART_ARRAY_RW:
7491 case RUN_ARRAY:
7492 case SET_ARRAY_INFO:
7493 case SET_BITMAP_FILE:
7494 case SET_DISK_FAULTY:
7495 case STOP_ARRAY:
7496 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007497 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007498 return true;
7499 default:
7500 return false;
7501 }
7502}
7503
Al Viroa39907f2008-03-02 10:31:15 -05007504static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505 unsigned int cmd, unsigned long arg)
7506{
7507 int err = 0;
7508 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11007509 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10007510 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08007511 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512
Nicolas Schichancb335f82014-01-15 16:58:52 +01007513 if (!md_ioctl_valid(cmd))
7514 return -ENOTTY;
7515
NeilBrown506c9e42011-12-23 10:17:26 +11007516 switch (cmd) {
7517 case RAID_VERSION:
7518 case GET_ARRAY_INFO:
7519 case GET_DISK_INFO:
7520 break;
7521 default:
7522 if (!capable(CAP_SYS_ADMIN))
7523 return -EACCES;
7524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525
7526 /*
7527 * Commands dealing with the RAID driver but not any
7528 * particular array:
7529 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007530 switch (cmd) {
7531 case RAID_VERSION:
7532 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007533 goto out;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007534 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535 }
7536
7537 /*
7538 * Commands creating/starting a new array:
7539 */
7540
Al Viroa39907f2008-03-02 10:31:15 -05007541 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542
7543 if (!mddev) {
7544 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007545 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546 }
7547
NeilBrown1ca69c42012-10-11 13:37:33 +11007548 /* Some actions do not requires the mutex */
7549 switch (cmd) {
7550 case GET_ARRAY_INFO:
7551 if (!mddev->raid_disks && !mddev->external)
7552 err = -ENODEV;
7553 else
7554 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007555 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007556
7557 case GET_DISK_INFO:
7558 if (!mddev->raid_disks && !mddev->external)
7559 err = -ENODEV;
7560 else
7561 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007562 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007563
7564 case SET_DISK_FAULTY:
7565 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007566 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007567
7568 case GET_BITMAP_FILE:
7569 err = get_bitmap_file(mddev, argp);
7570 goto out;
7571
NeilBrown1ca69c42012-10-11 13:37:33 +11007572 }
7573
Guoqing Jiang78b990c2020-04-04 23:57:10 +02007574 if (cmd == ADD_NEW_DISK || cmd == HOT_ADD_DISK)
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02007575 flush_rdev_wq(mddev);
NeilBrowna7a3f082012-12-11 13:35:54 +11007576
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007577 if (cmd == HOT_REMOVE_DISK)
7578 /* need to ensure recovery thread has run */
7579 wait_event_interruptible_timeout(mddev->sb_wait,
7580 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007581 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007582 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007583 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7584 /* Need to flush page cache, and ensure no-one else opens
7585 * and writes
7586 */
7587 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007588 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007589 mutex_unlock(&mddev->open_mutex);
7590 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007591 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007592 }
NeilBrown065e5192017-04-06 11:16:33 +08007593 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007594 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007595 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007596 mutex_unlock(&mddev->open_mutex);
7597 sync_blockdev(bdev);
7598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599 err = mddev_lock(mddev);
7600 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007601 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7602 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007603 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604 }
7605
NeilBrownc02c0ae2012-12-11 13:39:21 +11007606 if (cmd == SET_ARRAY_INFO) {
7607 mdu_array_info_t info;
7608 if (!arg)
7609 memset(&info, 0, sizeof(info));
7610 else if (copy_from_user(&info, argp, sizeof(info))) {
7611 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007612 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007613 }
7614 if (mddev->pers) {
7615 err = update_array_info(mddev, &info);
7616 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007617 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007618 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007620 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007621 }
7622 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007623 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007624 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007625 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007626 }
7627 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007628 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007629 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007630 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007631 }
Christoph Hellwig7e0adbf2020-06-07 17:31:19 +02007632 err = md_set_array_info(mddev, &info);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007633 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007634 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007635 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007636 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007637 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007638 }
7639
7640 /*
7641 * Commands querying/configuring an existing array:
7642 */
NeilBrown32a76272005-06-21 17:17:14 -07007643 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007644 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007645 if ((!mddev->raid_disks && !mddev->external)
7646 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7647 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7648 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007649 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007650 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651 }
7652
7653 /*
7654 * Commands even a read-only array can execute:
7655 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007656 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007657 case RESTART_ARRAY_RW:
7658 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007659 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007660
7661 case STOP_ARRAY:
7662 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007663 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007664
7665 case STOP_ARRAY_RO:
7666 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007667 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007668
NeilBrown3ea8929d2013-04-24 11:42:41 +10007669 case HOT_REMOVE_DISK:
7670 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007671 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007672
NeilBrown7ceb17e2013-04-24 11:42:42 +10007673 case ADD_NEW_DISK:
7674 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007675 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007676 * So require mddev->pers and MD_DISK_SYNC.
7677 */
7678 if (mddev->pers) {
7679 mdu_disk_info_t info;
7680 if (copy_from_user(&info, argp, sizeof(info)))
7681 err = -EFAULT;
7682 else if (!(info.state & (1<<MD_DISK_SYNC)))
7683 /* Need to clear read-only for this */
7684 break;
7685 else
Christoph Hellwig7e0adbf2020-06-07 17:31:19 +02007686 err = md_add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007687 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007688 }
7689 break;
7690
NeilBrownc02c0ae2012-12-11 13:39:21 +11007691 case BLKROSET:
7692 if (get_user(ro, (int __user *)(arg))) {
7693 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007694 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007695 }
7696 err = -EINVAL;
7697
7698 /* if the bdev is going readonly the value of mddev->ro
7699 * does not matter, no writes are coming
7700 */
7701 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007702 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007703
NeilBrownc02c0ae2012-12-11 13:39:21 +11007704 /* are we are already prepared for writes? */
7705 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007706 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007707
7708 /* transitioning to readauto need only happen for
7709 * arrays that call md_write_start
7710 */
7711 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007713 if (err == 0) {
7714 mddev->ro = 2;
7715 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007716 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007717 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007718 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007719 }
7720
7721 /*
7722 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007723 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007724 */
NeilBrown326eb172014-09-30 15:36:28 +10007725 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007726 if (mddev->ro == 2) {
7727 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007728 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007729 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007730 /* mddev_unlock will wake thread */
7731 /* If a device failed while we were read-only, we
7732 * need to make sure the metadata is updated now.
7733 */
Shaohua Li29530792016-12-08 15:48:19 -08007734 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007735 mddev_unlock(mddev);
7736 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007737 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7738 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007739 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007740 }
NeilBrownf91de922005-11-08 21:39:36 -08007741 } else {
7742 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007743 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745 }
7746
NeilBrownc02c0ae2012-12-11 13:39:21 +11007747 switch (cmd) {
7748 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007749 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007750 mdu_disk_info_t info;
7751 if (copy_from_user(&info, argp, sizeof(info)))
7752 err = -EFAULT;
7753 else
Christoph Hellwig7e0adbf2020-06-07 17:31:19 +02007754 err = md_add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007755 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007758 case CLUSTERED_DISK_NACK:
7759 if (mddev_is_clustered(mddev))
7760 md_cluster_ops->new_disk_ack(mddev, false);
7761 else
7762 err = -EINVAL;
7763 goto unlock;
7764
NeilBrownc02c0ae2012-12-11 13:39:21 +11007765 case HOT_ADD_DISK:
7766 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007767 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007768
NeilBrownc02c0ae2012-12-11 13:39:21 +11007769 case RUN_ARRAY:
7770 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007771 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772
NeilBrownc02c0ae2012-12-11 13:39:21 +11007773 case SET_BITMAP_FILE:
7774 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007775 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007776
NeilBrownc02c0ae2012-12-11 13:39:21 +11007777 default:
7778 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007779 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780 }
7781
NeilBrown3adc28d2014-09-30 15:46:41 +10007782unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007783 if (mddev->hold_active == UNTIL_IOCTL &&
7784 err != -EINVAL)
7785 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007787out:
NeilBrown065e5192017-04-06 11:16:33 +08007788 if(did_set_md_closing)
7789 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790 return err;
7791}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007792#ifdef CONFIG_COMPAT
7793static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7794 unsigned int cmd, unsigned long arg)
7795{
7796 switch (cmd) {
7797 case HOT_REMOVE_DISK:
7798 case HOT_ADD_DISK:
7799 case SET_DISK_FAULTY:
7800 case SET_BITMAP_FILE:
7801 /* These take in integer arg, do not convert */
7802 break;
7803 default:
7804 arg = (unsigned long)compat_ptr(arg);
7805 break;
7806 }
7807
7808 return md_ioctl(bdev, mode, cmd, arg);
7809}
7810#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811
Al Viroa39907f2008-03-02 10:31:15 -05007812static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813{
7814 /*
7815 * Succeed if we can lock the mddev, which confirms that
7816 * it isn't being stopped right now.
7817 */
NeilBrownfd01b882011-10-11 16:47:53 +11007818 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007819 int err;
7820
Yuanhan Liu0c098222012-05-22 13:55:32 +10007821 if (!mddev)
7822 return -ENODEV;
7823
NeilBrownd3374822009-01-09 08:31:10 +11007824 if (mddev->gendisk != bdev->bd_disk) {
7825 /* we are racing with mddev_put which is discarding this
7826 * bd_disk.
7827 */
7828 mddev_put(mddev);
7829 /* Wait until bdev->bd_disk is definitely gone */
Guoqing Jiangf6766ff2020-04-04 23:57:09 +02007830 if (work_pending(&mddev->del_work))
7831 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007832 /* Then retry the open from the top */
7833 return -ERESTARTSYS;
7834 }
7835 BUG_ON(mddev != bdev->bd_disk->private_data);
7836
NeilBrownc8c00a62009-08-10 12:50:52 +10007837 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007838 goto out;
7839
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007840 if (test_bit(MD_CLOSING, &mddev->flags)) {
7841 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007842 err = -ENODEV;
7843 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007844 }
7845
Linus Torvalds1da177e2005-04-16 15:20:36 -07007846 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007847 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007848 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007849
Christoph Hellwig818077d2020-09-08 16:53:43 +02007850 bdev_check_media_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007852 if (err)
7853 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854 return err;
7855}
7856
Al Virodb2a1442013-05-05 21:52:57 -04007857static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007858{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007859 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007861 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007862 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007865
Christoph Hellwiga564e232020-07-08 14:25:41 +02007866static unsigned int md_check_events(struct gendisk *disk, unsigned int clearing)
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007867{
NeilBrownfd01b882011-10-11 16:47:53 +11007868 struct mddev *mddev = disk->private_data;
Christoph Hellwiga564e232020-07-08 14:25:41 +02007869 unsigned int ret = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007870
Christoph Hellwiga564e232020-07-08 14:25:41 +02007871 if (mddev->changed)
7872 ret = DISK_EVENT_MEDIA_CHANGE;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007873 mddev->changed = 0;
Christoph Hellwiga564e232020-07-08 14:25:41 +02007874 return ret;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007875}
Christoph Hellwiga564e232020-07-08 14:25:41 +02007876
Christoph Hellwig7e0adbf2020-06-07 17:31:19 +02007877const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878{
7879 .owner = THIS_MODULE,
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02007880 .submit_bio = md_submit_bio,
Al Viroa39907f2008-03-02 10:31:15 -05007881 .open = md_open,
7882 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007883 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007884#ifdef CONFIG_COMPAT
7885 .compat_ioctl = md_compat_ioctl,
7886#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007887 .getgeo = md_getgeo,
Christoph Hellwiga564e232020-07-08 14:25:41 +02007888 .check_events = md_check_events,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889};
7890
NeilBrownf72ffdd2014-09-30 14:23:59 +10007891static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892{
NeilBrown2b8bf342011-10-11 16:48:23 +11007893 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894
Linus Torvalds1da177e2005-04-16 15:20:36 -07007895 /*
7896 * md_thread is a 'system-thread', it's priority should be very
7897 * high. We avoid resource deadlocks individually in each
7898 * raid personality. (RAID5 does preallocation) We also use RR and
7899 * the very same RT priority as kswapd, thus we will never get
7900 * into a priority inversion deadlock.
7901 *
7902 * we definitely have to have equal or higher priority than
7903 * bdflush, otherwise bdflush will deadlock if there are too
7904 * many dirty RAID5 blocks.
7905 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007906
NeilBrown6985c432005-10-19 21:23:47 -07007907 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007908 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909
NeilBrown93588e22005-11-15 00:09:12 -08007910 /* We need to wait INTERRUPTIBLE so that
7911 * we don't add to the load-average.
7912 * That means we need to be sure no signals are
7913 * pending
7914 */
7915 if (signal_pending(current))
7916 flush_signals(current);
7917
7918 wait_event_interruptible_timeout
7919 (thread->wqueue,
7920 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007921 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007922 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923
NeilBrown6c987912011-01-14 09:13:53 +11007924 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007925 if (kthread_should_park())
7926 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007927 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007928 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007930
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931 return 0;
7932}
7933
NeilBrown2b8bf342011-10-11 16:48:23 +11007934void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935{
7936 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007937 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Guoqing Jiangd1d90142017-10-09 10:32:48 +08007938 set_bit(THREAD_WAKEUP, &thread->flags);
7939 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940 }
7941}
NeilBrown6c144d32014-09-30 16:15:38 +10007942EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007943
Shaohua Li4ed87312012-10-11 13:34:00 +11007944struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7945 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007946{
NeilBrown2b8bf342011-10-11 16:48:23 +11007947 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948
NeilBrown2b8bf342011-10-11 16:48:23 +11007949 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007950 if (!thread)
7951 return NULL;
7952
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953 init_waitqueue_head(&thread->wqueue);
7954
Linus Torvalds1da177e2005-04-16 15:20:36 -07007955 thread->run = run;
7956 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007957 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007958 thread->tsk = kthread_run(md_thread, thread,
7959 "%s_%s",
7960 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007961 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007962 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963 kfree(thread);
7964 return NULL;
7965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966 return thread;
7967}
NeilBrown6c144d32014-09-30 16:15:38 +10007968EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007969
NeilBrown2b8bf342011-10-11 16:48:23 +11007970void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007971{
NeilBrown2b8bf342011-10-11 16:48:23 +11007972 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007973 if (!thread)
7974 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007975 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007976 /* Locking ensures that mddev_unlock does not wake_up a
7977 * non-existent thread
7978 */
7979 spin_lock(&pers_lock);
7980 *threadp = NULL;
7981 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007982
7983 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007984 kfree(thread);
7985}
NeilBrown6c144d32014-09-30 16:15:38 +10007986EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007987
NeilBrownfd01b882011-10-11 16:47:53 +11007988void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989{
NeilBrownb2d444d2005-11-08 21:39:31 -08007990 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007992
NeilBrownde393cd2011-07-28 11:31:48 +10007993 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007994 return;
7995 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007996 if (mddev->degraded)
7997 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007998 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007999 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8000 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8001 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10008002 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02008003 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08008004 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005}
NeilBrown6c144d32014-09-30 16:15:38 +10008006EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007
8008/* seq_file implementation /proc/mdstat */
8009
8010static void status_unused(struct seq_file *seq)
8011{
8012 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008013 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008014
8015 seq_printf(seq, "unused devices: ");
8016
Cheng Renquan159ec1f2009-01-09 08:31:08 +11008017 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008018 char b[BDEVNAME_SIZE];
8019 i++;
8020 seq_printf(seq, "%s ",
8021 bdevname(rdev->bdev,b));
8022 }
8023 if (!i)
8024 seq_printf(seq, "<none>");
8025
8026 seq_printf(seq, "\n");
8027}
8028
NeilBrownf7851be2015-07-02 17:12:58 +10008029static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008030{
NeilBrowndd71cf62009-05-07 12:49:35 +10008031 sector_t max_sectors, resync, res;
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008032 unsigned long dt, db = 0;
8033 sector_t rt, curr_mark_cnt, resync_mark_cnt;
8034 int scale, recovery_active;
NeilBrown4588b422006-03-27 01:18:04 -08008035 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008036
NeilBrownc804cde2012-05-21 09:28:33 +10008037 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8038 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10008039 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008040 else
NeilBrowndd71cf62009-05-07 12:49:35 +10008041 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008042
NeilBrownf7851be2015-07-02 17:12:58 +10008043 resync = mddev->curr_resync;
8044 if (resync <= 3) {
8045 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
8046 /* Still cleaning up */
8047 resync = max_sectors;
Nate Daileyd2e2ec82017-11-30 11:33:30 -05008048 } else if (resync > max_sectors)
8049 resync = max_sectors;
8050 else
NeilBrownf7851be2015-07-02 17:12:58 +10008051 resync -= atomic_read(&mddev->recovery_active);
8052
8053 if (resync == 0) {
Guoqing Jiang0357ba22018-07-02 16:26:25 +08008054 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
8055 struct md_rdev *rdev;
8056
8057 rdev_for_each(rdev, mddev)
8058 if (rdev->raid_disk >= 0 &&
8059 !test_bit(Faulty, &rdev->flags) &&
8060 rdev->recovery_offset != MaxSector &&
8061 rdev->recovery_offset) {
8062 seq_printf(seq, "\trecover=REMOTE");
8063 return 1;
8064 }
8065 if (mddev->reshape_position != MaxSector)
8066 seq_printf(seq, "\treshape=REMOTE");
8067 else
8068 seq_printf(seq, "\tresync=REMOTE");
8069 return 1;
8070 }
NeilBrownf7851be2015-07-02 17:12:58 +10008071 if (mddev->recovery_cp < MaxSector) {
8072 seq_printf(seq, "\tresync=PENDING");
8073 return 1;
8074 }
8075 return 0;
8076 }
8077 if (resync < 3) {
8078 seq_printf(seq, "\tresync=DELAYED");
8079 return 1;
8080 }
8081
NeilBrown403df472014-09-30 15:52:29 +10008082 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08008083 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10008084 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08008085 * u32, as those are the requirements for sector_div.
8086 * Thus 'scale' must be at least 10
8087 */
8088 scale = 10;
8089 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10008090 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08008091 scale++;
8092 }
8093 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10008094 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08008095
8096 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008097 {
NeilBrown4588b422006-03-27 01:18:04 -08008098 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008099 seq_printf(seq, "[");
8100 for (i = 0; i < x; i++)
8101 seq_printf(seq, "=");
8102 seq_printf(seq, ">");
8103 for (i = 0; i < y; i++)
8104 seq_printf(seq, ".");
8105 seq_printf(seq, "] ");
8106 }
NeilBrown4588b422006-03-27 01:18:04 -08008107 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08008108 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
8109 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07008110 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
8111 "check" :
8112 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
8113 "resync" : "recovery"))),
8114 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10008115 (unsigned long long) resync/2,
8116 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008117
8118 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07008119 * dt: time from mark until now
8120 * db: blocks written from mark until now
8121 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10008122 *
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008123 * rt is a sector_t, which is always 64bit now. We are keeping
8124 * the original algorithm, but it is not really necessary.
8125 *
8126 * Original algorithm:
8127 * So we divide before multiply in case it is 32bit and close
8128 * to the limit.
8129 * We scale the divisor (db) by 32 to avoid losing precision
8130 * near the end of resync when the number of remaining sectors
8131 * is close to 'db'.
8132 * We then divide rt by 32 after multiplying by db to compensate.
8133 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008134 */
8135 dt = ((jiffies - mddev->resync_mark) / HZ);
8136 if (!dt) dt++;
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008137
8138 curr_mark_cnt = mddev->curr_mark_cnt;
8139 recovery_active = atomic_read(&mddev->recovery_active);
8140 resync_mark_cnt = mddev->resync_mark_cnt;
8141
8142 if (curr_mark_cnt >= (recovery_active + resync_mark_cnt))
8143 db = curr_mark_cnt - (recovery_active + resync_mark_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008144
NeilBrowndd71cf62009-05-07 12:49:35 +10008145 rt = max_sectors - resync; /* number of remaining sectors */
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008146 rt = div64_u64(rt, db/32+1);
NeilBrowndd71cf62009-05-07 12:49:35 +10008147 rt *= dt;
8148 rt >>= 5;
8149
8150 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
8151 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152
NeilBrownff4e8d92006-07-10 04:44:16 -07008153 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10008154 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155}
8156
8157static void *md_seq_start(struct seq_file *seq, loff_t *pos)
8158{
8159 struct list_head *tmp;
8160 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11008161 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008162
8163 if (l >= 0x10000)
8164 return NULL;
8165 if (!l--)
8166 /* header */
8167 return (void*)1;
8168
8169 spin_lock(&all_mddevs_lock);
8170 list_for_each(tmp,&all_mddevs)
8171 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11008172 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008173 mddev_get(mddev);
8174 spin_unlock(&all_mddevs_lock);
8175 return mddev;
8176 }
8177 spin_unlock(&all_mddevs_lock);
8178 if (!l--)
8179 return (void*)2;/* tail */
8180 return NULL;
8181}
8182
8183static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
8184{
8185 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008186 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10008187
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188 ++*pos;
8189 if (v == (void*)2)
8190 return NULL;
8191
8192 spin_lock(&all_mddevs_lock);
8193 if (v == (void*)1)
8194 tmp = all_mddevs.next;
8195 else
8196 tmp = mddev->all_mddevs.next;
8197 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11008198 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008199 else {
8200 next_mddev = (void*)2;
8201 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10008202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008203 spin_unlock(&all_mddevs_lock);
8204
8205 if (v != (void*)1)
8206 mddev_put(mddev);
8207 return next_mddev;
8208
8209}
8210
8211static void md_seq_stop(struct seq_file *seq, void *v)
8212{
NeilBrownfd01b882011-10-11 16:47:53 +11008213 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214
8215 if (mddev && v != (void*)1 && v != (void*)2)
8216 mddev_put(mddev);
8217}
8218
8219static int md_seq_show(struct seq_file *seq, void *v)
8220{
NeilBrownfd01b882011-10-11 16:47:53 +11008221 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11008222 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11008223 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224
8225 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11008226 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227 seq_printf(seq, "Personalities : ");
8228 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008229 list_for_each_entry(pers, &pers_list, list)
8230 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008231
8232 spin_unlock(&pers_lock);
8233 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02008234 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008235 return 0;
8236 }
8237 if (v == (void*)2) {
8238 status_unused(seq);
8239 return 0;
8240 }
8241
NeilBrown36d091f2014-12-15 12:56:58 +11008242 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008243 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
8244 seq_printf(seq, "%s : %sactive", mdname(mddev),
8245 mddev->pers ? "" : "in");
8246 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08008247 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08008249 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07008250 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008251 seq_printf(seq, " %s", mddev->pers->name);
8252 }
8253
Andre Nolldd8ac332009-03-31 14:33:13 +11008254 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11008255 rcu_read_lock();
8256 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008257 char b[BDEVNAME_SIZE];
8258 seq_printf(seq, " %s[%d]",
8259 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07008260 if (test_bit(WriteMostly, &rdev->flags))
8261 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07008262 if (test_bit(Journal, &rdev->flags))
8263 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08008264 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008265 seq_printf(seq, "(F)");
8266 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11008267 }
8268 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07008269 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11008270 if (test_bit(Replacement, &rdev->flags))
8271 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11008272 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008273 }
NeilBrownf97fcad2014-12-15 12:56:59 +11008274 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008275
8276 if (!list_empty(&mddev->disks)) {
8277 if (mddev->pers)
8278 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10008279 (unsigned long long)
8280 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008281 else
8282 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11008283 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008284 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07008285 if (mddev->persistent) {
8286 if (mddev->major_version != 0 ||
8287 mddev->minor_version != 90) {
8288 seq_printf(seq," super %d.%d",
8289 mddev->major_version,
8290 mddev->minor_version);
8291 }
NeilBrowne6910632008-02-06 01:39:51 -08008292 } else if (mddev->external)
8293 seq_printf(seq, " super external:%s",
8294 mddev->metadata_type);
8295 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07008296 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008297
8298 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11008299 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10008300 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08008301 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10008302 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08008303 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08008304 }
NeilBrown32a76272005-06-21 17:17:14 -07008305 } else
8306 seq_printf(seq, "\n ");
8307
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008308 md_bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008309
8310 seq_printf(seq, "\n");
8311 }
NeilBrown36d091f2014-12-15 12:56:58 +11008312 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10008313
Linus Torvalds1da177e2005-04-16 15:20:36 -07008314 return 0;
8315}
8316
Jan Engelhardt110518b2009-05-07 12:49:37 +10008317static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008318 .start = md_seq_start,
8319 .next = md_seq_next,
8320 .stop = md_seq_stop,
8321 .show = md_seq_show,
8322};
8323
8324static int md_seq_open(struct inode *inode, struct file *file)
8325{
Kay Sieversf1514632011-07-12 20:48:39 +02008326 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008327 int error;
8328
8329 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08008330 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02008331 return error;
8332
8333 seq = file->private_data;
8334 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008335 return error;
8336}
8337
NeilBrowne2f23b62014-04-09 14:33:51 +10008338static int md_unloading;
Al Viroafc9a422017-07-03 06:39:46 -04008339static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
NeilBrownd7603b72006-01-06 00:20:30 -08008340{
Kay Sieversf1514632011-07-12 20:48:39 +02008341 struct seq_file *seq = filp->private_data;
Al Viroafc9a422017-07-03 06:39:46 -04008342 __poll_t mask;
NeilBrownd7603b72006-01-06 00:20:30 -08008343
NeilBrowne2f23b62014-04-09 14:33:51 +10008344 if (md_unloading)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008345 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08008346 poll_wait(filp, &md_event_waiters, wait);
8347
8348 /* always allow read */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008349 mask = EPOLLIN | EPOLLRDNORM;
NeilBrownd7603b72006-01-06 00:20:30 -08008350
Kay Sieversf1514632011-07-12 20:48:39 +02008351 if (seq->poll_event != atomic_read(&md_event_count))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008352 mask |= EPOLLERR | EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08008353 return mask;
8354}
8355
Alexey Dobriyan97a32532020-02-03 17:37:17 -08008356static const struct proc_ops mdstat_proc_ops = {
8357 .proc_open = md_seq_open,
8358 .proc_read = seq_read,
8359 .proc_lseek = seq_lseek,
8360 .proc_release = seq_release,
8361 .proc_poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362};
8363
NeilBrown84fc4b52011-10-11 16:49:58 +11008364int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008365{
NeilBrown9d487392016-11-02 14:16:49 +11008366 pr_debug("md: %s personality registered for level %d\n",
8367 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008368 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008369 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008370 spin_unlock(&pers_lock);
8371 return 0;
8372}
NeilBrown6c144d32014-09-30 16:15:38 +10008373EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374
NeilBrown84fc4b52011-10-11 16:49:58 +11008375int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008376{
NeilBrown9d487392016-11-02 14:16:49 +11008377 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008378 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008379 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008380 spin_unlock(&pers_lock);
8381 return 0;
8382}
NeilBrown6c144d32014-09-30 16:15:38 +10008383EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384
NeilBrown6022e752015-08-13 12:32:55 +10008385int register_md_cluster_operations(struct md_cluster_operations *ops,
8386 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008387{
NeilBrown6022e752015-08-13 12:32:55 +10008388 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008389 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008390 if (md_cluster_ops != NULL)
8391 ret = -EALREADY;
8392 else {
8393 md_cluster_ops = ops;
8394 md_cluster_mod = module;
8395 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008396 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008397 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008398}
8399EXPORT_SYMBOL(register_md_cluster_operations);
8400
8401int unregister_md_cluster_operations(void)
8402{
8403 spin_lock(&pers_lock);
8404 md_cluster_ops = NULL;
8405 spin_unlock(&pers_lock);
8406 return 0;
8407}
8408EXPORT_SYMBOL(unregister_md_cluster_operations);
8409
8410int md_setup_cluster(struct mddev *mddev, int nodes)
8411{
Zhao Heming7c9d5c52020-07-21 02:08:52 +08008412 int ret;
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008413 if (!md_cluster_ops)
8414 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008415 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008416 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008417 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11008418 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008419 spin_unlock(&pers_lock);
8420 return -ENOENT;
8421 }
8422 spin_unlock(&pers_lock);
8423
Zhao Heming7c9d5c52020-07-21 02:08:52 +08008424 ret = md_cluster_ops->join(mddev, nodes);
8425 if (!ret)
8426 mddev->safemode_delay = 0;
8427 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008428}
8429
8430void md_cluster_stop(struct mddev *mddev)
8431{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05008432 if (!md_cluster_ops)
8433 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008434 md_cluster_ops->leave(mddev);
8435 module_put(md_cluster_mod);
8436}
8437
NeilBrownfd01b882011-10-11 16:47:53 +11008438static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439{
NeilBrownf72ffdd2014-09-30 14:23:59 +10008440 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008441 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11008442 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008443
8444 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10008445 rcu_read_lock();
8446 rdev_for_each_rcu(rdev, mddev) {
Christoph Hellwig4245e522020-09-03 07:40:59 +02008447 struct gendisk *disk = rdev->bdev->bd_disk;
Michael Callahan59767fb2018-07-18 04:47:37 -07008448 curr_events = (int)part_stat_read_accum(&disk->part0, sectors) -
NeilBrowneea1bf32009-03-31 14:27:02 +11008449 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07008450 /* sync IO will cause sync_io to increase before the disk_stats
8451 * as sync_io is counted when a request starts, and
8452 * disk_stats is counted when it completes.
8453 * So resync activity will cause curr_events to be smaller than
8454 * when there was no such activity.
8455 * non-sync IO will cause disk_stat to increase without
8456 * increasing sync_io so curr_events will (eventually)
8457 * be larger than it was before. Once it becomes
8458 * substantially larger, the test below will cause
8459 * the array to appear non-idle, and resync will slow
8460 * down.
8461 * If there is a lot of outstanding resync activity when
8462 * we set last_event to curr_events, then all that activity
8463 * completing might cause the array to appear non-idle
8464 * and resync will be slowed down even though there might
8465 * not have been non-resync activity. This will only
8466 * happen once though. 'last_events' will soon reflect
8467 * the state where there is little or no outstanding
8468 * resync requests, and further resync activity will
8469 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08008470 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471 */
NeilBrowneea1bf32009-03-31 14:27:02 +11008472 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008473 rdev->last_events = curr_events;
8474 idle = 0;
8475 }
8476 }
NeilBrown4b809912008-07-21 17:05:25 +10008477 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008478 return idle;
8479}
8480
NeilBrownfd01b882011-10-11 16:47:53 +11008481void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008482{
8483 /* another "blocks" (512byte) blocks have been synced */
8484 atomic_sub(blocks, &mddev->recovery_active);
8485 wake_up(&mddev->recovery_wait);
8486 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07008487 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08008488 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008489 md_wakeup_thread(mddev->thread);
8490 // stop recovery, signal do_sync ....
8491 }
8492}
NeilBrown6c144d32014-09-30 16:15:38 +10008493EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008494
NeilBrown06d91a52005-06-21 17:17:12 -07008495/* md_write_start(mddev, bi)
8496 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07008497 * in superblock) before writing, schedule a superblock update
8498 * and wait for it to complete.
NeilBrowncc27b0c2017-06-05 16:49:39 +10008499 * A return value of 'false' means that the write wasn't recorded
8500 * and cannot proceed as the array is being suspend.
NeilBrown06d91a52005-06-21 17:17:12 -07008501 */
NeilBrowncc27b0c2017-06-05 16:49:39 +10008502bool md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008503{
Neil Brown0fd62b82008-06-28 08:31:36 +10008504 int did_change = 0;
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008505
NeilBrown06d91a52005-06-21 17:17:12 -07008506 if (bio_data_dir(bi) != WRITE)
NeilBrowncc27b0c2017-06-05 16:49:39 +10008507 return true;
NeilBrown06d91a52005-06-21 17:17:12 -07008508
NeilBrownf91de922005-11-08 21:39:36 -08008509 BUG_ON(mddev->ro == 1);
8510 if (mddev->ro == 2) {
8511 /* need to switch to read/write */
8512 mddev->ro = 0;
8513 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8514 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08008515 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008516 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08008517 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008518 rcu_read_lock();
8519 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11008520 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07008521 if (mddev->safemode == 1)
8522 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11008523 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
NeilBrown81fe48e2017-08-08 16:56:36 +10008524 if (mddev->in_sync || mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11008525 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07008526 if (mddev->in_sync) {
8527 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008528 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8529 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07008530 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008531 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07008532 }
NeilBrown85572d72014-12-15 12:56:56 +11008533 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07008534 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008535 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10008536 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008537 sysfs_notify_dirent_safe(mddev->sysfs_state);
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008538 if (!mddev->has_superblocks)
8539 return true;
NeilBrown09a44cc2008-05-23 13:04:36 -07008540 wait_event(mddev->sb_wait,
NeilBrownd47c8ad2017-10-05 16:23:16 +11008541 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8542 mddev->suspended);
NeilBrowncc27b0c2017-06-05 16:49:39 +10008543 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8544 percpu_ref_put(&mddev->writes_pending);
8545 return false;
8546 }
8547 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008548}
NeilBrown6c144d32014-09-30 16:15:38 +10008549EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008550
NeilBrown49728052017-03-15 14:05:12 +11008551/* md_write_inc can only be called when md_write_start() has
8552 * already been called at least once of the current request.
8553 * It increments the counter and is useful when a single request
8554 * is split into several parts. Each part causes an increment and
8555 * so needs a matching md_write_end().
8556 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8557 * a spinlocked region.
8558 */
8559void md_write_inc(struct mddev *mddev, struct bio *bi)
8560{
8561 if (bio_data_dir(bi) != WRITE)
8562 return;
8563 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11008564 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008565}
8566EXPORT_SYMBOL(md_write_inc);
8567
NeilBrownfd01b882011-10-11 16:47:53 +11008568void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008569{
NeilBrown4ad23a972017-03-15 14:05:14 +11008570 percpu_ref_put(&mddev->writes_pending);
8571
8572 if (mddev->safemode == 2)
8573 md_wakeup_thread(mddev->thread);
8574 else if (mddev->safemode_delay)
8575 /* The roundup() ensures this only performs locking once
8576 * every ->safemode_delay jiffies
8577 */
8578 mod_timer(&mddev->safemode_timer,
8579 roundup(jiffies, mddev->safemode_delay) +
8580 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008581}
NeilBrown4ad23a972017-03-15 14:05:14 +11008582
NeilBrown6c144d32014-09-30 16:15:38 +10008583EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008584
Xiao Ni26280892020-08-25 13:42:59 +08008585/* This is used by raid0 and raid10 */
8586void md_submit_discard_bio(struct mddev *mddev, struct md_rdev *rdev,
8587 struct bio *bio, sector_t start, sector_t size)
8588{
8589 struct bio *discard_bio = NULL;
8590
8591 if (__blkdev_issue_discard(rdev->bdev, start, size,
8592 GFP_NOIO, 0, &discard_bio) || !discard_bio)
8593 return;
8594
8595 bio_chain(discard_bio, bio);
8596 bio_clone_blkg_association(discard_bio, bio);
8597 if (mddev->gendisk)
8598 trace_block_bio_remap(bdev_get_queue(rdev->bdev),
8599 discard_bio, disk_devt(mddev->gendisk),
8600 bio->bi_iter.bi_sector);
8601 submit_bio_noacct(discard_bio);
8602}
8603EXPORT_SYMBOL(md_submit_discard_bio);
8604
NeilBrown2a2275d2007-01-26 00:57:11 -08008605/* md_allow_write(mddev)
8606 * Calling this ensures that the array is marked 'active' so that writes
8607 * may proceed without blocking. It is important to call this before
8608 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8609 * Must be called with mddev_lock held.
8610 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008611void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008612{
8613 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008614 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008615 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008616 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008617 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008618 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008619
NeilBrown85572d72014-12-15 12:56:56 +11008620 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008621 if (mddev->in_sync) {
8622 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008623 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8624 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008625 if (mddev->safemode_delay &&
8626 mddev->safemode == 0)
8627 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008628 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008629 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008630 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008631 /* wait for the dirty state to be recorded in the metadata */
8632 wait_event(mddev->sb_wait,
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008633 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008634 } else
NeilBrown85572d72014-12-15 12:56:56 +11008635 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008636}
8637EXPORT_SYMBOL_GPL(md_allow_write);
8638
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639#define SYNC_MARKS 10
8640#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008641#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008642void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008643{
Shaohua Li4ed87312012-10-11 13:34:00 +11008644 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008645 struct mddev *mddev2;
Yufen Yue5b521e2019-06-14 15:41:07 -07008646 unsigned int currspeed = 0, window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008647 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008648 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008649 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008650 sector_t mark_cnt[SYNC_MARKS];
8651 int last_mark,m;
8652 struct list_head *tmp;
8653 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008654 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008655 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008656 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008657 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008658 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008659
8660 /* just incase thread restarts... */
Song Liud5d885f2017-11-19 22:17:01 -08008661 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8662 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008663 return;
NeilBrown3991b312014-05-28 13:39:23 +10008664 if (mddev->ro) {/* never try to sync a read-only array */
8665 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008666 return;
NeilBrown3991b312014-05-28 13:39:23 +10008667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008668
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008669 if (mddev_is_clustered(mddev)) {
8670 ret = md_cluster_ops->resync_start(mddev);
8671 if (ret)
8672 goto skip;
8673
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008674 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008675 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8676 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8677 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8678 && ((unsigned long long)mddev->curr_resync_completed
8679 < (unsigned long long)mddev->resync_max_sectors))
8680 goto skip;
8681 }
8682
NeilBrown61df9d92006-10-03 01:15:57 -07008683 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008684 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008685 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008686 action = "check";
8687 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008688 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008689 action = "repair";
8690 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008691 desc = "resync";
8692 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8693 desc = "reshape";
8694 else
8695 desc = "recovery";
8696
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008697 mddev->last_sync_action = action ?: desc;
8698
Linus Torvalds1da177e2005-04-16 15:20:36 -07008699 /* we overload curr_resync somewhat here.
8700 * 0 == not engaged in resync at all
8701 * 2 == checking that there is no conflict with another sync
8702 * 1 == like 2, but have yielded to allow conflicting resync to
Yufen Yue5b521e2019-06-14 15:41:07 -07008703 * commence
Linus Torvalds1da177e2005-04-16 15:20:36 -07008704 * other == active in resync - this many blocks
8705 *
8706 * Before starting a resync we must have set curr_resync to
8707 * 2, and then checked that every "conflicting" array has curr_resync
8708 * less than ours. When we find one that is the same or higher
8709 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8710 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8711 * This will mean we have to start checking from the beginning again.
8712 *
8713 */
8714
8715 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008716 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008717 mddev->curr_resync = 2;
8718
8719 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008720 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008721 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008722 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008723 if (mddev2 == mddev)
8724 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008725 if (!mddev->parallel_resync
8726 && mddev2->curr_resync
8727 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008728 DEFINE_WAIT(wq);
8729 if (mddev < mddev2 && mddev->curr_resync == 2) {
8730 /* arbitrarily yield */
8731 mddev->curr_resync = 1;
8732 wake_up(&resync_wait);
8733 }
8734 if (mddev > mddev2 && mddev->curr_resync == 1)
8735 /* no need to wait here, we can wait the next
8736 * time 'round when curr_resync == 2
8737 */
8738 continue;
NeilBrown97441972008-09-19 11:49:54 +10008739 /* We need to wait 'interruptible' so as not to
8740 * contribute to the load average, and not to
8741 * be caught by 'softlockup'
8742 */
8743 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008744 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008745 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008746 if (mddev2_minor != mddev2->md_minor) {
8747 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008748 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8749 desc, mdname(mddev),
8750 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008752 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008753 if (signal_pending(current))
8754 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008755 schedule();
8756 finish_wait(&resync_wait, &wq);
8757 goto try_again;
8758 }
8759 finish_wait(&resync_wait, &wq);
8760 }
8761 }
8762 } while (mddev->curr_resync < 2);
8763
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008764 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008765 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008766 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008767 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008768 */
8769 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008770 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008771 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008772 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8773 j = mddev->resync_min;
8774 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008775 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008776
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008777 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
NeilBrownc804cde2012-05-21 09:28:33 +10008778 max_sectors = mddev->resync_max_sectors;
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008779 /*
8780 * If the original node aborts reshaping then we continue the
8781 * reshaping, so set j again to avoid restart reshape from the
8782 * first beginning
8783 */
8784 if (mddev_is_clustered(mddev) &&
8785 mddev->reshape_position != MaxSector)
8786 j = mddev->reshape_position;
8787 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008788 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008789 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008790 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008791 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008792 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008793 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008794 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008795 !test_bit(Faulty, &rdev->flags) &&
8796 !test_bit(In_sync, &rdev->flags) &&
8797 rdev->recovery_offset < j)
8798 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008799 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008800
8801 /* If there is a bitmap, we need to make sure all
8802 * writes that started before we added a spare
8803 * complete before we start doing a recovery.
8804 * Otherwise the write might complete and (via
8805 * bitmap_endwrite) set a bit in the bitmap after the
8806 * recovery has checked that bit and skipped that
8807 * region.
8808 */
8809 if (mddev->bitmap) {
8810 mddev->pers->quiesce(mddev, 1);
8811 mddev->pers->quiesce(mddev, 0);
8812 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008814
NeilBrown9d487392016-11-02 14:16:49 +11008815 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8816 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8817 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8818 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008819
NeilBrowneea1bf32009-03-31 14:27:02 +11008820 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008821
NeilBrown57afd892005-06-21 17:17:13 -07008822 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008823 for (m = 0; m < SYNC_MARKS; m++) {
8824 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008825 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008826 }
8827 last_mark = 0;
8828 mddev->resync_mark = mark[last_mark];
8829 mddev->resync_mark_cnt = mark_cnt[last_mark];
8830
8831 /*
8832 * Tune reconstruction:
8833 */
Yufen Yue5b521e2019-06-14 15:41:07 -07008834 window = 32 * (PAGE_SIZE / 512);
NeilBrown9d487392016-11-02 14:16:49 +11008835 pr_debug("md: using %dk window, over a total of %lluk.\n",
8836 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008837
8838 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008839 last_check = 0;
8840
8841 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008842 pr_debug("md: resuming %s of %s from checkpoint.\n",
8843 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008844 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008845 } else
8846 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008847 mddev->curr_resync_completed = j;
Junxiao Bie1a86db2020-07-14 16:10:26 -07008848 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown72f36d52012-10-11 14:25:57 +11008849 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008850 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008851
majianpeng7c2c57c2012-07-03 12:12:26 +10008852 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008853 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008854 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008855
NeilBrown57afd892005-06-21 17:17:13 -07008856 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008857
NeilBrown7a91ee12009-05-26 12:57:21 +10008858 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8859 ((mddev->curr_resync > mddev->curr_resync_completed &&
8860 (mddev->curr_resync - mddev->curr_resync_completed)
8861 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008862 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008863 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008864 >= mddev->resync_max - mddev->curr_resync_completed ||
8865 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008866 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008867 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008868 wait_event(mddev->recovery_wait,
8869 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008870 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008871 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8872 j > mddev->recovery_cp)
8873 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008874 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008875 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
Junxiao Bie1a86db2020-07-14 16:10:26 -07008876 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown97e4f422009-03-31 14:33:13 +11008877 }
NeilBrownacb180b2009-04-14 16:28:34 +10008878
NeilBrownc91abf52013-11-19 12:02:01 +11008879 while (j >= mddev->resync_max &&
8880 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008881 /* As this condition is controlled by user-space,
8882 * we can block indefinitely, so use '_interruptible'
8883 * to avoid triggering warnings.
8884 */
8885 flush_signals(current); /* just in case */
8886 wait_event_interruptible(mddev->recovery_wait,
8887 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008888 || test_bit(MD_RECOVERY_INTR,
8889 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008890 }
NeilBrownacb180b2009-04-14 16:28:34 +10008891
NeilBrownc91abf52013-11-19 12:02:01 +11008892 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8893 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008894
NeilBrown09314792015-02-19 16:04:40 +11008895 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008896 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008897 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008898 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008899 }
NeilBrown57afd892005-06-21 17:17:13 -07008900
8901 if (!skipped) { /* actual IO requested */
8902 io_sectors += sectors;
8903 atomic_add(sectors, &mddev->recovery_active);
8904 }
8905
NeilBrowne875ece2011-07-28 11:39:24 +10008906 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8907 break;
8908
Linus Torvalds1da177e2005-04-16 15:20:36 -07008909 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008910 if (j > max_sectors)
8911 /* when skipping, extra large numbers can be returned. */
8912 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008913 if (j > 2)
8914 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008915 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008916 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008917 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008918 * visible in /proc/mdstat
8919 */
8920 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008921
8922 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008923 continue;
8924
NeilBrown57afd892005-06-21 17:17:13 -07008925 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008926 repeat:
8927 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8928 /* step marks */
8929 int next = (last_mark+1) % SYNC_MARKS;
8930
8931 mddev->resync_mark = mark[next];
8932 mddev->resync_mark_cnt = mark_cnt[next];
8933 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008934 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008935 last_mark = next;
8936 }
8937
NeilBrownc91abf52013-11-19 12:02:01 +11008938 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8939 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008940
8941 /*
8942 * this loop exits only if either when we are slower than
8943 * the 'hard' speed limit, or the system was IO-idle for
8944 * a jiffy.
8945 * the system might be non-idle CPU-wise, but we only care
8946 * about not overloading the IO subsystem. (things like an
8947 * e2fsck being done on the RAID array should execute fast)
8948 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008949 cond_resched();
8950
Xiao Niac7e50a2014-08-07 09:37:41 -04008951 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8952 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008953 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008954
NeilBrown88202a02006-01-06 00:21:36 -08008955 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008956 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008957 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008958 goto repeat;
8959 }
NeilBrownac8fa412015-02-19 16:55:00 +11008960 if (!is_mddev_idle(mddev, 0)) {
8961 /*
8962 * Give other IO more of a chance.
8963 * The faster the devices, the less we wait.
8964 */
8965 wait_event(mddev->recovery_wait,
8966 !atomic_read(&mddev->recovery_active));
8967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008968 }
8969 }
NeilBrown9d487392016-11-02 14:16:49 +11008970 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8971 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8972 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008973 /*
8974 * this also signals 'finished resyncing' to md_stop
8975 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008976 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008977 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8978
NeilBrown5ed1df22015-07-24 13:27:08 +10008979 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8980 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008981 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008982 mddev->curr_resync_completed = mddev->curr_resync;
Junxiao Bie1a86db2020-07-14 16:10:26 -07008983 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown5ed1df22015-07-24 13:27:08 +10008984 }
NeilBrown09314792015-02-19 16:04:40 +11008985 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008986
NeilBrowndfc70642008-05-23 13:04:39 -07008987 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008988 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008989 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8990 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8991 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008992 pr_debug("md: checkpointing %s of %s.\n",
8993 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008994 if (test_bit(MD_RECOVERY_ERROR,
8995 &mddev->recovery))
8996 mddev->recovery_cp =
8997 mddev->curr_resync_completed;
8998 else
8999 mddev->recovery_cp =
9000 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07009001 }
9002 } else
9003 mddev->recovery_cp = MaxSector;
9004 } else {
9005 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
9006 mddev->curr_resync = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11009007 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9008 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
9009 rcu_read_lock();
9010 rdev_for_each_rcu(rdev, mddev)
9011 if (rdev->raid_disk >= 0 &&
9012 mddev->delta_disks >= 0 &&
9013 !test_bit(Journal, &rdev->flags) &&
9014 !test_bit(Faulty, &rdev->flags) &&
9015 !test_bit(In_sync, &rdev->flags) &&
9016 rdev->recovery_offset < mddev->curr_resync)
9017 rdev->recovery_offset = mddev->curr_resync;
9018 rcu_read_unlock();
9019 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07009020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009021 }
NeilBrowndb91ff52012-02-07 12:01:51 +11009022 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04009023 /* set CHANGE_PENDING here since maybe another update is needed,
9024 * so other nodes are informed. It should be harmless for normal
9025 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08009026 set_mask_bits(&mddev->sb_flags, 0,
9027 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05009028
BingJing Chang88763912018-02-22 13:34:46 +08009029 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9030 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
9031 mddev->delta_disks > 0 &&
9032 mddev->pers->finish_reshape &&
9033 mddev->pers->size &&
9034 mddev->queue) {
9035 mddev_lock_nointr(mddev);
9036 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
9037 mddev_unlock(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009038 if (!mddev_is_clustered(mddev)) {
9039 set_capacity(mddev->gendisk, mddev->array_sectors);
Christoph Hellwig659e56b2020-09-01 17:57:43 +02009040 revalidate_disk_size(mddev->gendisk, true);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009041 }
BingJing Chang88763912018-02-22 13:34:46 +08009042 }
9043
NeilBrown23da4222014-12-15 12:57:01 +11009044 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11009045 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
9046 /* We completed so min/max setting can be forgotten if used. */
9047 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
9048 mddev->resync_min = 0;
9049 mddev->resync_max = MaxSector;
9050 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
9051 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10009052 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009053 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11009054 spin_unlock(&mddev->lock);
9055
Linus Torvalds1da177e2005-04-16 15:20:36 -07009056 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009057 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08009058 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009059}
NeilBrown29269552006-03-27 01:18:10 -08009060EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009061
NeilBrown746d3202013-04-24 11:42:41 +10009062static int remove_and_add_spares(struct mddev *mddev,
9063 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009064{
NeilBrown3cb03002011-10-11 16:45:26 +11009065 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009066 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11009067 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10009068 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009069
NeilBrown39772f02018-02-03 09:19:30 +11009070 if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
9071 /* Mustn't remove devices when resync thread is running */
9072 return 0;
9073
NeilBrownd787be42016-06-02 16:19:53 +10009074 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10009075 if ((this == NULL || rdev == this) &&
9076 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07009077 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10009078 test_bit(Faulty, &rdev->flags) &&
9079 atomic_read(&rdev->nr_pending)==0) {
9080 /* Faulty non-Blocked devices with nr_pending == 0
9081 * never get nr_pending incremented,
9082 * never get Faulty cleared, and never get Blocked set.
9083 * So we can synchronize_rcu now rather than once per device
9084 */
9085 remove_some = true;
9086 set_bit(RemoveSynchronized, &rdev->flags);
9087 }
9088 }
9089
9090 if (remove_some)
9091 synchronize_rcu();
9092 rdev_for_each(rdev, mddev) {
9093 if ((this == NULL || rdev == this) &&
9094 rdev->raid_disk >= 0 &&
9095 !test_bit(Blocked, &rdev->flags) &&
9096 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07009097 (!test_bit(In_sync, &rdev->flags) &&
9098 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10009099 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009100 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11009101 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10009102 sysfs_unlink_rdev(mddev, rdev);
NeilBrown011abdc2018-04-26 14:46:29 +10009103 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009104 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11009105 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009106 }
9107 }
NeilBrownd787be42016-06-02 16:19:53 +10009108 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
9109 clear_bit(RemoveSynchronized, &rdev->flags);
9110 }
9111
Jonathan Brassow90584fc2013-03-07 16:24:26 -06009112 if (removed && mddev->kobj.sd)
Junxiao Bie1a86db2020-07-14 16:10:26 -07009113 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009114
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05009115 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10009116 goto no_add;
9117
NeilBrowndafb20f2012-03-19 12:46:39 +11009118 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05009119 if (this && this != rdev)
9120 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009121 if (test_bit(Candidate, &rdev->flags))
9122 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11009123 if (rdev->raid_disk >= 0 &&
9124 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07009125 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11009126 !test_bit(Faulty, &rdev->flags))
9127 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009128 if (rdev->raid_disk >= 0)
9129 continue;
9130 if (test_bit(Faulty, &rdev->flags))
9131 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009132 if (!test_bit(Journal, &rdev->flags)) {
9133 if (mddev->ro &&
9134 ! (rdev->saved_raid_disk >= 0 &&
9135 !test_bit(Bitmap_sync, &rdev->flags)))
9136 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009137
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009138 rdev->recovery_offset = 0;
9139 }
Guoqing Jiang3f79cc22020-04-04 23:57:11 +02009140 if (mddev->pers->hot_add_disk(mddev, rdev) == 0) {
Damien Le Moal5e3b8a82020-07-16 13:54:40 +09009141 /* failure here is OK */
9142 sysfs_link_rdev(mddev, rdev);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009143 if (!test_bit(Journal, &rdev->flags))
9144 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009145 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08009146 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07009147 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009148 }
NeilBrown746d3202013-04-24 11:42:41 +10009149no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10009150 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08009151 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009152 return spares;
9153}
NeilBrown7ebc0be2011-01-14 09:14:33 +11009154
NeilBrownac05f252014-09-30 08:10:42 +10009155static void md_start_sync(struct work_struct *ws)
9156{
9157 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05009158
NeilBrownac05f252014-09-30 08:10:42 +10009159 mddev->sync_thread = md_register_thread(md_do_sync,
9160 mddev,
9161 "resync");
9162 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11009163 pr_warn("%s: could not start resync thread...\n",
9164 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10009165 /* leave the spares where they are, it shouldn't hurt */
9166 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9167 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9168 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9169 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9170 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11009171 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10009172 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9173 &mddev->recovery))
9174 if (mddev->sysfs_action)
9175 sysfs_notify_dirent_safe(mddev->sysfs_action);
9176 } else
9177 md_wakeup_thread(mddev->sync_thread);
9178 sysfs_notify_dirent_safe(mddev->sysfs_action);
9179 md_new_event(mddev);
9180}
9181
Linus Torvalds1da177e2005-04-16 15:20:36 -07009182/*
9183 * This routine is regularly called by all per-raid-array threads to
9184 * deal with generic issues like resync and super-block update.
9185 * Raid personalities that don't have a thread (linear/raid0) do not
9186 * need this as they never do any recovery or update the superblock.
9187 *
9188 * It does not do any resync itself, but rather "forks" off other threads
9189 * to do that as needed.
9190 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
9191 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07009192 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07009193 * and wakeups up this thread which will reap the thread and finish up.
9194 * This thread also removes any faulty devices (with nr_pending == 0).
9195 *
9196 * The overall approach is:
9197 * 1/ if the superblock needs updating, update it.
9198 * 2/ If a recovery thread is running, don't do anything else.
9199 * 3/ If recovery has finished, clean up, possibly marking spares active.
9200 * 4/ If there are any faulty devices, remove them.
9201 * 5/ If array is degraded, try to add spares devices
9202 * 6/ If array has spares or is not in-sync, start a resync thread.
9203 */
NeilBrownfd01b882011-10-11 16:47:53 +11009204void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009205{
NeilBrown059421e2018-10-03 15:04:41 +10009206 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
9207 /* Write superblock - thread that called mddev_suspend()
9208 * holds reconfig_mutex for us.
9209 */
9210 set_bit(MD_UPDATING_SB, &mddev->flags);
9211 smp_mb__after_atomic();
9212 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
9213 md_update_sb(mddev, 0);
9214 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
9215 wake_up(&mddev->sb_wait);
9216 }
9217
Jonathan Brassow68866e42011-06-08 15:10:08 +10009218 if (mddev->suspended)
9219 return;
9220
NeilBrown5f404022005-06-21 17:17:16 -07009221 if (mddev->bitmap)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009222 md_bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009223
NeilBrownfca4d842005-06-21 17:17:11 -07009224 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07009225 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11009226 pr_debug("md: %s in immediate safe mode\n",
9227 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07009228 mddev->safemode = 2;
9229 }
9230 flush_signals(current);
9231 }
9232
NeilBrownc89a8ee2008-08-05 15:54:13 +10009233 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
9234 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009235 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08009236 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07009237 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07009238 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07009239 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11009240 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07009241 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009242 ))
9243 return;
NeilBrownfca4d842005-06-21 17:17:11 -07009244
NeilBrowndf5b89b2006-03-27 01:18:20 -08009245 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009246 int spares = 0;
NeilBrown480523f2019-08-20 10:21:09 +10009247 bool try_set_sync = mddev->safemode != 0;
NeilBrownfca4d842005-06-21 17:17:11 -07009248
Shaohua Liafc1f552017-08-11 20:34:45 -07009249 if (!mddev->external && mddev->safemode == 1)
NeilBrown33182d12017-08-08 16:56:36 +10009250 mddev->safemode = 0;
9251
NeilBrownc89a8ee2008-08-05 15:54:13 +10009252 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10009253 struct md_rdev *rdev;
9254 if (!mddev->external && mddev->in_sync)
9255 /* 'Blocked' flag not needed as failed devices
9256 * will be recorded if array switched to read/write.
9257 * Leaving it set will prevent the device
9258 * from being removed.
9259 */
9260 rdev_for_each(rdev, mddev)
9261 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10009262 /* On a read-only array we can:
9263 * - remove failed devices
9264 * - add already-in_sync devices if the array itself
9265 * is in-sync.
9266 * As we only add devices that are already in-sync,
9267 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10009268 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10009269 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11009270 /* There is no thread, but we need to call
9271 * ->spare_active and clear saved_raid_disk
9272 */
NeilBrown2ac295a2014-05-29 11:40:03 +10009273 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11009274 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10009275 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11009276 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08009277 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10009278 goto unlock;
9279 }
9280
Guoqing Jiang659b2542015-12-21 10:50:59 +11009281 if (mddev_is_clustered(mddev)) {
9282 struct md_rdev *rdev;
9283 /* kick the device if another node issued a
9284 * remove disk.
9285 */
9286 rdev_for_each(rdev, mddev) {
9287 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
9288 rdev->raid_disk < 0)
9289 md_kick_rdev_from_array(rdev);
9290 }
9291 }
9292
NeilBrown480523f2019-08-20 10:21:09 +10009293 if (try_set_sync && !mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11009294 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11009295 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11009296 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07009297 }
NeilBrownfca4d842005-06-21 17:17:11 -07009298
Shaohua Li29530792016-12-08 15:48:19 -08009299 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07009300 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07009301
Linus Torvalds1da177e2005-04-16 15:20:36 -07009302 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
9303 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
9304 /* resync/recovery still happening */
9305 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9306 goto unlock;
9307 }
9308 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009309 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009310 goto unlock;
9311 }
Neil Brown72a23c22008-06-28 08:31:41 +10009312 /* Set RUNNING before clearing NEEDED to avoid
9313 * any transients in the value of "sync_action".
9314 */
NeilBrown72f36d52012-10-11 14:25:57 +11009315 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11009316 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10009317 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11009318 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08009319 /* Clear some bits that don't mean anything, but
9320 * might be left set
9321 */
NeilBrown24dd4692005-11-08 21:39:26 -08009322 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
9323 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009324
NeilBrowned209582012-04-24 10:23:14 +10009325 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
9326 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10009327 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009328 /* no recovery is running.
9329 * remove any failed drives, then
9330 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11009331 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07009332 * the personality to fail the re-add.
9333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009334
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009335 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10009336 if (mddev->pers->check_reshape == NULL ||
9337 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009338 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10009339 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009340 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009341 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10009342 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08009343 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9344 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07009345 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009346 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08009347 } else if (mddev->recovery_cp < MaxSector) {
9348 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009349 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08009350 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
9351 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10009352 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08009353
Linus Torvalds1da177e2005-04-16 15:20:36 -07009354 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10009355 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07009356 /* We are adding a device or devices to an array
9357 * which has the bitmap stored on all devices.
9358 * So make sure all bitmap pages get written
9359 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009360 md_bitmap_write_all(mddev->bitmap);
NeilBrowna654b9d82005-06-21 17:17:27 -07009361 }
NeilBrownac05f252014-09-30 08:10:42 +10009362 INIT_WORK(&mddev->del_work, md_start_sync);
9363 queue_work(md_misc_wq, &mddev->del_work);
9364 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365 }
NeilBrownac05f252014-09-30 08:10:42 +10009366 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10009367 if (!mddev->sync_thread) {
9368 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11009369 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10009370 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9371 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11009372 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10009373 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10009374 }
NeilBrownac05f252014-09-30 08:10:42 +10009375 unlock:
9376 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377 mddev_unlock(mddev);
9378 }
9379}
NeilBrown6c144d32014-09-30 16:15:38 +10009380EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009381
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009382void md_reap_sync_thread(struct mddev *mddev)
9383{
9384 struct md_rdev *rdev;
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009385 sector_t old_dev_sectors = mddev->dev_sectors;
9386 bool is_reshaped = false;
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009387
9388 /* resync has finished, collect result */
9389 md_unregister_thread(&mddev->sync_thread);
9390 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
Guoqing Jiang0d8ed0e92019-07-24 11:09:21 +02009391 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
9392 mddev->degraded != mddev->raid_disks) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009393 /* success...*/
9394 /* activate any spares */
9395 if (mddev->pers->spare_active(mddev)) {
Junxiao Bie1a86db2020-07-14 16:10:26 -07009396 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
Shaohua Li29530792016-12-08 15:48:19 -08009397 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009398 }
9399 }
9400 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009401 mddev->pers->finish_reshape) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009402 mddev->pers->finish_reshape(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009403 if (mddev_is_clustered(mddev))
9404 is_reshaped = true;
9405 }
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009406
9407 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11009408 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009409 */
NeilBrownf4667222013-12-09 12:04:56 +11009410 if (!mddev->degraded)
9411 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009412 rdev->saved_raid_disk = -1;
9413
9414 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08009415 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04009416 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9417 * clustered raid */
9418 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
9419 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009420 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10009421 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009422 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9423 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9424 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9425 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009426 /*
9427 * We call md_cluster_ops->update_size here because sync_size could
9428 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9429 * so it is time to update size across cluster.
9430 */
9431 if (mddev_is_clustered(mddev) && is_reshaped
9432 && !test_bit(MD_CLOSING, &mddev->flags))
9433 md_cluster_ops->update_size(mddev, old_dev_sectors);
NeilBrownf851b602014-12-11 10:02:10 +11009434 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009435 /* flag recovery needed just to double check */
9436 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9437 sysfs_notify_dirent_safe(mddev->sysfs_action);
9438 md_new_event(mddev);
9439 if (mddev->event_work.func)
9440 queue_work(md_misc_wq, &mddev->event_work);
9441}
NeilBrown6c144d32014-09-30 16:15:38 +10009442EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009443
NeilBrownfd01b882011-10-11 16:47:53 +11009444void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07009445{
NeilBrown00bcb4a2010-06-01 19:37:23 +10009446 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07009447 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10009448 !test_bit(Blocked, &rdev->flags) &&
9449 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07009450 msecs_to_jiffies(5000));
9451 rdev_dec_pending(rdev, mddev);
9452}
9453EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9454
NeilBrownc6563a82012-05-21 09:27:00 +10009455void md_finish_reshape(struct mddev *mddev)
9456{
9457 /* called be personality module when reshape completes. */
9458 struct md_rdev *rdev;
9459
9460 rdev_for_each(rdev, mddev) {
9461 if (rdev->data_offset > rdev->new_data_offset)
9462 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9463 else
9464 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9465 rdev->data_offset = rdev->new_data_offset;
9466 }
9467}
9468EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10009469
Vishal Vermafc974ee2015-12-24 19:20:34 -07009470/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10009471
Vishal Vermafc974ee2015-12-24 19:20:34 -07009472/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11009473int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10009474 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009475{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04009476 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10009477 int rv;
9478 if (is_new)
9479 s += rdev->new_data_offset;
9480 else
9481 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07009482 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9483 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10009484 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009485 if (test_bit(ExternalBbl, &rdev->flags))
Junxiao Bie1a86db2020-07-14 16:10:26 -07009486 sysfs_notify_dirent_safe(rdev->sysfs_unack_badblocks);
NeilBrown8bd2f0a2011-12-08 16:26:08 +11009487 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08009488 set_mask_bits(&mddev->sb_flags, 0,
9489 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10009490 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07009491 return 1;
9492 } else
9493 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10009494}
9495EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9496
NeilBrownc6563a82012-05-21 09:27:00 +10009497int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9498 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009499{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009500 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10009501 if (is_new)
9502 s += rdev->new_data_offset;
9503 else
9504 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009505 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9506 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
Junxiao Bie1a86db2020-07-14 16:10:26 -07009507 sysfs_notify_dirent_safe(rdev->sysfs_badblocks);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009508 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10009509}
9510EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9511
Adrian Bunk75c96f82005-05-05 16:16:09 -07009512static int md_notify_reboot(struct notifier_block *this,
9513 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009514{
9515 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11009516 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009517 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009518
NeilBrownc744a652012-03-19 12:46:37 +11009519 for_each_mddev(mddev, tmp) {
9520 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10009521 if (mddev->pers)
9522 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009523 if (mddev->persistent)
9524 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009525 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009526 }
NeilBrownc744a652012-03-19 12:46:37 +11009527 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009528 }
NeilBrownc744a652012-03-19 12:46:37 +11009529 /*
9530 * certain more exotic SCSI devices are known to be
9531 * volatile wrt too early system reboots. While the
9532 * right place to handle this issue is the given
9533 * driver, we do want to have a safe RAID driver ...
9534 */
9535 if (need_delay)
9536 mdelay(1000*1);
9537
Linus Torvalds1da177e2005-04-16 15:20:36 -07009538 return NOTIFY_DONE;
9539}
9540
Adrian Bunk75c96f82005-05-05 16:16:09 -07009541static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009542 .notifier_call = md_notify_reboot,
9543 .next = NULL,
9544 .priority = INT_MAX, /* before any real devices */
9545};
9546
9547static void md_geninit(void)
9548{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009549 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009550
Alexey Dobriyan97a32532020-02-03 17:37:17 -08009551 proc_create("mdstat", S_IRUGO, NULL, &mdstat_proc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009552}
9553
Adrian Bunk75c96f82005-05-05 16:16:09 -07009554static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009555{
Tejun Heoe804ac72010-10-15 15:36:08 +02009556 int ret = -ENOMEM;
9557
Tejun Heoada609e2011-01-25 14:35:54 +01009558 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009559 if (!md_wq)
9560 goto err_wq;
9561
9562 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9563 if (!md_misc_wq)
9564 goto err_misc_wq;
9565
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009566 md_rdev_misc_wq = alloc_workqueue("md_rdev_misc", 0, 0);
Guoqing Jiangcf0b9b42020-10-08 05:19:09 +02009567 if (!md_rdev_misc_wq)
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009568 goto err_rdev_misc_wq;
9569
Tejun Heoe804ac72010-10-15 15:36:08 +02009570 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9571 goto err_md;
9572
9573 if ((ret = register_blkdev(0, "mdp")) < 0)
9574 goto err_mdp;
9575 mdp_major = ret;
9576
NeilBrownaf5628f2014-07-31 13:54:54 +10009577 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009578 md_probe, NULL, NULL);
9579 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009580 md_probe, NULL, NULL);
9581
Linus Torvalds1da177e2005-04-16 15:20:36 -07009582 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009583 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009584
9585 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009587
Tejun Heoe804ac72010-10-15 15:36:08 +02009588err_mdp:
9589 unregister_blkdev(MD_MAJOR, "md");
9590err_md:
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009591 destroy_workqueue(md_rdev_misc_wq);
9592err_rdev_misc_wq:
Tejun Heoe804ac72010-10-15 15:36:08 +02009593 destroy_workqueue(md_misc_wq);
9594err_misc_wq:
9595 destroy_workqueue(md_wq);
9596err_wq:
9597 return ret;
9598}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009599
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009600static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009601{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009602 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9603 struct md_rdev *rdev2;
9604 int role, ret;
9605 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009606
Guoqing Jiang818da592017-03-01 16:42:40 +08009607 /*
9608 * If size is changed in another node then we need to
9609 * do resize as well.
9610 */
9611 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9612 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9613 if (ret)
9614 pr_info("md-cluster: resize failed\n");
9615 else
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009616 md_bitmap_update_sb(mddev->bitmap);
Guoqing Jiang818da592017-03-01 16:42:40 +08009617 }
9618
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009619 /* Check for change of roles in the active devices */
9620 rdev_for_each(rdev2, mddev) {
9621 if (test_bit(Faulty, &rdev2->flags))
9622 continue;
9623
9624 /* Check if the roles changed */
9625 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009626
9627 if (test_bit(Candidate, &rdev2->flags)) {
9628 if (role == 0xfffe) {
9629 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9630 md_kick_rdev_from_array(rdev2);
9631 continue;
9632 }
9633 else
9634 clear_bit(Candidate, &rdev2->flags);
9635 }
9636
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009637 if (role != rdev2->raid_disk) {
Guoqing Jiangca1e98e2018-10-18 16:37:45 +08009638 /*
9639 * got activated except reshape is happening.
9640 */
9641 if (rdev2->raid_disk == -1 && role != 0xffff &&
9642 !(le32_to_cpu(sb->feature_map) &
9643 MD_FEATURE_RESHAPE_ACTIVE)) {
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009644 rdev2->saved_raid_disk = role;
9645 ret = remove_and_add_spares(mddev, rdev2);
9646 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11009647 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04009648 /* wakeup mddev->thread here, so array could
9649 * perform resync with the new activated disk */
9650 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9651 md_wakeup_thread(mddev->thread);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009652 }
9653 /* device faulty
9654 * We just want to do the minimum to mark the disk
9655 * as faulty. The recovery is performed by the
9656 * one who initiated the error.
9657 */
9658 if ((role == 0xfffe) || (role == 0xfffd)) {
9659 md_error(mddev, rdev2);
9660 clear_bit(Blocked, &rdev2->flags);
9661 }
9662 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009663 }
9664
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009665 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9666 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009667
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009668 /*
9669 * Since mddev->delta_disks has already updated in update_raid_disks,
9670 * so it is time to check reshape.
9671 */
9672 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9673 (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9674 /*
9675 * reshape is happening in the remote node, we need to
9676 * update reshape_position and call start_reshape.
9677 */
Christoph Hellwiged4d0a4e2019-04-04 18:56:10 +02009678 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009679 if (mddev->pers->update_reshape_pos)
9680 mddev->pers->update_reshape_pos(mddev);
9681 if (mddev->pers->start_reshape)
9682 mddev->pers->start_reshape(mddev);
9683 } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9684 mddev->reshape_position != MaxSector &&
9685 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9686 /* reshape is just done in another node. */
9687 mddev->reshape_position = MaxSector;
9688 if (mddev->pers->update_reshape_pos)
9689 mddev->pers->update_reshape_pos(mddev);
9690 }
9691
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009692 /* Finally set the event to be up to date */
9693 mddev->events = le64_to_cpu(sb->events);
9694}
9695
9696static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9697{
9698 int err;
9699 struct page *swapout = rdev->sb_page;
9700 struct mdp_superblock_1 *sb;
9701
9702 /* Store the sb page of the rdev in the swapout temporary
9703 * variable in case we err in the future
9704 */
9705 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009706 err = alloc_disk_sb(rdev);
9707 if (err == 0) {
9708 ClearPageUptodate(rdev->sb_page);
9709 rdev->sb_loaded = 0;
9710 err = super_types[mddev->major_version].
9711 load_super(rdev, NULL, mddev->minor_version);
9712 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009713 if (err < 0) {
9714 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9715 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009716 if (rdev->sb_page)
9717 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009718 rdev->sb_page = swapout;
9719 rdev->sb_loaded = 1;
9720 return err;
9721 }
9722
9723 sb = page_address(rdev->sb_page);
9724 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9725 * is not set
9726 */
9727
9728 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9729 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9730
9731 /* The other node finished recovery, call spare_active to set
9732 * device In_sync and mddev->degraded
9733 */
9734 if (rdev->recovery_offset == MaxSector &&
9735 !test_bit(In_sync, &rdev->flags) &&
9736 mddev->pers->spare_active(mddev))
Junxiao Bie1a86db2020-07-14 16:10:26 -07009737 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009738
9739 put_page(swapout);
9740 return 0;
9741}
9742
9743void md_reload_sb(struct mddev *mddev, int nr)
9744{
9745 struct md_rdev *rdev;
9746 int err;
9747
9748 /* Find the rdev */
9749 rdev_for_each_rcu(rdev, mddev) {
9750 if (rdev->desc_nr == nr)
9751 break;
9752 }
9753
9754 if (!rdev || rdev->desc_nr != nr) {
9755 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9756 return;
9757 }
9758
9759 err = read_rdev(mddev, rdev);
9760 if (err < 0)
9761 return;
9762
9763 check_sb_changes(mddev, rdev);
9764
9765 /* Read all rdev's to update recovery_offset */
Guoqing Jiang0ea99242018-04-09 17:01:21 +08009766 rdev_for_each_rcu(rdev, mddev) {
9767 if (!test_bit(Faulty, &rdev->flags))
9768 read_rdev(mddev, rdev);
9769 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009770}
9771EXPORT_SYMBOL(md_reload_sb);
9772
Linus Torvalds1da177e2005-04-16 15:20:36 -07009773#ifndef MODULE
9774
9775/*
9776 * Searches all registered partitions for autorun RAID arrays
9777 * at boot time.
9778 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009779
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009780static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009781static LIST_HEAD(all_detected_devices);
9782struct detected_devices_node {
9783 struct list_head list;
9784 dev_t dev;
9785};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009786
9787void md_autodetect_dev(dev_t dev)
9788{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009789 struct detected_devices_node *node_detected_dev;
9790
9791 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9792 if (node_detected_dev) {
9793 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009794 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009795 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009796 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009798}
9799
Christoph Hellwigd82fa812020-06-06 15:00:24 +02009800void md_autostart_arrays(int part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009801{
NeilBrown3cb03002011-10-11 16:45:26 +11009802 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009803 struct detected_devices_node *node_detected_dev;
9804 dev_t dev;
9805 int i_scanned, i_passed;
9806
9807 i_scanned = 0;
9808 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009809
NeilBrown9d487392016-11-02 14:16:49 +11009810 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009811
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009812 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009813 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9814 i_scanned++;
9815 node_detected_dev = list_entry(all_detected_devices.next,
9816 struct detected_devices_node, list);
9817 list_del(&node_detected_dev->list);
9818 dev = node_detected_dev->dev;
9819 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009820 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009821 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009822 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009823 if (IS_ERR(rdev))
9824 continue;
9825
NeilBrown403df472014-09-30 15:52:29 +10009826 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009827 continue;
NeilBrown403df472014-09-30 15:52:29 +10009828
NeilBrownd0fae182008-03-04 14:29:31 -08009829 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009830 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009831 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009832 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009833 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009834
NeilBrown9d487392016-11-02 14:16:49 +11009835 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009836
9837 autorun_devices(part);
9838}
9839
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009840#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009841
9842static __exit void md_exit(void)
9843{
NeilBrownfd01b882011-10-11 16:47:53 +11009844 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009845 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009846 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009847
NeilBrownaf5628f2014-07-31 13:54:54 +10009848 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009849 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009851 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009852 unregister_blkdev(mdp_major, "mdp");
9853 unregister_reboot_notifier(&md_notifier);
9854 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009855
9856 /* We cannot unload the modules while some process is
9857 * waiting for us in select() or poll() - wake them up
9858 */
9859 md_unloading = 1;
9860 while (waitqueue_active(&md_event_waiters)) {
9861 /* not safe to leave yet */
9862 wake_up(&md_event_waiters);
9863 msleep(delay);
9864 delay += delay;
9865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009866 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009867
NeilBrown29ac4aa2008-02-06 01:39:58 -08009868 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009869 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009870 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009871 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009872 /*
9873 * for_each_mddev() will call mddev_put() at the end of each
9874 * iteration. As the mddev is now fully clear, this will
9875 * schedule the mddev for destruction by a workqueue, and the
9876 * destroy_workqueue() below will wait for that to complete.
9877 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009878 }
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009879 destroy_workqueue(md_rdev_misc_wq);
Tejun Heoe804ac72010-10-15 15:36:08 +02009880 destroy_workqueue(md_misc_wq);
9881 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009882}
9883
Dan Williams685784a2007-07-09 11:56:42 -07009884subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009885module_exit(md_exit)
9886
Kees Cooke4dca7b2017-10-17 19:04:42 -07009887static int get_ro(char *buffer, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009888{
Xiongfeng Wang3f999802020-05-11 16:23:25 +08009889 return sprintf(buffer, "%d\n", start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009890}
Kees Cooke4dca7b2017-10-17 19:04:42 -07009891static int set_ro(const char *val, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009892{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009893 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009894}
9895
NeilBrown80ca3a42006-07-10 04:44:18 -07009896module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9897module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009898module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009899module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009900
Linus Torvalds1da177e2005-04-16 15:20:36 -07009901MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009902MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009903MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009904MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);