blob: 49452149ac7234ace59b2637ed4a874228477f06 [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 &&
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200202 rdev->bdev->bd_disk->queue->nr_hw_queues != 1 &&
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100203 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) {
Coly Li3024ba22020-04-09 22:17:23 +0800231 /*
232 * already in memalloc noio context by
233 * mddev_suspend()
234 */
Guoqing Jiang404659c2019-12-23 10:48:53 +0100235 mddev->serial_info_pool =
236 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
237 sizeof(struct serial_info));
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
Christoph Hellwigc62b37d2020-07-01 10:59:43 +0200466static blk_qc_t md_submit_bio(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));
Christoph Hellwige4fc5a72020-05-08 18:15:14 +0200470 struct mddev *mddev = bio->bi_disk->private_data;
Chris Masone91ece52011-02-07 19:21:48 -0500471 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100472
Colin Ian King9a5a8592020-07-02 12:35:02 +0100473 if (mddev == NULL || mddev->pers == NULL) {
474 bio_io_error(bio);
475 return BLK_QC_T_NONE;
476 }
477
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -0300478 if (unlikely(test_bit(MD_BROKEN, &mddev->flags)) && (rw == WRITE)) {
479 bio_io_error(bio);
480 return BLK_QC_T_NONE;
481 }
482
Christoph Hellwigf695ca32020-07-01 10:59:39 +0200483 blk_queue_split(&bio);
Kent Overstreet54efd502015-04-23 22:37:18 -0700484
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100485 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200486 if (bio_sectors(bio) != 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200487 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200488 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700489 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100490 }
NeilBrown49077322010-03-25 16:20:56 +1100491
Chris Masone91ece52011-02-07 19:21:48 -0500492 /*
493 * save the sectors now since our bio can
494 * go away inside make_request
495 */
496 sectors = bio_sectors(bio);
Shaohua Li9c573de2016-04-25 16:52:38 -0700497 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600498 bio->bi_opf &= ~REQ_NOMERGE;
Shaohua Li393debc2017-09-21 10:23:35 -0700499
500 md_handle_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100501
Mike Snitzer112f1582018-12-06 11:41:18 -0500502 part_stat_lock();
503 part_stat_inc(&mddev->gendisk->part0, ios[sgrp]);
504 part_stat_add(&mddev->gendisk->part0, sectors[sgrp], sectors);
Gu Zheng74672d02015-04-03 08:44:47 +0800505 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100506
Jens Axboedece1632015-11-05 10:41:16 -0700507 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
NeilBrown9e35b992010-04-06 14:23:02 +1000510/* mddev_suspend makes sure no new requests are submitted
511 * to the device, and that any requests that have been submitted
512 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100513 * Once mddev_detach() is called and completes, the module will be
514 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000515 */
NeilBrownfd01b882011-10-11 16:47:53 +1100516void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100517{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200518 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
NeilBrown4d5324f2017-10-19 12:17:16 +1100519 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100520 if (mddev->suspended++)
521 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100522 synchronize_rcu();
NeilBrowncc27b0c2017-06-05 16:49:39 +1000523 wake_up(&mddev->sb_wait);
NeilBrown35bfc522017-10-17 13:46:43 +1100524 set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
525 smp_mb__after_atomic();
NeilBrown409c57f2009-03-31 14:39:39 +1100526 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
527 mddev->pers->quiesce(mddev, 1);
NeilBrown35bfc522017-10-17 13:46:43 +1100528 clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags);
529 wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags));
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500530
531 del_timer_sync(&mddev->safemode_timer);
Coly Li78f57ef2020-04-09 22:17:20 +0800532 /* restrict memory reclaim I/O during raid array is suspend */
533 mddev->noio_flag = memalloc_noio_save();
NeilBrown409c57f2009-03-31 14:39:39 +1100534}
NeilBrown390ee602010-06-01 19:37:27 +1000535EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100536
NeilBrownfd01b882011-10-11 16:47:53 +1100537void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100538{
Coly Li78f57ef2020-04-09 22:17:20 +0800539 /* entred the memalloc scope from mddev_suspend() */
540 memalloc_noio_restore(mddev->noio_flag);
NeilBrown4d5324f2017-10-19 12:17:16 +1100541 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100542 if (--mddev->suspended)
543 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100544 wake_up(&mddev->sb_wait);
545 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500546
Jonathan Brassow47525e52012-05-22 13:55:29 +1000547 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500548 md_wakeup_thread(mddev->thread);
549 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100550}
NeilBrown390ee602010-06-01 19:37:27 +1000551EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100552
NeilBrownfd01b882011-10-11 16:47:53 +1100553int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000554{
NeilBrown5c675f82014-12-15 12:56:56 +1100555 struct md_personality *pers = mddev->pers;
556 int ret = 0;
557
558 rcu_read_lock();
559 if (mddev->suspended)
560 ret = 1;
561 else if (pers && pers->congested)
562 ret = pers->congested(mddev, bits);
563 rcu_read_unlock();
564 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000565}
NeilBrown5c675f82014-12-15 12:56:56 +1100566EXPORT_SYMBOL_GPL(mddev_congested);
567static int md_congested(void *data, int bits)
568{
569 struct mddev *mddev = data;
570 return mddev_congested(mddev, bits);
571}
NeilBrown3fa841d2009-09-23 18:10:29 +1000572
NeilBrowna2826aa2009-12-14 12:49:49 +1100573/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200574 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100575 */
NeilBrown4bc034d2019-03-29 10:46:16 -0700576
577static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100578{
NeilBrown4bc034d2019-03-29 10:46:16 -0700579 struct md_rdev *rdev = bio->bi_private;
580 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100581
582 rdev_dec_pending(rdev, mddev);
583
NeilBrown4bc034d2019-03-29 10:46:16 -0700584 if (atomic_dec_and_test(&mddev->flush_pending)) {
585 /* The pre-request flush has finished */
586 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100587 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700588 bio_put(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100589}
590
NeilBrown4bc034d2019-03-29 10:46:16 -0700591static void md_submit_flush_data(struct work_struct *ws);
592
593static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100594{
NeilBrown4bc034d2019-03-29 10:46:16 -0700595 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100596 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100597
NeilBrown2bc13b82019-03-29 10:46:17 -0700598 mddev->start_flush = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700599 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
600 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100601 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100602 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100603 if (rdev->raid_disk >= 0 &&
604 !test_bit(Faulty, &rdev->flags)) {
605 /* Take two references, one is dropped
606 * when request finishes, one after
607 * we reclaim rcu_read_lock
608 */
609 struct bio *bi;
610 atomic_inc(&rdev->nr_pending);
611 atomic_inc(&rdev->nr_pending);
612 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000613 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Xiao Ni5a409b42018-05-21 11:49:54 +0800614 bi->bi_end_io = md_end_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700615 bi->bi_private = rdev;
616 bio_set_dev(bi, rdev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600617 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
NeilBrown4bc034d2019-03-29 10:46:16 -0700618 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500619 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100620 rcu_read_lock();
621 rdev_dec_pending(rdev, mddev);
622 }
623 rcu_read_unlock();
NeilBrown4bc034d2019-03-29 10:46:16 -0700624 if (atomic_dec_and_test(&mddev->flush_pending))
625 queue_work(md_wq, &mddev->flush_work);
626}
NeilBrowna2826aa2009-12-14 12:49:49 +1100627
NeilBrown4bc034d2019-03-29 10:46:16 -0700628static void md_submit_flush_data(struct work_struct *ws)
629{
630 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
631 struct bio *bio = mddev->flush_bio;
632
633 /*
634 * must reset flush_bio before calling into md_handle_request to avoid a
635 * deadlock, because other bios passed md_handle_request suspend check
636 * could wait for this and below md_handle_request could wait for those
637 * bios because of suspend check
638 */
NeilBrown2bc13b82019-03-29 10:46:17 -0700639 mddev->last_flush = mddev->start_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700640 mddev->flush_bio = NULL;
641 wake_up(&mddev->sb_wait);
642
643 if (bio->bi_iter.bi_size == 0) {
644 /* an empty barrier - all done */
645 bio_endio(bio);
646 } else {
647 bio->bi_opf &= ~REQ_PREFLUSH;
648 md_handle_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100649 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100650}
NeilBrown4bc034d2019-03-29 10:46:16 -0700651
David Jeffery775d7832019-09-16 13:15:14 -0400652/*
653 * Manages consolidation of flushes and submitting any flushes needed for
654 * a bio with REQ_PREFLUSH. Returns true if the bio is finished or is
655 * being finished in another context. Returns false if the flushing is
656 * complete but still needs the I/O portion of the bio to be processed.
657 */
658bool md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrown4bc034d2019-03-29 10:46:16 -0700659{
NeilBrown2bc13b82019-03-29 10:46:17 -0700660 ktime_t start = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700661 spin_lock_irq(&mddev->lock);
662 wait_event_lock_irq(mddev->sb_wait,
NeilBrown2bc13b82019-03-29 10:46:17 -0700663 !mddev->flush_bio ||
664 ktime_after(mddev->last_flush, start),
NeilBrown4bc034d2019-03-29 10:46:16 -0700665 mddev->lock);
NeilBrown2bc13b82019-03-29 10:46:17 -0700666 if (!ktime_after(mddev->last_flush, start)) {
667 WARN_ON(mddev->flush_bio);
668 mddev->flush_bio = bio;
669 bio = NULL;
670 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700671 spin_unlock_irq(&mddev->lock);
672
NeilBrown2bc13b82019-03-29 10:46:17 -0700673 if (!bio) {
674 INIT_WORK(&mddev->flush_work, submit_flushes);
675 queue_work(md_wq, &mddev->flush_work);
676 } else {
677 /* flush was performed for some other bio while we waited. */
678 if (bio->bi_iter.bi_size == 0)
679 /* an empty barrier - all done */
680 bio_endio(bio);
681 else {
682 bio->bi_opf &= ~REQ_PREFLUSH;
David Jeffery775d7832019-09-16 13:15:14 -0400683 return false;
NeilBrown2bc13b82019-03-29 10:46:17 -0700684 }
685 }
David Jeffery775d7832019-09-16 13:15:14 -0400686 return true;
NeilBrown4bc034d2019-03-29 10:46:16 -0700687}
Tejun Heoe9c74692010-09-03 11:56:18 +0200688EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100689
NeilBrownfd01b882011-10-11 16:47:53 +1100690static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 atomic_inc(&mddev->active);
693 return mddev;
694}
695
Dan Williams5fd3a172009-03-04 00:57:25 -0700696static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100697
NeilBrownfd01b882011-10-11 16:47:53 +1100698static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
701 return;
NeilBrownd3374822009-01-09 08:31:10 +1100702 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100703 mddev->ctime == 0 && !mddev->hold_active) {
704 /* Array is not configured at all, and not held active,
705 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100706 list_del_init(&mddev->all_mddevs);
Kent Overstreet28dec872018-06-07 20:52:54 -0400707
708 /*
709 * Call queue_work inside the spinlock so that
710 * flush_workqueue() after mddev_find will succeed in waiting
711 * for the work to be done.
712 */
713 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
714 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100715 }
716 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Kees Cook8376d3c2017-10-16 17:01:48 -0700719static void md_safemode_timeout(struct timer_list *t);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400720
NeilBrownfd01b882011-10-11 16:47:53 +1100721void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100722{
Kent Overstreet28dec872018-06-07 20:52:54 -0400723 kobject_init(&mddev->kobj, &md_ktype);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100724 mutex_init(&mddev->open_mutex);
725 mutex_init(&mddev->reconfig_mutex);
726 mutex_init(&mddev->bitmap_info.mutex);
727 INIT_LIST_HEAD(&mddev->disks);
728 INIT_LIST_HEAD(&mddev->all_mddevs);
Kees Cook8376d3c2017-10-16 17:01:48 -0700729 timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100730 atomic_set(&mddev->active, 1);
731 atomic_set(&mddev->openers, 0);
732 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100733 spin_lock_init(&mddev->lock);
NeilBrown4bc034d2019-03-29 10:46:16 -0700734 atomic_set(&mddev->flush_pending, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100735 init_waitqueue_head(&mddev->sb_wait);
736 init_waitqueue_head(&mddev->recovery_wait);
737 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000738 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500739 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100740 mddev->resync_min = 0;
741 mddev->resync_max = MaxSector;
742 mddev->level = LEVEL_NONE;
743}
NeilBrown390ee602010-06-01 19:37:27 +1000744EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100745
NeilBrownf72ffdd2014-09-30 14:23:59 +1000746static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
NeilBrownfd01b882011-10-11 16:47:53 +1100748 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
NeilBrown8f5f02c2011-02-16 13:58:51 +1100750 if (unit && MAJOR(unit) != MD_MAJOR)
751 unit &= ~((1<<MdpMinorShift)-1);
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 retry:
754 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
NeilBrownefeb53c2009-01-09 08:31:10 +1100756 if (unit) {
757 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
758 if (mddev->unit == unit) {
759 mddev_get(mddev);
760 spin_unlock(&all_mddevs_lock);
761 kfree(new);
762 return mddev;
763 }
764
765 if (new) {
766 list_add(&new->all_mddevs, &all_mddevs);
767 spin_unlock(&all_mddevs_lock);
768 new->hold_active = UNTIL_IOCTL;
769 return new;
770 }
771 } else if (new) {
772 /* find an unused unit number */
773 static int next_minor = 512;
774 int start = next_minor;
775 int is_free = 0;
776 int dev = 0;
777 while (!is_free) {
778 dev = MKDEV(MD_MAJOR, next_minor);
779 next_minor++;
780 if (next_minor > MINORMASK)
781 next_minor = 0;
782 if (next_minor == start) {
783 /* Oh dear, all in use. */
784 spin_unlock(&all_mddevs_lock);
785 kfree(new);
786 return NULL;
787 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000788
NeilBrownefeb53c2009-01-09 08:31:10 +1100789 is_free = 1;
790 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
791 if (mddev->unit == dev) {
792 is_free = 0;
793 break;
794 }
795 }
796 new->unit = dev;
797 new->md_minor = MINOR(dev);
798 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 list_add(&new->all_mddevs, &all_mddevs);
800 spin_unlock(&all_mddevs_lock);
801 return new;
802 }
803 spin_unlock(&all_mddevs_lock);
804
NeilBrown9ffae0c2006-01-06 00:20:32 -0800805 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (!new)
807 return NULL;
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 new->unit = unit;
810 if (MAJOR(unit) == MD_MAJOR)
811 new->md_minor = MINOR(unit);
812 else
813 new->md_minor = MINOR(unit) >> MdpMinorShift;
814
NeilBrownfafd7fb2010-04-01 15:55:30 +1100815 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 goto retry;
818}
819
NeilBrownb6eb1272010-04-15 10:13:47 +1000820static struct attribute_group md_redundancy_group;
821
NeilBrown5c47daf2014-12-15 12:57:01 +1100822void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
NeilBrowna64c8762010-04-14 17:15:37 +1000824 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000825 /* These cannot be removed under reconfig_mutex as
826 * an access to the files will try to take reconfig_mutex
827 * while holding the file unremovable, which leads to
828 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000829 * So hold set sysfs_active while the remove in happeing,
830 * and anything else which might set ->to_remove or my
831 * otherwise change the sysfs namespace will fail with
832 * -EBUSY if sysfs_active is still set.
833 * We set sysfs_active under reconfig_mutex and elsewhere
834 * test it under the same mutex to ensure its correct value
835 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000836 */
NeilBrowna64c8762010-04-14 17:15:37 +1000837 struct attribute_group *to_remove = mddev->to_remove;
838 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000839 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000840 mutex_unlock(&mddev->reconfig_mutex);
841
NeilBrown00bcb4a2010-06-01 19:37:23 +1000842 if (mddev->kobj.sd) {
843 if (to_remove != &md_redundancy_group)
844 sysfs_remove_group(&mddev->kobj, to_remove);
845 if (mddev->pers == NULL ||
846 mddev->pers->sync_request == NULL) {
847 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
848 if (mddev->sysfs_action)
849 sysfs_put(mddev->sysfs_action);
850 mddev->sysfs_action = NULL;
851 }
NeilBrowna64c8762010-04-14 17:15:37 +1000852 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000853 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000854 } else
855 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Chris Dunlop751e67c2011-10-19 16:48:26 +1100857 /* As we've dropped the mutex we need a spinlock to
858 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000859 */
860 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700861 md_wakeup_thread(mddev->thread);
NeilBrown4d5324f2017-10-19 12:17:16 +1100862 wake_up(&mddev->sb_wait);
NeilBrown01f96c02011-09-21 15:30:20 +1000863 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
NeilBrown5c47daf2014-12-15 12:57:01 +1100865EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500867struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100868{
869 struct md_rdev *rdev;
870
871 rdev_for_each_rcu(rdev, mddev)
872 if (rdev->desc_nr == nr)
873 return rdev;
874
875 return NULL;
876}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500877EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100878
879static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
NeilBrown3cb03002011-10-11 16:45:26 +1100881 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
NeilBrowndafb20f2012-03-19 12:46:39 +1100883 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (rdev->bdev->bd_dev == dev)
885 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return NULL;
888}
889
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100890struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev)
NeilBrown1ca69c42012-10-11 13:37:33 +1100891{
892 struct md_rdev *rdev;
893
894 rdev_for_each_rcu(rdev, mddev)
895 if (rdev->bdev->bd_dev == dev)
896 return rdev;
897
898 return NULL;
899}
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100900EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100901
NeilBrown84fc4b52011-10-11 16:49:58 +1100902static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800903{
NeilBrown84fc4b52011-10-11 16:49:58 +1100904 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800905 list_for_each_entry(pers, &pers_list, list) {
906 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800907 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800908 if (strcmp(pers->name, clevel)==0)
909 return pers;
910 }
NeilBrown2604b702006-01-06 00:20:36 -0800911 return NULL;
912}
913
Andre Nollb73df2d2008-07-11 22:02:23 +1000914/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100915static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100917 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000918 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
NeilBrownf72ffdd2014-09-30 14:23:59 +1000921static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100924 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000925 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return 0;
927}
928
NeilBrown545c8792012-05-22 13:54:30 +1000929void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800932 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 rdev->sb_loaded = 0;
934 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000935 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100936 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
NeilBrown2699b672011-07-28 11:31:47 +1000938 if (rdev->bb_page) {
939 put_page(rdev->bb_page);
940 rdev->bb_page = NULL;
941 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800942 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
NeilBrown545c8792012-05-22 13:54:30 +1000944EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200946static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700947{
NeilBrown3cb03002011-10-11 16:45:26 +1100948 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100949 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700950
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200951 if (bio->bi_status) {
952 pr_err("md: super_written gets error=%d\n", bio->bi_status);
NeilBrowna9701a32005-11-08 21:39:34 -0800953 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100954 if (!test_bit(Faulty, &rdev->flags)
955 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800956 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100957 set_bit(LastDev, &rdev->flags);
958 }
959 } else
960 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700961
NeilBrowna9701a32005-11-08 21:39:34 -0800962 if (atomic_dec_and_test(&mddev->pending_writes))
963 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700964 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700965 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700966}
967
NeilBrownfd01b882011-10-11 16:47:53 +1100968void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700969 sector_t sector, int size, struct page *page)
970{
971 /* write first size bytes of page to sector of rdev
972 * Increment mddev->pending_writes before returning
973 * and decrement it on completion, waking up sb_wait
974 * if zero is reached.
975 * If an error occurred, call md_error
976 */
NeilBrown46533ff2016-11-18 16:16:11 +1100977 struct bio *bio;
978 int ff = 0;
979
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +0100980 if (!page)
981 return;
982
NeilBrown46533ff2016-11-18 16:16:11 +1100983 if (test_bit(Faulty, &rdev->flags))
984 return;
985
NeilBrown5a850712017-06-21 09:12:21 +1000986 bio = md_bio_alloc_sync(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700987
Shaohua Lied3b98c2016-03-29 14:00:19 -0700988 atomic_inc(&rdev->nr_pending);
989
Christoph Hellwig74d46992017-08-23 19:10:32 +0200990 bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700991 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700992 bio_add_page(bio, page, size, 0);
993 bio->bi_private = rdev;
994 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +1100995
996 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
997 test_bit(FailFast, &rdev->flags) &&
998 !test_bit(LastDev, &rdev->flags))
999 ff = MD_FAILFAST;
Jan Kara5a8948f2017-05-31 09:44:33 +02001000 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -08001001
NeilBrown7bfa19f2005-06-21 17:17:28 -07001002 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -05001003 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -08001004}
1005
NeilBrown46533ff2016-11-18 16:16:11 +11001006int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -08001007{
Tejun Heoe9c74692010-09-03 11:56:18 +02001008 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +10001009 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -08001010 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +11001011 return -EAGAIN;
1012 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001013}
1014
NeilBrown3cb03002011-10-11 16:45:26 +11001015int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -05001016 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
NeilBrown5a850712017-06-21 09:12:21 +10001018 struct bio *bio = md_bio_alloc_sync(rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 int ret;
1020
Christoph Hellwig74d46992017-08-23 19:10:32 +02001021 if (metadata_op && rdev->meta_bdev)
1022 bio_set_dev(bio, rdev->meta_bdev);
1023 else
1024 bio_set_dev(bio, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -05001025 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001026 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -07001027 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +10001028 else if (rdev->mddev->reshape_position != MaxSector &&
1029 (rdev->mddev->reshape_backwards ==
1030 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001031 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001032 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001033 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -05001035
1036 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001038 ret = !bio->bi_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 bio_put(bio);
1040 return ret;
1041}
NeilBrowna8745db2006-01-06 00:20:34 -08001042EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
NeilBrownf72ffdd2014-09-30 14:23:59 +10001044static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (rdev->sb_loaded)
1049 return 0;
1050
Mike Christie796a5cf2016-06-05 14:32:07 -05001051 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 goto fail;
1053 rdev->sb_loaded = 1;
1054 return 0;
1055
1056fail:
NeilBrown9d487392016-11-02 14:16:49 +11001057 pr_err("md: disabled device %s, could not read superblock.\n",
1058 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return -EINVAL;
1060}
1061
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001062static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
NeilBrownf72ffdd2014-09-30 14:23:59 +10001064 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +10001065 sb1->set_uuid1 == sb2->set_uuid1 &&
1066 sb1->set_uuid2 == sb2->set_uuid2 &&
1067 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001070static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 int ret;
1073 mdp_super_t *tmp1, *tmp2;
1074
1075 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
1076 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
1077
1078 if (!tmp1 || !tmp2) {
1079 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 goto abort;
1081 }
1082
1083 *tmp1 = *sb1;
1084 *tmp2 = *sb2;
1085
1086 /*
1087 * nr_disks is not constant
1088 */
1089 tmp1->nr_disks = 0;
1090 tmp2->nr_disks = 0;
1091
Andre Nollce0c8e02008-07-11 22:02:20 +10001092 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001094 kfree(tmp1);
1095 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return ret;
1097}
1098
NeilBrown4d167f02007-05-09 02:35:37 -07001099static u32 md_csum_fold(u32 csum)
1100{
1101 csum = (csum & 0xffff) + (csum >> 16);
1102 return (csum & 0xffff) + (csum >> 16);
1103}
1104
NeilBrownf72ffdd2014-09-30 14:23:59 +10001105static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
NeilBrown4d167f02007-05-09 02:35:37 -07001107 u64 newcsum = 0;
1108 u32 *sb32 = (u32*)sb;
1109 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 unsigned int disk_csum, csum;
1111
1112 disk_csum = sb->sb_csum;
1113 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -07001114
1115 for (i = 0; i < MD_SB_BYTES/4 ; i++)
1116 newcsum += sb32[i];
1117 csum = (newcsum & 0xffffffff) + (newcsum>>32);
1118
NeilBrown4d167f02007-05-09 02:35:37 -07001119#ifdef CONFIG_ALPHA
1120 /* This used to use csum_partial, which was wrong for several
1121 * reasons including that different results are returned on
1122 * different architectures. It isn't critical that we get exactly
1123 * the same return value as before (we always csum_fold before
1124 * testing, and that removes any differences). However as we
1125 * know that csum_partial always returned a 16bit value on
1126 * alphas, do a fold to maximise conformity to previous behaviour.
1127 */
1128 sb->sb_csum = md_csum_fold(disk_csum);
1129#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -07001131#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return csum;
1133}
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135/*
1136 * Handle superblock details.
1137 * We want to be able to handle multiple superblock formats
1138 * so we have a common interface to them all, and an array of
1139 * different handlers.
1140 * We rely on user-space to write the initial superblock, and support
1141 * reading and updating of superblocks.
1142 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +11001143 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 * loads and validates a superblock on dev.
1145 * if refdev != NULL, compare superblocks on both devices
1146 * Return:
1147 * 0 - dev has a superblock that is compatible with refdev
1148 * 1 - dev has a superblock that is compatible and newer than refdev
1149 * so dev should be used as the refdev in future
1150 * -EINVAL superblock incompatible or invalid
1151 * -othererror e.g. -EIO
1152 *
NeilBrownfd01b882011-10-11 16:47:53 +11001153 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 * Verify that dev is acceptable into mddev.
1155 * The first time, mddev->raid_disks will be 0, and data from
1156 * dev should be merged in. Subsequent calls check that dev
1157 * is new enough. Return 0 or -EINVAL
1158 *
NeilBrownfd01b882011-10-11 16:47:53 +11001159 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 * Update the superblock for rdev with data in mddev
1161 * This does not write to disc.
1162 *
1163 */
1164
1165struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001166 char *name;
1167 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +10001168 int (*load_super)(struct md_rdev *rdev,
1169 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001170 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +10001171 int (*validate_super)(struct mddev *mddev,
1172 struct md_rdev *rdev);
1173 void (*sync_super)(struct mddev *mddev,
1174 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001175 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001176 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +10001177 int (*allow_new_offset)(struct md_rdev *rdev,
1178 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179};
1180
1181/*
Andre Noll0894cc32009-06-18 08:49:23 +10001182 * Check that the given mddev has no bitmap.
1183 *
1184 * This function is called from the run method of all personalities that do not
1185 * support bitmaps. It prints an error message and returns non-zero if mddev
1186 * has a bitmap. Otherwise, it returns 0.
1187 *
1188 */
NeilBrownfd01b882011-10-11 16:47:53 +11001189int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001190{
NeilBrownc3d97142009-12-14 12:49:52 +11001191 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001192 return 0;
NeilBrown9d487392016-11-02 14:16:49 +11001193 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +10001194 mdname(mddev), mddev->pers->name);
1195 return 1;
1196}
1197EXPORT_SYMBOL(md_check_no_bitmap);
1198
1199/*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001200 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 */
NeilBrown3cb03002011-10-11 16:45:26 +11001202static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1205 mdp_super_t *sb;
1206 int ret;
Yufen Yu228fc7d2019-10-30 18:47:02 +08001207 bool spare_disk = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 /*
Andre Noll0f420352008-07-11 22:02:23 +10001210 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 * it's at the end of the disk.
1212 *
1213 * It also happens to be a multiple of 4Kb.
1214 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001215 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
NeilBrown0002b272005-09-09 16:23:53 -07001217 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +11001218 if (ret)
1219 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 ret = -EINVAL;
1222
1223 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001224 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +11001227 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 goto abort;
1229 }
1230
1231 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001232 sb->minor_version < 90 ||
1233 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +11001234 pr_warn("Bad version number %d.%d on %s\n",
1235 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 goto abort;
1237 }
1238
1239 if (sb->raid_disks <= 0)
1240 goto abort;
1241
NeilBrown4d167f02007-05-09 02:35:37 -07001242 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001243 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 goto abort;
1245 }
1246
1247 rdev->preferred_minor = sb->md_minor;
1248 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001249 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001250 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001251 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 if (sb->level == LEVEL_MULTIPATH)
1254 rdev->desc_nr = -1;
1255 else
1256 rdev->desc_nr = sb->this_disk.number;
1257
Yufen Yu228fc7d2019-10-30 18:47:02 +08001258 /* not spare disk, or LEVEL_MULTIPATH */
1259 if (sb->level == LEVEL_MULTIPATH ||
1260 (rdev->desc_nr >= 0 &&
Yufen Yu3b7436c2019-12-10 15:01:29 +08001261 rdev->desc_nr < MD_SB_DISKS &&
Yufen Yu228fc7d2019-10-30 18:47:02 +08001262 sb->disks[rdev->desc_nr].state &
1263 ((1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))))
1264 spare_disk = false;
1265
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001266 if (!refdev) {
Yufen Yu228fc7d2019-10-30 18:47:02 +08001267 if (!spare_disk)
Yufen Yu6a5cb532019-10-16 16:00:03 +08001268 ret = 1;
1269 else
1270 ret = 0;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001271 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001273 mdp_super_t *refsb = page_address(refdev->sb_page);
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001274 if (!md_uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001275 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 b, bdevname(refdev->bdev,b2));
1277 goto abort;
1278 }
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001279 if (!md_sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001280 pr_warn("md: %s has same UUID but different superblock to %s\n",
1281 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 goto abort;
1283 }
1284 ev1 = md_event(sb);
1285 ev2 = md_event(refsb);
Yufen Yu6a5cb532019-10-16 16:00:03 +08001286
Yufen Yu228fc7d2019-10-30 18:47:02 +08001287 if (!spare_disk && ev1 > ev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001289 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 ret = 0;
1291 }
NeilBrown8190e752009-06-18 08:48:58 +10001292 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001293 /* Limit to 4TB as metadata cannot record more than that.
1294 * (not needed for Linear and RAID0 as metadata doesn't
1295 * record this size)
1296 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001297 if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001298 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
NeilBrown27a7b262011-09-10 17:21:28 +10001300 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001301 /* "this cannot possibly happen" ... */
1302 ret = -EINVAL;
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 abort:
1305 return ret;
1306}
1307
1308/*
1309 * validate_super for 0.90.0
1310 */
NeilBrownfd01b882011-10-11 16:47:53 +11001311static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001314 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001315 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
NeilBrown41158c72005-06-21 17:17:25 -07001317 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001318 clear_bit(Faulty, &rdev->flags);
1319 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001320 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001321 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (mddev->raid_disks == 0) {
1324 mddev->major_version = 0;
1325 mddev->minor_version = sb->minor_version;
1326 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001327 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001328 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 mddev->ctime = sb->ctime;
1330 mddev->utime = sb->utime;
1331 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001332 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 mddev->layout = sb->layout;
1334 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001335 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001336 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001337 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001338 mddev->bitmap_info.space = 0;
1339 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001340 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001341 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001342 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
NeilBrownf6705572006-03-27 01:18:11 -08001344 if (mddev->minor_version >= 91) {
1345 mddev->reshape_position = sb->reshape_position;
1346 mddev->delta_disks = sb->delta_disks;
1347 mddev->new_level = sb->new_level;
1348 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001349 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001350 if (mddev->delta_disks < 0)
1351 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001352 } else {
1353 mddev->reshape_position = MaxSector;
1354 mddev->delta_disks = 0;
1355 mddev->new_level = mddev->level;
1356 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001357 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001358 }
NeilBrown33f2c352019-09-09 16:52:29 +10001359 if (mddev->level == 0)
1360 mddev->layout = -1;
NeilBrownf6705572006-03-27 01:18:11 -08001361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (sb->state & (1<<MD_SB_CLEAN))
1363 mddev->recovery_cp = MaxSector;
1364 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001365 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 sb->events_lo == sb->cp_events_lo) {
1367 mddev->recovery_cp = sb->recovery_cp;
1368 } else
1369 mddev->recovery_cp = 0;
1370 }
1371
1372 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1373 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1374 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1375 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1376
1377 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001378
1379 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001380 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001381 mddev->bitmap_info.offset =
1382 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001383 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001384 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001385 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001386
NeilBrown41158c72005-06-21 17:17:25 -07001387 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001388 /* Insist on good event counter while assembling, except
1389 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001391 if (sb->disks[rdev->desc_nr].state & (
1392 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001393 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001394 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001395 } else if (mddev->bitmap) {
1396 /* if adding to array with a bitmap, then we can accept an
1397 * older device ... but not too old.
1398 */
NeilBrown41158c72005-06-21 17:17:25 -07001399 if (ev1 < mddev->bitmap->events_cleared)
1400 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001401 if (ev1 < mddev->events)
1402 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001403 } else {
1404 if (ev1 < mddev->events)
1405 /* just a hot-add of a new device, leave raid_disk at -1 */
1406 return 0;
1407 }
NeilBrown41158c72005-06-21 17:17:25 -07001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 desc = sb->disks + rdev->desc_nr;
1411
1412 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001413 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001414 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1415 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001416 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001418 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001419 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1420 /* active but not in sync implies recovery up to
1421 * reshape position. We don't know exactly where
1422 * that is, so set to zero for now */
1423 if (mddev->minor_version >= 91) {
1424 rdev->recovery_offset = 0;
1425 rdev->raid_disk = desc->raid_disk;
1426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001428 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1429 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001430 if (desc->state & (1<<MD_DISK_FAILFAST))
1431 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001432 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001433 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return 0;
1435}
1436
1437/*
1438 * sync_super for 0.90.0
1439 */
NeilBrownfd01b882011-10-11 16:47:53 +11001440static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001443 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 /* make rdev->sb match mddev data..
1447 *
1448 * 1/ zero out disks
1449 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1450 * 3/ any empty disks < next_spare become removed
1451 *
1452 * disks[0] gets initialised to REMOVED because
1453 * we cannot be sure from other fields if it has
1454 * been initialised or not.
1455 */
1456 int i;
1457 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1458
NeilBrown61181562005-09-09 16:24:02 -07001459 rdev->sb_size = MD_SB_BYTES;
1460
Namhyung Kim65a06f062011-07-27 11:00:36 +10001461 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 memset(sb, 0, sizeof(*sb));
1464
1465 sb->md_magic = MD_SB_MAGIC;
1466 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 sb->patch_version = mddev->patch_version;
1468 sb->gvalid_words = 0; /* ignored */
1469 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1470 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1471 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1472 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1473
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001474 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001476 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 sb->raid_disks = mddev->raid_disks;
1478 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001479 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001480 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 sb->state = 0;
1482 sb->events_hi = (mddev->events>>32);
1483 sb->events_lo = (u32)mddev->events;
1484
NeilBrownf6705572006-03-27 01:18:11 -08001485 if (mddev->reshape_position == MaxSector)
1486 sb->minor_version = 90;
1487 else {
1488 sb->minor_version = 91;
1489 sb->reshape_position = mddev->reshape_position;
1490 sb->new_level = mddev->new_level;
1491 sb->delta_disks = mddev->delta_disks;
1492 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001493 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001494 }
1495 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (mddev->in_sync)
1497 {
1498 sb->recovery_cp = mddev->recovery_cp;
1499 sb->cp_events_hi = (mddev->events>>32);
1500 sb->cp_events_lo = (u32)mddev->events;
1501 if (mddev->recovery_cp == MaxSector)
1502 sb->state = (1<< MD_SB_CLEAN);
1503 } else
1504 sb->recovery_cp = 0;
1505
1506 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001507 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
NeilBrownc3d97142009-12-14 12:49:52 +11001509 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001510 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001513 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001515 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001516 int is_active = test_bit(In_sync, &rdev2->flags);
1517
1518 if (rdev2->raid_disk >= 0 &&
1519 sb->minor_version >= 91)
1520 /* we have nowhere to store the recovery_offset,
1521 * but if it is not below the reshape_position,
1522 * we can piggy-back on that.
1523 */
1524 is_active = 1;
1525 if (rdev2->raid_disk < 0 ||
1526 test_bit(Faulty, &rdev2->flags))
1527 is_active = 0;
1528 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001529 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001531 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001532 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 d = &sb->disks[rdev2->desc_nr];
1534 nr_disks++;
1535 d->number = rdev2->desc_nr;
1536 d->major = MAJOR(rdev2->bdev->bd_dev);
1537 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001538 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 d->raid_disk = rdev2->raid_disk;
1540 else
1541 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001542 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001544 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001546 if (test_bit(In_sync, &rdev2->flags))
1547 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 active++;
1549 working++;
1550 } else {
1551 d->state = 0;
1552 spare++;
1553 working++;
1554 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001555 if (test_bit(WriteMostly, &rdev2->flags))
1556 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001557 if (test_bit(FailFast, &rdev2->flags))
1558 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 /* now set the "removed" and "faulty" bits on any missing devices */
1561 for (i=0 ; i < mddev->raid_disks ; i++) {
1562 mdp_disk_t *d = &sb->disks[i];
1563 if (d->state == 0 && d->number == 0) {
1564 d->number = i;
1565 d->raid_disk = i;
1566 d->state = (1<<MD_DISK_REMOVED);
1567 d->state |= (1<<MD_DISK_FAULTY);
1568 failed++;
1569 }
1570 }
1571 sb->nr_disks = nr_disks;
1572 sb->active_disks = active;
1573 sb->working_disks = working;
1574 sb->failed_disks = failed;
1575 sb->spare_disks = spare;
1576
1577 sb->this_disk = sb->disks[rdev->desc_nr];
1578 sb->sb_csum = calc_sb_csum(sb);
1579}
1580
1581/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001582 * rdev_size_change for 0.90.0
1583 */
1584static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001585super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001586{
Andre Noll58c0fed2009-03-31 14:33:13 +11001587 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001588 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001589 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001590 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001591 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001592 if (!num_sectors || num_sectors > rdev->sb_start)
1593 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001594 /* Limit to 4TB as metadata cannot record more than that.
1595 * 4TB == 2^32 KB, or 2*2^32 sectors.
1596 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001597 if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001598 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001599 do {
1600 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001601 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001602 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001603 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001604}
1605
NeilBrownc6563a82012-05-21 09:27:00 +10001606static int
1607super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1608{
1609 /* non-zero offset changes not possible with v0.90 */
1610 return new_offset == 0;
1611}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001612
1613/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 * version 1 superblock
1615 */
1616
NeilBrownf72ffdd2014-09-30 14:23:59 +10001617static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001619 __le32 disk_csum;
1620 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 unsigned long long newcsum;
1622 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001623 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 disk_csum = sb->sb_csum;
1626 sb->sb_csum = 0;
1627 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001628 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 newcsum += le32_to_cpu(*isuper++);
1630
1631 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001632 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1635 sb->sb_csum = disk_csum;
1636 return cpu_to_le32(csum);
1637}
1638
NeilBrown3cb03002011-10-11 16:45:26 +11001639static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 struct mdp_superblock_1 *sb;
1642 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001643 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001644 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001646 int bmask;
Yufen Yu228fc7d2019-10-30 18:47:02 +08001647 bool spare_disk = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 /*
Andre Noll0f420352008-07-11 22:02:23 +10001650 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 * It is always aligned to a 4K boundary and
1652 * depeding on minor_version, it can be:
1653 * 0: At least 8K, but less than 12K, from end of device
1654 * 1: At start of device
1655 * 2: 4K from start of device.
1656 */
1657 switch(minor_version) {
1658 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001659 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001660 sb_start -= 8*2;
1661 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 break;
1663 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001664 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 break;
1666 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001667 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 break;
1669 default:
1670 return -EINVAL;
1671 }
Andre Noll0f420352008-07-11 22:02:23 +10001672 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
NeilBrown0002b272005-09-09 16:23:53 -07001674 /* superblock is rarely larger than 1K, but it can be larger,
1675 * and it is safe to read 4k, so we do that
1676 */
1677 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (ret) return ret;
1679
Namhyung Kim65a06f062011-07-27 11:00:36 +10001680 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1683 sb->major_version != cpu_to_le32(1) ||
1684 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001685 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001686 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return -EINVAL;
1688
1689 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001690 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 bdevname(rdev->bdev,b));
1692 return -EINVAL;
1693 }
1694 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001695 pr_warn("md: data_size too small on %s\n",
1696 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return -EINVAL;
1698 }
NeilBrownc6563a82012-05-21 09:27:00 +10001699 if (sb->pad0 ||
1700 sb->pad3[0] ||
1701 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1702 /* Some padding is non-zero, might be a new feature */
1703 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 rdev->preferred_minor = 0xffff;
1706 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001707 rdev->new_data_offset = rdev->data_offset;
1708 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1709 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1710 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001711 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
NeilBrown0002b272005-09-09 16:23:53 -07001713 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001714 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001715 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001716 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1717
1718 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001719 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001720 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001721 if (minor_version
1722 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1723 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001724
NeilBrown31b65a02006-07-10 04:44:14 -07001725 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1726 rdev->desc_nr = -1;
1727 else
1728 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1729
NeilBrown2699b672011-07-28 11:31:47 +10001730 if (!rdev->bb_page) {
1731 rdev->bb_page = alloc_page(GFP_KERNEL);
1732 if (!rdev->bb_page)
1733 return -ENOMEM;
1734 }
1735 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1736 rdev->badblocks.count == 0) {
1737 /* need to load the bad block list.
1738 * Currently we limit it to one page.
1739 */
1740 s32 offset;
1741 sector_t bb_sector;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001742 __le64 *bbp;
NeilBrown2699b672011-07-28 11:31:47 +10001743 int i;
1744 int sectors = le16_to_cpu(sb->bblog_size);
1745 if (sectors > (PAGE_SIZE / 512))
1746 return -EINVAL;
1747 offset = le32_to_cpu(sb->bblog_offset);
1748 if (offset == 0)
1749 return -EINVAL;
1750 bb_sector = (long long)offset;
1751 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001752 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001753 return -EIO;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001754 bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10001755 rdev->badblocks.shift = sb->bblog_shift;
1756 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1757 u64 bb = le64_to_cpu(*bbp);
1758 int count = bb & (0x3ff);
1759 u64 sector = bb >> 10;
1760 sector <<= sb->bblog_shift;
1761 count <<= sb->bblog_shift;
1762 if (bb + 1 == 0)
1763 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001764 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001765 return -EINVAL;
1766 }
NeilBrown486adf72013-04-24 11:42:44 +10001767 } else if (sb->bblog_offset != 0)
1768 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001769
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001770 if ((le32_to_cpu(sb->feature_map) &
1771 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001772 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1773 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1774 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1775 }
1776
NeilBrown33f2c352019-09-09 16:52:29 +10001777 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT) &&
1778 sb->level != 0)
1779 return -EINVAL;
1780
Yufen Yu228fc7d2019-10-30 18:47:02 +08001781 /* not spare disk, or LEVEL_MULTIPATH */
1782 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH) ||
1783 (rdev->desc_nr >= 0 &&
1784 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1785 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1786 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)))
1787 spare_disk = false;
Yufen Yu6a5cb532019-10-16 16:00:03 +08001788
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001789 if (!refdev) {
Yufen Yu228fc7d2019-10-30 18:47:02 +08001790 if (!spare_disk)
Yufen Yu6a5cb532019-10-16 16:00:03 +08001791 ret = 1;
1792 else
1793 ret = 0;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001794 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001796 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1799 sb->level != refsb->level ||
1800 sb->layout != refsb->layout ||
1801 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001802 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 bdevname(rdev->bdev,b),
1804 bdevname(refdev->bdev,b2));
1805 return -EINVAL;
1806 }
1807 ev1 = le64_to_cpu(sb->events);
1808 ev2 = le64_to_cpu(refsb->events);
1809
Yufen Yu228fc7d2019-10-30 18:47:02 +08001810 if (!spare_disk && ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001811 ret = 1;
1812 else
1813 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 }
NeilBrownc6563a82012-05-21 09:27:00 +10001815 if (minor_version) {
1816 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1817 sectors -= rdev->data_offset;
1818 } else
1819 sectors = rdev->sb_start;
1820 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001822 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001823 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
1825
NeilBrownfd01b882011-10-11 16:47:53 +11001826static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001828 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001829 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
NeilBrown41158c72005-06-21 17:17:25 -07001831 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001832 clear_bit(Faulty, &rdev->flags);
1833 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001834 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001835 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (mddev->raid_disks == 0) {
1838 mddev->major_version = 1;
1839 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001840 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001841 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001842 mddev->ctime = le64_to_cpu(sb->ctime);
1843 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001845 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 mddev->layout = le32_to_cpu(sb->layout);
1847 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001848 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001849 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001850 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001851 mddev->bitmap_info.space = 0;
1852 /* Default location for bitmap is 1K after superblock
1853 * using 3K - total of 4K
1854 */
NeilBrownc3d97142009-12-14 12:49:52 +11001855 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001856 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001857 mddev->reshape_backwards = 0;
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1860 memcpy(mddev->uuid, sb->set_uuid, 16);
1861
1862 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001863
NeilBrown71c08052005-09-09 16:23:51 -07001864 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001865 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001866 mddev->bitmap_info.offset =
1867 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001868 /* Metadata doesn't record how much space is available.
1869 * For 1.0, we assume we can use up to the superblock
1870 * if before, else to 4K beyond superblock.
1871 * For others, assume no change is possible.
1872 */
1873 if (mddev->minor_version > 0)
1874 mddev->bitmap_info.space = 0;
1875 else if (mddev->bitmap_info.offset > 0)
1876 mddev->bitmap_info.space =
1877 8 - mddev->bitmap_info.offset;
1878 else
1879 mddev->bitmap_info.space =
1880 -mddev->bitmap_info.offset;
1881 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001882
NeilBrownf6705572006-03-27 01:18:11 -08001883 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1884 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1885 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1886 mddev->new_level = le32_to_cpu(sb->new_level);
1887 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001888 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001889 if (mddev->delta_disks < 0 ||
1890 (mddev->delta_disks == 0 &&
1891 (le32_to_cpu(sb->feature_map)
1892 & MD_FEATURE_RESHAPE_BACKWARDS)))
1893 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001894 } else {
1895 mddev->reshape_position = MaxSector;
1896 mddev->delta_disks = 0;
1897 mddev->new_level = mddev->level;
1898 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001899 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001900 }
1901
NeilBrown33f2c352019-09-09 16:52:29 +10001902 if (mddev->level == 0 &&
1903 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT))
1904 mddev->layout = -1;
1905
Song Liu486b0f72016-08-19 15:34:01 -07001906 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001907 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001908
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001909 if (le32_to_cpu(sb->feature_map) &
1910 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001911 if (le32_to_cpu(sb->feature_map) &
1912 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1913 return -EINVAL;
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001914 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1915 (le32_to_cpu(sb->feature_map) &
1916 MD_FEATURE_MULTIPLE_PPLS))
1917 return -EINVAL;
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001918 set_bit(MD_HAS_PPL, &mddev->flags);
1919 }
NeilBrown41158c72005-06-21 17:17:25 -07001920 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001921 /* Insist of good event counter while assembling, except for
1922 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001924 if (rdev->desc_nr >= 0 &&
1925 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001926 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1927 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001928 if (ev1 < mddev->events)
1929 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001930 } else if (mddev->bitmap) {
1931 /* If adding to array with a bitmap, then we can accept an
1932 * older device, but not too old.
1933 */
NeilBrown41158c72005-06-21 17:17:25 -07001934 if (ev1 < mddev->bitmap->events_cleared)
1935 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001936 if (ev1 < mddev->events)
1937 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001938 } else {
1939 if (ev1 < mddev->events)
1940 /* just a hot-add of a new device, leave raid_disk at -1 */
1941 return 0;
1942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (mddev->level != LEVEL_MULTIPATH) {
1944 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001945 if (rdev->desc_nr < 0 ||
1946 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001947 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001948 rdev->desc_nr = -1;
1949 } else
1950 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001952 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001954 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001955 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 break;
Song Liubac624f2015-08-13 14:31:55 -07001957 case MD_DISK_ROLE_JOURNAL: /* journal device */
1958 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1959 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001960 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001961 return -EINVAL;
1962 }
1963 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001964 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001965 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001966 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 default:
NeilBrownf4667222013-12-09 12:04:56 +11001968 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001969 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001970 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001971 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001972 if (!(le32_to_cpu(sb->feature_map) &
1973 MD_FEATURE_RECOVERY_BITMAP))
1974 rdev->saved_raid_disk = -1;
Guoqing Jiang062f5b2a2019-07-24 11:09:20 +02001975 } else {
1976 /*
1977 * If the array is FROZEN, then the device can't
1978 * be in_sync with rest of array.
1979 */
1980 if (!test_bit(MD_RECOVERY_FROZEN,
1981 &mddev->recovery))
1982 set_bit(In_sync, &rdev->flags);
1983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 rdev->raid_disk = role;
1985 break;
1986 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001987 if (sb->devflags & WriteMostly1)
1988 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001989 if (sb->devflags & FailFast1)
1990 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001991 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1992 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001993 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001994 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return 0;
1997}
1998
NeilBrownfd01b882011-10-11 16:47:53 +11001999static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
2001 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11002002 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 int max_dev, i;
2004 /* make rdev->sb match mddev and rdev data. */
2005
Namhyung Kim65a06f062011-07-27 11:00:36 +10002006 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 sb->feature_map = 0;
2009 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002010 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 memset(sb->pad3, 0, sizeof(sb->pad3));
2012
2013 sb->utime = cpu_to_le64((__u64)mddev->utime);
2014 sb->events = cpu_to_le64(mddev->events);
2015 if (mddev->in_sync)
2016 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07002017 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
2018 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 else
2020 sb->resync_offset = cpu_to_le64(0);
2021
NeilBrown1c05b4b2006-10-21 10:24:08 -07002022 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08002023
NeilBrownf0ca3402006-02-02 14:28:04 -08002024 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11002025 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10002026 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10002027 sb->level = cpu_to_le32(mddev->level);
2028 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11002029 if (test_bit(FailFast, &rdev->flags))
2030 sb->devflags |= FailFast1;
2031 else
2032 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08002033
NeilBrownaeb9b2112011-08-25 14:43:08 +10002034 if (test_bit(WriteMostly, &rdev->flags))
2035 sb->devflags |= WriteMostly1;
2036 else
2037 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10002038 sb->data_offset = cpu_to_le64(rdev->data_offset);
2039 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10002040
NeilBrownc3d97142009-12-14 12:49:52 +11002041 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
2042 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07002043 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07002044 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002045
Shaohua Lif2076e72015-10-08 21:54:12 -07002046 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11002047 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11002048 sb->feature_map |=
2049 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
2050 sb->recovery_offset =
2051 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11002052 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
2053 sb->feature_map |=
2054 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002055 }
Shaohua Li3069aa82015-08-13 14:31:56 -07002056 /* Note: recovery_offset and journal_tail share space */
2057 if (test_bit(Journal, &rdev->flags))
2058 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002059 if (test_bit(Replacement, &rdev->flags))
2060 sb->feature_map |=
2061 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002062
NeilBrownf6705572006-03-27 01:18:11 -08002063 if (mddev->reshape_position != MaxSector) {
2064 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
2065 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
2066 sb->new_layout = cpu_to_le32(mddev->new_layout);
2067 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
2068 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10002069 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10002070 if (mddev->delta_disks == 0 &&
2071 mddev->reshape_backwards)
2072 sb->feature_map
2073 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10002074 if (rdev->new_data_offset != rdev->data_offset) {
2075 sb->feature_map
2076 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
2077 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
2078 - rdev->data_offset));
2079 }
NeilBrownf6705572006-03-27 01:18:11 -08002080 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002081
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10002082 if (mddev_is_clustered(mddev))
2083 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
2084
NeilBrown2699b672011-07-28 11:31:47 +10002085 if (rdev->badblocks.count == 0)
2086 /* Nothing to do for bad blocks*/ ;
2087 else if (sb->bblog_offset == 0)
2088 /* Cannot record bad blocks on this device */
2089 md_error(mddev, rdev);
2090 else {
2091 struct badblocks *bb = &rdev->badblocks;
Christoph Hellwigae506402019-04-04 18:56:13 +02002092 __le64 *bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10002093 u64 *p = bb->page;
2094 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
2095 if (bb->changed) {
2096 unsigned seq;
2097
2098retry:
2099 seq = read_seqbegin(&bb->lock);
2100
2101 memset(bbp, 0xff, PAGE_SIZE);
2102
2103 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08002104 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10002105 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
2106 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08002107 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10002108 }
NeilBrownd0962932012-03-19 12:46:41 +11002109 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002110 if (read_seqretry(&bb->lock, seq))
2111 goto retry;
2112
2113 bb->sector = (rdev->sb_start +
2114 (int)le32_to_cpu(sb->bblog_offset));
2115 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10002116 }
2117 }
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11002120 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (rdev2->desc_nr+1 > max_dev)
2122 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07002123
NeilBrown70471da2009-08-03 10:59:57 +10002124 if (max_dev > le32_to_cpu(sb->max_dev)) {
2125 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07002126 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10002127 rdev->sb_size = max_dev * 2 + 256;
2128 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
2129 if (rdev->sb_size & bmask)
2130 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10002131 } else
2132 max_dev = le32_to_cpu(sb->max_dev);
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 for (i=0; i<max_dev;i++)
Lidong Zhong8df72022017-06-12 10:45:55 +08002135 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
NeilBrownf72ffdd2014-09-30 14:23:59 +10002136
Song Liua97b7892015-10-08 21:54:09 -07002137 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
2138 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01002140 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
Pawel Baldysiakddc08822017-08-16 17:13:45 +02002141 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
2142 sb->feature_map |=
2143 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
2144 else
2145 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01002146 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
2147 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
2148 }
2149
NeilBrowndafb20f2012-03-19 12:46:39 +11002150 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002152 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07002153 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08002154 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07002156 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07002157 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11002158 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002159 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 else
Song Liuc4d4c912015-08-13 14:31:54 -07002161 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 sb->sb_csum = calc_sb_1_csum(sb);
2165}
2166
Chris Webb0cd17fe2008-06-28 08:31:46 +10002167static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11002168super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002169{
2170 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002171 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11002172 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002173 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10002174 if (rdev->data_offset != rdev->new_data_offset)
2175 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10002176 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10002177 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01002178 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002179 max_sectors -= rdev->data_offset;
2180 if (!num_sectors || num_sectors > max_sectors)
2181 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11002182 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10002183 /* minor version 0 with bitmap we can't move */
2184 return 0;
2185 } else {
2186 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10002187 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01002188 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10002189 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11002190 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002191 if (!num_sectors || num_sectors > max_sectors)
2192 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10002193 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002194 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10002195 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10002196 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08002197 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10002198 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11002199 do {
2200 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
2201 rdev->sb_page);
2202 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11002203 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10002204
2205}
2206
2207static int
2208super_1_allow_new_offset(struct md_rdev *rdev,
2209 unsigned long long new_offset)
2210{
2211 /* All necessary checks on new >= old have been done */
2212 struct bitmap *bitmap;
2213 if (new_offset >= rdev->data_offset)
2214 return 1;
2215
2216 /* with 1.0 metadata, there is no metadata to tread on
2217 * so we can always move back */
2218 if (rdev->mddev->minor_version == 0)
2219 return 1;
2220
2221 /* otherwise we must be sure not to step on
2222 * any metadata, so stay:
2223 * 36K beyond start of superblock
2224 * beyond end of badblocks
2225 * beyond write-intent bitmap
2226 */
2227 if (rdev->sb_start + (32+4)*2 > new_offset)
2228 return 0;
2229 bitmap = rdev->mddev->bitmap;
2230 if (bitmap && !rdev->mddev->bitmap_info.file &&
2231 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10002232 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10002233 return 0;
2234 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2235 return 0;
2236
2237 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002238}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Adrian Bunk75c96f82005-05-05 16:16:09 -07002240static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 [0] = {
2242 .name = "0.90.0",
2243 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002244 .load_super = super_90_load,
2245 .validate_super = super_90_validate,
2246 .sync_super = super_90_sync,
2247 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002248 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 },
2250 [1] = {
2251 .name = "md-1",
2252 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002253 .load_super = super_1_load,
2254 .validate_super = super_1_validate,
2255 .sync_super = super_1_sync,
2256 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002257 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 },
2259};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
NeilBrownfd01b882011-10-11 16:47:53 +11002261static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05002262{
2263 if (mddev->sync_super) {
2264 mddev->sync_super(mddev, rdev);
2265 return;
2266 }
2267
2268 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2269
2270 super_types[mddev->major_version].sync_super(mddev, rdev);
2271}
2272
NeilBrownfd01b882011-10-11 16:47:53 +11002273static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
NeilBrown3cb03002011-10-11 16:45:26 +11002275 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
NeilBrown4b809912008-07-21 17:05:25 +10002277 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07002278 rdev_for_each_rcu(rdev, mddev1) {
2279 if (test_bit(Faulty, &rdev->flags) ||
2280 test_bit(Journal, &rdev->flags) ||
2281 rdev->raid_disk == -1)
2282 continue;
2283 rdev_for_each_rcu(rdev2, mddev2) {
2284 if (test_bit(Faulty, &rdev2->flags) ||
2285 test_bit(Journal, &rdev2->flags) ||
2286 rdev2->raid_disk == -1)
2287 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002288 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002289 rdev2->bdev->bd_contains) {
2290 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002291 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002292 }
Song Liu0b020e82015-09-03 23:00:35 -07002293 }
2294 }
NeilBrown4b809912008-07-21 17:05:25 +10002295 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return 0;
2297}
2298
2299static LIST_HEAD(pending_raid_disks);
2300
Andre Nollac5e7112009-08-03 10:59:47 +10002301/*
2302 * Try to register data integrity profile for an mddev
2303 *
2304 * This is called when an array is started and after a disk has been kicked
2305 * from the array. It only succeeds if all working and active component devices
2306 * are integrity capable with matching profiles.
2307 */
NeilBrownfd01b882011-10-11 16:47:53 +11002308int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002309{
NeilBrown3cb03002011-10-11 16:45:26 +11002310 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002311
Andre Nollac5e7112009-08-03 10:59:47 +10002312 if (list_empty(&mddev->disks))
2313 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002314 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2315 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002316 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002317 /* skip spares and non-functional disks */
2318 if (test_bit(Faulty, &rdev->flags))
2319 continue;
2320 if (rdev->raid_disk < 0)
2321 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002322 if (!reference) {
2323 /* Use the first rdev as the reference */
2324 reference = rdev;
2325 continue;
2326 }
2327 /* does this rdev's profile match the reference profile? */
2328 if (blk_integrity_compare(reference->bdev->bd_disk,
2329 rdev->bdev->bd_disk) < 0)
2330 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002331 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002332 if (!reference || !bdev_get_integrity(reference->bdev))
2333 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002334 /*
2335 * All component devices are integrity capable and have matching
2336 * profiles, register the common profile for the md device.
2337 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002338 blk_integrity_register(mddev->gendisk,
2339 bdev_get_integrity(reference->bdev));
2340
NeilBrown9d487392016-11-02 14:16:49 +11002341 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Kent Overstreetafeee512018-05-20 18:25:52 -04002342 if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002343 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002344 mdname(mddev));
2345 return -EINVAL;
2346 }
Andre Nollac5e7112009-08-03 10:59:47 +10002347 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002348}
Andre Nollac5e7112009-08-03 10:59:47 +10002349EXPORT_SYMBOL(md_integrity_register);
2350
Dan Williams1501efa2016-01-13 16:00:07 -08002351/*
2352 * Attempt to add an rdev, but only if it is consistent with the current
2353 * integrity profile
2354 */
2355int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002356{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002357 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002358 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002359
2360 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002361 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002362
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002363 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002364
2365 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002366 return 0;
2367
2368 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002369 pr_err("%s: incompatible integrity profile for %s\n",
2370 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002371 return -ENXIO;
2372 }
2373
2374 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002375}
2376EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002377
NeilBrownf72ffdd2014-09-30 14:23:59 +10002378static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002380 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002381 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002382 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Dan Williams11e2ede2008-04-30 00:52:32 -07002384 /* prevent duplicates */
2385 if (find_rdev(mddev, rdev->bdev->bd_dev))
2386 return -EEXIST;
2387
NeilBrown97b20ef2017-04-13 08:53:48 +10002388 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2389 mddev->pers)
2390 return -EROFS;
2391
Andre Nolldd8ac332009-03-31 14:33:13 +11002392 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002393 if (!test_bit(Journal, &rdev->flags) &&
2394 rdev->sectors &&
2395 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002396 if (mddev->pers) {
2397 /* Cannot change size, so fail
2398 * If mddev->level <= 0, then we don't care
2399 * about aligning sizes (e.g. linear)
2400 */
2401 if (mddev->level > 0)
2402 return -ENOSPC;
2403 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002404 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 /* Verify rdev->desc_nr is unique.
2408 * If it is -1, assign a free number, else
2409 * check number is not in use
2410 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002411 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (rdev->desc_nr < 0) {
2413 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002414 if (mddev->pers)
2415 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002416 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 choice++;
2418 rdev->desc_nr = choice;
2419 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002420 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002421 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002425 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002426 if (!test_bit(Journal, &rdev->flags) &&
2427 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002428 pr_warn("md: %s: array is limited to %d devices\n",
2429 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002430 return -EBUSY;
2431 }
NeilBrown19133a42005-11-08 21:39:35 -08002432 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002433 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002434
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002436 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002437
Guoqing Jiang963c5552019-06-14 17:10:36 +08002438 if (mddev->raid_disks)
Guoqing Jiang404659c2019-12-23 10:48:53 +01002439 mddev_create_serial_pool(mddev, rdev, false);
Guoqing Jiang963c5552019-06-14 17:10:36 +08002440
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002441 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002442 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002443
Tejun Heo0762b8b2008-08-25 19:56:12 +09002444 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002445 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2446 /* failure here is OK */;
2447 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002448
NeilBrown4b809912008-07-21 17:05:25 +10002449 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002450 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002451
2452 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002453 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002456
2457 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002458 pr_warn("md: failed to register dev-%s for %s\n",
2459 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002460 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461}
2462
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002463static void rdev_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002464{
NeilBrown3cb03002011-10-11 16:45:26 +11002465 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002466 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002467 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002468}
2469
NeilBrownf72ffdd2014-09-30 14:23:59 +10002470static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
2472 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002473
Tejun Heo49731ba2011-01-14 18:43:57 +01002474 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002475 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002476 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +01002477 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002479 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002480 sysfs_put(rdev->sysfs_state);
2481 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002482 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002483 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002484 * writing to 'remove' to "dev/state". We also need
2485 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002486 */
NeilBrown4b809912008-07-21 17:05:25 +10002487 synchronize_rcu();
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002488 INIT_WORK(&rdev->del_work, rdev_delayed_delete);
NeilBrown177a99b2008-02-06 01:39:56 -08002489 kobject_get(&rdev->kobj);
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002490 queue_work(md_rdev_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491}
2492
2493/*
2494 * prevent the device from being mounted, repartitioned or
2495 * otherwise reused by a RAID array (or any other kernel
2496 * subsystem), by bd_claiming the device.
2497 */
NeilBrown3cb03002011-10-11 16:45:26 +11002498static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
2500 int err = 0;
2501 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Tejun Heod4d77622010-11-13 11:55:18 +01002503 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002504 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (IS_ERR(bdev)) {
Christoph Hellwigea3edd42020-03-24 08:25:11 +01002506 pr_warn("md: could not open device unknown-block(%u,%u).\n",
2507 MAJOR(dev), MINOR(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return PTR_ERR(bdev);
2509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 rdev->bdev = bdev;
2511 return err;
2512}
2513
NeilBrown3cb03002011-10-11 16:45:26 +11002514static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515{
2516 struct block_device *bdev = rdev->bdev;
2517 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002518 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519}
2520
2521void md_autodetect_dev(dev_t dev);
2522
NeilBrownf72ffdd2014-09-30 14:23:59 +10002523static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
2525 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002526
NeilBrown9d487392016-11-02 14:16:49 +11002527 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002528 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002530 if (test_bit(AutoDetected, &rdev->flags))
2531 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532#endif
2533 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002534 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
2536
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002537void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
2539 unbind_rdev_from_array(rdev);
2540 export_rdev(rdev);
2541}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002542EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
NeilBrownfd01b882011-10-11 16:47:53 +11002544static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
NeilBrown0638bb02014-09-25 17:43:47 +10002546 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
NeilBrown0638bb02014-09-25 17:43:47 +10002548 while (!list_empty(&mddev->disks)) {
2549 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2550 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002551 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 mddev->raid_disks = 0;
2554 mddev->major_version = 0;
2555}
2556
NeilBrown6497709b2017-03-15 14:05:14 +11002557static bool set_in_sync(struct mddev *mddev)
2558{
Shaohua Liefa4b772017-10-18 22:08:13 -07002559 lockdep_assert_held(&mddev->lock);
NeilBrown4ad23a972017-03-15 14:05:14 +11002560 if (!mddev->in_sync) {
2561 mddev->sync_checkers++;
2562 spin_unlock(&mddev->lock);
2563 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2564 spin_lock(&mddev->lock);
2565 if (!mddev->in_sync &&
2566 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002567 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002568 /*
2569 * Ensure ->in_sync is visible before we clear
2570 * ->sync_checkers.
2571 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002572 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002573 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2574 sysfs_notify_dirent_safe(mddev->sysfs_state);
2575 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002576 if (--mddev->sync_checkers == 0)
2577 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002578 }
2579 if (mddev->safemode == 1)
2580 mddev->safemode = 0;
2581 return mddev->in_sync;
2582}
2583
NeilBrownf72ffdd2014-09-30 14:23:59 +10002584static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
NeilBrown42543762006-06-26 00:27:57 -07002586 /* Update each superblock (in-memory image), but
2587 * if we are allowed to, skip spares which already
2588 * have the right event counter, or have one earlier
2589 * (which would mean they aren't being marked as dirty
2590 * with the rest of the array)
2591 */
NeilBrown3cb03002011-10-11 16:45:26 +11002592 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002593 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002594 if (rdev->sb_events == mddev->events ||
2595 (nospares &&
2596 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002597 rdev->sb_events+1 == mddev->events)) {
2598 /* Don't update this superblock */
2599 rdev->sb_loaded = 2;
2600 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002601 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002602 rdev->sb_loaded = 1;
2603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 }
2605}
2606
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002607static bool does_sb_need_changing(struct mddev *mddev)
2608{
2609 struct md_rdev *rdev;
2610 struct mdp_superblock_1 *sb;
2611 int role;
2612
2613 /* Find a good rdev */
2614 rdev_for_each(rdev, mddev)
2615 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2616 break;
2617
2618 /* No good device found. */
2619 if (!rdev)
2620 return false;
2621
2622 sb = page_address(rdev->sb_page);
2623 /* Check if a device has become faulty or a spare become active */
2624 rdev_for_each(rdev, mddev) {
2625 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2626 /* Device activated? */
2627 if (role == 0xffff && rdev->raid_disk >=0 &&
2628 !test_bit(Faulty, &rdev->flags))
2629 return true;
2630 /* Device turned faulty? */
2631 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2632 return true;
2633 }
2634
2635 /* Check if any mddev parameters have changed */
2636 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2637 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002638 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002639 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2640 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2641 return true;
2642
2643 return false;
2644}
2645
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002646void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647{
NeilBrown3cb03002011-10-11 16:45:26 +11002648 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002649 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002650 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002651 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002652 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
NeilBrownd87f0642013-04-24 11:42:40 +10002654 if (mddev->ro) {
2655 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002656 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002657 return;
2658 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002659
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002660repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002661 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002662 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002663 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002664 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002665 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002666 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002667 /* Has someone else has updated the sb */
2668 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002669 if (ret == 0)
2670 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002671 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2672 BIT(MD_SB_CHANGE_DEVS) |
2673 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002674 return;
2675 }
2676 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002677
NeilBrowndb0505d2017-10-17 16:18:36 +11002678 /*
2679 * First make sure individual recovery_offsets are correct
2680 * curr_resync_completed can only be used during recovery.
2681 * During reshape/resync it might use array-addresses rather
2682 * that device addresses.
2683 */
NeilBrowndafb20f2012-03-19 12:46:39 +11002684 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002685 if (rdev->raid_disk >= 0 &&
2686 mddev->delta_disks >= 0 &&
NeilBrowndb0505d2017-10-17 16:18:36 +11002687 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2688 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2689 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002690 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002691 !test_bit(In_sync, &rdev->flags) &&
2692 mddev->curr_resync_completed > rdev->recovery_offset)
2693 rdev->recovery_offset = mddev->curr_resync_completed;
2694
NeilBrownf72ffdd2014-09-30 14:23:59 +10002695 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002696 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002697 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2698 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002699 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002700 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002701 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002702 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002703 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002704 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002705 md_error(mddev, rdev);
2706 }
2707 clear_bit(Blocked, &rdev->flags);
2708 clear_bit(BlockedBadBlocks, &rdev->flags);
2709 wake_up(&rdev->blocked_wait);
2710 }
2711 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002712 wake_up(&mddev->sb_wait);
2713 return;
2714 }
2715
NeilBrown85572d72014-12-15 12:56:56 +11002716 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002717
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002718 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002719
Shaohua Li29530792016-12-08 15:48:19 -08002720 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002721 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002722 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002723 /* just a clean<-> dirty transition, possibly leave spares alone,
2724 * though if events isn't the right even/odd, we will have to do
2725 * spares after all
2726 */
2727 nospares = 1;
2728 if (force_change)
2729 nospares = 0;
2730 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002731 /* If the array is degraded, then skipping spares is both
2732 * dangerous and fairly pointless.
2733 * Dangerous because a device that was removed from the array
2734 * might have a event_count that still looks up-to-date,
2735 * so it can be re-added without a resync.
2736 * Pointless because if there are any spares to skip,
2737 * then a recovery will happen and soon that array won't
2738 * be degraded any more and the spare can go back to sleep then.
2739 */
NeilBrown850b2b422006-10-03 01:15:46 -07002740 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002741
NeilBrown06d91a52005-06-21 17:17:12 -07002742 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002743
2744 /* If this is just a dirty<->clean transition, and the array is clean
2745 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002746 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002747 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002748 && mddev->can_decrease_events
2749 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002750 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002751 mddev->can_decrease_events = 0;
2752 } else {
NeilBrown42543762006-06-26 00:27:57 -07002753 /* otherwise we have to go forward and ... */
2754 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002755 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
NeilBrown403df472014-09-30 15:52:29 +10002758 /*
2759 * This 64-bit counter should never wrap.
2760 * Either we are in around ~1 trillion A.C., assuming
2761 * 1 reboot per second, or we have a bug...
2762 */
2763 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002764
NeilBrowndafb20f2012-03-19 12:46:39 +11002765 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002766 if (rdev->badblocks.changed)
2767 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002768 if (test_bit(Faulty, &rdev->flags))
2769 set_bit(FaultRecorded, &rdev->flags);
2770 }
NeilBrown2699b672011-07-28 11:31:47 +10002771
NeilBrowne6910632008-02-06 01:39:51 -08002772 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002773 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
NeilBrown36a4e1f2011-10-07 14:23:17 +11002775 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2776 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Shaohua Li504634f2016-11-18 09:44:08 -08002778 if (mddev->queue)
2779 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002780rewrite:
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002781 md_bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002782 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002784
NeilBrown42543762006-06-26 00:27:57 -07002785 if (rdev->sb_loaded != 1)
2786 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
NeilBrownf4667222013-12-09 12:04:56 +11002788 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002789 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002790 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002791 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002792 pr_debug("md: (write) %s's sb offset: %llu\n",
2793 bdevname(rdev->bdev, b),
2794 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002795 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002796 if (rdev->badblocks.size) {
2797 md_super_write(mddev, rdev,
2798 rdev->badblocks.sector,
2799 rdev->badblocks.size << 9,
2800 rdev->bb_page);
2801 rdev->badblocks.size = 0;
2802 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002803
NeilBrownf4667222013-12-09 12:04:56 +11002804 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002805 pr_debug("md: %s (skipping faulty)\n",
2806 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002807
NeilBrown7bfa19f2005-06-21 17:17:28 -07002808 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 /* only need to write one superblock... */
2810 break;
2811 }
NeilBrown46533ff2016-11-18 16:16:11 +11002812 if (md_super_wait(mddev) < 0)
2813 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002814 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002815
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002816 if (mddev_is_clustered(mddev) && ret == 0)
2817 md_cluster_ops->metadata_update_finish(mddev);
2818
NeilBrown850b2b422006-10-03 01:15:46 -07002819 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002820 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2821 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002822 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002823 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002824 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002825 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2826 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002827
NeilBrowndafb20f2012-03-19 12:46:39 +11002828 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002829 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2830 clear_bit(Blocked, &rdev->flags);
2831
2832 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002833 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002834 clear_bit(BlockedBadBlocks, &rdev->flags);
2835 wake_up(&rdev->blocked_wait);
2836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002838EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002840static int add_bound_rdev(struct md_rdev *rdev)
2841{
2842 struct mddev *mddev = rdev->mddev;
2843 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002844 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002845
Shaohua Li87d4d912016-01-06 14:37:14 -08002846 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002847 /* If there is hot_add_disk but no hot_remove_disk
2848 * then added disks for geometry changes,
2849 * and should be added immediately.
2850 */
2851 super_types[mddev->major_version].
2852 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002853 if (add_journal)
2854 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002855 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002856 if (add_journal)
2857 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002858 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002859 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002860 return err;
2861 }
2862 }
2863 sysfs_notify_dirent_safe(rdev->sysfs_state);
2864
Shaohua Li29530792016-12-08 15:48:19 -08002865 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002866 if (mddev->degraded)
2867 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2868 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2869 md_new_event(mddev);
2870 md_wakeup_thread(mddev->thread);
2871 return 0;
2872}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Andre Noll7f6ce762008-03-23 18:34:54 +01002874/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002875 * We want to accept with case. For this we use cmd_match.
2876 */
2877static int cmd_match(const char *cmd, const char *str)
2878{
2879 /* See if cmd, written into a sysfs file, matches
2880 * str. They must either be the same, or cmd can
2881 * have a trailing newline
2882 */
2883 while (*cmd && *str && *cmd == *str) {
2884 cmd++;
2885 str++;
2886 }
2887 if (*cmd == '\n')
2888 cmd++;
2889 if (*str || *cmd)
2890 return 0;
2891 return 1;
2892}
2893
NeilBrown86e6ffd2005-11-08 21:39:24 -08002894struct rdev_sysfs_entry {
2895 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002896 ssize_t (*show)(struct md_rdev *, char *);
2897 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002898};
2899
2900static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002901state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002902{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002903 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002904 size_t len = 0;
Mark Rutland6aa7de02017-10-23 14:07:29 -07002905 unsigned long flags = READ_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002906
NeilBrown758bfc82014-12-15 12:56:59 +11002907 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002908 (!test_bit(ExternalBbl, &flags) &&
2909 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002910 len += sprintf(page+len, "faulty%s", sep);
2911 if (test_bit(In_sync, &flags))
2912 len += sprintf(page+len, "in_sync%s", sep);
2913 if (test_bit(Journal, &flags))
2914 len += sprintf(page+len, "journal%s", sep);
2915 if (test_bit(WriteMostly, &flags))
2916 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002917 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002918 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002919 && !test_bit(Faulty, &flags)))
2920 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002921 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002922 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002923 !test_bit(In_sync, &flags))
2924 len += sprintf(page+len, "spare%s", sep);
2925 if (test_bit(WriteErrorSeen, &flags))
2926 len += sprintf(page+len, "write_error%s", sep);
2927 if (test_bit(WantReplacement, &flags))
2928 len += sprintf(page+len, "want_replacement%s", sep);
2929 if (test_bit(Replacement, &flags))
2930 len += sprintf(page+len, "replacement%s", sep);
2931 if (test_bit(ExternalBbl, &flags))
2932 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002933 if (test_bit(FailFast, &flags))
2934 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002935
2936 if (len)
2937 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002938
NeilBrown86e6ffd2005-11-08 21:39:24 -08002939 return len+sprintf(page+len, "\n");
2940}
2941
NeilBrown45dc2de2006-06-26 00:27:58 -07002942static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002943state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002944{
2945 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002946 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002947 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002948 * writemostly - sets write_mostly
2949 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002950 * blocked - sets the Blocked flags
2951 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002952 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002953 * -insync - clear Insync for a device with a slot assigned,
2954 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002955 * write_error - sets WriteErrorSeen
2956 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002957 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002958 */
2959 int err = -EINVAL;
2960 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2961 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002962 if (test_bit(Faulty, &rdev->flags))
2963 err = 0;
2964 else
2965 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002966 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002967 if (rdev->mddev->pers) {
2968 clear_bit(Blocked, &rdev->flags);
2969 remove_and_add_spares(rdev->mddev, rdev);
2970 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002971 if (rdev->raid_disk >= 0)
2972 err = -EBUSY;
2973 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002974 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002975 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002976 if (mddev_is_clustered(mddev))
2977 err = md_cluster_ops->remove_disk(mddev, rdev);
2978
2979 if (err == 0) {
2980 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002981 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08002982 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11002983 md_wakeup_thread(mddev->thread);
2984 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002985 md_new_event(mddev);
2986 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002987 }
NeilBrownf6556752006-06-26 00:28:01 -07002988 } else if (cmd_match(buf, "writemostly")) {
2989 set_bit(WriteMostly, &rdev->flags);
Guoqing Jiang404659c2019-12-23 10:48:53 +01002990 mddev_create_serial_pool(rdev->mddev, rdev, false);
NeilBrownf6556752006-06-26 00:28:01 -07002991 err = 0;
2992 } else if (cmd_match(buf, "-writemostly")) {
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +01002993 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
NeilBrownf6556752006-06-26 00:28:01 -07002994 clear_bit(WriteMostly, &rdev->flags);
2995 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002996 } else if (cmd_match(buf, "blocked")) {
2997 set_bit(Blocked, &rdev->flags);
2998 err = 0;
2999 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10003000 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02003001 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10003002 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10003003 /* metadata handler doesn't understand badblocks,
3004 * so we need to fail the device
3005 */
3006 md_error(rdev->mddev, rdev);
3007 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003008 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10003009 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003010 wake_up(&rdev->blocked_wait);
3011 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3012 md_wakeup_thread(rdev->mddev->thread);
3013
3014 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10003015 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
3016 set_bit(In_sync, &rdev->flags);
3017 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11003018 } else if (cmd_match(buf, "failfast")) {
3019 set_bit(FailFast, &rdev->flags);
3020 err = 0;
3021 } else if (cmd_match(buf, "-failfast")) {
3022 clear_bit(FailFast, &rdev->flags);
3023 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07003024 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
3025 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10003026 if (rdev->mddev->pers == NULL) {
3027 clear_bit(In_sync, &rdev->flags);
3028 rdev->saved_raid_disk = rdev->raid_disk;
3029 rdev->raid_disk = -1;
3030 err = 0;
3031 }
NeilBrownd7a9d442011-07-28 11:31:48 +10003032 } else if (cmd_match(buf, "write_error")) {
3033 set_bit(WriteErrorSeen, &rdev->flags);
3034 err = 0;
3035 } else if (cmd_match(buf, "-write_error")) {
3036 clear_bit(WriteErrorSeen, &rdev->flags);
3037 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11003038 } else if (cmd_match(buf, "want_replacement")) {
3039 /* Any non-spare device that is not a replacement can
3040 * become want_replacement at any time, but we then need to
3041 * check if recovery is needed.
3042 */
3043 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07003044 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11003045 !test_bit(Replacement, &rdev->flags))
3046 set_bit(WantReplacement, &rdev->flags);
3047 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3048 md_wakeup_thread(rdev->mddev->thread);
3049 err = 0;
3050 } else if (cmd_match(buf, "-want_replacement")) {
3051 /* Clearing 'want_replacement' is always allowed.
3052 * Once replacements starts it is too late though.
3053 */
3054 err = 0;
3055 clear_bit(WantReplacement, &rdev->flags);
3056 } else if (cmd_match(buf, "replacement")) {
3057 /* Can only set a device as a replacement when array has not
3058 * yet been started. Once running, replacement is automatic
3059 * from spares, or by assigning 'slot'.
3060 */
3061 if (rdev->mddev->pers)
3062 err = -EBUSY;
3063 else {
3064 set_bit(Replacement, &rdev->flags);
3065 err = 0;
3066 }
3067 } else if (cmd_match(buf, "-replacement")) {
3068 /* Similarly, can only clear Replacement before start */
3069 if (rdev->mddev->pers)
3070 err = -EBUSY;
3071 else {
3072 clear_bit(Replacement, &rdev->flags);
3073 err = 0;
3074 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05003075 } else if (cmd_match(buf, "re-add")) {
Yufen Yuee37e622019-04-02 14:22:14 +08003076 if (!rdev->mddev->pers)
3077 err = -EINVAL;
3078 else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) &&
3079 rdev->saved_raid_disk >= 0) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05003080 /* clear_bit is performed _after_ all the devices
3081 * have their local Faulty bit cleared. If any writes
3082 * happen in the meantime in the local node, they
3083 * will land in the local bitmap, which will be synced
3084 * by this node eventually
3085 */
3086 if (!mddev_is_clustered(rdev->mddev) ||
3087 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
3088 clear_bit(Faulty, &rdev->flags);
3089 err = add_bound_rdev(rdev);
3090 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05003091 } else
3092 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02003093 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
3094 set_bit(ExternalBbl, &rdev->flags);
3095 rdev->badblocks.shift = 0;
3096 err = 0;
3097 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
3098 clear_bit(ExternalBbl, &rdev->flags);
3099 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07003100 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10003101 if (!err)
3102 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07003103 return err ? err : len;
3104}
NeilBrown80ca3a42006-07-10 04:44:18 -07003105static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10003106__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003107
3108static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003109errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08003110{
3111 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
3112}
3113
3114static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003115errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08003116{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003117 unsigned int n;
3118 int rv;
3119
3120 rv = kstrtouint(buf, 10, &n);
3121 if (rv < 0)
3122 return rv;
3123 atomic_set(&rdev->corrected_errors, n);
3124 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08003125}
3126static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07003127__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08003128
NeilBrown014236d2006-01-06 00:20:55 -08003129static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003130slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08003131{
Shaohua Lif2076e72015-10-08 21:54:12 -07003132 if (test_bit(Journal, &rdev->flags))
3133 return sprintf(page, "journal\n");
3134 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08003135 return sprintf(page, "none\n");
3136 else
3137 return sprintf(page, "%d\n", rdev->raid_disk);
3138}
3139
3140static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003141slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08003142{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003143 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08003144 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003145
Shaohua Lif2076e72015-10-08 21:54:12 -07003146 if (test_bit(Journal, &rdev->flags))
3147 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08003148 if (strncmp(buf, "none", 4)==0)
3149 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003150 else {
3151 err = kstrtouint(buf, 10, (unsigned int *)&slot);
3152 if (err < 0)
3153 return err;
3154 }
Neil Brown6c2fce22008-06-28 08:31:31 +10003155 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08003156 /* Setting 'slot' on an active array requires also
3157 * updating the 'rd%d' link, and communicating
3158 * with the personality with ->hot_*_disk.
3159 * For now we only support removing
3160 * failed/spare devices. This normally happens automatically,
3161 * but not when the metadata is externally managed.
3162 */
NeilBrownc303da62008-02-06 01:39:51 -08003163 if (rdev->raid_disk == -1)
3164 return -EEXIST;
3165 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10003166 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08003167 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10003168 clear_bit(Blocked, &rdev->flags);
3169 remove_and_add_spares(rdev->mddev, rdev);
3170 if (rdev->raid_disk >= 0)
3171 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08003172 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3173 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10003174 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10003175 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10003176 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10003177 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11003178 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10003179
3180 if (rdev->raid_disk != -1)
3181 return -EBUSY;
3182
NeilBrownc6751b22011-02-02 11:57:13 +11003183 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
3184 return -EBUSY;
3185
Neil Brown6c2fce22008-06-28 08:31:31 +10003186 if (rdev->mddev->pers->hot_add_disk == NULL)
3187 return -EINVAL;
3188
NeilBrownba1b41b2011-01-14 09:14:34 +11003189 if (slot >= rdev->mddev->raid_disks &&
3190 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
3191 return -ENOSPC;
3192
Neil Brown6c2fce22008-06-28 08:31:31 +10003193 rdev->raid_disk = slot;
3194 if (test_bit(In_sync, &rdev->flags))
3195 rdev->saved_raid_disk = slot;
3196 else
3197 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11003198 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11003199 clear_bit(Bitmap_sync, &rdev->flags);
Guoqing Jiang3f79cc22020-04-04 23:57:11 +02003200 err = rdev->mddev->pers->hot_add_disk(rdev->mddev, rdev);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11003201 if (err) {
3202 rdev->raid_disk = -1;
3203 return err;
3204 } else
3205 sysfs_notify_dirent_safe(rdev->sysfs_state);
3206 if (sysfs_link_rdev(rdev->mddev, rdev))
3207 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10003208 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08003209 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11003210 if (slot >= rdev->mddev->raid_disks &&
3211 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08003212 return -ENOSPC;
3213 rdev->raid_disk = slot;
3214 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08003215 clear_bit(Faulty, &rdev->flags);
3216 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08003217 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003218 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08003219 }
NeilBrown014236d2006-01-06 00:20:55 -08003220 return len;
3221}
3222
NeilBrown014236d2006-01-06 00:20:55 -08003223static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07003224__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08003225
NeilBrown93c8cad2006-01-06 00:20:56 -08003226static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003227offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08003228{
Andrew Morton6961ece2006-01-06 00:20:59 -08003229 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08003230}
3231
3232static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003233offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08003234{
NeilBrownc6563a82012-05-21 09:27:00 +10003235 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003236 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003237 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10003238 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003239 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003240 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08003241 /* Must set offset before size, so overlap checks
3242 * can be sane */
3243 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08003244 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10003245 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08003246 return len;
3247}
3248
3249static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07003250__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08003251
NeilBrownc6563a82012-05-21 09:27:00 +10003252static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3253{
3254 return sprintf(page, "%llu\n",
3255 (unsigned long long)rdev->new_data_offset);
3256}
3257
3258static ssize_t new_offset_store(struct md_rdev *rdev,
3259 const char *buf, size_t len)
3260{
3261 unsigned long long new_offset;
3262 struct mddev *mddev = rdev->mddev;
3263
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003264 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10003265 return -EINVAL;
3266
NeilBrownf851b602014-12-11 10:02:10 +11003267 if (mddev->sync_thread ||
3268 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10003269 return -EBUSY;
3270 if (new_offset == rdev->data_offset)
3271 /* reset is always permitted */
3272 ;
3273 else if (new_offset > rdev->data_offset) {
3274 /* must not push array size beyond rdev_sectors */
3275 if (new_offset - rdev->data_offset
3276 + mddev->dev_sectors > rdev->sectors)
3277 return -E2BIG;
3278 }
3279 /* Metadata worries about other space details. */
3280
3281 /* decreasing the offset is inconsistent with a backwards
3282 * reshape.
3283 */
3284 if (new_offset < rdev->data_offset &&
3285 mddev->reshape_backwards)
3286 return -EINVAL;
3287 /* Increasing offset is inconsistent with forwards
3288 * reshape. reshape_direction should be set to
3289 * 'backwards' first.
3290 */
3291 if (new_offset > rdev->data_offset &&
3292 !mddev->reshape_backwards)
3293 return -EINVAL;
3294
3295 if (mddev->pers && mddev->persistent &&
3296 !super_types[mddev->major_version]
3297 .allow_new_offset(rdev, new_offset))
3298 return -E2BIG;
3299 rdev->new_data_offset = new_offset;
3300 if (new_offset > rdev->data_offset)
3301 mddev->reshape_backwards = 1;
3302 else if (new_offset < rdev->data_offset)
3303 mddev->reshape_backwards = 0;
3304
3305 return len;
3306}
3307static struct rdev_sysfs_entry rdev_new_offset =
3308__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3309
NeilBrown83303b62006-01-06 00:21:06 -08003310static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003311rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003312{
Andre Nolldd8ac332009-03-31 14:33:13 +11003313 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003314}
3315
NeilBrownc5d79ad2008-02-06 01:39:54 -08003316static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3317{
3318 /* check if two start/length pairs overlap */
3319 if (s1+l1 <= s2)
3320 return 0;
3321 if (s2+l2 <= s1)
3322 return 0;
3323 return 1;
3324}
3325
Dan Williamsb522adc2009-03-31 15:00:31 +11003326static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3327{
3328 unsigned long long blocks;
3329 sector_t new;
3330
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003331 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003332 return -EINVAL;
3333
3334 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3335 return -EINVAL; /* sector conversion overflow */
3336
3337 new = blocks * 2;
3338 if (new != blocks * 2)
3339 return -EINVAL; /* unsigned long long to sector_t overflow */
3340
3341 *sectors = new;
3342 return 0;
3343}
3344
NeilBrown83303b62006-01-06 00:21:06 -08003345static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003346rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003347{
NeilBrownfd01b882011-10-11 16:47:53 +11003348 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003349 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003350 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003351
Shaohua Lif2076e72015-10-08 21:54:12 -07003352 if (test_bit(Journal, &rdev->flags))
3353 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003354 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003355 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003356 if (rdev->data_offset != rdev->new_data_offset)
3357 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003358 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003359 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003360 sectors = super_types[my_mddev->major_version].
3361 rdev_size_change(rdev, sectors);
3362 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003363 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003364 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003365 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003366 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003367 if (!my_mddev->pers->resize)
3368 /* Cannot change size for RAID0 or Linear etc */
3369 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003370 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003371 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003372 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003373
Andre Nolldd8ac332009-03-31 14:33:13 +11003374 rdev->sectors = sectors;
3375 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003376 /* Need to check that all other rdevs with the same
3377 * ->bdev do not overlap. 'rcu' is sufficient to walk
3378 * the rdev lists safely.
3379 * This check does not provide a hard guarantee, it
3380 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003381 */
NeilBrownfd01b882011-10-11 16:47:53 +11003382 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003383 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003384 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003385
NeilBrown8b1afc32014-09-29 15:33:20 +10003386 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003387 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003388 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003389
NeilBrowndafb20f2012-03-19 12:46:39 +11003390 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003391 if (rdev->bdev == rdev2->bdev &&
3392 rdev != rdev2 &&
3393 overlaps(rdev->data_offset, rdev->sectors,
3394 rdev2->data_offset,
3395 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003396 overlap = 1;
3397 break;
3398 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003399 if (overlap) {
3400 mddev_put(mddev);
3401 break;
3402 }
3403 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003404 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003405 if (overlap) {
3406 /* Someone else could have slipped in a size
3407 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003408 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003409 * safe, and trust userspace not to race with
3410 * itself
3411 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003412 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003413 return -EBUSY;
3414 }
3415 }
NeilBrown83303b62006-01-06 00:21:06 -08003416 return len;
3417}
3418
3419static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003420__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003421
NeilBrown3cb03002011-10-11 16:45:26 +11003422static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003423{
3424 unsigned long long recovery_start = rdev->recovery_offset;
3425
3426 if (test_bit(In_sync, &rdev->flags) ||
3427 recovery_start == MaxSector)
3428 return sprintf(page, "none\n");
3429
3430 return sprintf(page, "%llu\n", recovery_start);
3431}
3432
NeilBrown3cb03002011-10-11 16:45:26 +11003433static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003434{
3435 unsigned long long recovery_start;
3436
3437 if (cmd_match(buf, "none"))
3438 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003439 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003440 return -EINVAL;
3441
3442 if (rdev->mddev->pers &&
3443 rdev->raid_disk >= 0)
3444 return -EBUSY;
3445
3446 rdev->recovery_offset = recovery_start;
3447 if (recovery_start == MaxSector)
3448 set_bit(In_sync, &rdev->flags);
3449 else
3450 clear_bit(In_sync, &rdev->flags);
3451 return len;
3452}
3453
3454static struct rdev_sysfs_entry rdev_recovery_start =
3455__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3456
Vishal Vermafc974ee2015-12-24 19:20:34 -07003457/* sysfs access to bad-blocks list.
3458 * We present two files.
3459 * 'bad-blocks' lists sector numbers and lengths of ranges that
3460 * are recorded as bad. The list is truncated to fit within
3461 * the one-page limit of sysfs.
3462 * Writing "sector length" to this file adds an acknowledged
3463 * bad block list.
3464 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3465 * been acknowledged. Writing to this file adds bad blocks
3466 * without acknowledging them. This is largely for testing.
3467 */
NeilBrown3cb03002011-10-11 16:45:26 +11003468static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003469{
3470 return badblocks_show(&rdev->badblocks, page, 0);
3471}
NeilBrown3cb03002011-10-11 16:45:26 +11003472static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003473{
NeilBrownde393cd2011-07-28 11:31:48 +10003474 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3475 /* Maybe that ack was all we needed */
3476 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3477 wake_up(&rdev->blocked_wait);
3478 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003479}
3480static struct rdev_sysfs_entry rdev_bad_blocks =
3481__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3482
NeilBrown3cb03002011-10-11 16:45:26 +11003483static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003484{
3485 return badblocks_show(&rdev->badblocks, page, 1);
3486}
NeilBrown3cb03002011-10-11 16:45:26 +11003487static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003488{
3489 return badblocks_store(&rdev->badblocks, page, len, 1);
3490}
3491static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3492__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3493
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003494static ssize_t
3495ppl_sector_show(struct md_rdev *rdev, char *page)
3496{
3497 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3498}
3499
3500static ssize_t
3501ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3502{
3503 unsigned long long sector;
3504
3505 if (kstrtoull(buf, 10, &sector) < 0)
3506 return -EINVAL;
3507 if (sector != (sector_t)sector)
3508 return -EINVAL;
3509
3510 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3511 rdev->raid_disk >= 0)
3512 return -EBUSY;
3513
3514 if (rdev->mddev->persistent) {
3515 if (rdev->mddev->major_version == 0)
3516 return -EINVAL;
3517 if ((sector > rdev->sb_start &&
3518 sector - rdev->sb_start > S16_MAX) ||
3519 (sector < rdev->sb_start &&
3520 rdev->sb_start - sector > -S16_MIN))
3521 return -EINVAL;
3522 rdev->ppl.offset = sector - rdev->sb_start;
3523 } else if (!rdev->mddev->external) {
3524 return -EBUSY;
3525 }
3526 rdev->ppl.sector = sector;
3527 return len;
3528}
3529
3530static struct rdev_sysfs_entry rdev_ppl_sector =
3531__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3532
3533static ssize_t
3534ppl_size_show(struct md_rdev *rdev, char *page)
3535{
3536 return sprintf(page, "%u\n", rdev->ppl.size);
3537}
3538
3539static ssize_t
3540ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3541{
3542 unsigned int size;
3543
3544 if (kstrtouint(buf, 10, &size) < 0)
3545 return -EINVAL;
3546
3547 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3548 rdev->raid_disk >= 0)
3549 return -EBUSY;
3550
3551 if (rdev->mddev->persistent) {
3552 if (rdev->mddev->major_version == 0)
3553 return -EINVAL;
3554 if (size > U16_MAX)
3555 return -EINVAL;
3556 } else if (!rdev->mddev->external) {
3557 return -EBUSY;
3558 }
3559 rdev->ppl.size = size;
3560 return len;
3561}
3562
3563static struct rdev_sysfs_entry rdev_ppl_size =
3564__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3565
NeilBrown86e6ffd2005-11-08 21:39:24 -08003566static struct attribute *rdev_default_attrs[] = {
3567 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003568 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003569 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003570 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003571 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003572 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003573 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003574 &rdev_bad_blocks.attr,
3575 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003576 &rdev_ppl_sector.attr,
3577 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003578 NULL,
3579};
3580static ssize_t
3581rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3582{
3583 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003584 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003585
3586 if (!entry->show)
3587 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003588 if (!rdev->mddev)
Marcos Paulo de Souza168b3052019-06-14 15:41:06 -07003589 return -ENODEV;
NeilBrown758bfc82014-12-15 12:56:59 +11003590 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003591}
3592
3593static ssize_t
3594rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3595 const char *page, size_t length)
3596{
3597 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003598 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003599 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003600 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003601
3602 if (!entry->store)
3603 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003604 if (!capable(CAP_SYS_ADMIN))
3605 return -EACCES;
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003606 rv = mddev ? mddev_lock(mddev) : -ENODEV;
NeilBrownca388052008-02-06 01:39:55 -08003607 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003608 if (rdev->mddev == NULL)
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003609 rv = -ENODEV;
NeilBrown27c529b2008-03-04 14:29:33 -08003610 else
3611 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003612 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003613 }
3614 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003615}
3616
3617static void rdev_free(struct kobject *ko)
3618{
NeilBrown3cb03002011-10-11 16:45:26 +11003619 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003620 kfree(rdev);
3621}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003622static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003623 .show = rdev_attr_show,
3624 .store = rdev_attr_store,
3625};
3626static struct kobj_type rdev_ktype = {
3627 .release = rdev_free,
3628 .sysfs_ops = &rdev_sysfs_ops,
3629 .default_attrs = rdev_default_attrs,
3630};
3631
NeilBrown3cb03002011-10-11 16:45:26 +11003632int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003633{
3634 rdev->desc_nr = -1;
3635 rdev->saved_raid_disk = -1;
3636 rdev->raid_disk = -1;
3637 rdev->flags = 0;
3638 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003639 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003640 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003641 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003642 rdev->sb_loaded = 0;
3643 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003644 atomic_set(&rdev->nr_pending, 0);
3645 atomic_set(&rdev->read_errors, 0);
3646 atomic_set(&rdev->corrected_errors, 0);
3647
3648 INIT_LIST_HEAD(&rdev->same_set);
3649 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003650
3651 /* Add space to store bad block list.
3652 * This reserves the space even on arrays where it cannot
3653 * be used - I wonder if that matters
3654 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003655 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003656}
3657EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658/*
3659 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3660 *
3661 * mark the device faulty if:
3662 *
3663 * - the device is nonexistent (zero size)
3664 * - the device has no valid superblock
3665 *
3666 * a faulty rdev _never_ has rdev->sb set.
3667 */
NeilBrown3cb03002011-10-11 16:45:26 +11003668static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669{
3670 char b[BDEVNAME_SIZE];
3671 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003672 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 sector_t size;
3674
NeilBrown9ffae0c2006-01-06 00:20:32 -08003675 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003676 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
NeilBrown2230dfe2011-07-28 11:31:46 +10003679 err = md_rdev_init(rdev);
3680 if (err)
3681 goto abort_free;
3682 err = alloc_disk_sb(rdev);
3683 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 goto abort_free;
3685
NeilBrownc5d79ad2008-02-06 01:39:54 -08003686 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 if (err)
3688 goto abort_free;
3689
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003690 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003691
Mike Snitzer77304d22010-11-08 14:39:12 +01003692 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003694 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 bdevname(rdev->bdev,b));
3696 err = -EINVAL;
3697 goto abort_free;
3698 }
3699
3700 if (super_format >= 0) {
3701 err = super_types[super_format].
3702 load_super(rdev, NULL, super_minor);
3703 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003704 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003705 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003706 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 goto abort_free;
3708 }
3709 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003710 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 bdevname(rdev->bdev,b));
3712 goto abort_free;
3713 }
3714 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003715
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 return rdev;
3717
3718abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003719 if (rdev->bdev)
3720 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003721 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 kfree(rdev);
3723 return ERR_PTR(err);
3724}
3725
3726/*
3727 * Check a full RAID array for plausibility
3728 */
3729
Yufen Yu6a5cb532019-10-16 16:00:03 +08003730static int analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731{
3732 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003733 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 char b[BDEVNAME_SIZE];
3735
3736 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003737 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 switch (super_types[mddev->major_version].
3739 load_super(rdev, freshest, mddev->minor_version)) {
3740 case 1:
3741 freshest = rdev;
3742 break;
3743 case 0:
3744 break;
3745 default:
NeilBrown9d487392016-11-02 14:16:49 +11003746 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003748 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 }
3750
Yufen Yu6a5cb532019-10-16 16:00:03 +08003751 /* Cannot find a valid fresh disk */
3752 if (!freshest) {
3753 pr_warn("md: cannot find a valid disk\n");
3754 return -EINVAL;
3755 }
3756
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 super_types[mddev->major_version].
3758 validate_super(mddev, freshest);
3759
3760 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003761 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003762 if (mddev->max_disks &&
3763 (rdev->desc_nr >= mddev->max_disks ||
3764 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003765 pr_warn("md: %s: %s: only %d devices permitted\n",
3766 mdname(mddev), bdevname(rdev->bdev, b),
3767 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003768 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003769 continue;
3770 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003771 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 if (super_types[mddev->major_version].
3773 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003774 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003776 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 continue;
3778 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 if (mddev->level == LEVEL_MULTIPATH) {
3781 rdev->desc_nr = i++;
3782 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003783 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003784 } else if (rdev->raid_disk >=
3785 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3786 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003787 rdev->raid_disk = -1;
3788 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 }
3790 }
Yufen Yu6a5cb532019-10-16 16:00:03 +08003791
3792 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793}
3794
NeilBrown72e02072009-12-14 12:49:55 +11003795/* Read a fixed-point number.
3796 * Numbers in sysfs attributes should be in "standard" units where
3797 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003798 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003799 * milliseconds or jiffies.
3800 * This function takes a decimal number with a possible fractional
3801 * component, and produces an integer which is the result of
3802 * multiplying that number by 10^'scale'.
3803 * all without any floating-point arithmetic.
3804 */
3805int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3806{
3807 unsigned long result = 0;
3808 long decimals = -1;
3809 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3810 if (*cp == '.')
3811 decimals = 0;
3812 else if (decimals < scale) {
3813 unsigned int value;
3814 value = *cp - '0';
3815 result = result * 10 + value;
3816 if (decimals >= 0)
3817 decimals++;
3818 }
3819 cp++;
3820 }
3821 if (*cp == '\n')
3822 cp++;
3823 if (*cp)
3824 return -EINVAL;
3825 if (decimals < 0)
3826 decimals = 0;
Andy Shevchenkocf891602019-07-23 23:41:55 +03003827 *res = result * int_pow(10, scale - decimals);
NeilBrown72e02072009-12-14 12:49:55 +11003828 return 0;
3829}
3830
NeilBrowneae17012005-11-08 21:39:23 -08003831static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003832safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003833{
3834 int msec = (mddev->safemode_delay*1000)/HZ;
3835 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3836}
3837static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003838safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003839{
NeilBrown16f17b32006-06-26 00:27:37 -07003840 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003841
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003842 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003843 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003844 return -EINVAL;
3845 }
3846
NeilBrown72e02072009-12-14 12:49:55 +11003847 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003848 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003849 if (msec == 0)
3850 mddev->safemode_delay = 0;
3851 else {
NeilBrown19052c02008-08-05 15:54:13 +10003852 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003853 unsigned long new_delay = (msec*HZ)/1000;
3854
3855 if (new_delay == 0)
3856 new_delay = 1;
3857 mddev->safemode_delay = new_delay;
3858 if (new_delay < old_delay || old_delay == 0)
3859 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003860 }
3861 return len;
3862}
3863static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003864__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003865
3866static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003867level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003868{
NeilBrown36d091f2014-12-15 12:56:58 +11003869 struct md_personality *p;
3870 int ret;
3871 spin_lock(&mddev->lock);
3872 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003873 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003874 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003875 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003876 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003877 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003878 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003879 else
NeilBrown36d091f2014-12-15 12:56:58 +11003880 ret = 0;
3881 spin_unlock(&mddev->lock);
3882 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003883}
3884
NeilBrownd9d166c2006-01-06 00:20:51 -08003885static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003886level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003887{
Dan Williamsf2859af2010-05-02 10:04:16 -07003888 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003889 ssize_t rv;
3890 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003891 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003892 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003893 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003894 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003895
NeilBrown67918752014-12-15 12:57:01 +11003896 if (slen == 0 || slen >= sizeof(clevel))
3897 return -EINVAL;
3898
3899 rv = mddev_lock(mddev);
3900 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003901 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003902
3903 if (mddev->pers == NULL) {
3904 strncpy(mddev->clevel, buf, slen);
3905 if (mddev->clevel[slen-1] == '\n')
3906 slen--;
3907 mddev->clevel[slen] = 0;
3908 mddev->level = LEVEL_NONE;
3909 rv = len;
3910 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003911 }
NeilBrown67918752014-12-15 12:57:01 +11003912 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003913 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003914 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003915
3916 /* request to change the personality. Need to ensure:
3917 * - array is not engaged in resync/recovery/reshape
3918 * - old personality can be suspended
3919 * - new personality will access other array.
3920 */
3921
NeilBrown67918752014-12-15 12:57:01 +11003922 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003923 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003924 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003925 mddev->reshape_position != MaxSector ||
3926 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003927 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003928
NeilBrown67918752014-12-15 12:57:01 +11003929 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003930 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003931 pr_warn("md: %s: %s does not support online personality change\n",
3932 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003933 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003934 }
3935
3936 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003937 strncpy(clevel, buf, slen);
3938 if (clevel[slen-1] == '\n')
3939 slen--;
3940 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003941 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003942 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003943
Dan Williamsf2859af2010-05-02 10:04:16 -07003944 if (request_module("md-%s", clevel) != 0)
3945 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003946 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003947 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003948 if (!pers || !try_module_get(pers->owner)) {
3949 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003950 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003951 rv = -EINVAL;
3952 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003953 }
3954 spin_unlock(&pers_lock);
3955
3956 if (pers == mddev->pers) {
3957 /* Nothing to do! */
3958 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003959 rv = len;
3960 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003961 }
3962 if (!pers->takeover) {
3963 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003964 pr_warn("md: %s: %s does not support personality takeover\n",
3965 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003966 rv = -EINVAL;
3967 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003968 }
3969
NeilBrowndafb20f2012-03-19 12:46:39 +11003970 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003971 rdev->new_raid_disk = rdev->raid_disk;
3972
NeilBrown245f46c2009-03-31 14:39:39 +11003973 /* ->takeover must set new_* and/or delta_disks
3974 * if it succeeds, and may set them when it fails.
3975 */
3976 priv = pers->takeover(mddev);
3977 if (IS_ERR(priv)) {
3978 mddev->new_level = mddev->level;
3979 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003980 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003981 mddev->raid_disks -= mddev->delta_disks;
3982 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003983 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003984 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003985 pr_warn("md: %s: %s would not accept array\n",
3986 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003987 rv = PTR_ERR(priv);
3988 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003989 }
3990
3991 /* Looks like we have a winner */
3992 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003993 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003994
3995 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003996 oldpers = mddev->pers;
3997 oldpriv = mddev->private;
3998 mddev->pers = pers;
3999 mddev->private = priv;
4000 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4001 mddev->level = mddev->new_level;
4002 mddev->layout = mddev->new_layout;
4003 mddev->chunk_sectors = mddev->new_chunk_sectors;
4004 mddev->delta_disks = 0;
4005 mddev->reshape_backwards = 0;
4006 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11004007 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10004008
NeilBrowndb721d32014-12-15 12:56:58 +11004009 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11004010 mddev->external) {
4011 /* We are converting from a no-redundancy array
4012 * to a redundancy array and metadata is managed
4013 * externally so we need to be sure that writes
4014 * won't block due to a need to transition
4015 * clean->dirty
4016 * until external management is started.
4017 */
4018 mddev->in_sync = 0;
4019 mddev->safemode_delay = 0;
4020 mddev->safemode = 0;
4021 }
4022
NeilBrowndb721d32014-12-15 12:56:58 +11004023 oldpers->free(mddev, oldpriv);
4024
4025 if (oldpers->sync_request == NULL &&
4026 pers->sync_request != NULL) {
4027 /* need to add the md_redundancy_group */
4028 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11004029 pr_warn("md: cannot register extra attributes for %s\n",
4030 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11004031 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
4032 }
4033 if (oldpers->sync_request != NULL &&
4034 pers->sync_request == NULL) {
4035 /* need to remove the md_redundancy_group */
4036 if (mddev->to_remove == NULL)
4037 mddev->to_remove = &md_redundancy_group;
4038 }
4039
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02004040 module_put(oldpers->owner);
4041
NeilBrowndafb20f2012-03-19 12:46:39 +11004042 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004043 if (rdev->raid_disk < 0)
4044 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11004045 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01004046 rdev->new_raid_disk = -1;
4047 if (rdev->new_raid_disk == rdev->raid_disk)
4048 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10004049 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01004050 }
NeilBrowndafb20f2012-03-19 12:46:39 +11004051 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004052 if (rdev->raid_disk < 0)
4053 continue;
4054 if (rdev->new_raid_disk == rdev->raid_disk)
4055 continue;
4056 rdev->raid_disk = rdev->new_raid_disk;
4057 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10004058 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01004059 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10004060 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11004061 pr_warn("md: cannot register rd%d for %s after level change\n",
4062 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10004063 }
NeilBrowne93f68a2010-06-15 09:36:03 +01004064 }
4065
NeilBrowndb721d32014-12-15 12:56:58 +11004066 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11004067 /* this is now an array without redundancy, so
4068 * it must always be in_sync
4069 */
4070 mddev->in_sync = 1;
4071 del_timer_sync(&mddev->safemode_timer);
4072 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11004073 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11004074 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004075 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10004076 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11004077 if (!mddev->thread)
4078 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10004079 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07004080 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004081 rv = len;
4082out_unlock:
4083 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08004084 return rv;
4085}
4086
4087static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07004088__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08004089
NeilBrownd4dbd022006-06-26 00:27:59 -07004090static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004091layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07004092{
4093 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07004094 if (mddev->reshape_position != MaxSector &&
4095 mddev->layout != mddev->new_layout)
4096 return sprintf(page, "%d (%d)\n",
4097 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07004098 return sprintf(page, "%d\n", mddev->layout);
4099}
4100
4101static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004102layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07004103{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004104 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11004105 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07004106
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004107 err = kstrtouint(buf, 10, &n);
4108 if (err < 0)
4109 return err;
NeilBrown67918752014-12-15 12:57:01 +11004110 err = mddev_lock(mddev);
4111 if (err)
4112 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07004113
NeilBrownb3546032009-03-31 14:56:41 +11004114 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10004115 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004116 err = -EBUSY;
4117 else if (mddev->ro)
4118 err = -EROFS;
4119 else {
4120 mddev->new_layout = n;
4121 err = mddev->pers->check_reshape(mddev);
4122 if (err)
4123 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10004124 }
NeilBrownb3546032009-03-31 14:56:41 +11004125 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07004126 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11004127 if (mddev->reshape_position == MaxSector)
4128 mddev->layout = n;
4129 }
NeilBrown67918752014-12-15 12:57:01 +11004130 mddev_unlock(mddev);
4131 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07004132}
4133static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07004134__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07004135
NeilBrowneae17012005-11-08 21:39:23 -08004136static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004137raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08004138{
NeilBrownbb636542005-11-08 21:39:45 -08004139 if (mddev->raid_disks == 0)
4140 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004141 if (mddev->reshape_position != MaxSector &&
4142 mddev->delta_disks != 0)
4143 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
4144 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08004145 return sprintf(page, "%d\n", mddev->raid_disks);
4146}
4147
NeilBrownfd01b882011-10-11 16:47:53 +11004148static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08004149
4150static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004151raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08004152{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004153 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11004154 int err;
NeilBrownda943b992006-01-06 00:20:54 -08004155
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004156 err = kstrtouint(buf, 10, &n);
4157 if (err < 0)
4158 return err;
NeilBrownda943b992006-01-06 00:20:54 -08004159
NeilBrown67918752014-12-15 12:57:01 +11004160 err = mddev_lock(mddev);
4161 if (err)
4162 return err;
NeilBrownda943b992006-01-06 00:20:54 -08004163 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11004164 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07004165 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10004166 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004167 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10004168
NeilBrown67918752014-12-15 12:57:01 +11004169 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10004170 rdev_for_each(rdev, mddev) {
4171 if (olddisks < n &&
4172 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11004173 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10004174 if (olddisks > n &&
4175 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11004176 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10004177 }
NeilBrown67918752014-12-15 12:57:01 +11004178 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004179 mddev->delta_disks = n - olddisks;
4180 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10004181 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07004182 } else
NeilBrownda943b992006-01-06 00:20:54 -08004183 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11004184out_unlock:
4185 mddev_unlock(mddev);
4186 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08004187}
4188static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07004189__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08004190
NeilBrown24dd4692005-11-08 21:39:26 -08004191static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004192chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08004193{
NeilBrown08a02ec2007-05-09 02:35:38 -07004194 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10004195 mddev->chunk_sectors != mddev->new_chunk_sectors)
4196 return sprintf(page, "%d (%d)\n",
4197 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10004198 mddev->chunk_sectors << 9);
4199 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08004200}
4201
4202static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004203chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08004204{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004205 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11004206 int err;
NeilBrown3b343802006-01-06 00:20:47 -08004207
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004208 err = kstrtoul(buf, 10, &n);
4209 if (err < 0)
4210 return err;
NeilBrown3b343802006-01-06 00:20:47 -08004211
NeilBrown67918752014-12-15 12:57:01 +11004212 err = mddev_lock(mddev);
4213 if (err)
4214 return err;
NeilBrownb3546032009-03-31 14:56:41 +11004215 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10004216 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004217 err = -EBUSY;
4218 else if (mddev->ro)
4219 err = -EROFS;
4220 else {
4221 mddev->new_chunk_sectors = n >> 9;
4222 err = mddev->pers->check_reshape(mddev);
4223 if (err)
4224 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10004225 }
NeilBrownb3546032009-03-31 14:56:41 +11004226 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10004227 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004228 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10004229 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004230 }
NeilBrown67918752014-12-15 12:57:01 +11004231 mddev_unlock(mddev);
4232 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08004233}
4234static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004235__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08004236
NeilBrowna94213b2006-06-26 00:28:00 -07004237static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004238resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07004239{
NeilBrownd1a7c502009-03-31 15:24:32 +11004240 if (mddev->recovery_cp == MaxSector)
4241 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07004242 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4243}
4244
4245static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004246resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07004247{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004248 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11004249 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004250
4251 if (cmd_match(buf, "none"))
4252 n = MaxSector;
4253 else {
4254 err = kstrtoull(buf, 10, &n);
4255 if (err < 0)
4256 return err;
4257 if (n != (sector_t)n)
4258 return -EINVAL;
4259 }
NeilBrowna94213b2006-06-26 00:28:00 -07004260
NeilBrown67918752014-12-15 12:57:01 +11004261 err = mddev_lock(mddev);
4262 if (err)
4263 return err;
NeilBrownb0986362011-05-11 15:52:21 +10004264 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11004265 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07004266
NeilBrown67918752014-12-15 12:57:01 +11004267 if (!err) {
4268 mddev->recovery_cp = n;
4269 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08004270 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11004271 }
4272 mddev_unlock(mddev);
4273 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07004274}
4275static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10004276__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4277 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07004278
NeilBrown9e653b62006-06-26 00:27:58 -07004279/*
4280 * The array state can be:
4281 *
4282 * clear
4283 * No devices, no size, no level
4284 * Equivalent to STOP_ARRAY ioctl
4285 * inactive
4286 * May have some settings, but array is not active
4287 * all IO results in error
4288 * When written, doesn't tear down array, but just stops it
4289 * suspended (not supported yet)
4290 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01004291 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07004292 * readonly
4293 * no resync can happen. no superblocks get written.
4294 * write requests fail
4295 * read-auto
4296 * like readonly, but behaves like 'clean' on a write request.
4297 *
4298 * clean - no pending writes, but otherwise active.
4299 * When written to inactive array, starts without resync
4300 * If a write request arrives then
4301 * if metadata is known, mark 'dirty' and switch to 'active'.
4302 * if not known, block and switch to write-pending
4303 * If written to an active array that has pending writes, then fails.
4304 * active
4305 * fully active: IO and resync can be happening.
4306 * When written to inactive array, starts with resync
4307 *
4308 * write-pending
4309 * clean, but writes are blocked waiting for 'active' to be written.
4310 *
4311 * active-idle
4312 * like active, but no writes have been seen for a while (100msec).
4313 *
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004314 * broken
4315 * RAID0/LINEAR-only: same as clean, but array is missing a member.
4316 * It's useful because RAID0/LINEAR mounted-arrays aren't stopped
4317 * when a member is gone, so this state will at least alert the
4318 * user that something is wrong.
NeilBrown9e653b62006-06-26 00:27:58 -07004319 */
4320enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004321 write_pending, active_idle, broken, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004322static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004323 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004324 "write-pending", "active-idle", "broken", NULL };
NeilBrown9e653b62006-06-26 00:27:58 -07004325
4326static int match_word(const char *word, char **list)
4327{
4328 int n;
4329 for (n=0; list[n]; n++)
4330 if (cmd_match(word, list[n]))
4331 break;
4332 return n;
4333}
4334
4335static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004336array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004337{
4338 enum array_state st = inactive;
4339
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004340 if (mddev->pers && !test_bit(MD_NOT_READY, &mddev->flags)) {
NeilBrown9e653b62006-06-26 00:27:58 -07004341 switch(mddev->ro) {
4342 case 1:
4343 st = readonly;
4344 break;
4345 case 2:
4346 st = read_auto;
4347 break;
4348 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004349 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004350 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004351 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004352 else if (mddev->in_sync)
4353 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004354 else if (mddev->safemode)
4355 st = active_idle;
4356 else
4357 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004358 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004359 }
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004360
4361 if (test_bit(MD_BROKEN, &mddev->flags) && st == clean)
4362 st = broken;
4363 } else {
NeilBrown9e653b62006-06-26 00:27:58 -07004364 if (list_empty(&mddev->disks) &&
4365 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004366 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004367 st = clear;
4368 else
4369 st = inactive;
4370 }
4371 return sprintf(page, "%s\n", array_states[st]);
4372}
4373
NeilBrownf72ffdd2014-09-30 14:23:59 +10004374static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4375static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4376static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004377static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004378
4379static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004380array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004381{
NeilBrown6497709b2017-03-15 14:05:14 +11004382 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004383 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004384
4385 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4386 /* don't take reconfig_mutex when toggling between
4387 * clean and active
4388 */
4389 spin_lock(&mddev->lock);
4390 if (st == active) {
4391 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004392 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004393 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004394 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004395 } else /* st == clean */ {
4396 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004397 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004398 err = -EBUSY;
4399 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004400 if (!err)
4401 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004402 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004403 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004404 }
4405 err = mddev_lock(mddev);
4406 if (err)
4407 return err;
4408 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004409 switch(st) {
4410 case bad_word:
4411 break;
4412 case clear:
4413 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004414 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004415 break;
4416 case inactive:
4417 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004418 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004419 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004420 else
NeilBrowne6910632008-02-06 01:39:51 -08004421 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004422 break;
4423 case suspended:
4424 break; /* not supported yet */
4425 case readonly:
4426 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004427 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004428 else {
4429 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004430 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004431 err = do_md_run(mddev);
4432 }
4433 break;
4434 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004435 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004436 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004437 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004438 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004439 err = restart_array(mddev);
4440 if (err == 0) {
4441 mddev->ro = 2;
4442 set_disk_ro(mddev->gendisk, 0);
4443 }
NeilBrown9e653b62006-06-26 00:27:58 -07004444 } else {
4445 mddev->ro = 2;
4446 err = do_md_run(mddev);
4447 }
4448 break;
4449 case clean:
4450 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004451 err = restart_array(mddev);
4452 if (err)
4453 break;
NeilBrown85572d72014-12-15 12:56:56 +11004454 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004455 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004456 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004457 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004458 } else
4459 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004460 break;
4461 case active:
4462 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004463 err = restart_array(mddev);
4464 if (err)
4465 break;
Shaohua Li29530792016-12-08 15:48:19 -08004466 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004467 wake_up(&mddev->sb_wait);
4468 err = 0;
4469 } else {
4470 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004471 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004472 err = do_md_run(mddev);
4473 }
4474 break;
4475 case write_pending:
4476 case active_idle:
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004477 case broken:
NeilBrown9e653b62006-06-26 00:27:58 -07004478 /* these cannot be set */
4479 break;
4480 }
NeilBrown67918752014-12-15 12:57:01 +11004481
4482 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004483 if (mddev->hold_active == UNTIL_IOCTL)
4484 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004485 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004486 }
NeilBrown67918752014-12-15 12:57:01 +11004487 mddev_unlock(mddev);
4488 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004489}
NeilBrown80ca3a42006-07-10 04:44:18 -07004490static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004491__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004492
NeilBrown6d7ff7382006-01-06 00:21:16 -08004493static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004494max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004495 return sprintf(page, "%d\n",
4496 atomic_read(&mddev->max_corr_read_errors));
4497}
4498
4499static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004500max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004501{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004502 unsigned int n;
4503 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004504
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004505 rv = kstrtouint(buf, 10, &n);
4506 if (rv < 0)
4507 return rv;
4508 atomic_set(&mddev->max_corr_read_errors, n);
4509 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004510}
4511
4512static struct md_sysfs_entry max_corr_read_errors =
4513__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4514 max_corrected_read_errors_store);
4515
4516static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004517null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004518{
4519 return -EINVAL;
4520}
4521
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004522/* need to ensure rdev_delayed_delete() has completed */
4523static void flush_rdev_wq(struct mddev *mddev)
4524{
4525 struct md_rdev *rdev;
4526
4527 rcu_read_lock();
4528 rdev_for_each_rcu(rdev, mddev)
4529 if (work_pending(&rdev->del_work)) {
4530 flush_workqueue(md_rdev_misc_wq);
4531 break;
4532 }
4533 rcu_read_unlock();
4534}
4535
NeilBrown6d7ff7382006-01-06 00:21:16 -08004536static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004537new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004538{
4539 /* buf must be %d:%d\n? giving major and minor numbers */
4540 /* The new device is added to the array.
4541 * If the array has a persistent superblock, we read the
4542 * superblock to initialise info and check validity.
4543 * Otherwise, only checking done is that in bind_rdev_to_array,
4544 * which mainly checks size.
4545 */
4546 char *e;
4547 int major = simple_strtoul(buf, &e, 10);
4548 int minor;
4549 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004550 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004551 int err;
4552
4553 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4554 return -EINVAL;
4555 minor = simple_strtoul(e+1, &e, 10);
4556 if (*e && *e != '\n')
4557 return -EINVAL;
4558 dev = MKDEV(major, minor);
4559 if (major != MAJOR(dev) ||
4560 minor != MINOR(dev))
4561 return -EOVERFLOW;
4562
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004563 flush_rdev_wq(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004564 err = mddev_lock(mddev);
4565 if (err)
4566 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004567 if (mddev->persistent) {
4568 rdev = md_import_device(dev, mddev->major_version,
4569 mddev->minor_version);
4570 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004571 struct md_rdev *rdev0
4572 = list_entry(mddev->disks.next,
4573 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004574 err = super_types[mddev->major_version]
4575 .load_super(rdev, rdev0, mddev->minor_version);
4576 if (err < 0)
4577 goto out;
4578 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004579 } else if (mddev->external)
4580 rdev = md_import_device(dev, -2, -1);
4581 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004582 rdev = md_import_device(dev, -1, -1);
4583
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004584 if (IS_ERR(rdev)) {
4585 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004586 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004587 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004588 err = bind_rdev_to_array(rdev, mddev);
4589 out:
4590 if (err)
4591 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004592 mddev_unlock(mddev);
Alexey Obitotskiy5492c462017-07-28 15:49:25 +02004593 if (!err)
4594 md_new_event(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004595 return err ? err : len;
4596}
4597
4598static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004599__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004600
4601static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004602bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004603{
4604 char *end;
4605 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004606 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004607
NeilBrown67918752014-12-15 12:57:01 +11004608 err = mddev_lock(mddev);
4609 if (err)
4610 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004611 if (!mddev->bitmap)
4612 goto out;
4613 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4614 while (*buf) {
4615 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4616 if (buf == end) break;
4617 if (*end == '-') { /* range */
4618 buf = end + 1;
4619 end_chunk = simple_strtoul(buf, &end, 0);
4620 if (buf == end) break;
4621 }
4622 if (*end && !isspace(*end)) break;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004623 md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004624 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004625 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004626 md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
Paul Clements9b1d1da2006-10-03 01:15:49 -07004627out:
NeilBrown67918752014-12-15 12:57:01 +11004628 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004629 return len;
4630}
4631
4632static struct md_sysfs_entry md_bitmap =
4633__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4634
4635static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004636size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004637{
Andre Noll58c0fed2009-03-31 14:33:13 +11004638 return sprintf(page, "%llu\n",
4639 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004640}
4641
NeilBrownfd01b882011-10-11 16:47:53 +11004642static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004643
4644static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004645size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004646{
4647 /* If array is inactive, we can reduce the component size, but
4648 * not increase it (except from 0).
4649 * If array is active, we can try an on-line resize
4650 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004651 sector_t sectors;
4652 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004653
Andre Noll58c0fed2009-03-31 14:33:13 +11004654 if (err < 0)
4655 return err;
NeilBrown67918752014-12-15 12:57:01 +11004656 err = mddev_lock(mddev);
4657 if (err)
4658 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004659 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004660 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004661 if (err == 0)
4662 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004663 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004664 if (mddev->dev_sectors == 0 ||
4665 mddev->dev_sectors > sectors)
4666 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004667 else
4668 err = -ENOSPC;
4669 }
NeilBrown67918752014-12-15 12:57:01 +11004670 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004671 return err ? err : len;
4672}
4673
4674static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004675__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004676
Masanari Iida83f0d772012-10-30 00:18:08 +09004677/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004678 * This is one of
4679 * 'none' for arrays with no metadata (good luck...)
4680 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004681 * or N.M for internally known formats
4682 */
4683static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004684metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004685{
4686 if (mddev->persistent)
4687 return sprintf(page, "%d.%d\n",
4688 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004689 else if (mddev->external)
4690 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004691 else
4692 return sprintf(page, "none\n");
4693}
4694
4695static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004696metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004697{
4698 int major, minor;
4699 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004700 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004701 /* Changing the details of 'external' metadata is
4702 * always permitted. Otherwise there must be
4703 * no devices attached to the array.
4704 */
NeilBrown67918752014-12-15 12:57:01 +11004705
4706 err = mddev_lock(mddev);
4707 if (err)
4708 return err;
4709 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004710 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4711 ;
4712 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004713 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004714
NeilBrown67918752014-12-15 12:57:01 +11004715 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004716 if (cmd_match(buf, "none")) {
4717 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004718 mddev->external = 0;
4719 mddev->major_version = 0;
4720 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004721 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004722 }
4723 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004724 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004725 if (namelen >= sizeof(mddev->metadata_type))
4726 namelen = sizeof(mddev->metadata_type)-1;
4727 strncpy(mddev->metadata_type, buf+9, namelen);
4728 mddev->metadata_type[namelen] = 0;
4729 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4730 mddev->metadata_type[--namelen] = 0;
4731 mddev->persistent = 0;
4732 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004733 mddev->major_version = 0;
4734 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004735 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004736 }
4737 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004738 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004739 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004740 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004741 buf = e+1;
4742 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004743 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004744 goto out_unlock;
4745 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004746 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004747 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004748 mddev->major_version = major;
4749 mddev->minor_version = minor;
4750 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004751 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004752 err = 0;
4753out_unlock:
4754 mddev_unlock(mddev);
4755 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004756}
4757
4758static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004759__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004760
NeilBrowna35b0d62006-01-06 00:20:49 -08004761static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004762action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004763{
NeilBrown7eec3142005-11-08 21:39:44 -08004764 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004765 unsigned long recovery = mddev->recovery;
4766 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004767 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004768 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4769 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4770 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004771 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004772 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4773 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004774 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004775 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004776 type = "check";
4777 else
4778 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004779 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004780 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004781 else if (mddev->reshape_position != MaxSector)
4782 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004783 }
4784 return sprintf(page, "%s\n", type);
4785}
4786
4787static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004788action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004789{
NeilBrown7eec3142005-11-08 21:39:44 -08004790 if (!mddev->pers || !mddev->pers->sync_request)
4791 return -EINVAL;
4792
NeilBrownb6a9ce62009-05-26 09:41:17 +10004793
4794 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004795 if (cmd_match(page, "frozen"))
4796 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4797 else
4798 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004799 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4800 mddev_lock(mddev) == 0) {
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004801 if (work_pending(&mddev->del_work))
4802 flush_workqueue(md_misc_wq);
NeilBrown8e8e2512015-06-12 19:51:27 +10004803 if (mddev->sync_thread) {
4804 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004805 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004806 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004807 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004808 }
NeilBrown312045e2015-12-21 11:01:21 +11004809 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004810 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004811 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004812 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004813 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004814 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004815 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004816 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004817 int err;
4818 if (mddev->pers->start_reshape == NULL)
4819 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004820 err = mddev_lock(mddev);
4821 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004822 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4823 err = -EBUSY;
4824 else {
4825 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4826 err = mddev->pers->start_reshape(mddev);
4827 }
NeilBrown67918752014-12-15 12:57:01 +11004828 mddev_unlock(mddev);
4829 }
NeilBrown16484bf2006-03-27 01:18:13 -08004830 if (err)
4831 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004832 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004833 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004834 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004835 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004836 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004837 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004838 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004839 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4840 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004841 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004842 if (mddev->ro == 2) {
4843 /* A write to sync_action is enough to justify
4844 * canceling read-auto mode
4845 */
4846 mddev->ro = 0;
4847 md_wakeup_thread(mddev->sync_thread);
4848 }
NeilBrown03c902e2006-01-06 00:20:46 -08004849 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004850 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004851 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004852 return len;
4853}
4854
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004855static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004856__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004857
4858static ssize_t
4859last_sync_action_show(struct mddev *mddev, char *page)
4860{
4861 return sprintf(page, "%s\n", mddev->last_sync_action);
4862}
4863
4864static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4865
NeilBrown9d888832005-11-08 21:39:26 -08004866static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004867mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004868{
4869 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004870 (unsigned long long)
4871 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004872}
4873
NeilBrown80ca3a42006-07-10 04:44:18 -07004874static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004875
NeilBrown88202a02006-01-06 00:21:36 -08004876static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004877sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004878{
4879 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4880 mddev->sync_speed_min ? "local": "system");
4881}
4882
4883static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004884sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004885{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004886 unsigned int min;
4887 int rv;
4888
NeilBrown88202a02006-01-06 00:21:36 -08004889 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004890 min = 0;
4891 } else {
4892 rv = kstrtouint(buf, 10, &min);
4893 if (rv < 0)
4894 return rv;
4895 if (min == 0)
4896 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004897 }
NeilBrown88202a02006-01-06 00:21:36 -08004898 mddev->sync_speed_min = min;
4899 return len;
4900}
4901
4902static struct md_sysfs_entry md_sync_min =
4903__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4904
4905static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004906sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004907{
4908 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4909 mddev->sync_speed_max ? "local": "system");
4910}
4911
4912static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004913sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004914{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004915 unsigned int max;
4916 int rv;
4917
NeilBrown88202a02006-01-06 00:21:36 -08004918 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004919 max = 0;
4920 } else {
4921 rv = kstrtouint(buf, 10, &max);
4922 if (rv < 0)
4923 return rv;
4924 if (max == 0)
4925 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004926 }
NeilBrown88202a02006-01-06 00:21:36 -08004927 mddev->sync_speed_max = max;
4928 return len;
4929}
4930
4931static struct md_sysfs_entry md_sync_max =
4932__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4933
Iustin Popd7f3d292007-10-16 23:30:54 -07004934static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004935degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004936{
4937 return sprintf(page, "%d\n", mddev->degraded);
4938}
4939static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004940
4941static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004942sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004943{
4944 return sprintf(page, "%d\n", mddev->parallel_resync);
4945}
4946
4947static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004948sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004949{
4950 long n;
4951
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004952 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004953 return -EINVAL;
4954
4955 if (n != 0 && n != 1)
4956 return -EINVAL;
4957
4958 mddev->parallel_resync = n;
4959
4960 if (mddev->sync_thread)
4961 wake_up(&resync_wait);
4962
4963 return len;
4964}
4965
4966/* force parallel resync, even with shared block devices */
4967static struct md_sysfs_entry md_sync_force_parallel =
4968__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4969 sync_force_parallel_show, sync_force_parallel_store);
4970
4971static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004972sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004973{
4974 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004975 if (mddev->curr_resync == 0)
4976 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004977 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4978 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004979 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004980 db = resync - mddev->resync_mark_cnt;
4981 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004982}
4983
NeilBrown80ca3a42006-07-10 04:44:18 -07004984static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004985
4986static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004987sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004988{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004989 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004990
NeilBrownacb180b2009-04-14 16:28:34 +10004991 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4992 return sprintf(page, "none\n");
4993
NeilBrown72f36d52012-10-11 14:25:57 +11004994 if (mddev->curr_resync == 1 ||
4995 mddev->curr_resync == 2)
4996 return sprintf(page, "delayed\n");
4997
NeilBrownc804cde2012-05-21 09:28:33 +10004998 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4999 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11005000 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08005001 else
Andre Noll58c0fed2009-03-31 14:33:13 +11005002 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08005003
NeilBrownacb180b2009-04-14 16:28:34 +10005004 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11005005 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08005006}
5007
NeilBrown750f1992014-09-30 08:53:05 +10005008static struct md_sysfs_entry md_sync_completed =
5009 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08005010
NeilBrowne464eaf2006-03-27 01:18:14 -08005011static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005012min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10005013{
5014 return sprintf(page, "%llu\n",
5015 (unsigned long long)mddev->resync_min);
5016}
5017static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005018min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10005019{
5020 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11005021 int err;
NeilBrown23da4222014-12-15 12:57:01 +11005022
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005023 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10005024 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11005025
5026 spin_lock(&mddev->lock);
5027 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10005028 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11005029 goto out_unlock;
5030
5031 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10005032 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11005033 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10005034
NeilBrown50c37b12015-03-23 17:36:38 +11005035 /* Round down to multiple of 4K for safety */
5036 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11005037 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10005038
NeilBrown23da4222014-12-15 12:57:01 +11005039out_unlock:
5040 spin_unlock(&mddev->lock);
5041 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10005042}
5043
5044static struct md_sysfs_entry md_min_sync =
5045__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
5046
5047static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005048max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08005049{
5050 if (mddev->resync_max == MaxSector)
5051 return sprintf(page, "max\n");
5052 else
5053 return sprintf(page, "%llu\n",
5054 (unsigned long long)mddev->resync_max);
5055}
5056static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005057max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08005058{
NeilBrown23da4222014-12-15 12:57:01 +11005059 int err;
5060 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08005061 if (strncmp(buf, "max", 3) == 0)
5062 mddev->resync_max = MaxSector;
5063 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10005064 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11005065 int chunk;
5066
5067 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005068 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11005069 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10005070 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11005071 goto out_unlock;
5072
5073 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08005074 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10005075 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08005076 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11005077 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08005078
5079 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11005080 chunk = mddev->chunk_sectors;
5081 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10005082 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11005083
5084 err = -EINVAL;
5085 if (sector_div(temp, chunk))
5086 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08005087 }
5088 mddev->resync_max = max;
5089 }
5090 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11005091 err = 0;
5092out_unlock:
5093 spin_unlock(&mddev->lock);
5094 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08005095}
5096
5097static struct md_sysfs_entry md_max_sync =
5098__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
5099
5100static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005101suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08005102{
5103 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
5104}
5105
5106static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005107suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08005108{
NeilBrownb03e0cc2017-10-19 12:49:15 +11005109 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005110 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08005111
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005112 err = kstrtoull(buf, 10, &new);
5113 if (err < 0)
5114 return err;
5115 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08005116 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11005117
NeilBrown67918752014-12-15 12:57:01 +11005118 err = mddev_lock(mddev);
5119 if (err)
5120 return err;
5121 err = -EINVAL;
5122 if (mddev->pers == NULL ||
5123 mddev->pers->quiesce == NULL)
5124 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005125 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11005126 mddev->suspend_lo = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005127 mddev_resume(mddev);
5128
NeilBrown67918752014-12-15 12:57:01 +11005129 err = 0;
5130unlock:
5131 mddev_unlock(mddev);
5132 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08005133}
5134static struct md_sysfs_entry md_suspend_lo =
5135__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
5136
NeilBrowne464eaf2006-03-27 01:18:14 -08005137static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005138suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08005139{
5140 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
5141}
5142
5143static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005144suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08005145{
NeilBrownb03e0cc2017-10-19 12:49:15 +11005146 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005147 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08005148
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005149 err = kstrtoull(buf, 10, &new);
5150 if (err < 0)
5151 return err;
5152 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08005153 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11005154
NeilBrown67918752014-12-15 12:57:01 +11005155 err = mddev_lock(mddev);
5156 if (err)
5157 return err;
5158 err = -EINVAL;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005159 if (mddev->pers == NULL)
NeilBrown67918752014-12-15 12:57:01 +11005160 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005161
5162 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11005163 mddev->suspend_hi = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005164 mddev_resume(mddev);
5165
NeilBrown67918752014-12-15 12:57:01 +11005166 err = 0;
5167unlock:
5168 mddev_unlock(mddev);
5169 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08005170}
5171static struct md_sysfs_entry md_suspend_hi =
5172__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
5173
NeilBrown08a02ec2007-05-09 02:35:38 -07005174static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005175reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07005176{
5177 if (mddev->reshape_position != MaxSector)
5178 return sprintf(page, "%llu\n",
5179 (unsigned long long)mddev->reshape_position);
5180 strcpy(page, "none\n");
5181 return 5;
5182}
5183
5184static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005185reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07005186{
NeilBrownc6563a82012-05-21 09:27:00 +10005187 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005188 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005189 int err;
NeilBrown67918752014-12-15 12:57:01 +11005190
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005191 err = kstrtoull(buf, 10, &new);
5192 if (err < 0)
5193 return err;
5194 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07005195 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11005196 err = mddev_lock(mddev);
5197 if (err)
5198 return err;
5199 err = -EBUSY;
5200 if (mddev->pers)
5201 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07005202 mddev->reshape_position = new;
5203 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005204 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07005205 mddev->new_level = mddev->level;
5206 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10005207 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10005208 rdev_for_each(rdev, mddev)
5209 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11005210 err = 0;
5211unlock:
5212 mddev_unlock(mddev);
5213 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07005214}
5215
5216static struct md_sysfs_entry md_reshape_position =
5217__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
5218 reshape_position_store);
5219
Dan Williamsb522adc2009-03-31 15:00:31 +11005220static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10005221reshape_direction_show(struct mddev *mddev, char *page)
5222{
5223 return sprintf(page, "%s\n",
5224 mddev->reshape_backwards ? "backwards" : "forwards");
5225}
5226
5227static ssize_t
5228reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
5229{
5230 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11005231 int err;
5232
NeilBrown2c810cd2012-05-21 09:27:00 +10005233 if (cmd_match(buf, "forwards"))
5234 backwards = 0;
5235 else if (cmd_match(buf, "backwards"))
5236 backwards = 1;
5237 else
5238 return -EINVAL;
5239 if (mddev->reshape_backwards == backwards)
5240 return len;
5241
NeilBrown67918752014-12-15 12:57:01 +11005242 err = mddev_lock(mddev);
5243 if (err)
5244 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10005245 /* check if we are allowed to change */
5246 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11005247 err = -EBUSY;
5248 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10005249 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11005250 err = -EINVAL;
5251 else
5252 mddev->reshape_backwards = backwards;
5253 mddev_unlock(mddev);
5254 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10005255}
5256
5257static struct md_sysfs_entry md_reshape_direction =
5258__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5259 reshape_direction_store);
5260
5261static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005262array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11005263{
5264 if (mddev->external_size)
5265 return sprintf(page, "%llu\n",
5266 (unsigned long long)mddev->array_sectors/2);
5267 else
5268 return sprintf(page, "default\n");
5269}
5270
5271static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005272array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11005273{
5274 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11005275 int err;
5276
5277 err = mddev_lock(mddev);
5278 if (err)
5279 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11005280
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005281 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08005282 if (mddev_is_clustered(mddev)) {
5283 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005284 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08005285 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005286
Dan Williamsb522adc2009-03-31 15:00:31 +11005287 if (strncmp(buf, "default", 7) == 0) {
5288 if (mddev->pers)
5289 sectors = mddev->pers->size(mddev, 0, 0);
5290 else
5291 sectors = mddev->array_sectors;
5292
5293 mddev->external_size = 0;
5294 } else {
5295 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11005296 err = -EINVAL;
5297 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5298 err = -E2BIG;
5299 else
5300 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11005301 }
5302
NeilBrown67918752014-12-15 12:57:01 +11005303 if (!err) {
5304 mddev->array_sectors = sectors;
5305 if (mddev->pers) {
5306 set_capacity(mddev->gendisk, mddev->array_sectors);
5307 revalidate_disk(mddev->gendisk);
5308 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11005309 }
NeilBrown67918752014-12-15 12:57:01 +11005310 mddev_unlock(mddev);
5311 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11005312}
5313
5314static struct md_sysfs_entry md_array_size =
5315__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5316 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005317
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005318static ssize_t
5319consistency_policy_show(struct mddev *mddev, char *page)
5320{
5321 int ret;
5322
5323 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5324 ret = sprintf(page, "journal\n");
5325 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5326 ret = sprintf(page, "ppl\n");
5327 } else if (mddev->bitmap) {
5328 ret = sprintf(page, "bitmap\n");
5329 } else if (mddev->pers) {
5330 if (mddev->pers->sync_request)
5331 ret = sprintf(page, "resync\n");
5332 else
5333 ret = sprintf(page, "none\n");
5334 } else {
5335 ret = sprintf(page, "unknown\n");
5336 }
5337
5338 return ret;
5339}
5340
5341static ssize_t
5342consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5343{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005344 int err = 0;
5345
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005346 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005347 if (mddev->pers->change_consistency_policy)
5348 err = mddev->pers->change_consistency_policy(mddev, buf);
5349 else
5350 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005351 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5352 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005353 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005354 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005355 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005356
5357 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005358}
5359
5360static struct md_sysfs_entry md_consistency_policy =
5361__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5362 consistency_policy_store);
5363
Guoqing Jiang9a567842019-07-24 11:09:19 +02005364static ssize_t fail_last_dev_show(struct mddev *mddev, char *page)
5365{
5366 return sprintf(page, "%d\n", mddev->fail_last_dev);
5367}
5368
5369/*
5370 * Setting fail_last_dev to true to allow last device to be forcibly removed
5371 * from RAID1/RAID10.
5372 */
5373static ssize_t
5374fail_last_dev_store(struct mddev *mddev, const char *buf, size_t len)
5375{
5376 int ret;
5377 bool value;
5378
5379 ret = kstrtobool(buf, &value);
5380 if (ret)
5381 return ret;
5382
5383 if (value != mddev->fail_last_dev)
5384 mddev->fail_last_dev = value;
5385
5386 return len;
5387}
5388static struct md_sysfs_entry md_fail_last_dev =
5389__ATTR(fail_last_dev, S_IRUGO | S_IWUSR, fail_last_dev_show,
5390 fail_last_dev_store);
5391
Guoqing Jiang3938f5f2019-12-23 10:48:56 +01005392static ssize_t serialize_policy_show(struct mddev *mddev, char *page)
5393{
5394 if (mddev->pers == NULL || (mddev->pers->level != 1))
5395 return sprintf(page, "n/a\n");
5396 else
5397 return sprintf(page, "%d\n", mddev->serialize_policy);
5398}
5399
5400/*
5401 * Setting serialize_policy to true to enforce write IO is not reordered
5402 * for raid1.
5403 */
5404static ssize_t
5405serialize_policy_store(struct mddev *mddev, const char *buf, size_t len)
5406{
5407 int err;
5408 bool value;
5409
5410 err = kstrtobool(buf, &value);
5411 if (err)
5412 return err;
5413
5414 if (value == mddev->serialize_policy)
5415 return len;
5416
5417 err = mddev_lock(mddev);
5418 if (err)
5419 return err;
5420 if (mddev->pers == NULL || (mddev->pers->level != 1)) {
5421 pr_err("md: serialize_policy is only effective for raid1\n");
5422 err = -EINVAL;
5423 goto unlock;
5424 }
5425
5426 mddev_suspend(mddev);
5427 if (value)
5428 mddev_create_serial_pool(mddev, NULL, true);
5429 else
5430 mddev_destroy_serial_pool(mddev, NULL, true);
5431 mddev->serialize_policy = value;
5432 mddev_resume(mddev);
5433unlock:
5434 mddev_unlock(mddev);
5435 return err ?: len;
5436}
5437
5438static struct md_sysfs_entry md_serialize_policy =
5439__ATTR(serialize_policy, S_IRUGO | S_IWUSR, serialize_policy_show,
5440 serialize_policy_store);
5441
5442
NeilBrowneae17012005-11-08 21:39:23 -08005443static struct attribute *md_default_attrs[] = {
5444 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005445 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005446 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005447 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005448 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005449 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005450 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005451 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005452 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005453 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005454 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005455 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005456 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005457 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005458 &md_consistency_policy.attr,
Guoqing Jiang9a567842019-07-24 11:09:19 +02005459 &md_fail_last_dev.attr,
Guoqing Jiang3938f5f2019-12-23 10:48:56 +01005460 &md_serialize_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005461 NULL,
5462};
5463
5464static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005465 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005466 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005467 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005468 &md_sync_min.attr,
5469 &md_sync_max.attr,
5470 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005471 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005472 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005473 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005474 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005475 &md_suspend_lo.attr,
5476 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005477 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005478 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005479 NULL,
5480};
NeilBrown411036f2005-11-08 21:39:40 -08005481static struct attribute_group md_redundancy_group = {
5482 .name = NULL,
5483 .attrs = md_redundancy_attrs,
5484};
5485
NeilBrowneae17012005-11-08 21:39:23 -08005486static ssize_t
5487md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5488{
5489 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005490 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005491 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005492
5493 if (!entry->show)
5494 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005495 spin_lock(&all_mddevs_lock);
5496 if (list_empty(&mddev->all_mddevs)) {
5497 spin_unlock(&all_mddevs_lock);
5498 return -EBUSY;
5499 }
5500 mddev_get(mddev);
5501 spin_unlock(&all_mddevs_lock);
5502
NeilBrownb7b17c92014-12-15 12:56:59 +11005503 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005504 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005505 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005506}
5507
5508static ssize_t
5509md_attr_store(struct kobject *kobj, struct attribute *attr,
5510 const char *page, size_t length)
5511{
5512 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005513 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005514 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005515
5516 if (!entry->store)
5517 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005518 if (!capable(CAP_SYS_ADMIN))
5519 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005520 spin_lock(&all_mddevs_lock);
5521 if (list_empty(&mddev->all_mddevs)) {
5522 spin_unlock(&all_mddevs_lock);
5523 return -EBUSY;
5524 }
5525 mddev_get(mddev);
5526 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005527 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005528 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005529 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005530}
5531
5532static void md_free(struct kobject *ko)
5533{
NeilBrownfd01b882011-10-11 16:47:53 +11005534 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005535
5536 if (mddev->sysfs_state)
5537 sysfs_put(mddev->sysfs_state);
5538
Bart Van Assched8115c352018-02-28 10:15:29 -08005539 if (mddev->gendisk)
5540 del_gendisk(mddev->gendisk);
NeilBrown6cd18e72015-04-27 14:12:22 +10005541 if (mddev->queue)
5542 blk_cleanup_queue(mddev->queue);
Bart Van Assched8115c352018-02-28 10:15:29 -08005543 if (mddev->gendisk)
NeilBrowna21d1502009-01-09 08:31:09 +11005544 put_disk(mddev->gendisk);
NeilBrown4ad23a972017-03-15 14:05:14 +11005545 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005546
Kent Overstreet28dec872018-06-07 20:52:54 -04005547 bioset_exit(&mddev->bio_set);
5548 bioset_exit(&mddev->sync_set);
NeilBrowneae17012005-11-08 21:39:23 -08005549 kfree(mddev);
5550}
5551
Emese Revfy52cf25d2010-01-19 02:58:23 +01005552static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005553 .show = md_attr_show,
5554 .store = md_attr_store,
5555};
5556static struct kobj_type md_ktype = {
5557 .release = md_free,
5558 .sysfs_ops = &md_sysfs_ops,
5559 .default_attrs = md_default_attrs,
5560};
5561
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562int mdp_major = 0;
5563
Dan Williams5fd3a172009-03-04 00:57:25 -07005564static void mddev_delayed_delete(struct work_struct *ws)
5565{
NeilBrownfd01b882011-10-11 16:47:53 +11005566 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005567
NeilBrown43a70502009-12-14 12:49:55 +11005568 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005569 kobject_del(&mddev->kobj);
5570 kobject_put(&mddev->kobj);
5571}
5572
NeilBrown4ad23a972017-03-15 14:05:14 +11005573static void no_op(struct percpu_ref *r) {}
5574
NeilBrowna415c0f2017-06-05 16:05:13 +10005575int mddev_init_writes_pending(struct mddev *mddev)
5576{
5577 if (mddev->writes_pending.percpu_count_ptr)
5578 return 0;
Roman Gushchinddde2af2019-05-07 10:01:49 -07005579 if (percpu_ref_init(&mddev->writes_pending, no_op,
5580 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL) < 0)
NeilBrowna415c0f2017-06-05 16:05:13 +10005581 return -ENOMEM;
5582 /* We want to start with the refcount at zero */
5583 percpu_ref_put(&mddev->writes_pending);
5584 return 0;
5585}
5586EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5587
NeilBrownefeb53c2009-01-09 08:31:10 +11005588static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589{
NeilBrown039b7222017-04-12 16:26:13 +10005590 /*
5591 * If dev is zero, name is the name of a device to allocate with
5592 * an arbitrary minor number. It will be "md_???"
5593 * If dev is non-zero it must be a device number with a MAJOR of
5594 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5595 * the device is being created by opening a node in /dev.
5596 * If "name" is not NULL, the device is being created by
5597 * writing to /sys/module/md_mod/parameters/new_array.
5598 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005599 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005600 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005602 int partitioned;
5603 int shift;
5604 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005605 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606
5607 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005608 return -ENODEV;
5609
5610 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5611 shift = partitioned ? MdpMinorShift : 0;
5612 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
Tejun Heoe804ac72010-10-15 15:36:08 +02005614 /* wait for any previous instance of this device to be
5615 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005616 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005617 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005618
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005619 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005620 error = -EEXIST;
5621 if (mddev->gendisk)
5622 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005623
NeilBrown039b7222017-04-12 16:26:13 +10005624 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005625 /* Need to ensure that 'name' is not a duplicate.
5626 */
NeilBrownfd01b882011-10-11 16:47:53 +11005627 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005628 spin_lock(&all_mddevs_lock);
5629
5630 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5631 if (mddev2->gendisk &&
5632 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5633 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005634 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005635 }
5636 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 }
NeilBrown039b7222017-04-12 16:26:13 +10005638 if (name && dev)
5639 /*
5640 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5641 */
5642 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005643
NeilBrown0909dc42009-07-01 12:27:21 +10005644 error = -ENOMEM;
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02005645 mddev->queue = blk_alloc_queue(NUMA_NO_NODE);
NeilBrown0909dc42009-07-01 12:27:21 +10005646 if (!mddev->queue)
5647 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005648
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,
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02007827 .submit_bio = md_submit_bio,
Al Viroa39907f2008-03-02 10:31:15 -05007828 .open = md_open,
7829 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007830 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007831#ifdef CONFIG_COMPAT
7832 .compat_ioctl = md_compat_ioctl,
7833#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007834 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007835 .media_changed = md_media_changed,
7836 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837};
7838
NeilBrownf72ffdd2014-09-30 14:23:59 +10007839static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840{
NeilBrown2b8bf342011-10-11 16:48:23 +11007841 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007842
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843 /*
7844 * md_thread is a 'system-thread', it's priority should be very
7845 * high. We avoid resource deadlocks individually in each
7846 * raid personality. (RAID5 does preallocation) We also use RR and
7847 * the very same RT priority as kswapd, thus we will never get
7848 * into a priority inversion deadlock.
7849 *
7850 * we definitely have to have equal or higher priority than
7851 * bdflush, otherwise bdflush will deadlock if there are too
7852 * many dirty RAID5 blocks.
7853 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854
NeilBrown6985c432005-10-19 21:23:47 -07007855 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007856 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857
NeilBrown93588e22005-11-15 00:09:12 -08007858 /* We need to wait INTERRUPTIBLE so that
7859 * we don't add to the load-average.
7860 * That means we need to be sure no signals are
7861 * pending
7862 */
7863 if (signal_pending(current))
7864 flush_signals(current);
7865
7866 wait_event_interruptible_timeout
7867 (thread->wqueue,
7868 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007869 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007870 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007871
NeilBrown6c987912011-01-14 09:13:53 +11007872 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007873 if (kthread_should_park())
7874 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007875 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007876 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007878
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879 return 0;
7880}
7881
NeilBrown2b8bf342011-10-11 16:48:23 +11007882void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007883{
7884 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007885 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Guoqing Jiangd1d90142017-10-09 10:32:48 +08007886 set_bit(THREAD_WAKEUP, &thread->flags);
7887 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888 }
7889}
NeilBrown6c144d32014-09-30 16:15:38 +10007890EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891
Shaohua Li4ed87312012-10-11 13:34:00 +11007892struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7893 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894{
NeilBrown2b8bf342011-10-11 16:48:23 +11007895 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007896
NeilBrown2b8bf342011-10-11 16:48:23 +11007897 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898 if (!thread)
7899 return NULL;
7900
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901 init_waitqueue_head(&thread->wqueue);
7902
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903 thread->run = run;
7904 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007905 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007906 thread->tsk = kthread_run(md_thread, thread,
7907 "%s_%s",
7908 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007909 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007910 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911 kfree(thread);
7912 return NULL;
7913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914 return thread;
7915}
NeilBrown6c144d32014-09-30 16:15:38 +10007916EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917
NeilBrown2b8bf342011-10-11 16:48:23 +11007918void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919{
NeilBrown2b8bf342011-10-11 16:48:23 +11007920 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007921 if (!thread)
7922 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007923 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007924 /* Locking ensures that mddev_unlock does not wake_up a
7925 * non-existent thread
7926 */
7927 spin_lock(&pers_lock);
7928 *threadp = NULL;
7929 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007930
7931 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932 kfree(thread);
7933}
NeilBrown6c144d32014-09-30 16:15:38 +10007934EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935
NeilBrownfd01b882011-10-11 16:47:53 +11007936void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937{
NeilBrownb2d444d2005-11-08 21:39:31 -08007938 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007939 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007940
NeilBrownde393cd2011-07-28 11:31:48 +10007941 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007942 return;
7943 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007944 if (mddev->degraded)
7945 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007946 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007947 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7948 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7949 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007950 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007951 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007952 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953}
NeilBrown6c144d32014-09-30 16:15:38 +10007954EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007955
7956/* seq_file implementation /proc/mdstat */
7957
7958static void status_unused(struct seq_file *seq)
7959{
7960 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007961 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007962
7963 seq_printf(seq, "unused devices: ");
7964
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007965 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966 char b[BDEVNAME_SIZE];
7967 i++;
7968 seq_printf(seq, "%s ",
7969 bdevname(rdev->bdev,b));
7970 }
7971 if (!i)
7972 seq_printf(seq, "<none>");
7973
7974 seq_printf(seq, "\n");
7975}
7976
NeilBrownf7851be2015-07-02 17:12:58 +10007977static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007978{
NeilBrowndd71cf62009-05-07 12:49:35 +10007979 sector_t max_sectors, resync, res;
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02007980 unsigned long dt, db = 0;
7981 sector_t rt, curr_mark_cnt, resync_mark_cnt;
7982 int scale, recovery_active;
NeilBrown4588b422006-03-27 01:18:04 -08007983 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007984
NeilBrownc804cde2012-05-21 09:28:33 +10007985 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7986 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007987 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007988 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007989 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007990
NeilBrownf7851be2015-07-02 17:12:58 +10007991 resync = mddev->curr_resync;
7992 if (resync <= 3) {
7993 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7994 /* Still cleaning up */
7995 resync = max_sectors;
Nate Daileyd2e2ec82017-11-30 11:33:30 -05007996 } else if (resync > max_sectors)
7997 resync = max_sectors;
7998 else
NeilBrownf7851be2015-07-02 17:12:58 +10007999 resync -= atomic_read(&mddev->recovery_active);
8000
8001 if (resync == 0) {
Guoqing Jiang0357ba22018-07-02 16:26:25 +08008002 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
8003 struct md_rdev *rdev;
8004
8005 rdev_for_each(rdev, mddev)
8006 if (rdev->raid_disk >= 0 &&
8007 !test_bit(Faulty, &rdev->flags) &&
8008 rdev->recovery_offset != MaxSector &&
8009 rdev->recovery_offset) {
8010 seq_printf(seq, "\trecover=REMOTE");
8011 return 1;
8012 }
8013 if (mddev->reshape_position != MaxSector)
8014 seq_printf(seq, "\treshape=REMOTE");
8015 else
8016 seq_printf(seq, "\tresync=REMOTE");
8017 return 1;
8018 }
NeilBrownf7851be2015-07-02 17:12:58 +10008019 if (mddev->recovery_cp < MaxSector) {
8020 seq_printf(seq, "\tresync=PENDING");
8021 return 1;
8022 }
8023 return 0;
8024 }
8025 if (resync < 3) {
8026 seq_printf(seq, "\tresync=DELAYED");
8027 return 1;
8028 }
8029
NeilBrown403df472014-09-30 15:52:29 +10008030 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08008031 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10008032 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08008033 * u32, as those are the requirements for sector_div.
8034 * Thus 'scale' must be at least 10
8035 */
8036 scale = 10;
8037 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10008038 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08008039 scale++;
8040 }
8041 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10008042 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08008043
8044 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045 {
NeilBrown4588b422006-03-27 01:18:04 -08008046 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008047 seq_printf(seq, "[");
8048 for (i = 0; i < x; i++)
8049 seq_printf(seq, "=");
8050 seq_printf(seq, ">");
8051 for (i = 0; i < y; i++)
8052 seq_printf(seq, ".");
8053 seq_printf(seq, "] ");
8054 }
NeilBrown4588b422006-03-27 01:18:04 -08008055 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08008056 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
8057 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07008058 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
8059 "check" :
8060 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
8061 "resync" : "recovery"))),
8062 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10008063 (unsigned long long) resync/2,
8064 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008065
8066 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07008067 * dt: time from mark until now
8068 * db: blocks written from mark until now
8069 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10008070 *
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008071 * rt is a sector_t, which is always 64bit now. We are keeping
8072 * the original algorithm, but it is not really necessary.
8073 *
8074 * Original algorithm:
8075 * So we divide before multiply in case it is 32bit and close
8076 * to the limit.
8077 * We scale the divisor (db) by 32 to avoid losing precision
8078 * near the end of resync when the number of remaining sectors
8079 * is close to 'db'.
8080 * We then divide rt by 32 after multiplying by db to compensate.
8081 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008082 */
8083 dt = ((jiffies - mddev->resync_mark) / HZ);
8084 if (!dt) dt++;
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008085
8086 curr_mark_cnt = mddev->curr_mark_cnt;
8087 recovery_active = atomic_read(&mddev->recovery_active);
8088 resync_mark_cnt = mddev->resync_mark_cnt;
8089
8090 if (curr_mark_cnt >= (recovery_active + resync_mark_cnt))
8091 db = curr_mark_cnt - (recovery_active + resync_mark_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008092
NeilBrowndd71cf62009-05-07 12:49:35 +10008093 rt = max_sectors - resync; /* number of remaining sectors */
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008094 rt = div64_u64(rt, db/32+1);
NeilBrowndd71cf62009-05-07 12:49:35 +10008095 rt *= dt;
8096 rt >>= 5;
8097
8098 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
8099 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008100
NeilBrownff4e8d92006-07-10 04:44:16 -07008101 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10008102 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008103}
8104
8105static void *md_seq_start(struct seq_file *seq, loff_t *pos)
8106{
8107 struct list_head *tmp;
8108 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11008109 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008110
8111 if (l >= 0x10000)
8112 return NULL;
8113 if (!l--)
8114 /* header */
8115 return (void*)1;
8116
8117 spin_lock(&all_mddevs_lock);
8118 list_for_each(tmp,&all_mddevs)
8119 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11008120 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008121 mddev_get(mddev);
8122 spin_unlock(&all_mddevs_lock);
8123 return mddev;
8124 }
8125 spin_unlock(&all_mddevs_lock);
8126 if (!l--)
8127 return (void*)2;/* tail */
8128 return NULL;
8129}
8130
8131static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
8132{
8133 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008134 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10008135
Linus Torvalds1da177e2005-04-16 15:20:36 -07008136 ++*pos;
8137 if (v == (void*)2)
8138 return NULL;
8139
8140 spin_lock(&all_mddevs_lock);
8141 if (v == (void*)1)
8142 tmp = all_mddevs.next;
8143 else
8144 tmp = mddev->all_mddevs.next;
8145 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11008146 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008147 else {
8148 next_mddev = (void*)2;
8149 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10008150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151 spin_unlock(&all_mddevs_lock);
8152
8153 if (v != (void*)1)
8154 mddev_put(mddev);
8155 return next_mddev;
8156
8157}
8158
8159static void md_seq_stop(struct seq_file *seq, void *v)
8160{
NeilBrownfd01b882011-10-11 16:47:53 +11008161 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008162
8163 if (mddev && v != (void*)1 && v != (void*)2)
8164 mddev_put(mddev);
8165}
8166
8167static int md_seq_show(struct seq_file *seq, void *v)
8168{
NeilBrownfd01b882011-10-11 16:47:53 +11008169 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11008170 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11008171 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008172
8173 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11008174 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008175 seq_printf(seq, "Personalities : ");
8176 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008177 list_for_each_entry(pers, &pers_list, list)
8178 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008179
8180 spin_unlock(&pers_lock);
8181 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02008182 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183 return 0;
8184 }
8185 if (v == (void*)2) {
8186 status_unused(seq);
8187 return 0;
8188 }
8189
NeilBrown36d091f2014-12-15 12:56:58 +11008190 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008191 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
8192 seq_printf(seq, "%s : %sactive", mdname(mddev),
8193 mddev->pers ? "" : "in");
8194 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08008195 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08008197 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07008198 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008199 seq_printf(seq, " %s", mddev->pers->name);
8200 }
8201
Andre Nolldd8ac332009-03-31 14:33:13 +11008202 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11008203 rcu_read_lock();
8204 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008205 char b[BDEVNAME_SIZE];
8206 seq_printf(seq, " %s[%d]",
8207 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07008208 if (test_bit(WriteMostly, &rdev->flags))
8209 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07008210 if (test_bit(Journal, &rdev->flags))
8211 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08008212 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008213 seq_printf(seq, "(F)");
8214 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11008215 }
8216 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07008217 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11008218 if (test_bit(Replacement, &rdev->flags))
8219 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11008220 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008221 }
NeilBrownf97fcad2014-12-15 12:56:59 +11008222 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008223
8224 if (!list_empty(&mddev->disks)) {
8225 if (mddev->pers)
8226 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10008227 (unsigned long long)
8228 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008229 else
8230 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11008231 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008232 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07008233 if (mddev->persistent) {
8234 if (mddev->major_version != 0 ||
8235 mddev->minor_version != 90) {
8236 seq_printf(seq," super %d.%d",
8237 mddev->major_version,
8238 mddev->minor_version);
8239 }
NeilBrowne6910632008-02-06 01:39:51 -08008240 } else if (mddev->external)
8241 seq_printf(seq, " super external:%s",
8242 mddev->metadata_type);
8243 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07008244 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008245
8246 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11008247 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10008248 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08008249 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10008250 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08008251 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08008252 }
NeilBrown32a76272005-06-21 17:17:14 -07008253 } else
8254 seq_printf(seq, "\n ");
8255
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008256 md_bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008257
8258 seq_printf(seq, "\n");
8259 }
NeilBrown36d091f2014-12-15 12:56:58 +11008260 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10008261
Linus Torvalds1da177e2005-04-16 15:20:36 -07008262 return 0;
8263}
8264
Jan Engelhardt110518b2009-05-07 12:49:37 +10008265static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008266 .start = md_seq_start,
8267 .next = md_seq_next,
8268 .stop = md_seq_stop,
8269 .show = md_seq_show,
8270};
8271
8272static int md_seq_open(struct inode *inode, struct file *file)
8273{
Kay Sieversf1514632011-07-12 20:48:39 +02008274 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008275 int error;
8276
8277 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08008278 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02008279 return error;
8280
8281 seq = file->private_data;
8282 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008283 return error;
8284}
8285
NeilBrowne2f23b62014-04-09 14:33:51 +10008286static int md_unloading;
Al Viroafc9a422017-07-03 06:39:46 -04008287static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
NeilBrownd7603b72006-01-06 00:20:30 -08008288{
Kay Sieversf1514632011-07-12 20:48:39 +02008289 struct seq_file *seq = filp->private_data;
Al Viroafc9a422017-07-03 06:39:46 -04008290 __poll_t mask;
NeilBrownd7603b72006-01-06 00:20:30 -08008291
NeilBrowne2f23b62014-04-09 14:33:51 +10008292 if (md_unloading)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008293 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08008294 poll_wait(filp, &md_event_waiters, wait);
8295
8296 /* always allow read */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008297 mask = EPOLLIN | EPOLLRDNORM;
NeilBrownd7603b72006-01-06 00:20:30 -08008298
Kay Sieversf1514632011-07-12 20:48:39 +02008299 if (seq->poll_event != atomic_read(&md_event_count))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008300 mask |= EPOLLERR | EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08008301 return mask;
8302}
8303
Alexey Dobriyan97a32532020-02-03 17:37:17 -08008304static const struct proc_ops mdstat_proc_ops = {
8305 .proc_open = md_seq_open,
8306 .proc_read = seq_read,
8307 .proc_lseek = seq_lseek,
8308 .proc_release = seq_release,
8309 .proc_poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008310};
8311
NeilBrown84fc4b52011-10-11 16:49:58 +11008312int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313{
NeilBrown9d487392016-11-02 14:16:49 +11008314 pr_debug("md: %s personality registered for level %d\n",
8315 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008316 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008317 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008318 spin_unlock(&pers_lock);
8319 return 0;
8320}
NeilBrown6c144d32014-09-30 16:15:38 +10008321EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008322
NeilBrown84fc4b52011-10-11 16:49:58 +11008323int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324{
NeilBrown9d487392016-11-02 14:16:49 +11008325 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008326 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008327 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008328 spin_unlock(&pers_lock);
8329 return 0;
8330}
NeilBrown6c144d32014-09-30 16:15:38 +10008331EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008332
NeilBrown6022e752015-08-13 12:32:55 +10008333int register_md_cluster_operations(struct md_cluster_operations *ops,
8334 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008335{
NeilBrown6022e752015-08-13 12:32:55 +10008336 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008337 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008338 if (md_cluster_ops != NULL)
8339 ret = -EALREADY;
8340 else {
8341 md_cluster_ops = ops;
8342 md_cluster_mod = module;
8343 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008344 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008345 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008346}
8347EXPORT_SYMBOL(register_md_cluster_operations);
8348
8349int unregister_md_cluster_operations(void)
8350{
8351 spin_lock(&pers_lock);
8352 md_cluster_ops = NULL;
8353 spin_unlock(&pers_lock);
8354 return 0;
8355}
8356EXPORT_SYMBOL(unregister_md_cluster_operations);
8357
8358int md_setup_cluster(struct mddev *mddev, int nodes)
8359{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008360 if (!md_cluster_ops)
8361 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008362 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008363 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008364 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11008365 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008366 spin_unlock(&pers_lock);
8367 return -ENOENT;
8368 }
8369 spin_unlock(&pers_lock);
8370
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05008371 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008372}
8373
8374void md_cluster_stop(struct mddev *mddev)
8375{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05008376 if (!md_cluster_ops)
8377 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008378 md_cluster_ops->leave(mddev);
8379 module_put(md_cluster_mod);
8380}
8381
NeilBrownfd01b882011-10-11 16:47:53 +11008382static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008383{
NeilBrownf72ffdd2014-09-30 14:23:59 +10008384 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008385 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11008386 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008387
8388 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10008389 rcu_read_lock();
8390 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008391 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Michael Callahan59767fb2018-07-18 04:47:37 -07008392 curr_events = (int)part_stat_read_accum(&disk->part0, sectors) -
NeilBrowneea1bf32009-03-31 14:27:02 +11008393 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07008394 /* sync IO will cause sync_io to increase before the disk_stats
8395 * as sync_io is counted when a request starts, and
8396 * disk_stats is counted when it completes.
8397 * So resync activity will cause curr_events to be smaller than
8398 * when there was no such activity.
8399 * non-sync IO will cause disk_stat to increase without
8400 * increasing sync_io so curr_events will (eventually)
8401 * be larger than it was before. Once it becomes
8402 * substantially larger, the test below will cause
8403 * the array to appear non-idle, and resync will slow
8404 * down.
8405 * If there is a lot of outstanding resync activity when
8406 * we set last_event to curr_events, then all that activity
8407 * completing might cause the array to appear non-idle
8408 * and resync will be slowed down even though there might
8409 * not have been non-resync activity. This will only
8410 * happen once though. 'last_events' will soon reflect
8411 * the state where there is little or no outstanding
8412 * resync requests, and further resync activity will
8413 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08008414 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415 */
NeilBrowneea1bf32009-03-31 14:27:02 +11008416 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008417 rdev->last_events = curr_events;
8418 idle = 0;
8419 }
8420 }
NeilBrown4b809912008-07-21 17:05:25 +10008421 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008422 return idle;
8423}
8424
NeilBrownfd01b882011-10-11 16:47:53 +11008425void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008426{
8427 /* another "blocks" (512byte) blocks have been synced */
8428 atomic_sub(blocks, &mddev->recovery_active);
8429 wake_up(&mddev->recovery_wait);
8430 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07008431 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08008432 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008433 md_wakeup_thread(mddev->thread);
8434 // stop recovery, signal do_sync ....
8435 }
8436}
NeilBrown6c144d32014-09-30 16:15:38 +10008437EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008438
NeilBrown06d91a52005-06-21 17:17:12 -07008439/* md_write_start(mddev, bi)
8440 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07008441 * in superblock) before writing, schedule a superblock update
8442 * and wait for it to complete.
NeilBrowncc27b0c2017-06-05 16:49:39 +10008443 * A return value of 'false' means that the write wasn't recorded
8444 * and cannot proceed as the array is being suspend.
NeilBrown06d91a52005-06-21 17:17:12 -07008445 */
NeilBrowncc27b0c2017-06-05 16:49:39 +10008446bool md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008447{
Neil Brown0fd62b82008-06-28 08:31:36 +10008448 int did_change = 0;
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008449
NeilBrown06d91a52005-06-21 17:17:12 -07008450 if (bio_data_dir(bi) != WRITE)
NeilBrowncc27b0c2017-06-05 16:49:39 +10008451 return true;
NeilBrown06d91a52005-06-21 17:17:12 -07008452
NeilBrownf91de922005-11-08 21:39:36 -08008453 BUG_ON(mddev->ro == 1);
8454 if (mddev->ro == 2) {
8455 /* need to switch to read/write */
8456 mddev->ro = 0;
8457 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8458 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08008459 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008460 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08008461 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008462 rcu_read_lock();
8463 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11008464 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07008465 if (mddev->safemode == 1)
8466 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11008467 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
NeilBrown81fe48e2017-08-08 16:56:36 +10008468 if (mddev->in_sync || mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11008469 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07008470 if (mddev->in_sync) {
8471 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008472 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8473 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07008474 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008475 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07008476 }
NeilBrown85572d72014-12-15 12:56:56 +11008477 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07008478 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008479 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10008480 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008481 sysfs_notify_dirent_safe(mddev->sysfs_state);
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008482 if (!mddev->has_superblocks)
8483 return true;
NeilBrown09a44cc2008-05-23 13:04:36 -07008484 wait_event(mddev->sb_wait,
NeilBrownd47c8ad2017-10-05 16:23:16 +11008485 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8486 mddev->suspended);
NeilBrowncc27b0c2017-06-05 16:49:39 +10008487 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8488 percpu_ref_put(&mddev->writes_pending);
8489 return false;
8490 }
8491 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008492}
NeilBrown6c144d32014-09-30 16:15:38 +10008493EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008494
NeilBrown49728052017-03-15 14:05:12 +11008495/* md_write_inc can only be called when md_write_start() has
8496 * already been called at least once of the current request.
8497 * It increments the counter and is useful when a single request
8498 * is split into several parts. Each part causes an increment and
8499 * so needs a matching md_write_end().
8500 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8501 * a spinlocked region.
8502 */
8503void md_write_inc(struct mddev *mddev, struct bio *bi)
8504{
8505 if (bio_data_dir(bi) != WRITE)
8506 return;
8507 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11008508 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008509}
8510EXPORT_SYMBOL(md_write_inc);
8511
NeilBrownfd01b882011-10-11 16:47:53 +11008512void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513{
NeilBrown4ad23a972017-03-15 14:05:14 +11008514 percpu_ref_put(&mddev->writes_pending);
8515
8516 if (mddev->safemode == 2)
8517 md_wakeup_thread(mddev->thread);
8518 else if (mddev->safemode_delay)
8519 /* The roundup() ensures this only performs locking once
8520 * every ->safemode_delay jiffies
8521 */
8522 mod_timer(&mddev->safemode_timer,
8523 roundup(jiffies, mddev->safemode_delay) +
8524 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008525}
NeilBrown4ad23a972017-03-15 14:05:14 +11008526
NeilBrown6c144d32014-09-30 16:15:38 +10008527EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008528
NeilBrown2a2275d2007-01-26 00:57:11 -08008529/* md_allow_write(mddev)
8530 * Calling this ensures that the array is marked 'active' so that writes
8531 * may proceed without blocking. It is important to call this before
8532 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8533 * Must be called with mddev_lock held.
8534 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008535void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008536{
8537 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008538 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008539 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008540 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008541 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008542 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008543
NeilBrown85572d72014-12-15 12:56:56 +11008544 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008545 if (mddev->in_sync) {
8546 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008547 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8548 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008549 if (mddev->safemode_delay &&
8550 mddev->safemode == 0)
8551 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008552 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008553 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008554 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008555 /* wait for the dirty state to be recorded in the metadata */
8556 wait_event(mddev->sb_wait,
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008557 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008558 } else
NeilBrown85572d72014-12-15 12:56:56 +11008559 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008560}
8561EXPORT_SYMBOL_GPL(md_allow_write);
8562
Linus Torvalds1da177e2005-04-16 15:20:36 -07008563#define SYNC_MARKS 10
8564#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008565#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008566void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008567{
Shaohua Li4ed87312012-10-11 13:34:00 +11008568 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008569 struct mddev *mddev2;
Yufen Yue5b521e2019-06-14 15:41:07 -07008570 unsigned int currspeed = 0, window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008571 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008572 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008573 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008574 sector_t mark_cnt[SYNC_MARKS];
8575 int last_mark,m;
8576 struct list_head *tmp;
8577 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008578 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008579 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008580 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008581 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008582 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008583
8584 /* just incase thread restarts... */
Song Liud5d885f2017-11-19 22:17:01 -08008585 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8586 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008587 return;
NeilBrown3991b312014-05-28 13:39:23 +10008588 if (mddev->ro) {/* never try to sync a read-only array */
8589 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008590 return;
NeilBrown3991b312014-05-28 13:39:23 +10008591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008592
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008593 if (mddev_is_clustered(mddev)) {
8594 ret = md_cluster_ops->resync_start(mddev);
8595 if (ret)
8596 goto skip;
8597
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008598 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008599 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8600 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8601 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8602 && ((unsigned long long)mddev->curr_resync_completed
8603 < (unsigned long long)mddev->resync_max_sectors))
8604 goto skip;
8605 }
8606
NeilBrown61df9d92006-10-03 01:15:57 -07008607 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008608 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008609 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008610 action = "check";
8611 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008612 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008613 action = "repair";
8614 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008615 desc = "resync";
8616 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8617 desc = "reshape";
8618 else
8619 desc = "recovery";
8620
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008621 mddev->last_sync_action = action ?: desc;
8622
Linus Torvalds1da177e2005-04-16 15:20:36 -07008623 /* we overload curr_resync somewhat here.
8624 * 0 == not engaged in resync at all
8625 * 2 == checking that there is no conflict with another sync
8626 * 1 == like 2, but have yielded to allow conflicting resync to
Yufen Yue5b521e2019-06-14 15:41:07 -07008627 * commence
Linus Torvalds1da177e2005-04-16 15:20:36 -07008628 * other == active in resync - this many blocks
8629 *
8630 * Before starting a resync we must have set curr_resync to
8631 * 2, and then checked that every "conflicting" array has curr_resync
8632 * less than ours. When we find one that is the same or higher
8633 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8634 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8635 * This will mean we have to start checking from the beginning again.
8636 *
8637 */
8638
8639 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008640 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641 mddev->curr_resync = 2;
8642
8643 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008644 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008646 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647 if (mddev2 == mddev)
8648 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008649 if (!mddev->parallel_resync
8650 && mddev2->curr_resync
8651 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008652 DEFINE_WAIT(wq);
8653 if (mddev < mddev2 && mddev->curr_resync == 2) {
8654 /* arbitrarily yield */
8655 mddev->curr_resync = 1;
8656 wake_up(&resync_wait);
8657 }
8658 if (mddev > mddev2 && mddev->curr_resync == 1)
8659 /* no need to wait here, we can wait the next
8660 * time 'round when curr_resync == 2
8661 */
8662 continue;
NeilBrown97441972008-09-19 11:49:54 +10008663 /* We need to wait 'interruptible' so as not to
8664 * contribute to the load average, and not to
8665 * be caught by 'softlockup'
8666 */
8667 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008668 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008669 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008670 if (mddev2_minor != mddev2->md_minor) {
8671 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008672 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8673 desc, mdname(mddev),
8674 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008676 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008677 if (signal_pending(current))
8678 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008679 schedule();
8680 finish_wait(&resync_wait, &wq);
8681 goto try_again;
8682 }
8683 finish_wait(&resync_wait, &wq);
8684 }
8685 }
8686 } while (mddev->curr_resync < 2);
8687
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008688 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008689 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008690 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008691 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008692 */
8693 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008694 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008695 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008696 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8697 j = mddev->resync_min;
8698 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008699 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008700
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008701 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
NeilBrownc804cde2012-05-21 09:28:33 +10008702 max_sectors = mddev->resync_max_sectors;
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008703 /*
8704 * If the original node aborts reshaping then we continue the
8705 * reshaping, so set j again to avoid restart reshape from the
8706 * first beginning
8707 */
8708 if (mddev_is_clustered(mddev) &&
8709 mddev->reshape_position != MaxSector)
8710 j = mddev->reshape_position;
8711 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008712 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008713 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008714 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008715 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008716 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008717 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008718 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008719 !test_bit(Faulty, &rdev->flags) &&
8720 !test_bit(In_sync, &rdev->flags) &&
8721 rdev->recovery_offset < j)
8722 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008723 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008724
8725 /* If there is a bitmap, we need to make sure all
8726 * writes that started before we added a spare
8727 * complete before we start doing a recovery.
8728 * Otherwise the write might complete and (via
8729 * bitmap_endwrite) set a bit in the bitmap after the
8730 * recovery has checked that bit and skipped that
8731 * region.
8732 */
8733 if (mddev->bitmap) {
8734 mddev->pers->quiesce(mddev, 1);
8735 mddev->pers->quiesce(mddev, 0);
8736 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008738
NeilBrown9d487392016-11-02 14:16:49 +11008739 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8740 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8741 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8742 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008743
NeilBrowneea1bf32009-03-31 14:27:02 +11008744 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008745
NeilBrown57afd892005-06-21 17:17:13 -07008746 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008747 for (m = 0; m < SYNC_MARKS; m++) {
8748 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008749 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008750 }
8751 last_mark = 0;
8752 mddev->resync_mark = mark[last_mark];
8753 mddev->resync_mark_cnt = mark_cnt[last_mark];
8754
8755 /*
8756 * Tune reconstruction:
8757 */
Yufen Yue5b521e2019-06-14 15:41:07 -07008758 window = 32 * (PAGE_SIZE / 512);
NeilBrown9d487392016-11-02 14:16:49 +11008759 pr_debug("md: using %dk window, over a total of %lluk.\n",
8760 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008761
8762 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008763 last_check = 0;
8764
8765 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008766 pr_debug("md: resuming %s of %s from checkpoint.\n",
8767 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008768 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008769 } else
8770 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008771 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008772 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8773 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008774 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008775
majianpeng7c2c57c2012-07-03 12:12:26 +10008776 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008777 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008778 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008779
NeilBrown57afd892005-06-21 17:17:13 -07008780 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008781
NeilBrown7a91ee12009-05-26 12:57:21 +10008782 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8783 ((mddev->curr_resync > mddev->curr_resync_completed &&
8784 (mddev->curr_resync - mddev->curr_resync_completed)
8785 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008786 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008787 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008788 >= mddev->resync_max - mddev->curr_resync_completed ||
8789 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008790 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008791 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008792 wait_event(mddev->recovery_wait,
8793 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008794 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008795 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8796 j > mddev->recovery_cp)
8797 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008798 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008799 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008800 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008801 }
NeilBrownacb180b2009-04-14 16:28:34 +10008802
NeilBrownc91abf52013-11-19 12:02:01 +11008803 while (j >= mddev->resync_max &&
8804 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008805 /* As this condition is controlled by user-space,
8806 * we can block indefinitely, so use '_interruptible'
8807 * to avoid triggering warnings.
8808 */
8809 flush_signals(current); /* just in case */
8810 wait_event_interruptible(mddev->recovery_wait,
8811 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008812 || test_bit(MD_RECOVERY_INTR,
8813 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008814 }
NeilBrownacb180b2009-04-14 16:28:34 +10008815
NeilBrownc91abf52013-11-19 12:02:01 +11008816 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8817 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008818
NeilBrown09314792015-02-19 16:04:40 +11008819 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008820 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008821 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008822 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008823 }
NeilBrown57afd892005-06-21 17:17:13 -07008824
8825 if (!skipped) { /* actual IO requested */
8826 io_sectors += sectors;
8827 atomic_add(sectors, &mddev->recovery_active);
8828 }
8829
NeilBrowne875ece2011-07-28 11:39:24 +10008830 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8831 break;
8832
Linus Torvalds1da177e2005-04-16 15:20:36 -07008833 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008834 if (j > max_sectors)
8835 /* when skipping, extra large numbers can be returned. */
8836 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008837 if (j > 2)
8838 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008839 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008840 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008841 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008842 * visible in /proc/mdstat
8843 */
8844 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008845
8846 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008847 continue;
8848
NeilBrown57afd892005-06-21 17:17:13 -07008849 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008850 repeat:
8851 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8852 /* step marks */
8853 int next = (last_mark+1) % SYNC_MARKS;
8854
8855 mddev->resync_mark = mark[next];
8856 mddev->resync_mark_cnt = mark_cnt[next];
8857 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008858 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008859 last_mark = next;
8860 }
8861
NeilBrownc91abf52013-11-19 12:02:01 +11008862 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8863 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008864
8865 /*
8866 * this loop exits only if either when we are slower than
8867 * the 'hard' speed limit, or the system was IO-idle for
8868 * a jiffy.
8869 * the system might be non-idle CPU-wise, but we only care
8870 * about not overloading the IO subsystem. (things like an
8871 * e2fsck being done on the RAID array should execute fast)
8872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008873 cond_resched();
8874
Xiao Niac7e50a2014-08-07 09:37:41 -04008875 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8876 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008877 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008878
NeilBrown88202a02006-01-06 00:21:36 -08008879 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008880 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008881 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008882 goto repeat;
8883 }
NeilBrownac8fa412015-02-19 16:55:00 +11008884 if (!is_mddev_idle(mddev, 0)) {
8885 /*
8886 * Give other IO more of a chance.
8887 * The faster the devices, the less we wait.
8888 */
8889 wait_event(mddev->recovery_wait,
8890 !atomic_read(&mddev->recovery_active));
8891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008892 }
8893 }
NeilBrown9d487392016-11-02 14:16:49 +11008894 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8895 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8896 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008897 /*
8898 * this also signals 'finished resyncing' to md_stop
8899 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008900 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008901 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8902
NeilBrown5ed1df22015-07-24 13:27:08 +10008903 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8904 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008905 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008906 mddev->curr_resync_completed = mddev->curr_resync;
8907 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8908 }
NeilBrown09314792015-02-19 16:04:40 +11008909 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008910
NeilBrowndfc70642008-05-23 13:04:39 -07008911 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008912 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008913 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8914 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8915 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008916 pr_debug("md: checkpointing %s of %s.\n",
8917 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008918 if (test_bit(MD_RECOVERY_ERROR,
8919 &mddev->recovery))
8920 mddev->recovery_cp =
8921 mddev->curr_resync_completed;
8922 else
8923 mddev->recovery_cp =
8924 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008925 }
8926 } else
8927 mddev->recovery_cp = MaxSector;
8928 } else {
8929 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8930 mddev->curr_resync = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11008931 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8932 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
8933 rcu_read_lock();
8934 rdev_for_each_rcu(rdev, mddev)
8935 if (rdev->raid_disk >= 0 &&
8936 mddev->delta_disks >= 0 &&
8937 !test_bit(Journal, &rdev->flags) &&
8938 !test_bit(Faulty, &rdev->flags) &&
8939 !test_bit(In_sync, &rdev->flags) &&
8940 rdev->recovery_offset < mddev->curr_resync)
8941 rdev->recovery_offset = mddev->curr_resync;
8942 rcu_read_unlock();
8943 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008946 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008947 /* set CHANGE_PENDING here since maybe another update is needed,
8948 * so other nodes are informed. It should be harmless for normal
8949 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008950 set_mask_bits(&mddev->sb_flags, 0,
8951 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008952
BingJing Chang88763912018-02-22 13:34:46 +08008953 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8954 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8955 mddev->delta_disks > 0 &&
8956 mddev->pers->finish_reshape &&
8957 mddev->pers->size &&
8958 mddev->queue) {
8959 mddev_lock_nointr(mddev);
8960 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
8961 mddev_unlock(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08008962 if (!mddev_is_clustered(mddev)) {
8963 set_capacity(mddev->gendisk, mddev->array_sectors);
8964 revalidate_disk(mddev->gendisk);
8965 }
BingJing Chang88763912018-02-22 13:34:46 +08008966 }
8967
NeilBrown23da4222014-12-15 12:57:01 +11008968 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008969 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8970 /* We completed so min/max setting can be forgotten if used. */
8971 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8972 mddev->resync_min = 0;
8973 mddev->resync_max = MaxSector;
8974 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8975 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008976 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008977 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008978 spin_unlock(&mddev->lock);
8979
Linus Torvalds1da177e2005-04-16 15:20:36 -07008980 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008982 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008983}
NeilBrown29269552006-03-27 01:18:10 -08008984EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008985
NeilBrown746d3202013-04-24 11:42:41 +10008986static int remove_and_add_spares(struct mddev *mddev,
8987 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008988{
NeilBrown3cb03002011-10-11 16:45:26 +11008989 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008990 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008991 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008992 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008993
NeilBrown39772f02018-02-03 09:19:30 +11008994 if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
8995 /* Mustn't remove devices when resync thread is running */
8996 return 0;
8997
NeilBrownd787be42016-06-02 16:19:53 +10008998 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008999 if ((this == NULL || rdev == this) &&
9000 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07009001 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10009002 test_bit(Faulty, &rdev->flags) &&
9003 atomic_read(&rdev->nr_pending)==0) {
9004 /* Faulty non-Blocked devices with nr_pending == 0
9005 * never get nr_pending incremented,
9006 * never get Faulty cleared, and never get Blocked set.
9007 * So we can synchronize_rcu now rather than once per device
9008 */
9009 remove_some = true;
9010 set_bit(RemoveSynchronized, &rdev->flags);
9011 }
9012 }
9013
9014 if (remove_some)
9015 synchronize_rcu();
9016 rdev_for_each(rdev, mddev) {
9017 if ((this == NULL || rdev == this) &&
9018 rdev->raid_disk >= 0 &&
9019 !test_bit(Blocked, &rdev->flags) &&
9020 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07009021 (!test_bit(In_sync, &rdev->flags) &&
9022 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10009023 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009024 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11009025 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10009026 sysfs_unlink_rdev(mddev, rdev);
NeilBrown011abdc2018-04-26 14:46:29 +10009027 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009028 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11009029 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009030 }
9031 }
NeilBrownd787be42016-06-02 16:19:53 +10009032 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
9033 clear_bit(RemoveSynchronized, &rdev->flags);
9034 }
9035
Jonathan Brassow90584fc2013-03-07 16:24:26 -06009036 if (removed && mddev->kobj.sd)
9037 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009038
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05009039 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10009040 goto no_add;
9041
NeilBrowndafb20f2012-03-19 12:46:39 +11009042 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05009043 if (this && this != rdev)
9044 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009045 if (test_bit(Candidate, &rdev->flags))
9046 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11009047 if (rdev->raid_disk >= 0 &&
9048 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07009049 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11009050 !test_bit(Faulty, &rdev->flags))
9051 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009052 if (rdev->raid_disk >= 0)
9053 continue;
9054 if (test_bit(Faulty, &rdev->flags))
9055 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009056 if (!test_bit(Journal, &rdev->flags)) {
9057 if (mddev->ro &&
9058 ! (rdev->saved_raid_disk >= 0 &&
9059 !test_bit(Bitmap_sync, &rdev->flags)))
9060 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009061
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009062 rdev->recovery_offset = 0;
9063 }
Guoqing Jiang3f79cc22020-04-04 23:57:11 +02009064 if (mddev->pers->hot_add_disk(mddev, rdev) == 0) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10009065 if (sysfs_link_rdev(mddev, rdev))
9066 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009067 if (!test_bit(Journal, &rdev->flags))
9068 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009069 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08009070 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07009071 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009072 }
NeilBrown746d3202013-04-24 11:42:41 +10009073no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10009074 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08009075 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009076 return spares;
9077}
NeilBrown7ebc0be2011-01-14 09:14:33 +11009078
NeilBrownac05f252014-09-30 08:10:42 +10009079static void md_start_sync(struct work_struct *ws)
9080{
9081 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05009082
NeilBrownac05f252014-09-30 08:10:42 +10009083 mddev->sync_thread = md_register_thread(md_do_sync,
9084 mddev,
9085 "resync");
9086 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11009087 pr_warn("%s: could not start resync thread...\n",
9088 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10009089 /* leave the spares where they are, it shouldn't hurt */
9090 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9091 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9092 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9093 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9094 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11009095 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10009096 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9097 &mddev->recovery))
9098 if (mddev->sysfs_action)
9099 sysfs_notify_dirent_safe(mddev->sysfs_action);
9100 } else
9101 md_wakeup_thread(mddev->sync_thread);
9102 sysfs_notify_dirent_safe(mddev->sysfs_action);
9103 md_new_event(mddev);
9104}
9105
Linus Torvalds1da177e2005-04-16 15:20:36 -07009106/*
9107 * This routine is regularly called by all per-raid-array threads to
9108 * deal with generic issues like resync and super-block update.
9109 * Raid personalities that don't have a thread (linear/raid0) do not
9110 * need this as they never do any recovery or update the superblock.
9111 *
9112 * It does not do any resync itself, but rather "forks" off other threads
9113 * to do that as needed.
9114 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
9115 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07009116 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07009117 * and wakeups up this thread which will reap the thread and finish up.
9118 * This thread also removes any faulty devices (with nr_pending == 0).
9119 *
9120 * The overall approach is:
9121 * 1/ if the superblock needs updating, update it.
9122 * 2/ If a recovery thread is running, don't do anything else.
9123 * 3/ If recovery has finished, clean up, possibly marking spares active.
9124 * 4/ If there are any faulty devices, remove them.
9125 * 5/ If array is degraded, try to add spares devices
9126 * 6/ If array has spares or is not in-sync, start a resync thread.
9127 */
NeilBrownfd01b882011-10-11 16:47:53 +11009128void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009129{
NeilBrown059421e2018-10-03 15:04:41 +10009130 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
9131 /* Write superblock - thread that called mddev_suspend()
9132 * holds reconfig_mutex for us.
9133 */
9134 set_bit(MD_UPDATING_SB, &mddev->flags);
9135 smp_mb__after_atomic();
9136 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
9137 md_update_sb(mddev, 0);
9138 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
9139 wake_up(&mddev->sb_wait);
9140 }
9141
Jonathan Brassow68866e42011-06-08 15:10:08 +10009142 if (mddev->suspended)
9143 return;
9144
NeilBrown5f404022005-06-21 17:17:16 -07009145 if (mddev->bitmap)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009146 md_bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009147
NeilBrownfca4d842005-06-21 17:17:11 -07009148 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07009149 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11009150 pr_debug("md: %s in immediate safe mode\n",
9151 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07009152 mddev->safemode = 2;
9153 }
9154 flush_signals(current);
9155 }
9156
NeilBrownc89a8ee2008-08-05 15:54:13 +10009157 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
9158 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009159 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08009160 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07009161 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07009162 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07009163 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11009164 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07009165 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009166 ))
9167 return;
NeilBrownfca4d842005-06-21 17:17:11 -07009168
NeilBrowndf5b89b2006-03-27 01:18:20 -08009169 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009170 int spares = 0;
NeilBrown480523f2019-08-20 10:21:09 +10009171 bool try_set_sync = mddev->safemode != 0;
NeilBrownfca4d842005-06-21 17:17:11 -07009172
Shaohua Liafc1f552017-08-11 20:34:45 -07009173 if (!mddev->external && mddev->safemode == 1)
NeilBrown33182d12017-08-08 16:56:36 +10009174 mddev->safemode = 0;
9175
NeilBrownc89a8ee2008-08-05 15:54:13 +10009176 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10009177 struct md_rdev *rdev;
9178 if (!mddev->external && mddev->in_sync)
9179 /* 'Blocked' flag not needed as failed devices
9180 * will be recorded if array switched to read/write.
9181 * Leaving it set will prevent the device
9182 * from being removed.
9183 */
9184 rdev_for_each(rdev, mddev)
9185 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10009186 /* On a read-only array we can:
9187 * - remove failed devices
9188 * - add already-in_sync devices if the array itself
9189 * is in-sync.
9190 * As we only add devices that are already in-sync,
9191 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10009192 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10009193 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11009194 /* There is no thread, but we need to call
9195 * ->spare_active and clear saved_raid_disk
9196 */
NeilBrown2ac295a2014-05-29 11:40:03 +10009197 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11009198 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10009199 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11009200 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08009201 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10009202 goto unlock;
9203 }
9204
Guoqing Jiang659b2542015-12-21 10:50:59 +11009205 if (mddev_is_clustered(mddev)) {
9206 struct md_rdev *rdev;
9207 /* kick the device if another node issued a
9208 * remove disk.
9209 */
9210 rdev_for_each(rdev, mddev) {
9211 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
9212 rdev->raid_disk < 0)
9213 md_kick_rdev_from_array(rdev);
9214 }
9215 }
9216
NeilBrown480523f2019-08-20 10:21:09 +10009217 if (try_set_sync && !mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11009218 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11009219 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11009220 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07009221 }
NeilBrownfca4d842005-06-21 17:17:11 -07009222
Shaohua Li29530792016-12-08 15:48:19 -08009223 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07009224 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07009225
Linus Torvalds1da177e2005-04-16 15:20:36 -07009226 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
9227 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
9228 /* resync/recovery still happening */
9229 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9230 goto unlock;
9231 }
9232 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009233 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009234 goto unlock;
9235 }
Neil Brown72a23c22008-06-28 08:31:41 +10009236 /* Set RUNNING before clearing NEEDED to avoid
9237 * any transients in the value of "sync_action".
9238 */
NeilBrown72f36d52012-10-11 14:25:57 +11009239 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11009240 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10009241 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11009242 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08009243 /* Clear some bits that don't mean anything, but
9244 * might be left set
9245 */
NeilBrown24dd4692005-11-08 21:39:26 -08009246 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
9247 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009248
NeilBrowned209582012-04-24 10:23:14 +10009249 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
9250 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10009251 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009252 /* no recovery is running.
9253 * remove any failed drives, then
9254 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11009255 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07009256 * the personality to fail the re-add.
9257 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009258
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009259 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10009260 if (mddev->pers->check_reshape == NULL ||
9261 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009262 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10009263 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009264 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009265 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10009266 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08009267 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9268 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07009269 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009270 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08009271 } else if (mddev->recovery_cp < MaxSector) {
9272 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009273 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08009274 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
9275 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10009276 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08009277
Linus Torvalds1da177e2005-04-16 15:20:36 -07009278 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10009279 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07009280 /* We are adding a device or devices to an array
9281 * which has the bitmap stored on all devices.
9282 * So make sure all bitmap pages get written
9283 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009284 md_bitmap_write_all(mddev->bitmap);
NeilBrowna654b9d82005-06-21 17:17:27 -07009285 }
NeilBrownac05f252014-09-30 08:10:42 +10009286 INIT_WORK(&mddev->del_work, md_start_sync);
9287 queue_work(md_misc_wq, &mddev->del_work);
9288 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009289 }
NeilBrownac05f252014-09-30 08:10:42 +10009290 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10009291 if (!mddev->sync_thread) {
9292 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11009293 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10009294 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9295 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11009296 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10009297 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10009298 }
NeilBrownac05f252014-09-30 08:10:42 +10009299 unlock:
9300 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009301 mddev_unlock(mddev);
9302 }
9303}
NeilBrown6c144d32014-09-30 16:15:38 +10009304EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009305
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009306void md_reap_sync_thread(struct mddev *mddev)
9307{
9308 struct md_rdev *rdev;
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009309 sector_t old_dev_sectors = mddev->dev_sectors;
9310 bool is_reshaped = false;
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009311
9312 /* resync has finished, collect result */
9313 md_unregister_thread(&mddev->sync_thread);
9314 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
Guoqing Jiang0d8ed0e92019-07-24 11:09:21 +02009315 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
9316 mddev->degraded != mddev->raid_disks) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009317 /* success...*/
9318 /* activate any spares */
9319 if (mddev->pers->spare_active(mddev)) {
9320 sysfs_notify(&mddev->kobj, NULL,
9321 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08009322 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009323 }
9324 }
9325 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009326 mddev->pers->finish_reshape) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009327 mddev->pers->finish_reshape(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009328 if (mddev_is_clustered(mddev))
9329 is_reshaped = true;
9330 }
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009331
9332 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11009333 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009334 */
NeilBrownf4667222013-12-09 12:04:56 +11009335 if (!mddev->degraded)
9336 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009337 rdev->saved_raid_disk = -1;
9338
9339 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08009340 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04009341 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9342 * clustered raid */
9343 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
9344 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009345 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10009346 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009347 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9348 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9349 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9350 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009351 /*
9352 * We call md_cluster_ops->update_size here because sync_size could
9353 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9354 * so it is time to update size across cluster.
9355 */
9356 if (mddev_is_clustered(mddev) && is_reshaped
9357 && !test_bit(MD_CLOSING, &mddev->flags))
9358 md_cluster_ops->update_size(mddev, old_dev_sectors);
NeilBrownf851b602014-12-11 10:02:10 +11009359 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009360 /* flag recovery needed just to double check */
9361 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9362 sysfs_notify_dirent_safe(mddev->sysfs_action);
9363 md_new_event(mddev);
9364 if (mddev->event_work.func)
9365 queue_work(md_misc_wq, &mddev->event_work);
9366}
NeilBrown6c144d32014-09-30 16:15:38 +10009367EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009368
NeilBrownfd01b882011-10-11 16:47:53 +11009369void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07009370{
NeilBrown00bcb4a2010-06-01 19:37:23 +10009371 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07009372 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10009373 !test_bit(Blocked, &rdev->flags) &&
9374 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07009375 msecs_to_jiffies(5000));
9376 rdev_dec_pending(rdev, mddev);
9377}
9378EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9379
NeilBrownc6563a82012-05-21 09:27:00 +10009380void md_finish_reshape(struct mddev *mddev)
9381{
9382 /* called be personality module when reshape completes. */
9383 struct md_rdev *rdev;
9384
9385 rdev_for_each(rdev, mddev) {
9386 if (rdev->data_offset > rdev->new_data_offset)
9387 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9388 else
9389 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9390 rdev->data_offset = rdev->new_data_offset;
9391 }
9392}
9393EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10009394
Vishal Vermafc974ee2015-12-24 19:20:34 -07009395/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10009396
Vishal Vermafc974ee2015-12-24 19:20:34 -07009397/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11009398int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10009399 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009400{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04009401 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10009402 int rv;
9403 if (is_new)
9404 s += rdev->new_data_offset;
9405 else
9406 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07009407 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9408 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10009409 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009410 if (test_bit(ExternalBbl, &rdev->flags))
9411 sysfs_notify(&rdev->kobj, NULL,
9412 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11009413 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08009414 set_mask_bits(&mddev->sb_flags, 0,
9415 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10009416 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07009417 return 1;
9418 } else
9419 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10009420}
9421EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9422
NeilBrownc6563a82012-05-21 09:27:00 +10009423int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9424 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009425{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009426 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10009427 if (is_new)
9428 s += rdev->new_data_offset;
9429 else
9430 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009431 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9432 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
9433 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
9434 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10009435}
9436EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9437
Adrian Bunk75c96f82005-05-05 16:16:09 -07009438static int md_notify_reboot(struct notifier_block *this,
9439 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009440{
9441 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11009442 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009443 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009444
NeilBrownc744a652012-03-19 12:46:37 +11009445 for_each_mddev(mddev, tmp) {
9446 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10009447 if (mddev->pers)
9448 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009449 if (mddev->persistent)
9450 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009451 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009452 }
NeilBrownc744a652012-03-19 12:46:37 +11009453 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009454 }
NeilBrownc744a652012-03-19 12:46:37 +11009455 /*
9456 * certain more exotic SCSI devices are known to be
9457 * volatile wrt too early system reboots. While the
9458 * right place to handle this issue is the given
9459 * driver, we do want to have a safe RAID driver ...
9460 */
9461 if (need_delay)
9462 mdelay(1000*1);
9463
Linus Torvalds1da177e2005-04-16 15:20:36 -07009464 return NOTIFY_DONE;
9465}
9466
Adrian Bunk75c96f82005-05-05 16:16:09 -07009467static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009468 .notifier_call = md_notify_reboot,
9469 .next = NULL,
9470 .priority = INT_MAX, /* before any real devices */
9471};
9472
9473static void md_geninit(void)
9474{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009475 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009476
Alexey Dobriyan97a32532020-02-03 17:37:17 -08009477 proc_create("mdstat", S_IRUGO, NULL, &mdstat_proc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009478}
9479
Adrian Bunk75c96f82005-05-05 16:16:09 -07009480static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009481{
Tejun Heoe804ac72010-10-15 15:36:08 +02009482 int ret = -ENOMEM;
9483
Tejun Heoada609e2011-01-25 14:35:54 +01009484 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009485 if (!md_wq)
9486 goto err_wq;
9487
9488 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9489 if (!md_misc_wq)
9490 goto err_misc_wq;
9491
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009492 md_rdev_misc_wq = alloc_workqueue("md_rdev_misc", 0, 0);
9493 if (!md_misc_wq)
9494 goto err_rdev_misc_wq;
9495
Tejun Heoe804ac72010-10-15 15:36:08 +02009496 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9497 goto err_md;
9498
9499 if ((ret = register_blkdev(0, "mdp")) < 0)
9500 goto err_mdp;
9501 mdp_major = ret;
9502
NeilBrownaf5628f2014-07-31 13:54:54 +10009503 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009504 md_probe, NULL, NULL);
9505 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009506 md_probe, NULL, NULL);
9507
Linus Torvalds1da177e2005-04-16 15:20:36 -07009508 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009509 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009510
9511 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009513
Tejun Heoe804ac72010-10-15 15:36:08 +02009514err_mdp:
9515 unregister_blkdev(MD_MAJOR, "md");
9516err_md:
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009517 destroy_workqueue(md_rdev_misc_wq);
9518err_rdev_misc_wq:
Tejun Heoe804ac72010-10-15 15:36:08 +02009519 destroy_workqueue(md_misc_wq);
9520err_misc_wq:
9521 destroy_workqueue(md_wq);
9522err_wq:
9523 return ret;
9524}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009525
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009526static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009527{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009528 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9529 struct md_rdev *rdev2;
9530 int role, ret;
9531 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009532
Guoqing Jiang818da592017-03-01 16:42:40 +08009533 /*
9534 * If size is changed in another node then we need to
9535 * do resize as well.
9536 */
9537 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9538 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9539 if (ret)
9540 pr_info("md-cluster: resize failed\n");
9541 else
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009542 md_bitmap_update_sb(mddev->bitmap);
Guoqing Jiang818da592017-03-01 16:42:40 +08009543 }
9544
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009545 /* Check for change of roles in the active devices */
9546 rdev_for_each(rdev2, mddev) {
9547 if (test_bit(Faulty, &rdev2->flags))
9548 continue;
9549
9550 /* Check if the roles changed */
9551 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009552
9553 if (test_bit(Candidate, &rdev2->flags)) {
9554 if (role == 0xfffe) {
9555 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9556 md_kick_rdev_from_array(rdev2);
9557 continue;
9558 }
9559 else
9560 clear_bit(Candidate, &rdev2->flags);
9561 }
9562
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009563 if (role != rdev2->raid_disk) {
Guoqing Jiangca1e98e2018-10-18 16:37:45 +08009564 /*
9565 * got activated except reshape is happening.
9566 */
9567 if (rdev2->raid_disk == -1 && role != 0xffff &&
9568 !(le32_to_cpu(sb->feature_map) &
9569 MD_FEATURE_RESHAPE_ACTIVE)) {
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009570 rdev2->saved_raid_disk = role;
9571 ret = remove_and_add_spares(mddev, rdev2);
9572 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11009573 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04009574 /* wakeup mddev->thread here, so array could
9575 * perform resync with the new activated disk */
9576 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9577 md_wakeup_thread(mddev->thread);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009578 }
9579 /* device faulty
9580 * We just want to do the minimum to mark the disk
9581 * as faulty. The recovery is performed by the
9582 * one who initiated the error.
9583 */
9584 if ((role == 0xfffe) || (role == 0xfffd)) {
9585 md_error(mddev, rdev2);
9586 clear_bit(Blocked, &rdev2->flags);
9587 }
9588 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009589 }
9590
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009591 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9592 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009593
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009594 /*
9595 * Since mddev->delta_disks has already updated in update_raid_disks,
9596 * so it is time to check reshape.
9597 */
9598 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9599 (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9600 /*
9601 * reshape is happening in the remote node, we need to
9602 * update reshape_position and call start_reshape.
9603 */
Christoph Hellwiged4d0a4e2019-04-04 18:56:10 +02009604 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009605 if (mddev->pers->update_reshape_pos)
9606 mddev->pers->update_reshape_pos(mddev);
9607 if (mddev->pers->start_reshape)
9608 mddev->pers->start_reshape(mddev);
9609 } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9610 mddev->reshape_position != MaxSector &&
9611 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9612 /* reshape is just done in another node. */
9613 mddev->reshape_position = MaxSector;
9614 if (mddev->pers->update_reshape_pos)
9615 mddev->pers->update_reshape_pos(mddev);
9616 }
9617
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009618 /* Finally set the event to be up to date */
9619 mddev->events = le64_to_cpu(sb->events);
9620}
9621
9622static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9623{
9624 int err;
9625 struct page *swapout = rdev->sb_page;
9626 struct mdp_superblock_1 *sb;
9627
9628 /* Store the sb page of the rdev in the swapout temporary
9629 * variable in case we err in the future
9630 */
9631 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009632 err = alloc_disk_sb(rdev);
9633 if (err == 0) {
9634 ClearPageUptodate(rdev->sb_page);
9635 rdev->sb_loaded = 0;
9636 err = super_types[mddev->major_version].
9637 load_super(rdev, NULL, mddev->minor_version);
9638 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009639 if (err < 0) {
9640 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9641 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009642 if (rdev->sb_page)
9643 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009644 rdev->sb_page = swapout;
9645 rdev->sb_loaded = 1;
9646 return err;
9647 }
9648
9649 sb = page_address(rdev->sb_page);
9650 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9651 * is not set
9652 */
9653
9654 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9655 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9656
9657 /* The other node finished recovery, call spare_active to set
9658 * device In_sync and mddev->degraded
9659 */
9660 if (rdev->recovery_offset == MaxSector &&
9661 !test_bit(In_sync, &rdev->flags) &&
9662 mddev->pers->spare_active(mddev))
9663 sysfs_notify(&mddev->kobj, NULL, "degraded");
9664
9665 put_page(swapout);
9666 return 0;
9667}
9668
9669void md_reload_sb(struct mddev *mddev, int nr)
9670{
9671 struct md_rdev *rdev;
9672 int err;
9673
9674 /* Find the rdev */
9675 rdev_for_each_rcu(rdev, mddev) {
9676 if (rdev->desc_nr == nr)
9677 break;
9678 }
9679
9680 if (!rdev || rdev->desc_nr != nr) {
9681 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9682 return;
9683 }
9684
9685 err = read_rdev(mddev, rdev);
9686 if (err < 0)
9687 return;
9688
9689 check_sb_changes(mddev, rdev);
9690
9691 /* Read all rdev's to update recovery_offset */
Guoqing Jiang0ea99242018-04-09 17:01:21 +08009692 rdev_for_each_rcu(rdev, mddev) {
9693 if (!test_bit(Faulty, &rdev->flags))
9694 read_rdev(mddev, rdev);
9695 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009696}
9697EXPORT_SYMBOL(md_reload_sb);
9698
Linus Torvalds1da177e2005-04-16 15:20:36 -07009699#ifndef MODULE
9700
9701/*
9702 * Searches all registered partitions for autorun RAID arrays
9703 * at boot time.
9704 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009705
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009706static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009707static LIST_HEAD(all_detected_devices);
9708struct detected_devices_node {
9709 struct list_head list;
9710 dev_t dev;
9711};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009712
9713void md_autodetect_dev(dev_t dev)
9714{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009715 struct detected_devices_node *node_detected_dev;
9716
9717 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9718 if (node_detected_dev) {
9719 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009720 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009721 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009722 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009724}
9725
Linus Torvalds1da177e2005-04-16 15:20:36 -07009726static void autostart_arrays(int part)
9727{
NeilBrown3cb03002011-10-11 16:45:26 +11009728 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009729 struct detected_devices_node *node_detected_dev;
9730 dev_t dev;
9731 int i_scanned, i_passed;
9732
9733 i_scanned = 0;
9734 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735
NeilBrown9d487392016-11-02 14:16:49 +11009736 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009737
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009738 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009739 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9740 i_scanned++;
9741 node_detected_dev = list_entry(all_detected_devices.next,
9742 struct detected_devices_node, list);
9743 list_del(&node_detected_dev->list);
9744 dev = node_detected_dev->dev;
9745 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009746 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009747 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009748 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009749 if (IS_ERR(rdev))
9750 continue;
9751
NeilBrown403df472014-09-30 15:52:29 +10009752 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009753 continue;
NeilBrown403df472014-09-30 15:52:29 +10009754
NeilBrownd0fae182008-03-04 14:29:31 -08009755 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009756 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009757 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009758 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009759 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009760
NeilBrown9d487392016-11-02 14:16:49 +11009761 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009762
9763 autorun_devices(part);
9764}
9765
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009766#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009767
9768static __exit void md_exit(void)
9769{
NeilBrownfd01b882011-10-11 16:47:53 +11009770 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009771 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009772 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009773
NeilBrownaf5628f2014-07-31 13:54:54 +10009774 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009775 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009777 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009778 unregister_blkdev(mdp_major, "mdp");
9779 unregister_reboot_notifier(&md_notifier);
9780 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009781
9782 /* We cannot unload the modules while some process is
9783 * waiting for us in select() or poll() - wake them up
9784 */
9785 md_unloading = 1;
9786 while (waitqueue_active(&md_event_waiters)) {
9787 /* not safe to leave yet */
9788 wake_up(&md_event_waiters);
9789 msleep(delay);
9790 delay += delay;
9791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009792 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009793
NeilBrown29ac4aa2008-02-06 01:39:58 -08009794 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009795 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009796 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009797 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009798 /*
9799 * for_each_mddev() will call mddev_put() at the end of each
9800 * iteration. As the mddev is now fully clear, this will
9801 * schedule the mddev for destruction by a workqueue, and the
9802 * destroy_workqueue() below will wait for that to complete.
9803 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009804 }
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009805 destroy_workqueue(md_rdev_misc_wq);
Tejun Heoe804ac72010-10-15 15:36:08 +02009806 destroy_workqueue(md_misc_wq);
9807 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009808}
9809
Dan Williams685784a2007-07-09 11:56:42 -07009810subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009811module_exit(md_exit)
9812
Kees Cooke4dca7b2017-10-17 19:04:42 -07009813static int get_ro(char *buffer, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009814{
Xiongfeng Wang3f999802020-05-11 16:23:25 +08009815 return sprintf(buffer, "%d\n", start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009816}
Kees Cooke4dca7b2017-10-17 19:04:42 -07009817static int set_ro(const char *val, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009818{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009819 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009820}
9821
NeilBrown80ca3a42006-07-10 04:44:18 -07009822module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9823module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009824module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009825module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009826
Linus Torvalds1da177e2005-04-16 15:20:36 -07009827MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009828MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009829MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009830MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);