blob: 5e8f772ab11723834963683432018b979c91f5eb [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110072static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#endif
74
NeilBrown01f96c02011-09-21 15:30:20 +100075/* pers_list is a list of registered personalities protected
76 * by pers_lock.
77 * pers_lock does extra service to protect accesses to
78 * mddev->thread when the mutex cannot be held.
79 */
NeilBrown2604b702006-01-06 00:20:36 -080080static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static DEFINE_SPINLOCK(pers_lock);
82
Kent Overstreet28dec872018-06-07 20:52:54 -040083static struct kobj_type md_ktype;
84
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050085struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050086EXPORT_SYMBOL(md_cluster_ops);
Christoph Hellwig2b598ee2019-04-04 18:56:14 +020087static struct module *md_cluster_mod;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050088
Bernd Schubert90b08712008-05-23 13:04:38 -070089static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020090static struct workqueue_struct *md_wq;
91static struct workqueue_struct *md_misc_wq;
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +020092static struct workqueue_struct *md_rdev_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070093
NeilBrown746d3202013-04-24 11:42:41 +100094static int remove_and_add_spares(struct mddev *mddev,
95 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110096static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
Robert Becker1e509152009-12-14 12:49:58 +110099 * Default number of read corrections we'll attempt on an rdev
100 * before ejecting it from the array. We divide the read error
101 * count by 2 for every hour elapsed between read errors.
102 */
103#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
Zhao Heming7c9d5c52020-07-21 02:08:52 +0800104/* Default safemode delay: 200 msec */
105#define DEFAULT_SAFEMODE_DELAY ((200 * HZ)/1000 +1)
Robert Becker1e509152009-12-14 12:49:58 +1100106/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
108 * is 1000 KB/sec, so the extra system load does not show up that much.
109 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -0700110 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * subsystem is idle. There is also an 'absolute maximum' reconstruction
112 * speed limit - in case reconstruction slows down your system despite
113 * idle IO detection.
114 *
115 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -0800116 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
118
119static int sysctl_speed_limit_min = 1000;
120static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100121static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800122{
123 return mddev->sync_speed_min ?
124 mddev->sync_speed_min : sysctl_speed_limit_min;
125}
126
NeilBrownfd01b882011-10-11 16:47:53 +1100127static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800128{
129 return mddev->sync_speed_max ?
130 mddev->sync_speed_max : sysctl_speed_limit_max;
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100133static void rdev_uninit_serial(struct md_rdev *rdev)
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800134{
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100135 if (!test_and_clear_bit(CollisionCheck, &rdev->flags))
136 return;
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800137
Guoqing Jiang025471f2019-12-23 10:49:01 +0100138 kvfree(rdev->serial);
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100139 rdev->serial = NULL;
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800140}
141
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100142static void rdevs_uninit_serial(struct mddev *mddev)
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100143{
144 struct md_rdev *rdev;
145
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100146 rdev_for_each(rdev, mddev)
147 rdev_uninit_serial(rdev);
148}
149
150static int rdev_init_serial(struct md_rdev *rdev)
151{
Guoqing Jiang025471f2019-12-23 10:49:01 +0100152 /* serial_nums equals with BARRIER_BUCKETS_NR */
153 int i, serial_nums = 1 << ((PAGE_SHIFT - ilog2(sizeof(atomic_t))));
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100154 struct serial_in_rdev *serial = NULL;
155
156 if (test_bit(CollisionCheck, &rdev->flags))
157 return 0;
158
Guoqing Jiang025471f2019-12-23 10:49:01 +0100159 serial = kvmalloc(sizeof(struct serial_in_rdev) * serial_nums,
160 GFP_KERNEL);
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100161 if (!serial)
162 return -ENOMEM;
163
Guoqing Jiang025471f2019-12-23 10:49:01 +0100164 for (i = 0; i < serial_nums; i++) {
165 struct serial_in_rdev *serial_tmp = &serial[i];
166
167 spin_lock_init(&serial_tmp->serial_lock);
168 serial_tmp->serial_rb = RB_ROOT_CACHED;
169 init_waitqueue_head(&serial_tmp->serial_io_wait);
170 }
171
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100172 rdev->serial = serial;
173 set_bit(CollisionCheck, &rdev->flags);
174
175 return 0;
176}
177
178static int rdevs_init_serial(struct mddev *mddev)
179{
180 struct md_rdev *rdev;
181 int ret = 0;
182
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100183 rdev_for_each(rdev, mddev) {
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100184 ret = rdev_init_serial(rdev);
185 if (ret)
186 break;
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100187 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100188
189 /* Free all resources if pool is not existed */
190 if (ret && !mddev->serial_info_pool)
191 rdevs_uninit_serial(mddev);
192
193 return ret;
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100194}
195
Guoqing Jiang963c5552019-06-14 17:10:36 +0800196/*
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100197 * rdev needs to enable serial stuffs if it meets the conditions:
198 * 1. it is multi-queue device flaged with writemostly.
199 * 2. the write-behind mode is enabled.
200 */
201static int rdev_need_serial(struct md_rdev *rdev)
202{
203 return (rdev && rdev->mddev->bitmap_info.max_write_behind > 0 &&
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200204 rdev->bdev->bd_disk->queue->nr_hw_queues != 1 &&
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100205 test_bit(WriteMostly, &rdev->flags));
206}
207
208/*
209 * Init resource for rdev(s), then create serial_info_pool if:
210 * 1. rdev is the first device which return true from rdev_enable_serial.
211 * 2. rdev is NULL, means we want to enable serialization for all rdevs.
Guoqing Jiang963c5552019-06-14 17:10:36 +0800212 */
Guoqing Jiang404659c2019-12-23 10:48:53 +0100213void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev,
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100214 bool is_suspend)
Guoqing Jiang963c5552019-06-14 17:10:36 +0800215{
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100216 int ret = 0;
217
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100218 if (rdev && !rdev_need_serial(rdev) &&
219 !test_bit(CollisionCheck, &rdev->flags))
Guoqing Jiang963c5552019-06-14 17:10:36 +0800220 return;
221
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100222 if (!is_suspend)
223 mddev_suspend(mddev);
224
225 if (!rdev)
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100226 ret = rdevs_init_serial(mddev);
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100227 else
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100228 ret = rdev_init_serial(rdev);
229 if (ret)
230 goto abort;
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100231
Guoqing Jiang404659c2019-12-23 10:48:53 +0100232 if (mddev->serial_info_pool == NULL) {
Coly Li3024ba22020-04-09 22:17:23 +0800233 /*
234 * already in memalloc noio context by
235 * mddev_suspend()
236 */
Guoqing Jiang404659c2019-12-23 10:48:53 +0100237 mddev->serial_info_pool =
238 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
239 sizeof(struct serial_info));
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100240 if (!mddev->serial_info_pool) {
241 rdevs_uninit_serial(mddev);
Guoqing Jiang404659c2019-12-23 10:48:53 +0100242 pr_err("can't alloc memory pool for serialization\n");
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100243 }
Guoqing Jiang963c5552019-06-14 17:10:36 +0800244 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100245
246abort:
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100247 if (!is_suspend)
248 mddev_resume(mddev);
Guoqing Jiang963c5552019-06-14 17:10:36 +0800249}
Guoqing Jiang963c5552019-06-14 17:10:36 +0800250
251/*
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100252 * Free resource from rdev(s), and destroy serial_info_pool under conditions:
253 * 1. rdev is the last device flaged with CollisionCheck.
254 * 2. when bitmap is destroyed while policy is not enabled.
255 * 3. for disable policy, the pool is destroyed only when no rdev needs it.
Guoqing Jiang963c5552019-06-14 17:10:36 +0800256 */
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100257void mddev_destroy_serial_pool(struct mddev *mddev, struct md_rdev *rdev,
258 bool is_suspend)
Guoqing Jiang963c5552019-06-14 17:10:36 +0800259{
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100260 if (rdev && !test_bit(CollisionCheck, &rdev->flags))
Guoqing Jiang963c5552019-06-14 17:10:36 +0800261 return;
262
Guoqing Jiang404659c2019-12-23 10:48:53 +0100263 if (mddev->serial_info_pool) {
Guoqing Jiang963c5552019-06-14 17:10:36 +0800264 struct md_rdev *temp;
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100265 int num = 0; /* used to track if other rdevs need the pool */
Guoqing Jiang963c5552019-06-14 17:10:36 +0800266
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100267 if (!is_suspend)
268 mddev_suspend(mddev);
269 rdev_for_each(temp, mddev) {
270 if (!rdev) {
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100271 if (!mddev->serialize_policy ||
272 !rdev_need_serial(temp))
273 rdev_uninit_serial(temp);
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100274 else
275 num++;
276 } else if (temp != rdev &&
277 test_bit(CollisionCheck, &temp->flags))
Guoqing Jiang963c5552019-06-14 17:10:36 +0800278 num++;
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100279 }
280
281 if (rdev)
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100282 rdev_uninit_serial(rdev);
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100283
284 if (num)
285 pr_info("The mempool could be used by other devices\n");
286 else {
Guoqing Jiang404659c2019-12-23 10:48:53 +0100287 mempool_destroy(mddev->serial_info_pool);
288 mddev->serial_info_pool = NULL;
Guoqing Jiang963c5552019-06-14 17:10:36 +0800289 }
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100290 if (!is_suspend)
291 mddev_resume(mddev);
Guoqing Jiang963c5552019-06-14 17:10:36 +0800292 }
293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295static struct ctl_table_header *raid_table_header;
296
Joe Perches82592c32013-11-14 15:16:18 +1100297static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 .procname = "speed_limit_min",
300 .data = &sysctl_speed_limit_min,
301 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700302 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800303 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 },
305 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 .procname = "speed_limit_max",
307 .data = &sysctl_speed_limit_max,
308 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700309 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800310 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800312 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313};
314
Joe Perches82592c32013-11-14 15:16:18 +1100315static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 .procname = "raid",
318 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700319 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 .child = raid_table,
321 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800322 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323};
324
Joe Perches82592c32013-11-14 15:16:18 +1100325static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 .procname = "dev",
328 .maxlen = 0,
329 .mode = 0555,
330 .child = raid_dir_table,
331 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800332 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333};
334
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700335static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
NeilBrownf91de922005-11-08 21:39:36 -0800337static int start_readonly;
338
NeilBrown78b63502017-04-12 16:26:13 +1000339/*
340 * The original mechanism for creating an md device is to create
341 * a device node in /dev and to open it. This causes races with device-close.
342 * The preferred method is to write to the "new_array" module parameter.
343 * This can avoid races.
344 * Setting create_on_open to false disables the original mechanism
345 * so all the races disappear.
346 */
347static bool create_on_open = true;
348
NeilBrowna167f662010-10-26 18:31:13 +1100349struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100350 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100351{
Kent Overstreetafeee512018-05-20 18:25:52 -0400352 if (!mddev || !bioset_initialized(&mddev->bio_set))
NeilBrowna167f662010-10-26 18:31:13 +1100353 return bio_alloc(gfp_mask, nr_iovecs);
354
Marcos Paulo de Souza62516912019-01-14 06:31:56 -0700355 return bio_alloc_bioset(gfp_mask, nr_iovecs, &mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100356}
357EXPORT_SYMBOL_GPL(bio_alloc_mddev);
358
NeilBrown5a850712017-06-21 09:12:21 +1000359static struct bio *md_bio_alloc_sync(struct mddev *mddev)
360{
Kent Overstreetafeee512018-05-20 18:25:52 -0400361 if (!mddev || !bioset_initialized(&mddev->sync_set))
NeilBrown5a850712017-06-21 09:12:21 +1000362 return bio_alloc(GFP_NOIO, 1);
363
Kent Overstreetafeee512018-05-20 18:25:52 -0400364 return bio_alloc_bioset(GFP_NOIO, 1, &mddev->sync_set);
NeilBrown5a850712017-06-21 09:12:21 +1000365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/*
NeilBrownd7603b72006-01-06 00:20:30 -0800368 * We have a system wide 'event count' that is incremented
369 * on any 'interesting' event, and readers of /proc/mdstat
370 * can use 'poll' or 'select' to find out when the event
371 * count increases.
372 *
373 * Events are:
374 * start array, stop array, error, add device, remove device,
375 * start build, activate spare
376 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800377static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800378static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100379void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800380{
381 atomic_inc(&md_event_count);
382 wake_up(&md_event_waiters);
383}
NeilBrown29269552006-03-27 01:18:10 -0800384EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800385
386/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 * Enables to iterate over all existing md arrays
388 * all_mddevs_lock protects this list.
389 */
390static LIST_HEAD(all_mddevs);
391static DEFINE_SPINLOCK(all_mddevs_lock);
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/*
394 * iterates through all used mddevs in the system.
395 * We take care to grab the all_mddevs_lock whenever navigating
396 * the list, and to always hold a refcount when unlocked.
397 * Any code which breaks out of this loop while own
398 * a reference to the current mddev and must mddev_put it.
399 */
NeilBrownfd01b882011-10-11 16:47:53 +1100400#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000402 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100403 _tmp = all_mddevs.next; \
404 _mddev = NULL;}); \
405 ({ if (_tmp != &all_mddevs) \
406 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100408 if (_mddev) mddev_put(_mddev); \
409 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
410 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100412 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 )
414
NeilBrown409c57f2009-03-31 14:39:39 +1100415/* Rather than calling directly into the personality make_request function,
416 * IO requests come here first so that we can check if the device is
417 * being suspended pending a reconfiguration.
418 * We hold a refcount over the call to ->make_request. By the time that
419 * call has finished, the bio has been linked into some internal structure
420 * and so is visible to ->quiesce(), so we don't need the refcount any more.
421 */
NeilBrownb3143b92017-10-17 13:46:43 +1100422static bool is_suspended(struct mddev *mddev, struct bio *bio)
423{
424 if (mddev->suspended)
425 return true;
426 if (bio_data_dir(bio) != WRITE)
427 return false;
428 if (mddev->suspend_lo >= mddev->suspend_hi)
429 return false;
430 if (bio->bi_iter.bi_sector >= mddev->suspend_hi)
431 return false;
432 if (bio_end_sector(bio) < mddev->suspend_lo)
433 return false;
434 return true;
435}
436
Shaohua Li393debc2017-09-21 10:23:35 -0700437void md_handle_request(struct mddev *mddev, struct bio *bio)
438{
439check_suspended:
440 rcu_read_lock();
NeilBrownb3143b92017-10-17 13:46:43 +1100441 if (is_suspended(mddev, bio)) {
Shaohua Li393debc2017-09-21 10:23:35 -0700442 DEFINE_WAIT(__wait);
443 for (;;) {
444 prepare_to_wait(&mddev->sb_wait, &__wait,
445 TASK_UNINTERRUPTIBLE);
NeilBrownb3143b92017-10-17 13:46:43 +1100446 if (!is_suspended(mddev, bio))
Shaohua Li393debc2017-09-21 10:23:35 -0700447 break;
448 rcu_read_unlock();
449 schedule();
450 rcu_read_lock();
451 }
452 finish_wait(&mddev->sb_wait, &__wait);
453 }
454 atomic_inc(&mddev->active_io);
455 rcu_read_unlock();
456
457 if (!mddev->pers->make_request(mddev, bio)) {
458 atomic_dec(&mddev->active_io);
459 wake_up(&mddev->sb_wait);
460 goto check_suspended;
461 }
462
463 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
464 wake_up(&mddev->sb_wait);
465}
466EXPORT_SYMBOL(md_handle_request);
467
Artur Paszkiewicz41d2d842020-07-03 11:13:09 +0200468struct md_io {
469 struct mddev *mddev;
470 bio_end_io_t *orig_bi_end_io;
471 void *orig_bi_private;
472 unsigned long start_time;
473};
474
475static void md_end_io(struct bio *bio)
476{
477 struct md_io *md_io = bio->bi_private;
478 struct mddev *mddev = md_io->mddev;
479
480 disk_end_io_acct(mddev->gendisk, bio_op(bio), md_io->start_time);
481
482 bio->bi_end_io = md_io->orig_bi_end_io;
483 bio->bi_private = md_io->orig_bi_private;
484
485 mempool_free(md_io, &mddev->md_io_pool);
486
487 if (bio->bi_end_io)
488 bio->bi_end_io(bio);
489}
490
Christoph Hellwigc62b37d2020-07-01 10:59:43 +0200491static blk_qc_t md_submit_bio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
NeilBrown49077322010-03-25 16:20:56 +1100493 const int rw = bio_data_dir(bio);
Christoph Hellwige4fc5a72020-05-08 18:15:14 +0200494 struct mddev *mddev = bio->bi_disk->private_data;
NeilBrown49077322010-03-25 16:20:56 +1100495
Colin Ian King9a5a8592020-07-02 12:35:02 +0100496 if (mddev == NULL || mddev->pers == NULL) {
497 bio_io_error(bio);
498 return BLK_QC_T_NONE;
499 }
500
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -0300501 if (unlikely(test_bit(MD_BROKEN, &mddev->flags)) && (rw == WRITE)) {
502 bio_io_error(bio);
503 return BLK_QC_T_NONE;
504 }
505
Christoph Hellwigf695ca32020-07-01 10:59:39 +0200506 blk_queue_split(&bio);
Kent Overstreet54efd502015-04-23 22:37:18 -0700507
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100508 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200509 if (bio_sectors(bio) != 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200510 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200511 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700512 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100513 }
NeilBrown49077322010-03-25 16:20:56 +1100514
Artur Paszkiewicz41d2d842020-07-03 11:13:09 +0200515 if (bio->bi_end_io != md_end_io) {
516 struct md_io *md_io;
517
518 md_io = mempool_alloc(&mddev->md_io_pool, GFP_NOIO);
519 md_io->mddev = mddev;
520 md_io->orig_bi_end_io = bio->bi_end_io;
521 md_io->orig_bi_private = bio->bi_private;
522
523 bio->bi_end_io = md_end_io;
524 bio->bi_private = md_io;
525
526 md_io->start_time = disk_start_io_acct(mddev->gendisk,
527 bio_sectors(bio),
528 bio_op(bio));
529 }
530
Shaohua Li9c573de2016-04-25 16:52:38 -0700531 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600532 bio->bi_opf &= ~REQ_NOMERGE;
Shaohua Li393debc2017-09-21 10:23:35 -0700533
534 md_handle_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100535
Jens Axboedece1632015-11-05 10:41:16 -0700536 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
NeilBrown9e35b992010-04-06 14:23:02 +1000539/* mddev_suspend makes sure no new requests are submitted
540 * to the device, and that any requests that have been submitted
541 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100542 * Once mddev_detach() is called and completes, the module will be
543 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000544 */
NeilBrownfd01b882011-10-11 16:47:53 +1100545void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100546{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200547 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
NeilBrown4d5324f2017-10-19 12:17:16 +1100548 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100549 if (mddev->suspended++)
550 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100551 synchronize_rcu();
NeilBrowncc27b0c2017-06-05 16:49:39 +1000552 wake_up(&mddev->sb_wait);
NeilBrown35bfc522017-10-17 13:46:43 +1100553 set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
554 smp_mb__after_atomic();
NeilBrown409c57f2009-03-31 14:39:39 +1100555 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
556 mddev->pers->quiesce(mddev, 1);
NeilBrown35bfc522017-10-17 13:46:43 +1100557 clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags);
558 wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags));
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500559
560 del_timer_sync(&mddev->safemode_timer);
Coly Li78f57ef2020-04-09 22:17:20 +0800561 /* restrict memory reclaim I/O during raid array is suspend */
562 mddev->noio_flag = memalloc_noio_save();
NeilBrown409c57f2009-03-31 14:39:39 +1100563}
NeilBrown390ee602010-06-01 19:37:27 +1000564EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100565
NeilBrownfd01b882011-10-11 16:47:53 +1100566void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100567{
Coly Li78f57ef2020-04-09 22:17:20 +0800568 /* entred the memalloc scope from mddev_suspend() */
569 memalloc_noio_restore(mddev->noio_flag);
NeilBrown4d5324f2017-10-19 12:17:16 +1100570 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100571 if (--mddev->suspended)
572 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100573 wake_up(&mddev->sb_wait);
574 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500575
Jonathan Brassow47525e52012-05-22 13:55:29 +1000576 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500577 md_wakeup_thread(mddev->thread);
578 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100579}
NeilBrown390ee602010-06-01 19:37:27 +1000580EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100581
NeilBrownfd01b882011-10-11 16:47:53 +1100582int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000583{
NeilBrown5c675f82014-12-15 12:56:56 +1100584 struct md_personality *pers = mddev->pers;
585 int ret = 0;
586
587 rcu_read_lock();
588 if (mddev->suspended)
589 ret = 1;
590 else if (pers && pers->congested)
591 ret = pers->congested(mddev, bits);
592 rcu_read_unlock();
593 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000594}
NeilBrown5c675f82014-12-15 12:56:56 +1100595EXPORT_SYMBOL_GPL(mddev_congested);
596static int md_congested(void *data, int bits)
597{
598 struct mddev *mddev = data;
599 return mddev_congested(mddev, bits);
600}
NeilBrown3fa841d2009-09-23 18:10:29 +1000601
NeilBrowna2826aa2009-12-14 12:49:49 +1100602/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200603 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100604 */
NeilBrown4bc034d2019-03-29 10:46:16 -0700605
606static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100607{
NeilBrown4bc034d2019-03-29 10:46:16 -0700608 struct md_rdev *rdev = bio->bi_private;
609 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100610
611 rdev_dec_pending(rdev, mddev);
612
NeilBrown4bc034d2019-03-29 10:46:16 -0700613 if (atomic_dec_and_test(&mddev->flush_pending)) {
614 /* The pre-request flush has finished */
615 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100616 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700617 bio_put(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100618}
619
NeilBrown4bc034d2019-03-29 10:46:16 -0700620static void md_submit_flush_data(struct work_struct *ws);
621
622static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100623{
NeilBrown4bc034d2019-03-29 10:46:16 -0700624 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100625 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100626
NeilBrown2bc13b82019-03-29 10:46:17 -0700627 mddev->start_flush = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700628 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
629 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100630 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100631 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100632 if (rdev->raid_disk >= 0 &&
633 !test_bit(Faulty, &rdev->flags)) {
634 /* Take two references, one is dropped
635 * when request finishes, one after
636 * we reclaim rcu_read_lock
637 */
638 struct bio *bi;
639 atomic_inc(&rdev->nr_pending);
640 atomic_inc(&rdev->nr_pending);
641 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000642 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Xiao Ni5a409b42018-05-21 11:49:54 +0800643 bi->bi_end_io = md_end_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700644 bi->bi_private = rdev;
645 bio_set_dev(bi, rdev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600646 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
NeilBrown4bc034d2019-03-29 10:46:16 -0700647 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500648 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100649 rcu_read_lock();
650 rdev_dec_pending(rdev, mddev);
651 }
652 rcu_read_unlock();
NeilBrown4bc034d2019-03-29 10:46:16 -0700653 if (atomic_dec_and_test(&mddev->flush_pending))
654 queue_work(md_wq, &mddev->flush_work);
655}
NeilBrowna2826aa2009-12-14 12:49:49 +1100656
NeilBrown4bc034d2019-03-29 10:46:16 -0700657static void md_submit_flush_data(struct work_struct *ws)
658{
659 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
660 struct bio *bio = mddev->flush_bio;
661
662 /*
663 * must reset flush_bio before calling into md_handle_request to avoid a
664 * deadlock, because other bios passed md_handle_request suspend check
665 * could wait for this and below md_handle_request could wait for those
666 * bios because of suspend check
667 */
NeilBrown2bc13b82019-03-29 10:46:17 -0700668 mddev->last_flush = mddev->start_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700669 mddev->flush_bio = NULL;
670 wake_up(&mddev->sb_wait);
671
672 if (bio->bi_iter.bi_size == 0) {
673 /* an empty barrier - all done */
674 bio_endio(bio);
675 } else {
676 bio->bi_opf &= ~REQ_PREFLUSH;
677 md_handle_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100678 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100679}
NeilBrown4bc034d2019-03-29 10:46:16 -0700680
David Jeffery775d7832019-09-16 13:15:14 -0400681/*
682 * Manages consolidation of flushes and submitting any flushes needed for
683 * a bio with REQ_PREFLUSH. Returns true if the bio is finished or is
684 * being finished in another context. Returns false if the flushing is
685 * complete but still needs the I/O portion of the bio to be processed.
686 */
687bool md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrown4bc034d2019-03-29 10:46:16 -0700688{
NeilBrown2bc13b82019-03-29 10:46:17 -0700689 ktime_t start = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700690 spin_lock_irq(&mddev->lock);
691 wait_event_lock_irq(mddev->sb_wait,
NeilBrown2bc13b82019-03-29 10:46:17 -0700692 !mddev->flush_bio ||
693 ktime_after(mddev->last_flush, start),
NeilBrown4bc034d2019-03-29 10:46:16 -0700694 mddev->lock);
NeilBrown2bc13b82019-03-29 10:46:17 -0700695 if (!ktime_after(mddev->last_flush, start)) {
696 WARN_ON(mddev->flush_bio);
697 mddev->flush_bio = bio;
698 bio = NULL;
699 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700700 spin_unlock_irq(&mddev->lock);
701
NeilBrown2bc13b82019-03-29 10:46:17 -0700702 if (!bio) {
703 INIT_WORK(&mddev->flush_work, submit_flushes);
704 queue_work(md_wq, &mddev->flush_work);
705 } else {
706 /* flush was performed for some other bio while we waited. */
707 if (bio->bi_iter.bi_size == 0)
708 /* an empty barrier - all done */
709 bio_endio(bio);
710 else {
711 bio->bi_opf &= ~REQ_PREFLUSH;
David Jeffery775d7832019-09-16 13:15:14 -0400712 return false;
NeilBrown2bc13b82019-03-29 10:46:17 -0700713 }
714 }
David Jeffery775d7832019-09-16 13:15:14 -0400715 return true;
NeilBrown4bc034d2019-03-29 10:46:16 -0700716}
Tejun Heoe9c74692010-09-03 11:56:18 +0200717EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100718
NeilBrownfd01b882011-10-11 16:47:53 +1100719static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 atomic_inc(&mddev->active);
722 return mddev;
723}
724
Dan Williams5fd3a172009-03-04 00:57:25 -0700725static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100726
NeilBrownfd01b882011-10-11 16:47:53 +1100727static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
730 return;
NeilBrownd3374822009-01-09 08:31:10 +1100731 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100732 mddev->ctime == 0 && !mddev->hold_active) {
733 /* Array is not configured at all, and not held active,
734 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100735 list_del_init(&mddev->all_mddevs);
Kent Overstreet28dec872018-06-07 20:52:54 -0400736
737 /*
738 * Call queue_work inside the spinlock so that
739 * flush_workqueue() after mddev_find will succeed in waiting
740 * for the work to be done.
741 */
742 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
743 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100744 }
745 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
Kees Cook8376d3c2017-10-16 17:01:48 -0700748static void md_safemode_timeout(struct timer_list *t);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400749
NeilBrownfd01b882011-10-11 16:47:53 +1100750void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100751{
Kent Overstreet28dec872018-06-07 20:52:54 -0400752 kobject_init(&mddev->kobj, &md_ktype);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100753 mutex_init(&mddev->open_mutex);
754 mutex_init(&mddev->reconfig_mutex);
755 mutex_init(&mddev->bitmap_info.mutex);
756 INIT_LIST_HEAD(&mddev->disks);
757 INIT_LIST_HEAD(&mddev->all_mddevs);
Kees Cook8376d3c2017-10-16 17:01:48 -0700758 timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100759 atomic_set(&mddev->active, 1);
760 atomic_set(&mddev->openers, 0);
761 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100762 spin_lock_init(&mddev->lock);
NeilBrown4bc034d2019-03-29 10:46:16 -0700763 atomic_set(&mddev->flush_pending, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100764 init_waitqueue_head(&mddev->sb_wait);
765 init_waitqueue_head(&mddev->recovery_wait);
766 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000767 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500768 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100769 mddev->resync_min = 0;
770 mddev->resync_max = MaxSector;
771 mddev->level = LEVEL_NONE;
772}
NeilBrown390ee602010-06-01 19:37:27 +1000773EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100774
NeilBrownf72ffdd2014-09-30 14:23:59 +1000775static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
NeilBrownfd01b882011-10-11 16:47:53 +1100777 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
NeilBrown8f5f02c2011-02-16 13:58:51 +1100779 if (unit && MAJOR(unit) != MD_MAJOR)
780 unit &= ~((1<<MdpMinorShift)-1);
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 retry:
783 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
NeilBrownefeb53c2009-01-09 08:31:10 +1100785 if (unit) {
786 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
787 if (mddev->unit == unit) {
788 mddev_get(mddev);
789 spin_unlock(&all_mddevs_lock);
790 kfree(new);
791 return mddev;
792 }
793
794 if (new) {
795 list_add(&new->all_mddevs, &all_mddevs);
796 spin_unlock(&all_mddevs_lock);
797 new->hold_active = UNTIL_IOCTL;
798 return new;
799 }
800 } else if (new) {
801 /* find an unused unit number */
802 static int next_minor = 512;
803 int start = next_minor;
804 int is_free = 0;
805 int dev = 0;
806 while (!is_free) {
807 dev = MKDEV(MD_MAJOR, next_minor);
808 next_minor++;
809 if (next_minor > MINORMASK)
810 next_minor = 0;
811 if (next_minor == start) {
812 /* Oh dear, all in use. */
813 spin_unlock(&all_mddevs_lock);
814 kfree(new);
815 return NULL;
816 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000817
NeilBrownefeb53c2009-01-09 08:31:10 +1100818 is_free = 1;
819 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
820 if (mddev->unit == dev) {
821 is_free = 0;
822 break;
823 }
824 }
825 new->unit = dev;
826 new->md_minor = MINOR(dev);
827 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 list_add(&new->all_mddevs, &all_mddevs);
829 spin_unlock(&all_mddevs_lock);
830 return new;
831 }
832 spin_unlock(&all_mddevs_lock);
833
NeilBrown9ffae0c2006-01-06 00:20:32 -0800834 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (!new)
836 return NULL;
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 new->unit = unit;
839 if (MAJOR(unit) == MD_MAJOR)
840 new->md_minor = MINOR(unit);
841 else
842 new->md_minor = MINOR(unit) >> MdpMinorShift;
843
NeilBrownfafd7fb2010-04-01 15:55:30 +1100844 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 goto retry;
847}
848
NeilBrownb6eb1272010-04-15 10:13:47 +1000849static struct attribute_group md_redundancy_group;
850
NeilBrown5c47daf2014-12-15 12:57:01 +1100851void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
NeilBrowna64c8762010-04-14 17:15:37 +1000853 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000854 /* These cannot be removed under reconfig_mutex as
855 * an access to the files will try to take reconfig_mutex
856 * while holding the file unremovable, which leads to
857 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000858 * So hold set sysfs_active while the remove in happeing,
859 * and anything else which might set ->to_remove or my
860 * otherwise change the sysfs namespace will fail with
861 * -EBUSY if sysfs_active is still set.
862 * We set sysfs_active under reconfig_mutex and elsewhere
863 * test it under the same mutex to ensure its correct value
864 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000865 */
NeilBrowna64c8762010-04-14 17:15:37 +1000866 struct attribute_group *to_remove = mddev->to_remove;
867 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000868 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000869 mutex_unlock(&mddev->reconfig_mutex);
870
NeilBrown00bcb4a2010-06-01 19:37:23 +1000871 if (mddev->kobj.sd) {
872 if (to_remove != &md_redundancy_group)
873 sysfs_remove_group(&mddev->kobj, to_remove);
874 if (mddev->pers == NULL ||
875 mddev->pers->sync_request == NULL) {
876 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
877 if (mddev->sysfs_action)
878 sysfs_put(mddev->sysfs_action);
879 mddev->sysfs_action = NULL;
880 }
NeilBrowna64c8762010-04-14 17:15:37 +1000881 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000882 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000883 } else
884 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Chris Dunlop751e67c2011-10-19 16:48:26 +1100886 /* As we've dropped the mutex we need a spinlock to
887 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000888 */
889 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700890 md_wakeup_thread(mddev->thread);
NeilBrown4d5324f2017-10-19 12:17:16 +1100891 wake_up(&mddev->sb_wait);
NeilBrown01f96c02011-09-21 15:30:20 +1000892 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
NeilBrown5c47daf2014-12-15 12:57:01 +1100894EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500896struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100897{
898 struct md_rdev *rdev;
899
900 rdev_for_each_rcu(rdev, mddev)
901 if (rdev->desc_nr == nr)
902 return rdev;
903
904 return NULL;
905}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500906EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100907
908static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
NeilBrown3cb03002011-10-11 16:45:26 +1100910 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
NeilBrowndafb20f2012-03-19 12:46:39 +1100912 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (rdev->bdev->bd_dev == dev)
914 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return NULL;
917}
918
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100919struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev)
NeilBrown1ca69c42012-10-11 13:37:33 +1100920{
921 struct md_rdev *rdev;
922
923 rdev_for_each_rcu(rdev, mddev)
924 if (rdev->bdev->bd_dev == dev)
925 return rdev;
926
927 return NULL;
928}
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100929EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100930
NeilBrown84fc4b52011-10-11 16:49:58 +1100931static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800932{
NeilBrown84fc4b52011-10-11 16:49:58 +1100933 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800934 list_for_each_entry(pers, &pers_list, list) {
935 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800936 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800937 if (strcmp(pers->name, clevel)==0)
938 return pers;
939 }
NeilBrown2604b702006-01-06 00:20:36 -0800940 return NULL;
941}
942
Andre Nollb73df2d2008-07-11 22:02:23 +1000943/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100944static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100946 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000947 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
NeilBrownf72ffdd2014-09-30 14:23:59 +1000950static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100953 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000954 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return 0;
956}
957
NeilBrown545c8792012-05-22 13:54:30 +1000958void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800961 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 rdev->sb_loaded = 0;
963 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000964 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100965 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
NeilBrown2699b672011-07-28 11:31:47 +1000967 if (rdev->bb_page) {
968 put_page(rdev->bb_page);
969 rdev->bb_page = NULL;
970 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800971 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
NeilBrown545c8792012-05-22 13:54:30 +1000973EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200975static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700976{
NeilBrown3cb03002011-10-11 16:45:26 +1100977 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100978 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700979
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200980 if (bio->bi_status) {
981 pr_err("md: super_written gets error=%d\n", bio->bi_status);
NeilBrowna9701a32005-11-08 21:39:34 -0800982 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100983 if (!test_bit(Faulty, &rdev->flags)
984 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800985 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100986 set_bit(LastDev, &rdev->flags);
987 }
988 } else
989 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700990
NeilBrowna9701a32005-11-08 21:39:34 -0800991 if (atomic_dec_and_test(&mddev->pending_writes))
992 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700993 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700994 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700995}
996
NeilBrownfd01b882011-10-11 16:47:53 +1100997void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700998 sector_t sector, int size, struct page *page)
999{
1000 /* write first size bytes of page to sector of rdev
1001 * Increment mddev->pending_writes before returning
1002 * and decrement it on completion, waking up sb_wait
1003 * if zero is reached.
1004 * If an error occurred, call md_error
1005 */
NeilBrown46533ff2016-11-18 16:16:11 +11001006 struct bio *bio;
1007 int ff = 0;
1008
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01001009 if (!page)
1010 return;
1011
NeilBrown46533ff2016-11-18 16:16:11 +11001012 if (test_bit(Faulty, &rdev->flags))
1013 return;
1014
NeilBrown5a850712017-06-21 09:12:21 +10001015 bio = md_bio_alloc_sync(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001016
Shaohua Lied3b98c2016-03-29 14:00:19 -07001017 atomic_inc(&rdev->nr_pending);
1018
Christoph Hellwig74d46992017-08-23 19:10:32 +02001019 bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001020 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001021 bio_add_page(bio, page, size, 0);
1022 bio->bi_private = rdev;
1023 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +11001024
1025 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
1026 test_bit(FailFast, &rdev->flags) &&
1027 !test_bit(LastDev, &rdev->flags))
1028 ff = MD_FAILFAST;
Jan Kara5a8948f2017-05-31 09:44:33 +02001029 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -08001030
NeilBrown7bfa19f2005-06-21 17:17:28 -07001031 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -05001032 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -08001033}
1034
NeilBrown46533ff2016-11-18 16:16:11 +11001035int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -08001036{
Tejun Heoe9c74692010-09-03 11:56:18 +02001037 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +10001038 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -08001039 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +11001040 return -EAGAIN;
1041 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001042}
1043
NeilBrown3cb03002011-10-11 16:45:26 +11001044int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -05001045 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
NeilBrown5a850712017-06-21 09:12:21 +10001047 struct bio *bio = md_bio_alloc_sync(rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 int ret;
1049
Christoph Hellwig74d46992017-08-23 19:10:32 +02001050 if (metadata_op && rdev->meta_bdev)
1051 bio_set_dev(bio, rdev->meta_bdev);
1052 else
1053 bio_set_dev(bio, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -05001054 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001055 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -07001056 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +10001057 else if (rdev->mddev->reshape_position != MaxSector &&
1058 (rdev->mddev->reshape_backwards ==
1059 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001060 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001061 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001062 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -05001064
1065 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001067 ret = !bio->bi_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 bio_put(bio);
1069 return ret;
1070}
NeilBrowna8745db2006-01-06 00:20:34 -08001071EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
NeilBrownf72ffdd2014-09-30 14:23:59 +10001073static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (rdev->sb_loaded)
1078 return 0;
1079
Mike Christie796a5cf2016-06-05 14:32:07 -05001080 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 goto fail;
1082 rdev->sb_loaded = 1;
1083 return 0;
1084
1085fail:
NeilBrown9d487392016-11-02 14:16:49 +11001086 pr_err("md: disabled device %s, could not read superblock.\n",
1087 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return -EINVAL;
1089}
1090
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001091static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
NeilBrownf72ffdd2014-09-30 14:23:59 +10001093 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +10001094 sb1->set_uuid1 == sb2->set_uuid1 &&
1095 sb1->set_uuid2 == sb2->set_uuid2 &&
1096 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001099static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 int ret;
1102 mdp_super_t *tmp1, *tmp2;
1103
1104 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
1105 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
1106
1107 if (!tmp1 || !tmp2) {
1108 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 goto abort;
1110 }
1111
1112 *tmp1 = *sb1;
1113 *tmp2 = *sb2;
1114
1115 /*
1116 * nr_disks is not constant
1117 */
1118 tmp1->nr_disks = 0;
1119 tmp2->nr_disks = 0;
1120
Andre Nollce0c8e02008-07-11 22:02:20 +10001121 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001123 kfree(tmp1);
1124 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return ret;
1126}
1127
NeilBrown4d167f02007-05-09 02:35:37 -07001128static u32 md_csum_fold(u32 csum)
1129{
1130 csum = (csum & 0xffff) + (csum >> 16);
1131 return (csum & 0xffff) + (csum >> 16);
1132}
1133
NeilBrownf72ffdd2014-09-30 14:23:59 +10001134static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
NeilBrown4d167f02007-05-09 02:35:37 -07001136 u64 newcsum = 0;
1137 u32 *sb32 = (u32*)sb;
1138 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 unsigned int disk_csum, csum;
1140
1141 disk_csum = sb->sb_csum;
1142 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -07001143
1144 for (i = 0; i < MD_SB_BYTES/4 ; i++)
1145 newcsum += sb32[i];
1146 csum = (newcsum & 0xffffffff) + (newcsum>>32);
1147
NeilBrown4d167f02007-05-09 02:35:37 -07001148#ifdef CONFIG_ALPHA
1149 /* This used to use csum_partial, which was wrong for several
1150 * reasons including that different results are returned on
1151 * different architectures. It isn't critical that we get exactly
1152 * the same return value as before (we always csum_fold before
1153 * testing, and that removes any differences). However as we
1154 * know that csum_partial always returned a 16bit value on
1155 * alphas, do a fold to maximise conformity to previous behaviour.
1156 */
1157 sb->sb_csum = md_csum_fold(disk_csum);
1158#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -07001160#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return csum;
1162}
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164/*
1165 * Handle superblock details.
1166 * We want to be able to handle multiple superblock formats
1167 * so we have a common interface to them all, and an array of
1168 * different handlers.
1169 * We rely on user-space to write the initial superblock, and support
1170 * reading and updating of superblocks.
1171 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +11001172 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 * loads and validates a superblock on dev.
1174 * if refdev != NULL, compare superblocks on both devices
1175 * Return:
1176 * 0 - dev has a superblock that is compatible with refdev
1177 * 1 - dev has a superblock that is compatible and newer than refdev
1178 * so dev should be used as the refdev in future
1179 * -EINVAL superblock incompatible or invalid
1180 * -othererror e.g. -EIO
1181 *
NeilBrownfd01b882011-10-11 16:47:53 +11001182 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 * Verify that dev is acceptable into mddev.
1184 * The first time, mddev->raid_disks will be 0, and data from
1185 * dev should be merged in. Subsequent calls check that dev
1186 * is new enough. Return 0 or -EINVAL
1187 *
NeilBrownfd01b882011-10-11 16:47:53 +11001188 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 * Update the superblock for rdev with data in mddev
1190 * This does not write to disc.
1191 *
1192 */
1193
1194struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001195 char *name;
1196 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +10001197 int (*load_super)(struct md_rdev *rdev,
1198 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001199 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +10001200 int (*validate_super)(struct mddev *mddev,
1201 struct md_rdev *rdev);
1202 void (*sync_super)(struct mddev *mddev,
1203 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001204 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001205 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +10001206 int (*allow_new_offset)(struct md_rdev *rdev,
1207 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208};
1209
1210/*
Andre Noll0894cc32009-06-18 08:49:23 +10001211 * Check that the given mddev has no bitmap.
1212 *
1213 * This function is called from the run method of all personalities that do not
1214 * support bitmaps. It prints an error message and returns non-zero if mddev
1215 * has a bitmap. Otherwise, it returns 0.
1216 *
1217 */
NeilBrownfd01b882011-10-11 16:47:53 +11001218int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001219{
NeilBrownc3d97142009-12-14 12:49:52 +11001220 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001221 return 0;
NeilBrown9d487392016-11-02 14:16:49 +11001222 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +10001223 mdname(mddev), mddev->pers->name);
1224 return 1;
1225}
1226EXPORT_SYMBOL(md_check_no_bitmap);
1227
1228/*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001229 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 */
NeilBrown3cb03002011-10-11 16:45:26 +11001231static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1234 mdp_super_t *sb;
1235 int ret;
Yufen Yu228fc7d2019-10-30 18:47:02 +08001236 bool spare_disk = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 /*
Andre Noll0f420352008-07-11 22:02:23 +10001239 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 * it's at the end of the disk.
1241 *
1242 * It also happens to be a multiple of 4Kb.
1243 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001244 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
NeilBrown0002b272005-09-09 16:23:53 -07001246 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +11001247 if (ret)
1248 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 ret = -EINVAL;
1251
1252 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001253 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +11001256 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 goto abort;
1258 }
1259
1260 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001261 sb->minor_version < 90 ||
1262 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +11001263 pr_warn("Bad version number %d.%d on %s\n",
1264 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 goto abort;
1266 }
1267
1268 if (sb->raid_disks <= 0)
1269 goto abort;
1270
NeilBrown4d167f02007-05-09 02:35:37 -07001271 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001272 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 goto abort;
1274 }
1275
1276 rdev->preferred_minor = sb->md_minor;
1277 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001278 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001279 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001280 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 if (sb->level == LEVEL_MULTIPATH)
1283 rdev->desc_nr = -1;
1284 else
1285 rdev->desc_nr = sb->this_disk.number;
1286
Yufen Yu228fc7d2019-10-30 18:47:02 +08001287 /* not spare disk, or LEVEL_MULTIPATH */
1288 if (sb->level == LEVEL_MULTIPATH ||
1289 (rdev->desc_nr >= 0 &&
Yufen Yu3b7436c2019-12-10 15:01:29 +08001290 rdev->desc_nr < MD_SB_DISKS &&
Yufen Yu228fc7d2019-10-30 18:47:02 +08001291 sb->disks[rdev->desc_nr].state &
1292 ((1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))))
1293 spare_disk = false;
1294
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001295 if (!refdev) {
Yufen Yu228fc7d2019-10-30 18:47:02 +08001296 if (!spare_disk)
Yufen Yu6a5cb532019-10-16 16:00:03 +08001297 ret = 1;
1298 else
1299 ret = 0;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001300 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001302 mdp_super_t *refsb = page_address(refdev->sb_page);
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001303 if (!md_uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001304 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 b, bdevname(refdev->bdev,b2));
1306 goto abort;
1307 }
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001308 if (!md_sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001309 pr_warn("md: %s has same UUID but different superblock to %s\n",
1310 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 goto abort;
1312 }
1313 ev1 = md_event(sb);
1314 ev2 = md_event(refsb);
Yufen Yu6a5cb532019-10-16 16:00:03 +08001315
Yufen Yu228fc7d2019-10-30 18:47:02 +08001316 if (!spare_disk && ev1 > ev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001318 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 ret = 0;
1320 }
NeilBrown8190e752009-06-18 08:48:58 +10001321 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001322 /* Limit to 4TB as metadata cannot record more than that.
1323 * (not needed for Linear and RAID0 as metadata doesn't
1324 * record this size)
1325 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001326 if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001327 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
NeilBrown27a7b262011-09-10 17:21:28 +10001329 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001330 /* "this cannot possibly happen" ... */
1331 ret = -EINVAL;
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 abort:
1334 return ret;
1335}
1336
1337/*
1338 * validate_super for 0.90.0
1339 */
NeilBrownfd01b882011-10-11 16:47:53 +11001340static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001343 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001344 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
NeilBrown41158c72005-06-21 17:17:25 -07001346 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001347 clear_bit(Faulty, &rdev->flags);
1348 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001349 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001350 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (mddev->raid_disks == 0) {
1353 mddev->major_version = 0;
1354 mddev->minor_version = sb->minor_version;
1355 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001356 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001357 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 mddev->ctime = sb->ctime;
1359 mddev->utime = sb->utime;
1360 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001361 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 mddev->layout = sb->layout;
1363 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001364 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001365 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001366 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001367 mddev->bitmap_info.space = 0;
1368 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001369 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001370 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001371 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
NeilBrownf6705572006-03-27 01:18:11 -08001373 if (mddev->minor_version >= 91) {
1374 mddev->reshape_position = sb->reshape_position;
1375 mddev->delta_disks = sb->delta_disks;
1376 mddev->new_level = sb->new_level;
1377 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001378 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001379 if (mddev->delta_disks < 0)
1380 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001381 } else {
1382 mddev->reshape_position = MaxSector;
1383 mddev->delta_disks = 0;
1384 mddev->new_level = mddev->level;
1385 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001386 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001387 }
NeilBrown33f2c352019-09-09 16:52:29 +10001388 if (mddev->level == 0)
1389 mddev->layout = -1;
NeilBrownf6705572006-03-27 01:18:11 -08001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (sb->state & (1<<MD_SB_CLEAN))
1392 mddev->recovery_cp = MaxSector;
1393 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001394 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 sb->events_lo == sb->cp_events_lo) {
1396 mddev->recovery_cp = sb->recovery_cp;
1397 } else
1398 mddev->recovery_cp = 0;
1399 }
1400
1401 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1402 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1403 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1404 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1405
1406 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001407
1408 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001409 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001410 mddev->bitmap_info.offset =
1411 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001412 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001413 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001414 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001415
NeilBrown41158c72005-06-21 17:17:25 -07001416 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001417 /* Insist on good event counter while assembling, except
1418 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001420 if (sb->disks[rdev->desc_nr].state & (
1421 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001422 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001423 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001424 } else if (mddev->bitmap) {
1425 /* if adding to array with a bitmap, then we can accept an
1426 * older device ... but not too old.
1427 */
NeilBrown41158c72005-06-21 17:17:25 -07001428 if (ev1 < mddev->bitmap->events_cleared)
1429 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001430 if (ev1 < mddev->events)
1431 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001432 } else {
1433 if (ev1 < mddev->events)
1434 /* just a hot-add of a new device, leave raid_disk at -1 */
1435 return 0;
1436 }
NeilBrown41158c72005-06-21 17:17:25 -07001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 desc = sb->disks + rdev->desc_nr;
1440
1441 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001442 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001443 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1444 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001445 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001447 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001448 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1449 /* active but not in sync implies recovery up to
1450 * reshape position. We don't know exactly where
1451 * that is, so set to zero for now */
1452 if (mddev->minor_version >= 91) {
1453 rdev->recovery_offset = 0;
1454 rdev->raid_disk = desc->raid_disk;
1455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001457 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1458 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001459 if (desc->state & (1<<MD_DISK_FAILFAST))
1460 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001461 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001462 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return 0;
1464}
1465
1466/*
1467 * sync_super for 0.90.0
1468 */
NeilBrownfd01b882011-10-11 16:47:53 +11001469static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001472 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 /* make rdev->sb match mddev data..
1476 *
1477 * 1/ zero out disks
1478 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1479 * 3/ any empty disks < next_spare become removed
1480 *
1481 * disks[0] gets initialised to REMOVED because
1482 * we cannot be sure from other fields if it has
1483 * been initialised or not.
1484 */
1485 int i;
1486 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1487
NeilBrown61181562005-09-09 16:24:02 -07001488 rdev->sb_size = MD_SB_BYTES;
1489
Namhyung Kim65a06f062011-07-27 11:00:36 +10001490 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 memset(sb, 0, sizeof(*sb));
1493
1494 sb->md_magic = MD_SB_MAGIC;
1495 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 sb->patch_version = mddev->patch_version;
1497 sb->gvalid_words = 0; /* ignored */
1498 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1499 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1500 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1501 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1502
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001503 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001505 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 sb->raid_disks = mddev->raid_disks;
1507 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001508 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001509 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 sb->state = 0;
1511 sb->events_hi = (mddev->events>>32);
1512 sb->events_lo = (u32)mddev->events;
1513
NeilBrownf6705572006-03-27 01:18:11 -08001514 if (mddev->reshape_position == MaxSector)
1515 sb->minor_version = 90;
1516 else {
1517 sb->minor_version = 91;
1518 sb->reshape_position = mddev->reshape_position;
1519 sb->new_level = mddev->new_level;
1520 sb->delta_disks = mddev->delta_disks;
1521 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001522 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001523 }
1524 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (mddev->in_sync)
1526 {
1527 sb->recovery_cp = mddev->recovery_cp;
1528 sb->cp_events_hi = (mddev->events>>32);
1529 sb->cp_events_lo = (u32)mddev->events;
1530 if (mddev->recovery_cp == MaxSector)
1531 sb->state = (1<< MD_SB_CLEAN);
1532 } else
1533 sb->recovery_cp = 0;
1534
1535 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001536 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
NeilBrownc3d97142009-12-14 12:49:52 +11001538 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001539 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001542 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001544 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001545 int is_active = test_bit(In_sync, &rdev2->flags);
1546
1547 if (rdev2->raid_disk >= 0 &&
1548 sb->minor_version >= 91)
1549 /* we have nowhere to store the recovery_offset,
1550 * but if it is not below the reshape_position,
1551 * we can piggy-back on that.
1552 */
1553 is_active = 1;
1554 if (rdev2->raid_disk < 0 ||
1555 test_bit(Faulty, &rdev2->flags))
1556 is_active = 0;
1557 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001558 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001560 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001561 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 d = &sb->disks[rdev2->desc_nr];
1563 nr_disks++;
1564 d->number = rdev2->desc_nr;
1565 d->major = MAJOR(rdev2->bdev->bd_dev);
1566 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001567 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 d->raid_disk = rdev2->raid_disk;
1569 else
1570 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001571 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001573 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001575 if (test_bit(In_sync, &rdev2->flags))
1576 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 active++;
1578 working++;
1579 } else {
1580 d->state = 0;
1581 spare++;
1582 working++;
1583 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001584 if (test_bit(WriteMostly, &rdev2->flags))
1585 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001586 if (test_bit(FailFast, &rdev2->flags))
1587 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /* now set the "removed" and "faulty" bits on any missing devices */
1590 for (i=0 ; i < mddev->raid_disks ; i++) {
1591 mdp_disk_t *d = &sb->disks[i];
1592 if (d->state == 0 && d->number == 0) {
1593 d->number = i;
1594 d->raid_disk = i;
1595 d->state = (1<<MD_DISK_REMOVED);
1596 d->state |= (1<<MD_DISK_FAULTY);
1597 failed++;
1598 }
1599 }
1600 sb->nr_disks = nr_disks;
1601 sb->active_disks = active;
1602 sb->working_disks = working;
1603 sb->failed_disks = failed;
1604 sb->spare_disks = spare;
1605
1606 sb->this_disk = sb->disks[rdev->desc_nr];
1607 sb->sb_csum = calc_sb_csum(sb);
1608}
1609
1610/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001611 * rdev_size_change for 0.90.0
1612 */
1613static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001614super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001615{
Andre Noll58c0fed2009-03-31 14:33:13 +11001616 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001617 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001618 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001619 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001620 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001621 if (!num_sectors || num_sectors > rdev->sb_start)
1622 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001623 /* Limit to 4TB as metadata cannot record more than that.
1624 * 4TB == 2^32 KB, or 2*2^32 sectors.
1625 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001626 if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001627 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001628 do {
1629 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001630 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001631 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001632 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001633}
1634
NeilBrownc6563a82012-05-21 09:27:00 +10001635static int
1636super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1637{
1638 /* non-zero offset changes not possible with v0.90 */
1639 return new_offset == 0;
1640}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001641
1642/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 * version 1 superblock
1644 */
1645
NeilBrownf72ffdd2014-09-30 14:23:59 +10001646static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001648 __le32 disk_csum;
1649 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 unsigned long long newcsum;
1651 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001652 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 disk_csum = sb->sb_csum;
1655 sb->sb_csum = 0;
1656 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001657 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 newcsum += le32_to_cpu(*isuper++);
1659
1660 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001661 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1664 sb->sb_csum = disk_csum;
1665 return cpu_to_le32(csum);
1666}
1667
NeilBrown3cb03002011-10-11 16:45:26 +11001668static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
1670 struct mdp_superblock_1 *sb;
1671 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001672 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001673 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001675 int bmask;
Yufen Yu228fc7d2019-10-30 18:47:02 +08001676 bool spare_disk = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 /*
Andre Noll0f420352008-07-11 22:02:23 +10001679 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 * It is always aligned to a 4K boundary and
1681 * depeding on minor_version, it can be:
1682 * 0: At least 8K, but less than 12K, from end of device
1683 * 1: At start of device
1684 * 2: 4K from start of device.
1685 */
1686 switch(minor_version) {
1687 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001688 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001689 sb_start -= 8*2;
1690 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 break;
1692 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001693 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 break;
1695 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001696 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 break;
1698 default:
1699 return -EINVAL;
1700 }
Andre Noll0f420352008-07-11 22:02:23 +10001701 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
NeilBrown0002b272005-09-09 16:23:53 -07001703 /* superblock is rarely larger than 1K, but it can be larger,
1704 * and it is safe to read 4k, so we do that
1705 */
1706 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (ret) return ret;
1708
Namhyung Kim65a06f062011-07-27 11:00:36 +10001709 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1712 sb->major_version != cpu_to_le32(1) ||
1713 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001714 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001715 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return -EINVAL;
1717
1718 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001719 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 bdevname(rdev->bdev,b));
1721 return -EINVAL;
1722 }
1723 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001724 pr_warn("md: data_size too small on %s\n",
1725 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return -EINVAL;
1727 }
NeilBrownc6563a82012-05-21 09:27:00 +10001728 if (sb->pad0 ||
1729 sb->pad3[0] ||
1730 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1731 /* Some padding is non-zero, might be a new feature */
1732 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 rdev->preferred_minor = 0xffff;
1735 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001736 rdev->new_data_offset = rdev->data_offset;
1737 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1738 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1739 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001740 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
NeilBrown0002b272005-09-09 16:23:53 -07001742 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001743 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001744 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001745 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1746
1747 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001748 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001749 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001750 if (minor_version
1751 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1752 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001753
NeilBrown31b65a02006-07-10 04:44:14 -07001754 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1755 rdev->desc_nr = -1;
1756 else
1757 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1758
NeilBrown2699b672011-07-28 11:31:47 +10001759 if (!rdev->bb_page) {
1760 rdev->bb_page = alloc_page(GFP_KERNEL);
1761 if (!rdev->bb_page)
1762 return -ENOMEM;
1763 }
1764 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1765 rdev->badblocks.count == 0) {
1766 /* need to load the bad block list.
1767 * Currently we limit it to one page.
1768 */
1769 s32 offset;
1770 sector_t bb_sector;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001771 __le64 *bbp;
NeilBrown2699b672011-07-28 11:31:47 +10001772 int i;
1773 int sectors = le16_to_cpu(sb->bblog_size);
1774 if (sectors > (PAGE_SIZE / 512))
1775 return -EINVAL;
1776 offset = le32_to_cpu(sb->bblog_offset);
1777 if (offset == 0)
1778 return -EINVAL;
1779 bb_sector = (long long)offset;
1780 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001781 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001782 return -EIO;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001783 bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10001784 rdev->badblocks.shift = sb->bblog_shift;
1785 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1786 u64 bb = le64_to_cpu(*bbp);
1787 int count = bb & (0x3ff);
1788 u64 sector = bb >> 10;
1789 sector <<= sb->bblog_shift;
1790 count <<= sb->bblog_shift;
1791 if (bb + 1 == 0)
1792 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001793 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001794 return -EINVAL;
1795 }
NeilBrown486adf72013-04-24 11:42:44 +10001796 } else if (sb->bblog_offset != 0)
1797 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001798
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001799 if ((le32_to_cpu(sb->feature_map) &
1800 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001801 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1802 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1803 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1804 }
1805
NeilBrown33f2c352019-09-09 16:52:29 +10001806 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT) &&
1807 sb->level != 0)
1808 return -EINVAL;
1809
Yufen Yu228fc7d2019-10-30 18:47:02 +08001810 /* not spare disk, or LEVEL_MULTIPATH */
1811 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH) ||
1812 (rdev->desc_nr >= 0 &&
1813 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1814 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1815 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)))
1816 spare_disk = false;
Yufen Yu6a5cb532019-10-16 16:00:03 +08001817
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001818 if (!refdev) {
Yufen Yu228fc7d2019-10-30 18:47:02 +08001819 if (!spare_disk)
Yufen Yu6a5cb532019-10-16 16:00:03 +08001820 ret = 1;
1821 else
1822 ret = 0;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001823 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001825 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1828 sb->level != refsb->level ||
1829 sb->layout != refsb->layout ||
1830 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001831 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 bdevname(rdev->bdev,b),
1833 bdevname(refdev->bdev,b2));
1834 return -EINVAL;
1835 }
1836 ev1 = le64_to_cpu(sb->events);
1837 ev2 = le64_to_cpu(refsb->events);
1838
Yufen Yu228fc7d2019-10-30 18:47:02 +08001839 if (!spare_disk && ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001840 ret = 1;
1841 else
1842 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
NeilBrownc6563a82012-05-21 09:27:00 +10001844 if (minor_version) {
1845 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1846 sectors -= rdev->data_offset;
1847 } else
1848 sectors = rdev->sb_start;
1849 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001851 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001852 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
NeilBrownfd01b882011-10-11 16:47:53 +11001855static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001857 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001858 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
NeilBrown41158c72005-06-21 17:17:25 -07001860 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001861 clear_bit(Faulty, &rdev->flags);
1862 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001863 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001864 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (mddev->raid_disks == 0) {
1867 mddev->major_version = 1;
1868 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001869 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001870 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001871 mddev->ctime = le64_to_cpu(sb->ctime);
1872 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001874 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 mddev->layout = le32_to_cpu(sb->layout);
1876 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001877 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001878 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001879 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001880 mddev->bitmap_info.space = 0;
1881 /* Default location for bitmap is 1K after superblock
1882 * using 3K - total of 4K
1883 */
NeilBrownc3d97142009-12-14 12:49:52 +11001884 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001885 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001886 mddev->reshape_backwards = 0;
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1889 memcpy(mddev->uuid, sb->set_uuid, 16);
1890
1891 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001892
NeilBrown71c08052005-09-09 16:23:51 -07001893 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001894 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001895 mddev->bitmap_info.offset =
1896 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001897 /* Metadata doesn't record how much space is available.
1898 * For 1.0, we assume we can use up to the superblock
1899 * if before, else to 4K beyond superblock.
1900 * For others, assume no change is possible.
1901 */
1902 if (mddev->minor_version > 0)
1903 mddev->bitmap_info.space = 0;
1904 else if (mddev->bitmap_info.offset > 0)
1905 mddev->bitmap_info.space =
1906 8 - mddev->bitmap_info.offset;
1907 else
1908 mddev->bitmap_info.space =
1909 -mddev->bitmap_info.offset;
1910 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001911
NeilBrownf6705572006-03-27 01:18:11 -08001912 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1913 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1914 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1915 mddev->new_level = le32_to_cpu(sb->new_level);
1916 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001917 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001918 if (mddev->delta_disks < 0 ||
1919 (mddev->delta_disks == 0 &&
1920 (le32_to_cpu(sb->feature_map)
1921 & MD_FEATURE_RESHAPE_BACKWARDS)))
1922 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001923 } else {
1924 mddev->reshape_position = MaxSector;
1925 mddev->delta_disks = 0;
1926 mddev->new_level = mddev->level;
1927 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001928 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001929 }
1930
NeilBrown33f2c352019-09-09 16:52:29 +10001931 if (mddev->level == 0 &&
1932 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT))
1933 mddev->layout = -1;
1934
Song Liu486b0f72016-08-19 15:34:01 -07001935 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001936 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001937
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001938 if (le32_to_cpu(sb->feature_map) &
1939 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001940 if (le32_to_cpu(sb->feature_map) &
1941 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1942 return -EINVAL;
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001943 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1944 (le32_to_cpu(sb->feature_map) &
1945 MD_FEATURE_MULTIPLE_PPLS))
1946 return -EINVAL;
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001947 set_bit(MD_HAS_PPL, &mddev->flags);
1948 }
NeilBrown41158c72005-06-21 17:17:25 -07001949 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001950 /* Insist of good event counter while assembling, except for
1951 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001953 if (rdev->desc_nr >= 0 &&
1954 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001955 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1956 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001957 if (ev1 < mddev->events)
1958 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001959 } else if (mddev->bitmap) {
1960 /* If adding to array with a bitmap, then we can accept an
1961 * older device, but not too old.
1962 */
NeilBrown41158c72005-06-21 17:17:25 -07001963 if (ev1 < mddev->bitmap->events_cleared)
1964 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001965 if (ev1 < mddev->events)
1966 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001967 } else {
1968 if (ev1 < mddev->events)
1969 /* just a hot-add of a new device, leave raid_disk at -1 */
1970 return 0;
1971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (mddev->level != LEVEL_MULTIPATH) {
1973 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001974 if (rdev->desc_nr < 0 ||
1975 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001976 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001977 rdev->desc_nr = -1;
1978 } else
1979 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001981 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001983 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001984 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 break;
Song Liubac624f2015-08-13 14:31:55 -07001986 case MD_DISK_ROLE_JOURNAL: /* journal device */
1987 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1988 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001989 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001990 return -EINVAL;
1991 }
1992 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001993 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001994 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001995 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 default:
NeilBrownf4667222013-12-09 12:04:56 +11001997 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001998 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001999 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002000 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11002001 if (!(le32_to_cpu(sb->feature_map) &
2002 MD_FEATURE_RECOVERY_BITMAP))
2003 rdev->saved_raid_disk = -1;
Guoqing Jiang062f5b2a2019-07-24 11:09:20 +02002004 } else {
2005 /*
2006 * If the array is FROZEN, then the device can't
2007 * be in_sync with rest of array.
2008 */
2009 if (!test_bit(MD_RECOVERY_FROZEN,
2010 &mddev->recovery))
2011 set_bit(In_sync, &rdev->flags);
2012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 rdev->raid_disk = role;
2014 break;
2015 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002016 if (sb->devflags & WriteMostly1)
2017 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11002018 if (sb->devflags & FailFast1)
2019 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002020 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
2021 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07002022 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08002023 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07002024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return 0;
2026}
2027
NeilBrownfd01b882011-10-11 16:47:53 +11002028static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11002031 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 int max_dev, i;
2033 /* make rdev->sb match mddev and rdev data. */
2034
Namhyung Kim65a06f062011-07-27 11:00:36 +10002035 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 sb->feature_map = 0;
2038 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002039 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 memset(sb->pad3, 0, sizeof(sb->pad3));
2041
2042 sb->utime = cpu_to_le64((__u64)mddev->utime);
2043 sb->events = cpu_to_le64(mddev->events);
2044 if (mddev->in_sync)
2045 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07002046 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
2047 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 else
2049 sb->resync_offset = cpu_to_le64(0);
2050
NeilBrown1c05b4b2006-10-21 10:24:08 -07002051 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08002052
NeilBrownf0ca3402006-02-02 14:28:04 -08002053 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11002054 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10002055 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10002056 sb->level = cpu_to_le32(mddev->level);
2057 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11002058 if (test_bit(FailFast, &rdev->flags))
2059 sb->devflags |= FailFast1;
2060 else
2061 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08002062
NeilBrownaeb9b2112011-08-25 14:43:08 +10002063 if (test_bit(WriteMostly, &rdev->flags))
2064 sb->devflags |= WriteMostly1;
2065 else
2066 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10002067 sb->data_offset = cpu_to_le64(rdev->data_offset);
2068 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10002069
NeilBrownc3d97142009-12-14 12:49:52 +11002070 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
2071 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07002072 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07002073 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002074
Shaohua Lif2076e72015-10-08 21:54:12 -07002075 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11002076 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11002077 sb->feature_map |=
2078 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
2079 sb->recovery_offset =
2080 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11002081 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
2082 sb->feature_map |=
2083 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002084 }
Shaohua Li3069aa82015-08-13 14:31:56 -07002085 /* Note: recovery_offset and journal_tail share space */
2086 if (test_bit(Journal, &rdev->flags))
2087 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002088 if (test_bit(Replacement, &rdev->flags))
2089 sb->feature_map |=
2090 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002091
NeilBrownf6705572006-03-27 01:18:11 -08002092 if (mddev->reshape_position != MaxSector) {
2093 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
2094 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
2095 sb->new_layout = cpu_to_le32(mddev->new_layout);
2096 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
2097 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10002098 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10002099 if (mddev->delta_disks == 0 &&
2100 mddev->reshape_backwards)
2101 sb->feature_map
2102 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10002103 if (rdev->new_data_offset != rdev->data_offset) {
2104 sb->feature_map
2105 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
2106 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
2107 - rdev->data_offset));
2108 }
NeilBrownf6705572006-03-27 01:18:11 -08002109 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002110
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10002111 if (mddev_is_clustered(mddev))
2112 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
2113
NeilBrown2699b672011-07-28 11:31:47 +10002114 if (rdev->badblocks.count == 0)
2115 /* Nothing to do for bad blocks*/ ;
2116 else if (sb->bblog_offset == 0)
2117 /* Cannot record bad blocks on this device */
2118 md_error(mddev, rdev);
2119 else {
2120 struct badblocks *bb = &rdev->badblocks;
Christoph Hellwigae506402019-04-04 18:56:13 +02002121 __le64 *bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10002122 u64 *p = bb->page;
2123 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
2124 if (bb->changed) {
2125 unsigned seq;
2126
2127retry:
2128 seq = read_seqbegin(&bb->lock);
2129
2130 memset(bbp, 0xff, PAGE_SIZE);
2131
2132 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08002133 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10002134 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
2135 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08002136 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10002137 }
NeilBrownd0962932012-03-19 12:46:41 +11002138 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002139 if (read_seqretry(&bb->lock, seq))
2140 goto retry;
2141
2142 bb->sector = (rdev->sb_start +
2143 (int)le32_to_cpu(sb->bblog_offset));
2144 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10002145 }
2146 }
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11002149 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (rdev2->desc_nr+1 > max_dev)
2151 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07002152
NeilBrown70471da2009-08-03 10:59:57 +10002153 if (max_dev > le32_to_cpu(sb->max_dev)) {
2154 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07002155 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10002156 rdev->sb_size = max_dev * 2 + 256;
2157 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
2158 if (rdev->sb_size & bmask)
2159 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10002160 } else
2161 max_dev = le32_to_cpu(sb->max_dev);
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 for (i=0; i<max_dev;i++)
Lidong Zhong8df72022017-06-12 10:45:55 +08002164 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
NeilBrownf72ffdd2014-09-30 14:23:59 +10002165
Song Liua97b7892015-10-08 21:54:09 -07002166 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
2167 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01002169 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
Pawel Baldysiakddc08822017-08-16 17:13:45 +02002170 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
2171 sb->feature_map |=
2172 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
2173 else
2174 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01002175 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
2176 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
2177 }
2178
NeilBrowndafb20f2012-03-19 12:46:39 +11002179 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002181 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07002182 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08002183 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07002185 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07002186 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11002187 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002188 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 else
Song Liuc4d4c912015-08-13 14:31:54 -07002190 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 sb->sb_csum = calc_sb_1_csum(sb);
2194}
2195
Xiao Nid9c0fa52020-06-30 15:55:36 +08002196static sector_t super_1_choose_bm_space(sector_t dev_size)
2197{
2198 sector_t bm_space;
2199
2200 /* if the device is bigger than 8Gig, save 64k for bitmap
2201 * usage, if bigger than 200Gig, save 128k
2202 */
2203 if (dev_size < 64*2)
2204 bm_space = 0;
2205 else if (dev_size - 64*2 >= 200*1024*1024*2)
2206 bm_space = 128*2;
2207 else if (dev_size - 4*2 > 8*1024*1024*2)
2208 bm_space = 64*2;
2209 else
2210 bm_space = 4*2;
2211 return bm_space;
2212}
2213
Chris Webb0cd17fe2008-06-28 08:31:46 +10002214static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11002215super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002216{
2217 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002218 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11002219 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002220 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10002221 if (rdev->data_offset != rdev->new_data_offset)
2222 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10002223 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10002224 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01002225 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002226 max_sectors -= rdev->data_offset;
2227 if (!num_sectors || num_sectors > max_sectors)
2228 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11002229 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10002230 /* minor version 0 with bitmap we can't move */
2231 return 0;
2232 } else {
2233 /* minor version 0; superblock after data */
Xiao Nid9c0fa52020-06-30 15:55:36 +08002234 sector_t sb_start, bm_space;
2235 sector_t dev_size = i_size_read(rdev->bdev->bd_inode) >> 9;
2236
2237 /* 8K is for superblock */
2238 sb_start = dev_size - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10002239 sb_start &= ~(sector_t)(4*2 - 1);
Xiao Nid9c0fa52020-06-30 15:55:36 +08002240
2241 bm_space = super_1_choose_bm_space(dev_size);
2242
2243 /* Space that can be used to store date needs to decrease
2244 * superblock bitmap space and bad block space(4K)
2245 */
2246 max_sectors = sb_start - bm_space - 4*2;
2247
Andre Noll15f4a5f2008-07-21 14:42:12 +10002248 if (!num_sectors || num_sectors > max_sectors)
2249 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002250 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10002251 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10002252 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08002253 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10002254 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11002255 do {
2256 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
2257 rdev->sb_page);
2258 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11002259 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10002260
2261}
2262
2263static int
2264super_1_allow_new_offset(struct md_rdev *rdev,
2265 unsigned long long new_offset)
2266{
2267 /* All necessary checks on new >= old have been done */
2268 struct bitmap *bitmap;
2269 if (new_offset >= rdev->data_offset)
2270 return 1;
2271
2272 /* with 1.0 metadata, there is no metadata to tread on
2273 * so we can always move back */
2274 if (rdev->mddev->minor_version == 0)
2275 return 1;
2276
2277 /* otherwise we must be sure not to step on
2278 * any metadata, so stay:
2279 * 36K beyond start of superblock
2280 * beyond end of badblocks
2281 * beyond write-intent bitmap
2282 */
2283 if (rdev->sb_start + (32+4)*2 > new_offset)
2284 return 0;
2285 bitmap = rdev->mddev->bitmap;
2286 if (bitmap && !rdev->mddev->bitmap_info.file &&
2287 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10002288 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10002289 return 0;
2290 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2291 return 0;
2292
2293 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002294}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Adrian Bunk75c96f82005-05-05 16:16:09 -07002296static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 [0] = {
2298 .name = "0.90.0",
2299 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002300 .load_super = super_90_load,
2301 .validate_super = super_90_validate,
2302 .sync_super = super_90_sync,
2303 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002304 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 },
2306 [1] = {
2307 .name = "md-1",
2308 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002309 .load_super = super_1_load,
2310 .validate_super = super_1_validate,
2311 .sync_super = super_1_sync,
2312 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002313 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 },
2315};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
NeilBrownfd01b882011-10-11 16:47:53 +11002317static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05002318{
2319 if (mddev->sync_super) {
2320 mddev->sync_super(mddev, rdev);
2321 return;
2322 }
2323
2324 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2325
2326 super_types[mddev->major_version].sync_super(mddev, rdev);
2327}
2328
NeilBrownfd01b882011-10-11 16:47:53 +11002329static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
NeilBrown3cb03002011-10-11 16:45:26 +11002331 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
NeilBrown4b809912008-07-21 17:05:25 +10002333 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07002334 rdev_for_each_rcu(rdev, mddev1) {
2335 if (test_bit(Faulty, &rdev->flags) ||
2336 test_bit(Journal, &rdev->flags) ||
2337 rdev->raid_disk == -1)
2338 continue;
2339 rdev_for_each_rcu(rdev2, mddev2) {
2340 if (test_bit(Faulty, &rdev2->flags) ||
2341 test_bit(Journal, &rdev2->flags) ||
2342 rdev2->raid_disk == -1)
2343 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002344 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002345 rdev2->bdev->bd_contains) {
2346 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002347 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002348 }
Song Liu0b020e82015-09-03 23:00:35 -07002349 }
2350 }
NeilBrown4b809912008-07-21 17:05:25 +10002351 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 return 0;
2353}
2354
2355static LIST_HEAD(pending_raid_disks);
2356
Andre Nollac5e7112009-08-03 10:59:47 +10002357/*
2358 * Try to register data integrity profile for an mddev
2359 *
2360 * This is called when an array is started and after a disk has been kicked
2361 * from the array. It only succeeds if all working and active component devices
2362 * are integrity capable with matching profiles.
2363 */
NeilBrownfd01b882011-10-11 16:47:53 +11002364int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002365{
NeilBrown3cb03002011-10-11 16:45:26 +11002366 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002367
Andre Nollac5e7112009-08-03 10:59:47 +10002368 if (list_empty(&mddev->disks))
2369 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002370 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2371 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002372 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002373 /* skip spares and non-functional disks */
2374 if (test_bit(Faulty, &rdev->flags))
2375 continue;
2376 if (rdev->raid_disk < 0)
2377 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002378 if (!reference) {
2379 /* Use the first rdev as the reference */
2380 reference = rdev;
2381 continue;
2382 }
2383 /* does this rdev's profile match the reference profile? */
2384 if (blk_integrity_compare(reference->bdev->bd_disk,
2385 rdev->bdev->bd_disk) < 0)
2386 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002387 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002388 if (!reference || !bdev_get_integrity(reference->bdev))
2389 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002390 /*
2391 * All component devices are integrity capable and have matching
2392 * profiles, register the common profile for the md device.
2393 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002394 blk_integrity_register(mddev->gendisk,
2395 bdev_get_integrity(reference->bdev));
2396
NeilBrown9d487392016-11-02 14:16:49 +11002397 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Kent Overstreetafeee512018-05-20 18:25:52 -04002398 if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002399 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002400 mdname(mddev));
2401 return -EINVAL;
2402 }
Andre Nollac5e7112009-08-03 10:59:47 +10002403 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002404}
Andre Nollac5e7112009-08-03 10:59:47 +10002405EXPORT_SYMBOL(md_integrity_register);
2406
Dan Williams1501efa2016-01-13 16:00:07 -08002407/*
2408 * Attempt to add an rdev, but only if it is consistent with the current
2409 * integrity profile
2410 */
2411int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002412{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002413 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002414 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002415
2416 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002417 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002418
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002419 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002420
2421 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002422 return 0;
2423
2424 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002425 pr_err("%s: incompatible integrity profile for %s\n",
2426 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002427 return -ENXIO;
2428 }
2429
2430 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002431}
2432EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002433
NeilBrownf72ffdd2014-09-30 14:23:59 +10002434static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002436 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002437 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002438 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Dan Williams11e2ede2008-04-30 00:52:32 -07002440 /* prevent duplicates */
2441 if (find_rdev(mddev, rdev->bdev->bd_dev))
2442 return -EEXIST;
2443
NeilBrown97b20ef2017-04-13 08:53:48 +10002444 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2445 mddev->pers)
2446 return -EROFS;
2447
Andre Nolldd8ac332009-03-31 14:33:13 +11002448 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002449 if (!test_bit(Journal, &rdev->flags) &&
2450 rdev->sectors &&
2451 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002452 if (mddev->pers) {
2453 /* Cannot change size, so fail
2454 * If mddev->level <= 0, then we don't care
2455 * about aligning sizes (e.g. linear)
2456 */
2457 if (mddev->level > 0)
2458 return -ENOSPC;
2459 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002460 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 /* Verify rdev->desc_nr is unique.
2464 * If it is -1, assign a free number, else
2465 * check number is not in use
2466 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002467 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 if (rdev->desc_nr < 0) {
2469 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002470 if (mddev->pers)
2471 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002472 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 choice++;
2474 rdev->desc_nr = choice;
2475 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002476 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002477 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002481 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002482 if (!test_bit(Journal, &rdev->flags) &&
2483 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002484 pr_warn("md: %s: array is limited to %d devices\n",
2485 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002486 return -EBUSY;
2487 }
NeilBrown19133a42005-11-08 21:39:35 -08002488 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002489 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002492 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002493
Guoqing Jiang963c5552019-06-14 17:10:36 +08002494 if (mddev->raid_disks)
Guoqing Jiang404659c2019-12-23 10:48:53 +01002495 mddev_create_serial_pool(mddev, rdev, false);
Guoqing Jiang963c5552019-06-14 17:10:36 +08002496
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002497 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002498 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002499
Tejun Heo0762b8b2008-08-25 19:56:12 +09002500 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
Damien Le Moal5e3b8a82020-07-16 13:54:40 +09002501 /* failure here is OK */
2502 err = sysfs_create_link(&rdev->kobj, ko, "block");
NeilBrown00bcb4a2010-06-01 19:37:23 +10002503 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
Junxiao Bie1a86db2020-07-14 16:10:26 -07002504 rdev->sysfs_unack_badblocks =
2505 sysfs_get_dirent_safe(rdev->kobj.sd, "unacknowledged_bad_blocks");
2506 rdev->sysfs_badblocks =
2507 sysfs_get_dirent_safe(rdev->kobj.sd, "bad_blocks");
NeilBrown3c0ee632008-10-21 13:25:28 +11002508
NeilBrown4b809912008-07-21 17:05:25 +10002509 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002510 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002511
2512 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002513 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002516
2517 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002518 pr_warn("md: failed to register dev-%s for %s\n",
2519 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002520 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
2522
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002523static void rdev_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002524{
NeilBrown3cb03002011-10-11 16:45:26 +11002525 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002526 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002527 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002528}
2529
NeilBrownf72ffdd2014-09-30 14:23:59 +10002530static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
2532 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002533
Tejun Heo49731ba2011-01-14 18:43:57 +01002534 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002535 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002536 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +01002537 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002539 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002540 sysfs_put(rdev->sysfs_state);
Junxiao Bie1a86db2020-07-14 16:10:26 -07002541 sysfs_put(rdev->sysfs_unack_badblocks);
2542 sysfs_put(rdev->sysfs_badblocks);
NeilBrown3c0ee632008-10-21 13:25:28 +11002543 rdev->sysfs_state = NULL;
Junxiao Bie1a86db2020-07-14 16:10:26 -07002544 rdev->sysfs_unack_badblocks = NULL;
2545 rdev->sysfs_badblocks = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002546 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002547 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002548 * writing to 'remove' to "dev/state". We also need
2549 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002550 */
NeilBrown4b809912008-07-21 17:05:25 +10002551 synchronize_rcu();
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002552 INIT_WORK(&rdev->del_work, rdev_delayed_delete);
NeilBrown177a99b2008-02-06 01:39:56 -08002553 kobject_get(&rdev->kobj);
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002554 queue_work(md_rdev_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556
2557/*
2558 * prevent the device from being mounted, repartitioned or
2559 * otherwise reused by a RAID array (or any other kernel
2560 * subsystem), by bd_claiming the device.
2561 */
NeilBrown3cb03002011-10-11 16:45:26 +11002562static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
2564 int err = 0;
2565 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Tejun Heod4d77622010-11-13 11:55:18 +01002567 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002568 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if (IS_ERR(bdev)) {
Christoph Hellwigea3edd42020-03-24 08:25:11 +01002570 pr_warn("md: could not open device unknown-block(%u,%u).\n",
2571 MAJOR(dev), MINOR(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 return PTR_ERR(bdev);
2573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 rdev->bdev = bdev;
2575 return err;
2576}
2577
NeilBrown3cb03002011-10-11 16:45:26 +11002578static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
2580 struct block_device *bdev = rdev->bdev;
2581 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002582 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
2585void md_autodetect_dev(dev_t dev);
2586
NeilBrownf72ffdd2014-09-30 14:23:59 +10002587static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
2589 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002590
NeilBrown9d487392016-11-02 14:16:49 +11002591 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002592 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002594 if (test_bit(AutoDetected, &rdev->flags))
2595 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596#endif
2597 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002598 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
2600
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002601void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
2603 unbind_rdev_from_array(rdev);
2604 export_rdev(rdev);
2605}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002606EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
NeilBrownfd01b882011-10-11 16:47:53 +11002608static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609{
NeilBrown0638bb02014-09-25 17:43:47 +10002610 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
NeilBrown0638bb02014-09-25 17:43:47 +10002612 while (!list_empty(&mddev->disks)) {
2613 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2614 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002615 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 mddev->raid_disks = 0;
2618 mddev->major_version = 0;
2619}
2620
NeilBrown6497709b2017-03-15 14:05:14 +11002621static bool set_in_sync(struct mddev *mddev)
2622{
Shaohua Liefa4b772017-10-18 22:08:13 -07002623 lockdep_assert_held(&mddev->lock);
NeilBrown4ad23a972017-03-15 14:05:14 +11002624 if (!mddev->in_sync) {
2625 mddev->sync_checkers++;
2626 spin_unlock(&mddev->lock);
2627 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2628 spin_lock(&mddev->lock);
2629 if (!mddev->in_sync &&
2630 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002631 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002632 /*
2633 * Ensure ->in_sync is visible before we clear
2634 * ->sync_checkers.
2635 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002636 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002637 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2638 sysfs_notify_dirent_safe(mddev->sysfs_state);
2639 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002640 if (--mddev->sync_checkers == 0)
2641 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002642 }
2643 if (mddev->safemode == 1)
2644 mddev->safemode = 0;
2645 return mddev->in_sync;
2646}
2647
NeilBrownf72ffdd2014-09-30 14:23:59 +10002648static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
NeilBrown42543762006-06-26 00:27:57 -07002650 /* Update each superblock (in-memory image), but
2651 * if we are allowed to, skip spares which already
2652 * have the right event counter, or have one earlier
2653 * (which would mean they aren't being marked as dirty
2654 * with the rest of the array)
2655 */
NeilBrown3cb03002011-10-11 16:45:26 +11002656 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002657 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002658 if (rdev->sb_events == mddev->events ||
2659 (nospares &&
2660 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002661 rdev->sb_events+1 == mddev->events)) {
2662 /* Don't update this superblock */
2663 rdev->sb_loaded = 2;
2664 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002665 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002666 rdev->sb_loaded = 1;
2667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 }
2669}
2670
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002671static bool does_sb_need_changing(struct mddev *mddev)
2672{
2673 struct md_rdev *rdev;
2674 struct mdp_superblock_1 *sb;
2675 int role;
2676
2677 /* Find a good rdev */
2678 rdev_for_each(rdev, mddev)
2679 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2680 break;
2681
2682 /* No good device found. */
2683 if (!rdev)
2684 return false;
2685
2686 sb = page_address(rdev->sb_page);
2687 /* Check if a device has become faulty or a spare become active */
2688 rdev_for_each(rdev, mddev) {
2689 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2690 /* Device activated? */
2691 if (role == 0xffff && rdev->raid_disk >=0 &&
2692 !test_bit(Faulty, &rdev->flags))
2693 return true;
2694 /* Device turned faulty? */
2695 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2696 return true;
2697 }
2698
2699 /* Check if any mddev parameters have changed */
2700 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2701 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002702 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002703 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2704 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2705 return true;
2706
2707 return false;
2708}
2709
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002710void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
NeilBrown3cb03002011-10-11 16:45:26 +11002712 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002713 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002714 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002715 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002716 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
NeilBrownd87f0642013-04-24 11:42:40 +10002718 if (mddev->ro) {
2719 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002720 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002721 return;
2722 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002723
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002724repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002725 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002726 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002727 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002728 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002729 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002730 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002731 /* Has someone else has updated the sb */
2732 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002733 if (ret == 0)
2734 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002735 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2736 BIT(MD_SB_CHANGE_DEVS) |
2737 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002738 return;
2739 }
2740 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002741
NeilBrowndb0505d2017-10-17 16:18:36 +11002742 /*
2743 * First make sure individual recovery_offsets are correct
2744 * curr_resync_completed can only be used during recovery.
2745 * During reshape/resync it might use array-addresses rather
2746 * that device addresses.
2747 */
NeilBrowndafb20f2012-03-19 12:46:39 +11002748 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002749 if (rdev->raid_disk >= 0 &&
2750 mddev->delta_disks >= 0 &&
NeilBrowndb0505d2017-10-17 16:18:36 +11002751 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2752 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2753 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002754 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002755 !test_bit(In_sync, &rdev->flags) &&
2756 mddev->curr_resync_completed > rdev->recovery_offset)
2757 rdev->recovery_offset = mddev->curr_resync_completed;
2758
NeilBrownf72ffdd2014-09-30 14:23:59 +10002759 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002760 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002761 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2762 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002763 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002764 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002765 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002766 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002767 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002768 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002769 md_error(mddev, rdev);
2770 }
2771 clear_bit(Blocked, &rdev->flags);
2772 clear_bit(BlockedBadBlocks, &rdev->flags);
2773 wake_up(&rdev->blocked_wait);
2774 }
2775 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002776 wake_up(&mddev->sb_wait);
2777 return;
2778 }
2779
NeilBrown85572d72014-12-15 12:56:56 +11002780 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002781
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002782 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002783
Shaohua Li29530792016-12-08 15:48:19 -08002784 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002785 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002786 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002787 /* just a clean<-> dirty transition, possibly leave spares alone,
2788 * though if events isn't the right even/odd, we will have to do
2789 * spares after all
2790 */
2791 nospares = 1;
2792 if (force_change)
2793 nospares = 0;
2794 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002795 /* If the array is degraded, then skipping spares is both
2796 * dangerous and fairly pointless.
2797 * Dangerous because a device that was removed from the array
2798 * might have a event_count that still looks up-to-date,
2799 * so it can be re-added without a resync.
2800 * Pointless because if there are any spares to skip,
2801 * then a recovery will happen and soon that array won't
2802 * be degraded any more and the spare can go back to sleep then.
2803 */
NeilBrown850b2b422006-10-03 01:15:46 -07002804 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002805
NeilBrown06d91a52005-06-21 17:17:12 -07002806 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002807
2808 /* If this is just a dirty<->clean transition, and the array is clean
2809 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002810 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002811 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002812 && mddev->can_decrease_events
2813 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002814 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002815 mddev->can_decrease_events = 0;
2816 } else {
NeilBrown42543762006-06-26 00:27:57 -07002817 /* otherwise we have to go forward and ... */
2818 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002819 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
NeilBrown403df472014-09-30 15:52:29 +10002822 /*
2823 * This 64-bit counter should never wrap.
2824 * Either we are in around ~1 trillion A.C., assuming
2825 * 1 reboot per second, or we have a bug...
2826 */
2827 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002828
NeilBrowndafb20f2012-03-19 12:46:39 +11002829 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002830 if (rdev->badblocks.changed)
2831 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002832 if (test_bit(Faulty, &rdev->flags))
2833 set_bit(FaultRecorded, &rdev->flags);
2834 }
NeilBrown2699b672011-07-28 11:31:47 +10002835
NeilBrowne6910632008-02-06 01:39:51 -08002836 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002837 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
NeilBrown36a4e1f2011-10-07 14:23:17 +11002839 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2840 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Shaohua Li504634f2016-11-18 09:44:08 -08002842 if (mddev->queue)
2843 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002844rewrite:
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002845 md_bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002846 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002848
NeilBrown42543762006-06-26 00:27:57 -07002849 if (rdev->sb_loaded != 1)
2850 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
NeilBrownf4667222013-12-09 12:04:56 +11002852 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002853 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002854 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002855 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002856 pr_debug("md: (write) %s's sb offset: %llu\n",
2857 bdevname(rdev->bdev, b),
2858 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002859 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002860 if (rdev->badblocks.size) {
2861 md_super_write(mddev, rdev,
2862 rdev->badblocks.sector,
2863 rdev->badblocks.size << 9,
2864 rdev->bb_page);
2865 rdev->badblocks.size = 0;
2866 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002867
NeilBrownf4667222013-12-09 12:04:56 +11002868 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002869 pr_debug("md: %s (skipping faulty)\n",
2870 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002871
NeilBrown7bfa19f2005-06-21 17:17:28 -07002872 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 /* only need to write one superblock... */
2874 break;
2875 }
NeilBrown46533ff2016-11-18 16:16:11 +11002876 if (md_super_wait(mddev) < 0)
2877 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002878 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002879
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002880 if (mddev_is_clustered(mddev) && ret == 0)
2881 md_cluster_ops->metadata_update_finish(mddev);
2882
NeilBrown850b2b422006-10-03 01:15:46 -07002883 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002884 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2885 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002886 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002887 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002888 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002889 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
Junxiao Bie1a86db2020-07-14 16:10:26 -07002890 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown06d91a52005-06-21 17:17:12 -07002891
NeilBrowndafb20f2012-03-19 12:46:39 +11002892 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002893 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2894 clear_bit(Blocked, &rdev->flags);
2895
2896 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002897 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002898 clear_bit(BlockedBadBlocks, &rdev->flags);
2899 wake_up(&rdev->blocked_wait);
2900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002902EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002904static int add_bound_rdev(struct md_rdev *rdev)
2905{
2906 struct mddev *mddev = rdev->mddev;
2907 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002908 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002909
Shaohua Li87d4d912016-01-06 14:37:14 -08002910 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002911 /* If there is hot_add_disk but no hot_remove_disk
2912 * then added disks for geometry changes,
2913 * and should be added immediately.
2914 */
2915 super_types[mddev->major_version].
2916 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002917 if (add_journal)
2918 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002919 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002920 if (add_journal)
2921 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002922 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002923 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002924 return err;
2925 }
2926 }
2927 sysfs_notify_dirent_safe(rdev->sysfs_state);
2928
Shaohua Li29530792016-12-08 15:48:19 -08002929 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002930 if (mddev->degraded)
2931 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2932 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2933 md_new_event(mddev);
2934 md_wakeup_thread(mddev->thread);
2935 return 0;
2936}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Andre Noll7f6ce762008-03-23 18:34:54 +01002938/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002939 * We want to accept with case. For this we use cmd_match.
2940 */
2941static int cmd_match(const char *cmd, const char *str)
2942{
2943 /* See if cmd, written into a sysfs file, matches
2944 * str. They must either be the same, or cmd can
2945 * have a trailing newline
2946 */
2947 while (*cmd && *str && *cmd == *str) {
2948 cmd++;
2949 str++;
2950 }
2951 if (*cmd == '\n')
2952 cmd++;
2953 if (*str || *cmd)
2954 return 0;
2955 return 1;
2956}
2957
NeilBrown86e6ffd2005-11-08 21:39:24 -08002958struct rdev_sysfs_entry {
2959 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002960 ssize_t (*show)(struct md_rdev *, char *);
2961 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002962};
2963
2964static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002965state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002966{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002967 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002968 size_t len = 0;
Mark Rutland6aa7de02017-10-23 14:07:29 -07002969 unsigned long flags = READ_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002970
NeilBrown758bfc82014-12-15 12:56:59 +11002971 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002972 (!test_bit(ExternalBbl, &flags) &&
2973 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002974 len += sprintf(page+len, "faulty%s", sep);
2975 if (test_bit(In_sync, &flags))
2976 len += sprintf(page+len, "in_sync%s", sep);
2977 if (test_bit(Journal, &flags))
2978 len += sprintf(page+len, "journal%s", sep);
2979 if (test_bit(WriteMostly, &flags))
2980 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002981 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002982 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002983 && !test_bit(Faulty, &flags)))
2984 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002985 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002986 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002987 !test_bit(In_sync, &flags))
2988 len += sprintf(page+len, "spare%s", sep);
2989 if (test_bit(WriteErrorSeen, &flags))
2990 len += sprintf(page+len, "write_error%s", sep);
2991 if (test_bit(WantReplacement, &flags))
2992 len += sprintf(page+len, "want_replacement%s", sep);
2993 if (test_bit(Replacement, &flags))
2994 len += sprintf(page+len, "replacement%s", sep);
2995 if (test_bit(ExternalBbl, &flags))
2996 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002997 if (test_bit(FailFast, &flags))
2998 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002999
3000 if (len)
3001 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11003002
NeilBrown86e6ffd2005-11-08 21:39:24 -08003003 return len+sprintf(page+len, "\n");
3004}
3005
NeilBrown45dc2de2006-06-26 00:27:58 -07003006static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003007state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07003008{
3009 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10003010 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07003011 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07003012 * writemostly - sets write_mostly
3013 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10003014 * blocked - sets the Blocked flags
3015 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10003016 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11003017 * -insync - clear Insync for a device with a slot assigned,
3018 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10003019 * write_error - sets WriteErrorSeen
3020 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11003021 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07003022 */
3023 int err = -EINVAL;
3024 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
3025 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10003026 if (test_bit(Faulty, &rdev->flags))
3027 err = 0;
3028 else
3029 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07003030 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07003031 if (rdev->mddev->pers) {
3032 clear_bit(Blocked, &rdev->flags);
3033 remove_and_add_spares(rdev->mddev, rdev);
3034 }
NeilBrown45dc2de2006-06-26 00:27:58 -07003035 if (rdev->raid_disk >= 0)
3036 err = -EBUSY;
3037 else {
NeilBrownfd01b882011-10-11 16:47:53 +11003038 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07003039 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08003040 if (mddev_is_clustered(mddev))
3041 err = md_cluster_ops->remove_disk(mddev, rdev);
3042
3043 if (err == 0) {
3044 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11003045 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08003046 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11003047 md_wakeup_thread(mddev->thread);
3048 }
Guoqing Jianga9720902015-10-12 17:21:27 +08003049 md_new_event(mddev);
3050 }
NeilBrown45dc2de2006-06-26 00:27:58 -07003051 }
NeilBrownf6556752006-06-26 00:28:01 -07003052 } else if (cmd_match(buf, "writemostly")) {
3053 set_bit(WriteMostly, &rdev->flags);
Guoqing Jiang404659c2019-12-23 10:48:53 +01003054 mddev_create_serial_pool(rdev->mddev, rdev, false);
NeilBrownf6556752006-06-26 00:28:01 -07003055 err = 0;
3056 } else if (cmd_match(buf, "-writemostly")) {
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +01003057 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
NeilBrownf6556752006-06-26 00:28:01 -07003058 clear_bit(WriteMostly, &rdev->flags);
3059 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003060 } else if (cmd_match(buf, "blocked")) {
3061 set_bit(Blocked, &rdev->flags);
3062 err = 0;
3063 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10003064 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02003065 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10003066 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10003067 /* metadata handler doesn't understand badblocks,
3068 * so we need to fail the device
3069 */
3070 md_error(rdev->mddev, rdev);
3071 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003072 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10003073 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003074 wake_up(&rdev->blocked_wait);
3075 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3076 md_wakeup_thread(rdev->mddev->thread);
3077
3078 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10003079 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
3080 set_bit(In_sync, &rdev->flags);
3081 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11003082 } else if (cmd_match(buf, "failfast")) {
3083 set_bit(FailFast, &rdev->flags);
3084 err = 0;
3085 } else if (cmd_match(buf, "-failfast")) {
3086 clear_bit(FailFast, &rdev->flags);
3087 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07003088 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
3089 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10003090 if (rdev->mddev->pers == NULL) {
3091 clear_bit(In_sync, &rdev->flags);
3092 rdev->saved_raid_disk = rdev->raid_disk;
3093 rdev->raid_disk = -1;
3094 err = 0;
3095 }
NeilBrownd7a9d442011-07-28 11:31:48 +10003096 } else if (cmd_match(buf, "write_error")) {
3097 set_bit(WriteErrorSeen, &rdev->flags);
3098 err = 0;
3099 } else if (cmd_match(buf, "-write_error")) {
3100 clear_bit(WriteErrorSeen, &rdev->flags);
3101 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11003102 } else if (cmd_match(buf, "want_replacement")) {
3103 /* Any non-spare device that is not a replacement can
3104 * become want_replacement at any time, but we then need to
3105 * check if recovery is needed.
3106 */
3107 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07003108 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11003109 !test_bit(Replacement, &rdev->flags))
3110 set_bit(WantReplacement, &rdev->flags);
3111 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3112 md_wakeup_thread(rdev->mddev->thread);
3113 err = 0;
3114 } else if (cmd_match(buf, "-want_replacement")) {
3115 /* Clearing 'want_replacement' is always allowed.
3116 * Once replacements starts it is too late though.
3117 */
3118 err = 0;
3119 clear_bit(WantReplacement, &rdev->flags);
3120 } else if (cmd_match(buf, "replacement")) {
3121 /* Can only set a device as a replacement when array has not
3122 * yet been started. Once running, replacement is automatic
3123 * from spares, or by assigning 'slot'.
3124 */
3125 if (rdev->mddev->pers)
3126 err = -EBUSY;
3127 else {
3128 set_bit(Replacement, &rdev->flags);
3129 err = 0;
3130 }
3131 } else if (cmd_match(buf, "-replacement")) {
3132 /* Similarly, can only clear Replacement before start */
3133 if (rdev->mddev->pers)
3134 err = -EBUSY;
3135 else {
3136 clear_bit(Replacement, &rdev->flags);
3137 err = 0;
3138 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05003139 } else if (cmd_match(buf, "re-add")) {
Yufen Yuee37e622019-04-02 14:22:14 +08003140 if (!rdev->mddev->pers)
3141 err = -EINVAL;
3142 else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) &&
3143 rdev->saved_raid_disk >= 0) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05003144 /* clear_bit is performed _after_ all the devices
3145 * have their local Faulty bit cleared. If any writes
3146 * happen in the meantime in the local node, they
3147 * will land in the local bitmap, which will be synced
3148 * by this node eventually
3149 */
3150 if (!mddev_is_clustered(rdev->mddev) ||
3151 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
3152 clear_bit(Faulty, &rdev->flags);
3153 err = add_bound_rdev(rdev);
3154 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05003155 } else
3156 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02003157 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
3158 set_bit(ExternalBbl, &rdev->flags);
3159 rdev->badblocks.shift = 0;
3160 err = 0;
3161 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
3162 clear_bit(ExternalBbl, &rdev->flags);
3163 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07003164 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10003165 if (!err)
3166 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07003167 return err ? err : len;
3168}
NeilBrown80ca3a42006-07-10 04:44:18 -07003169static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10003170__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003171
3172static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003173errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08003174{
3175 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
3176}
3177
3178static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003179errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08003180{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003181 unsigned int n;
3182 int rv;
3183
3184 rv = kstrtouint(buf, 10, &n);
3185 if (rv < 0)
3186 return rv;
3187 atomic_set(&rdev->corrected_errors, n);
3188 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08003189}
3190static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07003191__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08003192
NeilBrown014236d2006-01-06 00:20:55 -08003193static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003194slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08003195{
Shaohua Lif2076e72015-10-08 21:54:12 -07003196 if (test_bit(Journal, &rdev->flags))
3197 return sprintf(page, "journal\n");
3198 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08003199 return sprintf(page, "none\n");
3200 else
3201 return sprintf(page, "%d\n", rdev->raid_disk);
3202}
3203
3204static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003205slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08003206{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003207 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08003208 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003209
Shaohua Lif2076e72015-10-08 21:54:12 -07003210 if (test_bit(Journal, &rdev->flags))
3211 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08003212 if (strncmp(buf, "none", 4)==0)
3213 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003214 else {
3215 err = kstrtouint(buf, 10, (unsigned int *)&slot);
3216 if (err < 0)
3217 return err;
3218 }
Neil Brown6c2fce22008-06-28 08:31:31 +10003219 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08003220 /* Setting 'slot' on an active array requires also
3221 * updating the 'rd%d' link, and communicating
3222 * with the personality with ->hot_*_disk.
3223 * For now we only support removing
3224 * failed/spare devices. This normally happens automatically,
3225 * but not when the metadata is externally managed.
3226 */
NeilBrownc303da62008-02-06 01:39:51 -08003227 if (rdev->raid_disk == -1)
3228 return -EEXIST;
3229 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10003230 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08003231 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10003232 clear_bit(Blocked, &rdev->flags);
3233 remove_and_add_spares(rdev->mddev, rdev);
3234 if (rdev->raid_disk >= 0)
3235 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08003236 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3237 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10003238 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10003239 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10003240 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10003241 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11003242 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10003243
3244 if (rdev->raid_disk != -1)
3245 return -EBUSY;
3246
NeilBrownc6751b22011-02-02 11:57:13 +11003247 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
3248 return -EBUSY;
3249
Neil Brown6c2fce22008-06-28 08:31:31 +10003250 if (rdev->mddev->pers->hot_add_disk == NULL)
3251 return -EINVAL;
3252
NeilBrownba1b41b2011-01-14 09:14:34 +11003253 if (slot >= rdev->mddev->raid_disks &&
3254 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
3255 return -ENOSPC;
3256
Neil Brown6c2fce22008-06-28 08:31:31 +10003257 rdev->raid_disk = slot;
3258 if (test_bit(In_sync, &rdev->flags))
3259 rdev->saved_raid_disk = slot;
3260 else
3261 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11003262 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11003263 clear_bit(Bitmap_sync, &rdev->flags);
Guoqing Jiang3f79cc22020-04-04 23:57:11 +02003264 err = rdev->mddev->pers->hot_add_disk(rdev->mddev, rdev);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11003265 if (err) {
3266 rdev->raid_disk = -1;
3267 return err;
3268 } else
3269 sysfs_notify_dirent_safe(rdev->sysfs_state);
Damien Le Moal5e3b8a82020-07-16 13:54:40 +09003270 /* failure here is OK */;
3271 sysfs_link_rdev(rdev->mddev, rdev);
Neil Brown6c2fce22008-06-28 08:31:31 +10003272 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08003273 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11003274 if (slot >= rdev->mddev->raid_disks &&
3275 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08003276 return -ENOSPC;
3277 rdev->raid_disk = slot;
3278 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08003279 clear_bit(Faulty, &rdev->flags);
3280 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08003281 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003282 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08003283 }
NeilBrown014236d2006-01-06 00:20:55 -08003284 return len;
3285}
3286
NeilBrown014236d2006-01-06 00:20:55 -08003287static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07003288__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08003289
NeilBrown93c8cad2006-01-06 00:20:56 -08003290static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003291offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08003292{
Andrew Morton6961ece2006-01-06 00:20:59 -08003293 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08003294}
3295
3296static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003297offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08003298{
NeilBrownc6563a82012-05-21 09:27:00 +10003299 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003300 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003301 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10003302 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003303 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003304 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08003305 /* Must set offset before size, so overlap checks
3306 * can be sane */
3307 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08003308 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10003309 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08003310 return len;
3311}
3312
3313static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07003314__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08003315
NeilBrownc6563a82012-05-21 09:27:00 +10003316static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3317{
3318 return sprintf(page, "%llu\n",
3319 (unsigned long long)rdev->new_data_offset);
3320}
3321
3322static ssize_t new_offset_store(struct md_rdev *rdev,
3323 const char *buf, size_t len)
3324{
3325 unsigned long long new_offset;
3326 struct mddev *mddev = rdev->mddev;
3327
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003328 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10003329 return -EINVAL;
3330
NeilBrownf851b602014-12-11 10:02:10 +11003331 if (mddev->sync_thread ||
3332 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10003333 return -EBUSY;
3334 if (new_offset == rdev->data_offset)
3335 /* reset is always permitted */
3336 ;
3337 else if (new_offset > rdev->data_offset) {
3338 /* must not push array size beyond rdev_sectors */
3339 if (new_offset - rdev->data_offset
3340 + mddev->dev_sectors > rdev->sectors)
3341 return -E2BIG;
3342 }
3343 /* Metadata worries about other space details. */
3344
3345 /* decreasing the offset is inconsistent with a backwards
3346 * reshape.
3347 */
3348 if (new_offset < rdev->data_offset &&
3349 mddev->reshape_backwards)
3350 return -EINVAL;
3351 /* Increasing offset is inconsistent with forwards
3352 * reshape. reshape_direction should be set to
3353 * 'backwards' first.
3354 */
3355 if (new_offset > rdev->data_offset &&
3356 !mddev->reshape_backwards)
3357 return -EINVAL;
3358
3359 if (mddev->pers && mddev->persistent &&
3360 !super_types[mddev->major_version]
3361 .allow_new_offset(rdev, new_offset))
3362 return -E2BIG;
3363 rdev->new_data_offset = new_offset;
3364 if (new_offset > rdev->data_offset)
3365 mddev->reshape_backwards = 1;
3366 else if (new_offset < rdev->data_offset)
3367 mddev->reshape_backwards = 0;
3368
3369 return len;
3370}
3371static struct rdev_sysfs_entry rdev_new_offset =
3372__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3373
NeilBrown83303b62006-01-06 00:21:06 -08003374static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003375rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003376{
Andre Nolldd8ac332009-03-31 14:33:13 +11003377 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003378}
3379
NeilBrownc5d79ad2008-02-06 01:39:54 -08003380static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3381{
3382 /* check if two start/length pairs overlap */
3383 if (s1+l1 <= s2)
3384 return 0;
3385 if (s2+l2 <= s1)
3386 return 0;
3387 return 1;
3388}
3389
Dan Williamsb522adc2009-03-31 15:00:31 +11003390static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3391{
3392 unsigned long long blocks;
3393 sector_t new;
3394
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003395 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003396 return -EINVAL;
3397
3398 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3399 return -EINVAL; /* sector conversion overflow */
3400
3401 new = blocks * 2;
3402 if (new != blocks * 2)
3403 return -EINVAL; /* unsigned long long to sector_t overflow */
3404
3405 *sectors = new;
3406 return 0;
3407}
3408
NeilBrown83303b62006-01-06 00:21:06 -08003409static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003410rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003411{
NeilBrownfd01b882011-10-11 16:47:53 +11003412 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003413 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003414 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003415
Shaohua Lif2076e72015-10-08 21:54:12 -07003416 if (test_bit(Journal, &rdev->flags))
3417 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003418 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003419 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003420 if (rdev->data_offset != rdev->new_data_offset)
3421 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003422 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003423 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003424 sectors = super_types[my_mddev->major_version].
3425 rdev_size_change(rdev, sectors);
3426 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003427 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003428 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003429 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003430 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003431 if (!my_mddev->pers->resize)
3432 /* Cannot change size for RAID0 or Linear etc */
3433 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003434 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003435 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003436 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003437
Andre Nolldd8ac332009-03-31 14:33:13 +11003438 rdev->sectors = sectors;
3439 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003440 /* Need to check that all other rdevs with the same
3441 * ->bdev do not overlap. 'rcu' is sufficient to walk
3442 * the rdev lists safely.
3443 * This check does not provide a hard guarantee, it
3444 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003445 */
NeilBrownfd01b882011-10-11 16:47:53 +11003446 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003447 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003448 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003449
NeilBrown8b1afc32014-09-29 15:33:20 +10003450 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003451 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003452 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003453
NeilBrowndafb20f2012-03-19 12:46:39 +11003454 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003455 if (rdev->bdev == rdev2->bdev &&
3456 rdev != rdev2 &&
3457 overlaps(rdev->data_offset, rdev->sectors,
3458 rdev2->data_offset,
3459 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003460 overlap = 1;
3461 break;
3462 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003463 if (overlap) {
3464 mddev_put(mddev);
3465 break;
3466 }
3467 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003468 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003469 if (overlap) {
3470 /* Someone else could have slipped in a size
3471 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003472 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003473 * safe, and trust userspace not to race with
3474 * itself
3475 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003476 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003477 return -EBUSY;
3478 }
3479 }
NeilBrown83303b62006-01-06 00:21:06 -08003480 return len;
3481}
3482
3483static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003484__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003485
NeilBrown3cb03002011-10-11 16:45:26 +11003486static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003487{
3488 unsigned long long recovery_start = rdev->recovery_offset;
3489
3490 if (test_bit(In_sync, &rdev->flags) ||
3491 recovery_start == MaxSector)
3492 return sprintf(page, "none\n");
3493
3494 return sprintf(page, "%llu\n", recovery_start);
3495}
3496
NeilBrown3cb03002011-10-11 16:45:26 +11003497static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003498{
3499 unsigned long long recovery_start;
3500
3501 if (cmd_match(buf, "none"))
3502 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003503 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003504 return -EINVAL;
3505
3506 if (rdev->mddev->pers &&
3507 rdev->raid_disk >= 0)
3508 return -EBUSY;
3509
3510 rdev->recovery_offset = recovery_start;
3511 if (recovery_start == MaxSector)
3512 set_bit(In_sync, &rdev->flags);
3513 else
3514 clear_bit(In_sync, &rdev->flags);
3515 return len;
3516}
3517
3518static struct rdev_sysfs_entry rdev_recovery_start =
3519__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3520
Vishal Vermafc974ee2015-12-24 19:20:34 -07003521/* sysfs access to bad-blocks list.
3522 * We present two files.
3523 * 'bad-blocks' lists sector numbers and lengths of ranges that
3524 * are recorded as bad. The list is truncated to fit within
3525 * the one-page limit of sysfs.
3526 * Writing "sector length" to this file adds an acknowledged
3527 * bad block list.
3528 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3529 * been acknowledged. Writing to this file adds bad blocks
3530 * without acknowledging them. This is largely for testing.
3531 */
NeilBrown3cb03002011-10-11 16:45:26 +11003532static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003533{
3534 return badblocks_show(&rdev->badblocks, page, 0);
3535}
NeilBrown3cb03002011-10-11 16:45:26 +11003536static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003537{
NeilBrownde393cd2011-07-28 11:31:48 +10003538 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3539 /* Maybe that ack was all we needed */
3540 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3541 wake_up(&rdev->blocked_wait);
3542 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003543}
3544static struct rdev_sysfs_entry rdev_bad_blocks =
3545__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3546
NeilBrown3cb03002011-10-11 16:45:26 +11003547static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003548{
3549 return badblocks_show(&rdev->badblocks, page, 1);
3550}
NeilBrown3cb03002011-10-11 16:45:26 +11003551static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003552{
3553 return badblocks_store(&rdev->badblocks, page, len, 1);
3554}
3555static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3556__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3557
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003558static ssize_t
3559ppl_sector_show(struct md_rdev *rdev, char *page)
3560{
3561 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3562}
3563
3564static ssize_t
3565ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3566{
3567 unsigned long long sector;
3568
3569 if (kstrtoull(buf, 10, &sector) < 0)
3570 return -EINVAL;
3571 if (sector != (sector_t)sector)
3572 return -EINVAL;
3573
3574 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3575 rdev->raid_disk >= 0)
3576 return -EBUSY;
3577
3578 if (rdev->mddev->persistent) {
3579 if (rdev->mddev->major_version == 0)
3580 return -EINVAL;
3581 if ((sector > rdev->sb_start &&
3582 sector - rdev->sb_start > S16_MAX) ||
3583 (sector < rdev->sb_start &&
3584 rdev->sb_start - sector > -S16_MIN))
3585 return -EINVAL;
3586 rdev->ppl.offset = sector - rdev->sb_start;
3587 } else if (!rdev->mddev->external) {
3588 return -EBUSY;
3589 }
3590 rdev->ppl.sector = sector;
3591 return len;
3592}
3593
3594static struct rdev_sysfs_entry rdev_ppl_sector =
3595__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3596
3597static ssize_t
3598ppl_size_show(struct md_rdev *rdev, char *page)
3599{
3600 return sprintf(page, "%u\n", rdev->ppl.size);
3601}
3602
3603static ssize_t
3604ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3605{
3606 unsigned int size;
3607
3608 if (kstrtouint(buf, 10, &size) < 0)
3609 return -EINVAL;
3610
3611 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3612 rdev->raid_disk >= 0)
3613 return -EBUSY;
3614
3615 if (rdev->mddev->persistent) {
3616 if (rdev->mddev->major_version == 0)
3617 return -EINVAL;
3618 if (size > U16_MAX)
3619 return -EINVAL;
3620 } else if (!rdev->mddev->external) {
3621 return -EBUSY;
3622 }
3623 rdev->ppl.size = size;
3624 return len;
3625}
3626
3627static struct rdev_sysfs_entry rdev_ppl_size =
3628__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3629
NeilBrown86e6ffd2005-11-08 21:39:24 -08003630static struct attribute *rdev_default_attrs[] = {
3631 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003632 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003633 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003634 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003635 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003636 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003637 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003638 &rdev_bad_blocks.attr,
3639 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003640 &rdev_ppl_sector.attr,
3641 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003642 NULL,
3643};
3644static ssize_t
3645rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3646{
3647 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003648 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003649
3650 if (!entry->show)
3651 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003652 if (!rdev->mddev)
Marcos Paulo de Souza168b3052019-06-14 15:41:06 -07003653 return -ENODEV;
NeilBrown758bfc82014-12-15 12:56:59 +11003654 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003655}
3656
3657static ssize_t
3658rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3659 const char *page, size_t length)
3660{
3661 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003662 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003663 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003664 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003665
3666 if (!entry->store)
3667 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003668 if (!capable(CAP_SYS_ADMIN))
3669 return -EACCES;
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003670 rv = mddev ? mddev_lock(mddev) : -ENODEV;
NeilBrownca388052008-02-06 01:39:55 -08003671 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003672 if (rdev->mddev == NULL)
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003673 rv = -ENODEV;
NeilBrown27c529b2008-03-04 14:29:33 -08003674 else
3675 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003676 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003677 }
3678 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003679}
3680
3681static void rdev_free(struct kobject *ko)
3682{
NeilBrown3cb03002011-10-11 16:45:26 +11003683 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003684 kfree(rdev);
3685}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003686static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003687 .show = rdev_attr_show,
3688 .store = rdev_attr_store,
3689};
3690static struct kobj_type rdev_ktype = {
3691 .release = rdev_free,
3692 .sysfs_ops = &rdev_sysfs_ops,
3693 .default_attrs = rdev_default_attrs,
3694};
3695
NeilBrown3cb03002011-10-11 16:45:26 +11003696int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003697{
3698 rdev->desc_nr = -1;
3699 rdev->saved_raid_disk = -1;
3700 rdev->raid_disk = -1;
3701 rdev->flags = 0;
3702 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003703 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003704 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003705 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003706 rdev->sb_loaded = 0;
3707 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003708 atomic_set(&rdev->nr_pending, 0);
3709 atomic_set(&rdev->read_errors, 0);
3710 atomic_set(&rdev->corrected_errors, 0);
3711
3712 INIT_LIST_HEAD(&rdev->same_set);
3713 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003714
3715 /* Add space to store bad block list.
3716 * This reserves the space even on arrays where it cannot
3717 * be used - I wonder if that matters
3718 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003719 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003720}
3721EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722/*
3723 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3724 *
3725 * mark the device faulty if:
3726 *
3727 * - the device is nonexistent (zero size)
3728 * - the device has no valid superblock
3729 *
3730 * a faulty rdev _never_ has rdev->sb set.
3731 */
NeilBrown3cb03002011-10-11 16:45:26 +11003732static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
3734 char b[BDEVNAME_SIZE];
3735 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003736 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 sector_t size;
3738
NeilBrown9ffae0c2006-01-06 00:20:32 -08003739 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003740 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
NeilBrown2230dfe2011-07-28 11:31:46 +10003743 err = md_rdev_init(rdev);
3744 if (err)
3745 goto abort_free;
3746 err = alloc_disk_sb(rdev);
3747 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 goto abort_free;
3749
NeilBrownc5d79ad2008-02-06 01:39:54 -08003750 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 if (err)
3752 goto abort_free;
3753
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003754 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003755
Mike Snitzer77304d22010-11-08 14:39:12 +01003756 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003758 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 bdevname(rdev->bdev,b));
3760 err = -EINVAL;
3761 goto abort_free;
3762 }
3763
3764 if (super_format >= 0) {
3765 err = super_types[super_format].
3766 load_super(rdev, NULL, super_minor);
3767 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003768 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003769 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003770 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 goto abort_free;
3772 }
3773 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003774 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 bdevname(rdev->bdev,b));
3776 goto abort_free;
3777 }
3778 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003779
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 return rdev;
3781
3782abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003783 if (rdev->bdev)
3784 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003785 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 kfree(rdev);
3787 return ERR_PTR(err);
3788}
3789
3790/*
3791 * Check a full RAID array for plausibility
3792 */
3793
Yufen Yu6a5cb532019-10-16 16:00:03 +08003794static int analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795{
3796 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003797 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 char b[BDEVNAME_SIZE];
3799
3800 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003801 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 switch (super_types[mddev->major_version].
3803 load_super(rdev, freshest, mddev->minor_version)) {
3804 case 1:
3805 freshest = rdev;
3806 break;
3807 case 0:
3808 break;
3809 default:
NeilBrown9d487392016-11-02 14:16:49 +11003810 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003812 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 }
3814
Yufen Yu6a5cb532019-10-16 16:00:03 +08003815 /* Cannot find a valid fresh disk */
3816 if (!freshest) {
3817 pr_warn("md: cannot find a valid disk\n");
3818 return -EINVAL;
3819 }
3820
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 super_types[mddev->major_version].
3822 validate_super(mddev, freshest);
3823
3824 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003825 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003826 if (mddev->max_disks &&
3827 (rdev->desc_nr >= mddev->max_disks ||
3828 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003829 pr_warn("md: %s: %s: only %d devices permitted\n",
3830 mdname(mddev), bdevname(rdev->bdev, b),
3831 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003832 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003833 continue;
3834 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003835 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 if (super_types[mddev->major_version].
3837 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003838 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003840 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 continue;
3842 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 if (mddev->level == LEVEL_MULTIPATH) {
3845 rdev->desc_nr = i++;
3846 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003847 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003848 } else if (rdev->raid_disk >=
3849 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3850 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003851 rdev->raid_disk = -1;
3852 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 }
3854 }
Yufen Yu6a5cb532019-10-16 16:00:03 +08003855
3856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857}
3858
NeilBrown72e02072009-12-14 12:49:55 +11003859/* Read a fixed-point number.
3860 * Numbers in sysfs attributes should be in "standard" units where
3861 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003862 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003863 * milliseconds or jiffies.
3864 * This function takes a decimal number with a possible fractional
3865 * component, and produces an integer which is the result of
3866 * multiplying that number by 10^'scale'.
3867 * all without any floating-point arithmetic.
3868 */
3869int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3870{
3871 unsigned long result = 0;
3872 long decimals = -1;
3873 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3874 if (*cp == '.')
3875 decimals = 0;
3876 else if (decimals < scale) {
3877 unsigned int value;
3878 value = *cp - '0';
3879 result = result * 10 + value;
3880 if (decimals >= 0)
3881 decimals++;
3882 }
3883 cp++;
3884 }
3885 if (*cp == '\n')
3886 cp++;
3887 if (*cp)
3888 return -EINVAL;
3889 if (decimals < 0)
3890 decimals = 0;
Andy Shevchenkocf891602019-07-23 23:41:55 +03003891 *res = result * int_pow(10, scale - decimals);
NeilBrown72e02072009-12-14 12:49:55 +11003892 return 0;
3893}
3894
NeilBrowneae17012005-11-08 21:39:23 -08003895static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003896safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003897{
3898 int msec = (mddev->safemode_delay*1000)/HZ;
3899 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3900}
3901static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003902safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003903{
NeilBrown16f17b32006-06-26 00:27:37 -07003904 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003905
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003906 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003907 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003908 return -EINVAL;
3909 }
3910
NeilBrown72e02072009-12-14 12:49:55 +11003911 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003912 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003913 if (msec == 0)
3914 mddev->safemode_delay = 0;
3915 else {
NeilBrown19052c02008-08-05 15:54:13 +10003916 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003917 unsigned long new_delay = (msec*HZ)/1000;
3918
3919 if (new_delay == 0)
3920 new_delay = 1;
3921 mddev->safemode_delay = new_delay;
3922 if (new_delay < old_delay || old_delay == 0)
3923 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003924 }
3925 return len;
3926}
3927static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003928__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003929
3930static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003931level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003932{
NeilBrown36d091f2014-12-15 12:56:58 +11003933 struct md_personality *p;
3934 int ret;
3935 spin_lock(&mddev->lock);
3936 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003937 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003938 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003939 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003940 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003941 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003942 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003943 else
NeilBrown36d091f2014-12-15 12:56:58 +11003944 ret = 0;
3945 spin_unlock(&mddev->lock);
3946 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003947}
3948
NeilBrownd9d166c2006-01-06 00:20:51 -08003949static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003950level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003951{
Dan Williamsf2859af2010-05-02 10:04:16 -07003952 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003953 ssize_t rv;
3954 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003955 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003956 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003957 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003958 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003959
NeilBrown67918752014-12-15 12:57:01 +11003960 if (slen == 0 || slen >= sizeof(clevel))
3961 return -EINVAL;
3962
3963 rv = mddev_lock(mddev);
3964 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003965 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003966
3967 if (mddev->pers == NULL) {
3968 strncpy(mddev->clevel, buf, slen);
3969 if (mddev->clevel[slen-1] == '\n')
3970 slen--;
3971 mddev->clevel[slen] = 0;
3972 mddev->level = LEVEL_NONE;
3973 rv = len;
3974 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003975 }
NeilBrown67918752014-12-15 12:57:01 +11003976 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003977 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003978 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003979
3980 /* request to change the personality. Need to ensure:
3981 * - array is not engaged in resync/recovery/reshape
3982 * - old personality can be suspended
3983 * - new personality will access other array.
3984 */
3985
NeilBrown67918752014-12-15 12:57:01 +11003986 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003987 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003988 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003989 mddev->reshape_position != MaxSector ||
3990 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003991 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003992
NeilBrown67918752014-12-15 12:57:01 +11003993 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003994 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003995 pr_warn("md: %s: %s does not support online personality change\n",
3996 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003997 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003998 }
3999
4000 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11004001 strncpy(clevel, buf, slen);
4002 if (clevel[slen-1] == '\n')
4003 slen--;
4004 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004005 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07004006 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11004007
Dan Williamsf2859af2010-05-02 10:04:16 -07004008 if (request_module("md-%s", clevel) != 0)
4009 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11004010 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07004011 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11004012 if (!pers || !try_module_get(pers->owner)) {
4013 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11004014 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11004015 rv = -EINVAL;
4016 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11004017 }
4018 spin_unlock(&pers_lock);
4019
4020 if (pers == mddev->pers) {
4021 /* Nothing to do! */
4022 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11004023 rv = len;
4024 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11004025 }
4026 if (!pers->takeover) {
4027 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11004028 pr_warn("md: %s: %s does not support personality takeover\n",
4029 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11004030 rv = -EINVAL;
4031 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11004032 }
4033
NeilBrowndafb20f2012-03-19 12:46:39 +11004034 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01004035 rdev->new_raid_disk = rdev->raid_disk;
4036
NeilBrown245f46c2009-03-31 14:39:39 +11004037 /* ->takeover must set new_* and/or delta_disks
4038 * if it succeeds, and may set them when it fails.
4039 */
4040 priv = pers->takeover(mddev);
4041 if (IS_ERR(priv)) {
4042 mddev->new_level = mddev->level;
4043 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004044 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11004045 mddev->raid_disks -= mddev->delta_disks;
4046 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004047 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11004048 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11004049 pr_warn("md: %s: %s would not accept array\n",
4050 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11004051 rv = PTR_ERR(priv);
4052 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11004053 }
4054
4055 /* Looks like we have a winner */
4056 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11004057 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11004058
4059 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11004060 oldpers = mddev->pers;
4061 oldpriv = mddev->private;
4062 mddev->pers = pers;
4063 mddev->private = priv;
4064 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4065 mddev->level = mddev->new_level;
4066 mddev->layout = mddev->new_layout;
4067 mddev->chunk_sectors = mddev->new_chunk_sectors;
4068 mddev->delta_disks = 0;
4069 mddev->reshape_backwards = 0;
4070 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11004071 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10004072
NeilBrowndb721d32014-12-15 12:56:58 +11004073 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11004074 mddev->external) {
4075 /* We are converting from a no-redundancy array
4076 * to a redundancy array and metadata is managed
4077 * externally so we need to be sure that writes
4078 * won't block due to a need to transition
4079 * clean->dirty
4080 * until external management is started.
4081 */
4082 mddev->in_sync = 0;
4083 mddev->safemode_delay = 0;
4084 mddev->safemode = 0;
4085 }
4086
NeilBrowndb721d32014-12-15 12:56:58 +11004087 oldpers->free(mddev, oldpriv);
4088
4089 if (oldpers->sync_request == NULL &&
4090 pers->sync_request != NULL) {
4091 /* need to add the md_redundancy_group */
4092 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11004093 pr_warn("md: cannot register extra attributes for %s\n",
4094 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11004095 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
4096 }
4097 if (oldpers->sync_request != NULL &&
4098 pers->sync_request == NULL) {
4099 /* need to remove the md_redundancy_group */
4100 if (mddev->to_remove == NULL)
4101 mddev->to_remove = &md_redundancy_group;
4102 }
4103
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02004104 module_put(oldpers->owner);
4105
NeilBrowndafb20f2012-03-19 12:46:39 +11004106 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004107 if (rdev->raid_disk < 0)
4108 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11004109 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01004110 rdev->new_raid_disk = -1;
4111 if (rdev->new_raid_disk == rdev->raid_disk)
4112 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10004113 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01004114 }
NeilBrowndafb20f2012-03-19 12:46:39 +11004115 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004116 if (rdev->raid_disk < 0)
4117 continue;
4118 if (rdev->new_raid_disk == rdev->raid_disk)
4119 continue;
4120 rdev->raid_disk = rdev->new_raid_disk;
4121 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10004122 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01004123 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10004124 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11004125 pr_warn("md: cannot register rd%d for %s after level change\n",
4126 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10004127 }
NeilBrowne93f68a2010-06-15 09:36:03 +01004128 }
4129
NeilBrowndb721d32014-12-15 12:56:58 +11004130 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11004131 /* this is now an array without redundancy, so
4132 * it must always be in_sync
4133 */
4134 mddev->in_sync = 1;
4135 del_timer_sync(&mddev->safemode_timer);
4136 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11004137 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11004138 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004139 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10004140 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11004141 if (!mddev->thread)
4142 md_update_sb(mddev, 1);
Junxiao Bie1a86db2020-07-14 16:10:26 -07004143 sysfs_notify_dirent_safe(mddev->sysfs_level);
Dan Williamsbb7f8d22010-05-01 18:14:57 -07004144 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004145 rv = len;
4146out_unlock:
4147 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08004148 return rv;
4149}
4150
4151static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07004152__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08004153
NeilBrownd4dbd022006-06-26 00:27:59 -07004154static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004155layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07004156{
4157 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07004158 if (mddev->reshape_position != MaxSector &&
4159 mddev->layout != mddev->new_layout)
4160 return sprintf(page, "%d (%d)\n",
4161 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07004162 return sprintf(page, "%d\n", mddev->layout);
4163}
4164
4165static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004166layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07004167{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004168 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11004169 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07004170
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004171 err = kstrtouint(buf, 10, &n);
4172 if (err < 0)
4173 return err;
NeilBrown67918752014-12-15 12:57:01 +11004174 err = mddev_lock(mddev);
4175 if (err)
4176 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07004177
NeilBrownb3546032009-03-31 14:56:41 +11004178 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10004179 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004180 err = -EBUSY;
4181 else if (mddev->ro)
4182 err = -EROFS;
4183 else {
4184 mddev->new_layout = n;
4185 err = mddev->pers->check_reshape(mddev);
4186 if (err)
4187 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10004188 }
NeilBrownb3546032009-03-31 14:56:41 +11004189 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07004190 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11004191 if (mddev->reshape_position == MaxSector)
4192 mddev->layout = n;
4193 }
NeilBrown67918752014-12-15 12:57:01 +11004194 mddev_unlock(mddev);
4195 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07004196}
4197static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07004198__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07004199
NeilBrowneae17012005-11-08 21:39:23 -08004200static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004201raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08004202{
NeilBrownbb636542005-11-08 21:39:45 -08004203 if (mddev->raid_disks == 0)
4204 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004205 if (mddev->reshape_position != MaxSector &&
4206 mddev->delta_disks != 0)
4207 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
4208 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08004209 return sprintf(page, "%d\n", mddev->raid_disks);
4210}
4211
NeilBrownfd01b882011-10-11 16:47:53 +11004212static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08004213
4214static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004215raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08004216{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004217 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11004218 int err;
NeilBrownda943b992006-01-06 00:20:54 -08004219
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004220 err = kstrtouint(buf, 10, &n);
4221 if (err < 0)
4222 return err;
NeilBrownda943b992006-01-06 00:20:54 -08004223
NeilBrown67918752014-12-15 12:57:01 +11004224 err = mddev_lock(mddev);
4225 if (err)
4226 return err;
NeilBrownda943b992006-01-06 00:20:54 -08004227 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11004228 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07004229 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10004230 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004231 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10004232
NeilBrown67918752014-12-15 12:57:01 +11004233 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10004234 rdev_for_each(rdev, mddev) {
4235 if (olddisks < n &&
4236 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11004237 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10004238 if (olddisks > n &&
4239 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11004240 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10004241 }
NeilBrown67918752014-12-15 12:57:01 +11004242 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004243 mddev->delta_disks = n - olddisks;
4244 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10004245 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07004246 } else
NeilBrownda943b992006-01-06 00:20:54 -08004247 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11004248out_unlock:
4249 mddev_unlock(mddev);
4250 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08004251}
4252static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07004253__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08004254
NeilBrown24dd4692005-11-08 21:39:26 -08004255static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004256chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08004257{
NeilBrown08a02ec2007-05-09 02:35:38 -07004258 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10004259 mddev->chunk_sectors != mddev->new_chunk_sectors)
4260 return sprintf(page, "%d (%d)\n",
4261 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10004262 mddev->chunk_sectors << 9);
4263 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08004264}
4265
4266static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004267chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08004268{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004269 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11004270 int err;
NeilBrown3b343802006-01-06 00:20:47 -08004271
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004272 err = kstrtoul(buf, 10, &n);
4273 if (err < 0)
4274 return err;
NeilBrown3b343802006-01-06 00:20:47 -08004275
NeilBrown67918752014-12-15 12:57:01 +11004276 err = mddev_lock(mddev);
4277 if (err)
4278 return err;
NeilBrownb3546032009-03-31 14:56:41 +11004279 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10004280 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004281 err = -EBUSY;
4282 else if (mddev->ro)
4283 err = -EROFS;
4284 else {
4285 mddev->new_chunk_sectors = n >> 9;
4286 err = mddev->pers->check_reshape(mddev);
4287 if (err)
4288 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10004289 }
NeilBrownb3546032009-03-31 14:56:41 +11004290 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10004291 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004292 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10004293 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004294 }
NeilBrown67918752014-12-15 12:57:01 +11004295 mddev_unlock(mddev);
4296 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08004297}
4298static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004299__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08004300
NeilBrowna94213b2006-06-26 00:28:00 -07004301static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004302resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07004303{
NeilBrownd1a7c502009-03-31 15:24:32 +11004304 if (mddev->recovery_cp == MaxSector)
4305 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07004306 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4307}
4308
4309static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004310resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07004311{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004312 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11004313 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004314
4315 if (cmd_match(buf, "none"))
4316 n = MaxSector;
4317 else {
4318 err = kstrtoull(buf, 10, &n);
4319 if (err < 0)
4320 return err;
4321 if (n != (sector_t)n)
4322 return -EINVAL;
4323 }
NeilBrowna94213b2006-06-26 00:28:00 -07004324
NeilBrown67918752014-12-15 12:57:01 +11004325 err = mddev_lock(mddev);
4326 if (err)
4327 return err;
NeilBrownb0986362011-05-11 15:52:21 +10004328 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11004329 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07004330
NeilBrown67918752014-12-15 12:57:01 +11004331 if (!err) {
4332 mddev->recovery_cp = n;
4333 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08004334 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11004335 }
4336 mddev_unlock(mddev);
4337 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07004338}
4339static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10004340__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4341 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07004342
NeilBrown9e653b62006-06-26 00:27:58 -07004343/*
4344 * The array state can be:
4345 *
4346 * clear
4347 * No devices, no size, no level
4348 * Equivalent to STOP_ARRAY ioctl
4349 * inactive
4350 * May have some settings, but array is not active
4351 * all IO results in error
4352 * When written, doesn't tear down array, but just stops it
4353 * suspended (not supported yet)
4354 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01004355 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07004356 * readonly
4357 * no resync can happen. no superblocks get written.
4358 * write requests fail
4359 * read-auto
4360 * like readonly, but behaves like 'clean' on a write request.
4361 *
4362 * clean - no pending writes, but otherwise active.
4363 * When written to inactive array, starts without resync
4364 * If a write request arrives then
4365 * if metadata is known, mark 'dirty' and switch to 'active'.
4366 * if not known, block and switch to write-pending
4367 * If written to an active array that has pending writes, then fails.
4368 * active
4369 * fully active: IO and resync can be happening.
4370 * When written to inactive array, starts with resync
4371 *
4372 * write-pending
4373 * clean, but writes are blocked waiting for 'active' to be written.
4374 *
4375 * active-idle
4376 * like active, but no writes have been seen for a while (100msec).
4377 *
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004378 * broken
4379 * RAID0/LINEAR-only: same as clean, but array is missing a member.
4380 * It's useful because RAID0/LINEAR mounted-arrays aren't stopped
4381 * when a member is gone, so this state will at least alert the
4382 * user that something is wrong.
NeilBrown9e653b62006-06-26 00:27:58 -07004383 */
4384enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004385 write_pending, active_idle, broken, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004386static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004387 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004388 "write-pending", "active-idle", "broken", NULL };
NeilBrown9e653b62006-06-26 00:27:58 -07004389
4390static int match_word(const char *word, char **list)
4391{
4392 int n;
4393 for (n=0; list[n]; n++)
4394 if (cmd_match(word, list[n]))
4395 break;
4396 return n;
4397}
4398
4399static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004400array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004401{
4402 enum array_state st = inactive;
4403
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004404 if (mddev->pers && !test_bit(MD_NOT_READY, &mddev->flags)) {
NeilBrown9e653b62006-06-26 00:27:58 -07004405 switch(mddev->ro) {
4406 case 1:
4407 st = readonly;
4408 break;
4409 case 2:
4410 st = read_auto;
4411 break;
4412 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004413 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004414 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004415 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004416 else if (mddev->in_sync)
4417 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004418 else if (mddev->safemode)
4419 st = active_idle;
4420 else
4421 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004422 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004423 }
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004424
4425 if (test_bit(MD_BROKEN, &mddev->flags) && st == clean)
4426 st = broken;
4427 } else {
NeilBrown9e653b62006-06-26 00:27:58 -07004428 if (list_empty(&mddev->disks) &&
4429 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004430 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004431 st = clear;
4432 else
4433 st = inactive;
4434 }
4435 return sprintf(page, "%s\n", array_states[st]);
4436}
4437
NeilBrownf72ffdd2014-09-30 14:23:59 +10004438static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4439static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4440static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004441static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004442
4443static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004444array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004445{
NeilBrown6497709b2017-03-15 14:05:14 +11004446 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004447 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004448
4449 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4450 /* don't take reconfig_mutex when toggling between
4451 * clean and active
4452 */
4453 spin_lock(&mddev->lock);
4454 if (st == active) {
4455 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004456 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004457 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004458 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004459 } else /* st == clean */ {
4460 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004461 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004462 err = -EBUSY;
4463 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004464 if (!err)
4465 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004466 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004467 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004468 }
4469 err = mddev_lock(mddev);
4470 if (err)
4471 return err;
4472 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004473 switch(st) {
4474 case bad_word:
4475 break;
4476 case clear:
4477 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004478 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004479 break;
4480 case inactive:
4481 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004482 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004483 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004484 else
NeilBrowne6910632008-02-06 01:39:51 -08004485 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004486 break;
4487 case suspended:
4488 break; /* not supported yet */
4489 case readonly:
4490 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004491 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004492 else {
4493 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004494 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004495 err = do_md_run(mddev);
4496 }
4497 break;
4498 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004499 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004500 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004501 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004502 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004503 err = restart_array(mddev);
4504 if (err == 0) {
4505 mddev->ro = 2;
4506 set_disk_ro(mddev->gendisk, 0);
4507 }
NeilBrown9e653b62006-06-26 00:27:58 -07004508 } else {
4509 mddev->ro = 2;
4510 err = do_md_run(mddev);
4511 }
4512 break;
4513 case clean:
4514 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004515 err = restart_array(mddev);
4516 if (err)
4517 break;
NeilBrown85572d72014-12-15 12:56:56 +11004518 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004519 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004520 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004521 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004522 } else
4523 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004524 break;
4525 case active:
4526 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004527 err = restart_array(mddev);
4528 if (err)
4529 break;
Shaohua Li29530792016-12-08 15:48:19 -08004530 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004531 wake_up(&mddev->sb_wait);
4532 err = 0;
4533 } else {
4534 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004535 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004536 err = do_md_run(mddev);
4537 }
4538 break;
4539 case write_pending:
4540 case active_idle:
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004541 case broken:
NeilBrown9e653b62006-06-26 00:27:58 -07004542 /* these cannot be set */
4543 break;
4544 }
NeilBrown67918752014-12-15 12:57:01 +11004545
4546 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004547 if (mddev->hold_active == UNTIL_IOCTL)
4548 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004549 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004550 }
NeilBrown67918752014-12-15 12:57:01 +11004551 mddev_unlock(mddev);
4552 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004553}
NeilBrown80ca3a42006-07-10 04:44:18 -07004554static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004555__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004556
NeilBrown6d7ff7382006-01-06 00:21:16 -08004557static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004558max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004559 return sprintf(page, "%d\n",
4560 atomic_read(&mddev->max_corr_read_errors));
4561}
4562
4563static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004564max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004565{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004566 unsigned int n;
4567 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004568
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004569 rv = kstrtouint(buf, 10, &n);
4570 if (rv < 0)
4571 return rv;
4572 atomic_set(&mddev->max_corr_read_errors, n);
4573 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004574}
4575
4576static struct md_sysfs_entry max_corr_read_errors =
4577__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4578 max_corrected_read_errors_store);
4579
4580static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004581null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004582{
4583 return -EINVAL;
4584}
4585
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004586/* need to ensure rdev_delayed_delete() has completed */
4587static void flush_rdev_wq(struct mddev *mddev)
4588{
4589 struct md_rdev *rdev;
4590
4591 rcu_read_lock();
4592 rdev_for_each_rcu(rdev, mddev)
4593 if (work_pending(&rdev->del_work)) {
4594 flush_workqueue(md_rdev_misc_wq);
4595 break;
4596 }
4597 rcu_read_unlock();
4598}
4599
NeilBrown6d7ff7382006-01-06 00:21:16 -08004600static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004601new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004602{
4603 /* buf must be %d:%d\n? giving major and minor numbers */
4604 /* The new device is added to the array.
4605 * If the array has a persistent superblock, we read the
4606 * superblock to initialise info and check validity.
4607 * Otherwise, only checking done is that in bind_rdev_to_array,
4608 * which mainly checks size.
4609 */
4610 char *e;
4611 int major = simple_strtoul(buf, &e, 10);
4612 int minor;
4613 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004614 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004615 int err;
4616
4617 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4618 return -EINVAL;
4619 minor = simple_strtoul(e+1, &e, 10);
4620 if (*e && *e != '\n')
4621 return -EINVAL;
4622 dev = MKDEV(major, minor);
4623 if (major != MAJOR(dev) ||
4624 minor != MINOR(dev))
4625 return -EOVERFLOW;
4626
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004627 flush_rdev_wq(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004628 err = mddev_lock(mddev);
4629 if (err)
4630 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004631 if (mddev->persistent) {
4632 rdev = md_import_device(dev, mddev->major_version,
4633 mddev->minor_version);
4634 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004635 struct md_rdev *rdev0
4636 = list_entry(mddev->disks.next,
4637 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004638 err = super_types[mddev->major_version]
4639 .load_super(rdev, rdev0, mddev->minor_version);
4640 if (err < 0)
4641 goto out;
4642 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004643 } else if (mddev->external)
4644 rdev = md_import_device(dev, -2, -1);
4645 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004646 rdev = md_import_device(dev, -1, -1);
4647
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004648 if (IS_ERR(rdev)) {
4649 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004650 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004651 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004652 err = bind_rdev_to_array(rdev, mddev);
4653 out:
4654 if (err)
4655 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004656 mddev_unlock(mddev);
Alexey Obitotskiy5492c462017-07-28 15:49:25 +02004657 if (!err)
4658 md_new_event(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004659 return err ? err : len;
4660}
4661
4662static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004663__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004664
4665static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004666bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004667{
4668 char *end;
4669 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004670 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004671
NeilBrown67918752014-12-15 12:57:01 +11004672 err = mddev_lock(mddev);
4673 if (err)
4674 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004675 if (!mddev->bitmap)
4676 goto out;
4677 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4678 while (*buf) {
4679 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4680 if (buf == end) break;
4681 if (*end == '-') { /* range */
4682 buf = end + 1;
4683 end_chunk = simple_strtoul(buf, &end, 0);
4684 if (buf == end) break;
4685 }
4686 if (*end && !isspace(*end)) break;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004687 md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004688 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004689 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004690 md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
Paul Clements9b1d1da2006-10-03 01:15:49 -07004691out:
NeilBrown67918752014-12-15 12:57:01 +11004692 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004693 return len;
4694}
4695
4696static struct md_sysfs_entry md_bitmap =
4697__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4698
4699static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004700size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004701{
Andre Noll58c0fed2009-03-31 14:33:13 +11004702 return sprintf(page, "%llu\n",
4703 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004704}
4705
NeilBrownfd01b882011-10-11 16:47:53 +11004706static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004707
4708static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004709size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004710{
4711 /* If array is inactive, we can reduce the component size, but
4712 * not increase it (except from 0).
4713 * If array is active, we can try an on-line resize
4714 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004715 sector_t sectors;
4716 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004717
Andre Noll58c0fed2009-03-31 14:33:13 +11004718 if (err < 0)
4719 return err;
NeilBrown67918752014-12-15 12:57:01 +11004720 err = mddev_lock(mddev);
4721 if (err)
4722 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004723 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004724 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004725 if (err == 0)
4726 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004727 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004728 if (mddev->dev_sectors == 0 ||
4729 mddev->dev_sectors > sectors)
4730 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004731 else
4732 err = -ENOSPC;
4733 }
NeilBrown67918752014-12-15 12:57:01 +11004734 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004735 return err ? err : len;
4736}
4737
4738static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004739__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004740
Masanari Iida83f0d772012-10-30 00:18:08 +09004741/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004742 * This is one of
4743 * 'none' for arrays with no metadata (good luck...)
4744 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004745 * or N.M for internally known formats
4746 */
4747static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004748metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004749{
4750 if (mddev->persistent)
4751 return sprintf(page, "%d.%d\n",
4752 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004753 else if (mddev->external)
4754 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004755 else
4756 return sprintf(page, "none\n");
4757}
4758
4759static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004760metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004761{
4762 int major, minor;
4763 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004764 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004765 /* Changing the details of 'external' metadata is
4766 * always permitted. Otherwise there must be
4767 * no devices attached to the array.
4768 */
NeilBrown67918752014-12-15 12:57:01 +11004769
4770 err = mddev_lock(mddev);
4771 if (err)
4772 return err;
4773 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004774 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4775 ;
4776 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004777 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004778
NeilBrown67918752014-12-15 12:57:01 +11004779 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004780 if (cmd_match(buf, "none")) {
4781 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004782 mddev->external = 0;
4783 mddev->major_version = 0;
4784 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004785 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004786 }
4787 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004788 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004789 if (namelen >= sizeof(mddev->metadata_type))
4790 namelen = sizeof(mddev->metadata_type)-1;
4791 strncpy(mddev->metadata_type, buf+9, namelen);
4792 mddev->metadata_type[namelen] = 0;
4793 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4794 mddev->metadata_type[--namelen] = 0;
4795 mddev->persistent = 0;
4796 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004797 mddev->major_version = 0;
4798 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004799 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004800 }
4801 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004802 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004803 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004804 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004805 buf = e+1;
4806 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004807 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004808 goto out_unlock;
4809 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004810 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004811 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004812 mddev->major_version = major;
4813 mddev->minor_version = minor;
4814 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004815 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004816 err = 0;
4817out_unlock:
4818 mddev_unlock(mddev);
4819 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004820}
4821
4822static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004823__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004824
NeilBrowna35b0d62006-01-06 00:20:49 -08004825static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004826action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004827{
NeilBrown7eec3142005-11-08 21:39:44 -08004828 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004829 unsigned long recovery = mddev->recovery;
4830 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004831 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004832 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4833 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4834 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004835 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004836 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4837 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004838 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004839 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004840 type = "check";
4841 else
4842 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004843 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004844 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004845 else if (mddev->reshape_position != MaxSector)
4846 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004847 }
4848 return sprintf(page, "%s\n", type);
4849}
4850
4851static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004852action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004853{
NeilBrown7eec3142005-11-08 21:39:44 -08004854 if (!mddev->pers || !mddev->pers->sync_request)
4855 return -EINVAL;
4856
NeilBrownb6a9ce62009-05-26 09:41:17 +10004857
4858 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004859 if (cmd_match(page, "frozen"))
4860 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4861 else
4862 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004863 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4864 mddev_lock(mddev) == 0) {
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004865 if (work_pending(&mddev->del_work))
4866 flush_workqueue(md_misc_wq);
NeilBrown8e8e2512015-06-12 19:51:27 +10004867 if (mddev->sync_thread) {
4868 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004869 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004870 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004871 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004872 }
NeilBrown312045e2015-12-21 11:01:21 +11004873 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004874 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004875 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004876 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004877 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004878 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004879 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004880 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004881 int err;
4882 if (mddev->pers->start_reshape == NULL)
4883 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004884 err = mddev_lock(mddev);
4885 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004886 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4887 err = -EBUSY;
4888 else {
4889 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4890 err = mddev->pers->start_reshape(mddev);
4891 }
NeilBrown67918752014-12-15 12:57:01 +11004892 mddev_unlock(mddev);
4893 }
NeilBrown16484bf2006-03-27 01:18:13 -08004894 if (err)
4895 return err;
Junxiao Bie1a86db2020-07-14 16:10:26 -07004896 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
NeilBrown16484bf2006-03-27 01:18:13 -08004897 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004898 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004899 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004900 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004901 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004902 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004903 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4904 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004905 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004906 if (mddev->ro == 2) {
4907 /* A write to sync_action is enough to justify
4908 * canceling read-auto mode
4909 */
4910 mddev->ro = 0;
4911 md_wakeup_thread(mddev->sync_thread);
4912 }
NeilBrown03c902e2006-01-06 00:20:46 -08004913 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004914 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004915 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004916 return len;
4917}
4918
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004919static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004920__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004921
4922static ssize_t
4923last_sync_action_show(struct mddev *mddev, char *page)
4924{
4925 return sprintf(page, "%s\n", mddev->last_sync_action);
4926}
4927
4928static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4929
NeilBrown9d888832005-11-08 21:39:26 -08004930static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004931mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004932{
4933 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004934 (unsigned long long)
4935 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004936}
4937
NeilBrown80ca3a42006-07-10 04:44:18 -07004938static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004939
NeilBrown88202a02006-01-06 00:21:36 -08004940static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004941sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004942{
4943 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4944 mddev->sync_speed_min ? "local": "system");
4945}
4946
4947static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004948sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004949{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004950 unsigned int min;
4951 int rv;
4952
NeilBrown88202a02006-01-06 00:21:36 -08004953 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004954 min = 0;
4955 } else {
4956 rv = kstrtouint(buf, 10, &min);
4957 if (rv < 0)
4958 return rv;
4959 if (min == 0)
4960 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004961 }
NeilBrown88202a02006-01-06 00:21:36 -08004962 mddev->sync_speed_min = min;
4963 return len;
4964}
4965
4966static struct md_sysfs_entry md_sync_min =
4967__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4968
4969static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004970sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004971{
4972 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4973 mddev->sync_speed_max ? "local": "system");
4974}
4975
4976static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004977sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004978{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004979 unsigned int max;
4980 int rv;
4981
NeilBrown88202a02006-01-06 00:21:36 -08004982 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004983 max = 0;
4984 } else {
4985 rv = kstrtouint(buf, 10, &max);
4986 if (rv < 0)
4987 return rv;
4988 if (max == 0)
4989 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004990 }
NeilBrown88202a02006-01-06 00:21:36 -08004991 mddev->sync_speed_max = max;
4992 return len;
4993}
4994
4995static struct md_sysfs_entry md_sync_max =
4996__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4997
Iustin Popd7f3d292007-10-16 23:30:54 -07004998static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004999degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07005000{
5001 return sprintf(page, "%d\n", mddev->degraded);
5002}
5003static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08005004
5005static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005006sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07005007{
5008 return sprintf(page, "%d\n", mddev->parallel_resync);
5009}
5010
5011static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005012sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07005013{
5014 long n;
5015
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005016 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07005017 return -EINVAL;
5018
5019 if (n != 0 && n != 1)
5020 return -EINVAL;
5021
5022 mddev->parallel_resync = n;
5023
5024 if (mddev->sync_thread)
5025 wake_up(&resync_wait);
5026
5027 return len;
5028}
5029
5030/* force parallel resync, even with shared block devices */
5031static struct md_sysfs_entry md_sync_force_parallel =
5032__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
5033 sync_force_parallel_show, sync_force_parallel_store);
5034
5035static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005036sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08005037{
5038 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11005039 if (mddev->curr_resync == 0)
5040 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01005041 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
5042 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08005043 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01005044 db = resync - mddev->resync_mark_cnt;
5045 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08005046}
5047
NeilBrown80ca3a42006-07-10 04:44:18 -07005048static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08005049
5050static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005051sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08005052{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11005053 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08005054
NeilBrownacb180b2009-04-14 16:28:34 +10005055 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5056 return sprintf(page, "none\n");
5057
NeilBrown72f36d52012-10-11 14:25:57 +11005058 if (mddev->curr_resync == 1 ||
5059 mddev->curr_resync == 2)
5060 return sprintf(page, "delayed\n");
5061
NeilBrownc804cde2012-05-21 09:28:33 +10005062 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
5063 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11005064 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08005065 else
Andre Noll58c0fed2009-03-31 14:33:13 +11005066 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08005067
NeilBrownacb180b2009-04-14 16:28:34 +10005068 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11005069 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08005070}
5071
NeilBrown750f1992014-09-30 08:53:05 +10005072static struct md_sysfs_entry md_sync_completed =
5073 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08005074
NeilBrowne464eaf2006-03-27 01:18:14 -08005075static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005076min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10005077{
5078 return sprintf(page, "%llu\n",
5079 (unsigned long long)mddev->resync_min);
5080}
5081static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005082min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10005083{
5084 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11005085 int err;
NeilBrown23da4222014-12-15 12:57:01 +11005086
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005087 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10005088 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11005089
5090 spin_lock(&mddev->lock);
5091 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10005092 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11005093 goto out_unlock;
5094
5095 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10005096 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11005097 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10005098
NeilBrown50c37b12015-03-23 17:36:38 +11005099 /* Round down to multiple of 4K for safety */
5100 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11005101 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10005102
NeilBrown23da4222014-12-15 12:57:01 +11005103out_unlock:
5104 spin_unlock(&mddev->lock);
5105 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10005106}
5107
5108static struct md_sysfs_entry md_min_sync =
5109__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
5110
5111static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005112max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08005113{
5114 if (mddev->resync_max == MaxSector)
5115 return sprintf(page, "max\n");
5116 else
5117 return sprintf(page, "%llu\n",
5118 (unsigned long long)mddev->resync_max);
5119}
5120static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005121max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08005122{
NeilBrown23da4222014-12-15 12:57:01 +11005123 int err;
5124 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08005125 if (strncmp(buf, "max", 3) == 0)
5126 mddev->resync_max = MaxSector;
5127 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10005128 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11005129 int chunk;
5130
5131 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005132 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11005133 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10005134 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11005135 goto out_unlock;
5136
5137 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08005138 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10005139 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08005140 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11005141 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08005142
5143 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11005144 chunk = mddev->chunk_sectors;
5145 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10005146 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11005147
5148 err = -EINVAL;
5149 if (sector_div(temp, chunk))
5150 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08005151 }
5152 mddev->resync_max = max;
5153 }
5154 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11005155 err = 0;
5156out_unlock:
5157 spin_unlock(&mddev->lock);
5158 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08005159}
5160
5161static struct md_sysfs_entry md_max_sync =
5162__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
5163
5164static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005165suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08005166{
5167 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
5168}
5169
5170static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005171suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08005172{
NeilBrownb03e0cc2017-10-19 12:49:15 +11005173 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005174 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08005175
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005176 err = kstrtoull(buf, 10, &new);
5177 if (err < 0)
5178 return err;
5179 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08005180 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11005181
NeilBrown67918752014-12-15 12:57:01 +11005182 err = mddev_lock(mddev);
5183 if (err)
5184 return err;
5185 err = -EINVAL;
5186 if (mddev->pers == NULL ||
5187 mddev->pers->quiesce == NULL)
5188 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005189 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11005190 mddev->suspend_lo = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005191 mddev_resume(mddev);
5192
NeilBrown67918752014-12-15 12:57:01 +11005193 err = 0;
5194unlock:
5195 mddev_unlock(mddev);
5196 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08005197}
5198static struct md_sysfs_entry md_suspend_lo =
5199__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
5200
NeilBrowne464eaf2006-03-27 01:18:14 -08005201static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005202suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08005203{
5204 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
5205}
5206
5207static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005208suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08005209{
NeilBrownb03e0cc2017-10-19 12:49:15 +11005210 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005211 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08005212
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005213 err = kstrtoull(buf, 10, &new);
5214 if (err < 0)
5215 return err;
5216 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08005217 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11005218
NeilBrown67918752014-12-15 12:57:01 +11005219 err = mddev_lock(mddev);
5220 if (err)
5221 return err;
5222 err = -EINVAL;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005223 if (mddev->pers == NULL)
NeilBrown67918752014-12-15 12:57:01 +11005224 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005225
5226 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11005227 mddev->suspend_hi = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005228 mddev_resume(mddev);
5229
NeilBrown67918752014-12-15 12:57:01 +11005230 err = 0;
5231unlock:
5232 mddev_unlock(mddev);
5233 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08005234}
5235static struct md_sysfs_entry md_suspend_hi =
5236__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
5237
NeilBrown08a02ec2007-05-09 02:35:38 -07005238static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005239reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07005240{
5241 if (mddev->reshape_position != MaxSector)
5242 return sprintf(page, "%llu\n",
5243 (unsigned long long)mddev->reshape_position);
5244 strcpy(page, "none\n");
5245 return 5;
5246}
5247
5248static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005249reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07005250{
NeilBrownc6563a82012-05-21 09:27:00 +10005251 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005252 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005253 int err;
NeilBrown67918752014-12-15 12:57:01 +11005254
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005255 err = kstrtoull(buf, 10, &new);
5256 if (err < 0)
5257 return err;
5258 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07005259 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11005260 err = mddev_lock(mddev);
5261 if (err)
5262 return err;
5263 err = -EBUSY;
5264 if (mddev->pers)
5265 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07005266 mddev->reshape_position = new;
5267 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005268 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07005269 mddev->new_level = mddev->level;
5270 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10005271 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10005272 rdev_for_each(rdev, mddev)
5273 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11005274 err = 0;
5275unlock:
5276 mddev_unlock(mddev);
5277 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07005278}
5279
5280static struct md_sysfs_entry md_reshape_position =
5281__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
5282 reshape_position_store);
5283
Dan Williamsb522adc2009-03-31 15:00:31 +11005284static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10005285reshape_direction_show(struct mddev *mddev, char *page)
5286{
5287 return sprintf(page, "%s\n",
5288 mddev->reshape_backwards ? "backwards" : "forwards");
5289}
5290
5291static ssize_t
5292reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
5293{
5294 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11005295 int err;
5296
NeilBrown2c810cd2012-05-21 09:27:00 +10005297 if (cmd_match(buf, "forwards"))
5298 backwards = 0;
5299 else if (cmd_match(buf, "backwards"))
5300 backwards = 1;
5301 else
5302 return -EINVAL;
5303 if (mddev->reshape_backwards == backwards)
5304 return len;
5305
NeilBrown67918752014-12-15 12:57:01 +11005306 err = mddev_lock(mddev);
5307 if (err)
5308 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10005309 /* check if we are allowed to change */
5310 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11005311 err = -EBUSY;
5312 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10005313 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11005314 err = -EINVAL;
5315 else
5316 mddev->reshape_backwards = backwards;
5317 mddev_unlock(mddev);
5318 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10005319}
5320
5321static struct md_sysfs_entry md_reshape_direction =
5322__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5323 reshape_direction_store);
5324
5325static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005326array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11005327{
5328 if (mddev->external_size)
5329 return sprintf(page, "%llu\n",
5330 (unsigned long long)mddev->array_sectors/2);
5331 else
5332 return sprintf(page, "default\n");
5333}
5334
5335static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005336array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11005337{
5338 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11005339 int err;
5340
5341 err = mddev_lock(mddev);
5342 if (err)
5343 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11005344
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005345 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08005346 if (mddev_is_clustered(mddev)) {
5347 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005348 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08005349 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005350
Dan Williamsb522adc2009-03-31 15:00:31 +11005351 if (strncmp(buf, "default", 7) == 0) {
5352 if (mddev->pers)
5353 sectors = mddev->pers->size(mddev, 0, 0);
5354 else
5355 sectors = mddev->array_sectors;
5356
5357 mddev->external_size = 0;
5358 } else {
5359 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11005360 err = -EINVAL;
5361 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5362 err = -E2BIG;
5363 else
5364 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11005365 }
5366
NeilBrown67918752014-12-15 12:57:01 +11005367 if (!err) {
5368 mddev->array_sectors = sectors;
5369 if (mddev->pers) {
5370 set_capacity(mddev->gendisk, mddev->array_sectors);
5371 revalidate_disk(mddev->gendisk);
5372 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11005373 }
NeilBrown67918752014-12-15 12:57:01 +11005374 mddev_unlock(mddev);
5375 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11005376}
5377
5378static struct md_sysfs_entry md_array_size =
5379__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5380 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005381
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005382static ssize_t
5383consistency_policy_show(struct mddev *mddev, char *page)
5384{
5385 int ret;
5386
5387 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5388 ret = sprintf(page, "journal\n");
5389 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5390 ret = sprintf(page, "ppl\n");
5391 } else if (mddev->bitmap) {
5392 ret = sprintf(page, "bitmap\n");
5393 } else if (mddev->pers) {
5394 if (mddev->pers->sync_request)
5395 ret = sprintf(page, "resync\n");
5396 else
5397 ret = sprintf(page, "none\n");
5398 } else {
5399 ret = sprintf(page, "unknown\n");
5400 }
5401
5402 return ret;
5403}
5404
5405static ssize_t
5406consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5407{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005408 int err = 0;
5409
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005410 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005411 if (mddev->pers->change_consistency_policy)
5412 err = mddev->pers->change_consistency_policy(mddev, buf);
5413 else
5414 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005415 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5416 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005417 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005418 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005419 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005420
5421 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005422}
5423
5424static struct md_sysfs_entry md_consistency_policy =
5425__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5426 consistency_policy_store);
5427
Guoqing Jiang9a567842019-07-24 11:09:19 +02005428static ssize_t fail_last_dev_show(struct mddev *mddev, char *page)
5429{
5430 return sprintf(page, "%d\n", mddev->fail_last_dev);
5431}
5432
5433/*
5434 * Setting fail_last_dev to true to allow last device to be forcibly removed
5435 * from RAID1/RAID10.
5436 */
5437static ssize_t
5438fail_last_dev_store(struct mddev *mddev, const char *buf, size_t len)
5439{
5440 int ret;
5441 bool value;
5442
5443 ret = kstrtobool(buf, &value);
5444 if (ret)
5445 return ret;
5446
5447 if (value != mddev->fail_last_dev)
5448 mddev->fail_last_dev = value;
5449
5450 return len;
5451}
5452static struct md_sysfs_entry md_fail_last_dev =
5453__ATTR(fail_last_dev, S_IRUGO | S_IWUSR, fail_last_dev_show,
5454 fail_last_dev_store);
5455
Guoqing Jiang3938f5f2019-12-23 10:48:56 +01005456static ssize_t serialize_policy_show(struct mddev *mddev, char *page)
5457{
5458 if (mddev->pers == NULL || (mddev->pers->level != 1))
5459 return sprintf(page, "n/a\n");
5460 else
5461 return sprintf(page, "%d\n", mddev->serialize_policy);
5462}
5463
5464/*
5465 * Setting serialize_policy to true to enforce write IO is not reordered
5466 * for raid1.
5467 */
5468static ssize_t
5469serialize_policy_store(struct mddev *mddev, const char *buf, size_t len)
5470{
5471 int err;
5472 bool value;
5473
5474 err = kstrtobool(buf, &value);
5475 if (err)
5476 return err;
5477
5478 if (value == mddev->serialize_policy)
5479 return len;
5480
5481 err = mddev_lock(mddev);
5482 if (err)
5483 return err;
5484 if (mddev->pers == NULL || (mddev->pers->level != 1)) {
5485 pr_err("md: serialize_policy is only effective for raid1\n");
5486 err = -EINVAL;
5487 goto unlock;
5488 }
5489
5490 mddev_suspend(mddev);
5491 if (value)
5492 mddev_create_serial_pool(mddev, NULL, true);
5493 else
5494 mddev_destroy_serial_pool(mddev, NULL, true);
5495 mddev->serialize_policy = value;
5496 mddev_resume(mddev);
5497unlock:
5498 mddev_unlock(mddev);
5499 return err ?: len;
5500}
5501
5502static struct md_sysfs_entry md_serialize_policy =
5503__ATTR(serialize_policy, S_IRUGO | S_IWUSR, serialize_policy_show,
5504 serialize_policy_store);
5505
5506
NeilBrowneae17012005-11-08 21:39:23 -08005507static struct attribute *md_default_attrs[] = {
5508 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005509 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005510 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005511 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005512 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005513 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005514 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005515 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005516 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005517 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005518 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005519 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005520 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005521 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005522 &md_consistency_policy.attr,
Guoqing Jiang9a567842019-07-24 11:09:19 +02005523 &md_fail_last_dev.attr,
Guoqing Jiang3938f5f2019-12-23 10:48:56 +01005524 &md_serialize_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005525 NULL,
5526};
5527
5528static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005529 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005530 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005531 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005532 &md_sync_min.attr,
5533 &md_sync_max.attr,
5534 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005535 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005536 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005537 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005538 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005539 &md_suspend_lo.attr,
5540 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005541 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005542 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005543 NULL,
5544};
NeilBrown411036f2005-11-08 21:39:40 -08005545static struct attribute_group md_redundancy_group = {
5546 .name = NULL,
5547 .attrs = md_redundancy_attrs,
5548};
5549
NeilBrowneae17012005-11-08 21:39:23 -08005550static ssize_t
5551md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5552{
5553 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005554 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005555 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005556
5557 if (!entry->show)
5558 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005559 spin_lock(&all_mddevs_lock);
5560 if (list_empty(&mddev->all_mddevs)) {
5561 spin_unlock(&all_mddevs_lock);
5562 return -EBUSY;
5563 }
5564 mddev_get(mddev);
5565 spin_unlock(&all_mddevs_lock);
5566
NeilBrownb7b17c92014-12-15 12:56:59 +11005567 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005568 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005569 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005570}
5571
5572static ssize_t
5573md_attr_store(struct kobject *kobj, struct attribute *attr,
5574 const char *page, size_t length)
5575{
5576 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005577 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005578 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005579
5580 if (!entry->store)
5581 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005582 if (!capable(CAP_SYS_ADMIN))
5583 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005584 spin_lock(&all_mddevs_lock);
5585 if (list_empty(&mddev->all_mddevs)) {
5586 spin_unlock(&all_mddevs_lock);
5587 return -EBUSY;
5588 }
5589 mddev_get(mddev);
5590 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005591 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005592 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005593 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005594}
5595
5596static void md_free(struct kobject *ko)
5597{
NeilBrownfd01b882011-10-11 16:47:53 +11005598 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005599
5600 if (mddev->sysfs_state)
5601 sysfs_put(mddev->sysfs_state);
Junxiao Bie1a86db2020-07-14 16:10:26 -07005602 if (mddev->sysfs_completed)
5603 sysfs_put(mddev->sysfs_completed);
5604 if (mddev->sysfs_degraded)
5605 sysfs_put(mddev->sysfs_degraded);
5606 if (mddev->sysfs_level)
5607 sysfs_put(mddev->sysfs_level);
5608
NeilBrowna21d1502009-01-09 08:31:09 +11005609
Bart Van Assched8115c352018-02-28 10:15:29 -08005610 if (mddev->gendisk)
5611 del_gendisk(mddev->gendisk);
NeilBrown6cd18e72015-04-27 14:12:22 +10005612 if (mddev->queue)
5613 blk_cleanup_queue(mddev->queue);
Bart Van Assched8115c352018-02-28 10:15:29 -08005614 if (mddev->gendisk)
NeilBrowna21d1502009-01-09 08:31:09 +11005615 put_disk(mddev->gendisk);
NeilBrown4ad23a972017-03-15 14:05:14 +11005616 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005617
Kent Overstreet28dec872018-06-07 20:52:54 -04005618 bioset_exit(&mddev->bio_set);
5619 bioset_exit(&mddev->sync_set);
Artur Paszkiewicz41d2d842020-07-03 11:13:09 +02005620 mempool_exit(&mddev->md_io_pool);
NeilBrowneae17012005-11-08 21:39:23 -08005621 kfree(mddev);
5622}
5623
Emese Revfy52cf25d2010-01-19 02:58:23 +01005624static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005625 .show = md_attr_show,
5626 .store = md_attr_store,
5627};
5628static struct kobj_type md_ktype = {
5629 .release = md_free,
5630 .sysfs_ops = &md_sysfs_ops,
5631 .default_attrs = md_default_attrs,
5632};
5633
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634int mdp_major = 0;
5635
Dan Williams5fd3a172009-03-04 00:57:25 -07005636static void mddev_delayed_delete(struct work_struct *ws)
5637{
NeilBrownfd01b882011-10-11 16:47:53 +11005638 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005639
NeilBrown43a70502009-12-14 12:49:55 +11005640 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005641 kobject_del(&mddev->kobj);
5642 kobject_put(&mddev->kobj);
5643}
5644
NeilBrown4ad23a972017-03-15 14:05:14 +11005645static void no_op(struct percpu_ref *r) {}
5646
NeilBrowna415c0f2017-06-05 16:05:13 +10005647int mddev_init_writes_pending(struct mddev *mddev)
5648{
5649 if (mddev->writes_pending.percpu_count_ptr)
5650 return 0;
Roman Gushchinddde2af2019-05-07 10:01:49 -07005651 if (percpu_ref_init(&mddev->writes_pending, no_op,
5652 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL) < 0)
NeilBrowna415c0f2017-06-05 16:05:13 +10005653 return -ENOMEM;
5654 /* We want to start with the refcount at zero */
5655 percpu_ref_put(&mddev->writes_pending);
5656 return 0;
5657}
5658EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5659
NeilBrownefeb53c2009-01-09 08:31:10 +11005660static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661{
NeilBrown039b7222017-04-12 16:26:13 +10005662 /*
5663 * If dev is zero, name is the name of a device to allocate with
5664 * an arbitrary minor number. It will be "md_???"
5665 * If dev is non-zero it must be a device number with a MAJOR of
5666 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5667 * the device is being created by opening a node in /dev.
5668 * If "name" is not NULL, the device is being created by
5669 * writing to /sys/module/md_mod/parameters/new_array.
5670 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005671 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005672 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005674 int partitioned;
5675 int shift;
5676 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005677 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678
5679 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005680 return -ENODEV;
5681
5682 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5683 shift = partitioned ? MdpMinorShift : 0;
5684 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685
Tejun Heoe804ac72010-10-15 15:36:08 +02005686 /* wait for any previous instance of this device to be
5687 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005688 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005689 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005690
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005691 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005692 error = -EEXIST;
5693 if (mddev->gendisk)
5694 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005695
NeilBrown039b7222017-04-12 16:26:13 +10005696 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005697 /* Need to ensure that 'name' is not a duplicate.
5698 */
NeilBrownfd01b882011-10-11 16:47:53 +11005699 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005700 spin_lock(&all_mddevs_lock);
5701
5702 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5703 if (mddev2->gendisk &&
5704 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5705 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005706 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005707 }
5708 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 }
NeilBrown039b7222017-04-12 16:26:13 +10005710 if (name && dev)
5711 /*
5712 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5713 */
5714 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005715
Artur Paszkiewicz41d2d842020-07-03 11:13:09 +02005716 error = mempool_init_kmalloc_pool(&mddev->md_io_pool, BIO_POOL_SIZE,
5717 sizeof(struct md_io));
5718 if (error)
5719 goto abort;
5720
NeilBrown0909dc42009-07-01 12:27:21 +10005721 error = -ENOMEM;
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02005722 mddev->queue = blk_alloc_queue(NUMA_NO_NODE);
NeilBrown0909dc42009-07-01 12:27:21 +10005723 if (!mddev->queue)
5724 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005725
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005726 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005727
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 disk = alloc_disk(1 << shift);
5729 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005730 blk_cleanup_queue(mddev->queue);
5731 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005732 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005734 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005736 if (name)
5737 strcpy(disk->disk_name, name);
5738 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005740 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 disk->fops = &md_fops;
5743 disk->private_data = mddev;
5744 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005745 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005746 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005747 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005748 * remove it now.
5749 */
5750 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005752 /* As soon as we call add_disk(), another thread could get
5753 * through to md_open, so make sure it doesn't get too far
5754 */
5755 mutex_lock(&mddev->open_mutex);
5756 add_disk(disk);
5757
Kent Overstreet28dec872018-06-07 20:52:54 -04005758 error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005759 if (error) {
5760 /* This isn't possible, but as kobject_init_and_add is marked
5761 * __must_check, we must do something with the result
5762 */
NeilBrown9d487392016-11-02 14:16:49 +11005763 pr_debug("md: cannot register %s/md - name in use\n",
5764 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005765 error = 0;
5766 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005767 if (mddev->kobj.sd &&
5768 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005769 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005770 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005771 abort:
5772 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005773 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005774 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005775 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
Junxiao Bie1a86db2020-07-14 16:10:26 -07005776 mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed");
5777 mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded");
5778 mddev->sysfs_level = sysfs_get_dirent_safe(mddev->kobj.sd, "level");
NeilBrownb62b7592008-10-21 13:25:21 +11005779 }
NeilBrownd3374822009-01-09 08:31:10 +11005780 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005781 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005782}
5783
5784static struct kobject *md_probe(dev_t dev, int *part, void *data)
5785{
NeilBrown78b63502017-04-12 16:26:13 +10005786 if (create_on_open)
5787 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 return NULL;
5789}
5790
Kees Cooke4dca7b2017-10-17 19:04:42 -07005791static int add_named_array(const char *val, const struct kernel_param *kp)
NeilBrownefeb53c2009-01-09 08:31:10 +11005792{
NeilBrown039b7222017-04-12 16:26:13 +10005793 /*
5794 * val must be "md_*" or "mdNNN".
5795 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005796 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005797 * For "mdNNN" we allocate an array with the minor number NNN
5798 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005799 */
5800 int len = strlen(val);
5801 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005802 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005803
5804 while (len && val[len-1] == '\n')
5805 len--;
5806 if (len >= DISK_NAME_LEN)
5807 return -E2BIG;
5808 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005809 if (strncmp(buf, "md_", 3) == 0)
5810 return md_alloc(0, buf);
5811 if (strncmp(buf, "md", 2) == 0 &&
5812 isdigit(buf[2]) &&
5813 kstrtoul(buf+2, 10, &devnum) == 0 &&
5814 devnum <= MINORMASK)
5815 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5816
5817 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005818}
5819
Kees Cook8376d3c2017-10-16 17:01:48 -07005820static void md_safemode_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821{
Kees Cook8376d3c2017-10-16 17:01:48 -07005822 struct mddev *mddev = from_timer(mddev, t, safemode_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823
NeilBrown4ad23a972017-03-15 14:05:14 +11005824 mddev->safemode = 1;
5825 if (mddev->external)
5826 sysfs_notify_dirent_safe(mddev->sysfs_state);
5827
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 md_wakeup_thread(mddev->thread);
5829}
5830
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005831static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
NeilBrownfd01b882011-10-11 16:47:53 +11005833int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834{
NeilBrown2604b702006-01-06 00:20:36 -08005835 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005836 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005837 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838
NeilBrowna757e642005-04-16 15:26:42 -07005839 if (list_empty(&mddev->disks))
5840 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842
5843 if (mddev->pers)
5844 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005845 /* Cannot run until previous stop completes properly */
5846 if (mddev->sysfs_active)
5847 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005848
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 /*
5850 * Analyze all RAID superblock(s)
5851 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005852 if (!mddev->raid_disks) {
5853 if (!mddev->persistent)
5854 return -EINVAL;
Yufen Yu6a5cb532019-10-16 16:00:03 +08005855 err = analyze_sbs(mddev);
5856 if (err)
5857 return -EINVAL;
NeilBrown1ec4a932008-02-06 01:39:53 -08005858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859
NeilBrownd9d166c2006-01-06 00:20:51 -08005860 if (mddev->level != LEVEL_NONE)
5861 request_module("md-level-%d", mddev->level);
5862 else if (mddev->clevel[0])
5863 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
5865 /*
5866 * Drop all container device buffers, from now on
5867 * the only valid external interface is through the md
5868 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 */
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005870 mddev->has_superblocks = false;
NeilBrowndafb20f2012-03-19 12:46:39 +11005871 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005872 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 continue;
5874 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005875 invalidate_bdev(rdev->bdev);
NeilBrown97b20ef2017-04-13 08:53:48 +10005876 if (mddev->ro != 1 &&
5877 (bdev_read_only(rdev->bdev) ||
5878 bdev_read_only(rdev->meta_bdev))) {
5879 mddev->ro = 1;
5880 if (mddev->gendisk)
5881 set_disk_ro(mddev->gendisk, 1);
5882 }
NeilBrownf0d76d72007-07-17 04:06:12 -07005883
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005884 if (rdev->sb_page)
5885 mddev->has_superblocks = true;
5886
NeilBrownf0d76d72007-07-17 04:06:12 -07005887 /* perform some consistency tests on the device.
5888 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005889 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005890 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005891 if (rdev->meta_bdev) {
5892 /* Nothing to check */;
5893 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005894 if (mddev->dev_sectors &&
5895 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005896 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005897 pr_warn("md: %s: data overlaps metadata\n",
5898 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005899 return -EINVAL;
5900 }
5901 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005902 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005903 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005904 pr_warn("md: %s: metadata overlaps data\n",
5905 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005906 return -EINVAL;
5907 }
5908 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005909 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 }
5911
Kent Overstreetafeee512018-05-20 18:25:52 -04005912 if (!bioset_initialized(&mddev->bio_set)) {
5913 err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5914 if (err)
5915 return err;
Ming Lei10273172017-02-14 23:29:00 +08005916 }
Kent Overstreetafeee512018-05-20 18:25:52 -04005917 if (!bioset_initialized(&mddev->sync_set)) {
5918 err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5919 if (err)
Kent Overstreet28dec872018-06-07 20:52:54 -04005920 return err;
NeilBrown5a850712017-06-21 09:12:21 +10005921 }
NeilBrowna167f662010-10-26 18:31:13 +11005922
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005924 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005925 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005927 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005928 pr_warn("md: personality for level %d is not loaded!\n",
5929 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005930 else
NeilBrown9d487392016-11-02 14:16:49 +11005931 pr_warn("md: personality for level %s is not loaded!\n",
5932 mddev->clevel);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005933 err = -EINVAL;
5934 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005937 if (mddev->level != pers->level) {
5938 mddev->level = pers->level;
5939 mddev->new_level = pers->level;
5940 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005941 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942
NeilBrownf6705572006-03-27 01:18:11 -08005943 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005944 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005945 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005946 module_put(pers->owner);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005947 err = -EINVAL;
5948 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08005949 }
5950
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005951 if (pers->sync_request) {
5952 /* Warn if this is a potentially silly
5953 * configuration.
5954 */
5955 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005956 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005957 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005958
NeilBrowndafb20f2012-03-19 12:46:39 +11005959 rdev_for_each(rdev, mddev)
5960 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005961 if (rdev < rdev2 &&
5962 rdev->bdev->bd_contains ==
5963 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005964 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5965 mdname(mddev),
5966 bdevname(rdev->bdev,b),
5967 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005968 warned = 1;
5969 }
5970 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005971
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005972 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005973 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005974 }
5975
NeilBrown657390d2005-08-26 18:34:16 -07005976 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005977 /* may be over-ridden by personality */
5978 mddev->resync_max_sectors = mddev->dev_sectors;
5979
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005980 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981
NeilBrown0f9552b52009-12-30 12:08:50 +11005982 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005983 mddev->ro = 2; /* read-only, but switch on first write */
5984
NeilBrown36d091f2014-12-15 12:56:58 +11005985 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005986 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005987 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005988 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005989 WARN_ONCE(!mddev->external_size,
5990 "%s: default size too small, but 'external_size' not in effect?\n",
5991 __func__);
5992 pr_warn("md: invalid array_size %llu > default size %llu\n",
5993 (unsigned long long)mddev->array_sectors / 2,
5994 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005995 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005996 }
NeilBrown36d091f2014-12-15 12:56:58 +11005997 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005998 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005999 struct bitmap *bitmap;
6000
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006001 bitmap = md_bitmap_create(mddev, -1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006002 if (IS_ERR(bitmap)) {
6003 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11006004 pr_warn("%s: failed to create bitmap (%d)\n",
6005 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006006 } else
6007 mddev->bitmap = bitmap;
6008
NeilBrownb15c2e52006-01-06 00:20:16 -08006009 }
Guoqing Jiangd4945492019-06-14 17:10:39 +08006010 if (err)
6011 goto bitmap_abort;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08006012
6013 if (mddev->bitmap_info.max_write_behind > 0) {
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01006014 bool create_pool = false;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08006015
6016 rdev_for_each(rdev, mddev) {
6017 if (test_bit(WriteMostly, &rdev->flags) &&
Guoqing Jiang404659c2019-12-23 10:48:53 +01006018 rdev_init_serial(rdev))
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01006019 create_pool = true;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08006020 }
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01006021 if (create_pool && mddev->serial_info_pool == NULL) {
Guoqing Jiang404659c2019-12-23 10:48:53 +01006022 mddev->serial_info_pool =
6023 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
6024 sizeof(struct serial_info));
6025 if (!mddev->serial_info_pool) {
Guoqing Jiang3e148a32019-06-19 17:30:46 +08006026 err = -ENOMEM;
Guoqing Jiangd4945492019-06-14 17:10:39 +08006027 goto bitmap_abort;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08006028 }
6029 }
6030 }
6031
NeilBrown5c675f82014-12-15 12:56:56 +11006032 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07006033 bool nonrot = true;
6034
6035 rdev_for_each(rdev, mddev) {
6036 if (rdev->raid_disk >= 0 &&
6037 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
6038 nonrot = false;
6039 break;
6040 }
6041 }
6042 if (mddev->degraded)
6043 nonrot = false;
6044 if (nonrot)
Bart Van Assche8b904b52018-03-07 17:10:10 -08006045 blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue);
Shaohua Libb086a82016-09-30 09:45:40 -07006046 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08006047 blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01006048 mddev->queue->backing_dev_info->congested_data = mddev;
6049 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11006050 }
NeilBrown36d091f2014-12-15 12:56:58 +11006051 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10006052 if (mddev->kobj.sd &&
6053 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11006054 pr_warn("md: cannot register extra attributes for %s\n",
6055 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10006056 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08006057 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08006058 mddev->ro = 0;
6059
Robert Becker1e509152009-12-14 12:49:58 +11006060 atomic_set(&mddev->max_corr_read_errors,
6061 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006063 if (mddev_is_clustered(mddev))
6064 mddev->safemode_delay = 0;
6065 else
Zhao Heming7c9d5c52020-07-21 02:08:52 +08006066 mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11006068 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11006069 spin_lock(&mddev->lock);
6070 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11006071 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11006072 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10006073 if (rdev->raid_disk >= 0)
Yufen Yue5b521e2019-06-14 15:41:07 -07006074 sysfs_link_rdev(mddev, rdev); /* failure here is OK */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006075
NeilBrowna4a3d262015-07-17 11:57:30 +10006076 if (mddev->degraded && !mddev->ro)
6077 /* This ensures that recovering status is reported immediately
6078 * via sysfs - until a lack of spares is confirmed.
6079 */
6080 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10006082
Shaohua Li29530792016-12-08 15:48:19 -08006083 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07006084 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085
NeilBrownd7603b72006-01-06 00:20:30 -08006086 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 return 0;
Xiao Nib1261942018-01-24 12:17:38 +08006088
Guoqing Jiangd4945492019-06-14 17:10:39 +08006089bitmap_abort:
6090 mddev_detach(mddev);
6091 if (mddev->private)
6092 pers->free(mddev, mddev->private);
6093 mddev->private = NULL;
6094 module_put(pers->owner);
6095 md_bitmap_destroy(mddev);
Xiao Nib1261942018-01-24 12:17:38 +08006096abort:
NeilBrown4bc034d2019-03-29 10:46:16 -07006097 bioset_exit(&mddev->bio_set);
6098 bioset_exit(&mddev->sync_set);
Xiao Nib1261942018-01-24 12:17:38 +08006099 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100}
NeilBrown390ee602010-06-01 19:37:27 +10006101EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102
NeilBrownfd01b882011-10-11 16:47:53 +11006103static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11006104{
6105 int err;
6106
NeilBrown9d4b45d2019-08-20 10:21:09 +10006107 set_bit(MD_NOT_READY, &mddev->flags);
NeilBrownfe60b012010-03-29 11:10:42 +11006108 err = md_run(mddev);
6109 if (err)
6110 goto out;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006111 err = md_bitmap_load(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006112 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006113 md_bitmap_destroy(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006114 goto out;
6115 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05006116
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006117 if (mddev_is_clustered(mddev))
6118 md_allow_write(mddev);
6119
Song Liud5d885f2017-11-19 22:17:01 -08006120 /* run start up tasks that require md_thread */
6121 md_start(mddev);
6122
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05006123 md_wakeup_thread(mddev->thread);
6124 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
6125
NeilBrownfe60b012010-03-29 11:10:42 +11006126 set_capacity(mddev->gendisk, mddev->array_sectors);
6127 revalidate_disk(mddev->gendisk);
NeilBrown9d4b45d2019-08-20 10:21:09 +10006128 clear_bit(MD_NOT_READY, &mddev->flags);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006129 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11006130 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrown9d4b45d2019-08-20 10:21:09 +10006131 sysfs_notify_dirent_safe(mddev->sysfs_state);
6132 sysfs_notify_dirent_safe(mddev->sysfs_action);
Junxiao Bie1a86db2020-07-14 16:10:26 -07006133 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
NeilBrownfe60b012010-03-29 11:10:42 +11006134out:
NeilBrown9d4b45d2019-08-20 10:21:09 +10006135 clear_bit(MD_NOT_READY, &mddev->flags);
NeilBrownfe60b012010-03-29 11:10:42 +11006136 return err;
6137}
6138
Song Liud5d885f2017-11-19 22:17:01 -08006139int md_start(struct mddev *mddev)
6140{
6141 int ret = 0;
6142
6143 if (mddev->pers->start) {
6144 set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6145 md_wakeup_thread(mddev->thread);
6146 ret = mddev->pers->start(mddev);
6147 clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6148 md_wakeup_thread(mddev->sync_thread);
6149 }
6150 return ret;
6151}
6152EXPORT_SYMBOL_GPL(md_start);
6153
NeilBrownfd01b882011-10-11 16:47:53 +11006154static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155{
6156 struct gendisk *disk = mddev->gendisk;
NeilBrown97b20ef2017-04-13 08:53:48 +10006157 struct md_rdev *rdev;
6158 bool has_journal = false;
6159 bool has_readonly = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160
Andre Noll80fab1d2008-07-11 22:02:21 +10006161 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10006163 return -ENXIO;
6164 if (!mddev->pers)
6165 return -EINVAL;
6166 if (!mddev->ro)
6167 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07006168
NeilBrown97b20ef2017-04-13 08:53:48 +10006169 rcu_read_lock();
6170 rdev_for_each_rcu(rdev, mddev) {
6171 if (test_bit(Journal, &rdev->flags) &&
6172 !test_bit(Faulty, &rdev->flags))
6173 has_journal = true;
6174 if (bdev_read_only(rdev->bdev))
6175 has_readonly = true;
Song Liu339421d2015-10-08 21:54:13 -07006176 }
NeilBrown97b20ef2017-04-13 08:53:48 +10006177 rcu_read_unlock();
6178 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
6179 /* Don't restart rw with journal missing/faulty */
6180 return -EINVAL;
6181 if (has_readonly)
6182 return -EROFS;
Song Liu339421d2015-10-08 21:54:13 -07006183
Andre Noll80fab1d2008-07-11 22:02:21 +10006184 mddev->safemode = 0;
6185 mddev->ro = 0;
6186 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11006187 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10006188 /* Kick recovery or resync if necessary */
6189 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6190 md_wakeup_thread(mddev->thread);
6191 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006192 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10006193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194}
6195
NeilBrownfd01b882011-10-11 16:47:53 +11006196static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11006197{
6198 mddev->array_sectors = 0;
6199 mddev->external_size = 0;
6200 mddev->dev_sectors = 0;
6201 mddev->raid_disks = 0;
6202 mddev->recovery_cp = 0;
6203 mddev->resync_min = 0;
6204 mddev->resync_max = MaxSector;
6205 mddev->reshape_position = MaxSector;
6206 mddev->external = 0;
6207 mddev->persistent = 0;
6208 mddev->level = LEVEL_NONE;
6209 mddev->clevel[0] = 0;
6210 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006211 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006212 mddev->ro = 0;
6213 mddev->metadata_type[0] = 0;
6214 mddev->chunk_sectors = 0;
6215 mddev->ctime = mddev->utime = 0;
6216 mddev->layout = 0;
6217 mddev->max_disks = 0;
6218 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10006219 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006220 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006221 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006222 mddev->new_level = LEVEL_NONE;
6223 mddev->new_layout = 0;
6224 mddev->new_chunk_sectors = 0;
6225 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11006226 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11006227 mddev->suspend_lo = mddev->suspend_hi = 0;
6228 mddev->sync_speed_min = mddev->sync_speed_max = 0;
6229 mddev->recovery = 0;
6230 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006231 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006232 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006233 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10006234 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08006235 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11006236 mddev->bitmap_info.offset = 0;
6237 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10006238 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006239 mddev->bitmap_info.chunksize = 0;
6240 mddev->bitmap_info.daemon_sleep = 0;
6241 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08006242 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006243}
6244
NeilBrownfd01b882011-10-11 16:47:53 +11006245static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11006246{
NeilBrown6b6204e2013-05-09 09:48:30 +10006247 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Guoqing Jiang21e09582020-04-04 23:57:07 +02006248 if (work_pending(&mddev->del_work))
6249 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11006250 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11006251 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10006252 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11006253 }
6254
6255 del_timer_sync(&mddev->safemode_timer);
6256
Shaohua Li034e33f2016-11-21 10:29:19 -08006257 if (mddev->pers && mddev->pers->quiesce) {
6258 mddev->pers->quiesce(mddev, 1);
6259 mddev->pers->quiesce(mddev, 0);
6260 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006261 md_bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11006262
NeilBrownb6d428c2013-04-24 11:42:42 +10006263 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006264 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08006265 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11006266 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006267 if (!mddev_is_clustered(mddev))
6268 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11006269 md_update_sb(mddev, 1);
6270 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +01006271 /* disable policy to guarantee rdevs free resources for serialization */
6272 mddev->serialize_policy = 0;
6273 mddev_destroy_serial_pool(mddev, NULL, true);
NeilBrowna047e122010-03-29 12:07:53 +11006274}
NeilBrowndefad612011-01-14 09:14:33 +11006275
NeilBrownfd01b882011-10-11 16:47:53 +11006276void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11006277{
NeilBrown29f097c2013-11-14 17:54:51 +11006278 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11006279 __md_stop_writes(mddev);
6280 mddev_unlock(mddev);
6281}
NeilBrown390ee602010-06-01 19:37:27 +10006282EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11006283
NeilBrown5aa61f42014-12-15 12:56:57 +11006284static void mddev_detach(struct mddev *mddev)
6285{
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006286 md_bitmap_wait_behind_writes(mddev);
Guoqing Jiang6b40bec2020-02-11 11:10:04 +01006287 if (mddev->pers && mddev->pers->quiesce && !mddev->suspended) {
NeilBrown5aa61f42014-12-15 12:56:57 +11006288 mddev->pers->quiesce(mddev, 1);
6289 mddev->pers->quiesce(mddev, 0);
6290 }
6291 md_unregister_thread(&mddev->thread);
6292 if (mddev->queue)
6293 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
6294}
6295
NeilBrown5eff3c42012-11-19 10:47:48 +11006296static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11006297{
NeilBrown36d091f2014-12-15 12:56:58 +11006298 struct md_personality *pers = mddev->pers;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006299 md_bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11006300 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10006301 /* Ensure ->event_work is done */
Guoqing Jiang21e09582020-04-04 23:57:07 +02006302 if (mddev->event_work.func)
6303 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11006304 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11006305 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11006306 spin_unlock(&mddev->lock);
6307 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10006308 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11006309 if (pers->sync_request && mddev->to_remove == NULL)
6310 mddev->to_remove = &md_redundancy_group;
6311 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11006312 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Jack Wang6aaa58c2018-10-19 16:21:31 +02006313}
6314
6315void md_stop(struct mddev *mddev)
6316{
6317 /* stop the array and free an attached data structures.
6318 * This is called from dm-raid
6319 */
6320 __md_stop(mddev);
Kent Overstreetafeee512018-05-20 18:25:52 -04006321 bioset_exit(&mddev->bio_set);
6322 bioset_exit(&mddev->sync_set);
NeilBrown5eff3c42012-11-19 10:47:48 +11006323}
6324
NeilBrown390ee602010-06-01 19:37:27 +10006325EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11006326
NeilBrowna05b7ea2012-07-19 15:59:18 +10006327static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11006328{
6329 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11006330 int did_freeze = 0;
6331
6332 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6333 did_freeze = 1;
6334 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6335 md_wakeup_thread(mddev->thread);
6336 }
NeilBrownf851b602014-12-11 10:02:10 +11006337 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11006338 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006339 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11006340 /* Thread might be blocked waiting for metadata update
6341 * which will now never happen */
6342 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11006343
Shaohua Li29530792016-12-08 15:48:19 -08006344 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10006345 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11006346 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006347 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
6348 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10006349 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08006350 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11006351 mddev_lock_nointr(mddev);
6352
NeilBrowna4bd82d2010-03-29 13:23:10 +11006353 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006354 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006355 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006356 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006357 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11006358 if (did_freeze) {
6359 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006360 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006361 md_wakeup_thread(mddev->thread);
6362 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11006363 err = -EBUSY;
6364 goto out;
6365 }
6366 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11006367 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11006368
6369 err = -ENXIO;
6370 if (mddev->ro==1)
6371 goto out;
6372 mddev->ro = 1;
6373 set_disk_ro(mddev->gendisk, 1);
6374 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006375 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6376 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006377 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11006378 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006379 }
6380out:
6381 mutex_unlock(&mddev->open_mutex);
6382 return err;
6383}
6384
NeilBrown9e653b62006-06-26 00:27:58 -07006385/* mode:
6386 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07006387 * 2 - stop but do not disassemble array
6388 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006389static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10006390 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11006393 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11006394 int did_freeze = 0;
6395
6396 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6397 did_freeze = 1;
6398 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6399 md_wakeup_thread(mddev->thread);
6400 }
NeilBrownf851b602014-12-11 10:02:10 +11006401 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11006402 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006403 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11006404 /* Thread might be blocked waiting for metadata update
6405 * which will now never happen */
6406 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11006407
NeilBrown30b8feb2013-11-14 15:16:17 +11006408 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006409 wait_event(resync_wait, (mddev->sync_thread == NULL &&
6410 !test_bit(MD_RECOVERY_RUNNING,
6411 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11006412 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413
NeilBrownc8c00a62009-08-10 12:50:52 +10006414 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006415 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006416 mddev->sysfs_active ||
6417 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006418 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006419 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10006420 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11006421 if (did_freeze) {
6422 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006423 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006424 md_wakeup_thread(mddev->thread);
6425 }
NeilBrown260fa032013-08-27 16:44:13 +10006426 return -EBUSY;
6427 }
NeilBrown6e17b022010-08-07 21:41:19 +10006428 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11006429 if (mddev->ro)
6430 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11006431
NeilBrowndefad612011-01-14 09:14:33 +11006432 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11006433 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01006434 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11006435
NeilBrowna4bd82d2010-03-29 13:23:10 +11006436 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10006437 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08006438
NeilBrowndafb20f2012-03-19 12:46:39 +11006439 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10006440 if (rdev->raid_disk >= 0)
6441 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10006442
NeilBrowna4bd82d2010-03-29 13:23:10 +11006443 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10006444 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006445 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006446 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08006447
NeilBrowna4bd82d2010-03-29 13:23:10 +11006448 if (mddev->ro)
6449 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10006450 } else
6451 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 /*
6453 * Free resources if final stop
6454 */
NeilBrown9e653b62006-06-26 00:27:58 -07006455 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006456 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457
NeilBrownc3d97142009-12-14 12:49:52 +11006458 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11006459 struct file *f = mddev->bitmap_info.file;
6460 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11006461 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11006462 spin_unlock(&mddev->lock);
6463 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08006464 }
NeilBrownc3d97142009-12-14 12:49:52 +11006465 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08006466
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 export_array(mddev);
6468
NeilBrown6177b472010-03-29 11:37:13 +11006469 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11006470 if (mddev->hold_active == UNTIL_STOP)
6471 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006472 }
NeilBrownd7603b72006-01-06 00:20:30 -08006473 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006474 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10006475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476}
6477
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006478#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11006479static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480{
NeilBrown3cb03002011-10-11 16:45:26 +11006481 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 int err;
6483
NeilBrowna757e642005-04-16 15:26:42 -07006484 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486
NeilBrown9d487392016-11-02 14:16:49 +11006487 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488
NeilBrowndafb20f2012-03-19 12:46:39 +11006489 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11006491 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 }
NeilBrown9d487392016-11-02 14:16:49 +11006493 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494
NeilBrownd710e132008-10-13 11:55:12 +11006495 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006497 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10006498 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 }
6500}
6501
6502/*
6503 * lets try to run arrays based on all disks that have arrived
6504 * until now. (those are in pending_raid_disks)
6505 *
6506 * the method: pick the first pending disk, collect all disks with
6507 * the same UUID, remove all from the pending list and put them into
6508 * the 'same_array' list. Then order this list based on superblock
6509 * update time (freshest comes first), kick out 'old' disks and
6510 * compare superblocks. If everything's fine then run it.
6511 *
6512 * If "unit" is allocated, then bump its reference count
6513 */
6514static void autorun_devices(int part)
6515{
NeilBrown3cb03002011-10-11 16:45:26 +11006516 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006517 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 char b[BDEVNAME_SIZE];
6519
NeilBrown9d487392016-11-02 14:16:49 +11006520 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07006522 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08006524 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11006526 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527
NeilBrown9d487392016-11-02 14:16:49 +11006528 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006530 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006532 pr_debug("md: adding %s ...\n",
6533 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534 list_move(&rdev->same_set, &candidates);
6535 }
6536 /*
6537 * now we have a set of devices, with all of them having
6538 * mostly sane superblocks. It's time to allocate the
6539 * mddev.
6540 */
NeilBrowne8703fe2006-10-03 01:15:59 -07006541 if (part) {
6542 dev = MKDEV(mdp_major,
6543 rdev0->preferred_minor << MdpMinorShift);
6544 unit = MINOR(dev) >> MdpMinorShift;
6545 } else {
6546 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6547 unit = MINOR(dev);
6548 }
6549 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11006550 pr_warn("md: unit number in %s is bad: %d\n",
6551 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552 break;
6553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554
6555 md_probe(dev, NULL, NULL);
6556 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006557 if (!mddev || !mddev->gendisk) {
6558 if (mddev)
6559 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 break;
6561 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006562 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006563 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 else if (mddev->raid_disks || mddev->major_version
6565 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006566 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567 mdname(mddev), bdevname(rdev0->bdev,b));
6568 mddev_unlock(mddev);
6569 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006570 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006571 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006572 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 list_del_init(&rdev->same_set);
6574 if (bind_rdev_to_array(rdev, mddev))
6575 export_rdev(rdev);
6576 }
6577 autorun_array(mddev);
6578 mddev_unlock(mddev);
6579 }
6580 /* on success, candidates will be empty, on error
6581 * it won't...
6582 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006583 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006584 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 mddev_put(mddev);
6588 }
NeilBrown9d487392016-11-02 14:16:49 +11006589 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006591#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592
NeilBrownf72ffdd2014-09-30 14:23:59 +10006593static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594{
6595 mdu_version_t ver;
6596
6597 ver.major = MD_MAJOR_VERSION;
6598 ver.minor = MD_MINOR_VERSION;
6599 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6600
6601 if (copy_to_user(arg, &ver, sizeof(ver)))
6602 return -EFAULT;
6603
6604 return 0;
6605}
6606
NeilBrownf72ffdd2014-09-30 14:23:59 +10006607static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608{
6609 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006610 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006611 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612
NeilBrown1ca69c42012-10-11 13:37:33 +11006613 nr = working = insync = failed = spare = 0;
6614 rcu_read_lock();
6615 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006617 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618 failed++;
6619 else {
6620 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006621 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006622 insync++;
Song Liub347af82016-08-11 17:14:45 -07006623 else if (test_bit(Journal, &rdev->flags))
6624 /* TODO: add journal count to md_u.h */
6625 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 else
6627 spare++;
6628 }
6629 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006630 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631
6632 info.major_version = mddev->major_version;
6633 info.minor_version = mddev->minor_version;
6634 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006635 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006637 info.size = mddev->dev_sectors / 2;
6638 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006639 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640 info.nr_disks = nr;
6641 info.raid_disks = mddev->raid_disks;
6642 info.md_minor = mddev->md_minor;
6643 info.not_persistent= !mddev->persistent;
6644
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006645 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646 info.state = 0;
6647 if (mddev->in_sync)
6648 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006649 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006650 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006651 if (mddev_is_clustered(mddev))
6652 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006653 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 info.working_disks = working;
6655 info.failed_disks = failed;
6656 info.spare_disks = spare;
6657
6658 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006659 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660
6661 if (copy_to_user(arg, &info, sizeof(info)))
6662 return -EFAULT;
6663
6664 return 0;
6665}
6666
NeilBrownf72ffdd2014-09-30 14:23:59 +10006667static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006668{
6669 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006670 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006671 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006672
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006673 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006674 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006675 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006676
NeilBrown32a76272005-06-21 17:17:14 -07006677 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006678 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006679 /* bitmap enabled */
6680 if (mddev->bitmap_info.file) {
6681 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6682 sizeof(file->pathname));
6683 if (IS_ERR(ptr))
6684 err = PTR_ERR(ptr);
6685 else
6686 memmove(file->pathname, ptr,
6687 sizeof(file->pathname)-(ptr-file->pathname));
6688 }
NeilBrown4af1a042014-12-15 12:57:00 +11006689 spin_unlock(&mddev->lock);
6690
6691 if (err == 0 &&
6692 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006693 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006694
NeilBrown32a76272005-06-21 17:17:14 -07006695 kfree(file);
6696 return err;
6697}
6698
NeilBrownf72ffdd2014-09-30 14:23:59 +10006699static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700{
6701 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006702 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703
6704 if (copy_from_user(&info, arg, sizeof(info)))
6705 return -EFAULT;
6706
NeilBrown1ca69c42012-10-11 13:37:33 +11006707 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006708 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 if (rdev) {
6710 info.major = MAJOR(rdev->bdev->bd_dev);
6711 info.minor = MINOR(rdev->bdev->bd_dev);
6712 info.raid_disk = rdev->raid_disk;
6713 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006714 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006716 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717 info.state |= (1<<MD_DISK_ACTIVE);
6718 info.state |= (1<<MD_DISK_SYNC);
6719 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006720 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006721 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006722 if (test_bit(WriteMostly, &rdev->flags))
6723 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006724 if (test_bit(FailFast, &rdev->flags))
6725 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 } else {
6727 info.major = info.minor = 0;
6728 info.raid_disk = -1;
6729 info.state = (1<<MD_DISK_REMOVED);
6730 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006731 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732
6733 if (copy_to_user(arg, &info, sizeof(info)))
6734 return -EFAULT;
6735
6736 return 0;
6737}
6738
NeilBrownf72ffdd2014-09-30 14:23:59 +10006739static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740{
6741 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006742 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743 dev_t dev = MKDEV(info->major,info->minor);
6744
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006745 if (mddev_is_clustered(mddev) &&
6746 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006747 pr_warn("%s: Cannot add to clustered mddev.\n",
6748 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006749 return -EINVAL;
6750 }
6751
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6753 return -EOVERFLOW;
6754
6755 if (!mddev->raid_disks) {
6756 int err;
6757 /* expecting a device which has a superblock */
6758 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6759 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006760 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761 PTR_ERR(rdev));
6762 return PTR_ERR(rdev);
6763 }
6764 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006765 struct md_rdev *rdev0
6766 = list_entry(mddev->disks.next,
6767 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006768 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 .load_super(rdev, rdev0, mddev->minor_version);
6770 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006771 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006772 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 bdevname(rdev0->bdev,b2));
6774 export_rdev(rdev);
6775 return -EINVAL;
6776 }
6777 }
6778 err = bind_rdev_to_array(rdev, mddev);
6779 if (err)
6780 export_rdev(rdev);
6781 return err;
6782 }
6783
6784 /*
6785 * add_new_disk can be used once the array is assembled
6786 * to add "hot spares". They must already have a superblock
6787 * written
6788 */
6789 if (mddev->pers) {
6790 int err;
6791 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006792 pr_warn("%s: personality does not support diskops!\n",
6793 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794 return -EINVAL;
6795 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006796 if (mddev->persistent)
6797 rdev = md_import_device(dev, mddev->major_version,
6798 mddev->minor_version);
6799 else
6800 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006802 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803 PTR_ERR(rdev));
6804 return PTR_ERR(rdev);
6805 }
NeilBrown1a855a02010-12-09 16:36:28 +11006806 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006807 if (!mddev->persistent) {
6808 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006809 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006810 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006811 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006812 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006813 } else
NeilBrown41158c72005-06-21 17:17:25 -07006814 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006815 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006816 } else
6817 super_types[mddev->major_version].
6818 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006819 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006820 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006821 /* This was a hot-add request, but events doesn't
6822 * match, so reject it.
6823 */
6824 export_rdev(rdev);
6825 return -EINVAL;
6826 }
6827
NeilBrownb2d444d2005-11-08 21:39:31 -08006828 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006829 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6830 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006831 else
6832 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006833 if (info->state & (1<<MD_DISK_FAILFAST))
6834 set_bit(FailFast, &rdev->flags);
6835 else
6836 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006837
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006838 if (info->state & (1<<MD_DISK_JOURNAL)) {
6839 struct md_rdev *rdev2;
6840 bool has_journal = false;
6841
6842 /* make sure no existing journal disk */
6843 rdev_for_each(rdev2, mddev) {
6844 if (test_bit(Journal, &rdev2->flags)) {
6845 has_journal = true;
6846 break;
6847 }
6848 }
NeilBrown230b55f2017-10-17 14:24:09 +11006849 if (has_journal || mddev->bitmap) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006850 export_rdev(rdev);
6851 return -EBUSY;
6852 }
Song Liubac624f2015-08-13 14:31:55 -07006853 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006854 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006855 /*
6856 * check whether the device shows up in other nodes
6857 */
6858 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006859 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006860 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006861 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006862 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006863 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006864 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006865 export_rdev(rdev);
6866 return err;
6867 }
6868 }
6869 }
6870
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871 rdev->raid_disk = -1;
6872 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006873
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874 if (err)
6875 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006876
6877 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006878 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6879 if (!err) {
6880 err = md_cluster_ops->new_disk_ack(mddev,
6881 err == 0);
6882 if (err)
6883 md_kick_rdev_from_array(rdev);
6884 }
6885 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006886 if (err)
6887 md_cluster_ops->add_new_disk_cancel(mddev);
6888 else
6889 err = add_bound_rdev(rdev);
6890 }
6891
6892 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006893 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006894
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 return err;
6896 }
6897
6898 /* otherwise, add_new_disk is only allowed
6899 * for major_version==0 superblocks
6900 */
6901 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006902 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 return -EINVAL;
6904 }
6905
6906 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6907 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006908 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006910 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 PTR_ERR(rdev));
6912 return PTR_ERR(rdev);
6913 }
6914 rdev->desc_nr = info->number;
6915 if (info->raid_disk < mddev->raid_disks)
6916 rdev->raid_disk = info->raid_disk;
6917 else
6918 rdev->raid_disk = -1;
6919
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006921 if (info->state & (1<<MD_DISK_SYNC))
6922 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006924 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6925 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006926 if (info->state & (1<<MD_DISK_FAILFAST))
6927 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006928
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006930 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006931 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6932 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006933 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006934 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935
NeilBrown2bf071b2006-01-06 00:20:55 -08006936 err = bind_rdev_to_array(rdev, mddev);
6937 if (err) {
6938 export_rdev(rdev);
6939 return err;
6940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 }
6942
6943 return 0;
6944}
6945
NeilBrownf72ffdd2014-09-30 14:23:59 +10006946static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947{
6948 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006949 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950
Yufen Yuc42a0e22018-05-04 18:08:10 +08006951 if (!mddev->pers)
6952 return -ENODEV;
6953
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954 rdev = find_rdev(mddev, dev);
6955 if (!rdev)
6956 return -ENXIO;
6957
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006958 if (rdev->raid_disk < 0)
6959 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006960
NeilBrown3ea8929d2013-04-24 11:42:41 +10006961 clear_bit(Blocked, &rdev->flags);
6962 remove_and_add_spares(mddev, rdev);
6963
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964 if (rdev->raid_disk >= 0)
6965 goto busy;
6966
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006967kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006968 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006969 md_cluster_ops->remove_disk(mddev, rdev);
6970
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006971 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006972 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006973 if (mddev->thread)
6974 md_wakeup_thread(mddev->thread);
6975 else
6976 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006977 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978
6979 return 0;
6980busy:
NeilBrown9d487392016-11-02 14:16:49 +11006981 pr_debug("md: cannot remove active disk %s from %s ...\n",
6982 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983 return -EBUSY;
6984}
6985
NeilBrownf72ffdd2014-09-30 14:23:59 +10006986static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987{
6988 char b[BDEVNAME_SIZE];
6989 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006990 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991
6992 if (!mddev->pers)
6993 return -ENODEV;
6994
6995 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006996 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 mdname(mddev));
6998 return -EINVAL;
6999 }
7000 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11007001 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002 mdname(mddev));
7003 return -EINVAL;
7004 }
7005
NeilBrownd710e132008-10-13 11:55:12 +11007006 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11007008 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009 PTR_ERR(rdev));
7010 return -EINVAL;
7011 }
7012
7013 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11007014 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015 else
Mike Snitzer77304d22010-11-08 14:39:12 +01007016 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017
NeilBrown8190e752009-06-18 08:48:58 +10007018 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019
NeilBrownb2d444d2005-11-08 21:39:31 -08007020 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11007021 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022 bdevname(rdev->bdev,b), mdname(mddev));
7023 err = -EINVAL;
7024 goto abort_export;
7025 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007026
NeilBrownb2d444d2005-11-08 21:39:31 -08007027 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07007029 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08007030 err = bind_rdev_to_array(rdev, mddev);
7031 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05007032 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033
7034 /*
7035 * The rest should better be atomic, we can have disk failures
7036 * noticed in interrupt contexts ...
7037 */
7038
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 rdev->raid_disk = -1;
7040
Shaohua Li29530792016-12-08 15:48:19 -08007041 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11007042 if (!mddev->thread)
7043 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044 /*
7045 * Kick recovery, maybe this spare has to be added to the
7046 * array immediately.
7047 */
7048 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7049 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08007050 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 return 0;
7052
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053abort_export:
7054 export_rdev(rdev);
7055 return err;
7056}
7057
NeilBrownfd01b882011-10-11 16:47:53 +11007058static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07007059{
NeilBrown035328c2014-04-09 12:25:40 +10007060 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07007061
NeilBrown36fa3062005-09-09 16:23:45 -07007062 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10007063 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07007064 return -EBUSY;
7065 if (mddev->recovery || mddev->sync_thread)
7066 return -EBUSY;
7067 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07007068 }
7069
NeilBrown36fa3062005-09-09 16:23:45 -07007070 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10007071 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11007072 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07007073
NeilBrown1e594bb2014-12-15 12:57:00 +11007074 if (mddev->bitmap || mddev->bitmap_info.file)
7075 return -EEXIST; /* cannot add when bitmap is present */
7076 f = fget(fd);
7077
7078 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11007079 pr_warn("%s: error: failed to get bitmap file\n",
7080 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07007081 return -EBADF;
7082 }
7083
NeilBrown1e594bb2014-12-15 12:57:00 +11007084 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10007085 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11007086 pr_warn("%s: error: bitmap file must be a regular file\n",
7087 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10007088 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11007089 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11007090 pr_warn("%s: error: bitmap file must open for write\n",
7091 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10007092 err = -EBADF;
7093 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11007094 pr_warn("%s: error: bitmap file is already in use\n",
7095 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10007096 err = -EBUSY;
7097 }
7098 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11007099 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07007100 return err;
7101 }
NeilBrown1e594bb2014-12-15 12:57:00 +11007102 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11007103 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07007104 } else if (mddev->bitmap == NULL)
7105 return -ENOENT; /* cannot remove what isn't there */
7106 err = 0;
7107 if (mddev->pers) {
NeilBrown69e51b42010-06-01 19:37:35 +10007108 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007109 struct bitmap *bitmap;
7110
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007111 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007112 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007113 if (!IS_ERR(bitmap)) {
7114 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007115 err = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11007116 } else
7117 err = PTR_ERR(bitmap);
NeilBrown52a0d492017-10-17 13:46:43 +11007118 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007119 md_bitmap_destroy(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11007120 fd = -1;
7121 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007122 mddev_resume(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11007123 } else if (fd < 0) {
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007124 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007125 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007126 mddev_resume(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07007127 }
NeilBrownd7375ab2006-06-26 00:27:43 -07007128 }
7129 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11007130 struct file *f = mddev->bitmap_info.file;
7131 if (f) {
7132 spin_lock(&mddev->lock);
7133 mddev->bitmap_info.file = NULL;
7134 spin_unlock(&mddev->lock);
7135 fput(f);
7136 }
NeilBrown36fa3062005-09-09 16:23:45 -07007137 }
7138
NeilBrown32a76272005-06-21 17:17:14 -07007139 return err;
7140}
7141
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142/*
7143 * set_array_info is used two different ways
7144 * The original usage is when creating a new array.
7145 * In this usage, raid_disks is > 0 and it together with
7146 * level, size, not_persistent,layout,chunksize determine the
7147 * shape of the array.
7148 * This will always create an array with a type-0.90.0 superblock.
7149 * The newer usage is when assembling an array.
7150 * In this case raid_disks will be 0, and the major_version field is
7151 * use to determine which style super-blocks are to be found on the devices.
7152 * The minor and patch _version numbers are also kept incase the
7153 * super_block handler wishes to interpret them.
7154 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10007155static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156{
7157
7158 if (info->raid_disks == 0) {
7159 /* just setting version number for superblock loading */
7160 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07007161 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 super_types[info->major_version].name == NULL) {
7163 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11007164 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165 info->major_version);
7166 return -EINVAL;
7167 }
7168 mddev->major_version = info->major_version;
7169 mddev->minor_version = info->minor_version;
7170 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08007171 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11007172 /* ensure mddev_put doesn't delete this now that there
7173 * is some minimal configuration.
7174 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11007175 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176 return 0;
7177 }
7178 mddev->major_version = MD_MAJOR_VERSION;
7179 mddev->minor_version = MD_MINOR_VERSION;
7180 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11007181 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182
7183 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08007184 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11007185 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186 mddev->raid_disks = info->raid_disks;
7187 /* don't set md_minor, it is determined by which /dev/md* was
7188 * openned
7189 */
7190 if (info->state & (1<<MD_SB_CLEAN))
7191 mddev->recovery_cp = MaxSector;
7192 else
7193 mddev->recovery_cp = 0;
7194 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08007195 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196
7197 mddev->layout = info->layout;
NeilBrown33f2c352019-09-09 16:52:29 +10007198 if (mddev->level == 0)
7199 /* Cannot trust RAID0 layout info here */
7200 mddev->layout = -1;
Andre Noll9d8f0362009-06-18 08:45:01 +10007201 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202
Shaohua Li29530792016-12-08 15:48:19 -08007203 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11007204 mddev->max_disks = MD_SB_DISKS;
7205 mddev->flags = 0;
7206 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08007207 }
7208 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209
NeilBrownc3d97142009-12-14 12:49:52 +11007210 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10007211 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11007212 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08007213
NeilBrownf6705572006-03-27 01:18:11 -08007214 mddev->reshape_position = MaxSector;
7215
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216 /*
7217 * Generate a 128 bit UUID
7218 */
7219 get_random_bytes(mddev->uuid, 16);
7220
NeilBrownf6705572006-03-27 01:18:11 -08007221 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10007222 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08007223 mddev->new_layout = mddev->layout;
7224 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007225 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08007226
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 return 0;
7228}
7229
NeilBrownfd01b882011-10-11 16:47:53 +11007230void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11007231{
Shaohua Liefa4b772017-10-18 22:08:13 -07007232 lockdep_assert_held(&mddev->reconfig_mutex);
Dan Williamsb522adc2009-03-31 15:00:31 +11007233
7234 if (mddev->external_size)
7235 return;
7236
Dan Williams1f403622009-03-31 14:59:03 +11007237 mddev->array_sectors = array_sectors;
7238}
7239EXPORT_SYMBOL(md_set_array_sectors);
7240
NeilBrownfd01b882011-10-11 16:47:53 +11007241static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08007242{
NeilBrown3cb03002011-10-11 16:45:26 +11007243 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08007244 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10007245 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08007246 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04007247
NeilBrowna35b0d62006-01-06 00:20:49 -08007248 if (mddev->pers->resize == NULL)
7249 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10007250 /* The "num_sectors" is the number of sectors of each device that
7251 * is used. This can only make sense for arrays with redundancy.
7252 * linear and raid0 always use whatever space is available. We can only
7253 * consider changing this number if no resync or reconstruction is
7254 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10007255 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10007256 * of each device. If num_sectors is zero, we find the largest size
7257 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08007258 */
NeilBrownf851b602014-12-11 10:02:10 +11007259 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7260 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08007261 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10007262 if (mddev->ro)
7263 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10007264
NeilBrowndafb20f2012-03-19 12:46:39 +11007265 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11007266 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07007267
Andre Nolld71f9f82008-07-11 22:02:22 +10007268 if (fit && (num_sectors == 0 || num_sectors > avail))
7269 num_sectors = avail;
7270 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08007271 return -ENOSPC;
7272 }
Andre Nolld71f9f82008-07-11 22:02:22 +10007273 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08007274 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08007275 if (mddev_is_clustered(mddev))
7276 md_cluster_ops->update_size(mddev, old_dev_sectors);
7277 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08007278 set_capacity(mddev->gendisk, mddev->array_sectors);
7279 revalidate_disk(mddev->gendisk);
7280 }
7281 }
NeilBrowna35b0d62006-01-06 00:20:49 -08007282 return rv;
7283}
7284
NeilBrownfd01b882011-10-11 16:47:53 +11007285static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08007286{
7287 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10007288 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08007289 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08007290 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08007291 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10007292 if (mddev->ro)
7293 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08007294 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10007295 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08007296 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11007297 if (mddev->sync_thread ||
7298 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7299 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08007300 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10007301
7302 rdev_for_each(rdev, mddev) {
7303 if (mddev->raid_disks < raid_disks &&
7304 rdev->data_offset < rdev->new_data_offset)
7305 return -EINVAL;
7306 if (mddev->raid_disks > raid_disks &&
7307 rdev->data_offset > rdev->new_data_offset)
7308 return -EINVAL;
7309 }
7310
NeilBrown63c70c42006-03-27 01:18:13 -08007311 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10007312 if (mddev->delta_disks < 0)
7313 mddev->reshape_backwards = 1;
7314 else if (mddev->delta_disks > 0)
7315 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08007316
7317 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10007318 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11007319 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007320 mddev->reshape_backwards = 0;
7321 }
NeilBrownda943b992006-01-06 00:20:54 -08007322 return rv;
7323}
7324
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325/*
7326 * update_array_info is used to change the configuration of an
7327 * on-line array.
7328 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
7329 * fields in the info are checked against the array.
7330 * Any differences that cannot be handled will cause an error.
7331 * Normally, only one change can be managed at a time.
7332 */
NeilBrownfd01b882011-10-11 16:47:53 +11007333static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334{
7335 int rv = 0;
7336 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007337 int state = 0;
7338
7339 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11007340 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07007341 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342
7343 if (mddev->major_version != info->major_version ||
7344 mddev->minor_version != info->minor_version ||
7345/* mddev->patch_version != info->patch_version || */
7346 mddev->ctime != info->ctime ||
7347 mddev->level != info->level ||
7348/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08007349 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10007350 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07007351 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
7352 ((state^info->state) & 0xfffffe00)
7353 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354 return -EINVAL;
7355 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11007356 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
7357 cnt++;
7358 if (mddev->raid_disks != info->raid_disks)
7359 cnt++;
7360 if (mddev->layout != info->layout)
7361 cnt++;
7362 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
7363 cnt++;
7364 if (cnt == 0)
7365 return 0;
7366 if (cnt > 1)
7367 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368
7369 if (mddev->layout != info->layout) {
7370 /* Change layout
7371 * we don't need to do anything at the md level, the
7372 * personality will take care of it all.
7373 */
NeilBrown50ac1682009-06-18 08:47:55 +10007374 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007376 else {
7377 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10007378 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10007379 if (rv)
7380 mddev->new_layout = mddev->layout;
7381 return rv;
7382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007384 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10007385 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386
NeilBrownda943b992006-01-06 00:20:54 -08007387 if (mddev->raid_disks != info->raid_disks)
7388 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389
NeilBrown36fa3062005-09-09 16:23:45 -07007390 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007391 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
7392 rv = -EINVAL;
7393 goto err;
7394 }
7395 if (mddev->recovery || mddev->sync_thread) {
7396 rv = -EBUSY;
7397 goto err;
7398 }
NeilBrown36fa3062005-09-09 16:23:45 -07007399 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007400 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07007401 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007402 if (mddev->bitmap) {
7403 rv = -EEXIST;
7404 goto err;
7405 }
7406 if (mddev->bitmap_info.default_offset == 0) {
7407 rv = -EINVAL;
7408 goto err;
7409 }
NeilBrownc3d97142009-12-14 12:49:52 +11007410 mddev->bitmap_info.offset =
7411 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10007412 mddev->bitmap_info.space =
7413 mddev->bitmap_info.default_space;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007414 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007415 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007416 if (!IS_ERR(bitmap)) {
7417 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007418 rv = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11007419 } else
7420 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07007421 if (rv)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007422 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007423 mddev_resume(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07007424 } else {
7425 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007426 if (!mddev->bitmap) {
7427 rv = -ENOENT;
7428 goto err;
7429 }
7430 if (mddev->bitmap->storage.file) {
7431 rv = -EINVAL;
7432 goto err;
7433 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11007434 if (mddev->bitmap_info.nodes) {
7435 /* hold PW on all the bitmap lock */
7436 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11007437 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 +11007438 rv = -EPERM;
7439 md_cluster_ops->unlock_all_bitmaps(mddev);
7440 goto err;
7441 }
7442
7443 mddev->bitmap_info.nodes = 0;
7444 md_cluster_ops->leave(mddev);
Zhao Hemingedee9df2020-07-21 02:08:53 +08007445 module_put(md_cluster_mod);
Zhao Heming7c9d5c52020-07-21 02:08:52 +08007446 mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY;
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11007447 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007448 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007449 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007450 mddev_resume(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11007451 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007452 }
7453 }
NeilBrown850b2b422006-10-03 01:15:46 -07007454 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007455 return rv;
7456err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457 return rv;
7458}
7459
NeilBrownfd01b882011-10-11 16:47:53 +11007460static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461{
NeilBrown3cb03002011-10-11 16:45:26 +11007462 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11007463 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464
7465 if (mddev->pers == NULL)
7466 return -ENODEV;
7467
NeilBrown1ca69c42012-10-11 13:37:33 +11007468 rcu_read_lock();
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01007469 rdev = md_find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11007471 err = -ENODEV;
7472 else {
7473 md_error(mddev, rdev);
7474 if (!test_bit(Faulty, &rdev->flags))
7475 err = -EBUSY;
7476 }
7477 rcu_read_unlock();
7478 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479}
7480
Andre Noll2f9618c2008-04-25 18:57:58 +02007481/*
7482 * We have a problem here : there is no easy way to give a CHS
7483 * virtual geometry. We currently pretend that we have a 2 heads
7484 * 4 sectors (with a BIG number of cylinders...). This drives
7485 * dosfs just mad... ;-)
7486 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007487static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7488{
NeilBrownfd01b882011-10-11 16:47:53 +11007489 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007490
7491 geo->heads = 2;
7492 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11007493 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007494 return 0;
7495}
7496
Nicolas Schichancb335f82014-01-15 16:58:52 +01007497static inline bool md_ioctl_valid(unsigned int cmd)
7498{
7499 switch (cmd) {
7500 case ADD_NEW_DISK:
7501 case BLKROSET:
7502 case GET_ARRAY_INFO:
7503 case GET_BITMAP_FILE:
7504 case GET_DISK_INFO:
7505 case HOT_ADD_DISK:
7506 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007507 case RAID_AUTORUN:
7508 case RAID_VERSION:
7509 case RESTART_ARRAY_RW:
7510 case RUN_ARRAY:
7511 case SET_ARRAY_INFO:
7512 case SET_BITMAP_FILE:
7513 case SET_DISK_FAULTY:
7514 case STOP_ARRAY:
7515 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007516 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007517 return true;
7518 default:
7519 return false;
7520 }
7521}
7522
Al Viroa39907f2008-03-02 10:31:15 -05007523static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524 unsigned int cmd, unsigned long arg)
7525{
7526 int err = 0;
7527 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11007528 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10007529 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08007530 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531
Nicolas Schichancb335f82014-01-15 16:58:52 +01007532 if (!md_ioctl_valid(cmd))
7533 return -ENOTTY;
7534
NeilBrown506c9e42011-12-23 10:17:26 +11007535 switch (cmd) {
7536 case RAID_VERSION:
7537 case GET_ARRAY_INFO:
7538 case GET_DISK_INFO:
7539 break;
7540 default:
7541 if (!capable(CAP_SYS_ADMIN))
7542 return -EACCES;
7543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544
7545 /*
7546 * Commands dealing with the RAID driver but not any
7547 * particular array:
7548 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007549 switch (cmd) {
7550 case RAID_VERSION:
7551 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007552 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553
Linus Torvalds1da177e2005-04-16 15:20:36 -07007554#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11007555 case RAID_AUTORUN:
7556 err = 0;
7557 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007558 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11007560 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561 }
7562
7563 /*
7564 * Commands creating/starting a new array:
7565 */
7566
Al Viroa39907f2008-03-02 10:31:15 -05007567 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568
7569 if (!mddev) {
7570 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007571 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572 }
7573
NeilBrown1ca69c42012-10-11 13:37:33 +11007574 /* Some actions do not requires the mutex */
7575 switch (cmd) {
7576 case GET_ARRAY_INFO:
7577 if (!mddev->raid_disks && !mddev->external)
7578 err = -ENODEV;
7579 else
7580 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007581 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007582
7583 case GET_DISK_INFO:
7584 if (!mddev->raid_disks && !mddev->external)
7585 err = -ENODEV;
7586 else
7587 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007588 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007589
7590 case SET_DISK_FAULTY:
7591 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007592 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007593
7594 case GET_BITMAP_FILE:
7595 err = get_bitmap_file(mddev, argp);
7596 goto out;
7597
NeilBrown1ca69c42012-10-11 13:37:33 +11007598 }
7599
Guoqing Jiang78b990c2020-04-04 23:57:10 +02007600 if (cmd == ADD_NEW_DISK || cmd == HOT_ADD_DISK)
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02007601 flush_rdev_wq(mddev);
NeilBrowna7a3f082012-12-11 13:35:54 +11007602
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007603 if (cmd == HOT_REMOVE_DISK)
7604 /* need to ensure recovery thread has run */
7605 wait_event_interruptible_timeout(mddev->sb_wait,
7606 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007607 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007608 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007609 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7610 /* Need to flush page cache, and ensure no-one else opens
7611 * and writes
7612 */
7613 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007614 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007615 mutex_unlock(&mddev->open_mutex);
7616 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007617 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007618 }
NeilBrown065e5192017-04-06 11:16:33 +08007619 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007620 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007621 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007622 mutex_unlock(&mddev->open_mutex);
7623 sync_blockdev(bdev);
7624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625 err = mddev_lock(mddev);
7626 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007627 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7628 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007629 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630 }
7631
NeilBrownc02c0ae2012-12-11 13:39:21 +11007632 if (cmd == SET_ARRAY_INFO) {
7633 mdu_array_info_t info;
7634 if (!arg)
7635 memset(&info, 0, sizeof(info));
7636 else if (copy_from_user(&info, argp, sizeof(info))) {
7637 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007638 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007639 }
7640 if (mddev->pers) {
7641 err = update_array_info(mddev, &info);
7642 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007643 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007644 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007645 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007646 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007647 }
7648 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007649 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007650 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007651 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007652 }
7653 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007654 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007655 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007656 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007657 }
7658 err = set_array_info(mddev, &info);
7659 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007660 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007661 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007662 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007663 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664 }
7665
7666 /*
7667 * Commands querying/configuring an existing array:
7668 */
NeilBrown32a76272005-06-21 17:17:14 -07007669 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007670 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007671 if ((!mddev->raid_disks && !mddev->external)
7672 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7673 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7674 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007675 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007676 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677 }
7678
7679 /*
7680 * Commands even a read-only array can execute:
7681 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007682 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007683 case RESTART_ARRAY_RW:
7684 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007685 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007686
7687 case STOP_ARRAY:
7688 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007689 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007690
7691 case STOP_ARRAY_RO:
7692 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007693 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007694
NeilBrown3ea8929d2013-04-24 11:42:41 +10007695 case HOT_REMOVE_DISK:
7696 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007697 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007698
NeilBrown7ceb17e2013-04-24 11:42:42 +10007699 case ADD_NEW_DISK:
7700 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007701 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007702 * So require mddev->pers and MD_DISK_SYNC.
7703 */
7704 if (mddev->pers) {
7705 mdu_disk_info_t info;
7706 if (copy_from_user(&info, argp, sizeof(info)))
7707 err = -EFAULT;
7708 else if (!(info.state & (1<<MD_DISK_SYNC)))
7709 /* Need to clear read-only for this */
7710 break;
7711 else
7712 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007713 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007714 }
7715 break;
7716
NeilBrownc02c0ae2012-12-11 13:39:21 +11007717 case BLKROSET:
7718 if (get_user(ro, (int __user *)(arg))) {
7719 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007720 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007721 }
7722 err = -EINVAL;
7723
7724 /* if the bdev is going readonly the value of mddev->ro
7725 * does not matter, no writes are coming
7726 */
7727 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007728 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007729
NeilBrownc02c0ae2012-12-11 13:39:21 +11007730 /* are we are already prepared for writes? */
7731 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007732 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007733
7734 /* transitioning to readauto need only happen for
7735 * arrays that call md_write_start
7736 */
7737 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007739 if (err == 0) {
7740 mddev->ro = 2;
7741 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007742 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007743 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007744 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745 }
7746
7747 /*
7748 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007749 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007750 */
NeilBrown326eb172014-09-30 15:36:28 +10007751 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007752 if (mddev->ro == 2) {
7753 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007754 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007755 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007756 /* mddev_unlock will wake thread */
7757 /* If a device failed while we were read-only, we
7758 * need to make sure the metadata is updated now.
7759 */
Shaohua Li29530792016-12-08 15:48:19 -08007760 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007761 mddev_unlock(mddev);
7762 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007763 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7764 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007765 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007766 }
NeilBrownf91de922005-11-08 21:39:36 -08007767 } else {
7768 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007769 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771 }
7772
NeilBrownc02c0ae2012-12-11 13:39:21 +11007773 switch (cmd) {
7774 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007775 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007776 mdu_disk_info_t info;
7777 if (copy_from_user(&info, argp, sizeof(info)))
7778 err = -EFAULT;
7779 else
7780 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007781 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007783
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007784 case CLUSTERED_DISK_NACK:
7785 if (mddev_is_clustered(mddev))
7786 md_cluster_ops->new_disk_ack(mddev, false);
7787 else
7788 err = -EINVAL;
7789 goto unlock;
7790
NeilBrownc02c0ae2012-12-11 13:39:21 +11007791 case HOT_ADD_DISK:
7792 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007793 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007794
NeilBrownc02c0ae2012-12-11 13:39:21 +11007795 case RUN_ARRAY:
7796 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007797 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007798
NeilBrownc02c0ae2012-12-11 13:39:21 +11007799 case SET_BITMAP_FILE:
7800 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007801 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007802
NeilBrownc02c0ae2012-12-11 13:39:21 +11007803 default:
7804 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007805 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007806 }
7807
NeilBrown3adc28d2014-09-30 15:46:41 +10007808unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007809 if (mddev->hold_active == UNTIL_IOCTL &&
7810 err != -EINVAL)
7811 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007813out:
NeilBrown065e5192017-04-06 11:16:33 +08007814 if(did_set_md_closing)
7815 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816 return err;
7817}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007818#ifdef CONFIG_COMPAT
7819static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7820 unsigned int cmd, unsigned long arg)
7821{
7822 switch (cmd) {
7823 case HOT_REMOVE_DISK:
7824 case HOT_ADD_DISK:
7825 case SET_DISK_FAULTY:
7826 case SET_BITMAP_FILE:
7827 /* These take in integer arg, do not convert */
7828 break;
7829 default:
7830 arg = (unsigned long)compat_ptr(arg);
7831 break;
7832 }
7833
7834 return md_ioctl(bdev, mode, cmd, arg);
7835}
7836#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837
Al Viroa39907f2008-03-02 10:31:15 -05007838static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007839{
7840 /*
7841 * Succeed if we can lock the mddev, which confirms that
7842 * it isn't being stopped right now.
7843 */
NeilBrownfd01b882011-10-11 16:47:53 +11007844 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845 int err;
7846
Yuanhan Liu0c098222012-05-22 13:55:32 +10007847 if (!mddev)
7848 return -ENODEV;
7849
NeilBrownd3374822009-01-09 08:31:10 +11007850 if (mddev->gendisk != bdev->bd_disk) {
7851 /* we are racing with mddev_put which is discarding this
7852 * bd_disk.
7853 */
7854 mddev_put(mddev);
7855 /* Wait until bdev->bd_disk is definitely gone */
Guoqing Jiangf6766ff2020-04-04 23:57:09 +02007856 if (work_pending(&mddev->del_work))
7857 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007858 /* Then retry the open from the top */
7859 return -ERESTARTSYS;
7860 }
7861 BUG_ON(mddev != bdev->bd_disk->private_data);
7862
NeilBrownc8c00a62009-08-10 12:50:52 +10007863 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864 goto out;
7865
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007866 if (test_bit(MD_CLOSING, &mddev->flags)) {
7867 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007868 err = -ENODEV;
7869 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007870 }
7871
Linus Torvalds1da177e2005-04-16 15:20:36 -07007872 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007873 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007874 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007876 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007878 if (err)
7879 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007880 return err;
7881}
7882
Al Virodb2a1442013-05-05 21:52:57 -04007883static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007884{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007885 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007887 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007888 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007891
7892static int md_media_changed(struct gendisk *disk)
7893{
NeilBrownfd01b882011-10-11 16:47:53 +11007894 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007895
7896 return mddev->changed;
7897}
7898
7899static int md_revalidate(struct gendisk *disk)
7900{
NeilBrownfd01b882011-10-11 16:47:53 +11007901 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007902
7903 mddev->changed = 0;
7904 return 0;
7905}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007906static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007907{
7908 .owner = THIS_MODULE,
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02007909 .submit_bio = md_submit_bio,
Al Viroa39907f2008-03-02 10:31:15 -05007910 .open = md_open,
7911 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007912 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007913#ifdef CONFIG_COMPAT
7914 .compat_ioctl = md_compat_ioctl,
7915#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007916 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007917 .media_changed = md_media_changed,
7918 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919};
7920
NeilBrownf72ffdd2014-09-30 14:23:59 +10007921static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922{
NeilBrown2b8bf342011-10-11 16:48:23 +11007923 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925 /*
7926 * md_thread is a 'system-thread', it's priority should be very
7927 * high. We avoid resource deadlocks individually in each
7928 * raid personality. (RAID5 does preallocation) We also use RR and
7929 * the very same RT priority as kswapd, thus we will never get
7930 * into a priority inversion deadlock.
7931 *
7932 * we definitely have to have equal or higher priority than
7933 * bdflush, otherwise bdflush will deadlock if there are too
7934 * many dirty RAID5 blocks.
7935 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936
NeilBrown6985c432005-10-19 21:23:47 -07007937 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007938 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007939
NeilBrown93588e22005-11-15 00:09:12 -08007940 /* We need to wait INTERRUPTIBLE so that
7941 * we don't add to the load-average.
7942 * That means we need to be sure no signals are
7943 * pending
7944 */
7945 if (signal_pending(current))
7946 flush_signals(current);
7947
7948 wait_event_interruptible_timeout
7949 (thread->wqueue,
7950 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007951 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007952 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953
NeilBrown6c987912011-01-14 09:13:53 +11007954 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007955 if (kthread_should_park())
7956 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007957 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007958 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007959 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007960
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961 return 0;
7962}
7963
NeilBrown2b8bf342011-10-11 16:48:23 +11007964void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965{
7966 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007967 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Guoqing Jiangd1d90142017-10-09 10:32:48 +08007968 set_bit(THREAD_WAKEUP, &thread->flags);
7969 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007970 }
7971}
NeilBrown6c144d32014-09-30 16:15:38 +10007972EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007973
Shaohua Li4ed87312012-10-11 13:34:00 +11007974struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7975 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976{
NeilBrown2b8bf342011-10-11 16:48:23 +11007977 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007978
NeilBrown2b8bf342011-10-11 16:48:23 +11007979 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007980 if (!thread)
7981 return NULL;
7982
Linus Torvalds1da177e2005-04-16 15:20:36 -07007983 init_waitqueue_head(&thread->wqueue);
7984
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985 thread->run = run;
7986 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007987 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007988 thread->tsk = kthread_run(md_thread, thread,
7989 "%s_%s",
7990 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007991 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007992 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993 kfree(thread);
7994 return NULL;
7995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007996 return thread;
7997}
NeilBrown6c144d32014-09-30 16:15:38 +10007998EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007999
NeilBrown2b8bf342011-10-11 16:48:23 +11008000void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008001{
NeilBrown2b8bf342011-10-11 16:48:23 +11008002 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11008003 if (!thread)
8004 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11008005 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10008006 /* Locking ensures that mddev_unlock does not wake_up a
8007 * non-existent thread
8008 */
8009 spin_lock(&pers_lock);
8010 *threadp = NULL;
8011 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07008012
8013 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008014 kfree(thread);
8015}
NeilBrown6c144d32014-09-30 16:15:38 +10008016EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008017
NeilBrownfd01b882011-10-11 16:47:53 +11008018void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008019{
NeilBrownb2d444d2005-11-08 21:39:31 -08008020 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008021 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07008022
NeilBrownde393cd2011-07-28 11:31:48 +10008023 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024 return;
8025 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10008026 if (mddev->degraded)
8027 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008028 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8030 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8031 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10008032 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02008033 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08008034 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008035}
NeilBrown6c144d32014-09-30 16:15:38 +10008036EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008037
8038/* seq_file implementation /proc/mdstat */
8039
8040static void status_unused(struct seq_file *seq)
8041{
8042 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008043 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008044
8045 seq_printf(seq, "unused devices: ");
8046
Cheng Renquan159ec1f2009-01-09 08:31:08 +11008047 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008048 char b[BDEVNAME_SIZE];
8049 i++;
8050 seq_printf(seq, "%s ",
8051 bdevname(rdev->bdev,b));
8052 }
8053 if (!i)
8054 seq_printf(seq, "<none>");
8055
8056 seq_printf(seq, "\n");
8057}
8058
NeilBrownf7851be2015-07-02 17:12:58 +10008059static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008060{
NeilBrowndd71cf62009-05-07 12:49:35 +10008061 sector_t max_sectors, resync, res;
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008062 unsigned long dt, db = 0;
8063 sector_t rt, curr_mark_cnt, resync_mark_cnt;
8064 int scale, recovery_active;
NeilBrown4588b422006-03-27 01:18:04 -08008065 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008066
NeilBrownc804cde2012-05-21 09:28:33 +10008067 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8068 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10008069 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008070 else
NeilBrowndd71cf62009-05-07 12:49:35 +10008071 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008072
NeilBrownf7851be2015-07-02 17:12:58 +10008073 resync = mddev->curr_resync;
8074 if (resync <= 3) {
8075 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
8076 /* Still cleaning up */
8077 resync = max_sectors;
Nate Daileyd2e2ec82017-11-30 11:33:30 -05008078 } else if (resync > max_sectors)
8079 resync = max_sectors;
8080 else
NeilBrownf7851be2015-07-02 17:12:58 +10008081 resync -= atomic_read(&mddev->recovery_active);
8082
8083 if (resync == 0) {
Guoqing Jiang0357ba22018-07-02 16:26:25 +08008084 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
8085 struct md_rdev *rdev;
8086
8087 rdev_for_each(rdev, mddev)
8088 if (rdev->raid_disk >= 0 &&
8089 !test_bit(Faulty, &rdev->flags) &&
8090 rdev->recovery_offset != MaxSector &&
8091 rdev->recovery_offset) {
8092 seq_printf(seq, "\trecover=REMOTE");
8093 return 1;
8094 }
8095 if (mddev->reshape_position != MaxSector)
8096 seq_printf(seq, "\treshape=REMOTE");
8097 else
8098 seq_printf(seq, "\tresync=REMOTE");
8099 return 1;
8100 }
NeilBrownf7851be2015-07-02 17:12:58 +10008101 if (mddev->recovery_cp < MaxSector) {
8102 seq_printf(seq, "\tresync=PENDING");
8103 return 1;
8104 }
8105 return 0;
8106 }
8107 if (resync < 3) {
8108 seq_printf(seq, "\tresync=DELAYED");
8109 return 1;
8110 }
8111
NeilBrown403df472014-09-30 15:52:29 +10008112 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08008113 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10008114 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08008115 * u32, as those are the requirements for sector_div.
8116 * Thus 'scale' must be at least 10
8117 */
8118 scale = 10;
8119 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10008120 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08008121 scale++;
8122 }
8123 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10008124 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08008125
8126 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008127 {
NeilBrown4588b422006-03-27 01:18:04 -08008128 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008129 seq_printf(seq, "[");
8130 for (i = 0; i < x; i++)
8131 seq_printf(seq, "=");
8132 seq_printf(seq, ">");
8133 for (i = 0; i < y; i++)
8134 seq_printf(seq, ".");
8135 seq_printf(seq, "] ");
8136 }
NeilBrown4588b422006-03-27 01:18:04 -08008137 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08008138 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
8139 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07008140 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
8141 "check" :
8142 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
8143 "resync" : "recovery"))),
8144 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10008145 (unsigned long long) resync/2,
8146 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008147
8148 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07008149 * dt: time from mark until now
8150 * db: blocks written from mark until now
8151 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10008152 *
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008153 * rt is a sector_t, which is always 64bit now. We are keeping
8154 * the original algorithm, but it is not really necessary.
8155 *
8156 * Original algorithm:
8157 * So we divide before multiply in case it is 32bit and close
8158 * to the limit.
8159 * We scale the divisor (db) by 32 to avoid losing precision
8160 * near the end of resync when the number of remaining sectors
8161 * is close to 'db'.
8162 * We then divide rt by 32 after multiplying by db to compensate.
8163 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008164 */
8165 dt = ((jiffies - mddev->resync_mark) / HZ);
8166 if (!dt) dt++;
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008167
8168 curr_mark_cnt = mddev->curr_mark_cnt;
8169 recovery_active = atomic_read(&mddev->recovery_active);
8170 resync_mark_cnt = mddev->resync_mark_cnt;
8171
8172 if (curr_mark_cnt >= (recovery_active + resync_mark_cnt))
8173 db = curr_mark_cnt - (recovery_active + resync_mark_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008174
NeilBrowndd71cf62009-05-07 12:49:35 +10008175 rt = max_sectors - resync; /* number of remaining sectors */
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008176 rt = div64_u64(rt, db/32+1);
NeilBrowndd71cf62009-05-07 12:49:35 +10008177 rt *= dt;
8178 rt >>= 5;
8179
8180 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
8181 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008182
NeilBrownff4e8d92006-07-10 04:44:16 -07008183 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10008184 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008185}
8186
8187static void *md_seq_start(struct seq_file *seq, loff_t *pos)
8188{
8189 struct list_head *tmp;
8190 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11008191 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192
8193 if (l >= 0x10000)
8194 return NULL;
8195 if (!l--)
8196 /* header */
8197 return (void*)1;
8198
8199 spin_lock(&all_mddevs_lock);
8200 list_for_each(tmp,&all_mddevs)
8201 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11008202 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008203 mddev_get(mddev);
8204 spin_unlock(&all_mddevs_lock);
8205 return mddev;
8206 }
8207 spin_unlock(&all_mddevs_lock);
8208 if (!l--)
8209 return (void*)2;/* tail */
8210 return NULL;
8211}
8212
8213static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
8214{
8215 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008216 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10008217
Linus Torvalds1da177e2005-04-16 15:20:36 -07008218 ++*pos;
8219 if (v == (void*)2)
8220 return NULL;
8221
8222 spin_lock(&all_mddevs_lock);
8223 if (v == (void*)1)
8224 tmp = all_mddevs.next;
8225 else
8226 tmp = mddev->all_mddevs.next;
8227 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11008228 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008229 else {
8230 next_mddev = (void*)2;
8231 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10008232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008233 spin_unlock(&all_mddevs_lock);
8234
8235 if (v != (void*)1)
8236 mddev_put(mddev);
8237 return next_mddev;
8238
8239}
8240
8241static void md_seq_stop(struct seq_file *seq, void *v)
8242{
NeilBrownfd01b882011-10-11 16:47:53 +11008243 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008244
8245 if (mddev && v != (void*)1 && v != (void*)2)
8246 mddev_put(mddev);
8247}
8248
8249static int md_seq_show(struct seq_file *seq, void *v)
8250{
NeilBrownfd01b882011-10-11 16:47:53 +11008251 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11008252 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11008253 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008254
8255 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11008256 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008257 seq_printf(seq, "Personalities : ");
8258 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008259 list_for_each_entry(pers, &pers_list, list)
8260 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008261
8262 spin_unlock(&pers_lock);
8263 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02008264 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008265 return 0;
8266 }
8267 if (v == (void*)2) {
8268 status_unused(seq);
8269 return 0;
8270 }
8271
NeilBrown36d091f2014-12-15 12:56:58 +11008272 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008273 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
8274 seq_printf(seq, "%s : %sactive", mdname(mddev),
8275 mddev->pers ? "" : "in");
8276 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08008277 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008278 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08008279 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07008280 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008281 seq_printf(seq, " %s", mddev->pers->name);
8282 }
8283
Andre Nolldd8ac332009-03-31 14:33:13 +11008284 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11008285 rcu_read_lock();
8286 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008287 char b[BDEVNAME_SIZE];
8288 seq_printf(seq, " %s[%d]",
8289 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07008290 if (test_bit(WriteMostly, &rdev->flags))
8291 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07008292 if (test_bit(Journal, &rdev->flags))
8293 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08008294 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008295 seq_printf(seq, "(F)");
8296 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11008297 }
8298 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07008299 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11008300 if (test_bit(Replacement, &rdev->flags))
8301 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11008302 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008303 }
NeilBrownf97fcad2014-12-15 12:56:59 +11008304 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008305
8306 if (!list_empty(&mddev->disks)) {
8307 if (mddev->pers)
8308 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10008309 (unsigned long long)
8310 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008311 else
8312 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11008313 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008314 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07008315 if (mddev->persistent) {
8316 if (mddev->major_version != 0 ||
8317 mddev->minor_version != 90) {
8318 seq_printf(seq," super %d.%d",
8319 mddev->major_version,
8320 mddev->minor_version);
8321 }
NeilBrowne6910632008-02-06 01:39:51 -08008322 } else if (mddev->external)
8323 seq_printf(seq, " super external:%s",
8324 mddev->metadata_type);
8325 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07008326 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008327
8328 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11008329 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10008330 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08008331 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10008332 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08008333 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08008334 }
NeilBrown32a76272005-06-21 17:17:14 -07008335 } else
8336 seq_printf(seq, "\n ");
8337
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008338 md_bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339
8340 seq_printf(seq, "\n");
8341 }
NeilBrown36d091f2014-12-15 12:56:58 +11008342 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10008343
Linus Torvalds1da177e2005-04-16 15:20:36 -07008344 return 0;
8345}
8346
Jan Engelhardt110518b2009-05-07 12:49:37 +10008347static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008348 .start = md_seq_start,
8349 .next = md_seq_next,
8350 .stop = md_seq_stop,
8351 .show = md_seq_show,
8352};
8353
8354static int md_seq_open(struct inode *inode, struct file *file)
8355{
Kay Sieversf1514632011-07-12 20:48:39 +02008356 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008357 int error;
8358
8359 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08008360 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02008361 return error;
8362
8363 seq = file->private_data;
8364 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008365 return error;
8366}
8367
NeilBrowne2f23b62014-04-09 14:33:51 +10008368static int md_unloading;
Al Viroafc9a422017-07-03 06:39:46 -04008369static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
NeilBrownd7603b72006-01-06 00:20:30 -08008370{
Kay Sieversf1514632011-07-12 20:48:39 +02008371 struct seq_file *seq = filp->private_data;
Al Viroafc9a422017-07-03 06:39:46 -04008372 __poll_t mask;
NeilBrownd7603b72006-01-06 00:20:30 -08008373
NeilBrowne2f23b62014-04-09 14:33:51 +10008374 if (md_unloading)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008375 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08008376 poll_wait(filp, &md_event_waiters, wait);
8377
8378 /* always allow read */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008379 mask = EPOLLIN | EPOLLRDNORM;
NeilBrownd7603b72006-01-06 00:20:30 -08008380
Kay Sieversf1514632011-07-12 20:48:39 +02008381 if (seq->poll_event != atomic_read(&md_event_count))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008382 mask |= EPOLLERR | EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08008383 return mask;
8384}
8385
Alexey Dobriyan97a32532020-02-03 17:37:17 -08008386static const struct proc_ops mdstat_proc_ops = {
8387 .proc_open = md_seq_open,
8388 .proc_read = seq_read,
8389 .proc_lseek = seq_lseek,
8390 .proc_release = seq_release,
8391 .proc_poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008392};
8393
NeilBrown84fc4b52011-10-11 16:49:58 +11008394int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008395{
NeilBrown9d487392016-11-02 14:16:49 +11008396 pr_debug("md: %s personality registered for level %d\n",
8397 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008398 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008399 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008400 spin_unlock(&pers_lock);
8401 return 0;
8402}
NeilBrown6c144d32014-09-30 16:15:38 +10008403EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404
NeilBrown84fc4b52011-10-11 16:49:58 +11008405int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008406{
NeilBrown9d487392016-11-02 14:16:49 +11008407 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008409 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008410 spin_unlock(&pers_lock);
8411 return 0;
8412}
NeilBrown6c144d32014-09-30 16:15:38 +10008413EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414
NeilBrown6022e752015-08-13 12:32:55 +10008415int register_md_cluster_operations(struct md_cluster_operations *ops,
8416 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008417{
NeilBrown6022e752015-08-13 12:32:55 +10008418 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008419 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008420 if (md_cluster_ops != NULL)
8421 ret = -EALREADY;
8422 else {
8423 md_cluster_ops = ops;
8424 md_cluster_mod = module;
8425 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008426 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008427 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008428}
8429EXPORT_SYMBOL(register_md_cluster_operations);
8430
8431int unregister_md_cluster_operations(void)
8432{
8433 spin_lock(&pers_lock);
8434 md_cluster_ops = NULL;
8435 spin_unlock(&pers_lock);
8436 return 0;
8437}
8438EXPORT_SYMBOL(unregister_md_cluster_operations);
8439
8440int md_setup_cluster(struct mddev *mddev, int nodes)
8441{
Zhao Heming7c9d5c52020-07-21 02:08:52 +08008442 int ret;
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008443 if (!md_cluster_ops)
8444 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008445 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008446 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008447 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11008448 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008449 spin_unlock(&pers_lock);
8450 return -ENOENT;
8451 }
8452 spin_unlock(&pers_lock);
8453
Zhao Heming7c9d5c52020-07-21 02:08:52 +08008454 ret = md_cluster_ops->join(mddev, nodes);
8455 if (!ret)
8456 mddev->safemode_delay = 0;
8457 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008458}
8459
8460void md_cluster_stop(struct mddev *mddev)
8461{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05008462 if (!md_cluster_ops)
8463 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008464 md_cluster_ops->leave(mddev);
8465 module_put(md_cluster_mod);
8466}
8467
NeilBrownfd01b882011-10-11 16:47:53 +11008468static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008469{
NeilBrownf72ffdd2014-09-30 14:23:59 +10008470 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11008472 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008473
8474 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10008475 rcu_read_lock();
8476 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008477 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Michael Callahan59767fb2018-07-18 04:47:37 -07008478 curr_events = (int)part_stat_read_accum(&disk->part0, sectors) -
NeilBrowneea1bf32009-03-31 14:27:02 +11008479 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07008480 /* sync IO will cause sync_io to increase before the disk_stats
8481 * as sync_io is counted when a request starts, and
8482 * disk_stats is counted when it completes.
8483 * So resync activity will cause curr_events to be smaller than
8484 * when there was no such activity.
8485 * non-sync IO will cause disk_stat to increase without
8486 * increasing sync_io so curr_events will (eventually)
8487 * be larger than it was before. Once it becomes
8488 * substantially larger, the test below will cause
8489 * the array to appear non-idle, and resync will slow
8490 * down.
8491 * If there is a lot of outstanding resync activity when
8492 * we set last_event to curr_events, then all that activity
8493 * completing might cause the array to appear non-idle
8494 * and resync will be slowed down even though there might
8495 * not have been non-resync activity. This will only
8496 * happen once though. 'last_events' will soon reflect
8497 * the state where there is little or no outstanding
8498 * resync requests, and further resync activity will
8499 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08008500 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 */
NeilBrowneea1bf32009-03-31 14:27:02 +11008502 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008503 rdev->last_events = curr_events;
8504 idle = 0;
8505 }
8506 }
NeilBrown4b809912008-07-21 17:05:25 +10008507 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008508 return idle;
8509}
8510
NeilBrownfd01b882011-10-11 16:47:53 +11008511void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008512{
8513 /* another "blocks" (512byte) blocks have been synced */
8514 atomic_sub(blocks, &mddev->recovery_active);
8515 wake_up(&mddev->recovery_wait);
8516 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07008517 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08008518 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008519 md_wakeup_thread(mddev->thread);
8520 // stop recovery, signal do_sync ....
8521 }
8522}
NeilBrown6c144d32014-09-30 16:15:38 +10008523EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008524
NeilBrown06d91a52005-06-21 17:17:12 -07008525/* md_write_start(mddev, bi)
8526 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07008527 * in superblock) before writing, schedule a superblock update
8528 * and wait for it to complete.
NeilBrowncc27b0c2017-06-05 16:49:39 +10008529 * A return value of 'false' means that the write wasn't recorded
8530 * and cannot proceed as the array is being suspend.
NeilBrown06d91a52005-06-21 17:17:12 -07008531 */
NeilBrowncc27b0c2017-06-05 16:49:39 +10008532bool md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533{
Neil Brown0fd62b82008-06-28 08:31:36 +10008534 int did_change = 0;
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008535
NeilBrown06d91a52005-06-21 17:17:12 -07008536 if (bio_data_dir(bi) != WRITE)
NeilBrowncc27b0c2017-06-05 16:49:39 +10008537 return true;
NeilBrown06d91a52005-06-21 17:17:12 -07008538
NeilBrownf91de922005-11-08 21:39:36 -08008539 BUG_ON(mddev->ro == 1);
8540 if (mddev->ro == 2) {
8541 /* need to switch to read/write */
8542 mddev->ro = 0;
8543 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8544 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08008545 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008546 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08008547 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008548 rcu_read_lock();
8549 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11008550 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07008551 if (mddev->safemode == 1)
8552 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11008553 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
NeilBrown81fe48e2017-08-08 16:56:36 +10008554 if (mddev->in_sync || mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11008555 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07008556 if (mddev->in_sync) {
8557 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008558 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8559 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07008560 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008561 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07008562 }
NeilBrown85572d72014-12-15 12:56:56 +11008563 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07008564 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008565 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10008566 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008567 sysfs_notify_dirent_safe(mddev->sysfs_state);
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008568 if (!mddev->has_superblocks)
8569 return true;
NeilBrown09a44cc2008-05-23 13:04:36 -07008570 wait_event(mddev->sb_wait,
NeilBrownd47c8ad2017-10-05 16:23:16 +11008571 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8572 mddev->suspended);
NeilBrowncc27b0c2017-06-05 16:49:39 +10008573 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8574 percpu_ref_put(&mddev->writes_pending);
8575 return false;
8576 }
8577 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008578}
NeilBrown6c144d32014-09-30 16:15:38 +10008579EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008580
NeilBrown49728052017-03-15 14:05:12 +11008581/* md_write_inc can only be called when md_write_start() has
8582 * already been called at least once of the current request.
8583 * It increments the counter and is useful when a single request
8584 * is split into several parts. Each part causes an increment and
8585 * so needs a matching md_write_end().
8586 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8587 * a spinlocked region.
8588 */
8589void md_write_inc(struct mddev *mddev, struct bio *bi)
8590{
8591 if (bio_data_dir(bi) != WRITE)
8592 return;
8593 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11008594 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008595}
8596EXPORT_SYMBOL(md_write_inc);
8597
NeilBrownfd01b882011-10-11 16:47:53 +11008598void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008599{
NeilBrown4ad23a972017-03-15 14:05:14 +11008600 percpu_ref_put(&mddev->writes_pending);
8601
8602 if (mddev->safemode == 2)
8603 md_wakeup_thread(mddev->thread);
8604 else if (mddev->safemode_delay)
8605 /* The roundup() ensures this only performs locking once
8606 * every ->safemode_delay jiffies
8607 */
8608 mod_timer(&mddev->safemode_timer,
8609 roundup(jiffies, mddev->safemode_delay) +
8610 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008611}
NeilBrown4ad23a972017-03-15 14:05:14 +11008612
NeilBrown6c144d32014-09-30 16:15:38 +10008613EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008614
NeilBrown2a2275d2007-01-26 00:57:11 -08008615/* md_allow_write(mddev)
8616 * Calling this ensures that the array is marked 'active' so that writes
8617 * may proceed without blocking. It is important to call this before
8618 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8619 * Must be called with mddev_lock held.
8620 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008621void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008622{
8623 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008624 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008625 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008626 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008627 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008628 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008629
NeilBrown85572d72014-12-15 12:56:56 +11008630 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008631 if (mddev->in_sync) {
8632 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008633 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8634 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008635 if (mddev->safemode_delay &&
8636 mddev->safemode == 0)
8637 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008638 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008639 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008640 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008641 /* wait for the dirty state to be recorded in the metadata */
8642 wait_event(mddev->sb_wait,
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008643 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008644 } else
NeilBrown85572d72014-12-15 12:56:56 +11008645 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008646}
8647EXPORT_SYMBOL_GPL(md_allow_write);
8648
Linus Torvalds1da177e2005-04-16 15:20:36 -07008649#define SYNC_MARKS 10
8650#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008651#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008652void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008653{
Shaohua Li4ed87312012-10-11 13:34:00 +11008654 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008655 struct mddev *mddev2;
Yufen Yue5b521e2019-06-14 15:41:07 -07008656 unsigned int currspeed = 0, window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008657 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008658 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008659 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008660 sector_t mark_cnt[SYNC_MARKS];
8661 int last_mark,m;
8662 struct list_head *tmp;
8663 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008664 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008665 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008666 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008667 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008668 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008669
8670 /* just incase thread restarts... */
Song Liud5d885f2017-11-19 22:17:01 -08008671 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8672 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008673 return;
NeilBrown3991b312014-05-28 13:39:23 +10008674 if (mddev->ro) {/* never try to sync a read-only array */
8675 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008676 return;
NeilBrown3991b312014-05-28 13:39:23 +10008677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008678
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008679 if (mddev_is_clustered(mddev)) {
8680 ret = md_cluster_ops->resync_start(mddev);
8681 if (ret)
8682 goto skip;
8683
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008684 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008685 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8686 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8687 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8688 && ((unsigned long long)mddev->curr_resync_completed
8689 < (unsigned long long)mddev->resync_max_sectors))
8690 goto skip;
8691 }
8692
NeilBrown61df9d92006-10-03 01:15:57 -07008693 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008694 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008695 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008696 action = "check";
8697 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008698 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008699 action = "repair";
8700 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008701 desc = "resync";
8702 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8703 desc = "reshape";
8704 else
8705 desc = "recovery";
8706
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008707 mddev->last_sync_action = action ?: desc;
8708
Linus Torvalds1da177e2005-04-16 15:20:36 -07008709 /* we overload curr_resync somewhat here.
8710 * 0 == not engaged in resync at all
8711 * 2 == checking that there is no conflict with another sync
8712 * 1 == like 2, but have yielded to allow conflicting resync to
Yufen Yue5b521e2019-06-14 15:41:07 -07008713 * commence
Linus Torvalds1da177e2005-04-16 15:20:36 -07008714 * other == active in resync - this many blocks
8715 *
8716 * Before starting a resync we must have set curr_resync to
8717 * 2, and then checked that every "conflicting" array has curr_resync
8718 * less than ours. When we find one that is the same or higher
8719 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8720 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8721 * This will mean we have to start checking from the beginning again.
8722 *
8723 */
8724
8725 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008726 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008727 mddev->curr_resync = 2;
8728
8729 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008730 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008731 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008732 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008733 if (mddev2 == mddev)
8734 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008735 if (!mddev->parallel_resync
8736 && mddev2->curr_resync
8737 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008738 DEFINE_WAIT(wq);
8739 if (mddev < mddev2 && mddev->curr_resync == 2) {
8740 /* arbitrarily yield */
8741 mddev->curr_resync = 1;
8742 wake_up(&resync_wait);
8743 }
8744 if (mddev > mddev2 && mddev->curr_resync == 1)
8745 /* no need to wait here, we can wait the next
8746 * time 'round when curr_resync == 2
8747 */
8748 continue;
NeilBrown97441972008-09-19 11:49:54 +10008749 /* We need to wait 'interruptible' so as not to
8750 * contribute to the load average, and not to
8751 * be caught by 'softlockup'
8752 */
8753 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008754 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008755 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008756 if (mddev2_minor != mddev2->md_minor) {
8757 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008758 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8759 desc, mdname(mddev),
8760 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008762 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008763 if (signal_pending(current))
8764 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008765 schedule();
8766 finish_wait(&resync_wait, &wq);
8767 goto try_again;
8768 }
8769 finish_wait(&resync_wait, &wq);
8770 }
8771 }
8772 } while (mddev->curr_resync < 2);
8773
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008774 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008775 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008776 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008777 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008778 */
8779 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008780 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008781 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008782 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8783 j = mddev->resync_min;
8784 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008785 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008786
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008787 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
NeilBrownc804cde2012-05-21 09:28:33 +10008788 max_sectors = mddev->resync_max_sectors;
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008789 /*
8790 * If the original node aborts reshaping then we continue the
8791 * reshaping, so set j again to avoid restart reshape from the
8792 * first beginning
8793 */
8794 if (mddev_is_clustered(mddev) &&
8795 mddev->reshape_position != MaxSector)
8796 j = mddev->reshape_position;
8797 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008798 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008799 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008800 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008801 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008802 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008803 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008804 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008805 !test_bit(Faulty, &rdev->flags) &&
8806 !test_bit(In_sync, &rdev->flags) &&
8807 rdev->recovery_offset < j)
8808 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008809 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008810
8811 /* If there is a bitmap, we need to make sure all
8812 * writes that started before we added a spare
8813 * complete before we start doing a recovery.
8814 * Otherwise the write might complete and (via
8815 * bitmap_endwrite) set a bit in the bitmap after the
8816 * recovery has checked that bit and skipped that
8817 * region.
8818 */
8819 if (mddev->bitmap) {
8820 mddev->pers->quiesce(mddev, 1);
8821 mddev->pers->quiesce(mddev, 0);
8822 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008824
NeilBrown9d487392016-11-02 14:16:49 +11008825 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8826 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8827 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8828 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008829
NeilBrowneea1bf32009-03-31 14:27:02 +11008830 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008831
NeilBrown57afd892005-06-21 17:17:13 -07008832 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008833 for (m = 0; m < SYNC_MARKS; m++) {
8834 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008835 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008836 }
8837 last_mark = 0;
8838 mddev->resync_mark = mark[last_mark];
8839 mddev->resync_mark_cnt = mark_cnt[last_mark];
8840
8841 /*
8842 * Tune reconstruction:
8843 */
Yufen Yue5b521e2019-06-14 15:41:07 -07008844 window = 32 * (PAGE_SIZE / 512);
NeilBrown9d487392016-11-02 14:16:49 +11008845 pr_debug("md: using %dk window, over a total of %lluk.\n",
8846 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008847
8848 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008849 last_check = 0;
8850
8851 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008852 pr_debug("md: resuming %s of %s from checkpoint.\n",
8853 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008854 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008855 } else
8856 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008857 mddev->curr_resync_completed = j;
Junxiao Bie1a86db2020-07-14 16:10:26 -07008858 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown72f36d52012-10-11 14:25:57 +11008859 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008860 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008861
majianpeng7c2c57c2012-07-03 12:12:26 +10008862 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008863 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008864 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008865
NeilBrown57afd892005-06-21 17:17:13 -07008866 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008867
NeilBrown7a91ee12009-05-26 12:57:21 +10008868 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8869 ((mddev->curr_resync > mddev->curr_resync_completed &&
8870 (mddev->curr_resync - mddev->curr_resync_completed)
8871 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008872 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008873 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008874 >= mddev->resync_max - mddev->curr_resync_completed ||
8875 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008876 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008877 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008878 wait_event(mddev->recovery_wait,
8879 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008880 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008881 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8882 j > mddev->recovery_cp)
8883 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008884 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008885 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
Junxiao Bie1a86db2020-07-14 16:10:26 -07008886 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown97e4f422009-03-31 14:33:13 +11008887 }
NeilBrownacb180b2009-04-14 16:28:34 +10008888
NeilBrownc91abf52013-11-19 12:02:01 +11008889 while (j >= mddev->resync_max &&
8890 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008891 /* As this condition is controlled by user-space,
8892 * we can block indefinitely, so use '_interruptible'
8893 * to avoid triggering warnings.
8894 */
8895 flush_signals(current); /* just in case */
8896 wait_event_interruptible(mddev->recovery_wait,
8897 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008898 || test_bit(MD_RECOVERY_INTR,
8899 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008900 }
NeilBrownacb180b2009-04-14 16:28:34 +10008901
NeilBrownc91abf52013-11-19 12:02:01 +11008902 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8903 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008904
NeilBrown09314792015-02-19 16:04:40 +11008905 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008906 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008907 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008908 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008909 }
NeilBrown57afd892005-06-21 17:17:13 -07008910
8911 if (!skipped) { /* actual IO requested */
8912 io_sectors += sectors;
8913 atomic_add(sectors, &mddev->recovery_active);
8914 }
8915
NeilBrowne875ece2011-07-28 11:39:24 +10008916 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8917 break;
8918
Linus Torvalds1da177e2005-04-16 15:20:36 -07008919 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008920 if (j > max_sectors)
8921 /* when skipping, extra large numbers can be returned. */
8922 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008923 if (j > 2)
8924 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008925 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008926 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008927 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008928 * visible in /proc/mdstat
8929 */
8930 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008931
8932 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008933 continue;
8934
NeilBrown57afd892005-06-21 17:17:13 -07008935 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008936 repeat:
8937 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8938 /* step marks */
8939 int next = (last_mark+1) % SYNC_MARKS;
8940
8941 mddev->resync_mark = mark[next];
8942 mddev->resync_mark_cnt = mark_cnt[next];
8943 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008944 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945 last_mark = next;
8946 }
8947
NeilBrownc91abf52013-11-19 12:02:01 +11008948 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8949 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008950
8951 /*
8952 * this loop exits only if either when we are slower than
8953 * the 'hard' speed limit, or the system was IO-idle for
8954 * a jiffy.
8955 * the system might be non-idle CPU-wise, but we only care
8956 * about not overloading the IO subsystem. (things like an
8957 * e2fsck being done on the RAID array should execute fast)
8958 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008959 cond_resched();
8960
Xiao Niac7e50a2014-08-07 09:37:41 -04008961 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8962 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008963 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008964
NeilBrown88202a02006-01-06 00:21:36 -08008965 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008966 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008967 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008968 goto repeat;
8969 }
NeilBrownac8fa412015-02-19 16:55:00 +11008970 if (!is_mddev_idle(mddev, 0)) {
8971 /*
8972 * Give other IO more of a chance.
8973 * The faster the devices, the less we wait.
8974 */
8975 wait_event(mddev->recovery_wait,
8976 !atomic_read(&mddev->recovery_active));
8977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008978 }
8979 }
NeilBrown9d487392016-11-02 14:16:49 +11008980 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8981 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8982 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008983 /*
8984 * this also signals 'finished resyncing' to md_stop
8985 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008986 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008987 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8988
NeilBrown5ed1df22015-07-24 13:27:08 +10008989 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8990 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008991 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008992 mddev->curr_resync_completed = mddev->curr_resync;
Junxiao Bie1a86db2020-07-14 16:10:26 -07008993 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown5ed1df22015-07-24 13:27:08 +10008994 }
NeilBrown09314792015-02-19 16:04:40 +11008995 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008996
NeilBrowndfc70642008-05-23 13:04:39 -07008997 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008998 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008999 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
9000 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
9001 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11009002 pr_debug("md: checkpointing %s of %s.\n",
9003 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08009004 if (test_bit(MD_RECOVERY_ERROR,
9005 &mddev->recovery))
9006 mddev->recovery_cp =
9007 mddev->curr_resync_completed;
9008 else
9009 mddev->recovery_cp =
9010 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07009011 }
9012 } else
9013 mddev->recovery_cp = MaxSector;
9014 } else {
9015 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
9016 mddev->curr_resync = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11009017 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9018 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
9019 rcu_read_lock();
9020 rdev_for_each_rcu(rdev, mddev)
9021 if (rdev->raid_disk >= 0 &&
9022 mddev->delta_disks >= 0 &&
9023 !test_bit(Journal, &rdev->flags) &&
9024 !test_bit(Faulty, &rdev->flags) &&
9025 !test_bit(In_sync, &rdev->flags) &&
9026 rdev->recovery_offset < mddev->curr_resync)
9027 rdev->recovery_offset = mddev->curr_resync;
9028 rcu_read_unlock();
9029 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07009030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009031 }
NeilBrowndb91ff52012-02-07 12:01:51 +11009032 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04009033 /* set CHANGE_PENDING here since maybe another update is needed,
9034 * so other nodes are informed. It should be harmless for normal
9035 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08009036 set_mask_bits(&mddev->sb_flags, 0,
9037 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05009038
BingJing Chang88763912018-02-22 13:34:46 +08009039 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9040 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
9041 mddev->delta_disks > 0 &&
9042 mddev->pers->finish_reshape &&
9043 mddev->pers->size &&
9044 mddev->queue) {
9045 mddev_lock_nointr(mddev);
9046 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
9047 mddev_unlock(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009048 if (!mddev_is_clustered(mddev)) {
9049 set_capacity(mddev->gendisk, mddev->array_sectors);
9050 revalidate_disk(mddev->gendisk);
9051 }
BingJing Chang88763912018-02-22 13:34:46 +08009052 }
9053
NeilBrown23da4222014-12-15 12:57:01 +11009054 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11009055 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
9056 /* We completed so min/max setting can be forgotten if used. */
9057 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
9058 mddev->resync_min = 0;
9059 mddev->resync_max = MaxSector;
9060 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
9061 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10009062 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009063 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11009064 spin_unlock(&mddev->lock);
9065
Linus Torvalds1da177e2005-04-16 15:20:36 -07009066 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009067 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08009068 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009069}
NeilBrown29269552006-03-27 01:18:10 -08009070EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009071
NeilBrown746d3202013-04-24 11:42:41 +10009072static int remove_and_add_spares(struct mddev *mddev,
9073 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009074{
NeilBrown3cb03002011-10-11 16:45:26 +11009075 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009076 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11009077 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10009078 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009079
NeilBrown39772f02018-02-03 09:19:30 +11009080 if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
9081 /* Mustn't remove devices when resync thread is running */
9082 return 0;
9083
NeilBrownd787be42016-06-02 16:19:53 +10009084 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10009085 if ((this == NULL || rdev == this) &&
9086 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07009087 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10009088 test_bit(Faulty, &rdev->flags) &&
9089 atomic_read(&rdev->nr_pending)==0) {
9090 /* Faulty non-Blocked devices with nr_pending == 0
9091 * never get nr_pending incremented,
9092 * never get Faulty cleared, and never get Blocked set.
9093 * So we can synchronize_rcu now rather than once per device
9094 */
9095 remove_some = true;
9096 set_bit(RemoveSynchronized, &rdev->flags);
9097 }
9098 }
9099
9100 if (remove_some)
9101 synchronize_rcu();
9102 rdev_for_each(rdev, mddev) {
9103 if ((this == NULL || rdev == this) &&
9104 rdev->raid_disk >= 0 &&
9105 !test_bit(Blocked, &rdev->flags) &&
9106 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07009107 (!test_bit(In_sync, &rdev->flags) &&
9108 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10009109 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009110 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11009111 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10009112 sysfs_unlink_rdev(mddev, rdev);
NeilBrown011abdc2018-04-26 14:46:29 +10009113 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009114 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11009115 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009116 }
9117 }
NeilBrownd787be42016-06-02 16:19:53 +10009118 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
9119 clear_bit(RemoveSynchronized, &rdev->flags);
9120 }
9121
Jonathan Brassow90584fc2013-03-07 16:24:26 -06009122 if (removed && mddev->kobj.sd)
Junxiao Bie1a86db2020-07-14 16:10:26 -07009123 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009124
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05009125 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10009126 goto no_add;
9127
NeilBrowndafb20f2012-03-19 12:46:39 +11009128 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05009129 if (this && this != rdev)
9130 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009131 if (test_bit(Candidate, &rdev->flags))
9132 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11009133 if (rdev->raid_disk >= 0 &&
9134 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07009135 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11009136 !test_bit(Faulty, &rdev->flags))
9137 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009138 if (rdev->raid_disk >= 0)
9139 continue;
9140 if (test_bit(Faulty, &rdev->flags))
9141 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009142 if (!test_bit(Journal, &rdev->flags)) {
9143 if (mddev->ro &&
9144 ! (rdev->saved_raid_disk >= 0 &&
9145 !test_bit(Bitmap_sync, &rdev->flags)))
9146 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009147
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009148 rdev->recovery_offset = 0;
9149 }
Guoqing Jiang3f79cc22020-04-04 23:57:11 +02009150 if (mddev->pers->hot_add_disk(mddev, rdev) == 0) {
Damien Le Moal5e3b8a82020-07-16 13:54:40 +09009151 /* failure here is OK */
9152 sysfs_link_rdev(mddev, rdev);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009153 if (!test_bit(Journal, &rdev->flags))
9154 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009155 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08009156 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07009157 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009158 }
NeilBrown746d3202013-04-24 11:42:41 +10009159no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10009160 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08009161 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009162 return spares;
9163}
NeilBrown7ebc0be2011-01-14 09:14:33 +11009164
NeilBrownac05f252014-09-30 08:10:42 +10009165static void md_start_sync(struct work_struct *ws)
9166{
9167 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05009168
NeilBrownac05f252014-09-30 08:10:42 +10009169 mddev->sync_thread = md_register_thread(md_do_sync,
9170 mddev,
9171 "resync");
9172 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11009173 pr_warn("%s: could not start resync thread...\n",
9174 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10009175 /* leave the spares where they are, it shouldn't hurt */
9176 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9177 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9178 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9179 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9180 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11009181 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10009182 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9183 &mddev->recovery))
9184 if (mddev->sysfs_action)
9185 sysfs_notify_dirent_safe(mddev->sysfs_action);
9186 } else
9187 md_wakeup_thread(mddev->sync_thread);
9188 sysfs_notify_dirent_safe(mddev->sysfs_action);
9189 md_new_event(mddev);
9190}
9191
Linus Torvalds1da177e2005-04-16 15:20:36 -07009192/*
9193 * This routine is regularly called by all per-raid-array threads to
9194 * deal with generic issues like resync and super-block update.
9195 * Raid personalities that don't have a thread (linear/raid0) do not
9196 * need this as they never do any recovery or update the superblock.
9197 *
9198 * It does not do any resync itself, but rather "forks" off other threads
9199 * to do that as needed.
9200 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
9201 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07009202 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07009203 * and wakeups up this thread which will reap the thread and finish up.
9204 * This thread also removes any faulty devices (with nr_pending == 0).
9205 *
9206 * The overall approach is:
9207 * 1/ if the superblock needs updating, update it.
9208 * 2/ If a recovery thread is running, don't do anything else.
9209 * 3/ If recovery has finished, clean up, possibly marking spares active.
9210 * 4/ If there are any faulty devices, remove them.
9211 * 5/ If array is degraded, try to add spares devices
9212 * 6/ If array has spares or is not in-sync, start a resync thread.
9213 */
NeilBrownfd01b882011-10-11 16:47:53 +11009214void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009215{
NeilBrown059421e2018-10-03 15:04:41 +10009216 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
9217 /* Write superblock - thread that called mddev_suspend()
9218 * holds reconfig_mutex for us.
9219 */
9220 set_bit(MD_UPDATING_SB, &mddev->flags);
9221 smp_mb__after_atomic();
9222 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
9223 md_update_sb(mddev, 0);
9224 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
9225 wake_up(&mddev->sb_wait);
9226 }
9227
Jonathan Brassow68866e42011-06-08 15:10:08 +10009228 if (mddev->suspended)
9229 return;
9230
NeilBrown5f404022005-06-21 17:17:16 -07009231 if (mddev->bitmap)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009232 md_bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009233
NeilBrownfca4d842005-06-21 17:17:11 -07009234 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07009235 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11009236 pr_debug("md: %s in immediate safe mode\n",
9237 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07009238 mddev->safemode = 2;
9239 }
9240 flush_signals(current);
9241 }
9242
NeilBrownc89a8ee2008-08-05 15:54:13 +10009243 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
9244 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009245 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08009246 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07009247 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07009248 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07009249 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11009250 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07009251 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009252 ))
9253 return;
NeilBrownfca4d842005-06-21 17:17:11 -07009254
NeilBrowndf5b89b2006-03-27 01:18:20 -08009255 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009256 int spares = 0;
NeilBrown480523f2019-08-20 10:21:09 +10009257 bool try_set_sync = mddev->safemode != 0;
NeilBrownfca4d842005-06-21 17:17:11 -07009258
Shaohua Liafc1f552017-08-11 20:34:45 -07009259 if (!mddev->external && mddev->safemode == 1)
NeilBrown33182d12017-08-08 16:56:36 +10009260 mddev->safemode = 0;
9261
NeilBrownc89a8ee2008-08-05 15:54:13 +10009262 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10009263 struct md_rdev *rdev;
9264 if (!mddev->external && mddev->in_sync)
9265 /* 'Blocked' flag not needed as failed devices
9266 * will be recorded if array switched to read/write.
9267 * Leaving it set will prevent the device
9268 * from being removed.
9269 */
9270 rdev_for_each(rdev, mddev)
9271 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10009272 /* On a read-only array we can:
9273 * - remove failed devices
9274 * - add already-in_sync devices if the array itself
9275 * is in-sync.
9276 * As we only add devices that are already in-sync,
9277 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10009278 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10009279 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11009280 /* There is no thread, but we need to call
9281 * ->spare_active and clear saved_raid_disk
9282 */
NeilBrown2ac295a2014-05-29 11:40:03 +10009283 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11009284 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10009285 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11009286 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08009287 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10009288 goto unlock;
9289 }
9290
Guoqing Jiang659b2542015-12-21 10:50:59 +11009291 if (mddev_is_clustered(mddev)) {
9292 struct md_rdev *rdev;
9293 /* kick the device if another node issued a
9294 * remove disk.
9295 */
9296 rdev_for_each(rdev, mddev) {
9297 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
9298 rdev->raid_disk < 0)
9299 md_kick_rdev_from_array(rdev);
9300 }
9301 }
9302
NeilBrown480523f2019-08-20 10:21:09 +10009303 if (try_set_sync && !mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11009304 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11009305 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11009306 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07009307 }
NeilBrownfca4d842005-06-21 17:17:11 -07009308
Shaohua Li29530792016-12-08 15:48:19 -08009309 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07009310 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07009311
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
9313 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
9314 /* resync/recovery still happening */
9315 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9316 goto unlock;
9317 }
9318 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009319 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009320 goto unlock;
9321 }
Neil Brown72a23c22008-06-28 08:31:41 +10009322 /* Set RUNNING before clearing NEEDED to avoid
9323 * any transients in the value of "sync_action".
9324 */
NeilBrown72f36d52012-10-11 14:25:57 +11009325 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11009326 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10009327 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11009328 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08009329 /* Clear some bits that don't mean anything, but
9330 * might be left set
9331 */
NeilBrown24dd4692005-11-08 21:39:26 -08009332 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
9333 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009334
NeilBrowned209582012-04-24 10:23:14 +10009335 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
9336 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10009337 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009338 /* no recovery is running.
9339 * remove any failed drives, then
9340 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11009341 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07009342 * the personality to fail the re-add.
9343 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009345 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10009346 if (mddev->pers->check_reshape == NULL ||
9347 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009348 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10009349 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009350 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009351 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10009352 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08009353 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9354 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07009355 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009356 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08009357 } else if (mddev->recovery_cp < MaxSector) {
9358 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009359 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08009360 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
9361 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10009362 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08009363
Linus Torvalds1da177e2005-04-16 15:20:36 -07009364 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10009365 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07009366 /* We are adding a device or devices to an array
9367 * which has the bitmap stored on all devices.
9368 * So make sure all bitmap pages get written
9369 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009370 md_bitmap_write_all(mddev->bitmap);
NeilBrowna654b9d82005-06-21 17:17:27 -07009371 }
NeilBrownac05f252014-09-30 08:10:42 +10009372 INIT_WORK(&mddev->del_work, md_start_sync);
9373 queue_work(md_misc_wq, &mddev->del_work);
9374 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009375 }
NeilBrownac05f252014-09-30 08:10:42 +10009376 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10009377 if (!mddev->sync_thread) {
9378 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11009379 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10009380 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9381 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11009382 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10009383 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10009384 }
NeilBrownac05f252014-09-30 08:10:42 +10009385 unlock:
9386 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387 mddev_unlock(mddev);
9388 }
9389}
NeilBrown6c144d32014-09-30 16:15:38 +10009390EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009391
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009392void md_reap_sync_thread(struct mddev *mddev)
9393{
9394 struct md_rdev *rdev;
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009395 sector_t old_dev_sectors = mddev->dev_sectors;
9396 bool is_reshaped = false;
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009397
9398 /* resync has finished, collect result */
9399 md_unregister_thread(&mddev->sync_thread);
9400 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
Guoqing Jiang0d8ed0e92019-07-24 11:09:21 +02009401 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
9402 mddev->degraded != mddev->raid_disks) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009403 /* success...*/
9404 /* activate any spares */
9405 if (mddev->pers->spare_active(mddev)) {
Junxiao Bie1a86db2020-07-14 16:10:26 -07009406 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
Shaohua Li29530792016-12-08 15:48:19 -08009407 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009408 }
9409 }
9410 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009411 mddev->pers->finish_reshape) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009412 mddev->pers->finish_reshape(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009413 if (mddev_is_clustered(mddev))
9414 is_reshaped = true;
9415 }
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009416
9417 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11009418 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009419 */
NeilBrownf4667222013-12-09 12:04:56 +11009420 if (!mddev->degraded)
9421 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009422 rdev->saved_raid_disk = -1;
9423
9424 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08009425 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04009426 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9427 * clustered raid */
9428 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
9429 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009430 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10009431 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009432 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9433 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9434 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9435 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009436 /*
9437 * We call md_cluster_ops->update_size here because sync_size could
9438 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9439 * so it is time to update size across cluster.
9440 */
9441 if (mddev_is_clustered(mddev) && is_reshaped
9442 && !test_bit(MD_CLOSING, &mddev->flags))
9443 md_cluster_ops->update_size(mddev, old_dev_sectors);
NeilBrownf851b602014-12-11 10:02:10 +11009444 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009445 /* flag recovery needed just to double check */
9446 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9447 sysfs_notify_dirent_safe(mddev->sysfs_action);
9448 md_new_event(mddev);
9449 if (mddev->event_work.func)
9450 queue_work(md_misc_wq, &mddev->event_work);
9451}
NeilBrown6c144d32014-09-30 16:15:38 +10009452EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009453
NeilBrownfd01b882011-10-11 16:47:53 +11009454void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07009455{
NeilBrown00bcb4a2010-06-01 19:37:23 +10009456 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07009457 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10009458 !test_bit(Blocked, &rdev->flags) &&
9459 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07009460 msecs_to_jiffies(5000));
9461 rdev_dec_pending(rdev, mddev);
9462}
9463EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9464
NeilBrownc6563a82012-05-21 09:27:00 +10009465void md_finish_reshape(struct mddev *mddev)
9466{
9467 /* called be personality module when reshape completes. */
9468 struct md_rdev *rdev;
9469
9470 rdev_for_each(rdev, mddev) {
9471 if (rdev->data_offset > rdev->new_data_offset)
9472 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9473 else
9474 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9475 rdev->data_offset = rdev->new_data_offset;
9476 }
9477}
9478EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10009479
Vishal Vermafc974ee2015-12-24 19:20:34 -07009480/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10009481
Vishal Vermafc974ee2015-12-24 19:20:34 -07009482/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11009483int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10009484 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009485{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04009486 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10009487 int rv;
9488 if (is_new)
9489 s += rdev->new_data_offset;
9490 else
9491 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07009492 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9493 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10009494 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009495 if (test_bit(ExternalBbl, &rdev->flags))
Junxiao Bie1a86db2020-07-14 16:10:26 -07009496 sysfs_notify_dirent_safe(rdev->sysfs_unack_badblocks);
NeilBrown8bd2f0a2011-12-08 16:26:08 +11009497 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08009498 set_mask_bits(&mddev->sb_flags, 0,
9499 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10009500 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07009501 return 1;
9502 } else
9503 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10009504}
9505EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9506
NeilBrownc6563a82012-05-21 09:27:00 +10009507int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9508 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009509{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009510 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10009511 if (is_new)
9512 s += rdev->new_data_offset;
9513 else
9514 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009515 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9516 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
Junxiao Bie1a86db2020-07-14 16:10:26 -07009517 sysfs_notify_dirent_safe(rdev->sysfs_badblocks);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009518 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10009519}
9520EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9521
Adrian Bunk75c96f82005-05-05 16:16:09 -07009522static int md_notify_reboot(struct notifier_block *this,
9523 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009524{
9525 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11009526 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009527 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009528
NeilBrownc744a652012-03-19 12:46:37 +11009529 for_each_mddev(mddev, tmp) {
9530 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10009531 if (mddev->pers)
9532 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009533 if (mddev->persistent)
9534 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009535 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009536 }
NeilBrownc744a652012-03-19 12:46:37 +11009537 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009538 }
NeilBrownc744a652012-03-19 12:46:37 +11009539 /*
9540 * certain more exotic SCSI devices are known to be
9541 * volatile wrt too early system reboots. While the
9542 * right place to handle this issue is the given
9543 * driver, we do want to have a safe RAID driver ...
9544 */
9545 if (need_delay)
9546 mdelay(1000*1);
9547
Linus Torvalds1da177e2005-04-16 15:20:36 -07009548 return NOTIFY_DONE;
9549}
9550
Adrian Bunk75c96f82005-05-05 16:16:09 -07009551static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009552 .notifier_call = md_notify_reboot,
9553 .next = NULL,
9554 .priority = INT_MAX, /* before any real devices */
9555};
9556
9557static void md_geninit(void)
9558{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009559 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009560
Alexey Dobriyan97a32532020-02-03 17:37:17 -08009561 proc_create("mdstat", S_IRUGO, NULL, &mdstat_proc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009562}
9563
Adrian Bunk75c96f82005-05-05 16:16:09 -07009564static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009565{
Tejun Heoe804ac72010-10-15 15:36:08 +02009566 int ret = -ENOMEM;
9567
Tejun Heoada609e2011-01-25 14:35:54 +01009568 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009569 if (!md_wq)
9570 goto err_wq;
9571
9572 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9573 if (!md_misc_wq)
9574 goto err_misc_wq;
9575
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009576 md_rdev_misc_wq = alloc_workqueue("md_rdev_misc", 0, 0);
9577 if (!md_misc_wq)
9578 goto err_rdev_misc_wq;
9579
Tejun Heoe804ac72010-10-15 15:36:08 +02009580 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9581 goto err_md;
9582
9583 if ((ret = register_blkdev(0, "mdp")) < 0)
9584 goto err_mdp;
9585 mdp_major = ret;
9586
NeilBrownaf5628f2014-07-31 13:54:54 +10009587 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009588 md_probe, NULL, NULL);
9589 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009590 md_probe, NULL, NULL);
9591
Linus Torvalds1da177e2005-04-16 15:20:36 -07009592 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009593 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009594
9595 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009597
Tejun Heoe804ac72010-10-15 15:36:08 +02009598err_mdp:
9599 unregister_blkdev(MD_MAJOR, "md");
9600err_md:
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009601 destroy_workqueue(md_rdev_misc_wq);
9602err_rdev_misc_wq:
Tejun Heoe804ac72010-10-15 15:36:08 +02009603 destroy_workqueue(md_misc_wq);
9604err_misc_wq:
9605 destroy_workqueue(md_wq);
9606err_wq:
9607 return ret;
9608}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009609
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009610static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009611{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009612 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9613 struct md_rdev *rdev2;
9614 int role, ret;
9615 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009616
Guoqing Jiang818da592017-03-01 16:42:40 +08009617 /*
9618 * If size is changed in another node then we need to
9619 * do resize as well.
9620 */
9621 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9622 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9623 if (ret)
9624 pr_info("md-cluster: resize failed\n");
9625 else
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009626 md_bitmap_update_sb(mddev->bitmap);
Guoqing Jiang818da592017-03-01 16:42:40 +08009627 }
9628
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009629 /* Check for change of roles in the active devices */
9630 rdev_for_each(rdev2, mddev) {
9631 if (test_bit(Faulty, &rdev2->flags))
9632 continue;
9633
9634 /* Check if the roles changed */
9635 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009636
9637 if (test_bit(Candidate, &rdev2->flags)) {
9638 if (role == 0xfffe) {
9639 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9640 md_kick_rdev_from_array(rdev2);
9641 continue;
9642 }
9643 else
9644 clear_bit(Candidate, &rdev2->flags);
9645 }
9646
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009647 if (role != rdev2->raid_disk) {
Guoqing Jiangca1e98e2018-10-18 16:37:45 +08009648 /*
9649 * got activated except reshape is happening.
9650 */
9651 if (rdev2->raid_disk == -1 && role != 0xffff &&
9652 !(le32_to_cpu(sb->feature_map) &
9653 MD_FEATURE_RESHAPE_ACTIVE)) {
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009654 rdev2->saved_raid_disk = role;
9655 ret = remove_and_add_spares(mddev, rdev2);
9656 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11009657 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04009658 /* wakeup mddev->thread here, so array could
9659 * perform resync with the new activated disk */
9660 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9661 md_wakeup_thread(mddev->thread);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009662 }
9663 /* device faulty
9664 * We just want to do the minimum to mark the disk
9665 * as faulty. The recovery is performed by the
9666 * one who initiated the error.
9667 */
9668 if ((role == 0xfffe) || (role == 0xfffd)) {
9669 md_error(mddev, rdev2);
9670 clear_bit(Blocked, &rdev2->flags);
9671 }
9672 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009673 }
9674
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009675 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9676 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009677
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009678 /*
9679 * Since mddev->delta_disks has already updated in update_raid_disks,
9680 * so it is time to check reshape.
9681 */
9682 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9683 (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9684 /*
9685 * reshape is happening in the remote node, we need to
9686 * update reshape_position and call start_reshape.
9687 */
Christoph Hellwiged4d0a4e2019-04-04 18:56:10 +02009688 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009689 if (mddev->pers->update_reshape_pos)
9690 mddev->pers->update_reshape_pos(mddev);
9691 if (mddev->pers->start_reshape)
9692 mddev->pers->start_reshape(mddev);
9693 } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9694 mddev->reshape_position != MaxSector &&
9695 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9696 /* reshape is just done in another node. */
9697 mddev->reshape_position = MaxSector;
9698 if (mddev->pers->update_reshape_pos)
9699 mddev->pers->update_reshape_pos(mddev);
9700 }
9701
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009702 /* Finally set the event to be up to date */
9703 mddev->events = le64_to_cpu(sb->events);
9704}
9705
9706static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9707{
9708 int err;
9709 struct page *swapout = rdev->sb_page;
9710 struct mdp_superblock_1 *sb;
9711
9712 /* Store the sb page of the rdev in the swapout temporary
9713 * variable in case we err in the future
9714 */
9715 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009716 err = alloc_disk_sb(rdev);
9717 if (err == 0) {
9718 ClearPageUptodate(rdev->sb_page);
9719 rdev->sb_loaded = 0;
9720 err = super_types[mddev->major_version].
9721 load_super(rdev, NULL, mddev->minor_version);
9722 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009723 if (err < 0) {
9724 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9725 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009726 if (rdev->sb_page)
9727 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009728 rdev->sb_page = swapout;
9729 rdev->sb_loaded = 1;
9730 return err;
9731 }
9732
9733 sb = page_address(rdev->sb_page);
9734 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9735 * is not set
9736 */
9737
9738 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9739 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9740
9741 /* The other node finished recovery, call spare_active to set
9742 * device In_sync and mddev->degraded
9743 */
9744 if (rdev->recovery_offset == MaxSector &&
9745 !test_bit(In_sync, &rdev->flags) &&
9746 mddev->pers->spare_active(mddev))
Junxiao Bie1a86db2020-07-14 16:10:26 -07009747 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009748
9749 put_page(swapout);
9750 return 0;
9751}
9752
9753void md_reload_sb(struct mddev *mddev, int nr)
9754{
9755 struct md_rdev *rdev;
9756 int err;
9757
9758 /* Find the rdev */
9759 rdev_for_each_rcu(rdev, mddev) {
9760 if (rdev->desc_nr == nr)
9761 break;
9762 }
9763
9764 if (!rdev || rdev->desc_nr != nr) {
9765 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9766 return;
9767 }
9768
9769 err = read_rdev(mddev, rdev);
9770 if (err < 0)
9771 return;
9772
9773 check_sb_changes(mddev, rdev);
9774
9775 /* Read all rdev's to update recovery_offset */
Guoqing Jiang0ea99242018-04-09 17:01:21 +08009776 rdev_for_each_rcu(rdev, mddev) {
9777 if (!test_bit(Faulty, &rdev->flags))
9778 read_rdev(mddev, rdev);
9779 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009780}
9781EXPORT_SYMBOL(md_reload_sb);
9782
Linus Torvalds1da177e2005-04-16 15:20:36 -07009783#ifndef MODULE
9784
9785/*
9786 * Searches all registered partitions for autorun RAID arrays
9787 * at boot time.
9788 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009789
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009790static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009791static LIST_HEAD(all_detected_devices);
9792struct detected_devices_node {
9793 struct list_head list;
9794 dev_t dev;
9795};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796
9797void md_autodetect_dev(dev_t dev)
9798{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009799 struct detected_devices_node *node_detected_dev;
9800
9801 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9802 if (node_detected_dev) {
9803 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009804 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009805 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009806 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009808}
9809
Linus Torvalds1da177e2005-04-16 15:20:36 -07009810static void autostart_arrays(int part)
9811{
NeilBrown3cb03002011-10-11 16:45:26 +11009812 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009813 struct detected_devices_node *node_detected_dev;
9814 dev_t dev;
9815 int i_scanned, i_passed;
9816
9817 i_scanned = 0;
9818 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009819
NeilBrown9d487392016-11-02 14:16:49 +11009820 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009821
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009822 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009823 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9824 i_scanned++;
9825 node_detected_dev = list_entry(all_detected_devices.next,
9826 struct detected_devices_node, list);
9827 list_del(&node_detected_dev->list);
9828 dev = node_detected_dev->dev;
9829 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009830 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009831 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009832 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009833 if (IS_ERR(rdev))
9834 continue;
9835
NeilBrown403df472014-09-30 15:52:29 +10009836 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009837 continue;
NeilBrown403df472014-09-30 15:52:29 +10009838
NeilBrownd0fae182008-03-04 14:29:31 -08009839 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009841 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009842 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009843 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009844
NeilBrown9d487392016-11-02 14:16:49 +11009845 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009846
9847 autorun_devices(part);
9848}
9849
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009850#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009851
9852static __exit void md_exit(void)
9853{
NeilBrownfd01b882011-10-11 16:47:53 +11009854 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009855 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009856 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009857
NeilBrownaf5628f2014-07-31 13:54:54 +10009858 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009859 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009860
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009861 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009862 unregister_blkdev(mdp_major, "mdp");
9863 unregister_reboot_notifier(&md_notifier);
9864 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009865
9866 /* We cannot unload the modules while some process is
9867 * waiting for us in select() or poll() - wake them up
9868 */
9869 md_unloading = 1;
9870 while (waitqueue_active(&md_event_waiters)) {
9871 /* not safe to leave yet */
9872 wake_up(&md_event_waiters);
9873 msleep(delay);
9874 delay += delay;
9875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009876 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009877
NeilBrown29ac4aa2008-02-06 01:39:58 -08009878 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009879 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009880 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009881 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009882 /*
9883 * for_each_mddev() will call mddev_put() at the end of each
9884 * iteration. As the mddev is now fully clear, this will
9885 * schedule the mddev for destruction by a workqueue, and the
9886 * destroy_workqueue() below will wait for that to complete.
9887 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009888 }
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009889 destroy_workqueue(md_rdev_misc_wq);
Tejun Heoe804ac72010-10-15 15:36:08 +02009890 destroy_workqueue(md_misc_wq);
9891 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009892}
9893
Dan Williams685784a2007-07-09 11:56:42 -07009894subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009895module_exit(md_exit)
9896
Kees Cooke4dca7b2017-10-17 19:04:42 -07009897static int get_ro(char *buffer, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009898{
Xiongfeng Wang3f999802020-05-11 16:23:25 +08009899 return sprintf(buffer, "%d\n", start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009900}
Kees Cooke4dca7b2017-10-17 19:04:42 -07009901static int set_ro(const char *val, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009902{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009903 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009904}
9905
NeilBrown80ca3a42006-07-10 04:44:18 -07009906module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9907module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009908module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009909module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009910
Linus Torvalds1da177e2005-04-16 15:20:36 -07009911MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009912MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009913MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009914MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);