blob: 1b3316c79b7b7fd34502119165c749d4299ed01b [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
104/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
106 * is 1000 KB/sec, so the extra system load does not show up that much.
107 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -0700108 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * subsystem is idle. There is also an 'absolute maximum' reconstruction
110 * speed limit - in case reconstruction slows down your system despite
111 * idle IO detection.
112 *
113 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -0800114 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
116
117static int sysctl_speed_limit_min = 1000;
118static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100119static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800120{
121 return mddev->sync_speed_min ?
122 mddev->sync_speed_min : sysctl_speed_limit_min;
123}
124
NeilBrownfd01b882011-10-11 16:47:53 +1100125static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800126{
127 return mddev->sync_speed_max ?
128 mddev->sync_speed_max : sysctl_speed_limit_max;
129}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100131static void rdev_uninit_serial(struct md_rdev *rdev)
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800132{
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100133 if (!test_and_clear_bit(CollisionCheck, &rdev->flags))
134 return;
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800135
Guoqing Jiang025471f2019-12-23 10:49:01 +0100136 kvfree(rdev->serial);
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100137 rdev->serial = NULL;
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800138}
139
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100140static void rdevs_uninit_serial(struct mddev *mddev)
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100141{
142 struct md_rdev *rdev;
143
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100144 rdev_for_each(rdev, mddev)
145 rdev_uninit_serial(rdev);
146}
147
148static int rdev_init_serial(struct md_rdev *rdev)
149{
Guoqing Jiang025471f2019-12-23 10:49:01 +0100150 /* serial_nums equals with BARRIER_BUCKETS_NR */
151 int i, serial_nums = 1 << ((PAGE_SHIFT - ilog2(sizeof(atomic_t))));
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100152 struct serial_in_rdev *serial = NULL;
153
154 if (test_bit(CollisionCheck, &rdev->flags))
155 return 0;
156
Guoqing Jiang025471f2019-12-23 10:49:01 +0100157 serial = kvmalloc(sizeof(struct serial_in_rdev) * serial_nums,
158 GFP_KERNEL);
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100159 if (!serial)
160 return -ENOMEM;
161
Guoqing Jiang025471f2019-12-23 10:49:01 +0100162 for (i = 0; i < serial_nums; i++) {
163 struct serial_in_rdev *serial_tmp = &serial[i];
164
165 spin_lock_init(&serial_tmp->serial_lock);
166 serial_tmp->serial_rb = RB_ROOT_CACHED;
167 init_waitqueue_head(&serial_tmp->serial_io_wait);
168 }
169
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100170 rdev->serial = serial;
171 set_bit(CollisionCheck, &rdev->flags);
172
173 return 0;
174}
175
176static int rdevs_init_serial(struct mddev *mddev)
177{
178 struct md_rdev *rdev;
179 int ret = 0;
180
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100181 rdev_for_each(rdev, mddev) {
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100182 ret = rdev_init_serial(rdev);
183 if (ret)
184 break;
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100185 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100186
187 /* Free all resources if pool is not existed */
188 if (ret && !mddev->serial_info_pool)
189 rdevs_uninit_serial(mddev);
190
191 return ret;
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100192}
193
Guoqing Jiang963c5552019-06-14 17:10:36 +0800194/*
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100195 * rdev needs to enable serial stuffs if it meets the conditions:
196 * 1. it is multi-queue device flaged with writemostly.
197 * 2. the write-behind mode is enabled.
198 */
199static int rdev_need_serial(struct md_rdev *rdev)
200{
201 return (rdev && rdev->mddev->bitmap_info.max_write_behind > 0 &&
202 rdev->bdev->bd_queue->nr_hw_queues != 1 &&
203 test_bit(WriteMostly, &rdev->flags));
204}
205
206/*
207 * Init resource for rdev(s), then create serial_info_pool if:
208 * 1. rdev is the first device which return true from rdev_enable_serial.
209 * 2. rdev is NULL, means we want to enable serialization for all rdevs.
Guoqing Jiang963c5552019-06-14 17:10:36 +0800210 */
Guoqing Jiang404659c2019-12-23 10:48:53 +0100211void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev,
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100212 bool is_suspend)
Guoqing Jiang963c5552019-06-14 17:10:36 +0800213{
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100214 int ret = 0;
215
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100216 if (rdev && !rdev_need_serial(rdev) &&
217 !test_bit(CollisionCheck, &rdev->flags))
Guoqing Jiang963c5552019-06-14 17:10:36 +0800218 return;
219
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100220 if (!is_suspend)
221 mddev_suspend(mddev);
222
223 if (!rdev)
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100224 ret = rdevs_init_serial(mddev);
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100225 else
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100226 ret = rdev_init_serial(rdev);
227 if (ret)
228 goto abort;
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100229
Guoqing Jiang404659c2019-12-23 10:48:53 +0100230 if (mddev->serial_info_pool == NULL) {
Guoqing Jiang963c5552019-06-14 17:10:36 +0800231 unsigned int noio_flag;
232
Guoqing Jiang963c5552019-06-14 17:10:36 +0800233 noio_flag = memalloc_noio_save();
Guoqing Jiang404659c2019-12-23 10:48:53 +0100234 mddev->serial_info_pool =
235 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
236 sizeof(struct serial_info));
Guoqing Jiang963c5552019-06-14 17:10:36 +0800237 memalloc_noio_restore(noio_flag);
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100238 if (!mddev->serial_info_pool) {
239 rdevs_uninit_serial(mddev);
Guoqing Jiang404659c2019-12-23 10:48:53 +0100240 pr_err("can't alloc memory pool for serialization\n");
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100241 }
Guoqing Jiang963c5552019-06-14 17:10:36 +0800242 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100243
244abort:
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100245 if (!is_suspend)
246 mddev_resume(mddev);
Guoqing Jiang963c5552019-06-14 17:10:36 +0800247}
Guoqing Jiang963c5552019-06-14 17:10:36 +0800248
249/*
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100250 * Free resource from rdev(s), and destroy serial_info_pool under conditions:
251 * 1. rdev is the last device flaged with CollisionCheck.
252 * 2. when bitmap is destroyed while policy is not enabled.
253 * 3. for disable policy, the pool is destroyed only when no rdev needs it.
Guoqing Jiang963c5552019-06-14 17:10:36 +0800254 */
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100255void mddev_destroy_serial_pool(struct mddev *mddev, struct md_rdev *rdev,
256 bool is_suspend)
Guoqing Jiang963c5552019-06-14 17:10:36 +0800257{
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100258 if (rdev && !test_bit(CollisionCheck, &rdev->flags))
Guoqing Jiang963c5552019-06-14 17:10:36 +0800259 return;
260
Guoqing Jiang404659c2019-12-23 10:48:53 +0100261 if (mddev->serial_info_pool) {
Guoqing Jiang963c5552019-06-14 17:10:36 +0800262 struct md_rdev *temp;
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100263 int num = 0; /* used to track if other rdevs need the pool */
Guoqing Jiang963c5552019-06-14 17:10:36 +0800264
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100265 if (!is_suspend)
266 mddev_suspend(mddev);
267 rdev_for_each(temp, mddev) {
268 if (!rdev) {
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100269 if (!mddev->serialize_policy ||
270 !rdev_need_serial(temp))
271 rdev_uninit_serial(temp);
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100272 else
273 num++;
274 } else if (temp != rdev &&
275 test_bit(CollisionCheck, &temp->flags))
Guoqing Jiang963c5552019-06-14 17:10:36 +0800276 num++;
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100277 }
278
279 if (rdev)
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100280 rdev_uninit_serial(rdev);
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100281
282 if (num)
283 pr_info("The mempool could be used by other devices\n");
284 else {
Guoqing Jiang404659c2019-12-23 10:48:53 +0100285 mempool_destroy(mddev->serial_info_pool);
286 mddev->serial_info_pool = NULL;
Guoqing Jiang963c5552019-06-14 17:10:36 +0800287 }
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100288 if (!is_suspend)
289 mddev_resume(mddev);
Guoqing Jiang963c5552019-06-14 17:10:36 +0800290 }
291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293static struct ctl_table_header *raid_table_header;
294
Joe Perches82592c32013-11-14 15:16:18 +1100295static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 .procname = "speed_limit_min",
298 .data = &sysctl_speed_limit_min,
299 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700300 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800301 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 },
303 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 .procname = "speed_limit_max",
305 .data = &sysctl_speed_limit_max,
306 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700307 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800308 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800310 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
Joe Perches82592c32013-11-14 15:16:18 +1100313static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 .procname = "raid",
316 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700317 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 .child = raid_table,
319 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800320 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321};
322
Joe Perches82592c32013-11-14 15:16:18 +1100323static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 .procname = "dev",
326 .maxlen = 0,
327 .mode = 0555,
328 .child = raid_dir_table,
329 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800330 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331};
332
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700333static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
NeilBrownf91de922005-11-08 21:39:36 -0800335static int start_readonly;
336
NeilBrown78b63502017-04-12 16:26:13 +1000337/*
338 * The original mechanism for creating an md device is to create
339 * a device node in /dev and to open it. This causes races with device-close.
340 * The preferred method is to write to the "new_array" module parameter.
341 * This can avoid races.
342 * Setting create_on_open to false disables the original mechanism
343 * so all the races disappear.
344 */
345static bool create_on_open = true;
346
NeilBrowna167f662010-10-26 18:31:13 +1100347struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100348 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100349{
Kent Overstreetafeee512018-05-20 18:25:52 -0400350 if (!mddev || !bioset_initialized(&mddev->bio_set))
NeilBrowna167f662010-10-26 18:31:13 +1100351 return bio_alloc(gfp_mask, nr_iovecs);
352
Marcos Paulo de Souza62516912019-01-14 06:31:56 -0700353 return bio_alloc_bioset(gfp_mask, nr_iovecs, &mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100354}
355EXPORT_SYMBOL_GPL(bio_alloc_mddev);
356
NeilBrown5a850712017-06-21 09:12:21 +1000357static struct bio *md_bio_alloc_sync(struct mddev *mddev)
358{
Kent Overstreetafeee512018-05-20 18:25:52 -0400359 if (!mddev || !bioset_initialized(&mddev->sync_set))
NeilBrown5a850712017-06-21 09:12:21 +1000360 return bio_alloc(GFP_NOIO, 1);
361
Kent Overstreetafeee512018-05-20 18:25:52 -0400362 return bio_alloc_bioset(GFP_NOIO, 1, &mddev->sync_set);
NeilBrown5a850712017-06-21 09:12:21 +1000363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/*
NeilBrownd7603b72006-01-06 00:20:30 -0800366 * We have a system wide 'event count' that is incremented
367 * on any 'interesting' event, and readers of /proc/mdstat
368 * can use 'poll' or 'select' to find out when the event
369 * count increases.
370 *
371 * Events are:
372 * start array, stop array, error, add device, remove device,
373 * start build, activate spare
374 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800375static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800376static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100377void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800378{
379 atomic_inc(&md_event_count);
380 wake_up(&md_event_waiters);
381}
NeilBrown29269552006-03-27 01:18:10 -0800382EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800383
384/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 * Enables to iterate over all existing md arrays
386 * all_mddevs_lock protects this list.
387 */
388static LIST_HEAD(all_mddevs);
389static DEFINE_SPINLOCK(all_mddevs_lock);
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/*
392 * iterates through all used mddevs in the system.
393 * We take care to grab the all_mddevs_lock whenever navigating
394 * the list, and to always hold a refcount when unlocked.
395 * Any code which breaks out of this loop while own
396 * a reference to the current mddev and must mddev_put it.
397 */
NeilBrownfd01b882011-10-11 16:47:53 +1100398#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000400 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100401 _tmp = all_mddevs.next; \
402 _mddev = NULL;}); \
403 ({ if (_tmp != &all_mddevs) \
404 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100406 if (_mddev) mddev_put(_mddev); \
407 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
408 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100410 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 )
412
NeilBrown409c57f2009-03-31 14:39:39 +1100413/* Rather than calling directly into the personality make_request function,
414 * IO requests come here first so that we can check if the device is
415 * being suspended pending a reconfiguration.
416 * We hold a refcount over the call to ->make_request. By the time that
417 * call has finished, the bio has been linked into some internal structure
418 * and so is visible to ->quiesce(), so we don't need the refcount any more.
419 */
NeilBrownb3143b92017-10-17 13:46:43 +1100420static bool is_suspended(struct mddev *mddev, struct bio *bio)
421{
422 if (mddev->suspended)
423 return true;
424 if (bio_data_dir(bio) != WRITE)
425 return false;
426 if (mddev->suspend_lo >= mddev->suspend_hi)
427 return false;
428 if (bio->bi_iter.bi_sector >= mddev->suspend_hi)
429 return false;
430 if (bio_end_sector(bio) < mddev->suspend_lo)
431 return false;
432 return true;
433}
434
Shaohua Li393debc2017-09-21 10:23:35 -0700435void md_handle_request(struct mddev *mddev, struct bio *bio)
436{
437check_suspended:
438 rcu_read_lock();
NeilBrownb3143b92017-10-17 13:46:43 +1100439 if (is_suspended(mddev, bio)) {
Shaohua Li393debc2017-09-21 10:23:35 -0700440 DEFINE_WAIT(__wait);
441 for (;;) {
442 prepare_to_wait(&mddev->sb_wait, &__wait,
443 TASK_UNINTERRUPTIBLE);
NeilBrownb3143b92017-10-17 13:46:43 +1100444 if (!is_suspended(mddev, bio))
Shaohua Li393debc2017-09-21 10:23:35 -0700445 break;
446 rcu_read_unlock();
447 schedule();
448 rcu_read_lock();
449 }
450 finish_wait(&mddev->sb_wait, &__wait);
451 }
452 atomic_inc(&mddev->active_io);
453 rcu_read_unlock();
454
455 if (!mddev->pers->make_request(mddev, bio)) {
456 atomic_dec(&mddev->active_io);
457 wake_up(&mddev->sb_wait);
458 goto check_suspended;
459 }
460
461 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
462 wake_up(&mddev->sb_wait);
463}
464EXPORT_SYMBOL(md_handle_request);
465
Jens Axboedece1632015-11-05 10:41:16 -0700466static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
NeilBrown49077322010-03-25 16:20:56 +1100468 const int rw = bio_data_dir(bio);
Michael Callahanddcf35d2018-07-18 04:47:39 -0700469 const int sgrp = op_stat_group(bio_op(bio));
NeilBrownfd01b882011-10-11 16:47:53 +1100470 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500471 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100472
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -0300473 if (unlikely(test_bit(MD_BROKEN, &mddev->flags)) && (rw == WRITE)) {
474 bio_io_error(bio);
475 return BLK_QC_T_NONE;
476 }
477
NeilBrownaf67c312017-06-18 14:38:57 +1000478 blk_queue_split(q, &bio);
Kent Overstreet54efd502015-04-23 22:37:18 -0700479
NeilBrown274d8cb2016-01-04 16:16:58 +1100480 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100481 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700482 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100483 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100484 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200485 if (bio_sectors(bio) != 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200486 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200487 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700488 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100489 }
NeilBrown49077322010-03-25 16:20:56 +1100490
Chris Masone91ece52011-02-07 19:21:48 -0500491 /*
492 * save the sectors now since our bio can
493 * go away inside make_request
494 */
495 sectors = bio_sectors(bio);
Shaohua Li9c573de2016-04-25 16:52:38 -0700496 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600497 bio->bi_opf &= ~REQ_NOMERGE;
Shaohua Li393debc2017-09-21 10:23:35 -0700498
499 md_handle_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100500
Mike Snitzer112f1582018-12-06 11:41:18 -0500501 part_stat_lock();
502 part_stat_inc(&mddev->gendisk->part0, ios[sgrp]);
503 part_stat_add(&mddev->gendisk->part0, sectors[sgrp], sectors);
Gu Zheng74672d02015-04-03 08:44:47 +0800504 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100505
Jens Axboedece1632015-11-05 10:41:16 -0700506 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
NeilBrown9e35b992010-04-06 14:23:02 +1000509/* mddev_suspend makes sure no new requests are submitted
510 * to the device, and that any requests that have been submitted
511 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100512 * Once mddev_detach() is called and completes, the module will be
513 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000514 */
NeilBrownfd01b882011-10-11 16:47:53 +1100515void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100516{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200517 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
NeilBrown4d5324f2017-10-19 12:17:16 +1100518 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100519 if (mddev->suspended++)
520 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100521 synchronize_rcu();
NeilBrowncc27b0c2017-06-05 16:49:39 +1000522 wake_up(&mddev->sb_wait);
NeilBrown35bfc522017-10-17 13:46:43 +1100523 set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
524 smp_mb__after_atomic();
NeilBrown409c57f2009-03-31 14:39:39 +1100525 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
526 mddev->pers->quiesce(mddev, 1);
NeilBrown35bfc522017-10-17 13:46:43 +1100527 clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags);
528 wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags));
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500529
530 del_timer_sync(&mddev->safemode_timer);
Coly Li78f57ef2020-04-09 22:17:20 +0800531 /* restrict memory reclaim I/O during raid array is suspend */
532 mddev->noio_flag = memalloc_noio_save();
NeilBrown409c57f2009-03-31 14:39:39 +1100533}
NeilBrown390ee602010-06-01 19:37:27 +1000534EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100535
NeilBrownfd01b882011-10-11 16:47:53 +1100536void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100537{
Coly Li78f57ef2020-04-09 22:17:20 +0800538 /* entred the memalloc scope from mddev_suspend() */
539 memalloc_noio_restore(mddev->noio_flag);
NeilBrown4d5324f2017-10-19 12:17:16 +1100540 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100541 if (--mddev->suspended)
542 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100543 wake_up(&mddev->sb_wait);
544 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500545
Jonathan Brassow47525e52012-05-22 13:55:29 +1000546 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500547 md_wakeup_thread(mddev->thread);
548 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100549}
NeilBrown390ee602010-06-01 19:37:27 +1000550EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100551
NeilBrownfd01b882011-10-11 16:47:53 +1100552int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000553{
NeilBrown5c675f82014-12-15 12:56:56 +1100554 struct md_personality *pers = mddev->pers;
555 int ret = 0;
556
557 rcu_read_lock();
558 if (mddev->suspended)
559 ret = 1;
560 else if (pers && pers->congested)
561 ret = pers->congested(mddev, bits);
562 rcu_read_unlock();
563 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000564}
NeilBrown5c675f82014-12-15 12:56:56 +1100565EXPORT_SYMBOL_GPL(mddev_congested);
566static int md_congested(void *data, int bits)
567{
568 struct mddev *mddev = data;
569 return mddev_congested(mddev, bits);
570}
NeilBrown3fa841d2009-09-23 18:10:29 +1000571
NeilBrowna2826aa2009-12-14 12:49:49 +1100572/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200573 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100574 */
NeilBrown4bc034d2019-03-29 10:46:16 -0700575
576static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100577{
NeilBrown4bc034d2019-03-29 10:46:16 -0700578 struct md_rdev *rdev = bio->bi_private;
579 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100580
581 rdev_dec_pending(rdev, mddev);
582
NeilBrown4bc034d2019-03-29 10:46:16 -0700583 if (atomic_dec_and_test(&mddev->flush_pending)) {
584 /* The pre-request flush has finished */
585 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100586 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700587 bio_put(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100588}
589
NeilBrown4bc034d2019-03-29 10:46:16 -0700590static void md_submit_flush_data(struct work_struct *ws);
591
592static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100593{
NeilBrown4bc034d2019-03-29 10:46:16 -0700594 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100595 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100596
NeilBrown2bc13b82019-03-29 10:46:17 -0700597 mddev->start_flush = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700598 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
599 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100600 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100601 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100602 if (rdev->raid_disk >= 0 &&
603 !test_bit(Faulty, &rdev->flags)) {
604 /* Take two references, one is dropped
605 * when request finishes, one after
606 * we reclaim rcu_read_lock
607 */
608 struct bio *bi;
609 atomic_inc(&rdev->nr_pending);
610 atomic_inc(&rdev->nr_pending);
611 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000612 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Xiao Ni5a409b42018-05-21 11:49:54 +0800613 bi->bi_end_io = md_end_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700614 bi->bi_private = rdev;
615 bio_set_dev(bi, rdev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600616 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
NeilBrown4bc034d2019-03-29 10:46:16 -0700617 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500618 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100619 rcu_read_lock();
620 rdev_dec_pending(rdev, mddev);
621 }
622 rcu_read_unlock();
NeilBrown4bc034d2019-03-29 10:46:16 -0700623 if (atomic_dec_and_test(&mddev->flush_pending))
624 queue_work(md_wq, &mddev->flush_work);
625}
NeilBrowna2826aa2009-12-14 12:49:49 +1100626
NeilBrown4bc034d2019-03-29 10:46:16 -0700627static void md_submit_flush_data(struct work_struct *ws)
628{
629 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
630 struct bio *bio = mddev->flush_bio;
631
632 /*
633 * must reset flush_bio before calling into md_handle_request to avoid a
634 * deadlock, because other bios passed md_handle_request suspend check
635 * could wait for this and below md_handle_request could wait for those
636 * bios because of suspend check
637 */
NeilBrown2bc13b82019-03-29 10:46:17 -0700638 mddev->last_flush = mddev->start_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700639 mddev->flush_bio = NULL;
640 wake_up(&mddev->sb_wait);
641
642 if (bio->bi_iter.bi_size == 0) {
643 /* an empty barrier - all done */
644 bio_endio(bio);
645 } else {
646 bio->bi_opf &= ~REQ_PREFLUSH;
647 md_handle_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100648 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100649}
NeilBrown4bc034d2019-03-29 10:46:16 -0700650
David Jeffery775d7832019-09-16 13:15:14 -0400651/*
652 * Manages consolidation of flushes and submitting any flushes needed for
653 * a bio with REQ_PREFLUSH. Returns true if the bio is finished or is
654 * being finished in another context. Returns false if the flushing is
655 * complete but still needs the I/O portion of the bio to be processed.
656 */
657bool md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrown4bc034d2019-03-29 10:46:16 -0700658{
NeilBrown2bc13b82019-03-29 10:46:17 -0700659 ktime_t start = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700660 spin_lock_irq(&mddev->lock);
661 wait_event_lock_irq(mddev->sb_wait,
NeilBrown2bc13b82019-03-29 10:46:17 -0700662 !mddev->flush_bio ||
663 ktime_after(mddev->last_flush, start),
NeilBrown4bc034d2019-03-29 10:46:16 -0700664 mddev->lock);
NeilBrown2bc13b82019-03-29 10:46:17 -0700665 if (!ktime_after(mddev->last_flush, start)) {
666 WARN_ON(mddev->flush_bio);
667 mddev->flush_bio = bio;
668 bio = NULL;
669 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700670 spin_unlock_irq(&mddev->lock);
671
NeilBrown2bc13b82019-03-29 10:46:17 -0700672 if (!bio) {
673 INIT_WORK(&mddev->flush_work, submit_flushes);
674 queue_work(md_wq, &mddev->flush_work);
675 } else {
676 /* flush was performed for some other bio while we waited. */
677 if (bio->bi_iter.bi_size == 0)
678 /* an empty barrier - all done */
679 bio_endio(bio);
680 else {
681 bio->bi_opf &= ~REQ_PREFLUSH;
David Jeffery775d7832019-09-16 13:15:14 -0400682 return false;
NeilBrown2bc13b82019-03-29 10:46:17 -0700683 }
684 }
David Jeffery775d7832019-09-16 13:15:14 -0400685 return true;
NeilBrown4bc034d2019-03-29 10:46:16 -0700686}
Tejun Heoe9c74692010-09-03 11:56:18 +0200687EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100688
NeilBrownfd01b882011-10-11 16:47:53 +1100689static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 atomic_inc(&mddev->active);
692 return mddev;
693}
694
Dan Williams5fd3a172009-03-04 00:57:25 -0700695static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100696
NeilBrownfd01b882011-10-11 16:47:53 +1100697static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
700 return;
NeilBrownd3374822009-01-09 08:31:10 +1100701 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100702 mddev->ctime == 0 && !mddev->hold_active) {
703 /* Array is not configured at all, and not held active,
704 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100705 list_del_init(&mddev->all_mddevs);
Kent Overstreet28dec872018-06-07 20:52:54 -0400706
707 /*
708 * Call queue_work inside the spinlock so that
709 * flush_workqueue() after mddev_find will succeed in waiting
710 * for the work to be done.
711 */
712 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
713 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100714 }
715 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Kees Cook8376d3c2017-10-16 17:01:48 -0700718static void md_safemode_timeout(struct timer_list *t);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400719
NeilBrownfd01b882011-10-11 16:47:53 +1100720void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100721{
Kent Overstreet28dec872018-06-07 20:52:54 -0400722 kobject_init(&mddev->kobj, &md_ktype);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100723 mutex_init(&mddev->open_mutex);
724 mutex_init(&mddev->reconfig_mutex);
725 mutex_init(&mddev->bitmap_info.mutex);
726 INIT_LIST_HEAD(&mddev->disks);
727 INIT_LIST_HEAD(&mddev->all_mddevs);
Kees Cook8376d3c2017-10-16 17:01:48 -0700728 timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100729 atomic_set(&mddev->active, 1);
730 atomic_set(&mddev->openers, 0);
731 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100732 spin_lock_init(&mddev->lock);
NeilBrown4bc034d2019-03-29 10:46:16 -0700733 atomic_set(&mddev->flush_pending, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100734 init_waitqueue_head(&mddev->sb_wait);
735 init_waitqueue_head(&mddev->recovery_wait);
736 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000737 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500738 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100739 mddev->resync_min = 0;
740 mddev->resync_max = MaxSector;
741 mddev->level = LEVEL_NONE;
742}
NeilBrown390ee602010-06-01 19:37:27 +1000743EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100744
NeilBrownf72ffdd2014-09-30 14:23:59 +1000745static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
NeilBrownfd01b882011-10-11 16:47:53 +1100747 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
NeilBrown8f5f02c2011-02-16 13:58:51 +1100749 if (unit && MAJOR(unit) != MD_MAJOR)
750 unit &= ~((1<<MdpMinorShift)-1);
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 retry:
753 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
NeilBrownefeb53c2009-01-09 08:31:10 +1100755 if (unit) {
756 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
757 if (mddev->unit == unit) {
758 mddev_get(mddev);
759 spin_unlock(&all_mddevs_lock);
760 kfree(new);
761 return mddev;
762 }
763
764 if (new) {
765 list_add(&new->all_mddevs, &all_mddevs);
766 spin_unlock(&all_mddevs_lock);
767 new->hold_active = UNTIL_IOCTL;
768 return new;
769 }
770 } else if (new) {
771 /* find an unused unit number */
772 static int next_minor = 512;
773 int start = next_minor;
774 int is_free = 0;
775 int dev = 0;
776 while (!is_free) {
777 dev = MKDEV(MD_MAJOR, next_minor);
778 next_minor++;
779 if (next_minor > MINORMASK)
780 next_minor = 0;
781 if (next_minor == start) {
782 /* Oh dear, all in use. */
783 spin_unlock(&all_mddevs_lock);
784 kfree(new);
785 return NULL;
786 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000787
NeilBrownefeb53c2009-01-09 08:31:10 +1100788 is_free = 1;
789 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
790 if (mddev->unit == dev) {
791 is_free = 0;
792 break;
793 }
794 }
795 new->unit = dev;
796 new->md_minor = MINOR(dev);
797 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 list_add(&new->all_mddevs, &all_mddevs);
799 spin_unlock(&all_mddevs_lock);
800 return new;
801 }
802 spin_unlock(&all_mddevs_lock);
803
NeilBrown9ffae0c2006-01-06 00:20:32 -0800804 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (!new)
806 return NULL;
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 new->unit = unit;
809 if (MAJOR(unit) == MD_MAJOR)
810 new->md_minor = MINOR(unit);
811 else
812 new->md_minor = MINOR(unit) >> MdpMinorShift;
813
NeilBrownfafd7fb2010-04-01 15:55:30 +1100814 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 goto retry;
817}
818
NeilBrownb6eb1272010-04-15 10:13:47 +1000819static struct attribute_group md_redundancy_group;
820
NeilBrown5c47daf2014-12-15 12:57:01 +1100821void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
NeilBrowna64c8762010-04-14 17:15:37 +1000823 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000824 /* These cannot be removed under reconfig_mutex as
825 * an access to the files will try to take reconfig_mutex
826 * while holding the file unremovable, which leads to
827 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000828 * So hold set sysfs_active while the remove in happeing,
829 * and anything else which might set ->to_remove or my
830 * otherwise change the sysfs namespace will fail with
831 * -EBUSY if sysfs_active is still set.
832 * We set sysfs_active under reconfig_mutex and elsewhere
833 * test it under the same mutex to ensure its correct value
834 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000835 */
NeilBrowna64c8762010-04-14 17:15:37 +1000836 struct attribute_group *to_remove = mddev->to_remove;
837 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000838 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000839 mutex_unlock(&mddev->reconfig_mutex);
840
NeilBrown00bcb4a2010-06-01 19:37:23 +1000841 if (mddev->kobj.sd) {
842 if (to_remove != &md_redundancy_group)
843 sysfs_remove_group(&mddev->kobj, to_remove);
844 if (mddev->pers == NULL ||
845 mddev->pers->sync_request == NULL) {
846 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
847 if (mddev->sysfs_action)
848 sysfs_put(mddev->sysfs_action);
849 mddev->sysfs_action = NULL;
850 }
NeilBrowna64c8762010-04-14 17:15:37 +1000851 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000852 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000853 } else
854 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Chris Dunlop751e67c2011-10-19 16:48:26 +1100856 /* As we've dropped the mutex we need a spinlock to
857 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000858 */
859 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700860 md_wakeup_thread(mddev->thread);
NeilBrown4d5324f2017-10-19 12:17:16 +1100861 wake_up(&mddev->sb_wait);
NeilBrown01f96c02011-09-21 15:30:20 +1000862 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
NeilBrown5c47daf2014-12-15 12:57:01 +1100864EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500866struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100867{
868 struct md_rdev *rdev;
869
870 rdev_for_each_rcu(rdev, mddev)
871 if (rdev->desc_nr == nr)
872 return rdev;
873
874 return NULL;
875}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500876EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100877
878static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
NeilBrown3cb03002011-10-11 16:45:26 +1100880 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
NeilBrowndafb20f2012-03-19 12:46:39 +1100882 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (rdev->bdev->bd_dev == dev)
884 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return NULL;
887}
888
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100889struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev)
NeilBrown1ca69c42012-10-11 13:37:33 +1100890{
891 struct md_rdev *rdev;
892
893 rdev_for_each_rcu(rdev, mddev)
894 if (rdev->bdev->bd_dev == dev)
895 return rdev;
896
897 return NULL;
898}
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100899EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100900
NeilBrown84fc4b52011-10-11 16:49:58 +1100901static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800902{
NeilBrown84fc4b52011-10-11 16:49:58 +1100903 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800904 list_for_each_entry(pers, &pers_list, list) {
905 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800906 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800907 if (strcmp(pers->name, clevel)==0)
908 return pers;
909 }
NeilBrown2604b702006-01-06 00:20:36 -0800910 return NULL;
911}
912
Andre Nollb73df2d2008-07-11 22:02:23 +1000913/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100914static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100916 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000917 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
NeilBrownf72ffdd2014-09-30 14:23:59 +1000920static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100923 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000924 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return 0;
926}
927
NeilBrown545c8792012-05-22 13:54:30 +1000928void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800931 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 rdev->sb_loaded = 0;
933 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000934 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100935 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
NeilBrown2699b672011-07-28 11:31:47 +1000937 if (rdev->bb_page) {
938 put_page(rdev->bb_page);
939 rdev->bb_page = NULL;
940 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800941 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
NeilBrown545c8792012-05-22 13:54:30 +1000943EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200945static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700946{
NeilBrown3cb03002011-10-11 16:45:26 +1100947 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100948 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700949
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200950 if (bio->bi_status) {
951 pr_err("md: super_written gets error=%d\n", bio->bi_status);
NeilBrowna9701a32005-11-08 21:39:34 -0800952 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100953 if (!test_bit(Faulty, &rdev->flags)
954 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800955 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100956 set_bit(LastDev, &rdev->flags);
957 }
958 } else
959 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700960
NeilBrowna9701a32005-11-08 21:39:34 -0800961 if (atomic_dec_and_test(&mddev->pending_writes))
962 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700963 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700964 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700965}
966
NeilBrownfd01b882011-10-11 16:47:53 +1100967void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700968 sector_t sector, int size, struct page *page)
969{
970 /* write first size bytes of page to sector of rdev
971 * Increment mddev->pending_writes before returning
972 * and decrement it on completion, waking up sb_wait
973 * if zero is reached.
974 * If an error occurred, call md_error
975 */
NeilBrown46533ff2016-11-18 16:16:11 +1100976 struct bio *bio;
977 int ff = 0;
978
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +0100979 if (!page)
980 return;
981
NeilBrown46533ff2016-11-18 16:16:11 +1100982 if (test_bit(Faulty, &rdev->flags))
983 return;
984
NeilBrown5a850712017-06-21 09:12:21 +1000985 bio = md_bio_alloc_sync(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700986
Shaohua Lied3b98c2016-03-29 14:00:19 -0700987 atomic_inc(&rdev->nr_pending);
988
Christoph Hellwig74d46992017-08-23 19:10:32 +0200989 bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700990 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700991 bio_add_page(bio, page, size, 0);
992 bio->bi_private = rdev;
993 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +1100994
995 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
996 test_bit(FailFast, &rdev->flags) &&
997 !test_bit(LastDev, &rdev->flags))
998 ff = MD_FAILFAST;
Jan Kara5a8948f2017-05-31 09:44:33 +0200999 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -08001000
NeilBrown7bfa19f2005-06-21 17:17:28 -07001001 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -05001002 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -08001003}
1004
NeilBrown46533ff2016-11-18 16:16:11 +11001005int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -08001006{
Tejun Heoe9c74692010-09-03 11:56:18 +02001007 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +10001008 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -08001009 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +11001010 return -EAGAIN;
1011 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001012}
1013
NeilBrown3cb03002011-10-11 16:45:26 +11001014int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -05001015 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
NeilBrown5a850712017-06-21 09:12:21 +10001017 struct bio *bio = md_bio_alloc_sync(rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 int ret;
1019
Christoph Hellwig74d46992017-08-23 19:10:32 +02001020 if (metadata_op && rdev->meta_bdev)
1021 bio_set_dev(bio, rdev->meta_bdev);
1022 else
1023 bio_set_dev(bio, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -05001024 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001025 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -07001026 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +10001027 else if (rdev->mddev->reshape_position != MaxSector &&
1028 (rdev->mddev->reshape_backwards ==
1029 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001030 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001031 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001032 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -05001034
1035 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001037 ret = !bio->bi_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 bio_put(bio);
1039 return ret;
1040}
NeilBrowna8745db2006-01-06 00:20:34 -08001041EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
NeilBrownf72ffdd2014-09-30 14:23:59 +10001043static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (rdev->sb_loaded)
1048 return 0;
1049
Mike Christie796a5cf2016-06-05 14:32:07 -05001050 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 goto fail;
1052 rdev->sb_loaded = 1;
1053 return 0;
1054
1055fail:
NeilBrown9d487392016-11-02 14:16:49 +11001056 pr_err("md: disabled device %s, could not read superblock.\n",
1057 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return -EINVAL;
1059}
1060
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001061static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
NeilBrownf72ffdd2014-09-30 14:23:59 +10001063 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +10001064 sb1->set_uuid1 == sb2->set_uuid1 &&
1065 sb1->set_uuid2 == sb2->set_uuid2 &&
1066 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001069static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 int ret;
1072 mdp_super_t *tmp1, *tmp2;
1073
1074 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
1075 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
1076
1077 if (!tmp1 || !tmp2) {
1078 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 goto abort;
1080 }
1081
1082 *tmp1 = *sb1;
1083 *tmp2 = *sb2;
1084
1085 /*
1086 * nr_disks is not constant
1087 */
1088 tmp1->nr_disks = 0;
1089 tmp2->nr_disks = 0;
1090
Andre Nollce0c8e02008-07-11 22:02:20 +10001091 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001093 kfree(tmp1);
1094 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return ret;
1096}
1097
NeilBrown4d167f02007-05-09 02:35:37 -07001098static u32 md_csum_fold(u32 csum)
1099{
1100 csum = (csum & 0xffff) + (csum >> 16);
1101 return (csum & 0xffff) + (csum >> 16);
1102}
1103
NeilBrownf72ffdd2014-09-30 14:23:59 +10001104static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
NeilBrown4d167f02007-05-09 02:35:37 -07001106 u64 newcsum = 0;
1107 u32 *sb32 = (u32*)sb;
1108 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 unsigned int disk_csum, csum;
1110
1111 disk_csum = sb->sb_csum;
1112 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -07001113
1114 for (i = 0; i < MD_SB_BYTES/4 ; i++)
1115 newcsum += sb32[i];
1116 csum = (newcsum & 0xffffffff) + (newcsum>>32);
1117
NeilBrown4d167f02007-05-09 02:35:37 -07001118#ifdef CONFIG_ALPHA
1119 /* This used to use csum_partial, which was wrong for several
1120 * reasons including that different results are returned on
1121 * different architectures. It isn't critical that we get exactly
1122 * the same return value as before (we always csum_fold before
1123 * testing, and that removes any differences). However as we
1124 * know that csum_partial always returned a 16bit value on
1125 * alphas, do a fold to maximise conformity to previous behaviour.
1126 */
1127 sb->sb_csum = md_csum_fold(disk_csum);
1128#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -07001130#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return csum;
1132}
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134/*
1135 * Handle superblock details.
1136 * We want to be able to handle multiple superblock formats
1137 * so we have a common interface to them all, and an array of
1138 * different handlers.
1139 * We rely on user-space to write the initial superblock, and support
1140 * reading and updating of superblocks.
1141 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +11001142 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 * loads and validates a superblock on dev.
1144 * if refdev != NULL, compare superblocks on both devices
1145 * Return:
1146 * 0 - dev has a superblock that is compatible with refdev
1147 * 1 - dev has a superblock that is compatible and newer than refdev
1148 * so dev should be used as the refdev in future
1149 * -EINVAL superblock incompatible or invalid
1150 * -othererror e.g. -EIO
1151 *
NeilBrownfd01b882011-10-11 16:47:53 +11001152 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 * Verify that dev is acceptable into mddev.
1154 * The first time, mddev->raid_disks will be 0, and data from
1155 * dev should be merged in. Subsequent calls check that dev
1156 * is new enough. Return 0 or -EINVAL
1157 *
NeilBrownfd01b882011-10-11 16:47:53 +11001158 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 * Update the superblock for rdev with data in mddev
1160 * This does not write to disc.
1161 *
1162 */
1163
1164struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001165 char *name;
1166 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +10001167 int (*load_super)(struct md_rdev *rdev,
1168 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001169 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +10001170 int (*validate_super)(struct mddev *mddev,
1171 struct md_rdev *rdev);
1172 void (*sync_super)(struct mddev *mddev,
1173 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001174 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001175 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +10001176 int (*allow_new_offset)(struct md_rdev *rdev,
1177 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178};
1179
1180/*
Andre Noll0894cc32009-06-18 08:49:23 +10001181 * Check that the given mddev has no bitmap.
1182 *
1183 * This function is called from the run method of all personalities that do not
1184 * support bitmaps. It prints an error message and returns non-zero if mddev
1185 * has a bitmap. Otherwise, it returns 0.
1186 *
1187 */
NeilBrownfd01b882011-10-11 16:47:53 +11001188int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001189{
NeilBrownc3d97142009-12-14 12:49:52 +11001190 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001191 return 0;
NeilBrown9d487392016-11-02 14:16:49 +11001192 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +10001193 mdname(mddev), mddev->pers->name);
1194 return 1;
1195}
1196EXPORT_SYMBOL(md_check_no_bitmap);
1197
1198/*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001199 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 */
NeilBrown3cb03002011-10-11 16:45:26 +11001201static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1204 mdp_super_t *sb;
1205 int ret;
Yufen Yu228fc7d2019-10-30 18:47:02 +08001206 bool spare_disk = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 /*
Andre Noll0f420352008-07-11 22:02:23 +10001209 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 * it's at the end of the disk.
1211 *
1212 * It also happens to be a multiple of 4Kb.
1213 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001214 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
NeilBrown0002b272005-09-09 16:23:53 -07001216 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +11001217 if (ret)
1218 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 ret = -EINVAL;
1221
1222 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001223 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +11001226 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 goto abort;
1228 }
1229
1230 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001231 sb->minor_version < 90 ||
1232 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +11001233 pr_warn("Bad version number %d.%d on %s\n",
1234 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 goto abort;
1236 }
1237
1238 if (sb->raid_disks <= 0)
1239 goto abort;
1240
NeilBrown4d167f02007-05-09 02:35:37 -07001241 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001242 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 goto abort;
1244 }
1245
1246 rdev->preferred_minor = sb->md_minor;
1247 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001248 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001249 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001250 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 if (sb->level == LEVEL_MULTIPATH)
1253 rdev->desc_nr = -1;
1254 else
1255 rdev->desc_nr = sb->this_disk.number;
1256
Yufen Yu228fc7d2019-10-30 18:47:02 +08001257 /* not spare disk, or LEVEL_MULTIPATH */
1258 if (sb->level == LEVEL_MULTIPATH ||
1259 (rdev->desc_nr >= 0 &&
Yufen Yu3b7436c2019-12-10 15:01:29 +08001260 rdev->desc_nr < MD_SB_DISKS &&
Yufen Yu228fc7d2019-10-30 18:47:02 +08001261 sb->disks[rdev->desc_nr].state &
1262 ((1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))))
1263 spare_disk = false;
1264
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001265 if (!refdev) {
Yufen Yu228fc7d2019-10-30 18:47:02 +08001266 if (!spare_disk)
Yufen Yu6a5cb532019-10-16 16:00:03 +08001267 ret = 1;
1268 else
1269 ret = 0;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001270 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001272 mdp_super_t *refsb = page_address(refdev->sb_page);
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001273 if (!md_uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001274 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 b, bdevname(refdev->bdev,b2));
1276 goto abort;
1277 }
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001278 if (!md_sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001279 pr_warn("md: %s has same UUID but different superblock to %s\n",
1280 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 goto abort;
1282 }
1283 ev1 = md_event(sb);
1284 ev2 = md_event(refsb);
Yufen Yu6a5cb532019-10-16 16:00:03 +08001285
Yufen Yu228fc7d2019-10-30 18:47:02 +08001286 if (!spare_disk && ev1 > ev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001288 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 ret = 0;
1290 }
NeilBrown8190e752009-06-18 08:48:58 +10001291 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001292 /* Limit to 4TB as metadata cannot record more than that.
1293 * (not needed for Linear and RAID0 as metadata doesn't
1294 * record this size)
1295 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001296 if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001297 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
NeilBrown27a7b262011-09-10 17:21:28 +10001299 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001300 /* "this cannot possibly happen" ... */
1301 ret = -EINVAL;
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 abort:
1304 return ret;
1305}
1306
1307/*
1308 * validate_super for 0.90.0
1309 */
NeilBrownfd01b882011-10-11 16:47:53 +11001310static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001313 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001314 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
NeilBrown41158c72005-06-21 17:17:25 -07001316 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001317 clear_bit(Faulty, &rdev->flags);
1318 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001319 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001320 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (mddev->raid_disks == 0) {
1323 mddev->major_version = 0;
1324 mddev->minor_version = sb->minor_version;
1325 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001326 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001327 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 mddev->ctime = sb->ctime;
1329 mddev->utime = sb->utime;
1330 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001331 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 mddev->layout = sb->layout;
1333 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001334 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001335 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001336 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001337 mddev->bitmap_info.space = 0;
1338 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001339 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001340 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001341 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
NeilBrownf6705572006-03-27 01:18:11 -08001343 if (mddev->minor_version >= 91) {
1344 mddev->reshape_position = sb->reshape_position;
1345 mddev->delta_disks = sb->delta_disks;
1346 mddev->new_level = sb->new_level;
1347 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001348 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001349 if (mddev->delta_disks < 0)
1350 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001351 } else {
1352 mddev->reshape_position = MaxSector;
1353 mddev->delta_disks = 0;
1354 mddev->new_level = mddev->level;
1355 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001356 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001357 }
NeilBrown33f2c352019-09-09 16:52:29 +10001358 if (mddev->level == 0)
1359 mddev->layout = -1;
NeilBrownf6705572006-03-27 01:18:11 -08001360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (sb->state & (1<<MD_SB_CLEAN))
1362 mddev->recovery_cp = MaxSector;
1363 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001364 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 sb->events_lo == sb->cp_events_lo) {
1366 mddev->recovery_cp = sb->recovery_cp;
1367 } else
1368 mddev->recovery_cp = 0;
1369 }
1370
1371 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1372 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1373 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1374 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1375
1376 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001377
1378 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001379 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001380 mddev->bitmap_info.offset =
1381 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001382 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001383 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001384 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001385
NeilBrown41158c72005-06-21 17:17:25 -07001386 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001387 /* Insist on good event counter while assembling, except
1388 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001390 if (sb->disks[rdev->desc_nr].state & (
1391 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001392 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001393 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001394 } else if (mddev->bitmap) {
1395 /* if adding to array with a bitmap, then we can accept an
1396 * older device ... but not too old.
1397 */
NeilBrown41158c72005-06-21 17:17:25 -07001398 if (ev1 < mddev->bitmap->events_cleared)
1399 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001400 if (ev1 < mddev->events)
1401 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001402 } else {
1403 if (ev1 < mddev->events)
1404 /* just a hot-add of a new device, leave raid_disk at -1 */
1405 return 0;
1406 }
NeilBrown41158c72005-06-21 17:17:25 -07001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 desc = sb->disks + rdev->desc_nr;
1410
1411 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001412 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001413 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1414 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001415 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001417 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001418 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1419 /* active but not in sync implies recovery up to
1420 * reshape position. We don't know exactly where
1421 * that is, so set to zero for now */
1422 if (mddev->minor_version >= 91) {
1423 rdev->recovery_offset = 0;
1424 rdev->raid_disk = desc->raid_disk;
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001427 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1428 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001429 if (desc->state & (1<<MD_DISK_FAILFAST))
1430 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001431 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001432 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return 0;
1434}
1435
1436/*
1437 * sync_super for 0.90.0
1438 */
NeilBrownfd01b882011-10-11 16:47:53 +11001439static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001442 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 /* make rdev->sb match mddev data..
1446 *
1447 * 1/ zero out disks
1448 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1449 * 3/ any empty disks < next_spare become removed
1450 *
1451 * disks[0] gets initialised to REMOVED because
1452 * we cannot be sure from other fields if it has
1453 * been initialised or not.
1454 */
1455 int i;
1456 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1457
NeilBrown61181562005-09-09 16:24:02 -07001458 rdev->sb_size = MD_SB_BYTES;
1459
Namhyung Kim65a06f062011-07-27 11:00:36 +10001460 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 memset(sb, 0, sizeof(*sb));
1463
1464 sb->md_magic = MD_SB_MAGIC;
1465 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 sb->patch_version = mddev->patch_version;
1467 sb->gvalid_words = 0; /* ignored */
1468 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1469 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1470 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1471 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1472
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001473 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001475 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 sb->raid_disks = mddev->raid_disks;
1477 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001478 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001479 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 sb->state = 0;
1481 sb->events_hi = (mddev->events>>32);
1482 sb->events_lo = (u32)mddev->events;
1483
NeilBrownf6705572006-03-27 01:18:11 -08001484 if (mddev->reshape_position == MaxSector)
1485 sb->minor_version = 90;
1486 else {
1487 sb->minor_version = 91;
1488 sb->reshape_position = mddev->reshape_position;
1489 sb->new_level = mddev->new_level;
1490 sb->delta_disks = mddev->delta_disks;
1491 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001492 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001493 }
1494 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (mddev->in_sync)
1496 {
1497 sb->recovery_cp = mddev->recovery_cp;
1498 sb->cp_events_hi = (mddev->events>>32);
1499 sb->cp_events_lo = (u32)mddev->events;
1500 if (mddev->recovery_cp == MaxSector)
1501 sb->state = (1<< MD_SB_CLEAN);
1502 } else
1503 sb->recovery_cp = 0;
1504
1505 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001506 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
NeilBrownc3d97142009-12-14 12:49:52 +11001508 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001509 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001512 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001514 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001515 int is_active = test_bit(In_sync, &rdev2->flags);
1516
1517 if (rdev2->raid_disk >= 0 &&
1518 sb->minor_version >= 91)
1519 /* we have nowhere to store the recovery_offset,
1520 * but if it is not below the reshape_position,
1521 * we can piggy-back on that.
1522 */
1523 is_active = 1;
1524 if (rdev2->raid_disk < 0 ||
1525 test_bit(Faulty, &rdev2->flags))
1526 is_active = 0;
1527 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001528 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001530 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001531 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 d = &sb->disks[rdev2->desc_nr];
1533 nr_disks++;
1534 d->number = rdev2->desc_nr;
1535 d->major = MAJOR(rdev2->bdev->bd_dev);
1536 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001537 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 d->raid_disk = rdev2->raid_disk;
1539 else
1540 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001541 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001543 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001545 if (test_bit(In_sync, &rdev2->flags))
1546 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 active++;
1548 working++;
1549 } else {
1550 d->state = 0;
1551 spare++;
1552 working++;
1553 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001554 if (test_bit(WriteMostly, &rdev2->flags))
1555 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001556 if (test_bit(FailFast, &rdev2->flags))
1557 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 /* now set the "removed" and "faulty" bits on any missing devices */
1560 for (i=0 ; i < mddev->raid_disks ; i++) {
1561 mdp_disk_t *d = &sb->disks[i];
1562 if (d->state == 0 && d->number == 0) {
1563 d->number = i;
1564 d->raid_disk = i;
1565 d->state = (1<<MD_DISK_REMOVED);
1566 d->state |= (1<<MD_DISK_FAULTY);
1567 failed++;
1568 }
1569 }
1570 sb->nr_disks = nr_disks;
1571 sb->active_disks = active;
1572 sb->working_disks = working;
1573 sb->failed_disks = failed;
1574 sb->spare_disks = spare;
1575
1576 sb->this_disk = sb->disks[rdev->desc_nr];
1577 sb->sb_csum = calc_sb_csum(sb);
1578}
1579
1580/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001581 * rdev_size_change for 0.90.0
1582 */
1583static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001584super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001585{
Andre Noll58c0fed2009-03-31 14:33:13 +11001586 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001587 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001588 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001589 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001590 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001591 if (!num_sectors || num_sectors > rdev->sb_start)
1592 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001593 /* Limit to 4TB as metadata cannot record more than that.
1594 * 4TB == 2^32 KB, or 2*2^32 sectors.
1595 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001596 if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001597 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001598 do {
1599 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001600 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001601 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001602 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001603}
1604
NeilBrownc6563a82012-05-21 09:27:00 +10001605static int
1606super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1607{
1608 /* non-zero offset changes not possible with v0.90 */
1609 return new_offset == 0;
1610}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001611
1612/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 * version 1 superblock
1614 */
1615
NeilBrownf72ffdd2014-09-30 14:23:59 +10001616static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001618 __le32 disk_csum;
1619 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 unsigned long long newcsum;
1621 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001622 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 disk_csum = sb->sb_csum;
1625 sb->sb_csum = 0;
1626 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001627 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 newcsum += le32_to_cpu(*isuper++);
1629
1630 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001631 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1634 sb->sb_csum = disk_csum;
1635 return cpu_to_le32(csum);
1636}
1637
NeilBrown3cb03002011-10-11 16:45:26 +11001638static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640 struct mdp_superblock_1 *sb;
1641 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001642 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001643 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001645 int bmask;
Yufen Yu228fc7d2019-10-30 18:47:02 +08001646 bool spare_disk = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 /*
Andre Noll0f420352008-07-11 22:02:23 +10001649 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 * It is always aligned to a 4K boundary and
1651 * depeding on minor_version, it can be:
1652 * 0: At least 8K, but less than 12K, from end of device
1653 * 1: At start of device
1654 * 2: 4K from start of device.
1655 */
1656 switch(minor_version) {
1657 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001658 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001659 sb_start -= 8*2;
1660 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 break;
1662 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001663 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 break;
1665 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001666 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 break;
1668 default:
1669 return -EINVAL;
1670 }
Andre Noll0f420352008-07-11 22:02:23 +10001671 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
NeilBrown0002b272005-09-09 16:23:53 -07001673 /* superblock is rarely larger than 1K, but it can be larger,
1674 * and it is safe to read 4k, so we do that
1675 */
1676 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 if (ret) return ret;
1678
Namhyung Kim65a06f062011-07-27 11:00:36 +10001679 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1682 sb->major_version != cpu_to_le32(1) ||
1683 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001684 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001685 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return -EINVAL;
1687
1688 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001689 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 bdevname(rdev->bdev,b));
1691 return -EINVAL;
1692 }
1693 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001694 pr_warn("md: data_size too small on %s\n",
1695 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return -EINVAL;
1697 }
NeilBrownc6563a82012-05-21 09:27:00 +10001698 if (sb->pad0 ||
1699 sb->pad3[0] ||
1700 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1701 /* Some padding is non-zero, might be a new feature */
1702 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 rdev->preferred_minor = 0xffff;
1705 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001706 rdev->new_data_offset = rdev->data_offset;
1707 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1708 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1709 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001710 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
NeilBrown0002b272005-09-09 16:23:53 -07001712 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001713 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001714 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001715 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1716
1717 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001718 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001719 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001720 if (minor_version
1721 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1722 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001723
NeilBrown31b65a02006-07-10 04:44:14 -07001724 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1725 rdev->desc_nr = -1;
1726 else
1727 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1728
NeilBrown2699b672011-07-28 11:31:47 +10001729 if (!rdev->bb_page) {
1730 rdev->bb_page = alloc_page(GFP_KERNEL);
1731 if (!rdev->bb_page)
1732 return -ENOMEM;
1733 }
1734 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1735 rdev->badblocks.count == 0) {
1736 /* need to load the bad block list.
1737 * Currently we limit it to one page.
1738 */
1739 s32 offset;
1740 sector_t bb_sector;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001741 __le64 *bbp;
NeilBrown2699b672011-07-28 11:31:47 +10001742 int i;
1743 int sectors = le16_to_cpu(sb->bblog_size);
1744 if (sectors > (PAGE_SIZE / 512))
1745 return -EINVAL;
1746 offset = le32_to_cpu(sb->bblog_offset);
1747 if (offset == 0)
1748 return -EINVAL;
1749 bb_sector = (long long)offset;
1750 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001751 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001752 return -EIO;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001753 bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10001754 rdev->badblocks.shift = sb->bblog_shift;
1755 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1756 u64 bb = le64_to_cpu(*bbp);
1757 int count = bb & (0x3ff);
1758 u64 sector = bb >> 10;
1759 sector <<= sb->bblog_shift;
1760 count <<= sb->bblog_shift;
1761 if (bb + 1 == 0)
1762 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001763 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001764 return -EINVAL;
1765 }
NeilBrown486adf72013-04-24 11:42:44 +10001766 } else if (sb->bblog_offset != 0)
1767 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001768
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001769 if ((le32_to_cpu(sb->feature_map) &
1770 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001771 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1772 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1773 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1774 }
1775
NeilBrown33f2c352019-09-09 16:52:29 +10001776 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT) &&
1777 sb->level != 0)
1778 return -EINVAL;
1779
Yufen Yu228fc7d2019-10-30 18:47:02 +08001780 /* not spare disk, or LEVEL_MULTIPATH */
1781 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH) ||
1782 (rdev->desc_nr >= 0 &&
1783 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1784 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1785 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)))
1786 spare_disk = false;
Yufen Yu6a5cb532019-10-16 16:00:03 +08001787
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001788 if (!refdev) {
Yufen Yu228fc7d2019-10-30 18:47:02 +08001789 if (!spare_disk)
Yufen Yu6a5cb532019-10-16 16:00:03 +08001790 ret = 1;
1791 else
1792 ret = 0;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001793 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001795 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1798 sb->level != refsb->level ||
1799 sb->layout != refsb->layout ||
1800 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001801 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 bdevname(rdev->bdev,b),
1803 bdevname(refdev->bdev,b2));
1804 return -EINVAL;
1805 }
1806 ev1 = le64_to_cpu(sb->events);
1807 ev2 = le64_to_cpu(refsb->events);
1808
Yufen Yu228fc7d2019-10-30 18:47:02 +08001809 if (!spare_disk && ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001810 ret = 1;
1811 else
1812 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
NeilBrownc6563a82012-05-21 09:27:00 +10001814 if (minor_version) {
1815 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1816 sectors -= rdev->data_offset;
1817 } else
1818 sectors = rdev->sb_start;
1819 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001821 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001822 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
NeilBrownfd01b882011-10-11 16:47:53 +11001825static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001827 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001828 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
NeilBrown41158c72005-06-21 17:17:25 -07001830 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001831 clear_bit(Faulty, &rdev->flags);
1832 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001833 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001834 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (mddev->raid_disks == 0) {
1837 mddev->major_version = 1;
1838 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001839 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001840 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001841 mddev->ctime = le64_to_cpu(sb->ctime);
1842 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001844 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 mddev->layout = le32_to_cpu(sb->layout);
1846 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001847 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001848 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001849 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001850 mddev->bitmap_info.space = 0;
1851 /* Default location for bitmap is 1K after superblock
1852 * using 3K - total of 4K
1853 */
NeilBrownc3d97142009-12-14 12:49:52 +11001854 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001855 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001856 mddev->reshape_backwards = 0;
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1859 memcpy(mddev->uuid, sb->set_uuid, 16);
1860
1861 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001862
NeilBrown71c08052005-09-09 16:23:51 -07001863 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001864 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001865 mddev->bitmap_info.offset =
1866 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001867 /* Metadata doesn't record how much space is available.
1868 * For 1.0, we assume we can use up to the superblock
1869 * if before, else to 4K beyond superblock.
1870 * For others, assume no change is possible.
1871 */
1872 if (mddev->minor_version > 0)
1873 mddev->bitmap_info.space = 0;
1874 else if (mddev->bitmap_info.offset > 0)
1875 mddev->bitmap_info.space =
1876 8 - mddev->bitmap_info.offset;
1877 else
1878 mddev->bitmap_info.space =
1879 -mddev->bitmap_info.offset;
1880 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001881
NeilBrownf6705572006-03-27 01:18:11 -08001882 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1883 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1884 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1885 mddev->new_level = le32_to_cpu(sb->new_level);
1886 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001887 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001888 if (mddev->delta_disks < 0 ||
1889 (mddev->delta_disks == 0 &&
1890 (le32_to_cpu(sb->feature_map)
1891 & MD_FEATURE_RESHAPE_BACKWARDS)))
1892 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001893 } else {
1894 mddev->reshape_position = MaxSector;
1895 mddev->delta_disks = 0;
1896 mddev->new_level = mddev->level;
1897 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001898 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001899 }
1900
NeilBrown33f2c352019-09-09 16:52:29 +10001901 if (mddev->level == 0 &&
1902 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT))
1903 mddev->layout = -1;
1904
Song Liu486b0f72016-08-19 15:34:01 -07001905 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001906 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001907
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001908 if (le32_to_cpu(sb->feature_map) &
1909 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001910 if (le32_to_cpu(sb->feature_map) &
1911 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1912 return -EINVAL;
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001913 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1914 (le32_to_cpu(sb->feature_map) &
1915 MD_FEATURE_MULTIPLE_PPLS))
1916 return -EINVAL;
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001917 set_bit(MD_HAS_PPL, &mddev->flags);
1918 }
NeilBrown41158c72005-06-21 17:17:25 -07001919 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001920 /* Insist of good event counter while assembling, except for
1921 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001923 if (rdev->desc_nr >= 0 &&
1924 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001925 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1926 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001927 if (ev1 < mddev->events)
1928 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001929 } else if (mddev->bitmap) {
1930 /* If adding to array with a bitmap, then we can accept an
1931 * older device, but not too old.
1932 */
NeilBrown41158c72005-06-21 17:17:25 -07001933 if (ev1 < mddev->bitmap->events_cleared)
1934 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001935 if (ev1 < mddev->events)
1936 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001937 } else {
1938 if (ev1 < mddev->events)
1939 /* just a hot-add of a new device, leave raid_disk at -1 */
1940 return 0;
1941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (mddev->level != LEVEL_MULTIPATH) {
1943 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001944 if (rdev->desc_nr < 0 ||
1945 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001946 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001947 rdev->desc_nr = -1;
1948 } else
1949 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001951 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001953 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001954 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 break;
Song Liubac624f2015-08-13 14:31:55 -07001956 case MD_DISK_ROLE_JOURNAL: /* journal device */
1957 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1958 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001959 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001960 return -EINVAL;
1961 }
1962 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001963 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001964 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001965 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 default:
NeilBrownf4667222013-12-09 12:04:56 +11001967 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001968 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001969 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001970 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001971 if (!(le32_to_cpu(sb->feature_map) &
1972 MD_FEATURE_RECOVERY_BITMAP))
1973 rdev->saved_raid_disk = -1;
Guoqing Jiang062f5b2a2019-07-24 11:09:20 +02001974 } else {
1975 /*
1976 * If the array is FROZEN, then the device can't
1977 * be in_sync with rest of array.
1978 */
1979 if (!test_bit(MD_RECOVERY_FROZEN,
1980 &mddev->recovery))
1981 set_bit(In_sync, &rdev->flags);
1982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 rdev->raid_disk = role;
1984 break;
1985 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001986 if (sb->devflags & WriteMostly1)
1987 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001988 if (sb->devflags & FailFast1)
1989 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001990 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1991 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001992 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001993 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return 0;
1996}
1997
NeilBrownfd01b882011-10-11 16:47:53 +11001998static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
2000 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11002001 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 int max_dev, i;
2003 /* make rdev->sb match mddev and rdev data. */
2004
Namhyung Kim65a06f062011-07-27 11:00:36 +10002005 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 sb->feature_map = 0;
2008 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002009 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 memset(sb->pad3, 0, sizeof(sb->pad3));
2011
2012 sb->utime = cpu_to_le64((__u64)mddev->utime);
2013 sb->events = cpu_to_le64(mddev->events);
2014 if (mddev->in_sync)
2015 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07002016 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
2017 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 else
2019 sb->resync_offset = cpu_to_le64(0);
2020
NeilBrown1c05b4b2006-10-21 10:24:08 -07002021 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08002022
NeilBrownf0ca3402006-02-02 14:28:04 -08002023 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11002024 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10002025 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10002026 sb->level = cpu_to_le32(mddev->level);
2027 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11002028 if (test_bit(FailFast, &rdev->flags))
2029 sb->devflags |= FailFast1;
2030 else
2031 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08002032
NeilBrownaeb9b2112011-08-25 14:43:08 +10002033 if (test_bit(WriteMostly, &rdev->flags))
2034 sb->devflags |= WriteMostly1;
2035 else
2036 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10002037 sb->data_offset = cpu_to_le64(rdev->data_offset);
2038 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10002039
NeilBrownc3d97142009-12-14 12:49:52 +11002040 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
2041 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07002042 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07002043 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002044
Shaohua Lif2076e72015-10-08 21:54:12 -07002045 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11002046 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11002047 sb->feature_map |=
2048 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
2049 sb->recovery_offset =
2050 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11002051 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
2052 sb->feature_map |=
2053 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002054 }
Shaohua Li3069aa82015-08-13 14:31:56 -07002055 /* Note: recovery_offset and journal_tail share space */
2056 if (test_bit(Journal, &rdev->flags))
2057 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002058 if (test_bit(Replacement, &rdev->flags))
2059 sb->feature_map |=
2060 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002061
NeilBrownf6705572006-03-27 01:18:11 -08002062 if (mddev->reshape_position != MaxSector) {
2063 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
2064 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
2065 sb->new_layout = cpu_to_le32(mddev->new_layout);
2066 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
2067 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10002068 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10002069 if (mddev->delta_disks == 0 &&
2070 mddev->reshape_backwards)
2071 sb->feature_map
2072 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10002073 if (rdev->new_data_offset != rdev->data_offset) {
2074 sb->feature_map
2075 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
2076 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
2077 - rdev->data_offset));
2078 }
NeilBrownf6705572006-03-27 01:18:11 -08002079 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002080
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10002081 if (mddev_is_clustered(mddev))
2082 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
2083
NeilBrown2699b672011-07-28 11:31:47 +10002084 if (rdev->badblocks.count == 0)
2085 /* Nothing to do for bad blocks*/ ;
2086 else if (sb->bblog_offset == 0)
2087 /* Cannot record bad blocks on this device */
2088 md_error(mddev, rdev);
2089 else {
2090 struct badblocks *bb = &rdev->badblocks;
Christoph Hellwigae506402019-04-04 18:56:13 +02002091 __le64 *bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10002092 u64 *p = bb->page;
2093 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
2094 if (bb->changed) {
2095 unsigned seq;
2096
2097retry:
2098 seq = read_seqbegin(&bb->lock);
2099
2100 memset(bbp, 0xff, PAGE_SIZE);
2101
2102 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08002103 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10002104 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
2105 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08002106 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10002107 }
NeilBrownd0962932012-03-19 12:46:41 +11002108 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002109 if (read_seqretry(&bb->lock, seq))
2110 goto retry;
2111
2112 bb->sector = (rdev->sb_start +
2113 (int)le32_to_cpu(sb->bblog_offset));
2114 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10002115 }
2116 }
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11002119 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (rdev2->desc_nr+1 > max_dev)
2121 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07002122
NeilBrown70471da2009-08-03 10:59:57 +10002123 if (max_dev > le32_to_cpu(sb->max_dev)) {
2124 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07002125 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10002126 rdev->sb_size = max_dev * 2 + 256;
2127 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
2128 if (rdev->sb_size & bmask)
2129 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10002130 } else
2131 max_dev = le32_to_cpu(sb->max_dev);
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 for (i=0; i<max_dev;i++)
Lidong Zhong8df72022017-06-12 10:45:55 +08002134 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
NeilBrownf72ffdd2014-09-30 14:23:59 +10002135
Song Liua97b7892015-10-08 21:54:09 -07002136 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
2137 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01002139 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
Pawel Baldysiakddc08822017-08-16 17:13:45 +02002140 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
2141 sb->feature_map |=
2142 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
2143 else
2144 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01002145 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
2146 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
2147 }
2148
NeilBrowndafb20f2012-03-19 12:46:39 +11002149 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002151 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07002152 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08002153 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07002155 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07002156 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11002157 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002158 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 else
Song Liuc4d4c912015-08-13 14:31:54 -07002160 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 sb->sb_csum = calc_sb_1_csum(sb);
2164}
2165
Chris Webb0cd17fe2008-06-28 08:31:46 +10002166static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11002167super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002168{
2169 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002170 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11002171 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002172 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10002173 if (rdev->data_offset != rdev->new_data_offset)
2174 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10002175 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10002176 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01002177 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002178 max_sectors -= rdev->data_offset;
2179 if (!num_sectors || num_sectors > max_sectors)
2180 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11002181 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10002182 /* minor version 0 with bitmap we can't move */
2183 return 0;
2184 } else {
2185 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10002186 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01002187 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10002188 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11002189 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002190 if (!num_sectors || num_sectors > max_sectors)
2191 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10002192 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002193 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10002194 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10002195 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08002196 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10002197 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11002198 do {
2199 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
2200 rdev->sb_page);
2201 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11002202 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10002203
2204}
2205
2206static int
2207super_1_allow_new_offset(struct md_rdev *rdev,
2208 unsigned long long new_offset)
2209{
2210 /* All necessary checks on new >= old have been done */
2211 struct bitmap *bitmap;
2212 if (new_offset >= rdev->data_offset)
2213 return 1;
2214
2215 /* with 1.0 metadata, there is no metadata to tread on
2216 * so we can always move back */
2217 if (rdev->mddev->minor_version == 0)
2218 return 1;
2219
2220 /* otherwise we must be sure not to step on
2221 * any metadata, so stay:
2222 * 36K beyond start of superblock
2223 * beyond end of badblocks
2224 * beyond write-intent bitmap
2225 */
2226 if (rdev->sb_start + (32+4)*2 > new_offset)
2227 return 0;
2228 bitmap = rdev->mddev->bitmap;
2229 if (bitmap && !rdev->mddev->bitmap_info.file &&
2230 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10002231 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10002232 return 0;
2233 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2234 return 0;
2235
2236 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002237}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Adrian Bunk75c96f82005-05-05 16:16:09 -07002239static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 [0] = {
2241 .name = "0.90.0",
2242 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002243 .load_super = super_90_load,
2244 .validate_super = super_90_validate,
2245 .sync_super = super_90_sync,
2246 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002247 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 },
2249 [1] = {
2250 .name = "md-1",
2251 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002252 .load_super = super_1_load,
2253 .validate_super = super_1_validate,
2254 .sync_super = super_1_sync,
2255 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002256 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 },
2258};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
NeilBrownfd01b882011-10-11 16:47:53 +11002260static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05002261{
2262 if (mddev->sync_super) {
2263 mddev->sync_super(mddev, rdev);
2264 return;
2265 }
2266
2267 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2268
2269 super_types[mddev->major_version].sync_super(mddev, rdev);
2270}
2271
NeilBrownfd01b882011-10-11 16:47:53 +11002272static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
NeilBrown3cb03002011-10-11 16:45:26 +11002274 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
NeilBrown4b809912008-07-21 17:05:25 +10002276 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07002277 rdev_for_each_rcu(rdev, mddev1) {
2278 if (test_bit(Faulty, &rdev->flags) ||
2279 test_bit(Journal, &rdev->flags) ||
2280 rdev->raid_disk == -1)
2281 continue;
2282 rdev_for_each_rcu(rdev2, mddev2) {
2283 if (test_bit(Faulty, &rdev2->flags) ||
2284 test_bit(Journal, &rdev2->flags) ||
2285 rdev2->raid_disk == -1)
2286 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002287 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002288 rdev2->bdev->bd_contains) {
2289 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002290 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002291 }
Song Liu0b020e82015-09-03 23:00:35 -07002292 }
2293 }
NeilBrown4b809912008-07-21 17:05:25 +10002294 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return 0;
2296}
2297
2298static LIST_HEAD(pending_raid_disks);
2299
Andre Nollac5e7112009-08-03 10:59:47 +10002300/*
2301 * Try to register data integrity profile for an mddev
2302 *
2303 * This is called when an array is started and after a disk has been kicked
2304 * from the array. It only succeeds if all working and active component devices
2305 * are integrity capable with matching profiles.
2306 */
NeilBrownfd01b882011-10-11 16:47:53 +11002307int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002308{
NeilBrown3cb03002011-10-11 16:45:26 +11002309 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002310
Andre Nollac5e7112009-08-03 10:59:47 +10002311 if (list_empty(&mddev->disks))
2312 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002313 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2314 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002315 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002316 /* skip spares and non-functional disks */
2317 if (test_bit(Faulty, &rdev->flags))
2318 continue;
2319 if (rdev->raid_disk < 0)
2320 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002321 if (!reference) {
2322 /* Use the first rdev as the reference */
2323 reference = rdev;
2324 continue;
2325 }
2326 /* does this rdev's profile match the reference profile? */
2327 if (blk_integrity_compare(reference->bdev->bd_disk,
2328 rdev->bdev->bd_disk) < 0)
2329 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002330 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002331 if (!reference || !bdev_get_integrity(reference->bdev))
2332 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002333 /*
2334 * All component devices are integrity capable and have matching
2335 * profiles, register the common profile for the md device.
2336 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002337 blk_integrity_register(mddev->gendisk,
2338 bdev_get_integrity(reference->bdev));
2339
NeilBrown9d487392016-11-02 14:16:49 +11002340 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Kent Overstreetafeee512018-05-20 18:25:52 -04002341 if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002342 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002343 mdname(mddev));
2344 return -EINVAL;
2345 }
Andre Nollac5e7112009-08-03 10:59:47 +10002346 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002347}
Andre Nollac5e7112009-08-03 10:59:47 +10002348EXPORT_SYMBOL(md_integrity_register);
2349
Dan Williams1501efa2016-01-13 16:00:07 -08002350/*
2351 * Attempt to add an rdev, but only if it is consistent with the current
2352 * integrity profile
2353 */
2354int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002355{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002356 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002357 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002358
2359 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002360 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002361
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002362 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002363
2364 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002365 return 0;
2366
2367 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002368 pr_err("%s: incompatible integrity profile for %s\n",
2369 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002370 return -ENXIO;
2371 }
2372
2373 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002374}
2375EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002376
NeilBrownf72ffdd2014-09-30 14:23:59 +10002377static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002379 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002380 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002381 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Dan Williams11e2ede2008-04-30 00:52:32 -07002383 /* prevent duplicates */
2384 if (find_rdev(mddev, rdev->bdev->bd_dev))
2385 return -EEXIST;
2386
NeilBrown97b20ef2017-04-13 08:53:48 +10002387 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2388 mddev->pers)
2389 return -EROFS;
2390
Andre Nolldd8ac332009-03-31 14:33:13 +11002391 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002392 if (!test_bit(Journal, &rdev->flags) &&
2393 rdev->sectors &&
2394 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002395 if (mddev->pers) {
2396 /* Cannot change size, so fail
2397 * If mddev->level <= 0, then we don't care
2398 * about aligning sizes (e.g. linear)
2399 */
2400 if (mddev->level > 0)
2401 return -ENOSPC;
2402 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002403 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 /* Verify rdev->desc_nr is unique.
2407 * If it is -1, assign a free number, else
2408 * check number is not in use
2409 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002410 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if (rdev->desc_nr < 0) {
2412 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002413 if (mddev->pers)
2414 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002415 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 choice++;
2417 rdev->desc_nr = choice;
2418 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002419 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002420 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002424 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002425 if (!test_bit(Journal, &rdev->flags) &&
2426 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002427 pr_warn("md: %s: array is limited to %d devices\n",
2428 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002429 return -EBUSY;
2430 }
NeilBrown19133a42005-11-08 21:39:35 -08002431 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002432 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002435 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002436
Guoqing Jiang963c5552019-06-14 17:10:36 +08002437 if (mddev->raid_disks)
Guoqing Jiang404659c2019-12-23 10:48:53 +01002438 mddev_create_serial_pool(mddev, rdev, false);
Guoqing Jiang963c5552019-06-14 17:10:36 +08002439
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002440 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002441 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002442
Tejun Heo0762b8b2008-08-25 19:56:12 +09002443 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002444 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2445 /* failure here is OK */;
2446 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002447
NeilBrown4b809912008-07-21 17:05:25 +10002448 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002449 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002450
2451 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002452 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002455
2456 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002457 pr_warn("md: failed to register dev-%s for %s\n",
2458 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002459 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002462static void rdev_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002463{
NeilBrown3cb03002011-10-11 16:45:26 +11002464 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002465 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002466 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002467}
2468
NeilBrownf72ffdd2014-09-30 14:23:59 +10002469static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002472
Tejun Heo49731ba2011-01-14 18:43:57 +01002473 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002474 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002475 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +01002476 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002478 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002479 sysfs_put(rdev->sysfs_state);
2480 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002481 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002482 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002483 * writing to 'remove' to "dev/state". We also need
2484 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002485 */
NeilBrown4b809912008-07-21 17:05:25 +10002486 synchronize_rcu();
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002487 INIT_WORK(&rdev->del_work, rdev_delayed_delete);
NeilBrown177a99b2008-02-06 01:39:56 -08002488 kobject_get(&rdev->kobj);
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002489 queue_work(md_rdev_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
2491
2492/*
2493 * prevent the device from being mounted, repartitioned or
2494 * otherwise reused by a RAID array (or any other kernel
2495 * subsystem), by bd_claiming the device.
2496 */
NeilBrown3cb03002011-10-11 16:45:26 +11002497static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
2499 int err = 0;
2500 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Tejun Heod4d77622010-11-13 11:55:18 +01002502 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002503 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 if (IS_ERR(bdev)) {
Christoph Hellwigea3edd42020-03-24 08:25:11 +01002505 pr_warn("md: could not open device unknown-block(%u,%u).\n",
2506 MAJOR(dev), MINOR(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 return PTR_ERR(bdev);
2508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 rdev->bdev = bdev;
2510 return err;
2511}
2512
NeilBrown3cb03002011-10-11 16:45:26 +11002513static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
2515 struct block_device *bdev = rdev->bdev;
2516 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002517 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
2519
2520void md_autodetect_dev(dev_t dev);
2521
NeilBrownf72ffdd2014-09-30 14:23:59 +10002522static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
2524 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002525
NeilBrown9d487392016-11-02 14:16:49 +11002526 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002527 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002529 if (test_bit(AutoDetected, &rdev->flags))
2530 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531#endif
2532 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002533 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534}
2535
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002536void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
2538 unbind_rdev_from_array(rdev);
2539 export_rdev(rdev);
2540}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002541EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
NeilBrownfd01b882011-10-11 16:47:53 +11002543static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
NeilBrown0638bb02014-09-25 17:43:47 +10002545 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
NeilBrown0638bb02014-09-25 17:43:47 +10002547 while (!list_empty(&mddev->disks)) {
2548 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2549 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002550 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 mddev->raid_disks = 0;
2553 mddev->major_version = 0;
2554}
2555
NeilBrown6497709b2017-03-15 14:05:14 +11002556static bool set_in_sync(struct mddev *mddev)
2557{
Shaohua Liefa4b772017-10-18 22:08:13 -07002558 lockdep_assert_held(&mddev->lock);
NeilBrown4ad23a972017-03-15 14:05:14 +11002559 if (!mddev->in_sync) {
2560 mddev->sync_checkers++;
2561 spin_unlock(&mddev->lock);
2562 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2563 spin_lock(&mddev->lock);
2564 if (!mddev->in_sync &&
2565 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002566 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002567 /*
2568 * Ensure ->in_sync is visible before we clear
2569 * ->sync_checkers.
2570 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002571 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002572 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2573 sysfs_notify_dirent_safe(mddev->sysfs_state);
2574 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002575 if (--mddev->sync_checkers == 0)
2576 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002577 }
2578 if (mddev->safemode == 1)
2579 mddev->safemode = 0;
2580 return mddev->in_sync;
2581}
2582
NeilBrownf72ffdd2014-09-30 14:23:59 +10002583static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
NeilBrown42543762006-06-26 00:27:57 -07002585 /* Update each superblock (in-memory image), but
2586 * if we are allowed to, skip spares which already
2587 * have the right event counter, or have one earlier
2588 * (which would mean they aren't being marked as dirty
2589 * with the rest of the array)
2590 */
NeilBrown3cb03002011-10-11 16:45:26 +11002591 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002592 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002593 if (rdev->sb_events == mddev->events ||
2594 (nospares &&
2595 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002596 rdev->sb_events+1 == mddev->events)) {
2597 /* Don't update this superblock */
2598 rdev->sb_loaded = 2;
2599 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002600 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002601 rdev->sb_loaded = 1;
2602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 }
2604}
2605
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002606static bool does_sb_need_changing(struct mddev *mddev)
2607{
2608 struct md_rdev *rdev;
2609 struct mdp_superblock_1 *sb;
2610 int role;
2611
2612 /* Find a good rdev */
2613 rdev_for_each(rdev, mddev)
2614 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2615 break;
2616
2617 /* No good device found. */
2618 if (!rdev)
2619 return false;
2620
2621 sb = page_address(rdev->sb_page);
2622 /* Check if a device has become faulty or a spare become active */
2623 rdev_for_each(rdev, mddev) {
2624 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2625 /* Device activated? */
2626 if (role == 0xffff && rdev->raid_disk >=0 &&
2627 !test_bit(Faulty, &rdev->flags))
2628 return true;
2629 /* Device turned faulty? */
2630 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2631 return true;
2632 }
2633
2634 /* Check if any mddev parameters have changed */
2635 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2636 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002637 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002638 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2639 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2640 return true;
2641
2642 return false;
2643}
2644
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002645void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
NeilBrown3cb03002011-10-11 16:45:26 +11002647 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002648 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002649 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002650 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002651 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
NeilBrownd87f0642013-04-24 11:42:40 +10002653 if (mddev->ro) {
2654 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002655 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002656 return;
2657 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002658
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002659repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002660 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002661 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002662 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002663 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002664 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002665 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002666 /* Has someone else has updated the sb */
2667 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002668 if (ret == 0)
2669 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002670 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2671 BIT(MD_SB_CHANGE_DEVS) |
2672 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002673 return;
2674 }
2675 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002676
NeilBrowndb0505d2017-10-17 16:18:36 +11002677 /*
2678 * First make sure individual recovery_offsets are correct
2679 * curr_resync_completed can only be used during recovery.
2680 * During reshape/resync it might use array-addresses rather
2681 * that device addresses.
2682 */
NeilBrowndafb20f2012-03-19 12:46:39 +11002683 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002684 if (rdev->raid_disk >= 0 &&
2685 mddev->delta_disks >= 0 &&
NeilBrowndb0505d2017-10-17 16:18:36 +11002686 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2687 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2688 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002689 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002690 !test_bit(In_sync, &rdev->flags) &&
2691 mddev->curr_resync_completed > rdev->recovery_offset)
2692 rdev->recovery_offset = mddev->curr_resync_completed;
2693
NeilBrownf72ffdd2014-09-30 14:23:59 +10002694 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002695 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002696 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2697 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002698 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002699 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002700 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002701 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002702 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002703 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002704 md_error(mddev, rdev);
2705 }
2706 clear_bit(Blocked, &rdev->flags);
2707 clear_bit(BlockedBadBlocks, &rdev->flags);
2708 wake_up(&rdev->blocked_wait);
2709 }
2710 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002711 wake_up(&mddev->sb_wait);
2712 return;
2713 }
2714
NeilBrown85572d72014-12-15 12:56:56 +11002715 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002716
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002717 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002718
Shaohua Li29530792016-12-08 15:48:19 -08002719 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002720 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002721 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002722 /* just a clean<-> dirty transition, possibly leave spares alone,
2723 * though if events isn't the right even/odd, we will have to do
2724 * spares after all
2725 */
2726 nospares = 1;
2727 if (force_change)
2728 nospares = 0;
2729 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002730 /* If the array is degraded, then skipping spares is both
2731 * dangerous and fairly pointless.
2732 * Dangerous because a device that was removed from the array
2733 * might have a event_count that still looks up-to-date,
2734 * so it can be re-added without a resync.
2735 * Pointless because if there are any spares to skip,
2736 * then a recovery will happen and soon that array won't
2737 * be degraded any more and the spare can go back to sleep then.
2738 */
NeilBrown850b2b422006-10-03 01:15:46 -07002739 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002740
NeilBrown06d91a52005-06-21 17:17:12 -07002741 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002742
2743 /* If this is just a dirty<->clean transition, and the array is clean
2744 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002745 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002746 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002747 && mddev->can_decrease_events
2748 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002749 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002750 mddev->can_decrease_events = 0;
2751 } else {
NeilBrown42543762006-06-26 00:27:57 -07002752 /* otherwise we have to go forward and ... */
2753 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002754 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
NeilBrown403df472014-09-30 15:52:29 +10002757 /*
2758 * This 64-bit counter should never wrap.
2759 * Either we are in around ~1 trillion A.C., assuming
2760 * 1 reboot per second, or we have a bug...
2761 */
2762 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002763
NeilBrowndafb20f2012-03-19 12:46:39 +11002764 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002765 if (rdev->badblocks.changed)
2766 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002767 if (test_bit(Faulty, &rdev->flags))
2768 set_bit(FaultRecorded, &rdev->flags);
2769 }
NeilBrown2699b672011-07-28 11:31:47 +10002770
NeilBrowne6910632008-02-06 01:39:51 -08002771 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002772 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
NeilBrown36a4e1f2011-10-07 14:23:17 +11002774 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2775 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Shaohua Li504634f2016-11-18 09:44:08 -08002777 if (mddev->queue)
2778 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002779rewrite:
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002780 md_bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002781 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002783
NeilBrown42543762006-06-26 00:27:57 -07002784 if (rdev->sb_loaded != 1)
2785 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
NeilBrownf4667222013-12-09 12:04:56 +11002787 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002788 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002789 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002790 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002791 pr_debug("md: (write) %s's sb offset: %llu\n",
2792 bdevname(rdev->bdev, b),
2793 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002794 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002795 if (rdev->badblocks.size) {
2796 md_super_write(mddev, rdev,
2797 rdev->badblocks.sector,
2798 rdev->badblocks.size << 9,
2799 rdev->bb_page);
2800 rdev->badblocks.size = 0;
2801 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002802
NeilBrownf4667222013-12-09 12:04:56 +11002803 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002804 pr_debug("md: %s (skipping faulty)\n",
2805 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002806
NeilBrown7bfa19f2005-06-21 17:17:28 -07002807 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 /* only need to write one superblock... */
2809 break;
2810 }
NeilBrown46533ff2016-11-18 16:16:11 +11002811 if (md_super_wait(mddev) < 0)
2812 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002813 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002814
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002815 if (mddev_is_clustered(mddev) && ret == 0)
2816 md_cluster_ops->metadata_update_finish(mddev);
2817
NeilBrown850b2b422006-10-03 01:15:46 -07002818 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002819 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2820 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002821 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002822 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002823 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002824 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2825 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002826
NeilBrowndafb20f2012-03-19 12:46:39 +11002827 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002828 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2829 clear_bit(Blocked, &rdev->flags);
2830
2831 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002832 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002833 clear_bit(BlockedBadBlocks, &rdev->flags);
2834 wake_up(&rdev->blocked_wait);
2835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002837EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002839static int add_bound_rdev(struct md_rdev *rdev)
2840{
2841 struct mddev *mddev = rdev->mddev;
2842 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002843 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002844
Shaohua Li87d4d912016-01-06 14:37:14 -08002845 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002846 /* If there is hot_add_disk but no hot_remove_disk
2847 * then added disks for geometry changes,
2848 * and should be added immediately.
2849 */
2850 super_types[mddev->major_version].
2851 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002852 if (add_journal)
2853 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002854 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002855 if (add_journal)
2856 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002857 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002858 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002859 return err;
2860 }
2861 }
2862 sysfs_notify_dirent_safe(rdev->sysfs_state);
2863
Shaohua Li29530792016-12-08 15:48:19 -08002864 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002865 if (mddev->degraded)
2866 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2867 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2868 md_new_event(mddev);
2869 md_wakeup_thread(mddev->thread);
2870 return 0;
2871}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Andre Noll7f6ce762008-03-23 18:34:54 +01002873/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002874 * We want to accept with case. For this we use cmd_match.
2875 */
2876static int cmd_match(const char *cmd, const char *str)
2877{
2878 /* See if cmd, written into a sysfs file, matches
2879 * str. They must either be the same, or cmd can
2880 * have a trailing newline
2881 */
2882 while (*cmd && *str && *cmd == *str) {
2883 cmd++;
2884 str++;
2885 }
2886 if (*cmd == '\n')
2887 cmd++;
2888 if (*str || *cmd)
2889 return 0;
2890 return 1;
2891}
2892
NeilBrown86e6ffd2005-11-08 21:39:24 -08002893struct rdev_sysfs_entry {
2894 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002895 ssize_t (*show)(struct md_rdev *, char *);
2896 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002897};
2898
2899static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002900state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002901{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002902 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002903 size_t len = 0;
Mark Rutland6aa7de02017-10-23 14:07:29 -07002904 unsigned long flags = READ_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002905
NeilBrown758bfc82014-12-15 12:56:59 +11002906 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002907 (!test_bit(ExternalBbl, &flags) &&
2908 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002909 len += sprintf(page+len, "faulty%s", sep);
2910 if (test_bit(In_sync, &flags))
2911 len += sprintf(page+len, "in_sync%s", sep);
2912 if (test_bit(Journal, &flags))
2913 len += sprintf(page+len, "journal%s", sep);
2914 if (test_bit(WriteMostly, &flags))
2915 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002916 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002917 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002918 && !test_bit(Faulty, &flags)))
2919 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002920 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002921 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002922 !test_bit(In_sync, &flags))
2923 len += sprintf(page+len, "spare%s", sep);
2924 if (test_bit(WriteErrorSeen, &flags))
2925 len += sprintf(page+len, "write_error%s", sep);
2926 if (test_bit(WantReplacement, &flags))
2927 len += sprintf(page+len, "want_replacement%s", sep);
2928 if (test_bit(Replacement, &flags))
2929 len += sprintf(page+len, "replacement%s", sep);
2930 if (test_bit(ExternalBbl, &flags))
2931 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002932 if (test_bit(FailFast, &flags))
2933 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002934
2935 if (len)
2936 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002937
NeilBrown86e6ffd2005-11-08 21:39:24 -08002938 return len+sprintf(page+len, "\n");
2939}
2940
NeilBrown45dc2de2006-06-26 00:27:58 -07002941static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002942state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002943{
2944 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002945 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002946 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002947 * writemostly - sets write_mostly
2948 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002949 * blocked - sets the Blocked flags
2950 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002951 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002952 * -insync - clear Insync for a device with a slot assigned,
2953 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002954 * write_error - sets WriteErrorSeen
2955 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002956 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002957 */
2958 int err = -EINVAL;
2959 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2960 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002961 if (test_bit(Faulty, &rdev->flags))
2962 err = 0;
2963 else
2964 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002965 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002966 if (rdev->mddev->pers) {
2967 clear_bit(Blocked, &rdev->flags);
2968 remove_and_add_spares(rdev->mddev, rdev);
2969 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002970 if (rdev->raid_disk >= 0)
2971 err = -EBUSY;
2972 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002973 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002974 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002975 if (mddev_is_clustered(mddev))
2976 err = md_cluster_ops->remove_disk(mddev, rdev);
2977
2978 if (err == 0) {
2979 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002980 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08002981 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11002982 md_wakeup_thread(mddev->thread);
2983 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002984 md_new_event(mddev);
2985 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002986 }
NeilBrownf6556752006-06-26 00:28:01 -07002987 } else if (cmd_match(buf, "writemostly")) {
2988 set_bit(WriteMostly, &rdev->flags);
Guoqing Jiang404659c2019-12-23 10:48:53 +01002989 mddev_create_serial_pool(rdev->mddev, rdev, false);
NeilBrownf6556752006-06-26 00:28:01 -07002990 err = 0;
2991 } else if (cmd_match(buf, "-writemostly")) {
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +01002992 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
NeilBrownf6556752006-06-26 00:28:01 -07002993 clear_bit(WriteMostly, &rdev->flags);
2994 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002995 } else if (cmd_match(buf, "blocked")) {
2996 set_bit(Blocked, &rdev->flags);
2997 err = 0;
2998 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002999 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02003000 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10003001 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10003002 /* metadata handler doesn't understand badblocks,
3003 * so we need to fail the device
3004 */
3005 md_error(rdev->mddev, rdev);
3006 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003007 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10003008 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003009 wake_up(&rdev->blocked_wait);
3010 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3011 md_wakeup_thread(rdev->mddev->thread);
3012
3013 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10003014 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
3015 set_bit(In_sync, &rdev->flags);
3016 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11003017 } else if (cmd_match(buf, "failfast")) {
3018 set_bit(FailFast, &rdev->flags);
3019 err = 0;
3020 } else if (cmd_match(buf, "-failfast")) {
3021 clear_bit(FailFast, &rdev->flags);
3022 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07003023 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
3024 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10003025 if (rdev->mddev->pers == NULL) {
3026 clear_bit(In_sync, &rdev->flags);
3027 rdev->saved_raid_disk = rdev->raid_disk;
3028 rdev->raid_disk = -1;
3029 err = 0;
3030 }
NeilBrownd7a9d442011-07-28 11:31:48 +10003031 } else if (cmd_match(buf, "write_error")) {
3032 set_bit(WriteErrorSeen, &rdev->flags);
3033 err = 0;
3034 } else if (cmd_match(buf, "-write_error")) {
3035 clear_bit(WriteErrorSeen, &rdev->flags);
3036 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11003037 } else if (cmd_match(buf, "want_replacement")) {
3038 /* Any non-spare device that is not a replacement can
3039 * become want_replacement at any time, but we then need to
3040 * check if recovery is needed.
3041 */
3042 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07003043 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11003044 !test_bit(Replacement, &rdev->flags))
3045 set_bit(WantReplacement, &rdev->flags);
3046 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3047 md_wakeup_thread(rdev->mddev->thread);
3048 err = 0;
3049 } else if (cmd_match(buf, "-want_replacement")) {
3050 /* Clearing 'want_replacement' is always allowed.
3051 * Once replacements starts it is too late though.
3052 */
3053 err = 0;
3054 clear_bit(WantReplacement, &rdev->flags);
3055 } else if (cmd_match(buf, "replacement")) {
3056 /* Can only set a device as a replacement when array has not
3057 * yet been started. Once running, replacement is automatic
3058 * from spares, or by assigning 'slot'.
3059 */
3060 if (rdev->mddev->pers)
3061 err = -EBUSY;
3062 else {
3063 set_bit(Replacement, &rdev->flags);
3064 err = 0;
3065 }
3066 } else if (cmd_match(buf, "-replacement")) {
3067 /* Similarly, can only clear Replacement before start */
3068 if (rdev->mddev->pers)
3069 err = -EBUSY;
3070 else {
3071 clear_bit(Replacement, &rdev->flags);
3072 err = 0;
3073 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05003074 } else if (cmd_match(buf, "re-add")) {
Yufen Yuee37e622019-04-02 14:22:14 +08003075 if (!rdev->mddev->pers)
3076 err = -EINVAL;
3077 else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) &&
3078 rdev->saved_raid_disk >= 0) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05003079 /* clear_bit is performed _after_ all the devices
3080 * have their local Faulty bit cleared. If any writes
3081 * happen in the meantime in the local node, they
3082 * will land in the local bitmap, which will be synced
3083 * by this node eventually
3084 */
3085 if (!mddev_is_clustered(rdev->mddev) ||
3086 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
3087 clear_bit(Faulty, &rdev->flags);
3088 err = add_bound_rdev(rdev);
3089 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05003090 } else
3091 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02003092 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
3093 set_bit(ExternalBbl, &rdev->flags);
3094 rdev->badblocks.shift = 0;
3095 err = 0;
3096 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
3097 clear_bit(ExternalBbl, &rdev->flags);
3098 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07003099 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10003100 if (!err)
3101 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07003102 return err ? err : len;
3103}
NeilBrown80ca3a42006-07-10 04:44:18 -07003104static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10003105__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003106
3107static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003108errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08003109{
3110 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
3111}
3112
3113static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003114errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08003115{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003116 unsigned int n;
3117 int rv;
3118
3119 rv = kstrtouint(buf, 10, &n);
3120 if (rv < 0)
3121 return rv;
3122 atomic_set(&rdev->corrected_errors, n);
3123 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08003124}
3125static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07003126__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08003127
NeilBrown014236d2006-01-06 00:20:55 -08003128static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003129slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08003130{
Shaohua Lif2076e72015-10-08 21:54:12 -07003131 if (test_bit(Journal, &rdev->flags))
3132 return sprintf(page, "journal\n");
3133 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08003134 return sprintf(page, "none\n");
3135 else
3136 return sprintf(page, "%d\n", rdev->raid_disk);
3137}
3138
3139static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003140slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08003141{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003142 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08003143 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003144
Shaohua Lif2076e72015-10-08 21:54:12 -07003145 if (test_bit(Journal, &rdev->flags))
3146 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08003147 if (strncmp(buf, "none", 4)==0)
3148 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003149 else {
3150 err = kstrtouint(buf, 10, (unsigned int *)&slot);
3151 if (err < 0)
3152 return err;
3153 }
Neil Brown6c2fce22008-06-28 08:31:31 +10003154 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08003155 /* Setting 'slot' on an active array requires also
3156 * updating the 'rd%d' link, and communicating
3157 * with the personality with ->hot_*_disk.
3158 * For now we only support removing
3159 * failed/spare devices. This normally happens automatically,
3160 * but not when the metadata is externally managed.
3161 */
NeilBrownc303da62008-02-06 01:39:51 -08003162 if (rdev->raid_disk == -1)
3163 return -EEXIST;
3164 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10003165 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08003166 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10003167 clear_bit(Blocked, &rdev->flags);
3168 remove_and_add_spares(rdev->mddev, rdev);
3169 if (rdev->raid_disk >= 0)
3170 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08003171 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3172 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10003173 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10003174 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10003175 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10003176 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11003177 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10003178
3179 if (rdev->raid_disk != -1)
3180 return -EBUSY;
3181
NeilBrownc6751b22011-02-02 11:57:13 +11003182 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
3183 return -EBUSY;
3184
Neil Brown6c2fce22008-06-28 08:31:31 +10003185 if (rdev->mddev->pers->hot_add_disk == NULL)
3186 return -EINVAL;
3187
NeilBrownba1b41b2011-01-14 09:14:34 +11003188 if (slot >= rdev->mddev->raid_disks &&
3189 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
3190 return -ENOSPC;
3191
Neil Brown6c2fce22008-06-28 08:31:31 +10003192 rdev->raid_disk = slot;
3193 if (test_bit(In_sync, &rdev->flags))
3194 rdev->saved_raid_disk = slot;
3195 else
3196 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11003197 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11003198 clear_bit(Bitmap_sync, &rdev->flags);
Guoqing Jiang3f79cc22020-04-04 23:57:11 +02003199 err = rdev->mddev->pers->hot_add_disk(rdev->mddev, rdev);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11003200 if (err) {
3201 rdev->raid_disk = -1;
3202 return err;
3203 } else
3204 sysfs_notify_dirent_safe(rdev->sysfs_state);
3205 if (sysfs_link_rdev(rdev->mddev, rdev))
3206 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10003207 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08003208 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11003209 if (slot >= rdev->mddev->raid_disks &&
3210 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08003211 return -ENOSPC;
3212 rdev->raid_disk = slot;
3213 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08003214 clear_bit(Faulty, &rdev->flags);
3215 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08003216 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003217 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08003218 }
NeilBrown014236d2006-01-06 00:20:55 -08003219 return len;
3220}
3221
NeilBrown014236d2006-01-06 00:20:55 -08003222static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07003223__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08003224
NeilBrown93c8cad2006-01-06 00:20:56 -08003225static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003226offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08003227{
Andrew Morton6961ece2006-01-06 00:20:59 -08003228 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08003229}
3230
3231static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003232offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08003233{
NeilBrownc6563a82012-05-21 09:27:00 +10003234 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003235 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003236 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10003237 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003238 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003239 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08003240 /* Must set offset before size, so overlap checks
3241 * can be sane */
3242 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08003243 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10003244 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08003245 return len;
3246}
3247
3248static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07003249__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08003250
NeilBrownc6563a82012-05-21 09:27:00 +10003251static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3252{
3253 return sprintf(page, "%llu\n",
3254 (unsigned long long)rdev->new_data_offset);
3255}
3256
3257static ssize_t new_offset_store(struct md_rdev *rdev,
3258 const char *buf, size_t len)
3259{
3260 unsigned long long new_offset;
3261 struct mddev *mddev = rdev->mddev;
3262
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003263 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10003264 return -EINVAL;
3265
NeilBrownf851b602014-12-11 10:02:10 +11003266 if (mddev->sync_thread ||
3267 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10003268 return -EBUSY;
3269 if (new_offset == rdev->data_offset)
3270 /* reset is always permitted */
3271 ;
3272 else if (new_offset > rdev->data_offset) {
3273 /* must not push array size beyond rdev_sectors */
3274 if (new_offset - rdev->data_offset
3275 + mddev->dev_sectors > rdev->sectors)
3276 return -E2BIG;
3277 }
3278 /* Metadata worries about other space details. */
3279
3280 /* decreasing the offset is inconsistent with a backwards
3281 * reshape.
3282 */
3283 if (new_offset < rdev->data_offset &&
3284 mddev->reshape_backwards)
3285 return -EINVAL;
3286 /* Increasing offset is inconsistent with forwards
3287 * reshape. reshape_direction should be set to
3288 * 'backwards' first.
3289 */
3290 if (new_offset > rdev->data_offset &&
3291 !mddev->reshape_backwards)
3292 return -EINVAL;
3293
3294 if (mddev->pers && mddev->persistent &&
3295 !super_types[mddev->major_version]
3296 .allow_new_offset(rdev, new_offset))
3297 return -E2BIG;
3298 rdev->new_data_offset = new_offset;
3299 if (new_offset > rdev->data_offset)
3300 mddev->reshape_backwards = 1;
3301 else if (new_offset < rdev->data_offset)
3302 mddev->reshape_backwards = 0;
3303
3304 return len;
3305}
3306static struct rdev_sysfs_entry rdev_new_offset =
3307__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3308
NeilBrown83303b62006-01-06 00:21:06 -08003309static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003310rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003311{
Andre Nolldd8ac332009-03-31 14:33:13 +11003312 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003313}
3314
NeilBrownc5d79ad2008-02-06 01:39:54 -08003315static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3316{
3317 /* check if two start/length pairs overlap */
3318 if (s1+l1 <= s2)
3319 return 0;
3320 if (s2+l2 <= s1)
3321 return 0;
3322 return 1;
3323}
3324
Dan Williamsb522adc2009-03-31 15:00:31 +11003325static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3326{
3327 unsigned long long blocks;
3328 sector_t new;
3329
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003330 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003331 return -EINVAL;
3332
3333 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3334 return -EINVAL; /* sector conversion overflow */
3335
3336 new = blocks * 2;
3337 if (new != blocks * 2)
3338 return -EINVAL; /* unsigned long long to sector_t overflow */
3339
3340 *sectors = new;
3341 return 0;
3342}
3343
NeilBrown83303b62006-01-06 00:21:06 -08003344static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003345rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003346{
NeilBrownfd01b882011-10-11 16:47:53 +11003347 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003348 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003349 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003350
Shaohua Lif2076e72015-10-08 21:54:12 -07003351 if (test_bit(Journal, &rdev->flags))
3352 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003353 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003354 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003355 if (rdev->data_offset != rdev->new_data_offset)
3356 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003357 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003358 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003359 sectors = super_types[my_mddev->major_version].
3360 rdev_size_change(rdev, sectors);
3361 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003362 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003363 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003364 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003365 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003366 if (!my_mddev->pers->resize)
3367 /* Cannot change size for RAID0 or Linear etc */
3368 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003369 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003370 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003371 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003372
Andre Nolldd8ac332009-03-31 14:33:13 +11003373 rdev->sectors = sectors;
3374 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003375 /* Need to check that all other rdevs with the same
3376 * ->bdev do not overlap. 'rcu' is sufficient to walk
3377 * the rdev lists safely.
3378 * This check does not provide a hard guarantee, it
3379 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003380 */
NeilBrownfd01b882011-10-11 16:47:53 +11003381 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003382 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003383 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003384
NeilBrown8b1afc32014-09-29 15:33:20 +10003385 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003386 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003387 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003388
NeilBrowndafb20f2012-03-19 12:46:39 +11003389 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003390 if (rdev->bdev == rdev2->bdev &&
3391 rdev != rdev2 &&
3392 overlaps(rdev->data_offset, rdev->sectors,
3393 rdev2->data_offset,
3394 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003395 overlap = 1;
3396 break;
3397 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003398 if (overlap) {
3399 mddev_put(mddev);
3400 break;
3401 }
3402 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003403 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003404 if (overlap) {
3405 /* Someone else could have slipped in a size
3406 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003407 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003408 * safe, and trust userspace not to race with
3409 * itself
3410 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003411 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003412 return -EBUSY;
3413 }
3414 }
NeilBrown83303b62006-01-06 00:21:06 -08003415 return len;
3416}
3417
3418static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003419__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003420
NeilBrown3cb03002011-10-11 16:45:26 +11003421static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003422{
3423 unsigned long long recovery_start = rdev->recovery_offset;
3424
3425 if (test_bit(In_sync, &rdev->flags) ||
3426 recovery_start == MaxSector)
3427 return sprintf(page, "none\n");
3428
3429 return sprintf(page, "%llu\n", recovery_start);
3430}
3431
NeilBrown3cb03002011-10-11 16:45:26 +11003432static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003433{
3434 unsigned long long recovery_start;
3435
3436 if (cmd_match(buf, "none"))
3437 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003438 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003439 return -EINVAL;
3440
3441 if (rdev->mddev->pers &&
3442 rdev->raid_disk >= 0)
3443 return -EBUSY;
3444
3445 rdev->recovery_offset = recovery_start;
3446 if (recovery_start == MaxSector)
3447 set_bit(In_sync, &rdev->flags);
3448 else
3449 clear_bit(In_sync, &rdev->flags);
3450 return len;
3451}
3452
3453static struct rdev_sysfs_entry rdev_recovery_start =
3454__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3455
Vishal Vermafc974ee2015-12-24 19:20:34 -07003456/* sysfs access to bad-blocks list.
3457 * We present two files.
3458 * 'bad-blocks' lists sector numbers and lengths of ranges that
3459 * are recorded as bad. The list is truncated to fit within
3460 * the one-page limit of sysfs.
3461 * Writing "sector length" to this file adds an acknowledged
3462 * bad block list.
3463 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3464 * been acknowledged. Writing to this file adds bad blocks
3465 * without acknowledging them. This is largely for testing.
3466 */
NeilBrown3cb03002011-10-11 16:45:26 +11003467static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003468{
3469 return badblocks_show(&rdev->badblocks, page, 0);
3470}
NeilBrown3cb03002011-10-11 16:45:26 +11003471static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003472{
NeilBrownde393cd2011-07-28 11:31:48 +10003473 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3474 /* Maybe that ack was all we needed */
3475 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3476 wake_up(&rdev->blocked_wait);
3477 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003478}
3479static struct rdev_sysfs_entry rdev_bad_blocks =
3480__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3481
NeilBrown3cb03002011-10-11 16:45:26 +11003482static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003483{
3484 return badblocks_show(&rdev->badblocks, page, 1);
3485}
NeilBrown3cb03002011-10-11 16:45:26 +11003486static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003487{
3488 return badblocks_store(&rdev->badblocks, page, len, 1);
3489}
3490static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3491__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3492
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003493static ssize_t
3494ppl_sector_show(struct md_rdev *rdev, char *page)
3495{
3496 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3497}
3498
3499static ssize_t
3500ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3501{
3502 unsigned long long sector;
3503
3504 if (kstrtoull(buf, 10, &sector) < 0)
3505 return -EINVAL;
3506 if (sector != (sector_t)sector)
3507 return -EINVAL;
3508
3509 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3510 rdev->raid_disk >= 0)
3511 return -EBUSY;
3512
3513 if (rdev->mddev->persistent) {
3514 if (rdev->mddev->major_version == 0)
3515 return -EINVAL;
3516 if ((sector > rdev->sb_start &&
3517 sector - rdev->sb_start > S16_MAX) ||
3518 (sector < rdev->sb_start &&
3519 rdev->sb_start - sector > -S16_MIN))
3520 return -EINVAL;
3521 rdev->ppl.offset = sector - rdev->sb_start;
3522 } else if (!rdev->mddev->external) {
3523 return -EBUSY;
3524 }
3525 rdev->ppl.sector = sector;
3526 return len;
3527}
3528
3529static struct rdev_sysfs_entry rdev_ppl_sector =
3530__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3531
3532static ssize_t
3533ppl_size_show(struct md_rdev *rdev, char *page)
3534{
3535 return sprintf(page, "%u\n", rdev->ppl.size);
3536}
3537
3538static ssize_t
3539ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3540{
3541 unsigned int size;
3542
3543 if (kstrtouint(buf, 10, &size) < 0)
3544 return -EINVAL;
3545
3546 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3547 rdev->raid_disk >= 0)
3548 return -EBUSY;
3549
3550 if (rdev->mddev->persistent) {
3551 if (rdev->mddev->major_version == 0)
3552 return -EINVAL;
3553 if (size > U16_MAX)
3554 return -EINVAL;
3555 } else if (!rdev->mddev->external) {
3556 return -EBUSY;
3557 }
3558 rdev->ppl.size = size;
3559 return len;
3560}
3561
3562static struct rdev_sysfs_entry rdev_ppl_size =
3563__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3564
NeilBrown86e6ffd2005-11-08 21:39:24 -08003565static struct attribute *rdev_default_attrs[] = {
3566 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003567 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003568 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003569 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003570 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003571 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003572 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003573 &rdev_bad_blocks.attr,
3574 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003575 &rdev_ppl_sector.attr,
3576 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003577 NULL,
3578};
3579static ssize_t
3580rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3581{
3582 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003583 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003584
3585 if (!entry->show)
3586 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003587 if (!rdev->mddev)
Marcos Paulo de Souza168b3052019-06-14 15:41:06 -07003588 return -ENODEV;
NeilBrown758bfc82014-12-15 12:56:59 +11003589 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003590}
3591
3592static ssize_t
3593rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3594 const char *page, size_t length)
3595{
3596 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003597 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003598 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003599 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003600
3601 if (!entry->store)
3602 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003603 if (!capable(CAP_SYS_ADMIN))
3604 return -EACCES;
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003605 rv = mddev ? mddev_lock(mddev) : -ENODEV;
NeilBrownca388052008-02-06 01:39:55 -08003606 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003607 if (rdev->mddev == NULL)
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003608 rv = -ENODEV;
NeilBrown27c529b2008-03-04 14:29:33 -08003609 else
3610 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003611 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003612 }
3613 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003614}
3615
3616static void rdev_free(struct kobject *ko)
3617{
NeilBrown3cb03002011-10-11 16:45:26 +11003618 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003619 kfree(rdev);
3620}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003621static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003622 .show = rdev_attr_show,
3623 .store = rdev_attr_store,
3624};
3625static struct kobj_type rdev_ktype = {
3626 .release = rdev_free,
3627 .sysfs_ops = &rdev_sysfs_ops,
3628 .default_attrs = rdev_default_attrs,
3629};
3630
NeilBrown3cb03002011-10-11 16:45:26 +11003631int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003632{
3633 rdev->desc_nr = -1;
3634 rdev->saved_raid_disk = -1;
3635 rdev->raid_disk = -1;
3636 rdev->flags = 0;
3637 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003638 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003639 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003640 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003641 rdev->sb_loaded = 0;
3642 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003643 atomic_set(&rdev->nr_pending, 0);
3644 atomic_set(&rdev->read_errors, 0);
3645 atomic_set(&rdev->corrected_errors, 0);
3646
3647 INIT_LIST_HEAD(&rdev->same_set);
3648 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003649
3650 /* Add space to store bad block list.
3651 * This reserves the space even on arrays where it cannot
3652 * be used - I wonder if that matters
3653 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003654 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003655}
3656EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657/*
3658 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3659 *
3660 * mark the device faulty if:
3661 *
3662 * - the device is nonexistent (zero size)
3663 * - the device has no valid superblock
3664 *
3665 * a faulty rdev _never_ has rdev->sb set.
3666 */
NeilBrown3cb03002011-10-11 16:45:26 +11003667static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668{
3669 char b[BDEVNAME_SIZE];
3670 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003671 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 sector_t size;
3673
NeilBrown9ffae0c2006-01-06 00:20:32 -08003674 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003675 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
NeilBrown2230dfe2011-07-28 11:31:46 +10003678 err = md_rdev_init(rdev);
3679 if (err)
3680 goto abort_free;
3681 err = alloc_disk_sb(rdev);
3682 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 goto abort_free;
3684
NeilBrownc5d79ad2008-02-06 01:39:54 -08003685 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 if (err)
3687 goto abort_free;
3688
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003689 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003690
Mike Snitzer77304d22010-11-08 14:39:12 +01003691 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003693 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 bdevname(rdev->bdev,b));
3695 err = -EINVAL;
3696 goto abort_free;
3697 }
3698
3699 if (super_format >= 0) {
3700 err = super_types[super_format].
3701 load_super(rdev, NULL, super_minor);
3702 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003703 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003704 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003705 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 goto abort_free;
3707 }
3708 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003709 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 bdevname(rdev->bdev,b));
3711 goto abort_free;
3712 }
3713 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 return rdev;
3716
3717abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003718 if (rdev->bdev)
3719 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003720 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 kfree(rdev);
3722 return ERR_PTR(err);
3723}
3724
3725/*
3726 * Check a full RAID array for plausibility
3727 */
3728
Yufen Yu6a5cb532019-10-16 16:00:03 +08003729static int analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
3731 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003732 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 char b[BDEVNAME_SIZE];
3734
3735 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003736 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 switch (super_types[mddev->major_version].
3738 load_super(rdev, freshest, mddev->minor_version)) {
3739 case 1:
3740 freshest = rdev;
3741 break;
3742 case 0:
3743 break;
3744 default:
NeilBrown9d487392016-11-02 14:16:49 +11003745 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003747 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 }
3749
Yufen Yu6a5cb532019-10-16 16:00:03 +08003750 /* Cannot find a valid fresh disk */
3751 if (!freshest) {
3752 pr_warn("md: cannot find a valid disk\n");
3753 return -EINVAL;
3754 }
3755
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 super_types[mddev->major_version].
3757 validate_super(mddev, freshest);
3758
3759 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003760 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003761 if (mddev->max_disks &&
3762 (rdev->desc_nr >= mddev->max_disks ||
3763 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003764 pr_warn("md: %s: %s: only %d devices permitted\n",
3765 mdname(mddev), bdevname(rdev->bdev, b),
3766 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003767 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003768 continue;
3769 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003770 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 if (super_types[mddev->major_version].
3772 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003773 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003775 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 continue;
3777 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 if (mddev->level == LEVEL_MULTIPATH) {
3780 rdev->desc_nr = i++;
3781 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003782 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003783 } else if (rdev->raid_disk >=
3784 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3785 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003786 rdev->raid_disk = -1;
3787 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 }
3789 }
Yufen Yu6a5cb532019-10-16 16:00:03 +08003790
3791 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792}
3793
NeilBrown72e02072009-12-14 12:49:55 +11003794/* Read a fixed-point number.
3795 * Numbers in sysfs attributes should be in "standard" units where
3796 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003797 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003798 * milliseconds or jiffies.
3799 * This function takes a decimal number with a possible fractional
3800 * component, and produces an integer which is the result of
3801 * multiplying that number by 10^'scale'.
3802 * all without any floating-point arithmetic.
3803 */
3804int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3805{
3806 unsigned long result = 0;
3807 long decimals = -1;
3808 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3809 if (*cp == '.')
3810 decimals = 0;
3811 else if (decimals < scale) {
3812 unsigned int value;
3813 value = *cp - '0';
3814 result = result * 10 + value;
3815 if (decimals >= 0)
3816 decimals++;
3817 }
3818 cp++;
3819 }
3820 if (*cp == '\n')
3821 cp++;
3822 if (*cp)
3823 return -EINVAL;
3824 if (decimals < 0)
3825 decimals = 0;
Andy Shevchenkocf891602019-07-23 23:41:55 +03003826 *res = result * int_pow(10, scale - decimals);
NeilBrown72e02072009-12-14 12:49:55 +11003827 return 0;
3828}
3829
NeilBrowneae17012005-11-08 21:39:23 -08003830static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003831safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003832{
3833 int msec = (mddev->safemode_delay*1000)/HZ;
3834 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3835}
3836static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003837safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003838{
NeilBrown16f17b32006-06-26 00:27:37 -07003839 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003840
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003841 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003842 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003843 return -EINVAL;
3844 }
3845
NeilBrown72e02072009-12-14 12:49:55 +11003846 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003847 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003848 if (msec == 0)
3849 mddev->safemode_delay = 0;
3850 else {
NeilBrown19052c02008-08-05 15:54:13 +10003851 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003852 unsigned long new_delay = (msec*HZ)/1000;
3853
3854 if (new_delay == 0)
3855 new_delay = 1;
3856 mddev->safemode_delay = new_delay;
3857 if (new_delay < old_delay || old_delay == 0)
3858 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003859 }
3860 return len;
3861}
3862static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003863__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003864
3865static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003866level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003867{
NeilBrown36d091f2014-12-15 12:56:58 +11003868 struct md_personality *p;
3869 int ret;
3870 spin_lock(&mddev->lock);
3871 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003872 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003873 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003874 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003875 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003876 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003877 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003878 else
NeilBrown36d091f2014-12-15 12:56:58 +11003879 ret = 0;
3880 spin_unlock(&mddev->lock);
3881 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003882}
3883
NeilBrownd9d166c2006-01-06 00:20:51 -08003884static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003885level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003886{
Dan Williamsf2859af2010-05-02 10:04:16 -07003887 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003888 ssize_t rv;
3889 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003890 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003891 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003892 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003893 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003894
NeilBrown67918752014-12-15 12:57:01 +11003895 if (slen == 0 || slen >= sizeof(clevel))
3896 return -EINVAL;
3897
3898 rv = mddev_lock(mddev);
3899 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003900 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003901
3902 if (mddev->pers == NULL) {
3903 strncpy(mddev->clevel, buf, slen);
3904 if (mddev->clevel[slen-1] == '\n')
3905 slen--;
3906 mddev->clevel[slen] = 0;
3907 mddev->level = LEVEL_NONE;
3908 rv = len;
3909 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003910 }
NeilBrown67918752014-12-15 12:57:01 +11003911 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003912 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003913 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003914
3915 /* request to change the personality. Need to ensure:
3916 * - array is not engaged in resync/recovery/reshape
3917 * - old personality can be suspended
3918 * - new personality will access other array.
3919 */
3920
NeilBrown67918752014-12-15 12:57:01 +11003921 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003922 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003923 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003924 mddev->reshape_position != MaxSector ||
3925 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003926 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003927
NeilBrown67918752014-12-15 12:57:01 +11003928 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003929 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003930 pr_warn("md: %s: %s does not support online personality change\n",
3931 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003932 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003933 }
3934
3935 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003936 strncpy(clevel, buf, slen);
3937 if (clevel[slen-1] == '\n')
3938 slen--;
3939 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003940 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003941 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003942
Dan Williamsf2859af2010-05-02 10:04:16 -07003943 if (request_module("md-%s", clevel) != 0)
3944 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003945 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003946 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003947 if (!pers || !try_module_get(pers->owner)) {
3948 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003949 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003950 rv = -EINVAL;
3951 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003952 }
3953 spin_unlock(&pers_lock);
3954
3955 if (pers == mddev->pers) {
3956 /* Nothing to do! */
3957 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003958 rv = len;
3959 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003960 }
3961 if (!pers->takeover) {
3962 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003963 pr_warn("md: %s: %s does not support personality takeover\n",
3964 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003965 rv = -EINVAL;
3966 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003967 }
3968
NeilBrowndafb20f2012-03-19 12:46:39 +11003969 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003970 rdev->new_raid_disk = rdev->raid_disk;
3971
NeilBrown245f46c2009-03-31 14:39:39 +11003972 /* ->takeover must set new_* and/or delta_disks
3973 * if it succeeds, and may set them when it fails.
3974 */
3975 priv = pers->takeover(mddev);
3976 if (IS_ERR(priv)) {
3977 mddev->new_level = mddev->level;
3978 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003979 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003980 mddev->raid_disks -= mddev->delta_disks;
3981 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003982 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003983 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003984 pr_warn("md: %s: %s would not accept array\n",
3985 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003986 rv = PTR_ERR(priv);
3987 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003988 }
3989
3990 /* Looks like we have a winner */
3991 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003992 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003993
3994 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003995 oldpers = mddev->pers;
3996 oldpriv = mddev->private;
3997 mddev->pers = pers;
3998 mddev->private = priv;
3999 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4000 mddev->level = mddev->new_level;
4001 mddev->layout = mddev->new_layout;
4002 mddev->chunk_sectors = mddev->new_chunk_sectors;
4003 mddev->delta_disks = 0;
4004 mddev->reshape_backwards = 0;
4005 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11004006 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10004007
NeilBrowndb721d32014-12-15 12:56:58 +11004008 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11004009 mddev->external) {
4010 /* We are converting from a no-redundancy array
4011 * to a redundancy array and metadata is managed
4012 * externally so we need to be sure that writes
4013 * won't block due to a need to transition
4014 * clean->dirty
4015 * until external management is started.
4016 */
4017 mddev->in_sync = 0;
4018 mddev->safemode_delay = 0;
4019 mddev->safemode = 0;
4020 }
4021
NeilBrowndb721d32014-12-15 12:56:58 +11004022 oldpers->free(mddev, oldpriv);
4023
4024 if (oldpers->sync_request == NULL &&
4025 pers->sync_request != NULL) {
4026 /* need to add the md_redundancy_group */
4027 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11004028 pr_warn("md: cannot register extra attributes for %s\n",
4029 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11004030 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
4031 }
4032 if (oldpers->sync_request != NULL &&
4033 pers->sync_request == NULL) {
4034 /* need to remove the md_redundancy_group */
4035 if (mddev->to_remove == NULL)
4036 mddev->to_remove = &md_redundancy_group;
4037 }
4038
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02004039 module_put(oldpers->owner);
4040
NeilBrowndafb20f2012-03-19 12:46:39 +11004041 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004042 if (rdev->raid_disk < 0)
4043 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11004044 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01004045 rdev->new_raid_disk = -1;
4046 if (rdev->new_raid_disk == rdev->raid_disk)
4047 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10004048 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01004049 }
NeilBrowndafb20f2012-03-19 12:46:39 +11004050 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004051 if (rdev->raid_disk < 0)
4052 continue;
4053 if (rdev->new_raid_disk == rdev->raid_disk)
4054 continue;
4055 rdev->raid_disk = rdev->new_raid_disk;
4056 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10004057 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01004058 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10004059 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11004060 pr_warn("md: cannot register rd%d for %s after level change\n",
4061 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10004062 }
NeilBrowne93f68a2010-06-15 09:36:03 +01004063 }
4064
NeilBrowndb721d32014-12-15 12:56:58 +11004065 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11004066 /* this is now an array without redundancy, so
4067 * it must always be in_sync
4068 */
4069 mddev->in_sync = 1;
4070 del_timer_sync(&mddev->safemode_timer);
4071 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11004072 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11004073 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004074 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10004075 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11004076 if (!mddev->thread)
4077 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10004078 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07004079 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004080 rv = len;
4081out_unlock:
4082 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08004083 return rv;
4084}
4085
4086static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07004087__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08004088
NeilBrownd4dbd022006-06-26 00:27:59 -07004089static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004090layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07004091{
4092 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07004093 if (mddev->reshape_position != MaxSector &&
4094 mddev->layout != mddev->new_layout)
4095 return sprintf(page, "%d (%d)\n",
4096 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07004097 return sprintf(page, "%d\n", mddev->layout);
4098}
4099
4100static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004101layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07004102{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004103 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11004104 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07004105
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004106 err = kstrtouint(buf, 10, &n);
4107 if (err < 0)
4108 return err;
NeilBrown67918752014-12-15 12:57:01 +11004109 err = mddev_lock(mddev);
4110 if (err)
4111 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07004112
NeilBrownb3546032009-03-31 14:56:41 +11004113 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10004114 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004115 err = -EBUSY;
4116 else if (mddev->ro)
4117 err = -EROFS;
4118 else {
4119 mddev->new_layout = n;
4120 err = mddev->pers->check_reshape(mddev);
4121 if (err)
4122 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10004123 }
NeilBrownb3546032009-03-31 14:56:41 +11004124 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07004125 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11004126 if (mddev->reshape_position == MaxSector)
4127 mddev->layout = n;
4128 }
NeilBrown67918752014-12-15 12:57:01 +11004129 mddev_unlock(mddev);
4130 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07004131}
4132static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07004133__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07004134
NeilBrowneae17012005-11-08 21:39:23 -08004135static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004136raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08004137{
NeilBrownbb636542005-11-08 21:39:45 -08004138 if (mddev->raid_disks == 0)
4139 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004140 if (mddev->reshape_position != MaxSector &&
4141 mddev->delta_disks != 0)
4142 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
4143 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08004144 return sprintf(page, "%d\n", mddev->raid_disks);
4145}
4146
NeilBrownfd01b882011-10-11 16:47:53 +11004147static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08004148
4149static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004150raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08004151{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004152 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11004153 int err;
NeilBrownda943b992006-01-06 00:20:54 -08004154
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004155 err = kstrtouint(buf, 10, &n);
4156 if (err < 0)
4157 return err;
NeilBrownda943b992006-01-06 00:20:54 -08004158
NeilBrown67918752014-12-15 12:57:01 +11004159 err = mddev_lock(mddev);
4160 if (err)
4161 return err;
NeilBrownda943b992006-01-06 00:20:54 -08004162 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11004163 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07004164 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10004165 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004166 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10004167
NeilBrown67918752014-12-15 12:57:01 +11004168 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10004169 rdev_for_each(rdev, mddev) {
4170 if (olddisks < n &&
4171 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11004172 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10004173 if (olddisks > n &&
4174 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11004175 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10004176 }
NeilBrown67918752014-12-15 12:57:01 +11004177 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004178 mddev->delta_disks = n - olddisks;
4179 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10004180 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07004181 } else
NeilBrownda943b992006-01-06 00:20:54 -08004182 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11004183out_unlock:
4184 mddev_unlock(mddev);
4185 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08004186}
4187static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07004188__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08004189
NeilBrown24dd4692005-11-08 21:39:26 -08004190static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004191chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08004192{
NeilBrown08a02ec2007-05-09 02:35:38 -07004193 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10004194 mddev->chunk_sectors != mddev->new_chunk_sectors)
4195 return sprintf(page, "%d (%d)\n",
4196 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10004197 mddev->chunk_sectors << 9);
4198 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08004199}
4200
4201static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004202chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08004203{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004204 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11004205 int err;
NeilBrown3b343802006-01-06 00:20:47 -08004206
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004207 err = kstrtoul(buf, 10, &n);
4208 if (err < 0)
4209 return err;
NeilBrown3b343802006-01-06 00:20:47 -08004210
NeilBrown67918752014-12-15 12:57:01 +11004211 err = mddev_lock(mddev);
4212 if (err)
4213 return err;
NeilBrownb3546032009-03-31 14:56:41 +11004214 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10004215 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004216 err = -EBUSY;
4217 else if (mddev->ro)
4218 err = -EROFS;
4219 else {
4220 mddev->new_chunk_sectors = n >> 9;
4221 err = mddev->pers->check_reshape(mddev);
4222 if (err)
4223 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10004224 }
NeilBrownb3546032009-03-31 14:56:41 +11004225 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10004226 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004227 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10004228 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004229 }
NeilBrown67918752014-12-15 12:57:01 +11004230 mddev_unlock(mddev);
4231 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08004232}
4233static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004234__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08004235
NeilBrowna94213b2006-06-26 00:28:00 -07004236static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004237resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07004238{
NeilBrownd1a7c502009-03-31 15:24:32 +11004239 if (mddev->recovery_cp == MaxSector)
4240 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07004241 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4242}
4243
4244static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004245resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07004246{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004247 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11004248 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004249
4250 if (cmd_match(buf, "none"))
4251 n = MaxSector;
4252 else {
4253 err = kstrtoull(buf, 10, &n);
4254 if (err < 0)
4255 return err;
4256 if (n != (sector_t)n)
4257 return -EINVAL;
4258 }
NeilBrowna94213b2006-06-26 00:28:00 -07004259
NeilBrown67918752014-12-15 12:57:01 +11004260 err = mddev_lock(mddev);
4261 if (err)
4262 return err;
NeilBrownb0986362011-05-11 15:52:21 +10004263 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11004264 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07004265
NeilBrown67918752014-12-15 12:57:01 +11004266 if (!err) {
4267 mddev->recovery_cp = n;
4268 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08004269 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11004270 }
4271 mddev_unlock(mddev);
4272 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07004273}
4274static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10004275__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4276 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07004277
NeilBrown9e653b62006-06-26 00:27:58 -07004278/*
4279 * The array state can be:
4280 *
4281 * clear
4282 * No devices, no size, no level
4283 * Equivalent to STOP_ARRAY ioctl
4284 * inactive
4285 * May have some settings, but array is not active
4286 * all IO results in error
4287 * When written, doesn't tear down array, but just stops it
4288 * suspended (not supported yet)
4289 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01004290 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07004291 * readonly
4292 * no resync can happen. no superblocks get written.
4293 * write requests fail
4294 * read-auto
4295 * like readonly, but behaves like 'clean' on a write request.
4296 *
4297 * clean - no pending writes, but otherwise active.
4298 * When written to inactive array, starts without resync
4299 * If a write request arrives then
4300 * if metadata is known, mark 'dirty' and switch to 'active'.
4301 * if not known, block and switch to write-pending
4302 * If written to an active array that has pending writes, then fails.
4303 * active
4304 * fully active: IO and resync can be happening.
4305 * When written to inactive array, starts with resync
4306 *
4307 * write-pending
4308 * clean, but writes are blocked waiting for 'active' to be written.
4309 *
4310 * active-idle
4311 * like active, but no writes have been seen for a while (100msec).
4312 *
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004313 * broken
4314 * RAID0/LINEAR-only: same as clean, but array is missing a member.
4315 * It's useful because RAID0/LINEAR mounted-arrays aren't stopped
4316 * when a member is gone, so this state will at least alert the
4317 * user that something is wrong.
NeilBrown9e653b62006-06-26 00:27:58 -07004318 */
4319enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004320 write_pending, active_idle, broken, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004321static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004322 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004323 "write-pending", "active-idle", "broken", NULL };
NeilBrown9e653b62006-06-26 00:27:58 -07004324
4325static int match_word(const char *word, char **list)
4326{
4327 int n;
4328 for (n=0; list[n]; n++)
4329 if (cmd_match(word, list[n]))
4330 break;
4331 return n;
4332}
4333
4334static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004335array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004336{
4337 enum array_state st = inactive;
4338
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004339 if (mddev->pers && !test_bit(MD_NOT_READY, &mddev->flags)) {
NeilBrown9e653b62006-06-26 00:27:58 -07004340 switch(mddev->ro) {
4341 case 1:
4342 st = readonly;
4343 break;
4344 case 2:
4345 st = read_auto;
4346 break;
4347 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004348 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004349 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004350 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004351 else if (mddev->in_sync)
4352 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004353 else if (mddev->safemode)
4354 st = active_idle;
4355 else
4356 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004357 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004358 }
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004359
4360 if (test_bit(MD_BROKEN, &mddev->flags) && st == clean)
4361 st = broken;
4362 } else {
NeilBrown9e653b62006-06-26 00:27:58 -07004363 if (list_empty(&mddev->disks) &&
4364 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004365 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004366 st = clear;
4367 else
4368 st = inactive;
4369 }
4370 return sprintf(page, "%s\n", array_states[st]);
4371}
4372
NeilBrownf72ffdd2014-09-30 14:23:59 +10004373static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4374static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4375static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004376static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004377
4378static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004379array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004380{
NeilBrown6497709b2017-03-15 14:05:14 +11004381 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004382 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004383
4384 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4385 /* don't take reconfig_mutex when toggling between
4386 * clean and active
4387 */
4388 spin_lock(&mddev->lock);
4389 if (st == active) {
4390 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004391 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004392 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004393 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004394 } else /* st == clean */ {
4395 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004396 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004397 err = -EBUSY;
4398 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004399 if (!err)
4400 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004401 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004402 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004403 }
4404 err = mddev_lock(mddev);
4405 if (err)
4406 return err;
4407 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004408 switch(st) {
4409 case bad_word:
4410 break;
4411 case clear:
4412 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004413 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004414 break;
4415 case inactive:
4416 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004417 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004418 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004419 else
NeilBrowne6910632008-02-06 01:39:51 -08004420 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004421 break;
4422 case suspended:
4423 break; /* not supported yet */
4424 case readonly:
4425 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004426 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004427 else {
4428 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004429 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004430 err = do_md_run(mddev);
4431 }
4432 break;
4433 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004434 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004435 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004436 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004437 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004438 err = restart_array(mddev);
4439 if (err == 0) {
4440 mddev->ro = 2;
4441 set_disk_ro(mddev->gendisk, 0);
4442 }
NeilBrown9e653b62006-06-26 00:27:58 -07004443 } else {
4444 mddev->ro = 2;
4445 err = do_md_run(mddev);
4446 }
4447 break;
4448 case clean:
4449 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004450 err = restart_array(mddev);
4451 if (err)
4452 break;
NeilBrown85572d72014-12-15 12:56:56 +11004453 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004454 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004455 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004456 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004457 } else
4458 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004459 break;
4460 case active:
4461 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004462 err = restart_array(mddev);
4463 if (err)
4464 break;
Shaohua Li29530792016-12-08 15:48:19 -08004465 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004466 wake_up(&mddev->sb_wait);
4467 err = 0;
4468 } else {
4469 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004470 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004471 err = do_md_run(mddev);
4472 }
4473 break;
4474 case write_pending:
4475 case active_idle:
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004476 case broken:
NeilBrown9e653b62006-06-26 00:27:58 -07004477 /* these cannot be set */
4478 break;
4479 }
NeilBrown67918752014-12-15 12:57:01 +11004480
4481 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004482 if (mddev->hold_active == UNTIL_IOCTL)
4483 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004484 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004485 }
NeilBrown67918752014-12-15 12:57:01 +11004486 mddev_unlock(mddev);
4487 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004488}
NeilBrown80ca3a42006-07-10 04:44:18 -07004489static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004490__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004491
NeilBrown6d7ff7382006-01-06 00:21:16 -08004492static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004493max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004494 return sprintf(page, "%d\n",
4495 atomic_read(&mddev->max_corr_read_errors));
4496}
4497
4498static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004499max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004500{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004501 unsigned int n;
4502 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004503
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004504 rv = kstrtouint(buf, 10, &n);
4505 if (rv < 0)
4506 return rv;
4507 atomic_set(&mddev->max_corr_read_errors, n);
4508 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004509}
4510
4511static struct md_sysfs_entry max_corr_read_errors =
4512__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4513 max_corrected_read_errors_store);
4514
4515static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004516null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004517{
4518 return -EINVAL;
4519}
4520
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004521/* need to ensure rdev_delayed_delete() has completed */
4522static void flush_rdev_wq(struct mddev *mddev)
4523{
4524 struct md_rdev *rdev;
4525
4526 rcu_read_lock();
4527 rdev_for_each_rcu(rdev, mddev)
4528 if (work_pending(&rdev->del_work)) {
4529 flush_workqueue(md_rdev_misc_wq);
4530 break;
4531 }
4532 rcu_read_unlock();
4533}
4534
NeilBrown6d7ff7382006-01-06 00:21:16 -08004535static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004536new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004537{
4538 /* buf must be %d:%d\n? giving major and minor numbers */
4539 /* The new device is added to the array.
4540 * If the array has a persistent superblock, we read the
4541 * superblock to initialise info and check validity.
4542 * Otherwise, only checking done is that in bind_rdev_to_array,
4543 * which mainly checks size.
4544 */
4545 char *e;
4546 int major = simple_strtoul(buf, &e, 10);
4547 int minor;
4548 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004549 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004550 int err;
4551
4552 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4553 return -EINVAL;
4554 minor = simple_strtoul(e+1, &e, 10);
4555 if (*e && *e != '\n')
4556 return -EINVAL;
4557 dev = MKDEV(major, minor);
4558 if (major != MAJOR(dev) ||
4559 minor != MINOR(dev))
4560 return -EOVERFLOW;
4561
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004562 flush_rdev_wq(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004563 err = mddev_lock(mddev);
4564 if (err)
4565 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004566 if (mddev->persistent) {
4567 rdev = md_import_device(dev, mddev->major_version,
4568 mddev->minor_version);
4569 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004570 struct md_rdev *rdev0
4571 = list_entry(mddev->disks.next,
4572 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004573 err = super_types[mddev->major_version]
4574 .load_super(rdev, rdev0, mddev->minor_version);
4575 if (err < 0)
4576 goto out;
4577 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004578 } else if (mddev->external)
4579 rdev = md_import_device(dev, -2, -1);
4580 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004581 rdev = md_import_device(dev, -1, -1);
4582
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004583 if (IS_ERR(rdev)) {
4584 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004585 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004586 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004587 err = bind_rdev_to_array(rdev, mddev);
4588 out:
4589 if (err)
4590 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004591 mddev_unlock(mddev);
Alexey Obitotskiy5492c462017-07-28 15:49:25 +02004592 if (!err)
4593 md_new_event(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004594 return err ? err : len;
4595}
4596
4597static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004598__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004599
4600static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004601bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004602{
4603 char *end;
4604 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004605 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004606
NeilBrown67918752014-12-15 12:57:01 +11004607 err = mddev_lock(mddev);
4608 if (err)
4609 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004610 if (!mddev->bitmap)
4611 goto out;
4612 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4613 while (*buf) {
4614 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4615 if (buf == end) break;
4616 if (*end == '-') { /* range */
4617 buf = end + 1;
4618 end_chunk = simple_strtoul(buf, &end, 0);
4619 if (buf == end) break;
4620 }
4621 if (*end && !isspace(*end)) break;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004622 md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004623 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004624 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004625 md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
Paul Clements9b1d1da2006-10-03 01:15:49 -07004626out:
NeilBrown67918752014-12-15 12:57:01 +11004627 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004628 return len;
4629}
4630
4631static struct md_sysfs_entry md_bitmap =
4632__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4633
4634static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004635size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004636{
Andre Noll58c0fed2009-03-31 14:33:13 +11004637 return sprintf(page, "%llu\n",
4638 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004639}
4640
NeilBrownfd01b882011-10-11 16:47:53 +11004641static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004642
4643static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004644size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004645{
4646 /* If array is inactive, we can reduce the component size, but
4647 * not increase it (except from 0).
4648 * If array is active, we can try an on-line resize
4649 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004650 sector_t sectors;
4651 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004652
Andre Noll58c0fed2009-03-31 14:33:13 +11004653 if (err < 0)
4654 return err;
NeilBrown67918752014-12-15 12:57:01 +11004655 err = mddev_lock(mddev);
4656 if (err)
4657 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004658 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004659 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004660 if (err == 0)
4661 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004662 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004663 if (mddev->dev_sectors == 0 ||
4664 mddev->dev_sectors > sectors)
4665 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004666 else
4667 err = -ENOSPC;
4668 }
NeilBrown67918752014-12-15 12:57:01 +11004669 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004670 return err ? err : len;
4671}
4672
4673static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004674__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004675
Masanari Iida83f0d772012-10-30 00:18:08 +09004676/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004677 * This is one of
4678 * 'none' for arrays with no metadata (good luck...)
4679 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004680 * or N.M for internally known formats
4681 */
4682static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004683metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004684{
4685 if (mddev->persistent)
4686 return sprintf(page, "%d.%d\n",
4687 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004688 else if (mddev->external)
4689 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004690 else
4691 return sprintf(page, "none\n");
4692}
4693
4694static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004695metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004696{
4697 int major, minor;
4698 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004699 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004700 /* Changing the details of 'external' metadata is
4701 * always permitted. Otherwise there must be
4702 * no devices attached to the array.
4703 */
NeilBrown67918752014-12-15 12:57:01 +11004704
4705 err = mddev_lock(mddev);
4706 if (err)
4707 return err;
4708 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004709 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4710 ;
4711 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004712 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004713
NeilBrown67918752014-12-15 12:57:01 +11004714 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004715 if (cmd_match(buf, "none")) {
4716 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004717 mddev->external = 0;
4718 mddev->major_version = 0;
4719 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004720 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004721 }
4722 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004723 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004724 if (namelen >= sizeof(mddev->metadata_type))
4725 namelen = sizeof(mddev->metadata_type)-1;
4726 strncpy(mddev->metadata_type, buf+9, namelen);
4727 mddev->metadata_type[namelen] = 0;
4728 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4729 mddev->metadata_type[--namelen] = 0;
4730 mddev->persistent = 0;
4731 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004732 mddev->major_version = 0;
4733 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004734 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004735 }
4736 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004737 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004738 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004739 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004740 buf = e+1;
4741 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004742 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004743 goto out_unlock;
4744 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004745 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004746 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004747 mddev->major_version = major;
4748 mddev->minor_version = minor;
4749 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004750 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004751 err = 0;
4752out_unlock:
4753 mddev_unlock(mddev);
4754 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004755}
4756
4757static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004758__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004759
NeilBrowna35b0d62006-01-06 00:20:49 -08004760static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004761action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004762{
NeilBrown7eec3142005-11-08 21:39:44 -08004763 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004764 unsigned long recovery = mddev->recovery;
4765 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004766 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004767 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4768 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4769 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004770 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004771 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4772 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004773 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004774 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004775 type = "check";
4776 else
4777 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004778 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004779 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004780 else if (mddev->reshape_position != MaxSector)
4781 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004782 }
4783 return sprintf(page, "%s\n", type);
4784}
4785
4786static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004787action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004788{
NeilBrown7eec3142005-11-08 21:39:44 -08004789 if (!mddev->pers || !mddev->pers->sync_request)
4790 return -EINVAL;
4791
NeilBrownb6a9ce62009-05-26 09:41:17 +10004792
4793 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004794 if (cmd_match(page, "frozen"))
4795 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4796 else
4797 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004798 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4799 mddev_lock(mddev) == 0) {
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004800 if (work_pending(&mddev->del_work))
4801 flush_workqueue(md_misc_wq);
NeilBrown8e8e2512015-06-12 19:51:27 +10004802 if (mddev->sync_thread) {
4803 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004804 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004805 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004806 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004807 }
NeilBrown312045e2015-12-21 11:01:21 +11004808 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004809 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004810 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004811 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004812 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004813 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004814 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004815 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004816 int err;
4817 if (mddev->pers->start_reshape == NULL)
4818 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004819 err = mddev_lock(mddev);
4820 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004821 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4822 err = -EBUSY;
4823 else {
4824 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4825 err = mddev->pers->start_reshape(mddev);
4826 }
NeilBrown67918752014-12-15 12:57:01 +11004827 mddev_unlock(mddev);
4828 }
NeilBrown16484bf2006-03-27 01:18:13 -08004829 if (err)
4830 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004831 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004832 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004833 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004834 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004835 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004836 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004837 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004838 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4839 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004840 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004841 if (mddev->ro == 2) {
4842 /* A write to sync_action is enough to justify
4843 * canceling read-auto mode
4844 */
4845 mddev->ro = 0;
4846 md_wakeup_thread(mddev->sync_thread);
4847 }
NeilBrown03c902e2006-01-06 00:20:46 -08004848 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004849 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004850 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004851 return len;
4852}
4853
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004854static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004855__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004856
4857static ssize_t
4858last_sync_action_show(struct mddev *mddev, char *page)
4859{
4860 return sprintf(page, "%s\n", mddev->last_sync_action);
4861}
4862
4863static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4864
NeilBrown9d888832005-11-08 21:39:26 -08004865static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004866mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004867{
4868 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004869 (unsigned long long)
4870 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004871}
4872
NeilBrown80ca3a42006-07-10 04:44:18 -07004873static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004874
NeilBrown88202a02006-01-06 00:21:36 -08004875static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004876sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004877{
4878 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4879 mddev->sync_speed_min ? "local": "system");
4880}
4881
4882static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004883sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004884{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004885 unsigned int min;
4886 int rv;
4887
NeilBrown88202a02006-01-06 00:21:36 -08004888 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004889 min = 0;
4890 } else {
4891 rv = kstrtouint(buf, 10, &min);
4892 if (rv < 0)
4893 return rv;
4894 if (min == 0)
4895 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004896 }
NeilBrown88202a02006-01-06 00:21:36 -08004897 mddev->sync_speed_min = min;
4898 return len;
4899}
4900
4901static struct md_sysfs_entry md_sync_min =
4902__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4903
4904static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004905sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004906{
4907 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4908 mddev->sync_speed_max ? "local": "system");
4909}
4910
4911static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004912sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004913{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004914 unsigned int max;
4915 int rv;
4916
NeilBrown88202a02006-01-06 00:21:36 -08004917 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004918 max = 0;
4919 } else {
4920 rv = kstrtouint(buf, 10, &max);
4921 if (rv < 0)
4922 return rv;
4923 if (max == 0)
4924 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004925 }
NeilBrown88202a02006-01-06 00:21:36 -08004926 mddev->sync_speed_max = max;
4927 return len;
4928}
4929
4930static struct md_sysfs_entry md_sync_max =
4931__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4932
Iustin Popd7f3d292007-10-16 23:30:54 -07004933static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004934degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004935{
4936 return sprintf(page, "%d\n", mddev->degraded);
4937}
4938static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004939
4940static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004941sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004942{
4943 return sprintf(page, "%d\n", mddev->parallel_resync);
4944}
4945
4946static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004947sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004948{
4949 long n;
4950
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004951 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004952 return -EINVAL;
4953
4954 if (n != 0 && n != 1)
4955 return -EINVAL;
4956
4957 mddev->parallel_resync = n;
4958
4959 if (mddev->sync_thread)
4960 wake_up(&resync_wait);
4961
4962 return len;
4963}
4964
4965/* force parallel resync, even with shared block devices */
4966static struct md_sysfs_entry md_sync_force_parallel =
4967__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4968 sync_force_parallel_show, sync_force_parallel_store);
4969
4970static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004971sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004972{
4973 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004974 if (mddev->curr_resync == 0)
4975 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004976 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4977 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004978 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004979 db = resync - mddev->resync_mark_cnt;
4980 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004981}
4982
NeilBrown80ca3a42006-07-10 04:44:18 -07004983static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004984
4985static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004986sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004987{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004988 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004989
NeilBrownacb180b2009-04-14 16:28:34 +10004990 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4991 return sprintf(page, "none\n");
4992
NeilBrown72f36d52012-10-11 14:25:57 +11004993 if (mddev->curr_resync == 1 ||
4994 mddev->curr_resync == 2)
4995 return sprintf(page, "delayed\n");
4996
NeilBrownc804cde2012-05-21 09:28:33 +10004997 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4998 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004999 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08005000 else
Andre Noll58c0fed2009-03-31 14:33:13 +11005001 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08005002
NeilBrownacb180b2009-04-14 16:28:34 +10005003 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11005004 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08005005}
5006
NeilBrown750f1992014-09-30 08:53:05 +10005007static struct md_sysfs_entry md_sync_completed =
5008 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08005009
NeilBrowne464eaf2006-03-27 01:18:14 -08005010static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005011min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10005012{
5013 return sprintf(page, "%llu\n",
5014 (unsigned long long)mddev->resync_min);
5015}
5016static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005017min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10005018{
5019 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11005020 int err;
NeilBrown23da4222014-12-15 12:57:01 +11005021
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005022 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10005023 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11005024
5025 spin_lock(&mddev->lock);
5026 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10005027 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11005028 goto out_unlock;
5029
5030 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10005031 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11005032 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10005033
NeilBrown50c37b12015-03-23 17:36:38 +11005034 /* Round down to multiple of 4K for safety */
5035 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11005036 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10005037
NeilBrown23da4222014-12-15 12:57:01 +11005038out_unlock:
5039 spin_unlock(&mddev->lock);
5040 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10005041}
5042
5043static struct md_sysfs_entry md_min_sync =
5044__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
5045
5046static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005047max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08005048{
5049 if (mddev->resync_max == MaxSector)
5050 return sprintf(page, "max\n");
5051 else
5052 return sprintf(page, "%llu\n",
5053 (unsigned long long)mddev->resync_max);
5054}
5055static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005056max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08005057{
NeilBrown23da4222014-12-15 12:57:01 +11005058 int err;
5059 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08005060 if (strncmp(buf, "max", 3) == 0)
5061 mddev->resync_max = MaxSector;
5062 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10005063 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11005064 int chunk;
5065
5066 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005067 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11005068 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10005069 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11005070 goto out_unlock;
5071
5072 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08005073 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10005074 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08005075 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11005076 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08005077
5078 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11005079 chunk = mddev->chunk_sectors;
5080 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10005081 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11005082
5083 err = -EINVAL;
5084 if (sector_div(temp, chunk))
5085 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08005086 }
5087 mddev->resync_max = max;
5088 }
5089 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11005090 err = 0;
5091out_unlock:
5092 spin_unlock(&mddev->lock);
5093 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08005094}
5095
5096static struct md_sysfs_entry md_max_sync =
5097__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
5098
5099static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005100suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08005101{
5102 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
5103}
5104
5105static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005106suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08005107{
NeilBrownb03e0cc2017-10-19 12:49:15 +11005108 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005109 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08005110
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005111 err = kstrtoull(buf, 10, &new);
5112 if (err < 0)
5113 return err;
5114 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08005115 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11005116
NeilBrown67918752014-12-15 12:57:01 +11005117 err = mddev_lock(mddev);
5118 if (err)
5119 return err;
5120 err = -EINVAL;
5121 if (mddev->pers == NULL ||
5122 mddev->pers->quiesce == NULL)
5123 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005124 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11005125 mddev->suspend_lo = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005126 mddev_resume(mddev);
5127
NeilBrown67918752014-12-15 12:57:01 +11005128 err = 0;
5129unlock:
5130 mddev_unlock(mddev);
5131 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08005132}
5133static struct md_sysfs_entry md_suspend_lo =
5134__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
5135
NeilBrowne464eaf2006-03-27 01:18:14 -08005136static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005137suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08005138{
5139 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
5140}
5141
5142static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005143suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08005144{
NeilBrownb03e0cc2017-10-19 12:49:15 +11005145 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005146 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08005147
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005148 err = kstrtoull(buf, 10, &new);
5149 if (err < 0)
5150 return err;
5151 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08005152 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11005153
NeilBrown67918752014-12-15 12:57:01 +11005154 err = mddev_lock(mddev);
5155 if (err)
5156 return err;
5157 err = -EINVAL;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005158 if (mddev->pers == NULL)
NeilBrown67918752014-12-15 12:57:01 +11005159 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005160
5161 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11005162 mddev->suspend_hi = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005163 mddev_resume(mddev);
5164
NeilBrown67918752014-12-15 12:57:01 +11005165 err = 0;
5166unlock:
5167 mddev_unlock(mddev);
5168 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08005169}
5170static struct md_sysfs_entry md_suspend_hi =
5171__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
5172
NeilBrown08a02ec2007-05-09 02:35:38 -07005173static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005174reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07005175{
5176 if (mddev->reshape_position != MaxSector)
5177 return sprintf(page, "%llu\n",
5178 (unsigned long long)mddev->reshape_position);
5179 strcpy(page, "none\n");
5180 return 5;
5181}
5182
5183static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005184reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07005185{
NeilBrownc6563a82012-05-21 09:27:00 +10005186 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005187 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005188 int err;
NeilBrown67918752014-12-15 12:57:01 +11005189
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005190 err = kstrtoull(buf, 10, &new);
5191 if (err < 0)
5192 return err;
5193 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07005194 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11005195 err = mddev_lock(mddev);
5196 if (err)
5197 return err;
5198 err = -EBUSY;
5199 if (mddev->pers)
5200 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07005201 mddev->reshape_position = new;
5202 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005203 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07005204 mddev->new_level = mddev->level;
5205 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10005206 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10005207 rdev_for_each(rdev, mddev)
5208 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11005209 err = 0;
5210unlock:
5211 mddev_unlock(mddev);
5212 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07005213}
5214
5215static struct md_sysfs_entry md_reshape_position =
5216__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
5217 reshape_position_store);
5218
Dan Williamsb522adc2009-03-31 15:00:31 +11005219static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10005220reshape_direction_show(struct mddev *mddev, char *page)
5221{
5222 return sprintf(page, "%s\n",
5223 mddev->reshape_backwards ? "backwards" : "forwards");
5224}
5225
5226static ssize_t
5227reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
5228{
5229 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11005230 int err;
5231
NeilBrown2c810cd2012-05-21 09:27:00 +10005232 if (cmd_match(buf, "forwards"))
5233 backwards = 0;
5234 else if (cmd_match(buf, "backwards"))
5235 backwards = 1;
5236 else
5237 return -EINVAL;
5238 if (mddev->reshape_backwards == backwards)
5239 return len;
5240
NeilBrown67918752014-12-15 12:57:01 +11005241 err = mddev_lock(mddev);
5242 if (err)
5243 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10005244 /* check if we are allowed to change */
5245 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11005246 err = -EBUSY;
5247 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10005248 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11005249 err = -EINVAL;
5250 else
5251 mddev->reshape_backwards = backwards;
5252 mddev_unlock(mddev);
5253 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10005254}
5255
5256static struct md_sysfs_entry md_reshape_direction =
5257__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5258 reshape_direction_store);
5259
5260static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005261array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11005262{
5263 if (mddev->external_size)
5264 return sprintf(page, "%llu\n",
5265 (unsigned long long)mddev->array_sectors/2);
5266 else
5267 return sprintf(page, "default\n");
5268}
5269
5270static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005271array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11005272{
5273 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11005274 int err;
5275
5276 err = mddev_lock(mddev);
5277 if (err)
5278 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11005279
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005280 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08005281 if (mddev_is_clustered(mddev)) {
5282 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005283 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08005284 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005285
Dan Williamsb522adc2009-03-31 15:00:31 +11005286 if (strncmp(buf, "default", 7) == 0) {
5287 if (mddev->pers)
5288 sectors = mddev->pers->size(mddev, 0, 0);
5289 else
5290 sectors = mddev->array_sectors;
5291
5292 mddev->external_size = 0;
5293 } else {
5294 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11005295 err = -EINVAL;
5296 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5297 err = -E2BIG;
5298 else
5299 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11005300 }
5301
NeilBrown67918752014-12-15 12:57:01 +11005302 if (!err) {
5303 mddev->array_sectors = sectors;
5304 if (mddev->pers) {
5305 set_capacity(mddev->gendisk, mddev->array_sectors);
5306 revalidate_disk(mddev->gendisk);
5307 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11005308 }
NeilBrown67918752014-12-15 12:57:01 +11005309 mddev_unlock(mddev);
5310 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11005311}
5312
5313static struct md_sysfs_entry md_array_size =
5314__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5315 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005316
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005317static ssize_t
5318consistency_policy_show(struct mddev *mddev, char *page)
5319{
5320 int ret;
5321
5322 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5323 ret = sprintf(page, "journal\n");
5324 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5325 ret = sprintf(page, "ppl\n");
5326 } else if (mddev->bitmap) {
5327 ret = sprintf(page, "bitmap\n");
5328 } else if (mddev->pers) {
5329 if (mddev->pers->sync_request)
5330 ret = sprintf(page, "resync\n");
5331 else
5332 ret = sprintf(page, "none\n");
5333 } else {
5334 ret = sprintf(page, "unknown\n");
5335 }
5336
5337 return ret;
5338}
5339
5340static ssize_t
5341consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5342{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005343 int err = 0;
5344
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005345 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005346 if (mddev->pers->change_consistency_policy)
5347 err = mddev->pers->change_consistency_policy(mddev, buf);
5348 else
5349 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005350 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5351 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005352 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005353 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005354 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005355
5356 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005357}
5358
5359static struct md_sysfs_entry md_consistency_policy =
5360__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5361 consistency_policy_store);
5362
Guoqing Jiang9a567842019-07-24 11:09:19 +02005363static ssize_t fail_last_dev_show(struct mddev *mddev, char *page)
5364{
5365 return sprintf(page, "%d\n", mddev->fail_last_dev);
5366}
5367
5368/*
5369 * Setting fail_last_dev to true to allow last device to be forcibly removed
5370 * from RAID1/RAID10.
5371 */
5372static ssize_t
5373fail_last_dev_store(struct mddev *mddev, const char *buf, size_t len)
5374{
5375 int ret;
5376 bool value;
5377
5378 ret = kstrtobool(buf, &value);
5379 if (ret)
5380 return ret;
5381
5382 if (value != mddev->fail_last_dev)
5383 mddev->fail_last_dev = value;
5384
5385 return len;
5386}
5387static struct md_sysfs_entry md_fail_last_dev =
5388__ATTR(fail_last_dev, S_IRUGO | S_IWUSR, fail_last_dev_show,
5389 fail_last_dev_store);
5390
Guoqing Jiang3938f5f2019-12-23 10:48:56 +01005391static ssize_t serialize_policy_show(struct mddev *mddev, char *page)
5392{
5393 if (mddev->pers == NULL || (mddev->pers->level != 1))
5394 return sprintf(page, "n/a\n");
5395 else
5396 return sprintf(page, "%d\n", mddev->serialize_policy);
5397}
5398
5399/*
5400 * Setting serialize_policy to true to enforce write IO is not reordered
5401 * for raid1.
5402 */
5403static ssize_t
5404serialize_policy_store(struct mddev *mddev, const char *buf, size_t len)
5405{
5406 int err;
5407 bool value;
5408
5409 err = kstrtobool(buf, &value);
5410 if (err)
5411 return err;
5412
5413 if (value == mddev->serialize_policy)
5414 return len;
5415
5416 err = mddev_lock(mddev);
5417 if (err)
5418 return err;
5419 if (mddev->pers == NULL || (mddev->pers->level != 1)) {
5420 pr_err("md: serialize_policy is only effective for raid1\n");
5421 err = -EINVAL;
5422 goto unlock;
5423 }
5424
5425 mddev_suspend(mddev);
5426 if (value)
5427 mddev_create_serial_pool(mddev, NULL, true);
5428 else
5429 mddev_destroy_serial_pool(mddev, NULL, true);
5430 mddev->serialize_policy = value;
5431 mddev_resume(mddev);
5432unlock:
5433 mddev_unlock(mddev);
5434 return err ?: len;
5435}
5436
5437static struct md_sysfs_entry md_serialize_policy =
5438__ATTR(serialize_policy, S_IRUGO | S_IWUSR, serialize_policy_show,
5439 serialize_policy_store);
5440
5441
NeilBrowneae17012005-11-08 21:39:23 -08005442static struct attribute *md_default_attrs[] = {
5443 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005444 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005445 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005446 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005447 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005448 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005449 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005450 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005451 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005452 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005453 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005454 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005455 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005456 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005457 &md_consistency_policy.attr,
Guoqing Jiang9a567842019-07-24 11:09:19 +02005458 &md_fail_last_dev.attr,
Guoqing Jiang3938f5f2019-12-23 10:48:56 +01005459 &md_serialize_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005460 NULL,
5461};
5462
5463static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005464 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005465 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005466 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005467 &md_sync_min.attr,
5468 &md_sync_max.attr,
5469 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005470 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005471 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005472 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005473 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005474 &md_suspend_lo.attr,
5475 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005476 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005477 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005478 NULL,
5479};
NeilBrown411036f2005-11-08 21:39:40 -08005480static struct attribute_group md_redundancy_group = {
5481 .name = NULL,
5482 .attrs = md_redundancy_attrs,
5483};
5484
NeilBrowneae17012005-11-08 21:39:23 -08005485static ssize_t
5486md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5487{
5488 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005489 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005490 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005491
5492 if (!entry->show)
5493 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005494 spin_lock(&all_mddevs_lock);
5495 if (list_empty(&mddev->all_mddevs)) {
5496 spin_unlock(&all_mddevs_lock);
5497 return -EBUSY;
5498 }
5499 mddev_get(mddev);
5500 spin_unlock(&all_mddevs_lock);
5501
NeilBrownb7b17c92014-12-15 12:56:59 +11005502 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005503 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005504 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005505}
5506
5507static ssize_t
5508md_attr_store(struct kobject *kobj, struct attribute *attr,
5509 const char *page, size_t length)
5510{
5511 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005512 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005513 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005514
5515 if (!entry->store)
5516 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005517 if (!capable(CAP_SYS_ADMIN))
5518 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005519 spin_lock(&all_mddevs_lock);
5520 if (list_empty(&mddev->all_mddevs)) {
5521 spin_unlock(&all_mddevs_lock);
5522 return -EBUSY;
5523 }
5524 mddev_get(mddev);
5525 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005526 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005527 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005528 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005529}
5530
5531static void md_free(struct kobject *ko)
5532{
NeilBrownfd01b882011-10-11 16:47:53 +11005533 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005534
5535 if (mddev->sysfs_state)
5536 sysfs_put(mddev->sysfs_state);
5537
Bart Van Assched8115c352018-02-28 10:15:29 -08005538 if (mddev->gendisk)
5539 del_gendisk(mddev->gendisk);
NeilBrown6cd18e72015-04-27 14:12:22 +10005540 if (mddev->queue)
5541 blk_cleanup_queue(mddev->queue);
Bart Van Assched8115c352018-02-28 10:15:29 -08005542 if (mddev->gendisk)
NeilBrowna21d1502009-01-09 08:31:09 +11005543 put_disk(mddev->gendisk);
NeilBrown4ad23a972017-03-15 14:05:14 +11005544 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005545
Kent Overstreet28dec872018-06-07 20:52:54 -04005546 bioset_exit(&mddev->bio_set);
5547 bioset_exit(&mddev->sync_set);
NeilBrowneae17012005-11-08 21:39:23 -08005548 kfree(mddev);
5549}
5550
Emese Revfy52cf25d2010-01-19 02:58:23 +01005551static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005552 .show = md_attr_show,
5553 .store = md_attr_store,
5554};
5555static struct kobj_type md_ktype = {
5556 .release = md_free,
5557 .sysfs_ops = &md_sysfs_ops,
5558 .default_attrs = md_default_attrs,
5559};
5560
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561int mdp_major = 0;
5562
Dan Williams5fd3a172009-03-04 00:57:25 -07005563static void mddev_delayed_delete(struct work_struct *ws)
5564{
NeilBrownfd01b882011-10-11 16:47:53 +11005565 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005566
NeilBrown43a70502009-12-14 12:49:55 +11005567 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005568 kobject_del(&mddev->kobj);
5569 kobject_put(&mddev->kobj);
5570}
5571
NeilBrown4ad23a972017-03-15 14:05:14 +11005572static void no_op(struct percpu_ref *r) {}
5573
NeilBrowna415c0f2017-06-05 16:05:13 +10005574int mddev_init_writes_pending(struct mddev *mddev)
5575{
5576 if (mddev->writes_pending.percpu_count_ptr)
5577 return 0;
Roman Gushchinddde2af2019-05-07 10:01:49 -07005578 if (percpu_ref_init(&mddev->writes_pending, no_op,
5579 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL) < 0)
NeilBrowna415c0f2017-06-05 16:05:13 +10005580 return -ENOMEM;
5581 /* We want to start with the refcount at zero */
5582 percpu_ref_put(&mddev->writes_pending);
5583 return 0;
5584}
5585EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5586
NeilBrownefeb53c2009-01-09 08:31:10 +11005587static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588{
NeilBrown039b7222017-04-12 16:26:13 +10005589 /*
5590 * If dev is zero, name is the name of a device to allocate with
5591 * an arbitrary minor number. It will be "md_???"
5592 * If dev is non-zero it must be a device number with a MAJOR of
5593 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5594 * the device is being created by opening a node in /dev.
5595 * If "name" is not NULL, the device is being created by
5596 * writing to /sys/module/md_mod/parameters/new_array.
5597 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005598 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005599 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005601 int partitioned;
5602 int shift;
5603 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005604 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605
5606 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005607 return -ENODEV;
5608
5609 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5610 shift = partitioned ? MdpMinorShift : 0;
5611 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612
Tejun Heoe804ac72010-10-15 15:36:08 +02005613 /* wait for any previous instance of this device to be
5614 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005615 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005616 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005617
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005618 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005619 error = -EEXIST;
5620 if (mddev->gendisk)
5621 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005622
NeilBrown039b7222017-04-12 16:26:13 +10005623 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005624 /* Need to ensure that 'name' is not a duplicate.
5625 */
NeilBrownfd01b882011-10-11 16:47:53 +11005626 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005627 spin_lock(&all_mddevs_lock);
5628
5629 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5630 if (mddev2->gendisk &&
5631 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5632 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005633 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005634 }
5635 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 }
NeilBrown039b7222017-04-12 16:26:13 +10005637 if (name && dev)
5638 /*
5639 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5640 */
5641 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005642
NeilBrown0909dc42009-07-01 12:27:21 +10005643 error = -ENOMEM;
Christoph Hellwig3d745ea2020-03-27 09:30:11 +01005644 mddev->queue = blk_alloc_queue(md_make_request, NUMA_NO_NODE);
NeilBrown0909dc42009-07-01 12:27:21 +10005645 if (!mddev->queue)
5646 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005647 mddev->queue->queuedata = mddev;
5648
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005649 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005650
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 disk = alloc_disk(1 << shift);
5652 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005653 blk_cleanup_queue(mddev->queue);
5654 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005655 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005657 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005659 if (name)
5660 strcpy(disk->disk_name, name);
5661 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005663 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 disk->fops = &md_fops;
5666 disk->private_data = mddev;
5667 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005668 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005669 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005670 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005671 * remove it now.
5672 */
5673 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005675 /* As soon as we call add_disk(), another thread could get
5676 * through to md_open, so make sure it doesn't get too far
5677 */
5678 mutex_lock(&mddev->open_mutex);
5679 add_disk(disk);
5680
Kent Overstreet28dec872018-06-07 20:52:54 -04005681 error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005682 if (error) {
5683 /* This isn't possible, but as kobject_init_and_add is marked
5684 * __must_check, we must do something with the result
5685 */
NeilBrown9d487392016-11-02 14:16:49 +11005686 pr_debug("md: cannot register %s/md - name in use\n",
5687 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005688 error = 0;
5689 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005690 if (mddev->kobj.sd &&
5691 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005692 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005693 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005694 abort:
5695 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005696 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005697 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005698 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005699 }
NeilBrownd3374822009-01-09 08:31:10 +11005700 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005701 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005702}
5703
5704static struct kobject *md_probe(dev_t dev, int *part, void *data)
5705{
NeilBrown78b63502017-04-12 16:26:13 +10005706 if (create_on_open)
5707 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 return NULL;
5709}
5710
Kees Cooke4dca7b2017-10-17 19:04:42 -07005711static int add_named_array(const char *val, const struct kernel_param *kp)
NeilBrownefeb53c2009-01-09 08:31:10 +11005712{
NeilBrown039b7222017-04-12 16:26:13 +10005713 /*
5714 * val must be "md_*" or "mdNNN".
5715 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005716 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005717 * For "mdNNN" we allocate an array with the minor number NNN
5718 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005719 */
5720 int len = strlen(val);
5721 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005722 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005723
5724 while (len && val[len-1] == '\n')
5725 len--;
5726 if (len >= DISK_NAME_LEN)
5727 return -E2BIG;
5728 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005729 if (strncmp(buf, "md_", 3) == 0)
5730 return md_alloc(0, buf);
5731 if (strncmp(buf, "md", 2) == 0 &&
5732 isdigit(buf[2]) &&
5733 kstrtoul(buf+2, 10, &devnum) == 0 &&
5734 devnum <= MINORMASK)
5735 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5736
5737 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005738}
5739
Kees Cook8376d3c2017-10-16 17:01:48 -07005740static void md_safemode_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741{
Kees Cook8376d3c2017-10-16 17:01:48 -07005742 struct mddev *mddev = from_timer(mddev, t, safemode_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743
NeilBrown4ad23a972017-03-15 14:05:14 +11005744 mddev->safemode = 1;
5745 if (mddev->external)
5746 sysfs_notify_dirent_safe(mddev->sysfs_state);
5747
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 md_wakeup_thread(mddev->thread);
5749}
5750
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005751static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752
NeilBrownfd01b882011-10-11 16:47:53 +11005753int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754{
NeilBrown2604b702006-01-06 00:20:36 -08005755 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005756 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005757 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758
NeilBrowna757e642005-04-16 15:26:42 -07005759 if (list_empty(&mddev->disks))
5760 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762
5763 if (mddev->pers)
5764 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005765 /* Cannot run until previous stop completes properly */
5766 if (mddev->sysfs_active)
5767 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005768
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 /*
5770 * Analyze all RAID superblock(s)
5771 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005772 if (!mddev->raid_disks) {
5773 if (!mddev->persistent)
5774 return -EINVAL;
Yufen Yu6a5cb532019-10-16 16:00:03 +08005775 err = analyze_sbs(mddev);
5776 if (err)
5777 return -EINVAL;
NeilBrown1ec4a932008-02-06 01:39:53 -08005778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779
NeilBrownd9d166c2006-01-06 00:20:51 -08005780 if (mddev->level != LEVEL_NONE)
5781 request_module("md-level-%d", mddev->level);
5782 else if (mddev->clevel[0])
5783 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
5785 /*
5786 * Drop all container device buffers, from now on
5787 * the only valid external interface is through the md
5788 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 */
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005790 mddev->has_superblocks = false;
NeilBrowndafb20f2012-03-19 12:46:39 +11005791 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005792 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 continue;
5794 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005795 invalidate_bdev(rdev->bdev);
NeilBrown97b20ef2017-04-13 08:53:48 +10005796 if (mddev->ro != 1 &&
5797 (bdev_read_only(rdev->bdev) ||
5798 bdev_read_only(rdev->meta_bdev))) {
5799 mddev->ro = 1;
5800 if (mddev->gendisk)
5801 set_disk_ro(mddev->gendisk, 1);
5802 }
NeilBrownf0d76d72007-07-17 04:06:12 -07005803
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005804 if (rdev->sb_page)
5805 mddev->has_superblocks = true;
5806
NeilBrownf0d76d72007-07-17 04:06:12 -07005807 /* perform some consistency tests on the device.
5808 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005809 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005810 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005811 if (rdev->meta_bdev) {
5812 /* Nothing to check */;
5813 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005814 if (mddev->dev_sectors &&
5815 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005816 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005817 pr_warn("md: %s: data overlaps metadata\n",
5818 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005819 return -EINVAL;
5820 }
5821 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005822 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005823 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005824 pr_warn("md: %s: metadata overlaps data\n",
5825 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005826 return -EINVAL;
5827 }
5828 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005829 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 }
5831
Kent Overstreetafeee512018-05-20 18:25:52 -04005832 if (!bioset_initialized(&mddev->bio_set)) {
5833 err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5834 if (err)
5835 return err;
Ming Lei10273172017-02-14 23:29:00 +08005836 }
Kent Overstreetafeee512018-05-20 18:25:52 -04005837 if (!bioset_initialized(&mddev->sync_set)) {
5838 err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5839 if (err)
Kent Overstreet28dec872018-06-07 20:52:54 -04005840 return err;
NeilBrown5a850712017-06-21 09:12:21 +10005841 }
NeilBrowna167f662010-10-26 18:31:13 +11005842
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005844 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005845 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005847 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005848 pr_warn("md: personality for level %d is not loaded!\n",
5849 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005850 else
NeilBrown9d487392016-11-02 14:16:49 +11005851 pr_warn("md: personality for level %s is not loaded!\n",
5852 mddev->clevel);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005853 err = -EINVAL;
5854 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005857 if (mddev->level != pers->level) {
5858 mddev->level = pers->level;
5859 mddev->new_level = pers->level;
5860 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005861 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862
NeilBrownf6705572006-03-27 01:18:11 -08005863 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005864 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005865 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005866 module_put(pers->owner);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005867 err = -EINVAL;
5868 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08005869 }
5870
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005871 if (pers->sync_request) {
5872 /* Warn if this is a potentially silly
5873 * configuration.
5874 */
5875 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005876 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005877 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005878
NeilBrowndafb20f2012-03-19 12:46:39 +11005879 rdev_for_each(rdev, mddev)
5880 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005881 if (rdev < rdev2 &&
5882 rdev->bdev->bd_contains ==
5883 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005884 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5885 mdname(mddev),
5886 bdevname(rdev->bdev,b),
5887 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005888 warned = 1;
5889 }
5890 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005891
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005892 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005893 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005894 }
5895
NeilBrown657390d2005-08-26 18:34:16 -07005896 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005897 /* may be over-ridden by personality */
5898 mddev->resync_max_sectors = mddev->dev_sectors;
5899
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005900 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
NeilBrown0f9552b52009-12-30 12:08:50 +11005902 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005903 mddev->ro = 2; /* read-only, but switch on first write */
5904
NeilBrown36d091f2014-12-15 12:56:58 +11005905 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005906 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005907 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005908 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005909 WARN_ONCE(!mddev->external_size,
5910 "%s: default size too small, but 'external_size' not in effect?\n",
5911 __func__);
5912 pr_warn("md: invalid array_size %llu > default size %llu\n",
5913 (unsigned long long)mddev->array_sectors / 2,
5914 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005915 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005916 }
NeilBrown36d091f2014-12-15 12:56:58 +11005917 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005918 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005919 struct bitmap *bitmap;
5920
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005921 bitmap = md_bitmap_create(mddev, -1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005922 if (IS_ERR(bitmap)) {
5923 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005924 pr_warn("%s: failed to create bitmap (%d)\n",
5925 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005926 } else
5927 mddev->bitmap = bitmap;
5928
NeilBrownb15c2e52006-01-06 00:20:16 -08005929 }
Guoqing Jiangd4945492019-06-14 17:10:39 +08005930 if (err)
5931 goto bitmap_abort;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005932
5933 if (mddev->bitmap_info.max_write_behind > 0) {
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01005934 bool create_pool = false;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005935
5936 rdev_for_each(rdev, mddev) {
5937 if (test_bit(WriteMostly, &rdev->flags) &&
Guoqing Jiang404659c2019-12-23 10:48:53 +01005938 rdev_init_serial(rdev))
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01005939 create_pool = true;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005940 }
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01005941 if (create_pool && mddev->serial_info_pool == NULL) {
Guoqing Jiang404659c2019-12-23 10:48:53 +01005942 mddev->serial_info_pool =
5943 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
5944 sizeof(struct serial_info));
5945 if (!mddev->serial_info_pool) {
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005946 err = -ENOMEM;
Guoqing Jiangd4945492019-06-14 17:10:39 +08005947 goto bitmap_abort;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005948 }
5949 }
5950 }
5951
NeilBrown5c675f82014-12-15 12:56:56 +11005952 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005953 bool nonrot = true;
5954
5955 rdev_for_each(rdev, mddev) {
5956 if (rdev->raid_disk >= 0 &&
5957 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5958 nonrot = false;
5959 break;
5960 }
5961 }
5962 if (mddev->degraded)
5963 nonrot = false;
5964 if (nonrot)
Bart Van Assche8b904b52018-03-07 17:10:10 -08005965 blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue);
Shaohua Libb086a82016-09-30 09:45:40 -07005966 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08005967 blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01005968 mddev->queue->backing_dev_info->congested_data = mddev;
5969 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11005970 }
NeilBrown36d091f2014-12-15 12:56:58 +11005971 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005972 if (mddev->kobj.sd &&
5973 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005974 pr_warn("md: cannot register extra attributes for %s\n",
5975 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005976 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005977 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005978 mddev->ro = 0;
5979
Robert Becker1e509152009-12-14 12:49:58 +11005980 atomic_set(&mddev->max_corr_read_errors,
5981 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005983 if (mddev_is_clustered(mddev))
5984 mddev->safemode_delay = 0;
5985 else
5986 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005988 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005989 spin_lock(&mddev->lock);
5990 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005991 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005992 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005993 if (rdev->raid_disk >= 0)
Yufen Yue5b521e2019-06-14 15:41:07 -07005994 sysfs_link_rdev(mddev, rdev); /* failure here is OK */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005995
NeilBrowna4a3d262015-07-17 11:57:30 +10005996 if (mddev->degraded && !mddev->ro)
5997 /* This ensures that recovering status is reported immediately
5998 * via sysfs - until a lack of spares is confirmed.
5999 */
6000 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10006002
Shaohua Li29530792016-12-08 15:48:19 -08006003 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07006004 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
NeilBrownd7603b72006-01-06 00:20:30 -08006006 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 return 0;
Xiao Nib1261942018-01-24 12:17:38 +08006008
Guoqing Jiangd4945492019-06-14 17:10:39 +08006009bitmap_abort:
6010 mddev_detach(mddev);
6011 if (mddev->private)
6012 pers->free(mddev, mddev->private);
6013 mddev->private = NULL;
6014 module_put(pers->owner);
6015 md_bitmap_destroy(mddev);
Xiao Nib1261942018-01-24 12:17:38 +08006016abort:
NeilBrown4bc034d2019-03-29 10:46:16 -07006017 bioset_exit(&mddev->bio_set);
6018 bioset_exit(&mddev->sync_set);
Xiao Nib1261942018-01-24 12:17:38 +08006019 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020}
NeilBrown390ee602010-06-01 19:37:27 +10006021EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
NeilBrownfd01b882011-10-11 16:47:53 +11006023static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11006024{
6025 int err;
6026
NeilBrown9d4b45d2019-08-20 10:21:09 +10006027 set_bit(MD_NOT_READY, &mddev->flags);
NeilBrownfe60b012010-03-29 11:10:42 +11006028 err = md_run(mddev);
6029 if (err)
6030 goto out;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006031 err = md_bitmap_load(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006032 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006033 md_bitmap_destroy(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006034 goto out;
6035 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05006036
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006037 if (mddev_is_clustered(mddev))
6038 md_allow_write(mddev);
6039
Song Liud5d885f2017-11-19 22:17:01 -08006040 /* run start up tasks that require md_thread */
6041 md_start(mddev);
6042
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05006043 md_wakeup_thread(mddev->thread);
6044 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
6045
NeilBrownfe60b012010-03-29 11:10:42 +11006046 set_capacity(mddev->gendisk, mddev->array_sectors);
6047 revalidate_disk(mddev->gendisk);
NeilBrown9d4b45d2019-08-20 10:21:09 +10006048 clear_bit(MD_NOT_READY, &mddev->flags);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006049 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11006050 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrown9d4b45d2019-08-20 10:21:09 +10006051 sysfs_notify_dirent_safe(mddev->sysfs_state);
6052 sysfs_notify_dirent_safe(mddev->sysfs_action);
6053 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownfe60b012010-03-29 11:10:42 +11006054out:
NeilBrown9d4b45d2019-08-20 10:21:09 +10006055 clear_bit(MD_NOT_READY, &mddev->flags);
NeilBrownfe60b012010-03-29 11:10:42 +11006056 return err;
6057}
6058
Song Liud5d885f2017-11-19 22:17:01 -08006059int md_start(struct mddev *mddev)
6060{
6061 int ret = 0;
6062
6063 if (mddev->pers->start) {
6064 set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6065 md_wakeup_thread(mddev->thread);
6066 ret = mddev->pers->start(mddev);
6067 clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6068 md_wakeup_thread(mddev->sync_thread);
6069 }
6070 return ret;
6071}
6072EXPORT_SYMBOL_GPL(md_start);
6073
NeilBrownfd01b882011-10-11 16:47:53 +11006074static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075{
6076 struct gendisk *disk = mddev->gendisk;
NeilBrown97b20ef2017-04-13 08:53:48 +10006077 struct md_rdev *rdev;
6078 bool has_journal = false;
6079 bool has_readonly = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
Andre Noll80fab1d2008-07-11 22:02:21 +10006081 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10006083 return -ENXIO;
6084 if (!mddev->pers)
6085 return -EINVAL;
6086 if (!mddev->ro)
6087 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07006088
NeilBrown97b20ef2017-04-13 08:53:48 +10006089 rcu_read_lock();
6090 rdev_for_each_rcu(rdev, mddev) {
6091 if (test_bit(Journal, &rdev->flags) &&
6092 !test_bit(Faulty, &rdev->flags))
6093 has_journal = true;
6094 if (bdev_read_only(rdev->bdev))
6095 has_readonly = true;
Song Liu339421d2015-10-08 21:54:13 -07006096 }
NeilBrown97b20ef2017-04-13 08:53:48 +10006097 rcu_read_unlock();
6098 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
6099 /* Don't restart rw with journal missing/faulty */
6100 return -EINVAL;
6101 if (has_readonly)
6102 return -EROFS;
Song Liu339421d2015-10-08 21:54:13 -07006103
Andre Noll80fab1d2008-07-11 22:02:21 +10006104 mddev->safemode = 0;
6105 mddev->ro = 0;
6106 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11006107 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10006108 /* Kick recovery or resync if necessary */
6109 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6110 md_wakeup_thread(mddev->thread);
6111 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006112 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10006113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114}
6115
NeilBrownfd01b882011-10-11 16:47:53 +11006116static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11006117{
6118 mddev->array_sectors = 0;
6119 mddev->external_size = 0;
6120 mddev->dev_sectors = 0;
6121 mddev->raid_disks = 0;
6122 mddev->recovery_cp = 0;
6123 mddev->resync_min = 0;
6124 mddev->resync_max = MaxSector;
6125 mddev->reshape_position = MaxSector;
6126 mddev->external = 0;
6127 mddev->persistent = 0;
6128 mddev->level = LEVEL_NONE;
6129 mddev->clevel[0] = 0;
6130 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006131 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006132 mddev->ro = 0;
6133 mddev->metadata_type[0] = 0;
6134 mddev->chunk_sectors = 0;
6135 mddev->ctime = mddev->utime = 0;
6136 mddev->layout = 0;
6137 mddev->max_disks = 0;
6138 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10006139 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006140 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006141 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006142 mddev->new_level = LEVEL_NONE;
6143 mddev->new_layout = 0;
6144 mddev->new_chunk_sectors = 0;
6145 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11006146 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11006147 mddev->suspend_lo = mddev->suspend_hi = 0;
6148 mddev->sync_speed_min = mddev->sync_speed_max = 0;
6149 mddev->recovery = 0;
6150 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006151 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006152 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006153 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10006154 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08006155 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11006156 mddev->bitmap_info.offset = 0;
6157 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10006158 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006159 mddev->bitmap_info.chunksize = 0;
6160 mddev->bitmap_info.daemon_sleep = 0;
6161 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08006162 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006163}
6164
NeilBrownfd01b882011-10-11 16:47:53 +11006165static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11006166{
NeilBrown6b6204e2013-05-09 09:48:30 +10006167 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Guoqing Jiang21e09582020-04-04 23:57:07 +02006168 if (work_pending(&mddev->del_work))
6169 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11006170 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11006171 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10006172 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11006173 }
6174
6175 del_timer_sync(&mddev->safemode_timer);
6176
Shaohua Li034e33f2016-11-21 10:29:19 -08006177 if (mddev->pers && mddev->pers->quiesce) {
6178 mddev->pers->quiesce(mddev, 1);
6179 mddev->pers->quiesce(mddev, 0);
6180 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006181 md_bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11006182
NeilBrownb6d428c2013-04-24 11:42:42 +10006183 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006184 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08006185 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11006186 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006187 if (!mddev_is_clustered(mddev))
6188 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11006189 md_update_sb(mddev, 1);
6190 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +01006191 /* disable policy to guarantee rdevs free resources for serialization */
6192 mddev->serialize_policy = 0;
6193 mddev_destroy_serial_pool(mddev, NULL, true);
NeilBrowna047e122010-03-29 12:07:53 +11006194}
NeilBrowndefad612011-01-14 09:14:33 +11006195
NeilBrownfd01b882011-10-11 16:47:53 +11006196void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11006197{
NeilBrown29f097c2013-11-14 17:54:51 +11006198 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11006199 __md_stop_writes(mddev);
6200 mddev_unlock(mddev);
6201}
NeilBrown390ee602010-06-01 19:37:27 +10006202EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11006203
NeilBrown5aa61f42014-12-15 12:56:57 +11006204static void mddev_detach(struct mddev *mddev)
6205{
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006206 md_bitmap_wait_behind_writes(mddev);
Guoqing Jiang6b40bec2020-02-11 11:10:04 +01006207 if (mddev->pers && mddev->pers->quiesce && !mddev->suspended) {
NeilBrown5aa61f42014-12-15 12:56:57 +11006208 mddev->pers->quiesce(mddev, 1);
6209 mddev->pers->quiesce(mddev, 0);
6210 }
6211 md_unregister_thread(&mddev->thread);
6212 if (mddev->queue)
6213 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
6214}
6215
NeilBrown5eff3c42012-11-19 10:47:48 +11006216static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11006217{
NeilBrown36d091f2014-12-15 12:56:58 +11006218 struct md_personality *pers = mddev->pers;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006219 md_bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11006220 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10006221 /* Ensure ->event_work is done */
Guoqing Jiang21e09582020-04-04 23:57:07 +02006222 if (mddev->event_work.func)
6223 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11006224 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11006225 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11006226 spin_unlock(&mddev->lock);
6227 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10006228 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11006229 if (pers->sync_request && mddev->to_remove == NULL)
6230 mddev->to_remove = &md_redundancy_group;
6231 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11006232 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Jack Wang6aaa58c2018-10-19 16:21:31 +02006233}
6234
6235void md_stop(struct mddev *mddev)
6236{
6237 /* stop the array and free an attached data structures.
6238 * This is called from dm-raid
6239 */
6240 __md_stop(mddev);
Kent Overstreetafeee512018-05-20 18:25:52 -04006241 bioset_exit(&mddev->bio_set);
6242 bioset_exit(&mddev->sync_set);
NeilBrown5eff3c42012-11-19 10:47:48 +11006243}
6244
NeilBrown390ee602010-06-01 19:37:27 +10006245EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11006246
NeilBrowna05b7ea2012-07-19 15:59:18 +10006247static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11006248{
6249 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11006250 int did_freeze = 0;
6251
6252 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6253 did_freeze = 1;
6254 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6255 md_wakeup_thread(mddev->thread);
6256 }
NeilBrownf851b602014-12-11 10:02:10 +11006257 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11006258 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006259 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11006260 /* Thread might be blocked waiting for metadata update
6261 * which will now never happen */
6262 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11006263
Shaohua Li29530792016-12-08 15:48:19 -08006264 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10006265 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11006266 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006267 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
6268 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10006269 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08006270 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11006271 mddev_lock_nointr(mddev);
6272
NeilBrowna4bd82d2010-03-29 13:23:10 +11006273 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006274 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006275 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006276 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006277 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11006278 if (did_freeze) {
6279 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006280 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006281 md_wakeup_thread(mddev->thread);
6282 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11006283 err = -EBUSY;
6284 goto out;
6285 }
6286 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11006287 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11006288
6289 err = -ENXIO;
6290 if (mddev->ro==1)
6291 goto out;
6292 mddev->ro = 1;
6293 set_disk_ro(mddev->gendisk, 1);
6294 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006295 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6296 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006297 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11006298 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006299 }
6300out:
6301 mutex_unlock(&mddev->open_mutex);
6302 return err;
6303}
6304
NeilBrown9e653b62006-06-26 00:27:58 -07006305/* mode:
6306 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07006307 * 2 - stop but do not disassemble array
6308 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006309static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10006310 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11006313 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11006314 int did_freeze = 0;
6315
6316 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6317 did_freeze = 1;
6318 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6319 md_wakeup_thread(mddev->thread);
6320 }
NeilBrownf851b602014-12-11 10:02:10 +11006321 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11006322 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006323 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11006324 /* Thread might be blocked waiting for metadata update
6325 * which will now never happen */
6326 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11006327
NeilBrown30b8feb2013-11-14 15:16:17 +11006328 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006329 wait_event(resync_wait, (mddev->sync_thread == NULL &&
6330 !test_bit(MD_RECOVERY_RUNNING,
6331 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11006332 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333
NeilBrownc8c00a62009-08-10 12:50:52 +10006334 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006335 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006336 mddev->sysfs_active ||
6337 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006338 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006339 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10006340 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11006341 if (did_freeze) {
6342 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006343 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006344 md_wakeup_thread(mddev->thread);
6345 }
NeilBrown260fa032013-08-27 16:44:13 +10006346 return -EBUSY;
6347 }
NeilBrown6e17b022010-08-07 21:41:19 +10006348 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11006349 if (mddev->ro)
6350 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11006351
NeilBrowndefad612011-01-14 09:14:33 +11006352 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11006353 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01006354 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11006355
NeilBrowna4bd82d2010-03-29 13:23:10 +11006356 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10006357 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08006358
NeilBrowndafb20f2012-03-19 12:46:39 +11006359 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10006360 if (rdev->raid_disk >= 0)
6361 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10006362
NeilBrowna4bd82d2010-03-29 13:23:10 +11006363 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10006364 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006365 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006366 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08006367
NeilBrowna4bd82d2010-03-29 13:23:10 +11006368 if (mddev->ro)
6369 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10006370 } else
6371 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 /*
6373 * Free resources if final stop
6374 */
NeilBrown9e653b62006-06-26 00:27:58 -07006375 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006376 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
NeilBrownc3d97142009-12-14 12:49:52 +11006378 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11006379 struct file *f = mddev->bitmap_info.file;
6380 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11006381 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11006382 spin_unlock(&mddev->lock);
6383 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08006384 }
NeilBrownc3d97142009-12-14 12:49:52 +11006385 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08006386
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 export_array(mddev);
6388
NeilBrown6177b472010-03-29 11:37:13 +11006389 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11006390 if (mddev->hold_active == UNTIL_STOP)
6391 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006392 }
NeilBrownd7603b72006-01-06 00:20:30 -08006393 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006394 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10006395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396}
6397
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006398#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11006399static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400{
NeilBrown3cb03002011-10-11 16:45:26 +11006401 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 int err;
6403
NeilBrowna757e642005-04-16 15:26:42 -07006404 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406
NeilBrown9d487392016-11-02 14:16:49 +11006407 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408
NeilBrowndafb20f2012-03-19 12:46:39 +11006409 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11006411 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 }
NeilBrown9d487392016-11-02 14:16:49 +11006413 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414
NeilBrownd710e132008-10-13 11:55:12 +11006415 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006417 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10006418 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 }
6420}
6421
6422/*
6423 * lets try to run arrays based on all disks that have arrived
6424 * until now. (those are in pending_raid_disks)
6425 *
6426 * the method: pick the first pending disk, collect all disks with
6427 * the same UUID, remove all from the pending list and put them into
6428 * the 'same_array' list. Then order this list based on superblock
6429 * update time (freshest comes first), kick out 'old' disks and
6430 * compare superblocks. If everything's fine then run it.
6431 *
6432 * If "unit" is allocated, then bump its reference count
6433 */
6434static void autorun_devices(int part)
6435{
NeilBrown3cb03002011-10-11 16:45:26 +11006436 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006437 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438 char b[BDEVNAME_SIZE];
6439
NeilBrown9d487392016-11-02 14:16:49 +11006440 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07006442 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08006444 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11006446 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447
NeilBrown9d487392016-11-02 14:16:49 +11006448 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006450 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006452 pr_debug("md: adding %s ...\n",
6453 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 list_move(&rdev->same_set, &candidates);
6455 }
6456 /*
6457 * now we have a set of devices, with all of them having
6458 * mostly sane superblocks. It's time to allocate the
6459 * mddev.
6460 */
NeilBrowne8703fe2006-10-03 01:15:59 -07006461 if (part) {
6462 dev = MKDEV(mdp_major,
6463 rdev0->preferred_minor << MdpMinorShift);
6464 unit = MINOR(dev) >> MdpMinorShift;
6465 } else {
6466 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6467 unit = MINOR(dev);
6468 }
6469 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11006470 pr_warn("md: unit number in %s is bad: %d\n",
6471 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 break;
6473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474
6475 md_probe(dev, NULL, NULL);
6476 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006477 if (!mddev || !mddev->gendisk) {
6478 if (mddev)
6479 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 break;
6481 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006482 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006483 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 else if (mddev->raid_disks || mddev->major_version
6485 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006486 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487 mdname(mddev), bdevname(rdev0->bdev,b));
6488 mddev_unlock(mddev);
6489 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006490 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006491 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006492 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493 list_del_init(&rdev->same_set);
6494 if (bind_rdev_to_array(rdev, mddev))
6495 export_rdev(rdev);
6496 }
6497 autorun_array(mddev);
6498 mddev_unlock(mddev);
6499 }
6500 /* on success, candidates will be empty, on error
6501 * it won't...
6502 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006503 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006504 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 mddev_put(mddev);
6508 }
NeilBrown9d487392016-11-02 14:16:49 +11006509 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006511#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512
NeilBrownf72ffdd2014-09-30 14:23:59 +10006513static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514{
6515 mdu_version_t ver;
6516
6517 ver.major = MD_MAJOR_VERSION;
6518 ver.minor = MD_MINOR_VERSION;
6519 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6520
6521 if (copy_to_user(arg, &ver, sizeof(ver)))
6522 return -EFAULT;
6523
6524 return 0;
6525}
6526
NeilBrownf72ffdd2014-09-30 14:23:59 +10006527static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528{
6529 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006530 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006531 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532
NeilBrown1ca69c42012-10-11 13:37:33 +11006533 nr = working = insync = failed = spare = 0;
6534 rcu_read_lock();
6535 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006537 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538 failed++;
6539 else {
6540 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006541 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006542 insync++;
Song Liub347af82016-08-11 17:14:45 -07006543 else if (test_bit(Journal, &rdev->flags))
6544 /* TODO: add journal count to md_u.h */
6545 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546 else
6547 spare++;
6548 }
6549 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006550 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551
6552 info.major_version = mddev->major_version;
6553 info.minor_version = mddev->minor_version;
6554 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006555 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006557 info.size = mddev->dev_sectors / 2;
6558 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006559 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 info.nr_disks = nr;
6561 info.raid_disks = mddev->raid_disks;
6562 info.md_minor = mddev->md_minor;
6563 info.not_persistent= !mddev->persistent;
6564
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006565 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566 info.state = 0;
6567 if (mddev->in_sync)
6568 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006569 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006570 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006571 if (mddev_is_clustered(mddev))
6572 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006573 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574 info.working_disks = working;
6575 info.failed_disks = failed;
6576 info.spare_disks = spare;
6577
6578 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006579 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580
6581 if (copy_to_user(arg, &info, sizeof(info)))
6582 return -EFAULT;
6583
6584 return 0;
6585}
6586
NeilBrownf72ffdd2014-09-30 14:23:59 +10006587static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006588{
6589 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006590 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006591 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006592
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006593 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006594 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006595 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006596
NeilBrown32a76272005-06-21 17:17:14 -07006597 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006598 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006599 /* bitmap enabled */
6600 if (mddev->bitmap_info.file) {
6601 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6602 sizeof(file->pathname));
6603 if (IS_ERR(ptr))
6604 err = PTR_ERR(ptr);
6605 else
6606 memmove(file->pathname, ptr,
6607 sizeof(file->pathname)-(ptr-file->pathname));
6608 }
NeilBrown4af1a042014-12-15 12:57:00 +11006609 spin_unlock(&mddev->lock);
6610
6611 if (err == 0 &&
6612 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006613 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006614
NeilBrown32a76272005-06-21 17:17:14 -07006615 kfree(file);
6616 return err;
6617}
6618
NeilBrownf72ffdd2014-09-30 14:23:59 +10006619static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620{
6621 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006622 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623
6624 if (copy_from_user(&info, arg, sizeof(info)))
6625 return -EFAULT;
6626
NeilBrown1ca69c42012-10-11 13:37:33 +11006627 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006628 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629 if (rdev) {
6630 info.major = MAJOR(rdev->bdev->bd_dev);
6631 info.minor = MINOR(rdev->bdev->bd_dev);
6632 info.raid_disk = rdev->raid_disk;
6633 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006634 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006636 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 info.state |= (1<<MD_DISK_ACTIVE);
6638 info.state |= (1<<MD_DISK_SYNC);
6639 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006640 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006641 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006642 if (test_bit(WriteMostly, &rdev->flags))
6643 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006644 if (test_bit(FailFast, &rdev->flags))
6645 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646 } else {
6647 info.major = info.minor = 0;
6648 info.raid_disk = -1;
6649 info.state = (1<<MD_DISK_REMOVED);
6650 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006651 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652
6653 if (copy_to_user(arg, &info, sizeof(info)))
6654 return -EFAULT;
6655
6656 return 0;
6657}
6658
NeilBrownf72ffdd2014-09-30 14:23:59 +10006659static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660{
6661 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006662 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 dev_t dev = MKDEV(info->major,info->minor);
6664
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006665 if (mddev_is_clustered(mddev) &&
6666 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006667 pr_warn("%s: Cannot add to clustered mddev.\n",
6668 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006669 return -EINVAL;
6670 }
6671
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6673 return -EOVERFLOW;
6674
6675 if (!mddev->raid_disks) {
6676 int err;
6677 /* expecting a device which has a superblock */
6678 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6679 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006680 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681 PTR_ERR(rdev));
6682 return PTR_ERR(rdev);
6683 }
6684 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006685 struct md_rdev *rdev0
6686 = list_entry(mddev->disks.next,
6687 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006688 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689 .load_super(rdev, rdev0, mddev->minor_version);
6690 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006691 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006692 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 bdevname(rdev0->bdev,b2));
6694 export_rdev(rdev);
6695 return -EINVAL;
6696 }
6697 }
6698 err = bind_rdev_to_array(rdev, mddev);
6699 if (err)
6700 export_rdev(rdev);
6701 return err;
6702 }
6703
6704 /*
6705 * add_new_disk can be used once the array is assembled
6706 * to add "hot spares". They must already have a superblock
6707 * written
6708 */
6709 if (mddev->pers) {
6710 int err;
6711 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006712 pr_warn("%s: personality does not support diskops!\n",
6713 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 return -EINVAL;
6715 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006716 if (mddev->persistent)
6717 rdev = md_import_device(dev, mddev->major_version,
6718 mddev->minor_version);
6719 else
6720 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006722 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 PTR_ERR(rdev));
6724 return PTR_ERR(rdev);
6725 }
NeilBrown1a855a02010-12-09 16:36:28 +11006726 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006727 if (!mddev->persistent) {
6728 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006729 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006730 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006731 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006732 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006733 } else
NeilBrown41158c72005-06-21 17:17:25 -07006734 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006735 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006736 } else
6737 super_types[mddev->major_version].
6738 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006739 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006740 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006741 /* This was a hot-add request, but events doesn't
6742 * match, so reject it.
6743 */
6744 export_rdev(rdev);
6745 return -EINVAL;
6746 }
6747
NeilBrownb2d444d2005-11-08 21:39:31 -08006748 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006749 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6750 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006751 else
6752 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006753 if (info->state & (1<<MD_DISK_FAILFAST))
6754 set_bit(FailFast, &rdev->flags);
6755 else
6756 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006757
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006758 if (info->state & (1<<MD_DISK_JOURNAL)) {
6759 struct md_rdev *rdev2;
6760 bool has_journal = false;
6761
6762 /* make sure no existing journal disk */
6763 rdev_for_each(rdev2, mddev) {
6764 if (test_bit(Journal, &rdev2->flags)) {
6765 has_journal = true;
6766 break;
6767 }
6768 }
NeilBrown230b55f2017-10-17 14:24:09 +11006769 if (has_journal || mddev->bitmap) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006770 export_rdev(rdev);
6771 return -EBUSY;
6772 }
Song Liubac624f2015-08-13 14:31:55 -07006773 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006774 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006775 /*
6776 * check whether the device shows up in other nodes
6777 */
6778 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006779 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006780 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006781 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006782 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006783 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006784 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006785 export_rdev(rdev);
6786 return err;
6787 }
6788 }
6789 }
6790
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 rdev->raid_disk = -1;
6792 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006793
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794 if (err)
6795 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006796
6797 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006798 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6799 if (!err) {
6800 err = md_cluster_ops->new_disk_ack(mddev,
6801 err == 0);
6802 if (err)
6803 md_kick_rdev_from_array(rdev);
6804 }
6805 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006806 if (err)
6807 md_cluster_ops->add_new_disk_cancel(mddev);
6808 else
6809 err = add_bound_rdev(rdev);
6810 }
6811
6812 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006813 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006814
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815 return err;
6816 }
6817
6818 /* otherwise, add_new_disk is only allowed
6819 * for major_version==0 superblocks
6820 */
6821 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006822 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 return -EINVAL;
6824 }
6825
6826 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6827 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006828 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006830 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 PTR_ERR(rdev));
6832 return PTR_ERR(rdev);
6833 }
6834 rdev->desc_nr = info->number;
6835 if (info->raid_disk < mddev->raid_disks)
6836 rdev->raid_disk = info->raid_disk;
6837 else
6838 rdev->raid_disk = -1;
6839
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006841 if (info->state & (1<<MD_DISK_SYNC))
6842 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006844 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6845 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006846 if (info->state & (1<<MD_DISK_FAILFAST))
6847 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006848
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006850 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006851 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6852 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006853 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006854 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855
NeilBrown2bf071b2006-01-06 00:20:55 -08006856 err = bind_rdev_to_array(rdev, mddev);
6857 if (err) {
6858 export_rdev(rdev);
6859 return err;
6860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861 }
6862
6863 return 0;
6864}
6865
NeilBrownf72ffdd2014-09-30 14:23:59 +10006866static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867{
6868 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006869 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870
Yufen Yuc42a0e22018-05-04 18:08:10 +08006871 if (!mddev->pers)
6872 return -ENODEV;
6873
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874 rdev = find_rdev(mddev, dev);
6875 if (!rdev)
6876 return -ENXIO;
6877
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006878 if (rdev->raid_disk < 0)
6879 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006880
NeilBrown3ea8929d2013-04-24 11:42:41 +10006881 clear_bit(Blocked, &rdev->flags);
6882 remove_and_add_spares(mddev, rdev);
6883
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 if (rdev->raid_disk >= 0)
6885 goto busy;
6886
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006887kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006888 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006889 md_cluster_ops->remove_disk(mddev, rdev);
6890
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006891 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006892 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006893 if (mddev->thread)
6894 md_wakeup_thread(mddev->thread);
6895 else
6896 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006897 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898
6899 return 0;
6900busy:
NeilBrown9d487392016-11-02 14:16:49 +11006901 pr_debug("md: cannot remove active disk %s from %s ...\n",
6902 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 return -EBUSY;
6904}
6905
NeilBrownf72ffdd2014-09-30 14:23:59 +10006906static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907{
6908 char b[BDEVNAME_SIZE];
6909 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006910 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911
6912 if (!mddev->pers)
6913 return -ENODEV;
6914
6915 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006916 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917 mdname(mddev));
6918 return -EINVAL;
6919 }
6920 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006921 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006922 mdname(mddev));
6923 return -EINVAL;
6924 }
6925
NeilBrownd710e132008-10-13 11:55:12 +11006926 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006928 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 PTR_ERR(rdev));
6930 return -EINVAL;
6931 }
6932
6933 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006934 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006936 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937
NeilBrown8190e752009-06-18 08:48:58 +10006938 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939
NeilBrownb2d444d2005-11-08 21:39:31 -08006940 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006941 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942 bdevname(rdev->bdev,b), mdname(mddev));
6943 err = -EINVAL;
6944 goto abort_export;
6945 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006946
NeilBrownb2d444d2005-11-08 21:39:31 -08006947 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006949 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006950 err = bind_rdev_to_array(rdev, mddev);
6951 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006952 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953
6954 /*
6955 * The rest should better be atomic, we can have disk failures
6956 * noticed in interrupt contexts ...
6957 */
6958
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 rdev->raid_disk = -1;
6960
Shaohua Li29530792016-12-08 15:48:19 -08006961 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006962 if (!mddev->thread)
6963 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964 /*
6965 * Kick recovery, maybe this spare has to be added to the
6966 * array immediately.
6967 */
6968 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6969 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006970 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971 return 0;
6972
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973abort_export:
6974 export_rdev(rdev);
6975 return err;
6976}
6977
NeilBrownfd01b882011-10-11 16:47:53 +11006978static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006979{
NeilBrown035328c2014-04-09 12:25:40 +10006980 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006981
NeilBrown36fa3062005-09-09 16:23:45 -07006982 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006983 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006984 return -EBUSY;
6985 if (mddev->recovery || mddev->sync_thread)
6986 return -EBUSY;
6987 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006988 }
6989
NeilBrown36fa3062005-09-09 16:23:45 -07006990 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006991 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006992 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006993
NeilBrown1e594bb2014-12-15 12:57:00 +11006994 if (mddev->bitmap || mddev->bitmap_info.file)
6995 return -EEXIST; /* cannot add when bitmap is present */
6996 f = fget(fd);
6997
6998 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006999 pr_warn("%s: error: failed to get bitmap file\n",
7000 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07007001 return -EBADF;
7002 }
7003
NeilBrown1e594bb2014-12-15 12:57:00 +11007004 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10007005 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11007006 pr_warn("%s: error: bitmap file must be a regular file\n",
7007 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10007008 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11007009 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11007010 pr_warn("%s: error: bitmap file must open for write\n",
7011 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10007012 err = -EBADF;
7013 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11007014 pr_warn("%s: error: bitmap file is already in use\n",
7015 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10007016 err = -EBUSY;
7017 }
7018 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11007019 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07007020 return err;
7021 }
NeilBrown1e594bb2014-12-15 12:57:00 +11007022 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11007023 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07007024 } else if (mddev->bitmap == NULL)
7025 return -ENOENT; /* cannot remove what isn't there */
7026 err = 0;
7027 if (mddev->pers) {
NeilBrown69e51b42010-06-01 19:37:35 +10007028 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007029 struct bitmap *bitmap;
7030
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007031 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007032 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007033 if (!IS_ERR(bitmap)) {
7034 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007035 err = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11007036 } else
7037 err = PTR_ERR(bitmap);
NeilBrown52a0d492017-10-17 13:46:43 +11007038 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007039 md_bitmap_destroy(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11007040 fd = -1;
7041 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007042 mddev_resume(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11007043 } else if (fd < 0) {
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007044 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007045 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007046 mddev_resume(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07007047 }
NeilBrownd7375ab2006-06-26 00:27:43 -07007048 }
7049 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11007050 struct file *f = mddev->bitmap_info.file;
7051 if (f) {
7052 spin_lock(&mddev->lock);
7053 mddev->bitmap_info.file = NULL;
7054 spin_unlock(&mddev->lock);
7055 fput(f);
7056 }
NeilBrown36fa3062005-09-09 16:23:45 -07007057 }
7058
NeilBrown32a76272005-06-21 17:17:14 -07007059 return err;
7060}
7061
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062/*
7063 * set_array_info is used two different ways
7064 * The original usage is when creating a new array.
7065 * In this usage, raid_disks is > 0 and it together with
7066 * level, size, not_persistent,layout,chunksize determine the
7067 * shape of the array.
7068 * This will always create an array with a type-0.90.0 superblock.
7069 * The newer usage is when assembling an array.
7070 * In this case raid_disks will be 0, and the major_version field is
7071 * use to determine which style super-blocks are to be found on the devices.
7072 * The minor and patch _version numbers are also kept incase the
7073 * super_block handler wishes to interpret them.
7074 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10007075static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076{
7077
7078 if (info->raid_disks == 0) {
7079 /* just setting version number for superblock loading */
7080 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07007081 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082 super_types[info->major_version].name == NULL) {
7083 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11007084 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 info->major_version);
7086 return -EINVAL;
7087 }
7088 mddev->major_version = info->major_version;
7089 mddev->minor_version = info->minor_version;
7090 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08007091 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11007092 /* ensure mddev_put doesn't delete this now that there
7093 * is some minimal configuration.
7094 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11007095 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 return 0;
7097 }
7098 mddev->major_version = MD_MAJOR_VERSION;
7099 mddev->minor_version = MD_MINOR_VERSION;
7100 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11007101 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102
7103 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08007104 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11007105 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106 mddev->raid_disks = info->raid_disks;
7107 /* don't set md_minor, it is determined by which /dev/md* was
7108 * openned
7109 */
7110 if (info->state & (1<<MD_SB_CLEAN))
7111 mddev->recovery_cp = MaxSector;
7112 else
7113 mddev->recovery_cp = 0;
7114 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08007115 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116
7117 mddev->layout = info->layout;
NeilBrown33f2c352019-09-09 16:52:29 +10007118 if (mddev->level == 0)
7119 /* Cannot trust RAID0 layout info here */
7120 mddev->layout = -1;
Andre Noll9d8f0362009-06-18 08:45:01 +10007121 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122
Shaohua Li29530792016-12-08 15:48:19 -08007123 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11007124 mddev->max_disks = MD_SB_DISKS;
7125 mddev->flags = 0;
7126 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08007127 }
7128 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129
NeilBrownc3d97142009-12-14 12:49:52 +11007130 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10007131 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11007132 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08007133
NeilBrownf6705572006-03-27 01:18:11 -08007134 mddev->reshape_position = MaxSector;
7135
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 /*
7137 * Generate a 128 bit UUID
7138 */
7139 get_random_bytes(mddev->uuid, 16);
7140
NeilBrownf6705572006-03-27 01:18:11 -08007141 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10007142 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08007143 mddev->new_layout = mddev->layout;
7144 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007145 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08007146
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 return 0;
7148}
7149
NeilBrownfd01b882011-10-11 16:47:53 +11007150void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11007151{
Shaohua Liefa4b772017-10-18 22:08:13 -07007152 lockdep_assert_held(&mddev->reconfig_mutex);
Dan Williamsb522adc2009-03-31 15:00:31 +11007153
7154 if (mddev->external_size)
7155 return;
7156
Dan Williams1f403622009-03-31 14:59:03 +11007157 mddev->array_sectors = array_sectors;
7158}
7159EXPORT_SYMBOL(md_set_array_sectors);
7160
NeilBrownfd01b882011-10-11 16:47:53 +11007161static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08007162{
NeilBrown3cb03002011-10-11 16:45:26 +11007163 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08007164 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10007165 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08007166 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04007167
NeilBrowna35b0d62006-01-06 00:20:49 -08007168 if (mddev->pers->resize == NULL)
7169 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10007170 /* The "num_sectors" is the number of sectors of each device that
7171 * is used. This can only make sense for arrays with redundancy.
7172 * linear and raid0 always use whatever space is available. We can only
7173 * consider changing this number if no resync or reconstruction is
7174 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10007175 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10007176 * of each device. If num_sectors is zero, we find the largest size
7177 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08007178 */
NeilBrownf851b602014-12-11 10:02:10 +11007179 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7180 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08007181 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10007182 if (mddev->ro)
7183 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10007184
NeilBrowndafb20f2012-03-19 12:46:39 +11007185 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11007186 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07007187
Andre Nolld71f9f82008-07-11 22:02:22 +10007188 if (fit && (num_sectors == 0 || num_sectors > avail))
7189 num_sectors = avail;
7190 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08007191 return -ENOSPC;
7192 }
Andre Nolld71f9f82008-07-11 22:02:22 +10007193 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08007194 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08007195 if (mddev_is_clustered(mddev))
7196 md_cluster_ops->update_size(mddev, old_dev_sectors);
7197 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08007198 set_capacity(mddev->gendisk, mddev->array_sectors);
7199 revalidate_disk(mddev->gendisk);
7200 }
7201 }
NeilBrowna35b0d62006-01-06 00:20:49 -08007202 return rv;
7203}
7204
NeilBrownfd01b882011-10-11 16:47:53 +11007205static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08007206{
7207 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10007208 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08007209 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08007210 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08007211 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10007212 if (mddev->ro)
7213 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08007214 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10007215 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08007216 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11007217 if (mddev->sync_thread ||
7218 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7219 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08007220 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10007221
7222 rdev_for_each(rdev, mddev) {
7223 if (mddev->raid_disks < raid_disks &&
7224 rdev->data_offset < rdev->new_data_offset)
7225 return -EINVAL;
7226 if (mddev->raid_disks > raid_disks &&
7227 rdev->data_offset > rdev->new_data_offset)
7228 return -EINVAL;
7229 }
7230
NeilBrown63c70c42006-03-27 01:18:13 -08007231 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10007232 if (mddev->delta_disks < 0)
7233 mddev->reshape_backwards = 1;
7234 else if (mddev->delta_disks > 0)
7235 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08007236
7237 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10007238 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11007239 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007240 mddev->reshape_backwards = 0;
7241 }
NeilBrownda943b992006-01-06 00:20:54 -08007242 return rv;
7243}
7244
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245/*
7246 * update_array_info is used to change the configuration of an
7247 * on-line array.
7248 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
7249 * fields in the info are checked against the array.
7250 * Any differences that cannot be handled will cause an error.
7251 * Normally, only one change can be managed at a time.
7252 */
NeilBrownfd01b882011-10-11 16:47:53 +11007253static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254{
7255 int rv = 0;
7256 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007257 int state = 0;
7258
7259 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11007260 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07007261 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262
7263 if (mddev->major_version != info->major_version ||
7264 mddev->minor_version != info->minor_version ||
7265/* mddev->patch_version != info->patch_version || */
7266 mddev->ctime != info->ctime ||
7267 mddev->level != info->level ||
7268/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08007269 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10007270 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07007271 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
7272 ((state^info->state) & 0xfffffe00)
7273 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274 return -EINVAL;
7275 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11007276 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
7277 cnt++;
7278 if (mddev->raid_disks != info->raid_disks)
7279 cnt++;
7280 if (mddev->layout != info->layout)
7281 cnt++;
7282 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
7283 cnt++;
7284 if (cnt == 0)
7285 return 0;
7286 if (cnt > 1)
7287 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288
7289 if (mddev->layout != info->layout) {
7290 /* Change layout
7291 * we don't need to do anything at the md level, the
7292 * personality will take care of it all.
7293 */
NeilBrown50ac1682009-06-18 08:47:55 +10007294 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007296 else {
7297 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10007298 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10007299 if (rv)
7300 mddev->new_layout = mddev->layout;
7301 return rv;
7302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007304 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10007305 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306
NeilBrownda943b992006-01-06 00:20:54 -08007307 if (mddev->raid_disks != info->raid_disks)
7308 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309
NeilBrown36fa3062005-09-09 16:23:45 -07007310 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007311 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
7312 rv = -EINVAL;
7313 goto err;
7314 }
7315 if (mddev->recovery || mddev->sync_thread) {
7316 rv = -EBUSY;
7317 goto err;
7318 }
NeilBrown36fa3062005-09-09 16:23:45 -07007319 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007320 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07007321 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007322 if (mddev->bitmap) {
7323 rv = -EEXIST;
7324 goto err;
7325 }
7326 if (mddev->bitmap_info.default_offset == 0) {
7327 rv = -EINVAL;
7328 goto err;
7329 }
NeilBrownc3d97142009-12-14 12:49:52 +11007330 mddev->bitmap_info.offset =
7331 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10007332 mddev->bitmap_info.space =
7333 mddev->bitmap_info.default_space;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007334 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007335 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007336 if (!IS_ERR(bitmap)) {
7337 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007338 rv = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11007339 } else
7340 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07007341 if (rv)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007342 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007343 mddev_resume(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07007344 } else {
7345 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007346 if (!mddev->bitmap) {
7347 rv = -ENOENT;
7348 goto err;
7349 }
7350 if (mddev->bitmap->storage.file) {
7351 rv = -EINVAL;
7352 goto err;
7353 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11007354 if (mddev->bitmap_info.nodes) {
7355 /* hold PW on all the bitmap lock */
7356 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11007357 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 +11007358 rv = -EPERM;
7359 md_cluster_ops->unlock_all_bitmaps(mddev);
7360 goto err;
7361 }
7362
7363 mddev->bitmap_info.nodes = 0;
7364 md_cluster_ops->leave(mddev);
7365 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007366 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007367 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007368 mddev_resume(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11007369 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007370 }
7371 }
NeilBrown850b2b422006-10-03 01:15:46 -07007372 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007373 return rv;
7374err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 return rv;
7376}
7377
NeilBrownfd01b882011-10-11 16:47:53 +11007378static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379{
NeilBrown3cb03002011-10-11 16:45:26 +11007380 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11007381 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382
7383 if (mddev->pers == NULL)
7384 return -ENODEV;
7385
NeilBrown1ca69c42012-10-11 13:37:33 +11007386 rcu_read_lock();
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01007387 rdev = md_find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11007389 err = -ENODEV;
7390 else {
7391 md_error(mddev, rdev);
7392 if (!test_bit(Faulty, &rdev->flags))
7393 err = -EBUSY;
7394 }
7395 rcu_read_unlock();
7396 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397}
7398
Andre Noll2f9618c2008-04-25 18:57:58 +02007399/*
7400 * We have a problem here : there is no easy way to give a CHS
7401 * virtual geometry. We currently pretend that we have a 2 heads
7402 * 4 sectors (with a BIG number of cylinders...). This drives
7403 * dosfs just mad... ;-)
7404 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007405static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7406{
NeilBrownfd01b882011-10-11 16:47:53 +11007407 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007408
7409 geo->heads = 2;
7410 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11007411 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007412 return 0;
7413}
7414
Nicolas Schichancb335f82014-01-15 16:58:52 +01007415static inline bool md_ioctl_valid(unsigned int cmd)
7416{
7417 switch (cmd) {
7418 case ADD_NEW_DISK:
7419 case BLKROSET:
7420 case GET_ARRAY_INFO:
7421 case GET_BITMAP_FILE:
7422 case GET_DISK_INFO:
7423 case HOT_ADD_DISK:
7424 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007425 case RAID_AUTORUN:
7426 case RAID_VERSION:
7427 case RESTART_ARRAY_RW:
7428 case RUN_ARRAY:
7429 case SET_ARRAY_INFO:
7430 case SET_BITMAP_FILE:
7431 case SET_DISK_FAULTY:
7432 case STOP_ARRAY:
7433 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007434 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007435 return true;
7436 default:
7437 return false;
7438 }
7439}
7440
Al Viroa39907f2008-03-02 10:31:15 -05007441static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007442 unsigned int cmd, unsigned long arg)
7443{
7444 int err = 0;
7445 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11007446 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10007447 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08007448 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449
Nicolas Schichancb335f82014-01-15 16:58:52 +01007450 if (!md_ioctl_valid(cmd))
7451 return -ENOTTY;
7452
NeilBrown506c9e42011-12-23 10:17:26 +11007453 switch (cmd) {
7454 case RAID_VERSION:
7455 case GET_ARRAY_INFO:
7456 case GET_DISK_INFO:
7457 break;
7458 default:
7459 if (!capable(CAP_SYS_ADMIN))
7460 return -EACCES;
7461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462
7463 /*
7464 * Commands dealing with the RAID driver but not any
7465 * particular array:
7466 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007467 switch (cmd) {
7468 case RAID_VERSION:
7469 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007470 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11007473 case RAID_AUTORUN:
7474 err = 0;
7475 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007476 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11007478 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479 }
7480
7481 /*
7482 * Commands creating/starting a new array:
7483 */
7484
Al Viroa39907f2008-03-02 10:31:15 -05007485 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007486
7487 if (!mddev) {
7488 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490 }
7491
NeilBrown1ca69c42012-10-11 13:37:33 +11007492 /* Some actions do not requires the mutex */
7493 switch (cmd) {
7494 case GET_ARRAY_INFO:
7495 if (!mddev->raid_disks && !mddev->external)
7496 err = -ENODEV;
7497 else
7498 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007499 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007500
7501 case GET_DISK_INFO:
7502 if (!mddev->raid_disks && !mddev->external)
7503 err = -ENODEV;
7504 else
7505 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007506 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007507
7508 case SET_DISK_FAULTY:
7509 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007510 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007511
7512 case GET_BITMAP_FILE:
7513 err = get_bitmap_file(mddev, argp);
7514 goto out;
7515
NeilBrown1ca69c42012-10-11 13:37:33 +11007516 }
7517
Guoqing Jiang78b990c2020-04-04 23:57:10 +02007518 if (cmd == ADD_NEW_DISK || cmd == HOT_ADD_DISK)
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02007519 flush_rdev_wq(mddev);
NeilBrowna7a3f082012-12-11 13:35:54 +11007520
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007521 if (cmd == HOT_REMOVE_DISK)
7522 /* need to ensure recovery thread has run */
7523 wait_event_interruptible_timeout(mddev->sb_wait,
7524 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007525 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007526 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007527 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7528 /* Need to flush page cache, and ensure no-one else opens
7529 * and writes
7530 */
7531 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007532 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007533 mutex_unlock(&mddev->open_mutex);
7534 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007535 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007536 }
NeilBrown065e5192017-04-06 11:16:33 +08007537 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007538 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007539 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007540 mutex_unlock(&mddev->open_mutex);
7541 sync_blockdev(bdev);
7542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007543 err = mddev_lock(mddev);
7544 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007545 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7546 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007547 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 }
7549
NeilBrownc02c0ae2012-12-11 13:39:21 +11007550 if (cmd == SET_ARRAY_INFO) {
7551 mdu_array_info_t info;
7552 if (!arg)
7553 memset(&info, 0, sizeof(info));
7554 else if (copy_from_user(&info, argp, sizeof(info))) {
7555 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007556 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007557 }
7558 if (mddev->pers) {
7559 err = update_array_info(mddev, &info);
7560 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007561 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007562 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007564 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007565 }
7566 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007567 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007568 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007569 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007570 }
7571 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007572 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007573 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007574 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007575 }
7576 err = set_array_info(mddev, &info);
7577 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007578 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007579 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007580 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007581 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582 }
7583
7584 /*
7585 * Commands querying/configuring an existing array:
7586 */
NeilBrown32a76272005-06-21 17:17:14 -07007587 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007588 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007589 if ((!mddev->raid_disks && !mddev->external)
7590 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7591 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7592 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007594 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595 }
7596
7597 /*
7598 * Commands even a read-only array can execute:
7599 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007600 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007601 case RESTART_ARRAY_RW:
7602 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007603 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007604
7605 case STOP_ARRAY:
7606 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007607 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007608
7609 case STOP_ARRAY_RO:
7610 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007611 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007612
NeilBrown3ea8929d2013-04-24 11:42:41 +10007613 case HOT_REMOVE_DISK:
7614 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007615 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007616
NeilBrown7ceb17e2013-04-24 11:42:42 +10007617 case ADD_NEW_DISK:
7618 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007619 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007620 * So require mddev->pers and MD_DISK_SYNC.
7621 */
7622 if (mddev->pers) {
7623 mdu_disk_info_t info;
7624 if (copy_from_user(&info, argp, sizeof(info)))
7625 err = -EFAULT;
7626 else if (!(info.state & (1<<MD_DISK_SYNC)))
7627 /* Need to clear read-only for this */
7628 break;
7629 else
7630 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007631 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007632 }
7633 break;
7634
NeilBrownc02c0ae2012-12-11 13:39:21 +11007635 case BLKROSET:
7636 if (get_user(ro, (int __user *)(arg))) {
7637 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007638 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007639 }
7640 err = -EINVAL;
7641
7642 /* if the bdev is going readonly the value of mddev->ro
7643 * does not matter, no writes are coming
7644 */
7645 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007646 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007647
NeilBrownc02c0ae2012-12-11 13:39:21 +11007648 /* are we are already prepared for writes? */
7649 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007650 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007651
7652 /* transitioning to readauto need only happen for
7653 * arrays that call md_write_start
7654 */
7655 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007657 if (err == 0) {
7658 mddev->ro = 2;
7659 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007660 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007661 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007662 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663 }
7664
7665 /*
7666 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007667 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668 */
NeilBrown326eb172014-09-30 15:36:28 +10007669 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007670 if (mddev->ro == 2) {
7671 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007672 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007673 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007674 /* mddev_unlock will wake thread */
7675 /* If a device failed while we were read-only, we
7676 * need to make sure the metadata is updated now.
7677 */
Shaohua Li29530792016-12-08 15:48:19 -08007678 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007679 mddev_unlock(mddev);
7680 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007681 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7682 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007683 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007684 }
NeilBrownf91de922005-11-08 21:39:36 -08007685 } else {
7686 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007687 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689 }
7690
NeilBrownc02c0ae2012-12-11 13:39:21 +11007691 switch (cmd) {
7692 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007694 mdu_disk_info_t info;
7695 if (copy_from_user(&info, argp, sizeof(info)))
7696 err = -EFAULT;
7697 else
7698 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007699 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007701
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007702 case CLUSTERED_DISK_NACK:
7703 if (mddev_is_clustered(mddev))
7704 md_cluster_ops->new_disk_ack(mddev, false);
7705 else
7706 err = -EINVAL;
7707 goto unlock;
7708
NeilBrownc02c0ae2012-12-11 13:39:21 +11007709 case HOT_ADD_DISK:
7710 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007711 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712
NeilBrownc02c0ae2012-12-11 13:39:21 +11007713 case RUN_ARRAY:
7714 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007715 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007716
NeilBrownc02c0ae2012-12-11 13:39:21 +11007717 case SET_BITMAP_FILE:
7718 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007719 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007720
NeilBrownc02c0ae2012-12-11 13:39:21 +11007721 default:
7722 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007723 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007724 }
7725
NeilBrown3adc28d2014-09-30 15:46:41 +10007726unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007727 if (mddev->hold_active == UNTIL_IOCTL &&
7728 err != -EINVAL)
7729 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007730 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007731out:
NeilBrown065e5192017-04-06 11:16:33 +08007732 if(did_set_md_closing)
7733 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007734 return err;
7735}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007736#ifdef CONFIG_COMPAT
7737static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7738 unsigned int cmd, unsigned long arg)
7739{
7740 switch (cmd) {
7741 case HOT_REMOVE_DISK:
7742 case HOT_ADD_DISK:
7743 case SET_DISK_FAULTY:
7744 case SET_BITMAP_FILE:
7745 /* These take in integer arg, do not convert */
7746 break;
7747 default:
7748 arg = (unsigned long)compat_ptr(arg);
7749 break;
7750 }
7751
7752 return md_ioctl(bdev, mode, cmd, arg);
7753}
7754#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755
Al Viroa39907f2008-03-02 10:31:15 -05007756static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757{
7758 /*
7759 * Succeed if we can lock the mddev, which confirms that
7760 * it isn't being stopped right now.
7761 */
NeilBrownfd01b882011-10-11 16:47:53 +11007762 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007763 int err;
7764
Yuanhan Liu0c098222012-05-22 13:55:32 +10007765 if (!mddev)
7766 return -ENODEV;
7767
NeilBrownd3374822009-01-09 08:31:10 +11007768 if (mddev->gendisk != bdev->bd_disk) {
7769 /* we are racing with mddev_put which is discarding this
7770 * bd_disk.
7771 */
7772 mddev_put(mddev);
7773 /* Wait until bdev->bd_disk is definitely gone */
Guoqing Jiangf6766ff2020-04-04 23:57:09 +02007774 if (work_pending(&mddev->del_work))
7775 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007776 /* Then retry the open from the top */
7777 return -ERESTARTSYS;
7778 }
7779 BUG_ON(mddev != bdev->bd_disk->private_data);
7780
NeilBrownc8c00a62009-08-10 12:50:52 +10007781 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007782 goto out;
7783
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007784 if (test_bit(MD_CLOSING, &mddev->flags)) {
7785 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007786 err = -ENODEV;
7787 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007788 }
7789
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007791 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007792 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007794 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007796 if (err)
7797 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007798 return err;
7799}
7800
Al Virodb2a1442013-05-05 21:52:57 -04007801static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007802{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007803 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007805 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007806 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007809
7810static int md_media_changed(struct gendisk *disk)
7811{
NeilBrownfd01b882011-10-11 16:47:53 +11007812 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007813
7814 return mddev->changed;
7815}
7816
7817static int md_revalidate(struct gendisk *disk)
7818{
NeilBrownfd01b882011-10-11 16:47:53 +11007819 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007820
7821 mddev->changed = 0;
7822 return 0;
7823}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007824static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825{
7826 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007827 .open = md_open,
7828 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007829 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007830#ifdef CONFIG_COMPAT
7831 .compat_ioctl = md_compat_ioctl,
7832#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007833 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007834 .media_changed = md_media_changed,
7835 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007836};
7837
NeilBrownf72ffdd2014-09-30 14:23:59 +10007838static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007839{
NeilBrown2b8bf342011-10-11 16:48:23 +11007840 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841
Linus Torvalds1da177e2005-04-16 15:20:36 -07007842 /*
7843 * md_thread is a 'system-thread', it's priority should be very
7844 * high. We avoid resource deadlocks individually in each
7845 * raid personality. (RAID5 does preallocation) We also use RR and
7846 * the very same RT priority as kswapd, thus we will never get
7847 * into a priority inversion deadlock.
7848 *
7849 * we definitely have to have equal or higher priority than
7850 * bdflush, otherwise bdflush will deadlock if there are too
7851 * many dirty RAID5 blocks.
7852 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853
NeilBrown6985c432005-10-19 21:23:47 -07007854 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007855 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007856
NeilBrown93588e22005-11-15 00:09:12 -08007857 /* We need to wait INTERRUPTIBLE so that
7858 * we don't add to the load-average.
7859 * That means we need to be sure no signals are
7860 * pending
7861 */
7862 if (signal_pending(current))
7863 flush_signals(current);
7864
7865 wait_event_interruptible_timeout
7866 (thread->wqueue,
7867 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007868 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007869 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870
NeilBrown6c987912011-01-14 09:13:53 +11007871 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007872 if (kthread_should_park())
7873 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007874 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007875 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007877
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878 return 0;
7879}
7880
NeilBrown2b8bf342011-10-11 16:48:23 +11007881void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882{
7883 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007884 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Guoqing Jiangd1d90142017-10-09 10:32:48 +08007885 set_bit(THREAD_WAKEUP, &thread->flags);
7886 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887 }
7888}
NeilBrown6c144d32014-09-30 16:15:38 +10007889EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890
Shaohua Li4ed87312012-10-11 13:34:00 +11007891struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7892 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893{
NeilBrown2b8bf342011-10-11 16:48:23 +11007894 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007895
NeilBrown2b8bf342011-10-11 16:48:23 +11007896 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007897 if (!thread)
7898 return NULL;
7899
Linus Torvalds1da177e2005-04-16 15:20:36 -07007900 init_waitqueue_head(&thread->wqueue);
7901
Linus Torvalds1da177e2005-04-16 15:20:36 -07007902 thread->run = run;
7903 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007904 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007905 thread->tsk = kthread_run(md_thread, thread,
7906 "%s_%s",
7907 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007908 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007909 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910 kfree(thread);
7911 return NULL;
7912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 return thread;
7914}
NeilBrown6c144d32014-09-30 16:15:38 +10007915EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916
NeilBrown2b8bf342011-10-11 16:48:23 +11007917void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007918{
NeilBrown2b8bf342011-10-11 16:48:23 +11007919 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007920 if (!thread)
7921 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007922 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007923 /* Locking ensures that mddev_unlock does not wake_up a
7924 * non-existent thread
7925 */
7926 spin_lock(&pers_lock);
7927 *threadp = NULL;
7928 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007929
7930 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931 kfree(thread);
7932}
NeilBrown6c144d32014-09-30 16:15:38 +10007933EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007934
NeilBrownfd01b882011-10-11 16:47:53 +11007935void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936{
NeilBrownb2d444d2005-11-08 21:39:31 -08007937 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007939
NeilBrownde393cd2011-07-28 11:31:48 +10007940 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941 return;
7942 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007943 if (mddev->degraded)
7944 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007945 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007946 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7947 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7948 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007949 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007950 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007951 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007952}
NeilBrown6c144d32014-09-30 16:15:38 +10007953EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007954
7955/* seq_file implementation /proc/mdstat */
7956
7957static void status_unused(struct seq_file *seq)
7958{
7959 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007960 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961
7962 seq_printf(seq, "unused devices: ");
7963
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007964 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965 char b[BDEVNAME_SIZE];
7966 i++;
7967 seq_printf(seq, "%s ",
7968 bdevname(rdev->bdev,b));
7969 }
7970 if (!i)
7971 seq_printf(seq, "<none>");
7972
7973 seq_printf(seq, "\n");
7974}
7975
NeilBrownf7851be2015-07-02 17:12:58 +10007976static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007977{
NeilBrowndd71cf62009-05-07 12:49:35 +10007978 sector_t max_sectors, resync, res;
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02007979 unsigned long dt, db = 0;
7980 sector_t rt, curr_mark_cnt, resync_mark_cnt;
7981 int scale, recovery_active;
NeilBrown4588b422006-03-27 01:18:04 -08007982 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007983
NeilBrownc804cde2012-05-21 09:28:33 +10007984 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7985 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007986 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007987 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007988 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989
NeilBrownf7851be2015-07-02 17:12:58 +10007990 resync = mddev->curr_resync;
7991 if (resync <= 3) {
7992 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7993 /* Still cleaning up */
7994 resync = max_sectors;
Nate Daileyd2e2ec82017-11-30 11:33:30 -05007995 } else if (resync > max_sectors)
7996 resync = max_sectors;
7997 else
NeilBrownf7851be2015-07-02 17:12:58 +10007998 resync -= atomic_read(&mddev->recovery_active);
7999
8000 if (resync == 0) {
Guoqing Jiang0357ba22018-07-02 16:26:25 +08008001 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
8002 struct md_rdev *rdev;
8003
8004 rdev_for_each(rdev, mddev)
8005 if (rdev->raid_disk >= 0 &&
8006 !test_bit(Faulty, &rdev->flags) &&
8007 rdev->recovery_offset != MaxSector &&
8008 rdev->recovery_offset) {
8009 seq_printf(seq, "\trecover=REMOTE");
8010 return 1;
8011 }
8012 if (mddev->reshape_position != MaxSector)
8013 seq_printf(seq, "\treshape=REMOTE");
8014 else
8015 seq_printf(seq, "\tresync=REMOTE");
8016 return 1;
8017 }
NeilBrownf7851be2015-07-02 17:12:58 +10008018 if (mddev->recovery_cp < MaxSector) {
8019 seq_printf(seq, "\tresync=PENDING");
8020 return 1;
8021 }
8022 return 0;
8023 }
8024 if (resync < 3) {
8025 seq_printf(seq, "\tresync=DELAYED");
8026 return 1;
8027 }
8028
NeilBrown403df472014-09-30 15:52:29 +10008029 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08008030 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10008031 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08008032 * u32, as those are the requirements for sector_div.
8033 * Thus 'scale' must be at least 10
8034 */
8035 scale = 10;
8036 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10008037 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08008038 scale++;
8039 }
8040 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10008041 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08008042
8043 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008044 {
NeilBrown4588b422006-03-27 01:18:04 -08008045 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008046 seq_printf(seq, "[");
8047 for (i = 0; i < x; i++)
8048 seq_printf(seq, "=");
8049 seq_printf(seq, ">");
8050 for (i = 0; i < y; i++)
8051 seq_printf(seq, ".");
8052 seq_printf(seq, "] ");
8053 }
NeilBrown4588b422006-03-27 01:18:04 -08008054 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08008055 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
8056 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07008057 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
8058 "check" :
8059 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
8060 "resync" : "recovery"))),
8061 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10008062 (unsigned long long) resync/2,
8063 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008064
8065 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07008066 * dt: time from mark until now
8067 * db: blocks written from mark until now
8068 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10008069 *
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008070 * rt is a sector_t, which is always 64bit now. We are keeping
8071 * the original algorithm, but it is not really necessary.
8072 *
8073 * Original algorithm:
8074 * So we divide before multiply in case it is 32bit and close
8075 * to the limit.
8076 * We scale the divisor (db) by 32 to avoid losing precision
8077 * near the end of resync when the number of remaining sectors
8078 * is close to 'db'.
8079 * We then divide rt by 32 after multiplying by db to compensate.
8080 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081 */
8082 dt = ((jiffies - mddev->resync_mark) / HZ);
8083 if (!dt) dt++;
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008084
8085 curr_mark_cnt = mddev->curr_mark_cnt;
8086 recovery_active = atomic_read(&mddev->recovery_active);
8087 resync_mark_cnt = mddev->resync_mark_cnt;
8088
8089 if (curr_mark_cnt >= (recovery_active + resync_mark_cnt))
8090 db = curr_mark_cnt - (recovery_active + resync_mark_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008091
NeilBrowndd71cf62009-05-07 12:49:35 +10008092 rt = max_sectors - resync; /* number of remaining sectors */
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008093 rt = div64_u64(rt, db/32+1);
NeilBrowndd71cf62009-05-07 12:49:35 +10008094 rt *= dt;
8095 rt >>= 5;
8096
8097 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
8098 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008099
NeilBrownff4e8d92006-07-10 04:44:16 -07008100 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10008101 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008102}
8103
8104static void *md_seq_start(struct seq_file *seq, loff_t *pos)
8105{
8106 struct list_head *tmp;
8107 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11008108 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008109
8110 if (l >= 0x10000)
8111 return NULL;
8112 if (!l--)
8113 /* header */
8114 return (void*)1;
8115
8116 spin_lock(&all_mddevs_lock);
8117 list_for_each(tmp,&all_mddevs)
8118 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11008119 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008120 mddev_get(mddev);
8121 spin_unlock(&all_mddevs_lock);
8122 return mddev;
8123 }
8124 spin_unlock(&all_mddevs_lock);
8125 if (!l--)
8126 return (void*)2;/* tail */
8127 return NULL;
8128}
8129
8130static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
8131{
8132 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008133 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10008134
Linus Torvalds1da177e2005-04-16 15:20:36 -07008135 ++*pos;
8136 if (v == (void*)2)
8137 return NULL;
8138
8139 spin_lock(&all_mddevs_lock);
8140 if (v == (void*)1)
8141 tmp = all_mddevs.next;
8142 else
8143 tmp = mddev->all_mddevs.next;
8144 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11008145 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008146 else {
8147 next_mddev = (void*)2;
8148 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10008149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008150 spin_unlock(&all_mddevs_lock);
8151
8152 if (v != (void*)1)
8153 mddev_put(mddev);
8154 return next_mddev;
8155
8156}
8157
8158static void md_seq_stop(struct seq_file *seq, void *v)
8159{
NeilBrownfd01b882011-10-11 16:47:53 +11008160 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161
8162 if (mddev && v != (void*)1 && v != (void*)2)
8163 mddev_put(mddev);
8164}
8165
8166static int md_seq_show(struct seq_file *seq, void *v)
8167{
NeilBrownfd01b882011-10-11 16:47:53 +11008168 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11008169 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11008170 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008171
8172 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11008173 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008174 seq_printf(seq, "Personalities : ");
8175 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008176 list_for_each_entry(pers, &pers_list, list)
8177 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178
8179 spin_unlock(&pers_lock);
8180 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02008181 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008182 return 0;
8183 }
8184 if (v == (void*)2) {
8185 status_unused(seq);
8186 return 0;
8187 }
8188
NeilBrown36d091f2014-12-15 12:56:58 +11008189 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008190 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
8191 seq_printf(seq, "%s : %sactive", mdname(mddev),
8192 mddev->pers ? "" : "in");
8193 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08008194 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08008196 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07008197 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008198 seq_printf(seq, " %s", mddev->pers->name);
8199 }
8200
Andre Nolldd8ac332009-03-31 14:33:13 +11008201 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11008202 rcu_read_lock();
8203 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008204 char b[BDEVNAME_SIZE];
8205 seq_printf(seq, " %s[%d]",
8206 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07008207 if (test_bit(WriteMostly, &rdev->flags))
8208 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07008209 if (test_bit(Journal, &rdev->flags))
8210 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08008211 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008212 seq_printf(seq, "(F)");
8213 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11008214 }
8215 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07008216 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11008217 if (test_bit(Replacement, &rdev->flags))
8218 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11008219 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008220 }
NeilBrownf97fcad2014-12-15 12:56:59 +11008221 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008222
8223 if (!list_empty(&mddev->disks)) {
8224 if (mddev->pers)
8225 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10008226 (unsigned long long)
8227 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008228 else
8229 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11008230 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008231 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07008232 if (mddev->persistent) {
8233 if (mddev->major_version != 0 ||
8234 mddev->minor_version != 90) {
8235 seq_printf(seq," super %d.%d",
8236 mddev->major_version,
8237 mddev->minor_version);
8238 }
NeilBrowne6910632008-02-06 01:39:51 -08008239 } else if (mddev->external)
8240 seq_printf(seq, " super external:%s",
8241 mddev->metadata_type);
8242 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07008243 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008244
8245 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11008246 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10008247 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08008248 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10008249 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08008250 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08008251 }
NeilBrown32a76272005-06-21 17:17:14 -07008252 } else
8253 seq_printf(seq, "\n ");
8254
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008255 md_bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008256
8257 seq_printf(seq, "\n");
8258 }
NeilBrown36d091f2014-12-15 12:56:58 +11008259 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10008260
Linus Torvalds1da177e2005-04-16 15:20:36 -07008261 return 0;
8262}
8263
Jan Engelhardt110518b2009-05-07 12:49:37 +10008264static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008265 .start = md_seq_start,
8266 .next = md_seq_next,
8267 .stop = md_seq_stop,
8268 .show = md_seq_show,
8269};
8270
8271static int md_seq_open(struct inode *inode, struct file *file)
8272{
Kay Sieversf1514632011-07-12 20:48:39 +02008273 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008274 int error;
8275
8276 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08008277 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02008278 return error;
8279
8280 seq = file->private_data;
8281 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008282 return error;
8283}
8284
NeilBrowne2f23b62014-04-09 14:33:51 +10008285static int md_unloading;
Al Viroafc9a422017-07-03 06:39:46 -04008286static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
NeilBrownd7603b72006-01-06 00:20:30 -08008287{
Kay Sieversf1514632011-07-12 20:48:39 +02008288 struct seq_file *seq = filp->private_data;
Al Viroafc9a422017-07-03 06:39:46 -04008289 __poll_t mask;
NeilBrownd7603b72006-01-06 00:20:30 -08008290
NeilBrowne2f23b62014-04-09 14:33:51 +10008291 if (md_unloading)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008292 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08008293 poll_wait(filp, &md_event_waiters, wait);
8294
8295 /* always allow read */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008296 mask = EPOLLIN | EPOLLRDNORM;
NeilBrownd7603b72006-01-06 00:20:30 -08008297
Kay Sieversf1514632011-07-12 20:48:39 +02008298 if (seq->poll_event != atomic_read(&md_event_count))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008299 mask |= EPOLLERR | EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08008300 return mask;
8301}
8302
Alexey Dobriyan97a32532020-02-03 17:37:17 -08008303static const struct proc_ops mdstat_proc_ops = {
8304 .proc_open = md_seq_open,
8305 .proc_read = seq_read,
8306 .proc_lseek = seq_lseek,
8307 .proc_release = seq_release,
8308 .proc_poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008309};
8310
NeilBrown84fc4b52011-10-11 16:49:58 +11008311int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008312{
NeilBrown9d487392016-11-02 14:16:49 +11008313 pr_debug("md: %s personality registered for level %d\n",
8314 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008315 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008316 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317 spin_unlock(&pers_lock);
8318 return 0;
8319}
NeilBrown6c144d32014-09-30 16:15:38 +10008320EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008321
NeilBrown84fc4b52011-10-11 16:49:58 +11008322int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008323{
NeilBrown9d487392016-11-02 14:16:49 +11008324 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008325 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008326 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008327 spin_unlock(&pers_lock);
8328 return 0;
8329}
NeilBrown6c144d32014-09-30 16:15:38 +10008330EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008331
NeilBrown6022e752015-08-13 12:32:55 +10008332int register_md_cluster_operations(struct md_cluster_operations *ops,
8333 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008334{
NeilBrown6022e752015-08-13 12:32:55 +10008335 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008336 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008337 if (md_cluster_ops != NULL)
8338 ret = -EALREADY;
8339 else {
8340 md_cluster_ops = ops;
8341 md_cluster_mod = module;
8342 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008343 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008344 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008345}
8346EXPORT_SYMBOL(register_md_cluster_operations);
8347
8348int unregister_md_cluster_operations(void)
8349{
8350 spin_lock(&pers_lock);
8351 md_cluster_ops = NULL;
8352 spin_unlock(&pers_lock);
8353 return 0;
8354}
8355EXPORT_SYMBOL(unregister_md_cluster_operations);
8356
8357int md_setup_cluster(struct mddev *mddev, int nodes)
8358{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008359 if (!md_cluster_ops)
8360 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008361 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008362 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008363 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11008364 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008365 spin_unlock(&pers_lock);
8366 return -ENOENT;
8367 }
8368 spin_unlock(&pers_lock);
8369
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05008370 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008371}
8372
8373void md_cluster_stop(struct mddev *mddev)
8374{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05008375 if (!md_cluster_ops)
8376 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008377 md_cluster_ops->leave(mddev);
8378 module_put(md_cluster_mod);
8379}
8380
NeilBrownfd01b882011-10-11 16:47:53 +11008381static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008382{
NeilBrownf72ffdd2014-09-30 14:23:59 +10008383 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11008385 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386
8387 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10008388 rcu_read_lock();
8389 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Michael Callahan59767fb2018-07-18 04:47:37 -07008391 curr_events = (int)part_stat_read_accum(&disk->part0, sectors) -
NeilBrowneea1bf32009-03-31 14:27:02 +11008392 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07008393 /* sync IO will cause sync_io to increase before the disk_stats
8394 * as sync_io is counted when a request starts, and
8395 * disk_stats is counted when it completes.
8396 * So resync activity will cause curr_events to be smaller than
8397 * when there was no such activity.
8398 * non-sync IO will cause disk_stat to increase without
8399 * increasing sync_io so curr_events will (eventually)
8400 * be larger than it was before. Once it becomes
8401 * substantially larger, the test below will cause
8402 * the array to appear non-idle, and resync will slow
8403 * down.
8404 * If there is a lot of outstanding resync activity when
8405 * we set last_event to curr_events, then all that activity
8406 * completing might cause the array to appear non-idle
8407 * and resync will be slowed down even though there might
8408 * not have been non-resync activity. This will only
8409 * happen once though. 'last_events' will soon reflect
8410 * the state where there is little or no outstanding
8411 * resync requests, and further resync activity will
8412 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08008413 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414 */
NeilBrowneea1bf32009-03-31 14:27:02 +11008415 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008416 rdev->last_events = curr_events;
8417 idle = 0;
8418 }
8419 }
NeilBrown4b809912008-07-21 17:05:25 +10008420 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008421 return idle;
8422}
8423
NeilBrownfd01b882011-10-11 16:47:53 +11008424void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008425{
8426 /* another "blocks" (512byte) blocks have been synced */
8427 atomic_sub(blocks, &mddev->recovery_active);
8428 wake_up(&mddev->recovery_wait);
8429 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07008430 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08008431 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008432 md_wakeup_thread(mddev->thread);
8433 // stop recovery, signal do_sync ....
8434 }
8435}
NeilBrown6c144d32014-09-30 16:15:38 +10008436EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008437
NeilBrown06d91a52005-06-21 17:17:12 -07008438/* md_write_start(mddev, bi)
8439 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07008440 * in superblock) before writing, schedule a superblock update
8441 * and wait for it to complete.
NeilBrowncc27b0c2017-06-05 16:49:39 +10008442 * A return value of 'false' means that the write wasn't recorded
8443 * and cannot proceed as the array is being suspend.
NeilBrown06d91a52005-06-21 17:17:12 -07008444 */
NeilBrowncc27b0c2017-06-05 16:49:39 +10008445bool md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446{
Neil Brown0fd62b82008-06-28 08:31:36 +10008447 int did_change = 0;
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008448
NeilBrown06d91a52005-06-21 17:17:12 -07008449 if (bio_data_dir(bi) != WRITE)
NeilBrowncc27b0c2017-06-05 16:49:39 +10008450 return true;
NeilBrown06d91a52005-06-21 17:17:12 -07008451
NeilBrownf91de922005-11-08 21:39:36 -08008452 BUG_ON(mddev->ro == 1);
8453 if (mddev->ro == 2) {
8454 /* need to switch to read/write */
8455 mddev->ro = 0;
8456 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8457 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08008458 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008459 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08008460 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008461 rcu_read_lock();
8462 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11008463 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07008464 if (mddev->safemode == 1)
8465 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11008466 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
NeilBrown81fe48e2017-08-08 16:56:36 +10008467 if (mddev->in_sync || mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11008468 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07008469 if (mddev->in_sync) {
8470 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008471 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8472 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07008473 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008474 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07008475 }
NeilBrown85572d72014-12-15 12:56:56 +11008476 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07008477 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008478 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10008479 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008480 sysfs_notify_dirent_safe(mddev->sysfs_state);
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008481 if (!mddev->has_superblocks)
8482 return true;
NeilBrown09a44cc2008-05-23 13:04:36 -07008483 wait_event(mddev->sb_wait,
NeilBrownd47c8ad2017-10-05 16:23:16 +11008484 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8485 mddev->suspended);
NeilBrowncc27b0c2017-06-05 16:49:39 +10008486 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8487 percpu_ref_put(&mddev->writes_pending);
8488 return false;
8489 }
8490 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008491}
NeilBrown6c144d32014-09-30 16:15:38 +10008492EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008493
NeilBrown49728052017-03-15 14:05:12 +11008494/* md_write_inc can only be called when md_write_start() has
8495 * already been called at least once of the current request.
8496 * It increments the counter and is useful when a single request
8497 * is split into several parts. Each part causes an increment and
8498 * so needs a matching md_write_end().
8499 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8500 * a spinlocked region.
8501 */
8502void md_write_inc(struct mddev *mddev, struct bio *bi)
8503{
8504 if (bio_data_dir(bi) != WRITE)
8505 return;
8506 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11008507 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008508}
8509EXPORT_SYMBOL(md_write_inc);
8510
NeilBrownfd01b882011-10-11 16:47:53 +11008511void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008512{
NeilBrown4ad23a972017-03-15 14:05:14 +11008513 percpu_ref_put(&mddev->writes_pending);
8514
8515 if (mddev->safemode == 2)
8516 md_wakeup_thread(mddev->thread);
8517 else if (mddev->safemode_delay)
8518 /* The roundup() ensures this only performs locking once
8519 * every ->safemode_delay jiffies
8520 */
8521 mod_timer(&mddev->safemode_timer,
8522 roundup(jiffies, mddev->safemode_delay) +
8523 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008524}
NeilBrown4ad23a972017-03-15 14:05:14 +11008525
NeilBrown6c144d32014-09-30 16:15:38 +10008526EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527
NeilBrown2a2275d2007-01-26 00:57:11 -08008528/* md_allow_write(mddev)
8529 * Calling this ensures that the array is marked 'active' so that writes
8530 * may proceed without blocking. It is important to call this before
8531 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8532 * Must be called with mddev_lock held.
8533 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008534void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008535{
8536 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008537 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008538 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008539 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008540 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008541 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008542
NeilBrown85572d72014-12-15 12:56:56 +11008543 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008544 if (mddev->in_sync) {
8545 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008546 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8547 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008548 if (mddev->safemode_delay &&
8549 mddev->safemode == 0)
8550 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008551 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008552 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008553 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008554 /* wait for the dirty state to be recorded in the metadata */
8555 wait_event(mddev->sb_wait,
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008556 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008557 } else
NeilBrown85572d72014-12-15 12:56:56 +11008558 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008559}
8560EXPORT_SYMBOL_GPL(md_allow_write);
8561
Linus Torvalds1da177e2005-04-16 15:20:36 -07008562#define SYNC_MARKS 10
8563#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008564#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008565void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008566{
Shaohua Li4ed87312012-10-11 13:34:00 +11008567 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008568 struct mddev *mddev2;
Yufen Yue5b521e2019-06-14 15:41:07 -07008569 unsigned int currspeed = 0, window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008570 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008571 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008572 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008573 sector_t mark_cnt[SYNC_MARKS];
8574 int last_mark,m;
8575 struct list_head *tmp;
8576 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008577 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008578 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008579 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008580 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008581 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008582
8583 /* just incase thread restarts... */
Song Liud5d885f2017-11-19 22:17:01 -08008584 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8585 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008586 return;
NeilBrown3991b312014-05-28 13:39:23 +10008587 if (mddev->ro) {/* never try to sync a read-only array */
8588 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008589 return;
NeilBrown3991b312014-05-28 13:39:23 +10008590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008591
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008592 if (mddev_is_clustered(mddev)) {
8593 ret = md_cluster_ops->resync_start(mddev);
8594 if (ret)
8595 goto skip;
8596
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008597 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008598 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8599 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8600 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8601 && ((unsigned long long)mddev->curr_resync_completed
8602 < (unsigned long long)mddev->resync_max_sectors))
8603 goto skip;
8604 }
8605
NeilBrown61df9d92006-10-03 01:15:57 -07008606 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008607 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008608 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008609 action = "check";
8610 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008611 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008612 action = "repair";
8613 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008614 desc = "resync";
8615 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8616 desc = "reshape";
8617 else
8618 desc = "recovery";
8619
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008620 mddev->last_sync_action = action ?: desc;
8621
Linus Torvalds1da177e2005-04-16 15:20:36 -07008622 /* we overload curr_resync somewhat here.
8623 * 0 == not engaged in resync at all
8624 * 2 == checking that there is no conflict with another sync
8625 * 1 == like 2, but have yielded to allow conflicting resync to
Yufen Yue5b521e2019-06-14 15:41:07 -07008626 * commence
Linus Torvalds1da177e2005-04-16 15:20:36 -07008627 * other == active in resync - this many blocks
8628 *
8629 * Before starting a resync we must have set curr_resync to
8630 * 2, and then checked that every "conflicting" array has curr_resync
8631 * less than ours. When we find one that is the same or higher
8632 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8633 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8634 * This will mean we have to start checking from the beginning again.
8635 *
8636 */
8637
8638 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008639 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640 mddev->curr_resync = 2;
8641
8642 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008643 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008644 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008645 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008646 if (mddev2 == mddev)
8647 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008648 if (!mddev->parallel_resync
8649 && mddev2->curr_resync
8650 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008651 DEFINE_WAIT(wq);
8652 if (mddev < mddev2 && mddev->curr_resync == 2) {
8653 /* arbitrarily yield */
8654 mddev->curr_resync = 1;
8655 wake_up(&resync_wait);
8656 }
8657 if (mddev > mddev2 && mddev->curr_resync == 1)
8658 /* no need to wait here, we can wait the next
8659 * time 'round when curr_resync == 2
8660 */
8661 continue;
NeilBrown97441972008-09-19 11:49:54 +10008662 /* We need to wait 'interruptible' so as not to
8663 * contribute to the load average, and not to
8664 * be caught by 'softlockup'
8665 */
8666 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008667 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008668 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008669 if (mddev2_minor != mddev2->md_minor) {
8670 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008671 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8672 desc, mdname(mddev),
8673 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008675 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008676 if (signal_pending(current))
8677 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008678 schedule();
8679 finish_wait(&resync_wait, &wq);
8680 goto try_again;
8681 }
8682 finish_wait(&resync_wait, &wq);
8683 }
8684 }
8685 } while (mddev->curr_resync < 2);
8686
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008687 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008688 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008689 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008690 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008691 */
8692 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008693 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008694 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008695 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8696 j = mddev->resync_min;
8697 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008698 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008699
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008700 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
NeilBrownc804cde2012-05-21 09:28:33 +10008701 max_sectors = mddev->resync_max_sectors;
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008702 /*
8703 * If the original node aborts reshaping then we continue the
8704 * reshaping, so set j again to avoid restart reshape from the
8705 * first beginning
8706 */
8707 if (mddev_is_clustered(mddev) &&
8708 mddev->reshape_position != MaxSector)
8709 j = mddev->reshape_position;
8710 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008711 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008712 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008713 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008714 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008715 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008716 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008717 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008718 !test_bit(Faulty, &rdev->flags) &&
8719 !test_bit(In_sync, &rdev->flags) &&
8720 rdev->recovery_offset < j)
8721 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008722 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008723
8724 /* If there is a bitmap, we need to make sure all
8725 * writes that started before we added a spare
8726 * complete before we start doing a recovery.
8727 * Otherwise the write might complete and (via
8728 * bitmap_endwrite) set a bit in the bitmap after the
8729 * recovery has checked that bit and skipped that
8730 * region.
8731 */
8732 if (mddev->bitmap) {
8733 mddev->pers->quiesce(mddev, 1);
8734 mddev->pers->quiesce(mddev, 0);
8735 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008737
NeilBrown9d487392016-11-02 14:16:49 +11008738 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8739 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8740 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8741 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008742
NeilBrowneea1bf32009-03-31 14:27:02 +11008743 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008744
NeilBrown57afd892005-06-21 17:17:13 -07008745 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008746 for (m = 0; m < SYNC_MARKS; m++) {
8747 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008748 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008749 }
8750 last_mark = 0;
8751 mddev->resync_mark = mark[last_mark];
8752 mddev->resync_mark_cnt = mark_cnt[last_mark];
8753
8754 /*
8755 * Tune reconstruction:
8756 */
Yufen Yue5b521e2019-06-14 15:41:07 -07008757 window = 32 * (PAGE_SIZE / 512);
NeilBrown9d487392016-11-02 14:16:49 +11008758 pr_debug("md: using %dk window, over a total of %lluk.\n",
8759 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008760
8761 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008762 last_check = 0;
8763
8764 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008765 pr_debug("md: resuming %s of %s from checkpoint.\n",
8766 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008767 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008768 } else
8769 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008770 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008771 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8772 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008773 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008774
majianpeng7c2c57c2012-07-03 12:12:26 +10008775 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008776 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008777 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008778
NeilBrown57afd892005-06-21 17:17:13 -07008779 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008780
NeilBrown7a91ee12009-05-26 12:57:21 +10008781 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8782 ((mddev->curr_resync > mddev->curr_resync_completed &&
8783 (mddev->curr_resync - mddev->curr_resync_completed)
8784 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008785 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008786 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008787 >= mddev->resync_max - mddev->curr_resync_completed ||
8788 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008789 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008790 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008791 wait_event(mddev->recovery_wait,
8792 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008793 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008794 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8795 j > mddev->recovery_cp)
8796 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008797 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008798 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008799 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008800 }
NeilBrownacb180b2009-04-14 16:28:34 +10008801
NeilBrownc91abf52013-11-19 12:02:01 +11008802 while (j >= mddev->resync_max &&
8803 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008804 /* As this condition is controlled by user-space,
8805 * we can block indefinitely, so use '_interruptible'
8806 * to avoid triggering warnings.
8807 */
8808 flush_signals(current); /* just in case */
8809 wait_event_interruptible(mddev->recovery_wait,
8810 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008811 || test_bit(MD_RECOVERY_INTR,
8812 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008813 }
NeilBrownacb180b2009-04-14 16:28:34 +10008814
NeilBrownc91abf52013-11-19 12:02:01 +11008815 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8816 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008817
NeilBrown09314792015-02-19 16:04:40 +11008818 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008819 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008820 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008821 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008822 }
NeilBrown57afd892005-06-21 17:17:13 -07008823
8824 if (!skipped) { /* actual IO requested */
8825 io_sectors += sectors;
8826 atomic_add(sectors, &mddev->recovery_active);
8827 }
8828
NeilBrowne875ece2011-07-28 11:39:24 +10008829 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8830 break;
8831
Linus Torvalds1da177e2005-04-16 15:20:36 -07008832 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008833 if (j > max_sectors)
8834 /* when skipping, extra large numbers can be returned. */
8835 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008836 if (j > 2)
8837 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008838 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008839 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008840 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008841 * visible in /proc/mdstat
8842 */
8843 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008844
8845 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008846 continue;
8847
NeilBrown57afd892005-06-21 17:17:13 -07008848 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008849 repeat:
8850 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8851 /* step marks */
8852 int next = (last_mark+1) % SYNC_MARKS;
8853
8854 mddev->resync_mark = mark[next];
8855 mddev->resync_mark_cnt = mark_cnt[next];
8856 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008857 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858 last_mark = next;
8859 }
8860
NeilBrownc91abf52013-11-19 12:02:01 +11008861 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8862 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008863
8864 /*
8865 * this loop exits only if either when we are slower than
8866 * the 'hard' speed limit, or the system was IO-idle for
8867 * a jiffy.
8868 * the system might be non-idle CPU-wise, but we only care
8869 * about not overloading the IO subsystem. (things like an
8870 * e2fsck being done on the RAID array should execute fast)
8871 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008872 cond_resched();
8873
Xiao Niac7e50a2014-08-07 09:37:41 -04008874 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8875 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008876 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008877
NeilBrown88202a02006-01-06 00:21:36 -08008878 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008879 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008880 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008881 goto repeat;
8882 }
NeilBrownac8fa412015-02-19 16:55:00 +11008883 if (!is_mddev_idle(mddev, 0)) {
8884 /*
8885 * Give other IO more of a chance.
8886 * The faster the devices, the less we wait.
8887 */
8888 wait_event(mddev->recovery_wait,
8889 !atomic_read(&mddev->recovery_active));
8890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008891 }
8892 }
NeilBrown9d487392016-11-02 14:16:49 +11008893 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8894 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8895 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008896 /*
8897 * this also signals 'finished resyncing' to md_stop
8898 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008899 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8901
NeilBrown5ed1df22015-07-24 13:27:08 +10008902 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8903 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008904 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008905 mddev->curr_resync_completed = mddev->curr_resync;
8906 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8907 }
NeilBrown09314792015-02-19 16:04:40 +11008908 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008909
NeilBrowndfc70642008-05-23 13:04:39 -07008910 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008911 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008912 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8913 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8914 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008915 pr_debug("md: checkpointing %s of %s.\n",
8916 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008917 if (test_bit(MD_RECOVERY_ERROR,
8918 &mddev->recovery))
8919 mddev->recovery_cp =
8920 mddev->curr_resync_completed;
8921 else
8922 mddev->recovery_cp =
8923 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008924 }
8925 } else
8926 mddev->recovery_cp = MaxSector;
8927 } else {
8928 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8929 mddev->curr_resync = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11008930 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8931 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
8932 rcu_read_lock();
8933 rdev_for_each_rcu(rdev, mddev)
8934 if (rdev->raid_disk >= 0 &&
8935 mddev->delta_disks >= 0 &&
8936 !test_bit(Journal, &rdev->flags) &&
8937 !test_bit(Faulty, &rdev->flags) &&
8938 !test_bit(In_sync, &rdev->flags) &&
8939 rdev->recovery_offset < mddev->curr_resync)
8940 rdev->recovery_offset = mddev->curr_resync;
8941 rcu_read_unlock();
8942 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008944 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008945 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008946 /* set CHANGE_PENDING here since maybe another update is needed,
8947 * so other nodes are informed. It should be harmless for normal
8948 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008949 set_mask_bits(&mddev->sb_flags, 0,
8950 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008951
BingJing Chang88763912018-02-22 13:34:46 +08008952 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8953 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8954 mddev->delta_disks > 0 &&
8955 mddev->pers->finish_reshape &&
8956 mddev->pers->size &&
8957 mddev->queue) {
8958 mddev_lock_nointr(mddev);
8959 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
8960 mddev_unlock(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08008961 if (!mddev_is_clustered(mddev)) {
8962 set_capacity(mddev->gendisk, mddev->array_sectors);
8963 revalidate_disk(mddev->gendisk);
8964 }
BingJing Chang88763912018-02-22 13:34:46 +08008965 }
8966
NeilBrown23da4222014-12-15 12:57:01 +11008967 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008968 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8969 /* We completed so min/max setting can be forgotten if used. */
8970 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8971 mddev->resync_min = 0;
8972 mddev->resync_max = MaxSector;
8973 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8974 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008975 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008976 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008977 spin_unlock(&mddev->lock);
8978
Linus Torvalds1da177e2005-04-16 15:20:36 -07008979 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008980 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008981 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008982}
NeilBrown29269552006-03-27 01:18:10 -08008983EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008984
NeilBrown746d3202013-04-24 11:42:41 +10008985static int remove_and_add_spares(struct mddev *mddev,
8986 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008987{
NeilBrown3cb03002011-10-11 16:45:26 +11008988 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008989 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008990 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008991 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008992
NeilBrown39772f02018-02-03 09:19:30 +11008993 if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
8994 /* Mustn't remove devices when resync thread is running */
8995 return 0;
8996
NeilBrownd787be42016-06-02 16:19:53 +10008997 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008998 if ((this == NULL || rdev == this) &&
8999 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07009000 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10009001 test_bit(Faulty, &rdev->flags) &&
9002 atomic_read(&rdev->nr_pending)==0) {
9003 /* Faulty non-Blocked devices with nr_pending == 0
9004 * never get nr_pending incremented,
9005 * never get Faulty cleared, and never get Blocked set.
9006 * So we can synchronize_rcu now rather than once per device
9007 */
9008 remove_some = true;
9009 set_bit(RemoveSynchronized, &rdev->flags);
9010 }
9011 }
9012
9013 if (remove_some)
9014 synchronize_rcu();
9015 rdev_for_each(rdev, mddev) {
9016 if ((this == NULL || rdev == this) &&
9017 rdev->raid_disk >= 0 &&
9018 !test_bit(Blocked, &rdev->flags) &&
9019 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07009020 (!test_bit(In_sync, &rdev->flags) &&
9021 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10009022 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009023 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11009024 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10009025 sysfs_unlink_rdev(mddev, rdev);
NeilBrown011abdc2018-04-26 14:46:29 +10009026 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009027 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11009028 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009029 }
9030 }
NeilBrownd787be42016-06-02 16:19:53 +10009031 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
9032 clear_bit(RemoveSynchronized, &rdev->flags);
9033 }
9034
Jonathan Brassow90584fc2013-03-07 16:24:26 -06009035 if (removed && mddev->kobj.sd)
9036 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009037
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05009038 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10009039 goto no_add;
9040
NeilBrowndafb20f2012-03-19 12:46:39 +11009041 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05009042 if (this && this != rdev)
9043 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009044 if (test_bit(Candidate, &rdev->flags))
9045 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11009046 if (rdev->raid_disk >= 0 &&
9047 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07009048 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11009049 !test_bit(Faulty, &rdev->flags))
9050 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009051 if (rdev->raid_disk >= 0)
9052 continue;
9053 if (test_bit(Faulty, &rdev->flags))
9054 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009055 if (!test_bit(Journal, &rdev->flags)) {
9056 if (mddev->ro &&
9057 ! (rdev->saved_raid_disk >= 0 &&
9058 !test_bit(Bitmap_sync, &rdev->flags)))
9059 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009060
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009061 rdev->recovery_offset = 0;
9062 }
Guoqing Jiang3f79cc22020-04-04 23:57:11 +02009063 if (mddev->pers->hot_add_disk(mddev, rdev) == 0) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10009064 if (sysfs_link_rdev(mddev, rdev))
9065 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009066 if (!test_bit(Journal, &rdev->flags))
9067 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009068 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08009069 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07009070 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009071 }
NeilBrown746d3202013-04-24 11:42:41 +10009072no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10009073 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08009074 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009075 return spares;
9076}
NeilBrown7ebc0be2011-01-14 09:14:33 +11009077
NeilBrownac05f252014-09-30 08:10:42 +10009078static void md_start_sync(struct work_struct *ws)
9079{
9080 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05009081
NeilBrownac05f252014-09-30 08:10:42 +10009082 mddev->sync_thread = md_register_thread(md_do_sync,
9083 mddev,
9084 "resync");
9085 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11009086 pr_warn("%s: could not start resync thread...\n",
9087 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10009088 /* leave the spares where they are, it shouldn't hurt */
9089 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9090 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9091 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9092 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9093 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11009094 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10009095 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9096 &mddev->recovery))
9097 if (mddev->sysfs_action)
9098 sysfs_notify_dirent_safe(mddev->sysfs_action);
9099 } else
9100 md_wakeup_thread(mddev->sync_thread);
9101 sysfs_notify_dirent_safe(mddev->sysfs_action);
9102 md_new_event(mddev);
9103}
9104
Linus Torvalds1da177e2005-04-16 15:20:36 -07009105/*
9106 * This routine is regularly called by all per-raid-array threads to
9107 * deal with generic issues like resync and super-block update.
9108 * Raid personalities that don't have a thread (linear/raid0) do not
9109 * need this as they never do any recovery or update the superblock.
9110 *
9111 * It does not do any resync itself, but rather "forks" off other threads
9112 * to do that as needed.
9113 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
9114 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07009115 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07009116 * and wakeups up this thread which will reap the thread and finish up.
9117 * This thread also removes any faulty devices (with nr_pending == 0).
9118 *
9119 * The overall approach is:
9120 * 1/ if the superblock needs updating, update it.
9121 * 2/ If a recovery thread is running, don't do anything else.
9122 * 3/ If recovery has finished, clean up, possibly marking spares active.
9123 * 4/ If there are any faulty devices, remove them.
9124 * 5/ If array is degraded, try to add spares devices
9125 * 6/ If array has spares or is not in-sync, start a resync thread.
9126 */
NeilBrownfd01b882011-10-11 16:47:53 +11009127void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009128{
NeilBrown059421e2018-10-03 15:04:41 +10009129 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
9130 /* Write superblock - thread that called mddev_suspend()
9131 * holds reconfig_mutex for us.
9132 */
9133 set_bit(MD_UPDATING_SB, &mddev->flags);
9134 smp_mb__after_atomic();
9135 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
9136 md_update_sb(mddev, 0);
9137 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
9138 wake_up(&mddev->sb_wait);
9139 }
9140
Jonathan Brassow68866e42011-06-08 15:10:08 +10009141 if (mddev->suspended)
9142 return;
9143
NeilBrown5f404022005-06-21 17:17:16 -07009144 if (mddev->bitmap)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009145 md_bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009146
NeilBrownfca4d842005-06-21 17:17:11 -07009147 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07009148 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11009149 pr_debug("md: %s in immediate safe mode\n",
9150 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07009151 mddev->safemode = 2;
9152 }
9153 flush_signals(current);
9154 }
9155
NeilBrownc89a8ee2008-08-05 15:54:13 +10009156 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
9157 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009158 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08009159 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07009160 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07009161 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07009162 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11009163 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07009164 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009165 ))
9166 return;
NeilBrownfca4d842005-06-21 17:17:11 -07009167
NeilBrowndf5b89b2006-03-27 01:18:20 -08009168 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009169 int spares = 0;
NeilBrown480523f2019-08-20 10:21:09 +10009170 bool try_set_sync = mddev->safemode != 0;
NeilBrownfca4d842005-06-21 17:17:11 -07009171
Shaohua Liafc1f552017-08-11 20:34:45 -07009172 if (!mddev->external && mddev->safemode == 1)
NeilBrown33182d12017-08-08 16:56:36 +10009173 mddev->safemode = 0;
9174
NeilBrownc89a8ee2008-08-05 15:54:13 +10009175 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10009176 struct md_rdev *rdev;
9177 if (!mddev->external && mddev->in_sync)
9178 /* 'Blocked' flag not needed as failed devices
9179 * will be recorded if array switched to read/write.
9180 * Leaving it set will prevent the device
9181 * from being removed.
9182 */
9183 rdev_for_each(rdev, mddev)
9184 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10009185 /* On a read-only array we can:
9186 * - remove failed devices
9187 * - add already-in_sync devices if the array itself
9188 * is in-sync.
9189 * As we only add devices that are already in-sync,
9190 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10009191 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10009192 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11009193 /* There is no thread, but we need to call
9194 * ->spare_active and clear saved_raid_disk
9195 */
NeilBrown2ac295a2014-05-29 11:40:03 +10009196 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11009197 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10009198 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11009199 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08009200 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10009201 goto unlock;
9202 }
9203
Guoqing Jiang659b2542015-12-21 10:50:59 +11009204 if (mddev_is_clustered(mddev)) {
9205 struct md_rdev *rdev;
9206 /* kick the device if another node issued a
9207 * remove disk.
9208 */
9209 rdev_for_each(rdev, mddev) {
9210 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
9211 rdev->raid_disk < 0)
9212 md_kick_rdev_from_array(rdev);
9213 }
9214 }
9215
NeilBrown480523f2019-08-20 10:21:09 +10009216 if (try_set_sync && !mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11009217 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11009218 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11009219 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07009220 }
NeilBrownfca4d842005-06-21 17:17:11 -07009221
Shaohua Li29530792016-12-08 15:48:19 -08009222 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07009223 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07009224
Linus Torvalds1da177e2005-04-16 15:20:36 -07009225 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
9226 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
9227 /* resync/recovery still happening */
9228 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9229 goto unlock;
9230 }
9231 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009232 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009233 goto unlock;
9234 }
Neil Brown72a23c22008-06-28 08:31:41 +10009235 /* Set RUNNING before clearing NEEDED to avoid
9236 * any transients in the value of "sync_action".
9237 */
NeilBrown72f36d52012-10-11 14:25:57 +11009238 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11009239 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10009240 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11009241 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08009242 /* Clear some bits that don't mean anything, but
9243 * might be left set
9244 */
NeilBrown24dd4692005-11-08 21:39:26 -08009245 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
9246 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009247
NeilBrowned209582012-04-24 10:23:14 +10009248 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
9249 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10009250 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009251 /* no recovery is running.
9252 * remove any failed drives, then
9253 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11009254 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07009255 * the personality to fail the re-add.
9256 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009257
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009258 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10009259 if (mddev->pers->check_reshape == NULL ||
9260 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009261 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10009262 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009263 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009264 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10009265 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08009266 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9267 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07009268 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009269 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08009270 } else if (mddev->recovery_cp < MaxSector) {
9271 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009272 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08009273 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
9274 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10009275 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08009276
Linus Torvalds1da177e2005-04-16 15:20:36 -07009277 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10009278 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07009279 /* We are adding a device or devices to an array
9280 * which has the bitmap stored on all devices.
9281 * So make sure all bitmap pages get written
9282 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009283 md_bitmap_write_all(mddev->bitmap);
NeilBrowna654b9d82005-06-21 17:17:27 -07009284 }
NeilBrownac05f252014-09-30 08:10:42 +10009285 INIT_WORK(&mddev->del_work, md_start_sync);
9286 queue_work(md_misc_wq, &mddev->del_work);
9287 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009288 }
NeilBrownac05f252014-09-30 08:10:42 +10009289 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10009290 if (!mddev->sync_thread) {
9291 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11009292 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10009293 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9294 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11009295 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10009296 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10009297 }
NeilBrownac05f252014-09-30 08:10:42 +10009298 unlock:
9299 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009300 mddev_unlock(mddev);
9301 }
9302}
NeilBrown6c144d32014-09-30 16:15:38 +10009303EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009304
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009305void md_reap_sync_thread(struct mddev *mddev)
9306{
9307 struct md_rdev *rdev;
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009308 sector_t old_dev_sectors = mddev->dev_sectors;
9309 bool is_reshaped = false;
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009310
9311 /* resync has finished, collect result */
9312 md_unregister_thread(&mddev->sync_thread);
9313 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
Guoqing Jiang0d8ed0e92019-07-24 11:09:21 +02009314 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
9315 mddev->degraded != mddev->raid_disks) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009316 /* success...*/
9317 /* activate any spares */
9318 if (mddev->pers->spare_active(mddev)) {
9319 sysfs_notify(&mddev->kobj, NULL,
9320 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08009321 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009322 }
9323 }
9324 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009325 mddev->pers->finish_reshape) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009326 mddev->pers->finish_reshape(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009327 if (mddev_is_clustered(mddev))
9328 is_reshaped = true;
9329 }
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009330
9331 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11009332 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009333 */
NeilBrownf4667222013-12-09 12:04:56 +11009334 if (!mddev->degraded)
9335 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009336 rdev->saved_raid_disk = -1;
9337
9338 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08009339 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04009340 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9341 * clustered raid */
9342 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
9343 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009344 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10009345 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009346 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9347 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9348 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9349 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009350 /*
9351 * We call md_cluster_ops->update_size here because sync_size could
9352 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9353 * so it is time to update size across cluster.
9354 */
9355 if (mddev_is_clustered(mddev) && is_reshaped
9356 && !test_bit(MD_CLOSING, &mddev->flags))
9357 md_cluster_ops->update_size(mddev, old_dev_sectors);
NeilBrownf851b602014-12-11 10:02:10 +11009358 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009359 /* flag recovery needed just to double check */
9360 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9361 sysfs_notify_dirent_safe(mddev->sysfs_action);
9362 md_new_event(mddev);
9363 if (mddev->event_work.func)
9364 queue_work(md_misc_wq, &mddev->event_work);
9365}
NeilBrown6c144d32014-09-30 16:15:38 +10009366EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009367
NeilBrownfd01b882011-10-11 16:47:53 +11009368void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07009369{
NeilBrown00bcb4a2010-06-01 19:37:23 +10009370 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07009371 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10009372 !test_bit(Blocked, &rdev->flags) &&
9373 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07009374 msecs_to_jiffies(5000));
9375 rdev_dec_pending(rdev, mddev);
9376}
9377EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9378
NeilBrownc6563a82012-05-21 09:27:00 +10009379void md_finish_reshape(struct mddev *mddev)
9380{
9381 /* called be personality module when reshape completes. */
9382 struct md_rdev *rdev;
9383
9384 rdev_for_each(rdev, mddev) {
9385 if (rdev->data_offset > rdev->new_data_offset)
9386 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9387 else
9388 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9389 rdev->data_offset = rdev->new_data_offset;
9390 }
9391}
9392EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10009393
Vishal Vermafc974ee2015-12-24 19:20:34 -07009394/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10009395
Vishal Vermafc974ee2015-12-24 19:20:34 -07009396/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11009397int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10009398 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009399{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04009400 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10009401 int rv;
9402 if (is_new)
9403 s += rdev->new_data_offset;
9404 else
9405 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07009406 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9407 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10009408 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009409 if (test_bit(ExternalBbl, &rdev->flags))
9410 sysfs_notify(&rdev->kobj, NULL,
9411 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11009412 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08009413 set_mask_bits(&mddev->sb_flags, 0,
9414 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10009415 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07009416 return 1;
9417 } else
9418 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10009419}
9420EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9421
NeilBrownc6563a82012-05-21 09:27:00 +10009422int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9423 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009424{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009425 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10009426 if (is_new)
9427 s += rdev->new_data_offset;
9428 else
9429 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009430 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9431 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
9432 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
9433 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10009434}
9435EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9436
Adrian Bunk75c96f82005-05-05 16:16:09 -07009437static int md_notify_reboot(struct notifier_block *this,
9438 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439{
9440 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11009441 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009442 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009443
NeilBrownc744a652012-03-19 12:46:37 +11009444 for_each_mddev(mddev, tmp) {
9445 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10009446 if (mddev->pers)
9447 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009448 if (mddev->persistent)
9449 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009450 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009451 }
NeilBrownc744a652012-03-19 12:46:37 +11009452 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009453 }
NeilBrownc744a652012-03-19 12:46:37 +11009454 /*
9455 * certain more exotic SCSI devices are known to be
9456 * volatile wrt too early system reboots. While the
9457 * right place to handle this issue is the given
9458 * driver, we do want to have a safe RAID driver ...
9459 */
9460 if (need_delay)
9461 mdelay(1000*1);
9462
Linus Torvalds1da177e2005-04-16 15:20:36 -07009463 return NOTIFY_DONE;
9464}
9465
Adrian Bunk75c96f82005-05-05 16:16:09 -07009466static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009467 .notifier_call = md_notify_reboot,
9468 .next = NULL,
9469 .priority = INT_MAX, /* before any real devices */
9470};
9471
9472static void md_geninit(void)
9473{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009474 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009475
Alexey Dobriyan97a32532020-02-03 17:37:17 -08009476 proc_create("mdstat", S_IRUGO, NULL, &mdstat_proc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009477}
9478
Adrian Bunk75c96f82005-05-05 16:16:09 -07009479static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009480{
Tejun Heoe804ac72010-10-15 15:36:08 +02009481 int ret = -ENOMEM;
9482
Tejun Heoada609e2011-01-25 14:35:54 +01009483 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009484 if (!md_wq)
9485 goto err_wq;
9486
9487 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9488 if (!md_misc_wq)
9489 goto err_misc_wq;
9490
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009491 md_rdev_misc_wq = alloc_workqueue("md_rdev_misc", 0, 0);
9492 if (!md_misc_wq)
9493 goto err_rdev_misc_wq;
9494
Tejun Heoe804ac72010-10-15 15:36:08 +02009495 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9496 goto err_md;
9497
9498 if ((ret = register_blkdev(0, "mdp")) < 0)
9499 goto err_mdp;
9500 mdp_major = ret;
9501
NeilBrownaf5628f2014-07-31 13:54:54 +10009502 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009503 md_probe, NULL, NULL);
9504 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009505 md_probe, NULL, NULL);
9506
Linus Torvalds1da177e2005-04-16 15:20:36 -07009507 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009508 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009509
9510 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009512
Tejun Heoe804ac72010-10-15 15:36:08 +02009513err_mdp:
9514 unregister_blkdev(MD_MAJOR, "md");
9515err_md:
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009516 destroy_workqueue(md_rdev_misc_wq);
9517err_rdev_misc_wq:
Tejun Heoe804ac72010-10-15 15:36:08 +02009518 destroy_workqueue(md_misc_wq);
9519err_misc_wq:
9520 destroy_workqueue(md_wq);
9521err_wq:
9522 return ret;
9523}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009524
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009525static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009526{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009527 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9528 struct md_rdev *rdev2;
9529 int role, ret;
9530 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009531
Guoqing Jiang818da592017-03-01 16:42:40 +08009532 /*
9533 * If size is changed in another node then we need to
9534 * do resize as well.
9535 */
9536 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9537 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9538 if (ret)
9539 pr_info("md-cluster: resize failed\n");
9540 else
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009541 md_bitmap_update_sb(mddev->bitmap);
Guoqing Jiang818da592017-03-01 16:42:40 +08009542 }
9543
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009544 /* Check for change of roles in the active devices */
9545 rdev_for_each(rdev2, mddev) {
9546 if (test_bit(Faulty, &rdev2->flags))
9547 continue;
9548
9549 /* Check if the roles changed */
9550 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009551
9552 if (test_bit(Candidate, &rdev2->flags)) {
9553 if (role == 0xfffe) {
9554 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9555 md_kick_rdev_from_array(rdev2);
9556 continue;
9557 }
9558 else
9559 clear_bit(Candidate, &rdev2->flags);
9560 }
9561
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009562 if (role != rdev2->raid_disk) {
Guoqing Jiangca1e98e2018-10-18 16:37:45 +08009563 /*
9564 * got activated except reshape is happening.
9565 */
9566 if (rdev2->raid_disk == -1 && role != 0xffff &&
9567 !(le32_to_cpu(sb->feature_map) &
9568 MD_FEATURE_RESHAPE_ACTIVE)) {
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009569 rdev2->saved_raid_disk = role;
9570 ret = remove_and_add_spares(mddev, rdev2);
9571 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11009572 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04009573 /* wakeup mddev->thread here, so array could
9574 * perform resync with the new activated disk */
9575 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9576 md_wakeup_thread(mddev->thread);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009577 }
9578 /* device faulty
9579 * We just want to do the minimum to mark the disk
9580 * as faulty. The recovery is performed by the
9581 * one who initiated the error.
9582 */
9583 if ((role == 0xfffe) || (role == 0xfffd)) {
9584 md_error(mddev, rdev2);
9585 clear_bit(Blocked, &rdev2->flags);
9586 }
9587 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009588 }
9589
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009590 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9591 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009592
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009593 /*
9594 * Since mddev->delta_disks has already updated in update_raid_disks,
9595 * so it is time to check reshape.
9596 */
9597 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9598 (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9599 /*
9600 * reshape is happening in the remote node, we need to
9601 * update reshape_position and call start_reshape.
9602 */
Christoph Hellwiged4d0a4e2019-04-04 18:56:10 +02009603 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009604 if (mddev->pers->update_reshape_pos)
9605 mddev->pers->update_reshape_pos(mddev);
9606 if (mddev->pers->start_reshape)
9607 mddev->pers->start_reshape(mddev);
9608 } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9609 mddev->reshape_position != MaxSector &&
9610 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9611 /* reshape is just done in another node. */
9612 mddev->reshape_position = MaxSector;
9613 if (mddev->pers->update_reshape_pos)
9614 mddev->pers->update_reshape_pos(mddev);
9615 }
9616
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009617 /* Finally set the event to be up to date */
9618 mddev->events = le64_to_cpu(sb->events);
9619}
9620
9621static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9622{
9623 int err;
9624 struct page *swapout = rdev->sb_page;
9625 struct mdp_superblock_1 *sb;
9626
9627 /* Store the sb page of the rdev in the swapout temporary
9628 * variable in case we err in the future
9629 */
9630 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009631 err = alloc_disk_sb(rdev);
9632 if (err == 0) {
9633 ClearPageUptodate(rdev->sb_page);
9634 rdev->sb_loaded = 0;
9635 err = super_types[mddev->major_version].
9636 load_super(rdev, NULL, mddev->minor_version);
9637 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009638 if (err < 0) {
9639 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9640 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009641 if (rdev->sb_page)
9642 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009643 rdev->sb_page = swapout;
9644 rdev->sb_loaded = 1;
9645 return err;
9646 }
9647
9648 sb = page_address(rdev->sb_page);
9649 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9650 * is not set
9651 */
9652
9653 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9654 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9655
9656 /* The other node finished recovery, call spare_active to set
9657 * device In_sync and mddev->degraded
9658 */
9659 if (rdev->recovery_offset == MaxSector &&
9660 !test_bit(In_sync, &rdev->flags) &&
9661 mddev->pers->spare_active(mddev))
9662 sysfs_notify(&mddev->kobj, NULL, "degraded");
9663
9664 put_page(swapout);
9665 return 0;
9666}
9667
9668void md_reload_sb(struct mddev *mddev, int nr)
9669{
9670 struct md_rdev *rdev;
9671 int err;
9672
9673 /* Find the rdev */
9674 rdev_for_each_rcu(rdev, mddev) {
9675 if (rdev->desc_nr == nr)
9676 break;
9677 }
9678
9679 if (!rdev || rdev->desc_nr != nr) {
9680 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9681 return;
9682 }
9683
9684 err = read_rdev(mddev, rdev);
9685 if (err < 0)
9686 return;
9687
9688 check_sb_changes(mddev, rdev);
9689
9690 /* Read all rdev's to update recovery_offset */
Guoqing Jiang0ea99242018-04-09 17:01:21 +08009691 rdev_for_each_rcu(rdev, mddev) {
9692 if (!test_bit(Faulty, &rdev->flags))
9693 read_rdev(mddev, rdev);
9694 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009695}
9696EXPORT_SYMBOL(md_reload_sb);
9697
Linus Torvalds1da177e2005-04-16 15:20:36 -07009698#ifndef MODULE
9699
9700/*
9701 * Searches all registered partitions for autorun RAID arrays
9702 * at boot time.
9703 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009704
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009705static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009706static LIST_HEAD(all_detected_devices);
9707struct detected_devices_node {
9708 struct list_head list;
9709 dev_t dev;
9710};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009711
9712void md_autodetect_dev(dev_t dev)
9713{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009714 struct detected_devices_node *node_detected_dev;
9715
9716 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9717 if (node_detected_dev) {
9718 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009719 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009720 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009721 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009723}
9724
Linus Torvalds1da177e2005-04-16 15:20:36 -07009725static void autostart_arrays(int part)
9726{
NeilBrown3cb03002011-10-11 16:45:26 +11009727 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009728 struct detected_devices_node *node_detected_dev;
9729 dev_t dev;
9730 int i_scanned, i_passed;
9731
9732 i_scanned = 0;
9733 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009734
NeilBrown9d487392016-11-02 14:16:49 +11009735 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009736
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009737 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009738 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9739 i_scanned++;
9740 node_detected_dev = list_entry(all_detected_devices.next,
9741 struct detected_devices_node, list);
9742 list_del(&node_detected_dev->list);
9743 dev = node_detected_dev->dev;
9744 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009745 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009746 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009747 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009748 if (IS_ERR(rdev))
9749 continue;
9750
NeilBrown403df472014-09-30 15:52:29 +10009751 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009752 continue;
NeilBrown403df472014-09-30 15:52:29 +10009753
NeilBrownd0fae182008-03-04 14:29:31 -08009754 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009755 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009756 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009757 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009758 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009759
NeilBrown9d487392016-11-02 14:16:49 +11009760 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009761
9762 autorun_devices(part);
9763}
9764
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009765#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009766
9767static __exit void md_exit(void)
9768{
NeilBrownfd01b882011-10-11 16:47:53 +11009769 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009770 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009771 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009772
NeilBrownaf5628f2014-07-31 13:54:54 +10009773 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009774 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009775
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009776 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009777 unregister_blkdev(mdp_major, "mdp");
9778 unregister_reboot_notifier(&md_notifier);
9779 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009780
9781 /* We cannot unload the modules while some process is
9782 * waiting for us in select() or poll() - wake them up
9783 */
9784 md_unloading = 1;
9785 while (waitqueue_active(&md_event_waiters)) {
9786 /* not safe to leave yet */
9787 wake_up(&md_event_waiters);
9788 msleep(delay);
9789 delay += delay;
9790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009791 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009792
NeilBrown29ac4aa2008-02-06 01:39:58 -08009793 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009794 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009795 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009796 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009797 /*
9798 * for_each_mddev() will call mddev_put() at the end of each
9799 * iteration. As the mddev is now fully clear, this will
9800 * schedule the mddev for destruction by a workqueue, and the
9801 * destroy_workqueue() below will wait for that to complete.
9802 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009803 }
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009804 destroy_workqueue(md_rdev_misc_wq);
Tejun Heoe804ac72010-10-15 15:36:08 +02009805 destroy_workqueue(md_misc_wq);
9806 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009807}
9808
Dan Williams685784a2007-07-09 11:56:42 -07009809subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009810module_exit(md_exit)
9811
Kees Cooke4dca7b2017-10-17 19:04:42 -07009812static int get_ro(char *buffer, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009813{
9814 return sprintf(buffer, "%d", start_readonly);
9815}
Kees Cooke4dca7b2017-10-17 19:04:42 -07009816static int set_ro(const char *val, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009817{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009818 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009819}
9820
NeilBrown80ca3a42006-07-10 04:44:18 -07009821module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9822module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009823module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009824module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009825
Linus Torvalds1da177e2005-04-16 15:20:36 -07009826MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009827MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009828MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009829MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);