blob: f567f536b529bd6fa7cf7ff74d5cad1aa589c1eb [file] [log] [blame]
Thomas Gleixneraf1a8892019-05-20 19:08:12 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 md.c : Multiple Devices driver for Linux
NeilBrownf72ffdd2014-09-30 14:23:59 +10004 Copyright (C) 1998, 1999, 2000 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
6 completely rewritten, based on the MD driver code from Marc Zyngier
7
8 Changes:
9
10 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
11 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
12 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
13 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
14 - kmod support by: Cyrus Durgin
15 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
16 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17
18 - lots of fixes and improvements to the RAID1/RAID5 and generic
19 RAID code (such as request based resynchronization):
20
21 Neil Brown <neilb@cse.unsw.edu.au>.
22
NeilBrown32a76272005-06-21 17:17:14 -070023 - persistent bitmap code
24 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25
NeilBrown9d487392016-11-02 14:16:49 +110026
27 Errors, Warnings, etc.
28 Please use:
29 pr_crit() for error conditions that risk data loss
30 pr_err() for error conditions that are unexpected, like an IO error
31 or internal inconsistency
32 pr_warn() for error conditions that could have been predicated, like
33 adding a device to an array when it has incompatible metadata
34 pr_info() for every interesting, very rare events, like an array starting
35 or stopping, or resync starting or stopping
36 pr_debug() for everything else.
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038*/
39
Guoqing Jiang963c5552019-06-14 17:10:36 +080040#include <linux/sched/mm.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010041#include <linux/sched/signal.h>
NeilBrowna6fb0932005-09-09 16:23:56 -070042#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110043#include <linux/blkdev.h>
Vishal Vermafc974ee2015-12-24 19:20:34 -070044#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040047#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080048#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070049#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080050#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110051#include <linux/hdreg.h>
52#include <linux/proc_fs.h>
53#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040054#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110055#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070056#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110057#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110058#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110059#include <linux/raid/md_p.h>
60#include <linux/raid/md_u.h>
Christoph Hellwig74cc979c2020-03-24 08:25:19 +010061#include <linux/raid/detect.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090062#include <linux/slab.h>
NeilBrown4ad23a972017-03-15 14:05:14 +110063#include <linux/percpu-refcount.h>
Christoph Hellwigc6a564ff2020-03-25 16:48:42 +010064#include <linux/part_stat.h>
NeilBrown4ad23a972017-03-15 14:05:14 +110065
Shaohua Li504634f2016-11-18 09:44:08 -080066#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110067#include "md.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040068#include "md-bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050069#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110072static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#endif
74
NeilBrown01f96c02011-09-21 15:30:20 +100075/* pers_list is a list of registered personalities protected
76 * by pers_lock.
77 * pers_lock does extra service to protect accesses to
78 * mddev->thread when the mutex cannot be held.
79 */
NeilBrown2604b702006-01-06 00:20:36 -080080static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static DEFINE_SPINLOCK(pers_lock);
82
Kent Overstreet28dec872018-06-07 20:52:54 -040083static struct kobj_type md_ktype;
84
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050085struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050086EXPORT_SYMBOL(md_cluster_ops);
Christoph Hellwig2b598ee2019-04-04 18:56:14 +020087static struct module *md_cluster_mod;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050088
Bernd Schubert90b08712008-05-23 13:04:38 -070089static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020090static struct workqueue_struct *md_wq;
91static struct workqueue_struct *md_misc_wq;
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +020092static struct workqueue_struct *md_rdev_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070093
NeilBrown746d3202013-04-24 11:42:41 +100094static int remove_and_add_spares(struct mddev *mddev,
95 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110096static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
Robert Becker1e509152009-12-14 12:49:58 +110099 * Default number of read corrections we'll attempt on an rdev
100 * before ejecting it from the array. We divide the read error
101 * count by 2 for every hour elapsed between read errors.
102 */
103#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
104/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
106 * is 1000 KB/sec, so the extra system load does not show up that much.
107 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -0700108 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * subsystem is idle. There is also an 'absolute maximum' reconstruction
110 * speed limit - in case reconstruction slows down your system despite
111 * idle IO detection.
112 *
113 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -0800114 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
116
117static int sysctl_speed_limit_min = 1000;
118static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100119static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800120{
121 return mddev->sync_speed_min ?
122 mddev->sync_speed_min : sysctl_speed_limit_min;
123}
124
NeilBrownfd01b882011-10-11 16:47:53 +1100125static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800126{
127 return mddev->sync_speed_max ?
128 mddev->sync_speed_max : sysctl_speed_limit_max;
129}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100131static void rdev_uninit_serial(struct md_rdev *rdev)
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800132{
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100133 if (!test_and_clear_bit(CollisionCheck, &rdev->flags))
134 return;
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800135
Guoqing Jiang025471f2019-12-23 10:49:01 +0100136 kvfree(rdev->serial);
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100137 rdev->serial = NULL;
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800138}
139
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100140static void rdevs_uninit_serial(struct mddev *mddev)
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100141{
142 struct md_rdev *rdev;
143
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100144 rdev_for_each(rdev, mddev)
145 rdev_uninit_serial(rdev);
146}
147
148static int rdev_init_serial(struct md_rdev *rdev)
149{
Guoqing Jiang025471f2019-12-23 10:49:01 +0100150 /* serial_nums equals with BARRIER_BUCKETS_NR */
151 int i, serial_nums = 1 << ((PAGE_SHIFT - ilog2(sizeof(atomic_t))));
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100152 struct serial_in_rdev *serial = NULL;
153
154 if (test_bit(CollisionCheck, &rdev->flags))
155 return 0;
156
Guoqing Jiang025471f2019-12-23 10:49:01 +0100157 serial = kvmalloc(sizeof(struct serial_in_rdev) * serial_nums,
158 GFP_KERNEL);
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100159 if (!serial)
160 return -ENOMEM;
161
Guoqing Jiang025471f2019-12-23 10:49:01 +0100162 for (i = 0; i < serial_nums; i++) {
163 struct serial_in_rdev *serial_tmp = &serial[i];
164
165 spin_lock_init(&serial_tmp->serial_lock);
166 serial_tmp->serial_rb = RB_ROOT_CACHED;
167 init_waitqueue_head(&serial_tmp->serial_io_wait);
168 }
169
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100170 rdev->serial = serial;
171 set_bit(CollisionCheck, &rdev->flags);
172
173 return 0;
174}
175
176static int rdevs_init_serial(struct mddev *mddev)
177{
178 struct md_rdev *rdev;
179 int ret = 0;
180
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100181 rdev_for_each(rdev, mddev) {
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100182 ret = rdev_init_serial(rdev);
183 if (ret)
184 break;
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100185 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100186
187 /* Free all resources if pool is not existed */
188 if (ret && !mddev->serial_info_pool)
189 rdevs_uninit_serial(mddev);
190
191 return ret;
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100192}
193
Guoqing Jiang963c5552019-06-14 17:10:36 +0800194/*
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100195 * rdev needs to enable serial stuffs if it meets the conditions:
196 * 1. it is multi-queue device flaged with writemostly.
197 * 2. the write-behind mode is enabled.
198 */
199static int rdev_need_serial(struct md_rdev *rdev)
200{
201 return (rdev && rdev->mddev->bitmap_info.max_write_behind > 0 &&
202 rdev->bdev->bd_queue->nr_hw_queues != 1 &&
203 test_bit(WriteMostly, &rdev->flags));
204}
205
206/*
207 * Init resource for rdev(s), then create serial_info_pool if:
208 * 1. rdev is the first device which return true from rdev_enable_serial.
209 * 2. rdev is NULL, means we want to enable serialization for all rdevs.
Guoqing Jiang963c5552019-06-14 17:10:36 +0800210 */
Guoqing Jiang404659c2019-12-23 10:48:53 +0100211void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev,
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100212 bool is_suspend)
Guoqing Jiang963c5552019-06-14 17:10:36 +0800213{
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100214 int ret = 0;
215
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100216 if (rdev && !rdev_need_serial(rdev) &&
217 !test_bit(CollisionCheck, &rdev->flags))
Guoqing Jiang963c5552019-06-14 17:10:36 +0800218 return;
219
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100220 if (!is_suspend)
221 mddev_suspend(mddev);
222
223 if (!rdev)
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100224 ret = rdevs_init_serial(mddev);
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100225 else
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100226 ret = rdev_init_serial(rdev);
227 if (ret)
228 goto abort;
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100229
Guoqing Jiang404659c2019-12-23 10:48:53 +0100230 if (mddev->serial_info_pool == NULL) {
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
Jens Axboedece1632015-11-05 10:41:16 -0700466static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
NeilBrown49077322010-03-25 16:20:56 +1100468 const int rw = bio_data_dir(bio);
Michael Callahanddcf35d2018-07-18 04:47:39 -0700469 const int sgrp = op_stat_group(bio_op(bio));
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
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -0300473 if (unlikely(test_bit(MD_BROKEN, &mddev->flags)) && (rw == WRITE)) {
474 bio_io_error(bio);
475 return BLK_QC_T_NONE;
476 }
477
NeilBrownaf67c312017-06-18 14:38:57 +1000478 blk_queue_split(q, &bio);
Kent Overstreet54efd502015-04-23 22:37:18 -0700479
NeilBrown274d8cb2016-01-04 16:16:58 +1100480 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100481 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700482 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100483 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100484 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200485 if (bio_sectors(bio) != 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200486 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200487 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700488 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100489 }
NeilBrown49077322010-03-25 16:20:56 +1100490
Chris Masone91ece52011-02-07 19:21:48 -0500491 /*
492 * save the sectors now since our bio can
493 * go away inside make_request
494 */
495 sectors = bio_sectors(bio);
Shaohua Li9c573de2016-04-25 16:52:38 -0700496 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600497 bio->bi_opf &= ~REQ_NOMERGE;
Shaohua Li393debc2017-09-21 10:23:35 -0700498
499 md_handle_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100500
Mike Snitzer112f1582018-12-06 11:41:18 -0500501 part_stat_lock();
502 part_stat_inc(&mddev->gendisk->part0, ios[sgrp]);
503 part_stat_add(&mddev->gendisk->part0, sectors[sgrp], sectors);
Gu Zheng74672d02015-04-03 08:44:47 +0800504 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100505
Jens Axboedece1632015-11-05 10:41:16 -0700506 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
NeilBrown9e35b992010-04-06 14:23:02 +1000509/* mddev_suspend makes sure no new requests are submitted
510 * to the device, and that any requests that have been submitted
511 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100512 * Once mddev_detach() is called and completes, the module will be
513 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000514 */
NeilBrownfd01b882011-10-11 16:47:53 +1100515void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100516{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200517 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
NeilBrown4d5324f2017-10-19 12:17:16 +1100518 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100519 if (mddev->suspended++)
520 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100521 synchronize_rcu();
NeilBrowncc27b0c2017-06-05 16:49:39 +1000522 wake_up(&mddev->sb_wait);
NeilBrown35bfc522017-10-17 13:46:43 +1100523 set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
524 smp_mb__after_atomic();
NeilBrown409c57f2009-03-31 14:39:39 +1100525 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
526 mddev->pers->quiesce(mddev, 1);
NeilBrown35bfc522017-10-17 13:46:43 +1100527 clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags);
528 wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags));
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500529
530 del_timer_sync(&mddev->safemode_timer);
Coly Li78f57ef2020-04-09 22:17:20 +0800531 /* restrict memory reclaim I/O during raid array is suspend */
532 mddev->noio_flag = memalloc_noio_save();
NeilBrown409c57f2009-03-31 14:39:39 +1100533}
NeilBrown390ee602010-06-01 19:37:27 +1000534EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100535
NeilBrownfd01b882011-10-11 16:47:53 +1100536void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100537{
Coly Li78f57ef2020-04-09 22:17:20 +0800538 /* entred the memalloc scope from mddev_suspend() */
539 memalloc_noio_restore(mddev->noio_flag);
NeilBrown4d5324f2017-10-19 12:17:16 +1100540 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100541 if (--mddev->suspended)
542 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100543 wake_up(&mddev->sb_wait);
544 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500545
Jonathan Brassow47525e52012-05-22 13:55:29 +1000546 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500547 md_wakeup_thread(mddev->thread);
548 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100549}
NeilBrown390ee602010-06-01 19:37:27 +1000550EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100551
NeilBrownfd01b882011-10-11 16:47:53 +1100552int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000553{
NeilBrown5c675f82014-12-15 12:56:56 +1100554 struct md_personality *pers = mddev->pers;
555 int ret = 0;
556
557 rcu_read_lock();
558 if (mddev->suspended)
559 ret = 1;
560 else if (pers && pers->congested)
561 ret = pers->congested(mddev, bits);
562 rcu_read_unlock();
563 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000564}
NeilBrown5c675f82014-12-15 12:56:56 +1100565EXPORT_SYMBOL_GPL(mddev_congested);
566static int md_congested(void *data, int bits)
567{
568 struct mddev *mddev = data;
569 return mddev_congested(mddev, bits);
570}
NeilBrown3fa841d2009-09-23 18:10:29 +1000571
NeilBrowna2826aa2009-12-14 12:49:49 +1100572/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200573 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100574 */
NeilBrown4bc034d2019-03-29 10:46:16 -0700575
576static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100577{
NeilBrown4bc034d2019-03-29 10:46:16 -0700578 struct md_rdev *rdev = bio->bi_private;
579 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100580
581 rdev_dec_pending(rdev, mddev);
582
NeilBrown4bc034d2019-03-29 10:46:16 -0700583 if (atomic_dec_and_test(&mddev->flush_pending)) {
584 /* The pre-request flush has finished */
585 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100586 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700587 bio_put(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100588}
589
NeilBrown4bc034d2019-03-29 10:46:16 -0700590static void md_submit_flush_data(struct work_struct *ws);
591
592static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100593{
NeilBrown4bc034d2019-03-29 10:46:16 -0700594 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100595 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100596
NeilBrown2bc13b82019-03-29 10:46:17 -0700597 mddev->start_flush = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700598 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
599 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100600 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100601 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100602 if (rdev->raid_disk >= 0 &&
603 !test_bit(Faulty, &rdev->flags)) {
604 /* Take two references, one is dropped
605 * when request finishes, one after
606 * we reclaim rcu_read_lock
607 */
608 struct bio *bi;
609 atomic_inc(&rdev->nr_pending);
610 atomic_inc(&rdev->nr_pending);
611 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000612 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Xiao Ni5a409b42018-05-21 11:49:54 +0800613 bi->bi_end_io = md_end_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700614 bi->bi_private = rdev;
615 bio_set_dev(bi, rdev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600616 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
NeilBrown4bc034d2019-03-29 10:46:16 -0700617 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500618 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100619 rcu_read_lock();
620 rdev_dec_pending(rdev, mddev);
621 }
622 rcu_read_unlock();
NeilBrown4bc034d2019-03-29 10:46:16 -0700623 if (atomic_dec_and_test(&mddev->flush_pending))
624 queue_work(md_wq, &mddev->flush_work);
625}
NeilBrowna2826aa2009-12-14 12:49:49 +1100626
NeilBrown4bc034d2019-03-29 10:46:16 -0700627static void md_submit_flush_data(struct work_struct *ws)
628{
629 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
630 struct bio *bio = mddev->flush_bio;
631
632 /*
633 * must reset flush_bio before calling into md_handle_request to avoid a
634 * deadlock, because other bios passed md_handle_request suspend check
635 * could wait for this and below md_handle_request could wait for those
636 * bios because of suspend check
637 */
NeilBrown2bc13b82019-03-29 10:46:17 -0700638 mddev->last_flush = mddev->start_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700639 mddev->flush_bio = NULL;
640 wake_up(&mddev->sb_wait);
641
642 if (bio->bi_iter.bi_size == 0) {
643 /* an empty barrier - all done */
644 bio_endio(bio);
645 } else {
646 bio->bi_opf &= ~REQ_PREFLUSH;
647 md_handle_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100648 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100649}
NeilBrown4bc034d2019-03-29 10:46:16 -0700650
David Jeffery775d7832019-09-16 13:15:14 -0400651/*
652 * Manages consolidation of flushes and submitting any flushes needed for
653 * a bio with REQ_PREFLUSH. Returns true if the bio is finished or is
654 * being finished in another context. Returns false if the flushing is
655 * complete but still needs the I/O portion of the bio to be processed.
656 */
657bool md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrown4bc034d2019-03-29 10:46:16 -0700658{
NeilBrown2bc13b82019-03-29 10:46:17 -0700659 ktime_t start = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700660 spin_lock_irq(&mddev->lock);
661 wait_event_lock_irq(mddev->sb_wait,
NeilBrown2bc13b82019-03-29 10:46:17 -0700662 !mddev->flush_bio ||
663 ktime_after(mddev->last_flush, start),
NeilBrown4bc034d2019-03-29 10:46:16 -0700664 mddev->lock);
NeilBrown2bc13b82019-03-29 10:46:17 -0700665 if (!ktime_after(mddev->last_flush, start)) {
666 WARN_ON(mddev->flush_bio);
667 mddev->flush_bio = bio;
668 bio = NULL;
669 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700670 spin_unlock_irq(&mddev->lock);
671
NeilBrown2bc13b82019-03-29 10:46:17 -0700672 if (!bio) {
673 INIT_WORK(&mddev->flush_work, submit_flushes);
674 queue_work(md_wq, &mddev->flush_work);
675 } else {
676 /* flush was performed for some other bio while we waited. */
677 if (bio->bi_iter.bi_size == 0)
678 /* an empty barrier - all done */
679 bio_endio(bio);
680 else {
681 bio->bi_opf &= ~REQ_PREFLUSH;
David Jeffery775d7832019-09-16 13:15:14 -0400682 return false;
NeilBrown2bc13b82019-03-29 10:46:17 -0700683 }
684 }
David Jeffery775d7832019-09-16 13:15:14 -0400685 return true;
NeilBrown4bc034d2019-03-29 10:46:16 -0700686}
Tejun Heoe9c74692010-09-03 11:56:18 +0200687EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100688
NeilBrownfd01b882011-10-11 16:47:53 +1100689static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 atomic_inc(&mddev->active);
692 return mddev;
693}
694
Dan Williams5fd3a172009-03-04 00:57:25 -0700695static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100696
NeilBrownfd01b882011-10-11 16:47:53 +1100697static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
700 return;
NeilBrownd3374822009-01-09 08:31:10 +1100701 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100702 mddev->ctime == 0 && !mddev->hold_active) {
703 /* Array is not configured at all, and not held active,
704 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100705 list_del_init(&mddev->all_mddevs);
Kent Overstreet28dec872018-06-07 20:52:54 -0400706
707 /*
708 * Call queue_work inside the spinlock so that
709 * flush_workqueue() after mddev_find will succeed in waiting
710 * for the work to be done.
711 */
712 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
713 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100714 }
715 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Kees Cook8376d3c2017-10-16 17:01:48 -0700718static void md_safemode_timeout(struct timer_list *t);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400719
NeilBrownfd01b882011-10-11 16:47:53 +1100720void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100721{
Kent Overstreet28dec872018-06-07 20:52:54 -0400722 kobject_init(&mddev->kobj, &md_ktype);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100723 mutex_init(&mddev->open_mutex);
724 mutex_init(&mddev->reconfig_mutex);
725 mutex_init(&mddev->bitmap_info.mutex);
726 INIT_LIST_HEAD(&mddev->disks);
727 INIT_LIST_HEAD(&mddev->all_mddevs);
Kees Cook8376d3c2017-10-16 17:01:48 -0700728 timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100729 atomic_set(&mddev->active, 1);
730 atomic_set(&mddev->openers, 0);
731 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100732 spin_lock_init(&mddev->lock);
NeilBrown4bc034d2019-03-29 10:46:16 -0700733 atomic_set(&mddev->flush_pending, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100734 init_waitqueue_head(&mddev->sb_wait);
735 init_waitqueue_head(&mddev->recovery_wait);
736 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000737 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500738 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100739 mddev->resync_min = 0;
740 mddev->resync_max = MaxSector;
741 mddev->level = LEVEL_NONE;
742}
NeilBrown390ee602010-06-01 19:37:27 +1000743EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100744
NeilBrownf72ffdd2014-09-30 14:23:59 +1000745static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
NeilBrownfd01b882011-10-11 16:47:53 +1100747 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
NeilBrown8f5f02c2011-02-16 13:58:51 +1100749 if (unit && MAJOR(unit) != MD_MAJOR)
750 unit &= ~((1<<MdpMinorShift)-1);
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 retry:
753 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
NeilBrownefeb53c2009-01-09 08:31:10 +1100755 if (unit) {
756 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
757 if (mddev->unit == unit) {
758 mddev_get(mddev);
759 spin_unlock(&all_mddevs_lock);
760 kfree(new);
761 return mddev;
762 }
763
764 if (new) {
765 list_add(&new->all_mddevs, &all_mddevs);
766 spin_unlock(&all_mddevs_lock);
767 new->hold_active = UNTIL_IOCTL;
768 return new;
769 }
770 } else if (new) {
771 /* find an unused unit number */
772 static int next_minor = 512;
773 int start = next_minor;
774 int is_free = 0;
775 int dev = 0;
776 while (!is_free) {
777 dev = MKDEV(MD_MAJOR, next_minor);
778 next_minor++;
779 if (next_minor > MINORMASK)
780 next_minor = 0;
781 if (next_minor == start) {
782 /* Oh dear, all in use. */
783 spin_unlock(&all_mddevs_lock);
784 kfree(new);
785 return NULL;
786 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000787
NeilBrownefeb53c2009-01-09 08:31:10 +1100788 is_free = 1;
789 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
790 if (mddev->unit == dev) {
791 is_free = 0;
792 break;
793 }
794 }
795 new->unit = dev;
796 new->md_minor = MINOR(dev);
797 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 list_add(&new->all_mddevs, &all_mddevs);
799 spin_unlock(&all_mddevs_lock);
800 return new;
801 }
802 spin_unlock(&all_mddevs_lock);
803
NeilBrown9ffae0c2006-01-06 00:20:32 -0800804 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (!new)
806 return NULL;
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 new->unit = unit;
809 if (MAJOR(unit) == MD_MAJOR)
810 new->md_minor = MINOR(unit);
811 else
812 new->md_minor = MINOR(unit) >> MdpMinorShift;
813
NeilBrownfafd7fb2010-04-01 15:55:30 +1100814 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 goto retry;
817}
818
NeilBrownb6eb1272010-04-15 10:13:47 +1000819static struct attribute_group md_redundancy_group;
820
NeilBrown5c47daf2014-12-15 12:57:01 +1100821void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
NeilBrowna64c8762010-04-14 17:15:37 +1000823 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000824 /* These cannot be removed under reconfig_mutex as
825 * an access to the files will try to take reconfig_mutex
826 * while holding the file unremovable, which leads to
827 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000828 * So hold set sysfs_active while the remove in happeing,
829 * and anything else which might set ->to_remove or my
830 * otherwise change the sysfs namespace will fail with
831 * -EBUSY if sysfs_active is still set.
832 * We set sysfs_active under reconfig_mutex and elsewhere
833 * test it under the same mutex to ensure its correct value
834 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000835 */
NeilBrowna64c8762010-04-14 17:15:37 +1000836 struct attribute_group *to_remove = mddev->to_remove;
837 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000838 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000839 mutex_unlock(&mddev->reconfig_mutex);
840
NeilBrown00bcb4a2010-06-01 19:37:23 +1000841 if (mddev->kobj.sd) {
842 if (to_remove != &md_redundancy_group)
843 sysfs_remove_group(&mddev->kobj, to_remove);
844 if (mddev->pers == NULL ||
845 mddev->pers->sync_request == NULL) {
846 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
847 if (mddev->sysfs_action)
848 sysfs_put(mddev->sysfs_action);
849 mddev->sysfs_action = NULL;
850 }
NeilBrowna64c8762010-04-14 17:15:37 +1000851 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000852 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000853 } else
854 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Chris Dunlop751e67c2011-10-19 16:48:26 +1100856 /* As we've dropped the mutex we need a spinlock to
857 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000858 */
859 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700860 md_wakeup_thread(mddev->thread);
NeilBrown4d5324f2017-10-19 12:17:16 +1100861 wake_up(&mddev->sb_wait);
NeilBrown01f96c02011-09-21 15:30:20 +1000862 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
NeilBrown5c47daf2014-12-15 12:57:01 +1100864EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500866struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100867{
868 struct md_rdev *rdev;
869
870 rdev_for_each_rcu(rdev, mddev)
871 if (rdev->desc_nr == nr)
872 return rdev;
873
874 return NULL;
875}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500876EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100877
878static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
NeilBrown3cb03002011-10-11 16:45:26 +1100880 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
NeilBrowndafb20f2012-03-19 12:46:39 +1100882 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (rdev->bdev->bd_dev == dev)
884 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return NULL;
887}
888
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100889struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev)
NeilBrown1ca69c42012-10-11 13:37:33 +1100890{
891 struct md_rdev *rdev;
892
893 rdev_for_each_rcu(rdev, mddev)
894 if (rdev->bdev->bd_dev == dev)
895 return rdev;
896
897 return NULL;
898}
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100899EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100900
NeilBrown84fc4b52011-10-11 16:49:58 +1100901static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800902{
NeilBrown84fc4b52011-10-11 16:49:58 +1100903 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800904 list_for_each_entry(pers, &pers_list, list) {
905 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800906 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800907 if (strcmp(pers->name, clevel)==0)
908 return pers;
909 }
NeilBrown2604b702006-01-06 00:20:36 -0800910 return NULL;
911}
912
Andre Nollb73df2d2008-07-11 22:02:23 +1000913/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100914static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100916 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000917 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
NeilBrownf72ffdd2014-09-30 14:23:59 +1000920static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100923 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000924 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return 0;
926}
927
NeilBrown545c8792012-05-22 13:54:30 +1000928void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800931 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 rdev->sb_loaded = 0;
933 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000934 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100935 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
NeilBrown2699b672011-07-28 11:31:47 +1000937 if (rdev->bb_page) {
938 put_page(rdev->bb_page);
939 rdev->bb_page = NULL;
940 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800941 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
NeilBrown545c8792012-05-22 13:54:30 +1000943EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200945static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700946{
NeilBrown3cb03002011-10-11 16:45:26 +1100947 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100948 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700949
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200950 if (bio->bi_status) {
951 pr_err("md: super_written gets error=%d\n", bio->bi_status);
NeilBrowna9701a32005-11-08 21:39:34 -0800952 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100953 if (!test_bit(Faulty, &rdev->flags)
954 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800955 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100956 set_bit(LastDev, &rdev->flags);
957 }
958 } else
959 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700960
NeilBrowna9701a32005-11-08 21:39:34 -0800961 if (atomic_dec_and_test(&mddev->pending_writes))
962 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700963 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700964 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700965}
966
NeilBrownfd01b882011-10-11 16:47:53 +1100967void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700968 sector_t sector, int size, struct page *page)
969{
970 /* write first size bytes of page to sector of rdev
971 * Increment mddev->pending_writes before returning
972 * and decrement it on completion, waking up sb_wait
973 * if zero is reached.
974 * If an error occurred, call md_error
975 */
NeilBrown46533ff2016-11-18 16:16:11 +1100976 struct bio *bio;
977 int ff = 0;
978
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +0100979 if (!page)
980 return;
981
NeilBrown46533ff2016-11-18 16:16:11 +1100982 if (test_bit(Faulty, &rdev->flags))
983 return;
984
NeilBrown5a850712017-06-21 09:12:21 +1000985 bio = md_bio_alloc_sync(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700986
Shaohua Lied3b98c2016-03-29 14:00:19 -0700987 atomic_inc(&rdev->nr_pending);
988
Christoph Hellwig74d46992017-08-23 19:10:32 +0200989 bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700990 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700991 bio_add_page(bio, page, size, 0);
992 bio->bi_private = rdev;
993 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +1100994
995 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
996 test_bit(FailFast, &rdev->flags) &&
997 !test_bit(LastDev, &rdev->flags))
998 ff = MD_FAILFAST;
Jan Kara5a8948f2017-05-31 09:44:33 +0200999 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -08001000
NeilBrown7bfa19f2005-06-21 17:17:28 -07001001 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -05001002 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -08001003}
1004
NeilBrown46533ff2016-11-18 16:16:11 +11001005int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -08001006{
Tejun Heoe9c74692010-09-03 11:56:18 +02001007 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +10001008 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -08001009 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +11001010 return -EAGAIN;
1011 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001012}
1013
NeilBrown3cb03002011-10-11 16:45:26 +11001014int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -05001015 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
NeilBrown5a850712017-06-21 09:12:21 +10001017 struct bio *bio = md_bio_alloc_sync(rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 int ret;
1019
Christoph Hellwig74d46992017-08-23 19:10:32 +02001020 if (metadata_op && rdev->meta_bdev)
1021 bio_set_dev(bio, rdev->meta_bdev);
1022 else
1023 bio_set_dev(bio, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -05001024 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001025 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -07001026 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +10001027 else if (rdev->mddev->reshape_position != MaxSector &&
1028 (rdev->mddev->reshape_backwards ==
1029 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001030 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001031 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001032 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -05001034
1035 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001037 ret = !bio->bi_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 bio_put(bio);
1039 return ret;
1040}
NeilBrowna8745db2006-01-06 00:20:34 -08001041EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
NeilBrownf72ffdd2014-09-30 14:23:59 +10001043static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (rdev->sb_loaded)
1048 return 0;
1049
Mike Christie796a5cf2016-06-05 14:32:07 -05001050 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 goto fail;
1052 rdev->sb_loaded = 1;
1053 return 0;
1054
1055fail:
NeilBrown9d487392016-11-02 14:16:49 +11001056 pr_err("md: disabled device %s, could not read superblock.\n",
1057 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return -EINVAL;
1059}
1060
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001061static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
NeilBrownf72ffdd2014-09-30 14:23:59 +10001063 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +10001064 sb1->set_uuid1 == sb2->set_uuid1 &&
1065 sb1->set_uuid2 == sb2->set_uuid2 &&
1066 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001069static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 int ret;
1072 mdp_super_t *tmp1, *tmp2;
1073
1074 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
1075 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
1076
1077 if (!tmp1 || !tmp2) {
1078 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 goto abort;
1080 }
1081
1082 *tmp1 = *sb1;
1083 *tmp2 = *sb2;
1084
1085 /*
1086 * nr_disks is not constant
1087 */
1088 tmp1->nr_disks = 0;
1089 tmp2->nr_disks = 0;
1090
Andre Nollce0c8e02008-07-11 22:02:20 +10001091 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001093 kfree(tmp1);
1094 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return ret;
1096}
1097
NeilBrown4d167f02007-05-09 02:35:37 -07001098static u32 md_csum_fold(u32 csum)
1099{
1100 csum = (csum & 0xffff) + (csum >> 16);
1101 return (csum & 0xffff) + (csum >> 16);
1102}
1103
NeilBrownf72ffdd2014-09-30 14:23:59 +10001104static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
NeilBrown4d167f02007-05-09 02:35:37 -07001106 u64 newcsum = 0;
1107 u32 *sb32 = (u32*)sb;
1108 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 unsigned int disk_csum, csum;
1110
1111 disk_csum = sb->sb_csum;
1112 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -07001113
1114 for (i = 0; i < MD_SB_BYTES/4 ; i++)
1115 newcsum += sb32[i];
1116 csum = (newcsum & 0xffffffff) + (newcsum>>32);
1117
NeilBrown4d167f02007-05-09 02:35:37 -07001118#ifdef CONFIG_ALPHA
1119 /* This used to use csum_partial, which was wrong for several
1120 * reasons including that different results are returned on
1121 * different architectures. It isn't critical that we get exactly
1122 * the same return value as before (we always csum_fold before
1123 * testing, and that removes any differences). However as we
1124 * know that csum_partial always returned a 16bit value on
1125 * alphas, do a fold to maximise conformity to previous behaviour.
1126 */
1127 sb->sb_csum = md_csum_fold(disk_csum);
1128#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -07001130#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return csum;
1132}
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134/*
1135 * Handle superblock details.
1136 * We want to be able to handle multiple superblock formats
1137 * so we have a common interface to them all, and an array of
1138 * different handlers.
1139 * We rely on user-space to write the initial superblock, and support
1140 * reading and updating of superblocks.
1141 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +11001142 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 * loads and validates a superblock on dev.
1144 * if refdev != NULL, compare superblocks on both devices
1145 * Return:
1146 * 0 - dev has a superblock that is compatible with refdev
1147 * 1 - dev has a superblock that is compatible and newer than refdev
1148 * so dev should be used as the refdev in future
1149 * -EINVAL superblock incompatible or invalid
1150 * -othererror e.g. -EIO
1151 *
NeilBrownfd01b882011-10-11 16:47:53 +11001152 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 * Verify that dev is acceptable into mddev.
1154 * The first time, mddev->raid_disks will be 0, and data from
1155 * dev should be merged in. Subsequent calls check that dev
1156 * is new enough. Return 0 or -EINVAL
1157 *
NeilBrownfd01b882011-10-11 16:47:53 +11001158 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 * Update the superblock for rdev with data in mddev
1160 * This does not write to disc.
1161 *
1162 */
1163
1164struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001165 char *name;
1166 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +10001167 int (*load_super)(struct md_rdev *rdev,
1168 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001169 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +10001170 int (*validate_super)(struct mddev *mddev,
1171 struct md_rdev *rdev);
1172 void (*sync_super)(struct mddev *mddev,
1173 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001174 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001175 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +10001176 int (*allow_new_offset)(struct md_rdev *rdev,
1177 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178};
1179
1180/*
Andre Noll0894cc32009-06-18 08:49:23 +10001181 * Check that the given mddev has no bitmap.
1182 *
1183 * This function is called from the run method of all personalities that do not
1184 * support bitmaps. It prints an error message and returns non-zero if mddev
1185 * has a bitmap. Otherwise, it returns 0.
1186 *
1187 */
NeilBrownfd01b882011-10-11 16:47:53 +11001188int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001189{
NeilBrownc3d97142009-12-14 12:49:52 +11001190 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001191 return 0;
NeilBrown9d487392016-11-02 14:16:49 +11001192 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +10001193 mdname(mddev), mddev->pers->name);
1194 return 1;
1195}
1196EXPORT_SYMBOL(md_check_no_bitmap);
1197
1198/*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001199 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 */
NeilBrown3cb03002011-10-11 16:45:26 +11001201static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1204 mdp_super_t *sb;
1205 int ret;
Yufen Yu228fc7d2019-10-30 18:47:02 +08001206 bool spare_disk = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 /*
Andre Noll0f420352008-07-11 22:02:23 +10001209 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 * it's at the end of the disk.
1211 *
1212 * It also happens to be a multiple of 4Kb.
1213 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001214 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
NeilBrown0002b272005-09-09 16:23:53 -07001216 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +11001217 if (ret)
1218 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 ret = -EINVAL;
1221
1222 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001223 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +11001226 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 goto abort;
1228 }
1229
1230 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001231 sb->minor_version < 90 ||
1232 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +11001233 pr_warn("Bad version number %d.%d on %s\n",
1234 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 goto abort;
1236 }
1237
1238 if (sb->raid_disks <= 0)
1239 goto abort;
1240
NeilBrown4d167f02007-05-09 02:35:37 -07001241 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001242 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 goto abort;
1244 }
1245
1246 rdev->preferred_minor = sb->md_minor;
1247 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001248 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001249 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001250 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 if (sb->level == LEVEL_MULTIPATH)
1253 rdev->desc_nr = -1;
1254 else
1255 rdev->desc_nr = sb->this_disk.number;
1256
Yufen Yu228fc7d2019-10-30 18:47:02 +08001257 /* not spare disk, or LEVEL_MULTIPATH */
1258 if (sb->level == LEVEL_MULTIPATH ||
1259 (rdev->desc_nr >= 0 &&
Yufen Yu3b7436c2019-12-10 15:01:29 +08001260 rdev->desc_nr < MD_SB_DISKS &&
Yufen Yu228fc7d2019-10-30 18:47:02 +08001261 sb->disks[rdev->desc_nr].state &
1262 ((1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))))
1263 spare_disk = false;
1264
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001265 if (!refdev) {
Yufen Yu228fc7d2019-10-30 18:47:02 +08001266 if (!spare_disk)
Yufen Yu6a5cb532019-10-16 16:00:03 +08001267 ret = 1;
1268 else
1269 ret = 0;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001270 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001272 mdp_super_t *refsb = page_address(refdev->sb_page);
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001273 if (!md_uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001274 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 b, bdevname(refdev->bdev,b2));
1276 goto abort;
1277 }
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001278 if (!md_sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001279 pr_warn("md: %s has same UUID but different superblock to %s\n",
1280 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 goto abort;
1282 }
1283 ev1 = md_event(sb);
1284 ev2 = md_event(refsb);
Yufen Yu6a5cb532019-10-16 16:00:03 +08001285
Yufen Yu228fc7d2019-10-30 18:47:02 +08001286 if (!spare_disk && ev1 > ev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001288 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 ret = 0;
1290 }
NeilBrown8190e752009-06-18 08:48:58 +10001291 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001292 /* Limit to 4TB as metadata cannot record more than that.
1293 * (not needed for Linear and RAID0 as metadata doesn't
1294 * record this size)
1295 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001296 if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001297 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
NeilBrown27a7b262011-09-10 17:21:28 +10001299 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001300 /* "this cannot possibly happen" ... */
1301 ret = -EINVAL;
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 abort:
1304 return ret;
1305}
1306
1307/*
1308 * validate_super for 0.90.0
1309 */
NeilBrownfd01b882011-10-11 16:47:53 +11001310static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001313 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001314 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
NeilBrown41158c72005-06-21 17:17:25 -07001316 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001317 clear_bit(Faulty, &rdev->flags);
1318 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001319 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001320 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (mddev->raid_disks == 0) {
1323 mddev->major_version = 0;
1324 mddev->minor_version = sb->minor_version;
1325 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001326 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001327 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 mddev->ctime = sb->ctime;
1329 mddev->utime = sb->utime;
1330 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001331 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 mddev->layout = sb->layout;
1333 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001334 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001335 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001336 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001337 mddev->bitmap_info.space = 0;
1338 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001339 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001340 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001341 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
NeilBrownf6705572006-03-27 01:18:11 -08001343 if (mddev->minor_version >= 91) {
1344 mddev->reshape_position = sb->reshape_position;
1345 mddev->delta_disks = sb->delta_disks;
1346 mddev->new_level = sb->new_level;
1347 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001348 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001349 if (mddev->delta_disks < 0)
1350 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001351 } else {
1352 mddev->reshape_position = MaxSector;
1353 mddev->delta_disks = 0;
1354 mddev->new_level = mddev->level;
1355 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001356 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001357 }
NeilBrown33f2c352019-09-09 16:52:29 +10001358 if (mddev->level == 0)
1359 mddev->layout = -1;
NeilBrownf6705572006-03-27 01:18:11 -08001360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (sb->state & (1<<MD_SB_CLEAN))
1362 mddev->recovery_cp = MaxSector;
1363 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001364 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 sb->events_lo == sb->cp_events_lo) {
1366 mddev->recovery_cp = sb->recovery_cp;
1367 } else
1368 mddev->recovery_cp = 0;
1369 }
1370
1371 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1372 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1373 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1374 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1375
1376 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001377
1378 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001379 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001380 mddev->bitmap_info.offset =
1381 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001382 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001383 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001384 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001385
NeilBrown41158c72005-06-21 17:17:25 -07001386 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001387 /* Insist on good event counter while assembling, except
1388 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001390 if (sb->disks[rdev->desc_nr].state & (
1391 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001392 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001393 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001394 } else if (mddev->bitmap) {
1395 /* if adding to array with a bitmap, then we can accept an
1396 * older device ... but not too old.
1397 */
NeilBrown41158c72005-06-21 17:17:25 -07001398 if (ev1 < mddev->bitmap->events_cleared)
1399 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001400 if (ev1 < mddev->events)
1401 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001402 } else {
1403 if (ev1 < mddev->events)
1404 /* just a hot-add of a new device, leave raid_disk at -1 */
1405 return 0;
1406 }
NeilBrown41158c72005-06-21 17:17:25 -07001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 desc = sb->disks + rdev->desc_nr;
1410
1411 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001412 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001413 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1414 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001415 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001417 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001418 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1419 /* active but not in sync implies recovery up to
1420 * reshape position. We don't know exactly where
1421 * that is, so set to zero for now */
1422 if (mddev->minor_version >= 91) {
1423 rdev->recovery_offset = 0;
1424 rdev->raid_disk = desc->raid_disk;
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001427 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1428 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001429 if (desc->state & (1<<MD_DISK_FAILFAST))
1430 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001431 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001432 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return 0;
1434}
1435
1436/*
1437 * sync_super for 0.90.0
1438 */
NeilBrownfd01b882011-10-11 16:47:53 +11001439static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001442 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 /* make rdev->sb match mddev data..
1446 *
1447 * 1/ zero out disks
1448 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1449 * 3/ any empty disks < next_spare become removed
1450 *
1451 * disks[0] gets initialised to REMOVED because
1452 * we cannot be sure from other fields if it has
1453 * been initialised or not.
1454 */
1455 int i;
1456 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1457
NeilBrown61181562005-09-09 16:24:02 -07001458 rdev->sb_size = MD_SB_BYTES;
1459
Namhyung Kim65a06f062011-07-27 11:00:36 +10001460 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 memset(sb, 0, sizeof(*sb));
1463
1464 sb->md_magic = MD_SB_MAGIC;
1465 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 sb->patch_version = mddev->patch_version;
1467 sb->gvalid_words = 0; /* ignored */
1468 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1469 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1470 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1471 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1472
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001473 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001475 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 sb->raid_disks = mddev->raid_disks;
1477 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001478 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001479 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 sb->state = 0;
1481 sb->events_hi = (mddev->events>>32);
1482 sb->events_lo = (u32)mddev->events;
1483
NeilBrownf6705572006-03-27 01:18:11 -08001484 if (mddev->reshape_position == MaxSector)
1485 sb->minor_version = 90;
1486 else {
1487 sb->minor_version = 91;
1488 sb->reshape_position = mddev->reshape_position;
1489 sb->new_level = mddev->new_level;
1490 sb->delta_disks = mddev->delta_disks;
1491 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001492 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001493 }
1494 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (mddev->in_sync)
1496 {
1497 sb->recovery_cp = mddev->recovery_cp;
1498 sb->cp_events_hi = (mddev->events>>32);
1499 sb->cp_events_lo = (u32)mddev->events;
1500 if (mddev->recovery_cp == MaxSector)
1501 sb->state = (1<< MD_SB_CLEAN);
1502 } else
1503 sb->recovery_cp = 0;
1504
1505 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001506 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
NeilBrownc3d97142009-12-14 12:49:52 +11001508 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001509 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001512 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001514 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001515 int is_active = test_bit(In_sync, &rdev2->flags);
1516
1517 if (rdev2->raid_disk >= 0 &&
1518 sb->minor_version >= 91)
1519 /* we have nowhere to store the recovery_offset,
1520 * but if it is not below the reshape_position,
1521 * we can piggy-back on that.
1522 */
1523 is_active = 1;
1524 if (rdev2->raid_disk < 0 ||
1525 test_bit(Faulty, &rdev2->flags))
1526 is_active = 0;
1527 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001528 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001530 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001531 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 d = &sb->disks[rdev2->desc_nr];
1533 nr_disks++;
1534 d->number = rdev2->desc_nr;
1535 d->major = MAJOR(rdev2->bdev->bd_dev);
1536 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001537 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 d->raid_disk = rdev2->raid_disk;
1539 else
1540 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001541 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001543 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001545 if (test_bit(In_sync, &rdev2->flags))
1546 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 active++;
1548 working++;
1549 } else {
1550 d->state = 0;
1551 spare++;
1552 working++;
1553 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001554 if (test_bit(WriteMostly, &rdev2->flags))
1555 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001556 if (test_bit(FailFast, &rdev2->flags))
1557 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 /* now set the "removed" and "faulty" bits on any missing devices */
1560 for (i=0 ; i < mddev->raid_disks ; i++) {
1561 mdp_disk_t *d = &sb->disks[i];
1562 if (d->state == 0 && d->number == 0) {
1563 d->number = i;
1564 d->raid_disk = i;
1565 d->state = (1<<MD_DISK_REMOVED);
1566 d->state |= (1<<MD_DISK_FAULTY);
1567 failed++;
1568 }
1569 }
1570 sb->nr_disks = nr_disks;
1571 sb->active_disks = active;
1572 sb->working_disks = working;
1573 sb->failed_disks = failed;
1574 sb->spare_disks = spare;
1575
1576 sb->this_disk = sb->disks[rdev->desc_nr];
1577 sb->sb_csum = calc_sb_csum(sb);
1578}
1579
1580/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001581 * rdev_size_change for 0.90.0
1582 */
1583static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001584super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001585{
Andre Noll58c0fed2009-03-31 14:33:13 +11001586 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001587 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001588 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001589 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001590 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001591 if (!num_sectors || num_sectors > rdev->sb_start)
1592 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001593 /* Limit to 4TB as metadata cannot record more than that.
1594 * 4TB == 2^32 KB, or 2*2^32 sectors.
1595 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001596 if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001597 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001598 do {
1599 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001600 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001601 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001602 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001603}
1604
NeilBrownc6563a82012-05-21 09:27:00 +10001605static int
1606super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1607{
1608 /* non-zero offset changes not possible with v0.90 */
1609 return new_offset == 0;
1610}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001611
1612/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 * version 1 superblock
1614 */
1615
NeilBrownf72ffdd2014-09-30 14:23:59 +10001616static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001618 __le32 disk_csum;
1619 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 unsigned long long newcsum;
1621 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001622 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 disk_csum = sb->sb_csum;
1625 sb->sb_csum = 0;
1626 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001627 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 newcsum += le32_to_cpu(*isuper++);
1629
1630 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001631 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1634 sb->sb_csum = disk_csum;
1635 return cpu_to_le32(csum);
1636}
1637
NeilBrown3cb03002011-10-11 16:45:26 +11001638static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640 struct mdp_superblock_1 *sb;
1641 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001642 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001643 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001645 int bmask;
Yufen Yu228fc7d2019-10-30 18:47:02 +08001646 bool spare_disk = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 /*
Andre Noll0f420352008-07-11 22:02:23 +10001649 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 * It is always aligned to a 4K boundary and
1651 * depeding on minor_version, it can be:
1652 * 0: At least 8K, but less than 12K, from end of device
1653 * 1: At start of device
1654 * 2: 4K from start of device.
1655 */
1656 switch(minor_version) {
1657 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001658 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001659 sb_start -= 8*2;
1660 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 break;
1662 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001663 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 break;
1665 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001666 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 break;
1668 default:
1669 return -EINVAL;
1670 }
Andre Noll0f420352008-07-11 22:02:23 +10001671 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
NeilBrown0002b272005-09-09 16:23:53 -07001673 /* superblock is rarely larger than 1K, but it can be larger,
1674 * and it is safe to read 4k, so we do that
1675 */
1676 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 if (ret) return ret;
1678
Namhyung Kim65a06f062011-07-27 11:00:36 +10001679 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1682 sb->major_version != cpu_to_le32(1) ||
1683 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001684 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001685 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 return -EINVAL;
1687
1688 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001689 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 bdevname(rdev->bdev,b));
1691 return -EINVAL;
1692 }
1693 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001694 pr_warn("md: data_size too small on %s\n",
1695 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return -EINVAL;
1697 }
NeilBrownc6563a82012-05-21 09:27:00 +10001698 if (sb->pad0 ||
1699 sb->pad3[0] ||
1700 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1701 /* Some padding is non-zero, might be a new feature */
1702 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 rdev->preferred_minor = 0xffff;
1705 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001706 rdev->new_data_offset = rdev->data_offset;
1707 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1708 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1709 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001710 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
NeilBrown0002b272005-09-09 16:23:53 -07001712 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001713 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001714 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001715 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1716
1717 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001718 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001719 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001720 if (minor_version
1721 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1722 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001723
NeilBrown31b65a02006-07-10 04:44:14 -07001724 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1725 rdev->desc_nr = -1;
1726 else
1727 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1728
NeilBrown2699b672011-07-28 11:31:47 +10001729 if (!rdev->bb_page) {
1730 rdev->bb_page = alloc_page(GFP_KERNEL);
1731 if (!rdev->bb_page)
1732 return -ENOMEM;
1733 }
1734 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1735 rdev->badblocks.count == 0) {
1736 /* need to load the bad block list.
1737 * Currently we limit it to one page.
1738 */
1739 s32 offset;
1740 sector_t bb_sector;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001741 __le64 *bbp;
NeilBrown2699b672011-07-28 11:31:47 +10001742 int i;
1743 int sectors = le16_to_cpu(sb->bblog_size);
1744 if (sectors > (PAGE_SIZE / 512))
1745 return -EINVAL;
1746 offset = le32_to_cpu(sb->bblog_offset);
1747 if (offset == 0)
1748 return -EINVAL;
1749 bb_sector = (long long)offset;
1750 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001751 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001752 return -EIO;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001753 bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10001754 rdev->badblocks.shift = sb->bblog_shift;
1755 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1756 u64 bb = le64_to_cpu(*bbp);
1757 int count = bb & (0x3ff);
1758 u64 sector = bb >> 10;
1759 sector <<= sb->bblog_shift;
1760 count <<= sb->bblog_shift;
1761 if (bb + 1 == 0)
1762 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001763 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001764 return -EINVAL;
1765 }
NeilBrown486adf72013-04-24 11:42:44 +10001766 } else if (sb->bblog_offset != 0)
1767 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001768
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001769 if ((le32_to_cpu(sb->feature_map) &
1770 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001771 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1772 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1773 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1774 }
1775
NeilBrown33f2c352019-09-09 16:52:29 +10001776 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT) &&
1777 sb->level != 0)
1778 return -EINVAL;
1779
Yufen Yu228fc7d2019-10-30 18:47:02 +08001780 /* not spare disk, or LEVEL_MULTIPATH */
1781 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH) ||
1782 (rdev->desc_nr >= 0 &&
1783 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1784 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1785 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)))
1786 spare_disk = false;
Yufen Yu6a5cb532019-10-16 16:00:03 +08001787
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001788 if (!refdev) {
Yufen Yu228fc7d2019-10-30 18:47:02 +08001789 if (!spare_disk)
Yufen Yu6a5cb532019-10-16 16:00:03 +08001790 ret = 1;
1791 else
1792 ret = 0;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001793 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001795 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1798 sb->level != refsb->level ||
1799 sb->layout != refsb->layout ||
1800 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001801 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 bdevname(rdev->bdev,b),
1803 bdevname(refdev->bdev,b2));
1804 return -EINVAL;
1805 }
1806 ev1 = le64_to_cpu(sb->events);
1807 ev2 = le64_to_cpu(refsb->events);
1808
Yufen Yu228fc7d2019-10-30 18:47:02 +08001809 if (!spare_disk && ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001810 ret = 1;
1811 else
1812 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
NeilBrownc6563a82012-05-21 09:27:00 +10001814 if (minor_version) {
1815 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1816 sectors -= rdev->data_offset;
1817 } else
1818 sectors = rdev->sb_start;
1819 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001821 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001822 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
NeilBrownfd01b882011-10-11 16:47:53 +11001825static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001827 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001828 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
NeilBrown41158c72005-06-21 17:17:25 -07001830 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001831 clear_bit(Faulty, &rdev->flags);
1832 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001833 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001834 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (mddev->raid_disks == 0) {
1837 mddev->major_version = 1;
1838 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001839 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001840 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001841 mddev->ctime = le64_to_cpu(sb->ctime);
1842 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001844 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 mddev->layout = le32_to_cpu(sb->layout);
1846 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001847 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001848 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001849 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001850 mddev->bitmap_info.space = 0;
1851 /* Default location for bitmap is 1K after superblock
1852 * using 3K - total of 4K
1853 */
NeilBrownc3d97142009-12-14 12:49:52 +11001854 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001855 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001856 mddev->reshape_backwards = 0;
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1859 memcpy(mddev->uuid, sb->set_uuid, 16);
1860
1861 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001862
NeilBrown71c08052005-09-09 16:23:51 -07001863 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001864 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001865 mddev->bitmap_info.offset =
1866 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001867 /* Metadata doesn't record how much space is available.
1868 * For 1.0, we assume we can use up to the superblock
1869 * if before, else to 4K beyond superblock.
1870 * For others, assume no change is possible.
1871 */
1872 if (mddev->minor_version > 0)
1873 mddev->bitmap_info.space = 0;
1874 else if (mddev->bitmap_info.offset > 0)
1875 mddev->bitmap_info.space =
1876 8 - mddev->bitmap_info.offset;
1877 else
1878 mddev->bitmap_info.space =
1879 -mddev->bitmap_info.offset;
1880 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001881
NeilBrownf6705572006-03-27 01:18:11 -08001882 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1883 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1884 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1885 mddev->new_level = le32_to_cpu(sb->new_level);
1886 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001887 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001888 if (mddev->delta_disks < 0 ||
1889 (mddev->delta_disks == 0 &&
1890 (le32_to_cpu(sb->feature_map)
1891 & MD_FEATURE_RESHAPE_BACKWARDS)))
1892 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001893 } else {
1894 mddev->reshape_position = MaxSector;
1895 mddev->delta_disks = 0;
1896 mddev->new_level = mddev->level;
1897 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001898 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001899 }
1900
NeilBrown33f2c352019-09-09 16:52:29 +10001901 if (mddev->level == 0 &&
1902 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT))
1903 mddev->layout = -1;
1904
Song Liu486b0f72016-08-19 15:34:01 -07001905 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001906 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001907
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001908 if (le32_to_cpu(sb->feature_map) &
1909 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001910 if (le32_to_cpu(sb->feature_map) &
1911 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1912 return -EINVAL;
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001913 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1914 (le32_to_cpu(sb->feature_map) &
1915 MD_FEATURE_MULTIPLE_PPLS))
1916 return -EINVAL;
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001917 set_bit(MD_HAS_PPL, &mddev->flags);
1918 }
NeilBrown41158c72005-06-21 17:17:25 -07001919 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001920 /* Insist of good event counter while assembling, except for
1921 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001923 if (rdev->desc_nr >= 0 &&
1924 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001925 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1926 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001927 if (ev1 < mddev->events)
1928 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001929 } else if (mddev->bitmap) {
1930 /* If adding to array with a bitmap, then we can accept an
1931 * older device, but not too old.
1932 */
NeilBrown41158c72005-06-21 17:17:25 -07001933 if (ev1 < mddev->bitmap->events_cleared)
1934 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001935 if (ev1 < mddev->events)
1936 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001937 } else {
1938 if (ev1 < mddev->events)
1939 /* just a hot-add of a new device, leave raid_disk at -1 */
1940 return 0;
1941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (mddev->level != LEVEL_MULTIPATH) {
1943 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001944 if (rdev->desc_nr < 0 ||
1945 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001946 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001947 rdev->desc_nr = -1;
1948 } else
1949 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001951 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001953 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001954 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 break;
Song Liubac624f2015-08-13 14:31:55 -07001956 case MD_DISK_ROLE_JOURNAL: /* journal device */
1957 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1958 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001959 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001960 return -EINVAL;
1961 }
1962 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001963 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001964 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001965 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 default:
NeilBrownf4667222013-12-09 12:04:56 +11001967 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001968 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001969 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001970 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001971 if (!(le32_to_cpu(sb->feature_map) &
1972 MD_FEATURE_RECOVERY_BITMAP))
1973 rdev->saved_raid_disk = -1;
Guoqing Jiang062f5b2a2019-07-24 11:09:20 +02001974 } else {
1975 /*
1976 * If the array is FROZEN, then the device can't
1977 * be in_sync with rest of array.
1978 */
1979 if (!test_bit(MD_RECOVERY_FROZEN,
1980 &mddev->recovery))
1981 set_bit(In_sync, &rdev->flags);
1982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 rdev->raid_disk = role;
1984 break;
1985 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001986 if (sb->devflags & WriteMostly1)
1987 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001988 if (sb->devflags & FailFast1)
1989 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001990 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1991 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001992 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001993 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return 0;
1996}
1997
NeilBrownfd01b882011-10-11 16:47:53 +11001998static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
2000 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11002001 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 int max_dev, i;
2003 /* make rdev->sb match mddev and rdev data. */
2004
Namhyung Kim65a06f062011-07-27 11:00:36 +10002005 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 sb->feature_map = 0;
2008 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002009 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 memset(sb->pad3, 0, sizeof(sb->pad3));
2011
2012 sb->utime = cpu_to_le64((__u64)mddev->utime);
2013 sb->events = cpu_to_le64(mddev->events);
2014 if (mddev->in_sync)
2015 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07002016 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
2017 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 else
2019 sb->resync_offset = cpu_to_le64(0);
2020
NeilBrown1c05b4b2006-10-21 10:24:08 -07002021 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08002022
NeilBrownf0ca3402006-02-02 14:28:04 -08002023 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11002024 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10002025 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10002026 sb->level = cpu_to_le32(mddev->level);
2027 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11002028 if (test_bit(FailFast, &rdev->flags))
2029 sb->devflags |= FailFast1;
2030 else
2031 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08002032
NeilBrownaeb9b2112011-08-25 14:43:08 +10002033 if (test_bit(WriteMostly, &rdev->flags))
2034 sb->devflags |= WriteMostly1;
2035 else
2036 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10002037 sb->data_offset = cpu_to_le64(rdev->data_offset);
2038 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10002039
NeilBrownc3d97142009-12-14 12:49:52 +11002040 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
2041 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07002042 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07002043 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002044
Shaohua Lif2076e72015-10-08 21:54:12 -07002045 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11002046 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11002047 sb->feature_map |=
2048 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
2049 sb->recovery_offset =
2050 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11002051 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
2052 sb->feature_map |=
2053 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002054 }
Shaohua Li3069aa82015-08-13 14:31:56 -07002055 /* Note: recovery_offset and journal_tail share space */
2056 if (test_bit(Journal, &rdev->flags))
2057 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002058 if (test_bit(Replacement, &rdev->flags))
2059 sb->feature_map |=
2060 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002061
NeilBrownf6705572006-03-27 01:18:11 -08002062 if (mddev->reshape_position != MaxSector) {
2063 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
2064 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
2065 sb->new_layout = cpu_to_le32(mddev->new_layout);
2066 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
2067 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10002068 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10002069 if (mddev->delta_disks == 0 &&
2070 mddev->reshape_backwards)
2071 sb->feature_map
2072 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10002073 if (rdev->new_data_offset != rdev->data_offset) {
2074 sb->feature_map
2075 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
2076 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
2077 - rdev->data_offset));
2078 }
NeilBrownf6705572006-03-27 01:18:11 -08002079 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002080
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10002081 if (mddev_is_clustered(mddev))
2082 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
2083
NeilBrown2699b672011-07-28 11:31:47 +10002084 if (rdev->badblocks.count == 0)
2085 /* Nothing to do for bad blocks*/ ;
2086 else if (sb->bblog_offset == 0)
2087 /* Cannot record bad blocks on this device */
2088 md_error(mddev, rdev);
2089 else {
2090 struct badblocks *bb = &rdev->badblocks;
Christoph Hellwigae506402019-04-04 18:56:13 +02002091 __le64 *bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10002092 u64 *p = bb->page;
2093 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
2094 if (bb->changed) {
2095 unsigned seq;
2096
2097retry:
2098 seq = read_seqbegin(&bb->lock);
2099
2100 memset(bbp, 0xff, PAGE_SIZE);
2101
2102 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08002103 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10002104 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
2105 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08002106 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10002107 }
NeilBrownd0962932012-03-19 12:46:41 +11002108 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002109 if (read_seqretry(&bb->lock, seq))
2110 goto retry;
2111
2112 bb->sector = (rdev->sb_start +
2113 (int)le32_to_cpu(sb->bblog_offset));
2114 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10002115 }
2116 }
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11002119 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (rdev2->desc_nr+1 > max_dev)
2121 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07002122
NeilBrown70471da2009-08-03 10:59:57 +10002123 if (max_dev > le32_to_cpu(sb->max_dev)) {
2124 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07002125 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10002126 rdev->sb_size = max_dev * 2 + 256;
2127 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
2128 if (rdev->sb_size & bmask)
2129 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10002130 } else
2131 max_dev = le32_to_cpu(sb->max_dev);
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 for (i=0; i<max_dev;i++)
Lidong Zhong8df72022017-06-12 10:45:55 +08002134 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
NeilBrownf72ffdd2014-09-30 14:23:59 +10002135
Song Liua97b7892015-10-08 21:54:09 -07002136 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
2137 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01002139 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
Pawel Baldysiakddc08822017-08-16 17:13:45 +02002140 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
2141 sb->feature_map |=
2142 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
2143 else
2144 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01002145 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
2146 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
2147 }
2148
NeilBrowndafb20f2012-03-19 12:46:39 +11002149 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002151 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07002152 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08002153 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07002155 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07002156 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11002157 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002158 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 else
Song Liuc4d4c912015-08-13 14:31:54 -07002160 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 sb->sb_csum = calc_sb_1_csum(sb);
2164}
2165
Chris Webb0cd17fe2008-06-28 08:31:46 +10002166static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11002167super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002168{
2169 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002170 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11002171 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002172 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10002173 if (rdev->data_offset != rdev->new_data_offset)
2174 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10002175 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10002176 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01002177 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002178 max_sectors -= rdev->data_offset;
2179 if (!num_sectors || num_sectors > max_sectors)
2180 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11002181 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10002182 /* minor version 0 with bitmap we can't move */
2183 return 0;
2184 } else {
2185 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10002186 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01002187 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10002188 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11002189 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002190 if (!num_sectors || num_sectors > max_sectors)
2191 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10002192 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002193 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10002194 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10002195 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08002196 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10002197 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11002198 do {
2199 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
2200 rdev->sb_page);
2201 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11002202 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10002203
2204}
2205
2206static int
2207super_1_allow_new_offset(struct md_rdev *rdev,
2208 unsigned long long new_offset)
2209{
2210 /* All necessary checks on new >= old have been done */
2211 struct bitmap *bitmap;
2212 if (new_offset >= rdev->data_offset)
2213 return 1;
2214
2215 /* with 1.0 metadata, there is no metadata to tread on
2216 * so we can always move back */
2217 if (rdev->mddev->minor_version == 0)
2218 return 1;
2219
2220 /* otherwise we must be sure not to step on
2221 * any metadata, so stay:
2222 * 36K beyond start of superblock
2223 * beyond end of badblocks
2224 * beyond write-intent bitmap
2225 */
2226 if (rdev->sb_start + (32+4)*2 > new_offset)
2227 return 0;
2228 bitmap = rdev->mddev->bitmap;
2229 if (bitmap && !rdev->mddev->bitmap_info.file &&
2230 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10002231 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10002232 return 0;
2233 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2234 return 0;
2235
2236 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002237}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Adrian Bunk75c96f82005-05-05 16:16:09 -07002239static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 [0] = {
2241 .name = "0.90.0",
2242 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002243 .load_super = super_90_load,
2244 .validate_super = super_90_validate,
2245 .sync_super = super_90_sync,
2246 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002247 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 },
2249 [1] = {
2250 .name = "md-1",
2251 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002252 .load_super = super_1_load,
2253 .validate_super = super_1_validate,
2254 .sync_super = super_1_sync,
2255 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002256 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 },
2258};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
NeilBrownfd01b882011-10-11 16:47:53 +11002260static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05002261{
2262 if (mddev->sync_super) {
2263 mddev->sync_super(mddev, rdev);
2264 return;
2265 }
2266
2267 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2268
2269 super_types[mddev->major_version].sync_super(mddev, rdev);
2270}
2271
NeilBrownfd01b882011-10-11 16:47:53 +11002272static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
NeilBrown3cb03002011-10-11 16:45:26 +11002274 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
NeilBrown4b809912008-07-21 17:05:25 +10002276 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07002277 rdev_for_each_rcu(rdev, mddev1) {
2278 if (test_bit(Faulty, &rdev->flags) ||
2279 test_bit(Journal, &rdev->flags) ||
2280 rdev->raid_disk == -1)
2281 continue;
2282 rdev_for_each_rcu(rdev2, mddev2) {
2283 if (test_bit(Faulty, &rdev2->flags) ||
2284 test_bit(Journal, &rdev2->flags) ||
2285 rdev2->raid_disk == -1)
2286 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002287 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002288 rdev2->bdev->bd_contains) {
2289 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002290 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002291 }
Song Liu0b020e82015-09-03 23:00:35 -07002292 }
2293 }
NeilBrown4b809912008-07-21 17:05:25 +10002294 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return 0;
2296}
2297
2298static LIST_HEAD(pending_raid_disks);
2299
Andre Nollac5e7112009-08-03 10:59:47 +10002300/*
2301 * Try to register data integrity profile for an mddev
2302 *
2303 * This is called when an array is started and after a disk has been kicked
2304 * from the array. It only succeeds if all working and active component devices
2305 * are integrity capable with matching profiles.
2306 */
NeilBrownfd01b882011-10-11 16:47:53 +11002307int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002308{
NeilBrown3cb03002011-10-11 16:45:26 +11002309 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002310
Andre Nollac5e7112009-08-03 10:59:47 +10002311 if (list_empty(&mddev->disks))
2312 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002313 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2314 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002315 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002316 /* skip spares and non-functional disks */
2317 if (test_bit(Faulty, &rdev->flags))
2318 continue;
2319 if (rdev->raid_disk < 0)
2320 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002321 if (!reference) {
2322 /* Use the first rdev as the reference */
2323 reference = rdev;
2324 continue;
2325 }
2326 /* does this rdev's profile match the reference profile? */
2327 if (blk_integrity_compare(reference->bdev->bd_disk,
2328 rdev->bdev->bd_disk) < 0)
2329 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002330 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002331 if (!reference || !bdev_get_integrity(reference->bdev))
2332 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002333 /*
2334 * All component devices are integrity capable and have matching
2335 * profiles, register the common profile for the md device.
2336 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002337 blk_integrity_register(mddev->gendisk,
2338 bdev_get_integrity(reference->bdev));
2339
NeilBrown9d487392016-11-02 14:16:49 +11002340 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Kent Overstreetafeee512018-05-20 18:25:52 -04002341 if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002342 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002343 mdname(mddev));
2344 return -EINVAL;
2345 }
Andre Nollac5e7112009-08-03 10:59:47 +10002346 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002347}
Andre Nollac5e7112009-08-03 10:59:47 +10002348EXPORT_SYMBOL(md_integrity_register);
2349
Dan Williams1501efa2016-01-13 16:00:07 -08002350/*
2351 * Attempt to add an rdev, but only if it is consistent with the current
2352 * integrity profile
2353 */
2354int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002355{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002356 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002357 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002358
2359 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002360 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002361
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002362 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002363
2364 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002365 return 0;
2366
2367 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002368 pr_err("%s: incompatible integrity profile for %s\n",
2369 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002370 return -ENXIO;
2371 }
2372
2373 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002374}
2375EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002376
NeilBrownf72ffdd2014-09-30 14:23:59 +10002377static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002379 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002380 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002381 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Dan Williams11e2ede2008-04-30 00:52:32 -07002383 /* prevent duplicates */
2384 if (find_rdev(mddev, rdev->bdev->bd_dev))
2385 return -EEXIST;
2386
NeilBrown97b20ef2017-04-13 08:53:48 +10002387 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2388 mddev->pers)
2389 return -EROFS;
2390
Andre Nolldd8ac332009-03-31 14:33:13 +11002391 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002392 if (!test_bit(Journal, &rdev->flags) &&
2393 rdev->sectors &&
2394 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002395 if (mddev->pers) {
2396 /* Cannot change size, so fail
2397 * If mddev->level <= 0, then we don't care
2398 * about aligning sizes (e.g. linear)
2399 */
2400 if (mddev->level > 0)
2401 return -ENOSPC;
2402 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002403 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 /* Verify rdev->desc_nr is unique.
2407 * If it is -1, assign a free number, else
2408 * check number is not in use
2409 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002410 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if (rdev->desc_nr < 0) {
2412 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002413 if (mddev->pers)
2414 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002415 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 choice++;
2417 rdev->desc_nr = choice;
2418 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002419 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002420 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002424 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002425 if (!test_bit(Journal, &rdev->flags) &&
2426 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002427 pr_warn("md: %s: array is limited to %d devices\n",
2428 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002429 return -EBUSY;
2430 }
NeilBrown19133a42005-11-08 21:39:35 -08002431 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002432 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002435 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002436
Guoqing Jiang963c5552019-06-14 17:10:36 +08002437 if (mddev->raid_disks)
Guoqing Jiang404659c2019-12-23 10:48:53 +01002438 mddev_create_serial_pool(mddev, rdev, false);
Guoqing Jiang963c5552019-06-14 17:10:36 +08002439
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002440 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002441 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002442
Tejun Heo0762b8b2008-08-25 19:56:12 +09002443 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002444 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2445 /* failure here is OK */;
2446 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002447
NeilBrown4b809912008-07-21 17:05:25 +10002448 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002449 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002450
2451 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002452 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002455
2456 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002457 pr_warn("md: failed to register dev-%s for %s\n",
2458 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002459 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002462static void rdev_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002463{
NeilBrown3cb03002011-10-11 16:45:26 +11002464 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002465 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002466 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002467}
2468
NeilBrownf72ffdd2014-09-30 14:23:59 +10002469static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002472
Tejun Heo49731ba2011-01-14 18:43:57 +01002473 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002474 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002475 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +01002476 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002478 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002479 sysfs_put(rdev->sysfs_state);
2480 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002481 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002482 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002483 * writing to 'remove' to "dev/state". We also need
2484 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002485 */
NeilBrown4b809912008-07-21 17:05:25 +10002486 synchronize_rcu();
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002487 INIT_WORK(&rdev->del_work, rdev_delayed_delete);
NeilBrown177a99b2008-02-06 01:39:56 -08002488 kobject_get(&rdev->kobj);
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002489 queue_work(md_rdev_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
2491
2492/*
2493 * prevent the device from being mounted, repartitioned or
2494 * otherwise reused by a RAID array (or any other kernel
2495 * subsystem), by bd_claiming the device.
2496 */
NeilBrown3cb03002011-10-11 16:45:26 +11002497static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
2499 int err = 0;
2500 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Tejun Heod4d77622010-11-13 11:55:18 +01002502 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002503 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 if (IS_ERR(bdev)) {
Christoph Hellwigea3edd42020-03-24 08:25:11 +01002505 pr_warn("md: could not open device unknown-block(%u,%u).\n",
2506 MAJOR(dev), MINOR(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 return PTR_ERR(bdev);
2508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 rdev->bdev = bdev;
2510 return err;
2511}
2512
NeilBrown3cb03002011-10-11 16:45:26 +11002513static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
2515 struct block_device *bdev = rdev->bdev;
2516 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002517 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
2519
2520void md_autodetect_dev(dev_t dev);
2521
NeilBrownf72ffdd2014-09-30 14:23:59 +10002522static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
2524 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002525
NeilBrown9d487392016-11-02 14:16:49 +11002526 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002527 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002529 if (test_bit(AutoDetected, &rdev->flags))
2530 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531#endif
2532 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002533 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534}
2535
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002536void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
2538 unbind_rdev_from_array(rdev);
2539 export_rdev(rdev);
2540}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002541EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
NeilBrownfd01b882011-10-11 16:47:53 +11002543static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
NeilBrown0638bb02014-09-25 17:43:47 +10002545 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
NeilBrown0638bb02014-09-25 17:43:47 +10002547 while (!list_empty(&mddev->disks)) {
2548 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2549 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002550 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 mddev->raid_disks = 0;
2553 mddev->major_version = 0;
2554}
2555
NeilBrown6497709b2017-03-15 14:05:14 +11002556static bool set_in_sync(struct mddev *mddev)
2557{
Shaohua Liefa4b772017-10-18 22:08:13 -07002558 lockdep_assert_held(&mddev->lock);
NeilBrown4ad23a972017-03-15 14:05:14 +11002559 if (!mddev->in_sync) {
2560 mddev->sync_checkers++;
2561 spin_unlock(&mddev->lock);
2562 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2563 spin_lock(&mddev->lock);
2564 if (!mddev->in_sync &&
2565 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002566 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002567 /*
2568 * Ensure ->in_sync is visible before we clear
2569 * ->sync_checkers.
2570 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002571 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002572 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2573 sysfs_notify_dirent_safe(mddev->sysfs_state);
2574 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002575 if (--mddev->sync_checkers == 0)
2576 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002577 }
2578 if (mddev->safemode == 1)
2579 mddev->safemode = 0;
2580 return mddev->in_sync;
2581}
2582
NeilBrownf72ffdd2014-09-30 14:23:59 +10002583static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
NeilBrown42543762006-06-26 00:27:57 -07002585 /* Update each superblock (in-memory image), but
2586 * if we are allowed to, skip spares which already
2587 * have the right event counter, or have one earlier
2588 * (which would mean they aren't being marked as dirty
2589 * with the rest of the array)
2590 */
NeilBrown3cb03002011-10-11 16:45:26 +11002591 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002592 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002593 if (rdev->sb_events == mddev->events ||
2594 (nospares &&
2595 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002596 rdev->sb_events+1 == mddev->events)) {
2597 /* Don't update this superblock */
2598 rdev->sb_loaded = 2;
2599 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002600 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002601 rdev->sb_loaded = 1;
2602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 }
2604}
2605
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002606static bool does_sb_need_changing(struct mddev *mddev)
2607{
2608 struct md_rdev *rdev;
2609 struct mdp_superblock_1 *sb;
2610 int role;
2611
2612 /* Find a good rdev */
2613 rdev_for_each(rdev, mddev)
2614 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2615 break;
2616
2617 /* No good device found. */
2618 if (!rdev)
2619 return false;
2620
2621 sb = page_address(rdev->sb_page);
2622 /* Check if a device has become faulty or a spare become active */
2623 rdev_for_each(rdev, mddev) {
2624 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2625 /* Device activated? */
2626 if (role == 0xffff && rdev->raid_disk >=0 &&
2627 !test_bit(Faulty, &rdev->flags))
2628 return true;
2629 /* Device turned faulty? */
2630 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2631 return true;
2632 }
2633
2634 /* Check if any mddev parameters have changed */
2635 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2636 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002637 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002638 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2639 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2640 return true;
2641
2642 return false;
2643}
2644
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002645void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
NeilBrown3cb03002011-10-11 16:45:26 +11002647 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002648 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002649 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002650 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002651 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
NeilBrownd87f0642013-04-24 11:42:40 +10002653 if (mddev->ro) {
2654 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002655 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002656 return;
2657 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002658
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002659repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002660 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002661 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002662 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002663 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002664 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002665 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002666 /* Has someone else has updated the sb */
2667 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002668 if (ret == 0)
2669 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002670 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2671 BIT(MD_SB_CHANGE_DEVS) |
2672 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002673 return;
2674 }
2675 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002676
NeilBrowndb0505d2017-10-17 16:18:36 +11002677 /*
2678 * First make sure individual recovery_offsets are correct
2679 * curr_resync_completed can only be used during recovery.
2680 * During reshape/resync it might use array-addresses rather
2681 * that device addresses.
2682 */
NeilBrowndafb20f2012-03-19 12:46:39 +11002683 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002684 if (rdev->raid_disk >= 0 &&
2685 mddev->delta_disks >= 0 &&
NeilBrowndb0505d2017-10-17 16:18:36 +11002686 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2687 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2688 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002689 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002690 !test_bit(In_sync, &rdev->flags) &&
2691 mddev->curr_resync_completed > rdev->recovery_offset)
2692 rdev->recovery_offset = mddev->curr_resync_completed;
2693
NeilBrownf72ffdd2014-09-30 14:23:59 +10002694 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002695 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002696 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2697 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002698 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002699 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002700 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002701 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002702 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002703 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002704 md_error(mddev, rdev);
2705 }
2706 clear_bit(Blocked, &rdev->flags);
2707 clear_bit(BlockedBadBlocks, &rdev->flags);
2708 wake_up(&rdev->blocked_wait);
2709 }
2710 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002711 wake_up(&mddev->sb_wait);
2712 return;
2713 }
2714
NeilBrown85572d72014-12-15 12:56:56 +11002715 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002716
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002717 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002718
Shaohua Li29530792016-12-08 15:48:19 -08002719 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002720 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002721 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002722 /* just a clean<-> dirty transition, possibly leave spares alone,
2723 * though if events isn't the right even/odd, we will have to do
2724 * spares after all
2725 */
2726 nospares = 1;
2727 if (force_change)
2728 nospares = 0;
2729 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002730 /* If the array is degraded, then skipping spares is both
2731 * dangerous and fairly pointless.
2732 * Dangerous because a device that was removed from the array
2733 * might have a event_count that still looks up-to-date,
2734 * so it can be re-added without a resync.
2735 * Pointless because if there are any spares to skip,
2736 * then a recovery will happen and soon that array won't
2737 * be degraded any more and the spare can go back to sleep then.
2738 */
NeilBrown850b2b422006-10-03 01:15:46 -07002739 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002740
NeilBrown06d91a52005-06-21 17:17:12 -07002741 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002742
2743 /* If this is just a dirty<->clean transition, and the array is clean
2744 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002745 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002746 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002747 && mddev->can_decrease_events
2748 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002749 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002750 mddev->can_decrease_events = 0;
2751 } else {
NeilBrown42543762006-06-26 00:27:57 -07002752 /* otherwise we have to go forward and ... */
2753 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002754 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
NeilBrown403df472014-09-30 15:52:29 +10002757 /*
2758 * This 64-bit counter should never wrap.
2759 * Either we are in around ~1 trillion A.C., assuming
2760 * 1 reboot per second, or we have a bug...
2761 */
2762 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002763
NeilBrowndafb20f2012-03-19 12:46:39 +11002764 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002765 if (rdev->badblocks.changed)
2766 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002767 if (test_bit(Faulty, &rdev->flags))
2768 set_bit(FaultRecorded, &rdev->flags);
2769 }
NeilBrown2699b672011-07-28 11:31:47 +10002770
NeilBrowne6910632008-02-06 01:39:51 -08002771 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002772 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
NeilBrown36a4e1f2011-10-07 14:23:17 +11002774 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2775 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Shaohua Li504634f2016-11-18 09:44:08 -08002777 if (mddev->queue)
2778 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002779rewrite:
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002780 md_bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002781 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002783
NeilBrown42543762006-06-26 00:27:57 -07002784 if (rdev->sb_loaded != 1)
2785 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
NeilBrownf4667222013-12-09 12:04:56 +11002787 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002788 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002789 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002790 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002791 pr_debug("md: (write) %s's sb offset: %llu\n",
2792 bdevname(rdev->bdev, b),
2793 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002794 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002795 if (rdev->badblocks.size) {
2796 md_super_write(mddev, rdev,
2797 rdev->badblocks.sector,
2798 rdev->badblocks.size << 9,
2799 rdev->bb_page);
2800 rdev->badblocks.size = 0;
2801 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002802
NeilBrownf4667222013-12-09 12:04:56 +11002803 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002804 pr_debug("md: %s (skipping faulty)\n",
2805 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002806
NeilBrown7bfa19f2005-06-21 17:17:28 -07002807 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 /* only need to write one superblock... */
2809 break;
2810 }
NeilBrown46533ff2016-11-18 16:16:11 +11002811 if (md_super_wait(mddev) < 0)
2812 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002813 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002814
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002815 if (mddev_is_clustered(mddev) && ret == 0)
2816 md_cluster_ops->metadata_update_finish(mddev);
2817
NeilBrown850b2b422006-10-03 01:15:46 -07002818 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002819 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2820 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002821 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002822 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002823 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002824 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2825 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002826
NeilBrowndafb20f2012-03-19 12:46:39 +11002827 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002828 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2829 clear_bit(Blocked, &rdev->flags);
2830
2831 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002832 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002833 clear_bit(BlockedBadBlocks, &rdev->flags);
2834 wake_up(&rdev->blocked_wait);
2835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002837EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002839static int add_bound_rdev(struct md_rdev *rdev)
2840{
2841 struct mddev *mddev = rdev->mddev;
2842 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002843 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002844
Shaohua Li87d4d912016-01-06 14:37:14 -08002845 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002846 /* If there is hot_add_disk but no hot_remove_disk
2847 * then added disks for geometry changes,
2848 * and should be added immediately.
2849 */
2850 super_types[mddev->major_version].
2851 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002852 if (add_journal)
2853 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002854 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002855 if (add_journal)
2856 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002857 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002858 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002859 return err;
2860 }
2861 }
2862 sysfs_notify_dirent_safe(rdev->sysfs_state);
2863
Shaohua Li29530792016-12-08 15:48:19 -08002864 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002865 if (mddev->degraded)
2866 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2867 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2868 md_new_event(mddev);
2869 md_wakeup_thread(mddev->thread);
2870 return 0;
2871}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Andre Noll7f6ce762008-03-23 18:34:54 +01002873/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002874 * We want to accept with case. For this we use cmd_match.
2875 */
2876static int cmd_match(const char *cmd, const char *str)
2877{
2878 /* See if cmd, written into a sysfs file, matches
2879 * str. They must either be the same, or cmd can
2880 * have a trailing newline
2881 */
2882 while (*cmd && *str && *cmd == *str) {
2883 cmd++;
2884 str++;
2885 }
2886 if (*cmd == '\n')
2887 cmd++;
2888 if (*str || *cmd)
2889 return 0;
2890 return 1;
2891}
2892
NeilBrown86e6ffd2005-11-08 21:39:24 -08002893struct rdev_sysfs_entry {
2894 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002895 ssize_t (*show)(struct md_rdev *, char *);
2896 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002897};
2898
2899static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002900state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002901{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002902 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002903 size_t len = 0;
Mark Rutland6aa7de02017-10-23 14:07:29 -07002904 unsigned long flags = READ_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002905
NeilBrown758bfc82014-12-15 12:56:59 +11002906 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002907 (!test_bit(ExternalBbl, &flags) &&
2908 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002909 len += sprintf(page+len, "faulty%s", sep);
2910 if (test_bit(In_sync, &flags))
2911 len += sprintf(page+len, "in_sync%s", sep);
2912 if (test_bit(Journal, &flags))
2913 len += sprintf(page+len, "journal%s", sep);
2914 if (test_bit(WriteMostly, &flags))
2915 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002916 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002917 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002918 && !test_bit(Faulty, &flags)))
2919 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002920 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002921 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002922 !test_bit(In_sync, &flags))
2923 len += sprintf(page+len, "spare%s", sep);
2924 if (test_bit(WriteErrorSeen, &flags))
2925 len += sprintf(page+len, "write_error%s", sep);
2926 if (test_bit(WantReplacement, &flags))
2927 len += sprintf(page+len, "want_replacement%s", sep);
2928 if (test_bit(Replacement, &flags))
2929 len += sprintf(page+len, "replacement%s", sep);
2930 if (test_bit(ExternalBbl, &flags))
2931 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002932 if (test_bit(FailFast, &flags))
2933 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002934
2935 if (len)
2936 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002937
NeilBrown86e6ffd2005-11-08 21:39:24 -08002938 return len+sprintf(page+len, "\n");
2939}
2940
NeilBrown45dc2de2006-06-26 00:27:58 -07002941static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002942state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002943{
2944 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002945 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002946 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002947 * writemostly - sets write_mostly
2948 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002949 * blocked - sets the Blocked flags
2950 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002951 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002952 * -insync - clear Insync for a device with a slot assigned,
2953 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002954 * write_error - sets WriteErrorSeen
2955 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002956 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002957 */
2958 int err = -EINVAL;
2959 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2960 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002961 if (test_bit(Faulty, &rdev->flags))
2962 err = 0;
2963 else
2964 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002965 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002966 if (rdev->mddev->pers) {
2967 clear_bit(Blocked, &rdev->flags);
2968 remove_and_add_spares(rdev->mddev, rdev);
2969 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002970 if (rdev->raid_disk >= 0)
2971 err = -EBUSY;
2972 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002973 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002974 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002975 if (mddev_is_clustered(mddev))
2976 err = md_cluster_ops->remove_disk(mddev, rdev);
2977
2978 if (err == 0) {
2979 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002980 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08002981 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11002982 md_wakeup_thread(mddev->thread);
2983 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002984 md_new_event(mddev);
2985 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002986 }
NeilBrownf6556752006-06-26 00:28:01 -07002987 } else if (cmd_match(buf, "writemostly")) {
2988 set_bit(WriteMostly, &rdev->flags);
Guoqing Jiang404659c2019-12-23 10:48:53 +01002989 mddev_create_serial_pool(rdev->mddev, rdev, false);
NeilBrownf6556752006-06-26 00:28:01 -07002990 err = 0;
2991 } else if (cmd_match(buf, "-writemostly")) {
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +01002992 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
NeilBrownf6556752006-06-26 00:28:01 -07002993 clear_bit(WriteMostly, &rdev->flags);
2994 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002995 } else if (cmd_match(buf, "blocked")) {
2996 set_bit(Blocked, &rdev->flags);
2997 err = 0;
2998 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002999 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02003000 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10003001 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10003002 /* metadata handler doesn't understand badblocks,
3003 * so we need to fail the device
3004 */
3005 md_error(rdev->mddev, rdev);
3006 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003007 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10003008 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003009 wake_up(&rdev->blocked_wait);
3010 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3011 md_wakeup_thread(rdev->mddev->thread);
3012
3013 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10003014 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
3015 set_bit(In_sync, &rdev->flags);
3016 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11003017 } else if (cmd_match(buf, "failfast")) {
3018 set_bit(FailFast, &rdev->flags);
3019 err = 0;
3020 } else if (cmd_match(buf, "-failfast")) {
3021 clear_bit(FailFast, &rdev->flags);
3022 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07003023 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
3024 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10003025 if (rdev->mddev->pers == NULL) {
3026 clear_bit(In_sync, &rdev->flags);
3027 rdev->saved_raid_disk = rdev->raid_disk;
3028 rdev->raid_disk = -1;
3029 err = 0;
3030 }
NeilBrownd7a9d442011-07-28 11:31:48 +10003031 } else if (cmd_match(buf, "write_error")) {
3032 set_bit(WriteErrorSeen, &rdev->flags);
3033 err = 0;
3034 } else if (cmd_match(buf, "-write_error")) {
3035 clear_bit(WriteErrorSeen, &rdev->flags);
3036 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11003037 } else if (cmd_match(buf, "want_replacement")) {
3038 /* Any non-spare device that is not a replacement can
3039 * become want_replacement at any time, but we then need to
3040 * check if recovery is needed.
3041 */
3042 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07003043 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11003044 !test_bit(Replacement, &rdev->flags))
3045 set_bit(WantReplacement, &rdev->flags);
3046 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3047 md_wakeup_thread(rdev->mddev->thread);
3048 err = 0;
3049 } else if (cmd_match(buf, "-want_replacement")) {
3050 /* Clearing 'want_replacement' is always allowed.
3051 * Once replacements starts it is too late though.
3052 */
3053 err = 0;
3054 clear_bit(WantReplacement, &rdev->flags);
3055 } else if (cmd_match(buf, "replacement")) {
3056 /* Can only set a device as a replacement when array has not
3057 * yet been started. Once running, replacement is automatic
3058 * from spares, or by assigning 'slot'.
3059 */
3060 if (rdev->mddev->pers)
3061 err = -EBUSY;
3062 else {
3063 set_bit(Replacement, &rdev->flags);
3064 err = 0;
3065 }
3066 } else if (cmd_match(buf, "-replacement")) {
3067 /* Similarly, can only clear Replacement before start */
3068 if (rdev->mddev->pers)
3069 err = -EBUSY;
3070 else {
3071 clear_bit(Replacement, &rdev->flags);
3072 err = 0;
3073 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05003074 } else if (cmd_match(buf, "re-add")) {
Yufen Yuee37e622019-04-02 14:22:14 +08003075 if (!rdev->mddev->pers)
3076 err = -EINVAL;
3077 else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) &&
3078 rdev->saved_raid_disk >= 0) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05003079 /* clear_bit is performed _after_ all the devices
3080 * have their local Faulty bit cleared. If any writes
3081 * happen in the meantime in the local node, they
3082 * will land in the local bitmap, which will be synced
3083 * by this node eventually
3084 */
3085 if (!mddev_is_clustered(rdev->mddev) ||
3086 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
3087 clear_bit(Faulty, &rdev->flags);
3088 err = add_bound_rdev(rdev);
3089 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05003090 } else
3091 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02003092 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
3093 set_bit(ExternalBbl, &rdev->flags);
3094 rdev->badblocks.shift = 0;
3095 err = 0;
3096 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
3097 clear_bit(ExternalBbl, &rdev->flags);
3098 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07003099 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10003100 if (!err)
3101 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07003102 return err ? err : len;
3103}
NeilBrown80ca3a42006-07-10 04:44:18 -07003104static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10003105__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003106
3107static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003108errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08003109{
3110 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
3111}
3112
3113static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003114errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08003115{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003116 unsigned int n;
3117 int rv;
3118
3119 rv = kstrtouint(buf, 10, &n);
3120 if (rv < 0)
3121 return rv;
3122 atomic_set(&rdev->corrected_errors, n);
3123 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08003124}
3125static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07003126__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08003127
NeilBrown014236d2006-01-06 00:20:55 -08003128static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003129slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08003130{
Shaohua Lif2076e72015-10-08 21:54:12 -07003131 if (test_bit(Journal, &rdev->flags))
3132 return sprintf(page, "journal\n");
3133 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08003134 return sprintf(page, "none\n");
3135 else
3136 return sprintf(page, "%d\n", rdev->raid_disk);
3137}
3138
3139static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003140slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08003141{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003142 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08003143 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003144
Shaohua Lif2076e72015-10-08 21:54:12 -07003145 if (test_bit(Journal, &rdev->flags))
3146 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08003147 if (strncmp(buf, "none", 4)==0)
3148 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003149 else {
3150 err = kstrtouint(buf, 10, (unsigned int *)&slot);
3151 if (err < 0)
3152 return err;
3153 }
Neil Brown6c2fce22008-06-28 08:31:31 +10003154 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08003155 /* Setting 'slot' on an active array requires also
3156 * updating the 'rd%d' link, and communicating
3157 * with the personality with ->hot_*_disk.
3158 * For now we only support removing
3159 * failed/spare devices. This normally happens automatically,
3160 * but not when the metadata is externally managed.
3161 */
NeilBrownc303da62008-02-06 01:39:51 -08003162 if (rdev->raid_disk == -1)
3163 return -EEXIST;
3164 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10003165 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08003166 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10003167 clear_bit(Blocked, &rdev->flags);
3168 remove_and_add_spares(rdev->mddev, rdev);
3169 if (rdev->raid_disk >= 0)
3170 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08003171 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3172 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10003173 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10003174 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10003175 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10003176 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11003177 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10003178
3179 if (rdev->raid_disk != -1)
3180 return -EBUSY;
3181
NeilBrownc6751b22011-02-02 11:57:13 +11003182 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
3183 return -EBUSY;
3184
Neil Brown6c2fce22008-06-28 08:31:31 +10003185 if (rdev->mddev->pers->hot_add_disk == NULL)
3186 return -EINVAL;
3187
NeilBrownba1b41b2011-01-14 09:14:34 +11003188 if (slot >= rdev->mddev->raid_disks &&
3189 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
3190 return -ENOSPC;
3191
Neil Brown6c2fce22008-06-28 08:31:31 +10003192 rdev->raid_disk = slot;
3193 if (test_bit(In_sync, &rdev->flags))
3194 rdev->saved_raid_disk = slot;
3195 else
3196 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11003197 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11003198 clear_bit(Bitmap_sync, &rdev->flags);
Guoqing Jiang3f79cc22020-04-04 23:57:11 +02003199 err = rdev->mddev->pers->hot_add_disk(rdev->mddev, rdev);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11003200 if (err) {
3201 rdev->raid_disk = -1;
3202 return err;
3203 } else
3204 sysfs_notify_dirent_safe(rdev->sysfs_state);
3205 if (sysfs_link_rdev(rdev->mddev, rdev))
3206 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10003207 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08003208 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11003209 if (slot >= rdev->mddev->raid_disks &&
3210 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08003211 return -ENOSPC;
3212 rdev->raid_disk = slot;
3213 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08003214 clear_bit(Faulty, &rdev->flags);
3215 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08003216 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003217 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08003218 }
NeilBrown014236d2006-01-06 00:20:55 -08003219 return len;
3220}
3221
NeilBrown014236d2006-01-06 00:20:55 -08003222static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07003223__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08003224
NeilBrown93c8cad2006-01-06 00:20:56 -08003225static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003226offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08003227{
Andrew Morton6961ece2006-01-06 00:20:59 -08003228 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08003229}
3230
3231static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003232offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08003233{
NeilBrownc6563a82012-05-21 09:27:00 +10003234 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003235 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003236 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10003237 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003238 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003239 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08003240 /* Must set offset before size, so overlap checks
3241 * can be sane */
3242 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08003243 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10003244 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08003245 return len;
3246}
3247
3248static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07003249__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08003250
NeilBrownc6563a82012-05-21 09:27:00 +10003251static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3252{
3253 return sprintf(page, "%llu\n",
3254 (unsigned long long)rdev->new_data_offset);
3255}
3256
3257static ssize_t new_offset_store(struct md_rdev *rdev,
3258 const char *buf, size_t len)
3259{
3260 unsigned long long new_offset;
3261 struct mddev *mddev = rdev->mddev;
3262
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003263 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10003264 return -EINVAL;
3265
NeilBrownf851b602014-12-11 10:02:10 +11003266 if (mddev->sync_thread ||
3267 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10003268 return -EBUSY;
3269 if (new_offset == rdev->data_offset)
3270 /* reset is always permitted */
3271 ;
3272 else if (new_offset > rdev->data_offset) {
3273 /* must not push array size beyond rdev_sectors */
3274 if (new_offset - rdev->data_offset
3275 + mddev->dev_sectors > rdev->sectors)
3276 return -E2BIG;
3277 }
3278 /* Metadata worries about other space details. */
3279
3280 /* decreasing the offset is inconsistent with a backwards
3281 * reshape.
3282 */
3283 if (new_offset < rdev->data_offset &&
3284 mddev->reshape_backwards)
3285 return -EINVAL;
3286 /* Increasing offset is inconsistent with forwards
3287 * reshape. reshape_direction should be set to
3288 * 'backwards' first.
3289 */
3290 if (new_offset > rdev->data_offset &&
3291 !mddev->reshape_backwards)
3292 return -EINVAL;
3293
3294 if (mddev->pers && mddev->persistent &&
3295 !super_types[mddev->major_version]
3296 .allow_new_offset(rdev, new_offset))
3297 return -E2BIG;
3298 rdev->new_data_offset = new_offset;
3299 if (new_offset > rdev->data_offset)
3300 mddev->reshape_backwards = 1;
3301 else if (new_offset < rdev->data_offset)
3302 mddev->reshape_backwards = 0;
3303
3304 return len;
3305}
3306static struct rdev_sysfs_entry rdev_new_offset =
3307__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3308
NeilBrown83303b62006-01-06 00:21:06 -08003309static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003310rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003311{
Andre Nolldd8ac332009-03-31 14:33:13 +11003312 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003313}
3314
NeilBrownc5d79ad2008-02-06 01:39:54 -08003315static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3316{
3317 /* check if two start/length pairs overlap */
3318 if (s1+l1 <= s2)
3319 return 0;
3320 if (s2+l2 <= s1)
3321 return 0;
3322 return 1;
3323}
3324
Dan Williamsb522adc2009-03-31 15:00:31 +11003325static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3326{
3327 unsigned long long blocks;
3328 sector_t new;
3329
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003330 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003331 return -EINVAL;
3332
3333 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3334 return -EINVAL; /* sector conversion overflow */
3335
3336 new = blocks * 2;
3337 if (new != blocks * 2)
3338 return -EINVAL; /* unsigned long long to sector_t overflow */
3339
3340 *sectors = new;
3341 return 0;
3342}
3343
NeilBrown83303b62006-01-06 00:21:06 -08003344static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003345rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003346{
NeilBrownfd01b882011-10-11 16:47:53 +11003347 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003348 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003349 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003350
Shaohua Lif2076e72015-10-08 21:54:12 -07003351 if (test_bit(Journal, &rdev->flags))
3352 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003353 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003354 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003355 if (rdev->data_offset != rdev->new_data_offset)
3356 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003357 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003358 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003359 sectors = super_types[my_mddev->major_version].
3360 rdev_size_change(rdev, sectors);
3361 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003362 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003363 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003364 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003365 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003366 if (!my_mddev->pers->resize)
3367 /* Cannot change size for RAID0 or Linear etc */
3368 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003369 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003370 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003371 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003372
Andre Nolldd8ac332009-03-31 14:33:13 +11003373 rdev->sectors = sectors;
3374 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003375 /* Need to check that all other rdevs with the same
3376 * ->bdev do not overlap. 'rcu' is sufficient to walk
3377 * the rdev lists safely.
3378 * This check does not provide a hard guarantee, it
3379 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003380 */
NeilBrownfd01b882011-10-11 16:47:53 +11003381 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003382 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003383 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003384
NeilBrown8b1afc32014-09-29 15:33:20 +10003385 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003386 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003387 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003388
NeilBrowndafb20f2012-03-19 12:46:39 +11003389 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003390 if (rdev->bdev == rdev2->bdev &&
3391 rdev != rdev2 &&
3392 overlaps(rdev->data_offset, rdev->sectors,
3393 rdev2->data_offset,
3394 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003395 overlap = 1;
3396 break;
3397 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003398 if (overlap) {
3399 mddev_put(mddev);
3400 break;
3401 }
3402 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003403 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003404 if (overlap) {
3405 /* Someone else could have slipped in a size
3406 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003407 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003408 * safe, and trust userspace not to race with
3409 * itself
3410 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003411 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003412 return -EBUSY;
3413 }
3414 }
NeilBrown83303b62006-01-06 00:21:06 -08003415 return len;
3416}
3417
3418static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003419__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003420
NeilBrown3cb03002011-10-11 16:45:26 +11003421static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003422{
3423 unsigned long long recovery_start = rdev->recovery_offset;
3424
3425 if (test_bit(In_sync, &rdev->flags) ||
3426 recovery_start == MaxSector)
3427 return sprintf(page, "none\n");
3428
3429 return sprintf(page, "%llu\n", recovery_start);
3430}
3431
NeilBrown3cb03002011-10-11 16:45:26 +11003432static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003433{
3434 unsigned long long recovery_start;
3435
3436 if (cmd_match(buf, "none"))
3437 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003438 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003439 return -EINVAL;
3440
3441 if (rdev->mddev->pers &&
3442 rdev->raid_disk >= 0)
3443 return -EBUSY;
3444
3445 rdev->recovery_offset = recovery_start;
3446 if (recovery_start == MaxSector)
3447 set_bit(In_sync, &rdev->flags);
3448 else
3449 clear_bit(In_sync, &rdev->flags);
3450 return len;
3451}
3452
3453static struct rdev_sysfs_entry rdev_recovery_start =
3454__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3455
Vishal Vermafc974ee2015-12-24 19:20:34 -07003456/* sysfs access to bad-blocks list.
3457 * We present two files.
3458 * 'bad-blocks' lists sector numbers and lengths of ranges that
3459 * are recorded as bad. The list is truncated to fit within
3460 * the one-page limit of sysfs.
3461 * Writing "sector length" to this file adds an acknowledged
3462 * bad block list.
3463 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3464 * been acknowledged. Writing to this file adds bad blocks
3465 * without acknowledging them. This is largely for testing.
3466 */
NeilBrown3cb03002011-10-11 16:45:26 +11003467static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003468{
3469 return badblocks_show(&rdev->badblocks, page, 0);
3470}
NeilBrown3cb03002011-10-11 16:45:26 +11003471static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003472{
NeilBrownde393cd2011-07-28 11:31:48 +10003473 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3474 /* Maybe that ack was all we needed */
3475 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3476 wake_up(&rdev->blocked_wait);
3477 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003478}
3479static struct rdev_sysfs_entry rdev_bad_blocks =
3480__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3481
NeilBrown3cb03002011-10-11 16:45:26 +11003482static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003483{
3484 return badblocks_show(&rdev->badblocks, page, 1);
3485}
NeilBrown3cb03002011-10-11 16:45:26 +11003486static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003487{
3488 return badblocks_store(&rdev->badblocks, page, len, 1);
3489}
3490static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3491__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3492
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003493static ssize_t
3494ppl_sector_show(struct md_rdev *rdev, char *page)
3495{
3496 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3497}
3498
3499static ssize_t
3500ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3501{
3502 unsigned long long sector;
3503
3504 if (kstrtoull(buf, 10, &sector) < 0)
3505 return -EINVAL;
3506 if (sector != (sector_t)sector)
3507 return -EINVAL;
3508
3509 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3510 rdev->raid_disk >= 0)
3511 return -EBUSY;
3512
3513 if (rdev->mddev->persistent) {
3514 if (rdev->mddev->major_version == 0)
3515 return -EINVAL;
3516 if ((sector > rdev->sb_start &&
3517 sector - rdev->sb_start > S16_MAX) ||
3518 (sector < rdev->sb_start &&
3519 rdev->sb_start - sector > -S16_MIN))
3520 return -EINVAL;
3521 rdev->ppl.offset = sector - rdev->sb_start;
3522 } else if (!rdev->mddev->external) {
3523 return -EBUSY;
3524 }
3525 rdev->ppl.sector = sector;
3526 return len;
3527}
3528
3529static struct rdev_sysfs_entry rdev_ppl_sector =
3530__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3531
3532static ssize_t
3533ppl_size_show(struct md_rdev *rdev, char *page)
3534{
3535 return sprintf(page, "%u\n", rdev->ppl.size);
3536}
3537
3538static ssize_t
3539ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3540{
3541 unsigned int size;
3542
3543 if (kstrtouint(buf, 10, &size) < 0)
3544 return -EINVAL;
3545
3546 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3547 rdev->raid_disk >= 0)
3548 return -EBUSY;
3549
3550 if (rdev->mddev->persistent) {
3551 if (rdev->mddev->major_version == 0)
3552 return -EINVAL;
3553 if (size > U16_MAX)
3554 return -EINVAL;
3555 } else if (!rdev->mddev->external) {
3556 return -EBUSY;
3557 }
3558 rdev->ppl.size = size;
3559 return len;
3560}
3561
3562static struct rdev_sysfs_entry rdev_ppl_size =
3563__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3564
NeilBrown86e6ffd2005-11-08 21:39:24 -08003565static struct attribute *rdev_default_attrs[] = {
3566 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003567 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003568 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003569 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003570 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003571 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003572 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003573 &rdev_bad_blocks.attr,
3574 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003575 &rdev_ppl_sector.attr,
3576 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003577 NULL,
3578};
3579static ssize_t
3580rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3581{
3582 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003583 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003584
3585 if (!entry->show)
3586 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003587 if (!rdev->mddev)
Marcos Paulo de Souza168b3052019-06-14 15:41:06 -07003588 return -ENODEV;
NeilBrown758bfc82014-12-15 12:56:59 +11003589 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003590}
3591
3592static ssize_t
3593rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3594 const char *page, size_t length)
3595{
3596 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003597 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003598 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003599 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003600
3601 if (!entry->store)
3602 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003603 if (!capable(CAP_SYS_ADMIN))
3604 return -EACCES;
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003605 rv = mddev ? mddev_lock(mddev) : -ENODEV;
NeilBrownca388052008-02-06 01:39:55 -08003606 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003607 if (rdev->mddev == NULL)
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003608 rv = -ENODEV;
NeilBrown27c529b2008-03-04 14:29:33 -08003609 else
3610 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003611 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003612 }
3613 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003614}
3615
3616static void rdev_free(struct kobject *ko)
3617{
NeilBrown3cb03002011-10-11 16:45:26 +11003618 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003619 kfree(rdev);
3620}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003621static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003622 .show = rdev_attr_show,
3623 .store = rdev_attr_store,
3624};
3625static struct kobj_type rdev_ktype = {
3626 .release = rdev_free,
3627 .sysfs_ops = &rdev_sysfs_ops,
3628 .default_attrs = rdev_default_attrs,
3629};
3630
NeilBrown3cb03002011-10-11 16:45:26 +11003631int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003632{
3633 rdev->desc_nr = -1;
3634 rdev->saved_raid_disk = -1;
3635 rdev->raid_disk = -1;
3636 rdev->flags = 0;
3637 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003638 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003639 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003640 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003641 rdev->sb_loaded = 0;
3642 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003643 atomic_set(&rdev->nr_pending, 0);
3644 atomic_set(&rdev->read_errors, 0);
3645 atomic_set(&rdev->corrected_errors, 0);
3646
3647 INIT_LIST_HEAD(&rdev->same_set);
3648 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003649
3650 /* Add space to store bad block list.
3651 * This reserves the space even on arrays where it cannot
3652 * be used - I wonder if that matters
3653 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003654 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003655}
3656EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657/*
3658 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3659 *
3660 * mark the device faulty if:
3661 *
3662 * - the device is nonexistent (zero size)
3663 * - the device has no valid superblock
3664 *
3665 * a faulty rdev _never_ has rdev->sb set.
3666 */
NeilBrown3cb03002011-10-11 16:45:26 +11003667static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668{
3669 char b[BDEVNAME_SIZE];
3670 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003671 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 sector_t size;
3673
NeilBrown9ffae0c2006-01-06 00:20:32 -08003674 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003675 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
NeilBrown2230dfe2011-07-28 11:31:46 +10003678 err = md_rdev_init(rdev);
3679 if (err)
3680 goto abort_free;
3681 err = alloc_disk_sb(rdev);
3682 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 goto abort_free;
3684
NeilBrownc5d79ad2008-02-06 01:39:54 -08003685 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 if (err)
3687 goto abort_free;
3688
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003689 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003690
Mike Snitzer77304d22010-11-08 14:39:12 +01003691 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003693 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 bdevname(rdev->bdev,b));
3695 err = -EINVAL;
3696 goto abort_free;
3697 }
3698
3699 if (super_format >= 0) {
3700 err = super_types[super_format].
3701 load_super(rdev, NULL, super_minor);
3702 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003703 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003704 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003705 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 goto abort_free;
3707 }
3708 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003709 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 bdevname(rdev->bdev,b));
3711 goto abort_free;
3712 }
3713 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 return rdev;
3716
3717abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003718 if (rdev->bdev)
3719 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003720 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 kfree(rdev);
3722 return ERR_PTR(err);
3723}
3724
3725/*
3726 * Check a full RAID array for plausibility
3727 */
3728
Yufen Yu6a5cb532019-10-16 16:00:03 +08003729static int analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
3731 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003732 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 char b[BDEVNAME_SIZE];
3734
3735 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003736 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 switch (super_types[mddev->major_version].
3738 load_super(rdev, freshest, mddev->minor_version)) {
3739 case 1:
3740 freshest = rdev;
3741 break;
3742 case 0:
3743 break;
3744 default:
NeilBrown9d487392016-11-02 14:16:49 +11003745 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003747 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 }
3749
Yufen Yu6a5cb532019-10-16 16:00:03 +08003750 /* Cannot find a valid fresh disk */
3751 if (!freshest) {
3752 pr_warn("md: cannot find a valid disk\n");
3753 return -EINVAL;
3754 }
3755
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 super_types[mddev->major_version].
3757 validate_super(mddev, freshest);
3758
3759 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003760 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003761 if (mddev->max_disks &&
3762 (rdev->desc_nr >= mddev->max_disks ||
3763 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003764 pr_warn("md: %s: %s: only %d devices permitted\n",
3765 mdname(mddev), bdevname(rdev->bdev, b),
3766 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003767 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003768 continue;
3769 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003770 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 if (super_types[mddev->major_version].
3772 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003773 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003775 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 continue;
3777 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 if (mddev->level == LEVEL_MULTIPATH) {
3780 rdev->desc_nr = i++;
3781 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003782 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003783 } else if (rdev->raid_disk >=
3784 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3785 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003786 rdev->raid_disk = -1;
3787 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 }
3789 }
Yufen Yu6a5cb532019-10-16 16:00:03 +08003790
3791 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792}
3793
NeilBrown72e02072009-12-14 12:49:55 +11003794/* Read a fixed-point number.
3795 * Numbers in sysfs attributes should be in "standard" units where
3796 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003797 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003798 * milliseconds or jiffies.
3799 * This function takes a decimal number with a possible fractional
3800 * component, and produces an integer which is the result of
3801 * multiplying that number by 10^'scale'.
3802 * all without any floating-point arithmetic.
3803 */
3804int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3805{
3806 unsigned long result = 0;
3807 long decimals = -1;
3808 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3809 if (*cp == '.')
3810 decimals = 0;
3811 else if (decimals < scale) {
3812 unsigned int value;
3813 value = *cp - '0';
3814 result = result * 10 + value;
3815 if (decimals >= 0)
3816 decimals++;
3817 }
3818 cp++;
3819 }
3820 if (*cp == '\n')
3821 cp++;
3822 if (*cp)
3823 return -EINVAL;
3824 if (decimals < 0)
3825 decimals = 0;
Andy Shevchenkocf891602019-07-23 23:41:55 +03003826 *res = result * int_pow(10, scale - decimals);
NeilBrown72e02072009-12-14 12:49:55 +11003827 return 0;
3828}
3829
NeilBrowneae17012005-11-08 21:39:23 -08003830static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003831safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003832{
3833 int msec = (mddev->safemode_delay*1000)/HZ;
3834 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3835}
3836static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003837safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003838{
NeilBrown16f17b32006-06-26 00:27:37 -07003839 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003840
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003841 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003842 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003843 return -EINVAL;
3844 }
3845
NeilBrown72e02072009-12-14 12:49:55 +11003846 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003847 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003848 if (msec == 0)
3849 mddev->safemode_delay = 0;
3850 else {
NeilBrown19052c02008-08-05 15:54:13 +10003851 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003852 unsigned long new_delay = (msec*HZ)/1000;
3853
3854 if (new_delay == 0)
3855 new_delay = 1;
3856 mddev->safemode_delay = new_delay;
3857 if (new_delay < old_delay || old_delay == 0)
3858 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003859 }
3860 return len;
3861}
3862static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003863__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003864
3865static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003866level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003867{
NeilBrown36d091f2014-12-15 12:56:58 +11003868 struct md_personality *p;
3869 int ret;
3870 spin_lock(&mddev->lock);
3871 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003872 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003873 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003874 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003875 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003876 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003877 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003878 else
NeilBrown36d091f2014-12-15 12:56:58 +11003879 ret = 0;
3880 spin_unlock(&mddev->lock);
3881 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003882}
3883
NeilBrownd9d166c2006-01-06 00:20:51 -08003884static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003885level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003886{
Dan Williamsf2859af2010-05-02 10:04:16 -07003887 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003888 ssize_t rv;
3889 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003890 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003891 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003892 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003893 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003894
NeilBrown67918752014-12-15 12:57:01 +11003895 if (slen == 0 || slen >= sizeof(clevel))
3896 return -EINVAL;
3897
3898 rv = mddev_lock(mddev);
3899 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003900 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003901
3902 if (mddev->pers == NULL) {
3903 strncpy(mddev->clevel, buf, slen);
3904 if (mddev->clevel[slen-1] == '\n')
3905 slen--;
3906 mddev->clevel[slen] = 0;
3907 mddev->level = LEVEL_NONE;
3908 rv = len;
3909 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003910 }
NeilBrown67918752014-12-15 12:57:01 +11003911 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003912 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003913 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003914
3915 /* request to change the personality. Need to ensure:
3916 * - array is not engaged in resync/recovery/reshape
3917 * - old personality can be suspended
3918 * - new personality will access other array.
3919 */
3920
NeilBrown67918752014-12-15 12:57:01 +11003921 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003922 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003923 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003924 mddev->reshape_position != MaxSector ||
3925 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003926 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003927
NeilBrown67918752014-12-15 12:57:01 +11003928 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003929 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003930 pr_warn("md: %s: %s does not support online personality change\n",
3931 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003932 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003933 }
3934
3935 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003936 strncpy(clevel, buf, slen);
3937 if (clevel[slen-1] == '\n')
3938 slen--;
3939 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003940 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003941 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003942
Dan Williamsf2859af2010-05-02 10:04:16 -07003943 if (request_module("md-%s", clevel) != 0)
3944 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003945 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003946 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003947 if (!pers || !try_module_get(pers->owner)) {
3948 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003949 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003950 rv = -EINVAL;
3951 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003952 }
3953 spin_unlock(&pers_lock);
3954
3955 if (pers == mddev->pers) {
3956 /* Nothing to do! */
3957 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003958 rv = len;
3959 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003960 }
3961 if (!pers->takeover) {
3962 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003963 pr_warn("md: %s: %s does not support personality takeover\n",
3964 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003965 rv = -EINVAL;
3966 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003967 }
3968
NeilBrowndafb20f2012-03-19 12:46:39 +11003969 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003970 rdev->new_raid_disk = rdev->raid_disk;
3971
NeilBrown245f46c2009-03-31 14:39:39 +11003972 /* ->takeover must set new_* and/or delta_disks
3973 * if it succeeds, and may set them when it fails.
3974 */
3975 priv = pers->takeover(mddev);
3976 if (IS_ERR(priv)) {
3977 mddev->new_level = mddev->level;
3978 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003979 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003980 mddev->raid_disks -= mddev->delta_disks;
3981 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003982 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003983 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003984 pr_warn("md: %s: %s would not accept array\n",
3985 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003986 rv = PTR_ERR(priv);
3987 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003988 }
3989
3990 /* Looks like we have a winner */
3991 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003992 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003993
3994 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003995 oldpers = mddev->pers;
3996 oldpriv = mddev->private;
3997 mddev->pers = pers;
3998 mddev->private = priv;
3999 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4000 mddev->level = mddev->new_level;
4001 mddev->layout = mddev->new_layout;
4002 mddev->chunk_sectors = mddev->new_chunk_sectors;
4003 mddev->delta_disks = 0;
4004 mddev->reshape_backwards = 0;
4005 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11004006 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10004007
NeilBrowndb721d32014-12-15 12:56:58 +11004008 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11004009 mddev->external) {
4010 /* We are converting from a no-redundancy array
4011 * to a redundancy array and metadata is managed
4012 * externally so we need to be sure that writes
4013 * won't block due to a need to transition
4014 * clean->dirty
4015 * until external management is started.
4016 */
4017 mddev->in_sync = 0;
4018 mddev->safemode_delay = 0;
4019 mddev->safemode = 0;
4020 }
4021
NeilBrowndb721d32014-12-15 12:56:58 +11004022 oldpers->free(mddev, oldpriv);
4023
4024 if (oldpers->sync_request == NULL &&
4025 pers->sync_request != NULL) {
4026 /* need to add the md_redundancy_group */
4027 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11004028 pr_warn("md: cannot register extra attributes for %s\n",
4029 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11004030 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
4031 }
4032 if (oldpers->sync_request != NULL &&
4033 pers->sync_request == NULL) {
4034 /* need to remove the md_redundancy_group */
4035 if (mddev->to_remove == NULL)
4036 mddev->to_remove = &md_redundancy_group;
4037 }
4038
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02004039 module_put(oldpers->owner);
4040
NeilBrowndafb20f2012-03-19 12:46:39 +11004041 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004042 if (rdev->raid_disk < 0)
4043 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11004044 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01004045 rdev->new_raid_disk = -1;
4046 if (rdev->new_raid_disk == rdev->raid_disk)
4047 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10004048 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01004049 }
NeilBrowndafb20f2012-03-19 12:46:39 +11004050 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004051 if (rdev->raid_disk < 0)
4052 continue;
4053 if (rdev->new_raid_disk == rdev->raid_disk)
4054 continue;
4055 rdev->raid_disk = rdev->new_raid_disk;
4056 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10004057 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01004058 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10004059 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11004060 pr_warn("md: cannot register rd%d for %s after level change\n",
4061 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10004062 }
NeilBrowne93f68a2010-06-15 09:36:03 +01004063 }
4064
NeilBrowndb721d32014-12-15 12:56:58 +11004065 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11004066 /* this is now an array without redundancy, so
4067 * it must always be in_sync
4068 */
4069 mddev->in_sync = 1;
4070 del_timer_sync(&mddev->safemode_timer);
4071 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11004072 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11004073 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004074 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10004075 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11004076 if (!mddev->thread)
4077 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10004078 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07004079 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004080 rv = len;
4081out_unlock:
4082 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08004083 return rv;
4084}
4085
4086static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07004087__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08004088
NeilBrownd4dbd022006-06-26 00:27:59 -07004089static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004090layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07004091{
4092 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07004093 if (mddev->reshape_position != MaxSector &&
4094 mddev->layout != mddev->new_layout)
4095 return sprintf(page, "%d (%d)\n",
4096 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07004097 return sprintf(page, "%d\n", mddev->layout);
4098}
4099
4100static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004101layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07004102{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004103 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11004104 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07004105
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004106 err = kstrtouint(buf, 10, &n);
4107 if (err < 0)
4108 return err;
NeilBrown67918752014-12-15 12:57:01 +11004109 err = mddev_lock(mddev);
4110 if (err)
4111 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07004112
NeilBrownb3546032009-03-31 14:56:41 +11004113 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10004114 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004115 err = -EBUSY;
4116 else if (mddev->ro)
4117 err = -EROFS;
4118 else {
4119 mddev->new_layout = n;
4120 err = mddev->pers->check_reshape(mddev);
4121 if (err)
4122 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10004123 }
NeilBrownb3546032009-03-31 14:56:41 +11004124 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07004125 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11004126 if (mddev->reshape_position == MaxSector)
4127 mddev->layout = n;
4128 }
NeilBrown67918752014-12-15 12:57:01 +11004129 mddev_unlock(mddev);
4130 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07004131}
4132static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07004133__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07004134
NeilBrowneae17012005-11-08 21:39:23 -08004135static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004136raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08004137{
NeilBrownbb636542005-11-08 21:39:45 -08004138 if (mddev->raid_disks == 0)
4139 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004140 if (mddev->reshape_position != MaxSector &&
4141 mddev->delta_disks != 0)
4142 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
4143 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08004144 return sprintf(page, "%d\n", mddev->raid_disks);
4145}
4146
NeilBrownfd01b882011-10-11 16:47:53 +11004147static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08004148
4149static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004150raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08004151{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004152 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11004153 int err;
NeilBrownda943b992006-01-06 00:20:54 -08004154
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004155 err = kstrtouint(buf, 10, &n);
4156 if (err < 0)
4157 return err;
NeilBrownda943b992006-01-06 00:20:54 -08004158
NeilBrown67918752014-12-15 12:57:01 +11004159 err = mddev_lock(mddev);
4160 if (err)
4161 return err;
NeilBrownda943b992006-01-06 00:20:54 -08004162 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11004163 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07004164 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10004165 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004166 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10004167
NeilBrown67918752014-12-15 12:57:01 +11004168 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10004169 rdev_for_each(rdev, mddev) {
4170 if (olddisks < n &&
4171 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11004172 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10004173 if (olddisks > n &&
4174 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11004175 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10004176 }
NeilBrown67918752014-12-15 12:57:01 +11004177 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004178 mddev->delta_disks = n - olddisks;
4179 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10004180 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07004181 } else
NeilBrownda943b992006-01-06 00:20:54 -08004182 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11004183out_unlock:
4184 mddev_unlock(mddev);
4185 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08004186}
4187static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07004188__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08004189
NeilBrown24dd4692005-11-08 21:39:26 -08004190static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004191chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08004192{
NeilBrown08a02ec2007-05-09 02:35:38 -07004193 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10004194 mddev->chunk_sectors != mddev->new_chunk_sectors)
4195 return sprintf(page, "%d (%d)\n",
4196 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10004197 mddev->chunk_sectors << 9);
4198 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08004199}
4200
4201static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004202chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08004203{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004204 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11004205 int err;
NeilBrown3b343802006-01-06 00:20:47 -08004206
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004207 err = kstrtoul(buf, 10, &n);
4208 if (err < 0)
4209 return err;
NeilBrown3b343802006-01-06 00:20:47 -08004210
NeilBrown67918752014-12-15 12:57:01 +11004211 err = mddev_lock(mddev);
4212 if (err)
4213 return err;
NeilBrownb3546032009-03-31 14:56:41 +11004214 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10004215 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004216 err = -EBUSY;
4217 else if (mddev->ro)
4218 err = -EROFS;
4219 else {
4220 mddev->new_chunk_sectors = n >> 9;
4221 err = mddev->pers->check_reshape(mddev);
4222 if (err)
4223 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10004224 }
NeilBrownb3546032009-03-31 14:56:41 +11004225 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10004226 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004227 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10004228 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004229 }
NeilBrown67918752014-12-15 12:57:01 +11004230 mddev_unlock(mddev);
4231 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08004232}
4233static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004234__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08004235
NeilBrowna94213b2006-06-26 00:28:00 -07004236static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004237resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07004238{
NeilBrownd1a7c502009-03-31 15:24:32 +11004239 if (mddev->recovery_cp == MaxSector)
4240 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07004241 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4242}
4243
4244static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004245resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07004246{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004247 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11004248 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004249
4250 if (cmd_match(buf, "none"))
4251 n = MaxSector;
4252 else {
4253 err = kstrtoull(buf, 10, &n);
4254 if (err < 0)
4255 return err;
4256 if (n != (sector_t)n)
4257 return -EINVAL;
4258 }
NeilBrowna94213b2006-06-26 00:28:00 -07004259
NeilBrown67918752014-12-15 12:57:01 +11004260 err = mddev_lock(mddev);
4261 if (err)
4262 return err;
NeilBrownb0986362011-05-11 15:52:21 +10004263 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11004264 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07004265
NeilBrown67918752014-12-15 12:57:01 +11004266 if (!err) {
4267 mddev->recovery_cp = n;
4268 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08004269 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11004270 }
4271 mddev_unlock(mddev);
4272 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07004273}
4274static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10004275__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4276 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07004277
NeilBrown9e653b62006-06-26 00:27:58 -07004278/*
4279 * The array state can be:
4280 *
4281 * clear
4282 * No devices, no size, no level
4283 * Equivalent to STOP_ARRAY ioctl
4284 * inactive
4285 * May have some settings, but array is not active
4286 * all IO results in error
4287 * When written, doesn't tear down array, but just stops it
4288 * suspended (not supported yet)
4289 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01004290 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07004291 * readonly
4292 * no resync can happen. no superblocks get written.
4293 * write requests fail
4294 * read-auto
4295 * like readonly, but behaves like 'clean' on a write request.
4296 *
4297 * clean - no pending writes, but otherwise active.
4298 * When written to inactive array, starts without resync
4299 * If a write request arrives then
4300 * if metadata is known, mark 'dirty' and switch to 'active'.
4301 * if not known, block and switch to write-pending
4302 * If written to an active array that has pending writes, then fails.
4303 * active
4304 * fully active: IO and resync can be happening.
4305 * When written to inactive array, starts with resync
4306 *
4307 * write-pending
4308 * clean, but writes are blocked waiting for 'active' to be written.
4309 *
4310 * active-idle
4311 * like active, but no writes have been seen for a while (100msec).
4312 *
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004313 * broken
4314 * RAID0/LINEAR-only: same as clean, but array is missing a member.
4315 * It's useful because RAID0/LINEAR mounted-arrays aren't stopped
4316 * when a member is gone, so this state will at least alert the
4317 * user that something is wrong.
NeilBrown9e653b62006-06-26 00:27:58 -07004318 */
4319enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004320 write_pending, active_idle, broken, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004321static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004322 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004323 "write-pending", "active-idle", "broken", NULL };
NeilBrown9e653b62006-06-26 00:27:58 -07004324
4325static int match_word(const char *word, char **list)
4326{
4327 int n;
4328 for (n=0; list[n]; n++)
4329 if (cmd_match(word, list[n]))
4330 break;
4331 return n;
4332}
4333
4334static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004335array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004336{
4337 enum array_state st = inactive;
4338
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004339 if (mddev->pers && !test_bit(MD_NOT_READY, &mddev->flags)) {
NeilBrown9e653b62006-06-26 00:27:58 -07004340 switch(mddev->ro) {
4341 case 1:
4342 st = readonly;
4343 break;
4344 case 2:
4345 st = read_auto;
4346 break;
4347 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004348 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004349 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004350 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004351 else if (mddev->in_sync)
4352 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004353 else if (mddev->safemode)
4354 st = active_idle;
4355 else
4356 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004357 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004358 }
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004359
4360 if (test_bit(MD_BROKEN, &mddev->flags) && st == clean)
4361 st = broken;
4362 } else {
NeilBrown9e653b62006-06-26 00:27:58 -07004363 if (list_empty(&mddev->disks) &&
4364 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004365 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004366 st = clear;
4367 else
4368 st = inactive;
4369 }
4370 return sprintf(page, "%s\n", array_states[st]);
4371}
4372
NeilBrownf72ffdd2014-09-30 14:23:59 +10004373static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4374static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4375static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004376static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004377
4378static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004379array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004380{
NeilBrown6497709b2017-03-15 14:05:14 +11004381 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004382 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004383
4384 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4385 /* don't take reconfig_mutex when toggling between
4386 * clean and active
4387 */
4388 spin_lock(&mddev->lock);
4389 if (st == active) {
4390 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004391 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004392 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004393 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004394 } else /* st == clean */ {
4395 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004396 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004397 err = -EBUSY;
4398 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004399 if (!err)
4400 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004401 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004402 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004403 }
4404 err = mddev_lock(mddev);
4405 if (err)
4406 return err;
4407 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004408 switch(st) {
4409 case bad_word:
4410 break;
4411 case clear:
4412 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004413 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004414 break;
4415 case inactive:
4416 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004417 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004418 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004419 else
NeilBrowne6910632008-02-06 01:39:51 -08004420 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004421 break;
4422 case suspended:
4423 break; /* not supported yet */
4424 case readonly:
4425 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004426 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004427 else {
4428 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004429 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004430 err = do_md_run(mddev);
4431 }
4432 break;
4433 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004434 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004435 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004436 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004437 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004438 err = restart_array(mddev);
4439 if (err == 0) {
4440 mddev->ro = 2;
4441 set_disk_ro(mddev->gendisk, 0);
4442 }
NeilBrown9e653b62006-06-26 00:27:58 -07004443 } else {
4444 mddev->ro = 2;
4445 err = do_md_run(mddev);
4446 }
4447 break;
4448 case clean:
4449 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004450 err = restart_array(mddev);
4451 if (err)
4452 break;
NeilBrown85572d72014-12-15 12:56:56 +11004453 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004454 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004455 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004456 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004457 } else
4458 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004459 break;
4460 case active:
4461 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004462 err = restart_array(mddev);
4463 if (err)
4464 break;
Shaohua Li29530792016-12-08 15:48:19 -08004465 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004466 wake_up(&mddev->sb_wait);
4467 err = 0;
4468 } else {
4469 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004470 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004471 err = do_md_run(mddev);
4472 }
4473 break;
4474 case write_pending:
4475 case active_idle:
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004476 case broken:
NeilBrown9e653b62006-06-26 00:27:58 -07004477 /* these cannot be set */
4478 break;
4479 }
NeilBrown67918752014-12-15 12:57:01 +11004480
4481 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004482 if (mddev->hold_active == UNTIL_IOCTL)
4483 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004484 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004485 }
NeilBrown67918752014-12-15 12:57:01 +11004486 mddev_unlock(mddev);
4487 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004488}
NeilBrown80ca3a42006-07-10 04:44:18 -07004489static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004490__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004491
NeilBrown6d7ff7382006-01-06 00:21:16 -08004492static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004493max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004494 return sprintf(page, "%d\n",
4495 atomic_read(&mddev->max_corr_read_errors));
4496}
4497
4498static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004499max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004500{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004501 unsigned int n;
4502 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004503
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004504 rv = kstrtouint(buf, 10, &n);
4505 if (rv < 0)
4506 return rv;
4507 atomic_set(&mddev->max_corr_read_errors, n);
4508 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004509}
4510
4511static struct md_sysfs_entry max_corr_read_errors =
4512__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4513 max_corrected_read_errors_store);
4514
4515static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004516null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004517{
4518 return -EINVAL;
4519}
4520
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004521/* need to ensure rdev_delayed_delete() has completed */
4522static void flush_rdev_wq(struct mddev *mddev)
4523{
4524 struct md_rdev *rdev;
4525
4526 rcu_read_lock();
4527 rdev_for_each_rcu(rdev, mddev)
4528 if (work_pending(&rdev->del_work)) {
4529 flush_workqueue(md_rdev_misc_wq);
4530 break;
4531 }
4532 rcu_read_unlock();
4533}
4534
NeilBrown6d7ff7382006-01-06 00:21:16 -08004535static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004536new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004537{
4538 /* buf must be %d:%d\n? giving major and minor numbers */
4539 /* The new device is added to the array.
4540 * If the array has a persistent superblock, we read the
4541 * superblock to initialise info and check validity.
4542 * Otherwise, only checking done is that in bind_rdev_to_array,
4543 * which mainly checks size.
4544 */
4545 char *e;
4546 int major = simple_strtoul(buf, &e, 10);
4547 int minor;
4548 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004549 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004550 int err;
4551
4552 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4553 return -EINVAL;
4554 minor = simple_strtoul(e+1, &e, 10);
4555 if (*e && *e != '\n')
4556 return -EINVAL;
4557 dev = MKDEV(major, minor);
4558 if (major != MAJOR(dev) ||
4559 minor != MINOR(dev))
4560 return -EOVERFLOW;
4561
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004562 flush_rdev_wq(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004563 err = mddev_lock(mddev);
4564 if (err)
4565 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004566 if (mddev->persistent) {
4567 rdev = md_import_device(dev, mddev->major_version,
4568 mddev->minor_version);
4569 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004570 struct md_rdev *rdev0
4571 = list_entry(mddev->disks.next,
4572 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004573 err = super_types[mddev->major_version]
4574 .load_super(rdev, rdev0, mddev->minor_version);
4575 if (err < 0)
4576 goto out;
4577 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004578 } else if (mddev->external)
4579 rdev = md_import_device(dev, -2, -1);
4580 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004581 rdev = md_import_device(dev, -1, -1);
4582
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004583 if (IS_ERR(rdev)) {
4584 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004585 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004586 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004587 err = bind_rdev_to_array(rdev, mddev);
4588 out:
4589 if (err)
4590 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004591 mddev_unlock(mddev);
Alexey Obitotskiy5492c462017-07-28 15:49:25 +02004592 if (!err)
4593 md_new_event(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004594 return err ? err : len;
4595}
4596
4597static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004598__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004599
4600static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004601bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004602{
4603 char *end;
4604 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004605 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004606
NeilBrown67918752014-12-15 12:57:01 +11004607 err = mddev_lock(mddev);
4608 if (err)
4609 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004610 if (!mddev->bitmap)
4611 goto out;
4612 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4613 while (*buf) {
4614 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4615 if (buf == end) break;
4616 if (*end == '-') { /* range */
4617 buf = end + 1;
4618 end_chunk = simple_strtoul(buf, &end, 0);
4619 if (buf == end) break;
4620 }
4621 if (*end && !isspace(*end)) break;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004622 md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004623 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004624 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004625 md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
Paul Clements9b1d1da2006-10-03 01:15:49 -07004626out:
NeilBrown67918752014-12-15 12:57:01 +11004627 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004628 return len;
4629}
4630
4631static struct md_sysfs_entry md_bitmap =
4632__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4633
4634static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004635size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004636{
Andre Noll58c0fed2009-03-31 14:33:13 +11004637 return sprintf(page, "%llu\n",
4638 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004639}
4640
NeilBrownfd01b882011-10-11 16:47:53 +11004641static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004642
4643static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004644size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004645{
4646 /* If array is inactive, we can reduce the component size, but
4647 * not increase it (except from 0).
4648 * If array is active, we can try an on-line resize
4649 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004650 sector_t sectors;
4651 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004652
Andre Noll58c0fed2009-03-31 14:33:13 +11004653 if (err < 0)
4654 return err;
NeilBrown67918752014-12-15 12:57:01 +11004655 err = mddev_lock(mddev);
4656 if (err)
4657 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004658 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004659 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004660 if (err == 0)
4661 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004662 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004663 if (mddev->dev_sectors == 0 ||
4664 mddev->dev_sectors > sectors)
4665 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004666 else
4667 err = -ENOSPC;
4668 }
NeilBrown67918752014-12-15 12:57:01 +11004669 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004670 return err ? err : len;
4671}
4672
4673static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004674__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004675
Masanari Iida83f0d772012-10-30 00:18:08 +09004676/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004677 * This is one of
4678 * 'none' for arrays with no metadata (good luck...)
4679 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004680 * or N.M for internally known formats
4681 */
4682static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004683metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004684{
4685 if (mddev->persistent)
4686 return sprintf(page, "%d.%d\n",
4687 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004688 else if (mddev->external)
4689 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004690 else
4691 return sprintf(page, "none\n");
4692}
4693
4694static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004695metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004696{
4697 int major, minor;
4698 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004699 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004700 /* Changing the details of 'external' metadata is
4701 * always permitted. Otherwise there must be
4702 * no devices attached to the array.
4703 */
NeilBrown67918752014-12-15 12:57:01 +11004704
4705 err = mddev_lock(mddev);
4706 if (err)
4707 return err;
4708 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004709 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4710 ;
4711 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004712 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004713
NeilBrown67918752014-12-15 12:57:01 +11004714 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004715 if (cmd_match(buf, "none")) {
4716 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004717 mddev->external = 0;
4718 mddev->major_version = 0;
4719 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004720 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004721 }
4722 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004723 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004724 if (namelen >= sizeof(mddev->metadata_type))
4725 namelen = sizeof(mddev->metadata_type)-1;
4726 strncpy(mddev->metadata_type, buf+9, namelen);
4727 mddev->metadata_type[namelen] = 0;
4728 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4729 mddev->metadata_type[--namelen] = 0;
4730 mddev->persistent = 0;
4731 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004732 mddev->major_version = 0;
4733 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004734 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004735 }
4736 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004737 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004738 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004739 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004740 buf = e+1;
4741 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004742 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004743 goto out_unlock;
4744 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004745 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004746 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004747 mddev->major_version = major;
4748 mddev->minor_version = minor;
4749 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004750 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004751 err = 0;
4752out_unlock:
4753 mddev_unlock(mddev);
4754 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004755}
4756
4757static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004758__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004759
NeilBrowna35b0d62006-01-06 00:20:49 -08004760static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004761action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004762{
NeilBrown7eec3142005-11-08 21:39:44 -08004763 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004764 unsigned long recovery = mddev->recovery;
4765 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004766 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004767 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4768 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4769 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004770 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004771 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4772 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004773 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004774 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004775 type = "check";
4776 else
4777 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004778 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004779 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004780 else if (mddev->reshape_position != MaxSector)
4781 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004782 }
4783 return sprintf(page, "%s\n", type);
4784}
4785
4786static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004787action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004788{
NeilBrown7eec3142005-11-08 21:39:44 -08004789 if (!mddev->pers || !mddev->pers->sync_request)
4790 return -EINVAL;
4791
NeilBrownb6a9ce62009-05-26 09:41:17 +10004792
4793 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004794 if (cmd_match(page, "frozen"))
4795 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4796 else
4797 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004798 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4799 mddev_lock(mddev) == 0) {
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004800 if (work_pending(&mddev->del_work))
4801 flush_workqueue(md_misc_wq);
NeilBrown8e8e2512015-06-12 19:51:27 +10004802 if (mddev->sync_thread) {
4803 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004804 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004805 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004806 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004807 }
NeilBrown312045e2015-12-21 11:01:21 +11004808 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004809 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004810 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004811 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004812 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004813 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004814 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004815 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004816 int err;
4817 if (mddev->pers->start_reshape == NULL)
4818 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004819 err = mddev_lock(mddev);
4820 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004821 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4822 err = -EBUSY;
4823 else {
4824 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4825 err = mddev->pers->start_reshape(mddev);
4826 }
NeilBrown67918752014-12-15 12:57:01 +11004827 mddev_unlock(mddev);
4828 }
NeilBrown16484bf2006-03-27 01:18:13 -08004829 if (err)
4830 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004831 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004832 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004833 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004834 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004835 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004836 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004837 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004838 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4839 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004840 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004841 if (mddev->ro == 2) {
4842 /* A write to sync_action is enough to justify
4843 * canceling read-auto mode
4844 */
4845 mddev->ro = 0;
4846 md_wakeup_thread(mddev->sync_thread);
4847 }
NeilBrown03c902e2006-01-06 00:20:46 -08004848 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004849 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004850 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004851 return len;
4852}
4853
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004854static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004855__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004856
4857static ssize_t
4858last_sync_action_show(struct mddev *mddev, char *page)
4859{
4860 return sprintf(page, "%s\n", mddev->last_sync_action);
4861}
4862
4863static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4864
NeilBrown9d888832005-11-08 21:39:26 -08004865static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004866mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004867{
4868 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004869 (unsigned long long)
4870 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004871}
4872
NeilBrown80ca3a42006-07-10 04:44:18 -07004873static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004874
NeilBrown88202a02006-01-06 00:21:36 -08004875static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004876sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004877{
4878 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4879 mddev->sync_speed_min ? "local": "system");
4880}
4881
4882static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004883sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004884{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004885 unsigned int min;
4886 int rv;
4887
NeilBrown88202a02006-01-06 00:21:36 -08004888 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004889 min = 0;
4890 } else {
4891 rv = kstrtouint(buf, 10, &min);
4892 if (rv < 0)
4893 return rv;
4894 if (min == 0)
4895 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004896 }
NeilBrown88202a02006-01-06 00:21:36 -08004897 mddev->sync_speed_min = min;
4898 return len;
4899}
4900
4901static struct md_sysfs_entry md_sync_min =
4902__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4903
4904static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004905sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004906{
4907 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4908 mddev->sync_speed_max ? "local": "system");
4909}
4910
4911static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004912sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004913{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004914 unsigned int max;
4915 int rv;
4916
NeilBrown88202a02006-01-06 00:21:36 -08004917 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004918 max = 0;
4919 } else {
4920 rv = kstrtouint(buf, 10, &max);
4921 if (rv < 0)
4922 return rv;
4923 if (max == 0)
4924 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004925 }
NeilBrown88202a02006-01-06 00:21:36 -08004926 mddev->sync_speed_max = max;
4927 return len;
4928}
4929
4930static struct md_sysfs_entry md_sync_max =
4931__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4932
Iustin Popd7f3d292007-10-16 23:30:54 -07004933static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004934degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004935{
4936 return sprintf(page, "%d\n", mddev->degraded);
4937}
4938static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004939
4940static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004941sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004942{
4943 return sprintf(page, "%d\n", mddev->parallel_resync);
4944}
4945
4946static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004947sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004948{
4949 long n;
4950
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004951 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004952 return -EINVAL;
4953
4954 if (n != 0 && n != 1)
4955 return -EINVAL;
4956
4957 mddev->parallel_resync = n;
4958
4959 if (mddev->sync_thread)
4960 wake_up(&resync_wait);
4961
4962 return len;
4963}
4964
4965/* force parallel resync, even with shared block devices */
4966static struct md_sysfs_entry md_sync_force_parallel =
4967__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4968 sync_force_parallel_show, sync_force_parallel_store);
4969
4970static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004971sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004972{
4973 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004974 if (mddev->curr_resync == 0)
4975 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004976 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4977 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004978 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004979 db = resync - mddev->resync_mark_cnt;
4980 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004981}
4982
NeilBrown80ca3a42006-07-10 04:44:18 -07004983static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004984
4985static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004986sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004987{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004988 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004989
NeilBrownacb180b2009-04-14 16:28:34 +10004990 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4991 return sprintf(page, "none\n");
4992
NeilBrown72f36d52012-10-11 14:25:57 +11004993 if (mddev->curr_resync == 1 ||
4994 mddev->curr_resync == 2)
4995 return sprintf(page, "delayed\n");
4996
NeilBrownc804cde2012-05-21 09:28:33 +10004997 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4998 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004999 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08005000 else
Andre Noll58c0fed2009-03-31 14:33:13 +11005001 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08005002
NeilBrownacb180b2009-04-14 16:28:34 +10005003 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11005004 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08005005}
5006
NeilBrown750f1992014-09-30 08:53:05 +10005007static struct md_sysfs_entry md_sync_completed =
5008 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08005009
NeilBrowne464eaf2006-03-27 01:18:14 -08005010static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005011min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10005012{
5013 return sprintf(page, "%llu\n",
5014 (unsigned long long)mddev->resync_min);
5015}
5016static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005017min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10005018{
5019 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11005020 int err;
NeilBrown23da4222014-12-15 12:57:01 +11005021
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005022 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10005023 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11005024
5025 spin_lock(&mddev->lock);
5026 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10005027 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11005028 goto out_unlock;
5029
5030 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10005031 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11005032 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10005033
NeilBrown50c37b12015-03-23 17:36:38 +11005034 /* Round down to multiple of 4K for safety */
5035 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11005036 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10005037
NeilBrown23da4222014-12-15 12:57:01 +11005038out_unlock:
5039 spin_unlock(&mddev->lock);
5040 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10005041}
5042
5043static struct md_sysfs_entry md_min_sync =
5044__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
5045
5046static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005047max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08005048{
5049 if (mddev->resync_max == MaxSector)
5050 return sprintf(page, "max\n");
5051 else
5052 return sprintf(page, "%llu\n",
5053 (unsigned long long)mddev->resync_max);
5054}
5055static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005056max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08005057{
NeilBrown23da4222014-12-15 12:57:01 +11005058 int err;
5059 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08005060 if (strncmp(buf, "max", 3) == 0)
5061 mddev->resync_max = MaxSector;
5062 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10005063 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11005064 int chunk;
5065
5066 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005067 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11005068 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10005069 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11005070 goto out_unlock;
5071
5072 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08005073 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10005074 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08005075 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11005076 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08005077
5078 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11005079 chunk = mddev->chunk_sectors;
5080 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10005081 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11005082
5083 err = -EINVAL;
5084 if (sector_div(temp, chunk))
5085 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08005086 }
5087 mddev->resync_max = max;
5088 }
5089 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11005090 err = 0;
5091out_unlock:
5092 spin_unlock(&mddev->lock);
5093 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08005094}
5095
5096static struct md_sysfs_entry md_max_sync =
5097__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
5098
5099static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005100suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08005101{
5102 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
5103}
5104
5105static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005106suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08005107{
NeilBrownb03e0cc2017-10-19 12:49:15 +11005108 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005109 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08005110
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005111 err = kstrtoull(buf, 10, &new);
5112 if (err < 0)
5113 return err;
5114 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08005115 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11005116
NeilBrown67918752014-12-15 12:57:01 +11005117 err = mddev_lock(mddev);
5118 if (err)
5119 return err;
5120 err = -EINVAL;
5121 if (mddev->pers == NULL ||
5122 mddev->pers->quiesce == NULL)
5123 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005124 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11005125 mddev->suspend_lo = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005126 mddev_resume(mddev);
5127
NeilBrown67918752014-12-15 12:57:01 +11005128 err = 0;
5129unlock:
5130 mddev_unlock(mddev);
5131 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08005132}
5133static struct md_sysfs_entry md_suspend_lo =
5134__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
5135
NeilBrowne464eaf2006-03-27 01:18:14 -08005136static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005137suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08005138{
5139 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
5140}
5141
5142static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005143suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08005144{
NeilBrownb03e0cc2017-10-19 12:49:15 +11005145 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005146 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08005147
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005148 err = kstrtoull(buf, 10, &new);
5149 if (err < 0)
5150 return err;
5151 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08005152 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11005153
NeilBrown67918752014-12-15 12:57:01 +11005154 err = mddev_lock(mddev);
5155 if (err)
5156 return err;
5157 err = -EINVAL;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005158 if (mddev->pers == NULL)
NeilBrown67918752014-12-15 12:57:01 +11005159 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005160
5161 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11005162 mddev->suspend_hi = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005163 mddev_resume(mddev);
5164
NeilBrown67918752014-12-15 12:57:01 +11005165 err = 0;
5166unlock:
5167 mddev_unlock(mddev);
5168 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08005169}
5170static struct md_sysfs_entry md_suspend_hi =
5171__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
5172
NeilBrown08a02ec2007-05-09 02:35:38 -07005173static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005174reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07005175{
5176 if (mddev->reshape_position != MaxSector)
5177 return sprintf(page, "%llu\n",
5178 (unsigned long long)mddev->reshape_position);
5179 strcpy(page, "none\n");
5180 return 5;
5181}
5182
5183static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005184reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07005185{
NeilBrownc6563a82012-05-21 09:27:00 +10005186 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005187 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005188 int err;
NeilBrown67918752014-12-15 12:57:01 +11005189
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005190 err = kstrtoull(buf, 10, &new);
5191 if (err < 0)
5192 return err;
5193 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07005194 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11005195 err = mddev_lock(mddev);
5196 if (err)
5197 return err;
5198 err = -EBUSY;
5199 if (mddev->pers)
5200 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07005201 mddev->reshape_position = new;
5202 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005203 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07005204 mddev->new_level = mddev->level;
5205 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10005206 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10005207 rdev_for_each(rdev, mddev)
5208 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11005209 err = 0;
5210unlock:
5211 mddev_unlock(mddev);
5212 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07005213}
5214
5215static struct md_sysfs_entry md_reshape_position =
5216__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
5217 reshape_position_store);
5218
Dan Williamsb522adc2009-03-31 15:00:31 +11005219static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10005220reshape_direction_show(struct mddev *mddev, char *page)
5221{
5222 return sprintf(page, "%s\n",
5223 mddev->reshape_backwards ? "backwards" : "forwards");
5224}
5225
5226static ssize_t
5227reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
5228{
5229 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11005230 int err;
5231
NeilBrown2c810cd2012-05-21 09:27:00 +10005232 if (cmd_match(buf, "forwards"))
5233 backwards = 0;
5234 else if (cmd_match(buf, "backwards"))
5235 backwards = 1;
5236 else
5237 return -EINVAL;
5238 if (mddev->reshape_backwards == backwards)
5239 return len;
5240
NeilBrown67918752014-12-15 12:57:01 +11005241 err = mddev_lock(mddev);
5242 if (err)
5243 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10005244 /* check if we are allowed to change */
5245 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11005246 err = -EBUSY;
5247 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10005248 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11005249 err = -EINVAL;
5250 else
5251 mddev->reshape_backwards = backwards;
5252 mddev_unlock(mddev);
5253 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10005254}
5255
5256static struct md_sysfs_entry md_reshape_direction =
5257__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5258 reshape_direction_store);
5259
5260static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005261array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11005262{
5263 if (mddev->external_size)
5264 return sprintf(page, "%llu\n",
5265 (unsigned long long)mddev->array_sectors/2);
5266 else
5267 return sprintf(page, "default\n");
5268}
5269
5270static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005271array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11005272{
5273 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11005274 int err;
5275
5276 err = mddev_lock(mddev);
5277 if (err)
5278 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11005279
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005280 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08005281 if (mddev_is_clustered(mddev)) {
5282 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005283 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08005284 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005285
Dan Williamsb522adc2009-03-31 15:00:31 +11005286 if (strncmp(buf, "default", 7) == 0) {
5287 if (mddev->pers)
5288 sectors = mddev->pers->size(mddev, 0, 0);
5289 else
5290 sectors = mddev->array_sectors;
5291
5292 mddev->external_size = 0;
5293 } else {
5294 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11005295 err = -EINVAL;
5296 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5297 err = -E2BIG;
5298 else
5299 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11005300 }
5301
NeilBrown67918752014-12-15 12:57:01 +11005302 if (!err) {
5303 mddev->array_sectors = sectors;
5304 if (mddev->pers) {
5305 set_capacity(mddev->gendisk, mddev->array_sectors);
5306 revalidate_disk(mddev->gendisk);
5307 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11005308 }
NeilBrown67918752014-12-15 12:57:01 +11005309 mddev_unlock(mddev);
5310 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11005311}
5312
5313static struct md_sysfs_entry md_array_size =
5314__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5315 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005316
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005317static ssize_t
5318consistency_policy_show(struct mddev *mddev, char *page)
5319{
5320 int ret;
5321
5322 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5323 ret = sprintf(page, "journal\n");
5324 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5325 ret = sprintf(page, "ppl\n");
5326 } else if (mddev->bitmap) {
5327 ret = sprintf(page, "bitmap\n");
5328 } else if (mddev->pers) {
5329 if (mddev->pers->sync_request)
5330 ret = sprintf(page, "resync\n");
5331 else
5332 ret = sprintf(page, "none\n");
5333 } else {
5334 ret = sprintf(page, "unknown\n");
5335 }
5336
5337 return ret;
5338}
5339
5340static ssize_t
5341consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5342{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005343 int err = 0;
5344
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005345 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005346 if (mddev->pers->change_consistency_policy)
5347 err = mddev->pers->change_consistency_policy(mddev, buf);
5348 else
5349 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005350 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5351 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005352 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005353 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005354 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005355
5356 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005357}
5358
5359static struct md_sysfs_entry md_consistency_policy =
5360__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5361 consistency_policy_store);
5362
Guoqing Jiang9a567842019-07-24 11:09:19 +02005363static ssize_t fail_last_dev_show(struct mddev *mddev, char *page)
5364{
5365 return sprintf(page, "%d\n", mddev->fail_last_dev);
5366}
5367
5368/*
5369 * Setting fail_last_dev to true to allow last device to be forcibly removed
5370 * from RAID1/RAID10.
5371 */
5372static ssize_t
5373fail_last_dev_store(struct mddev *mddev, const char *buf, size_t len)
5374{
5375 int ret;
5376 bool value;
5377
5378 ret = kstrtobool(buf, &value);
5379 if (ret)
5380 return ret;
5381
5382 if (value != mddev->fail_last_dev)
5383 mddev->fail_last_dev = value;
5384
5385 return len;
5386}
5387static struct md_sysfs_entry md_fail_last_dev =
5388__ATTR(fail_last_dev, S_IRUGO | S_IWUSR, fail_last_dev_show,
5389 fail_last_dev_store);
5390
Guoqing Jiang3938f5f2019-12-23 10:48:56 +01005391static ssize_t serialize_policy_show(struct mddev *mddev, char *page)
5392{
5393 if (mddev->pers == NULL || (mddev->pers->level != 1))
5394 return sprintf(page, "n/a\n");
5395 else
5396 return sprintf(page, "%d\n", mddev->serialize_policy);
5397}
5398
5399/*
5400 * Setting serialize_policy to true to enforce write IO is not reordered
5401 * for raid1.
5402 */
5403static ssize_t
5404serialize_policy_store(struct mddev *mddev, const char *buf, size_t len)
5405{
5406 int err;
5407 bool value;
5408
5409 err = kstrtobool(buf, &value);
5410 if (err)
5411 return err;
5412
5413 if (value == mddev->serialize_policy)
5414 return len;
5415
5416 err = mddev_lock(mddev);
5417 if (err)
5418 return err;
5419 if (mddev->pers == NULL || (mddev->pers->level != 1)) {
5420 pr_err("md: serialize_policy is only effective for raid1\n");
5421 err = -EINVAL;
5422 goto unlock;
5423 }
5424
5425 mddev_suspend(mddev);
5426 if (value)
5427 mddev_create_serial_pool(mddev, NULL, true);
5428 else
5429 mddev_destroy_serial_pool(mddev, NULL, true);
5430 mddev->serialize_policy = value;
5431 mddev_resume(mddev);
5432unlock:
5433 mddev_unlock(mddev);
5434 return err ?: len;
5435}
5436
5437static struct md_sysfs_entry md_serialize_policy =
5438__ATTR(serialize_policy, S_IRUGO | S_IWUSR, serialize_policy_show,
5439 serialize_policy_store);
5440
5441
NeilBrowneae17012005-11-08 21:39:23 -08005442static struct attribute *md_default_attrs[] = {
5443 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005444 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005445 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005446 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005447 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005448 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005449 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005450 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005451 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005452 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005453 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005454 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005455 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005456 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005457 &md_consistency_policy.attr,
Guoqing Jiang9a567842019-07-24 11:09:19 +02005458 &md_fail_last_dev.attr,
Guoqing Jiang3938f5f2019-12-23 10:48:56 +01005459 &md_serialize_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005460 NULL,
5461};
5462
5463static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005464 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005465 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005466 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005467 &md_sync_min.attr,
5468 &md_sync_max.attr,
5469 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005470 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005471 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005472 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005473 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005474 &md_suspend_lo.attr,
5475 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005476 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005477 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005478 NULL,
5479};
NeilBrown411036f2005-11-08 21:39:40 -08005480static struct attribute_group md_redundancy_group = {
5481 .name = NULL,
5482 .attrs = md_redundancy_attrs,
5483};
5484
NeilBrowneae17012005-11-08 21:39:23 -08005485static ssize_t
5486md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5487{
5488 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005489 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005490 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005491
5492 if (!entry->show)
5493 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005494 spin_lock(&all_mddevs_lock);
5495 if (list_empty(&mddev->all_mddevs)) {
5496 spin_unlock(&all_mddevs_lock);
5497 return -EBUSY;
5498 }
5499 mddev_get(mddev);
5500 spin_unlock(&all_mddevs_lock);
5501
NeilBrownb7b17c92014-12-15 12:56:59 +11005502 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005503 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005504 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005505}
5506
5507static ssize_t
5508md_attr_store(struct kobject *kobj, struct attribute *attr,
5509 const char *page, size_t length)
5510{
5511 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005512 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005513 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005514
5515 if (!entry->store)
5516 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005517 if (!capable(CAP_SYS_ADMIN))
5518 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005519 spin_lock(&all_mddevs_lock);
5520 if (list_empty(&mddev->all_mddevs)) {
5521 spin_unlock(&all_mddevs_lock);
5522 return -EBUSY;
5523 }
5524 mddev_get(mddev);
5525 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005526 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005527 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005528 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005529}
5530
5531static void md_free(struct kobject *ko)
5532{
NeilBrownfd01b882011-10-11 16:47:53 +11005533 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005534
5535 if (mddev->sysfs_state)
5536 sysfs_put(mddev->sysfs_state);
5537
Bart Van Assched8115c352018-02-28 10:15:29 -08005538 if (mddev->gendisk)
5539 del_gendisk(mddev->gendisk);
NeilBrown6cd18e72015-04-27 14:12:22 +10005540 if (mddev->queue)
5541 blk_cleanup_queue(mddev->queue);
Bart Van Assched8115c352018-02-28 10:15:29 -08005542 if (mddev->gendisk)
NeilBrowna21d1502009-01-09 08:31:09 +11005543 put_disk(mddev->gendisk);
NeilBrown4ad23a972017-03-15 14:05:14 +11005544 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005545
Kent Overstreet28dec872018-06-07 20:52:54 -04005546 bioset_exit(&mddev->bio_set);
5547 bioset_exit(&mddev->sync_set);
NeilBrowneae17012005-11-08 21:39:23 -08005548 kfree(mddev);
5549}
5550
Emese Revfy52cf25d2010-01-19 02:58:23 +01005551static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005552 .show = md_attr_show,
5553 .store = md_attr_store,
5554};
5555static struct kobj_type md_ktype = {
5556 .release = md_free,
5557 .sysfs_ops = &md_sysfs_ops,
5558 .default_attrs = md_default_attrs,
5559};
5560
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561int mdp_major = 0;
5562
Dan Williams5fd3a172009-03-04 00:57:25 -07005563static void mddev_delayed_delete(struct work_struct *ws)
5564{
NeilBrownfd01b882011-10-11 16:47:53 +11005565 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005566
NeilBrown43a70502009-12-14 12:49:55 +11005567 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005568 kobject_del(&mddev->kobj);
5569 kobject_put(&mddev->kobj);
5570}
5571
NeilBrown4ad23a972017-03-15 14:05:14 +11005572static void no_op(struct percpu_ref *r) {}
5573
NeilBrowna415c0f2017-06-05 16:05:13 +10005574int mddev_init_writes_pending(struct mddev *mddev)
5575{
5576 if (mddev->writes_pending.percpu_count_ptr)
5577 return 0;
Roman Gushchinddde2af2019-05-07 10:01:49 -07005578 if (percpu_ref_init(&mddev->writes_pending, no_op,
5579 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL) < 0)
NeilBrowna415c0f2017-06-05 16:05:13 +10005580 return -ENOMEM;
5581 /* We want to start with the refcount at zero */
5582 percpu_ref_put(&mddev->writes_pending);
5583 return 0;
5584}
5585EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5586
NeilBrownefeb53c2009-01-09 08:31:10 +11005587static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588{
NeilBrown039b7222017-04-12 16:26:13 +10005589 /*
5590 * If dev is zero, name is the name of a device to allocate with
5591 * an arbitrary minor number. It will be "md_???"
5592 * If dev is non-zero it must be a device number with a MAJOR of
5593 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5594 * the device is being created by opening a node in /dev.
5595 * If "name" is not NULL, the device is being created by
5596 * writing to /sys/module/md_mod/parameters/new_array.
5597 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005598 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005599 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005601 int partitioned;
5602 int shift;
5603 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005604 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605
5606 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005607 return -ENODEV;
5608
5609 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5610 shift = partitioned ? MdpMinorShift : 0;
5611 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612
Tejun Heoe804ac72010-10-15 15:36:08 +02005613 /* wait for any previous instance of this device to be
5614 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005615 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005616 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005617
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005618 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005619 error = -EEXIST;
5620 if (mddev->gendisk)
5621 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005622
NeilBrown039b7222017-04-12 16:26:13 +10005623 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005624 /* Need to ensure that 'name' is not a duplicate.
5625 */
NeilBrownfd01b882011-10-11 16:47:53 +11005626 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005627 spin_lock(&all_mddevs_lock);
5628
5629 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5630 if (mddev2->gendisk &&
5631 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5632 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005633 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005634 }
5635 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 }
NeilBrown039b7222017-04-12 16:26:13 +10005637 if (name && dev)
5638 /*
5639 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5640 */
5641 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005642
NeilBrown0909dc42009-07-01 12:27:21 +10005643 error = -ENOMEM;
Christoph Hellwig3d745ea2020-03-27 09:30:11 +01005644 mddev->queue = blk_alloc_queue(md_make_request, NUMA_NO_NODE);
NeilBrown0909dc42009-07-01 12:27:21 +10005645 if (!mddev->queue)
5646 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005647
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005648 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005649
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 disk = alloc_disk(1 << shift);
5651 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005652 blk_cleanup_queue(mddev->queue);
5653 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005654 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005656 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005658 if (name)
5659 strcpy(disk->disk_name, name);
5660 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005662 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 disk->fops = &md_fops;
5665 disk->private_data = mddev;
5666 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005667 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005668 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005669 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005670 * remove it now.
5671 */
5672 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005674 /* As soon as we call add_disk(), another thread could get
5675 * through to md_open, so make sure it doesn't get too far
5676 */
5677 mutex_lock(&mddev->open_mutex);
5678 add_disk(disk);
5679
Kent Overstreet28dec872018-06-07 20:52:54 -04005680 error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005681 if (error) {
5682 /* This isn't possible, but as kobject_init_and_add is marked
5683 * __must_check, we must do something with the result
5684 */
NeilBrown9d487392016-11-02 14:16:49 +11005685 pr_debug("md: cannot register %s/md - name in use\n",
5686 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005687 error = 0;
5688 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005689 if (mddev->kobj.sd &&
5690 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005691 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005692 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005693 abort:
5694 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005695 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005696 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005697 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005698 }
NeilBrownd3374822009-01-09 08:31:10 +11005699 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005700 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005701}
5702
5703static struct kobject *md_probe(dev_t dev, int *part, void *data)
5704{
NeilBrown78b63502017-04-12 16:26:13 +10005705 if (create_on_open)
5706 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 return NULL;
5708}
5709
Kees Cooke4dca7b2017-10-17 19:04:42 -07005710static int add_named_array(const char *val, const struct kernel_param *kp)
NeilBrownefeb53c2009-01-09 08:31:10 +11005711{
NeilBrown039b7222017-04-12 16:26:13 +10005712 /*
5713 * val must be "md_*" or "mdNNN".
5714 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005715 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005716 * For "mdNNN" we allocate an array with the minor number NNN
5717 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005718 */
5719 int len = strlen(val);
5720 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005721 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005722
5723 while (len && val[len-1] == '\n')
5724 len--;
5725 if (len >= DISK_NAME_LEN)
5726 return -E2BIG;
5727 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005728 if (strncmp(buf, "md_", 3) == 0)
5729 return md_alloc(0, buf);
5730 if (strncmp(buf, "md", 2) == 0 &&
5731 isdigit(buf[2]) &&
5732 kstrtoul(buf+2, 10, &devnum) == 0 &&
5733 devnum <= MINORMASK)
5734 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5735
5736 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005737}
5738
Kees Cook8376d3c2017-10-16 17:01:48 -07005739static void md_safemode_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740{
Kees Cook8376d3c2017-10-16 17:01:48 -07005741 struct mddev *mddev = from_timer(mddev, t, safemode_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742
NeilBrown4ad23a972017-03-15 14:05:14 +11005743 mddev->safemode = 1;
5744 if (mddev->external)
5745 sysfs_notify_dirent_safe(mddev->sysfs_state);
5746
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 md_wakeup_thread(mddev->thread);
5748}
5749
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005750static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751
NeilBrownfd01b882011-10-11 16:47:53 +11005752int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753{
NeilBrown2604b702006-01-06 00:20:36 -08005754 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005755 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005756 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
NeilBrowna757e642005-04-16 15:26:42 -07005758 if (list_empty(&mddev->disks))
5759 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761
5762 if (mddev->pers)
5763 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005764 /* Cannot run until previous stop completes properly */
5765 if (mddev->sysfs_active)
5766 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005767
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 /*
5769 * Analyze all RAID superblock(s)
5770 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005771 if (!mddev->raid_disks) {
5772 if (!mddev->persistent)
5773 return -EINVAL;
Yufen Yu6a5cb532019-10-16 16:00:03 +08005774 err = analyze_sbs(mddev);
5775 if (err)
5776 return -EINVAL;
NeilBrown1ec4a932008-02-06 01:39:53 -08005777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778
NeilBrownd9d166c2006-01-06 00:20:51 -08005779 if (mddev->level != LEVEL_NONE)
5780 request_module("md-level-%d", mddev->level);
5781 else if (mddev->clevel[0])
5782 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783
5784 /*
5785 * Drop all container device buffers, from now on
5786 * the only valid external interface is through the md
5787 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 */
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005789 mddev->has_superblocks = false;
NeilBrowndafb20f2012-03-19 12:46:39 +11005790 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005791 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 continue;
5793 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005794 invalidate_bdev(rdev->bdev);
NeilBrown97b20ef2017-04-13 08:53:48 +10005795 if (mddev->ro != 1 &&
5796 (bdev_read_only(rdev->bdev) ||
5797 bdev_read_only(rdev->meta_bdev))) {
5798 mddev->ro = 1;
5799 if (mddev->gendisk)
5800 set_disk_ro(mddev->gendisk, 1);
5801 }
NeilBrownf0d76d72007-07-17 04:06:12 -07005802
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005803 if (rdev->sb_page)
5804 mddev->has_superblocks = true;
5805
NeilBrownf0d76d72007-07-17 04:06:12 -07005806 /* perform some consistency tests on the device.
5807 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005808 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005809 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005810 if (rdev->meta_bdev) {
5811 /* Nothing to check */;
5812 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005813 if (mddev->dev_sectors &&
5814 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005815 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005816 pr_warn("md: %s: data overlaps metadata\n",
5817 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005818 return -EINVAL;
5819 }
5820 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005821 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005822 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005823 pr_warn("md: %s: metadata overlaps data\n",
5824 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005825 return -EINVAL;
5826 }
5827 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005828 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 }
5830
Kent Overstreetafeee512018-05-20 18:25:52 -04005831 if (!bioset_initialized(&mddev->bio_set)) {
5832 err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5833 if (err)
5834 return err;
Ming Lei10273172017-02-14 23:29:00 +08005835 }
Kent Overstreetafeee512018-05-20 18:25:52 -04005836 if (!bioset_initialized(&mddev->sync_set)) {
5837 err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5838 if (err)
Kent Overstreet28dec872018-06-07 20:52:54 -04005839 return err;
NeilBrown5a850712017-06-21 09:12:21 +10005840 }
NeilBrowna167f662010-10-26 18:31:13 +11005841
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005843 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005844 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005846 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005847 pr_warn("md: personality for level %d is not loaded!\n",
5848 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005849 else
NeilBrown9d487392016-11-02 14:16:49 +11005850 pr_warn("md: personality for level %s is not loaded!\n",
5851 mddev->clevel);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005852 err = -EINVAL;
5853 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005856 if (mddev->level != pers->level) {
5857 mddev->level = pers->level;
5858 mddev->new_level = pers->level;
5859 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005860 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861
NeilBrownf6705572006-03-27 01:18:11 -08005862 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005863 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005864 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005865 module_put(pers->owner);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005866 err = -EINVAL;
5867 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08005868 }
5869
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005870 if (pers->sync_request) {
5871 /* Warn if this is a potentially silly
5872 * configuration.
5873 */
5874 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005875 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005876 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005877
NeilBrowndafb20f2012-03-19 12:46:39 +11005878 rdev_for_each(rdev, mddev)
5879 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005880 if (rdev < rdev2 &&
5881 rdev->bdev->bd_contains ==
5882 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005883 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5884 mdname(mddev),
5885 bdevname(rdev->bdev,b),
5886 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005887 warned = 1;
5888 }
5889 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005890
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005891 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005892 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005893 }
5894
NeilBrown657390d2005-08-26 18:34:16 -07005895 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005896 /* may be over-ridden by personality */
5897 mddev->resync_max_sectors = mddev->dev_sectors;
5898
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005899 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
NeilBrown0f9552b52009-12-30 12:08:50 +11005901 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005902 mddev->ro = 2; /* read-only, but switch on first write */
5903
NeilBrown36d091f2014-12-15 12:56:58 +11005904 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005905 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005906 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005907 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005908 WARN_ONCE(!mddev->external_size,
5909 "%s: default size too small, but 'external_size' not in effect?\n",
5910 __func__);
5911 pr_warn("md: invalid array_size %llu > default size %llu\n",
5912 (unsigned long long)mddev->array_sectors / 2,
5913 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005914 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005915 }
NeilBrown36d091f2014-12-15 12:56:58 +11005916 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005917 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005918 struct bitmap *bitmap;
5919
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005920 bitmap = md_bitmap_create(mddev, -1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005921 if (IS_ERR(bitmap)) {
5922 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005923 pr_warn("%s: failed to create bitmap (%d)\n",
5924 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005925 } else
5926 mddev->bitmap = bitmap;
5927
NeilBrownb15c2e52006-01-06 00:20:16 -08005928 }
Guoqing Jiangd4945492019-06-14 17:10:39 +08005929 if (err)
5930 goto bitmap_abort;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005931
5932 if (mddev->bitmap_info.max_write_behind > 0) {
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01005933 bool create_pool = false;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005934
5935 rdev_for_each(rdev, mddev) {
5936 if (test_bit(WriteMostly, &rdev->flags) &&
Guoqing Jiang404659c2019-12-23 10:48:53 +01005937 rdev_init_serial(rdev))
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01005938 create_pool = true;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005939 }
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01005940 if (create_pool && mddev->serial_info_pool == NULL) {
Guoqing Jiang404659c2019-12-23 10:48:53 +01005941 mddev->serial_info_pool =
5942 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
5943 sizeof(struct serial_info));
5944 if (!mddev->serial_info_pool) {
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005945 err = -ENOMEM;
Guoqing Jiangd4945492019-06-14 17:10:39 +08005946 goto bitmap_abort;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005947 }
5948 }
5949 }
5950
NeilBrown5c675f82014-12-15 12:56:56 +11005951 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005952 bool nonrot = true;
5953
5954 rdev_for_each(rdev, mddev) {
5955 if (rdev->raid_disk >= 0 &&
5956 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5957 nonrot = false;
5958 break;
5959 }
5960 }
5961 if (mddev->degraded)
5962 nonrot = false;
5963 if (nonrot)
Bart Van Assche8b904b52018-03-07 17:10:10 -08005964 blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue);
Shaohua Libb086a82016-09-30 09:45:40 -07005965 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08005966 blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01005967 mddev->queue->backing_dev_info->congested_data = mddev;
5968 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11005969 }
NeilBrown36d091f2014-12-15 12:56:58 +11005970 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005971 if (mddev->kobj.sd &&
5972 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005973 pr_warn("md: cannot register extra attributes for %s\n",
5974 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005975 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005976 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005977 mddev->ro = 0;
5978
Robert Becker1e509152009-12-14 12:49:58 +11005979 atomic_set(&mddev->max_corr_read_errors,
5980 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005982 if (mddev_is_clustered(mddev))
5983 mddev->safemode_delay = 0;
5984 else
5985 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005987 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005988 spin_lock(&mddev->lock);
5989 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005990 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005991 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005992 if (rdev->raid_disk >= 0)
Yufen Yue5b521e2019-06-14 15:41:07 -07005993 sysfs_link_rdev(mddev, rdev); /* failure here is OK */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005994
NeilBrowna4a3d262015-07-17 11:57:30 +10005995 if (mddev->degraded && !mddev->ro)
5996 /* This ensures that recovering status is reported immediately
5997 * via sysfs - until a lack of spares is confirmed.
5998 */
5999 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10006001
Shaohua Li29530792016-12-08 15:48:19 -08006002 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07006003 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004
NeilBrownd7603b72006-01-06 00:20:30 -08006005 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 return 0;
Xiao Nib1261942018-01-24 12:17:38 +08006007
Guoqing Jiangd4945492019-06-14 17:10:39 +08006008bitmap_abort:
6009 mddev_detach(mddev);
6010 if (mddev->private)
6011 pers->free(mddev, mddev->private);
6012 mddev->private = NULL;
6013 module_put(pers->owner);
6014 md_bitmap_destroy(mddev);
Xiao Nib1261942018-01-24 12:17:38 +08006015abort:
NeilBrown4bc034d2019-03-29 10:46:16 -07006016 bioset_exit(&mddev->bio_set);
6017 bioset_exit(&mddev->sync_set);
Xiao Nib1261942018-01-24 12:17:38 +08006018 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019}
NeilBrown390ee602010-06-01 19:37:27 +10006020EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021
NeilBrownfd01b882011-10-11 16:47:53 +11006022static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11006023{
6024 int err;
6025
NeilBrown9d4b45d2019-08-20 10:21:09 +10006026 set_bit(MD_NOT_READY, &mddev->flags);
NeilBrownfe60b012010-03-29 11:10:42 +11006027 err = md_run(mddev);
6028 if (err)
6029 goto out;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006030 err = md_bitmap_load(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006031 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006032 md_bitmap_destroy(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006033 goto out;
6034 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05006035
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006036 if (mddev_is_clustered(mddev))
6037 md_allow_write(mddev);
6038
Song Liud5d885f2017-11-19 22:17:01 -08006039 /* run start up tasks that require md_thread */
6040 md_start(mddev);
6041
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05006042 md_wakeup_thread(mddev->thread);
6043 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
6044
NeilBrownfe60b012010-03-29 11:10:42 +11006045 set_capacity(mddev->gendisk, mddev->array_sectors);
6046 revalidate_disk(mddev->gendisk);
NeilBrown9d4b45d2019-08-20 10:21:09 +10006047 clear_bit(MD_NOT_READY, &mddev->flags);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006048 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11006049 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrown9d4b45d2019-08-20 10:21:09 +10006050 sysfs_notify_dirent_safe(mddev->sysfs_state);
6051 sysfs_notify_dirent_safe(mddev->sysfs_action);
6052 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownfe60b012010-03-29 11:10:42 +11006053out:
NeilBrown9d4b45d2019-08-20 10:21:09 +10006054 clear_bit(MD_NOT_READY, &mddev->flags);
NeilBrownfe60b012010-03-29 11:10:42 +11006055 return err;
6056}
6057
Song Liud5d885f2017-11-19 22:17:01 -08006058int md_start(struct mddev *mddev)
6059{
6060 int ret = 0;
6061
6062 if (mddev->pers->start) {
6063 set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6064 md_wakeup_thread(mddev->thread);
6065 ret = mddev->pers->start(mddev);
6066 clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6067 md_wakeup_thread(mddev->sync_thread);
6068 }
6069 return ret;
6070}
6071EXPORT_SYMBOL_GPL(md_start);
6072
NeilBrownfd01b882011-10-11 16:47:53 +11006073static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074{
6075 struct gendisk *disk = mddev->gendisk;
NeilBrown97b20ef2017-04-13 08:53:48 +10006076 struct md_rdev *rdev;
6077 bool has_journal = false;
6078 bool has_readonly = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
Andre Noll80fab1d2008-07-11 22:02:21 +10006080 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10006082 return -ENXIO;
6083 if (!mddev->pers)
6084 return -EINVAL;
6085 if (!mddev->ro)
6086 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07006087
NeilBrown97b20ef2017-04-13 08:53:48 +10006088 rcu_read_lock();
6089 rdev_for_each_rcu(rdev, mddev) {
6090 if (test_bit(Journal, &rdev->flags) &&
6091 !test_bit(Faulty, &rdev->flags))
6092 has_journal = true;
6093 if (bdev_read_only(rdev->bdev))
6094 has_readonly = true;
Song Liu339421d2015-10-08 21:54:13 -07006095 }
NeilBrown97b20ef2017-04-13 08:53:48 +10006096 rcu_read_unlock();
6097 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
6098 /* Don't restart rw with journal missing/faulty */
6099 return -EINVAL;
6100 if (has_readonly)
6101 return -EROFS;
Song Liu339421d2015-10-08 21:54:13 -07006102
Andre Noll80fab1d2008-07-11 22:02:21 +10006103 mddev->safemode = 0;
6104 mddev->ro = 0;
6105 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11006106 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10006107 /* Kick recovery or resync if necessary */
6108 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6109 md_wakeup_thread(mddev->thread);
6110 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006111 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10006112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113}
6114
NeilBrownfd01b882011-10-11 16:47:53 +11006115static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11006116{
6117 mddev->array_sectors = 0;
6118 mddev->external_size = 0;
6119 mddev->dev_sectors = 0;
6120 mddev->raid_disks = 0;
6121 mddev->recovery_cp = 0;
6122 mddev->resync_min = 0;
6123 mddev->resync_max = MaxSector;
6124 mddev->reshape_position = MaxSector;
6125 mddev->external = 0;
6126 mddev->persistent = 0;
6127 mddev->level = LEVEL_NONE;
6128 mddev->clevel[0] = 0;
6129 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006130 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006131 mddev->ro = 0;
6132 mddev->metadata_type[0] = 0;
6133 mddev->chunk_sectors = 0;
6134 mddev->ctime = mddev->utime = 0;
6135 mddev->layout = 0;
6136 mddev->max_disks = 0;
6137 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10006138 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006139 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006140 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006141 mddev->new_level = LEVEL_NONE;
6142 mddev->new_layout = 0;
6143 mddev->new_chunk_sectors = 0;
6144 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11006145 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11006146 mddev->suspend_lo = mddev->suspend_hi = 0;
6147 mddev->sync_speed_min = mddev->sync_speed_max = 0;
6148 mddev->recovery = 0;
6149 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006150 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006151 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006152 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10006153 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08006154 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11006155 mddev->bitmap_info.offset = 0;
6156 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10006157 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006158 mddev->bitmap_info.chunksize = 0;
6159 mddev->bitmap_info.daemon_sleep = 0;
6160 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08006161 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006162}
6163
NeilBrownfd01b882011-10-11 16:47:53 +11006164static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11006165{
NeilBrown6b6204e2013-05-09 09:48:30 +10006166 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Guoqing Jiang21e09582020-04-04 23:57:07 +02006167 if (work_pending(&mddev->del_work))
6168 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11006169 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11006170 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10006171 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11006172 }
6173
6174 del_timer_sync(&mddev->safemode_timer);
6175
Shaohua Li034e33f2016-11-21 10:29:19 -08006176 if (mddev->pers && mddev->pers->quiesce) {
6177 mddev->pers->quiesce(mddev, 1);
6178 mddev->pers->quiesce(mddev, 0);
6179 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006180 md_bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11006181
NeilBrownb6d428c2013-04-24 11:42:42 +10006182 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006183 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08006184 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11006185 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006186 if (!mddev_is_clustered(mddev))
6187 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11006188 md_update_sb(mddev, 1);
6189 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +01006190 /* disable policy to guarantee rdevs free resources for serialization */
6191 mddev->serialize_policy = 0;
6192 mddev_destroy_serial_pool(mddev, NULL, true);
NeilBrowna047e122010-03-29 12:07:53 +11006193}
NeilBrowndefad612011-01-14 09:14:33 +11006194
NeilBrownfd01b882011-10-11 16:47:53 +11006195void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11006196{
NeilBrown29f097c2013-11-14 17:54:51 +11006197 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11006198 __md_stop_writes(mddev);
6199 mddev_unlock(mddev);
6200}
NeilBrown390ee602010-06-01 19:37:27 +10006201EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11006202
NeilBrown5aa61f42014-12-15 12:56:57 +11006203static void mddev_detach(struct mddev *mddev)
6204{
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006205 md_bitmap_wait_behind_writes(mddev);
Guoqing Jiang6b40bec2020-02-11 11:10:04 +01006206 if (mddev->pers && mddev->pers->quiesce && !mddev->suspended) {
NeilBrown5aa61f42014-12-15 12:56:57 +11006207 mddev->pers->quiesce(mddev, 1);
6208 mddev->pers->quiesce(mddev, 0);
6209 }
6210 md_unregister_thread(&mddev->thread);
6211 if (mddev->queue)
6212 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
6213}
6214
NeilBrown5eff3c42012-11-19 10:47:48 +11006215static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11006216{
NeilBrown36d091f2014-12-15 12:56:58 +11006217 struct md_personality *pers = mddev->pers;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006218 md_bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11006219 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10006220 /* Ensure ->event_work is done */
Guoqing Jiang21e09582020-04-04 23:57:07 +02006221 if (mddev->event_work.func)
6222 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11006223 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11006224 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11006225 spin_unlock(&mddev->lock);
6226 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10006227 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11006228 if (pers->sync_request && mddev->to_remove == NULL)
6229 mddev->to_remove = &md_redundancy_group;
6230 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11006231 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Jack Wang6aaa58c2018-10-19 16:21:31 +02006232}
6233
6234void md_stop(struct mddev *mddev)
6235{
6236 /* stop the array and free an attached data structures.
6237 * This is called from dm-raid
6238 */
6239 __md_stop(mddev);
Kent Overstreetafeee512018-05-20 18:25:52 -04006240 bioset_exit(&mddev->bio_set);
6241 bioset_exit(&mddev->sync_set);
NeilBrown5eff3c42012-11-19 10:47:48 +11006242}
6243
NeilBrown390ee602010-06-01 19:37:27 +10006244EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11006245
NeilBrowna05b7ea2012-07-19 15:59:18 +10006246static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11006247{
6248 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11006249 int did_freeze = 0;
6250
6251 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6252 did_freeze = 1;
6253 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6254 md_wakeup_thread(mddev->thread);
6255 }
NeilBrownf851b602014-12-11 10:02:10 +11006256 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11006257 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006258 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11006259 /* Thread might be blocked waiting for metadata update
6260 * which will now never happen */
6261 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11006262
Shaohua Li29530792016-12-08 15:48:19 -08006263 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10006264 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11006265 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006266 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
6267 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10006268 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08006269 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11006270 mddev_lock_nointr(mddev);
6271
NeilBrowna4bd82d2010-03-29 13:23:10 +11006272 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006273 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006274 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006275 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006276 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11006277 if (did_freeze) {
6278 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006279 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006280 md_wakeup_thread(mddev->thread);
6281 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11006282 err = -EBUSY;
6283 goto out;
6284 }
6285 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11006286 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11006287
6288 err = -ENXIO;
6289 if (mddev->ro==1)
6290 goto out;
6291 mddev->ro = 1;
6292 set_disk_ro(mddev->gendisk, 1);
6293 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006294 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6295 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006296 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11006297 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006298 }
6299out:
6300 mutex_unlock(&mddev->open_mutex);
6301 return err;
6302}
6303
NeilBrown9e653b62006-06-26 00:27:58 -07006304/* mode:
6305 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07006306 * 2 - stop but do not disassemble array
6307 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006308static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10006309 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11006312 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11006313 int did_freeze = 0;
6314
6315 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6316 did_freeze = 1;
6317 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6318 md_wakeup_thread(mddev->thread);
6319 }
NeilBrownf851b602014-12-11 10:02:10 +11006320 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11006321 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006322 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11006323 /* Thread might be blocked waiting for metadata update
6324 * which will now never happen */
6325 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11006326
NeilBrown30b8feb2013-11-14 15:16:17 +11006327 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006328 wait_event(resync_wait, (mddev->sync_thread == NULL &&
6329 !test_bit(MD_RECOVERY_RUNNING,
6330 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11006331 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332
NeilBrownc8c00a62009-08-10 12:50:52 +10006333 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006334 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006335 mddev->sysfs_active ||
6336 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006337 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006338 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10006339 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11006340 if (did_freeze) {
6341 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006342 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006343 md_wakeup_thread(mddev->thread);
6344 }
NeilBrown260fa032013-08-27 16:44:13 +10006345 return -EBUSY;
6346 }
NeilBrown6e17b022010-08-07 21:41:19 +10006347 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11006348 if (mddev->ro)
6349 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11006350
NeilBrowndefad612011-01-14 09:14:33 +11006351 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11006352 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01006353 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11006354
NeilBrowna4bd82d2010-03-29 13:23:10 +11006355 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10006356 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08006357
NeilBrowndafb20f2012-03-19 12:46:39 +11006358 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10006359 if (rdev->raid_disk >= 0)
6360 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10006361
NeilBrowna4bd82d2010-03-29 13:23:10 +11006362 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10006363 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006364 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006365 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08006366
NeilBrowna4bd82d2010-03-29 13:23:10 +11006367 if (mddev->ro)
6368 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10006369 } else
6370 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 /*
6372 * Free resources if final stop
6373 */
NeilBrown9e653b62006-06-26 00:27:58 -07006374 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006375 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376
NeilBrownc3d97142009-12-14 12:49:52 +11006377 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11006378 struct file *f = mddev->bitmap_info.file;
6379 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11006380 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11006381 spin_unlock(&mddev->lock);
6382 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08006383 }
NeilBrownc3d97142009-12-14 12:49:52 +11006384 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08006385
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 export_array(mddev);
6387
NeilBrown6177b472010-03-29 11:37:13 +11006388 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11006389 if (mddev->hold_active == UNTIL_STOP)
6390 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006391 }
NeilBrownd7603b72006-01-06 00:20:30 -08006392 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006393 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10006394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395}
6396
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006397#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11006398static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399{
NeilBrown3cb03002011-10-11 16:45:26 +11006400 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401 int err;
6402
NeilBrowna757e642005-04-16 15:26:42 -07006403 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405
NeilBrown9d487392016-11-02 14:16:49 +11006406 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407
NeilBrowndafb20f2012-03-19 12:46:39 +11006408 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11006410 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 }
NeilBrown9d487392016-11-02 14:16:49 +11006412 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413
NeilBrownd710e132008-10-13 11:55:12 +11006414 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006416 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10006417 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 }
6419}
6420
6421/*
6422 * lets try to run arrays based on all disks that have arrived
6423 * until now. (those are in pending_raid_disks)
6424 *
6425 * the method: pick the first pending disk, collect all disks with
6426 * the same UUID, remove all from the pending list and put them into
6427 * the 'same_array' list. Then order this list based on superblock
6428 * update time (freshest comes first), kick out 'old' disks and
6429 * compare superblocks. If everything's fine then run it.
6430 *
6431 * If "unit" is allocated, then bump its reference count
6432 */
6433static void autorun_devices(int part)
6434{
NeilBrown3cb03002011-10-11 16:45:26 +11006435 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006436 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 char b[BDEVNAME_SIZE];
6438
NeilBrown9d487392016-11-02 14:16:49 +11006439 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07006441 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08006443 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11006445 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446
NeilBrown9d487392016-11-02 14:16:49 +11006447 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006449 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006451 pr_debug("md: adding %s ...\n",
6452 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 list_move(&rdev->same_set, &candidates);
6454 }
6455 /*
6456 * now we have a set of devices, with all of them having
6457 * mostly sane superblocks. It's time to allocate the
6458 * mddev.
6459 */
NeilBrowne8703fe2006-10-03 01:15:59 -07006460 if (part) {
6461 dev = MKDEV(mdp_major,
6462 rdev0->preferred_minor << MdpMinorShift);
6463 unit = MINOR(dev) >> MdpMinorShift;
6464 } else {
6465 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6466 unit = MINOR(dev);
6467 }
6468 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11006469 pr_warn("md: unit number in %s is bad: %d\n",
6470 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 break;
6472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473
6474 md_probe(dev, NULL, NULL);
6475 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006476 if (!mddev || !mddev->gendisk) {
6477 if (mddev)
6478 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479 break;
6480 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006481 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006482 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483 else if (mddev->raid_disks || mddev->major_version
6484 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006485 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 mdname(mddev), bdevname(rdev0->bdev,b));
6487 mddev_unlock(mddev);
6488 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006489 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006490 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006491 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 list_del_init(&rdev->same_set);
6493 if (bind_rdev_to_array(rdev, mddev))
6494 export_rdev(rdev);
6495 }
6496 autorun_array(mddev);
6497 mddev_unlock(mddev);
6498 }
6499 /* on success, candidates will be empty, on error
6500 * it won't...
6501 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006502 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006503 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506 mddev_put(mddev);
6507 }
NeilBrown9d487392016-11-02 14:16:49 +11006508 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006510#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511
NeilBrownf72ffdd2014-09-30 14:23:59 +10006512static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513{
6514 mdu_version_t ver;
6515
6516 ver.major = MD_MAJOR_VERSION;
6517 ver.minor = MD_MINOR_VERSION;
6518 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6519
6520 if (copy_to_user(arg, &ver, sizeof(ver)))
6521 return -EFAULT;
6522
6523 return 0;
6524}
6525
NeilBrownf72ffdd2014-09-30 14:23:59 +10006526static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527{
6528 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006529 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006530 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531
NeilBrown1ca69c42012-10-11 13:37:33 +11006532 nr = working = insync = failed = spare = 0;
6533 rcu_read_lock();
6534 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006536 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 failed++;
6538 else {
6539 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006540 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006541 insync++;
Song Liub347af82016-08-11 17:14:45 -07006542 else if (test_bit(Journal, &rdev->flags))
6543 /* TODO: add journal count to md_u.h */
6544 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545 else
6546 spare++;
6547 }
6548 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006549 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550
6551 info.major_version = mddev->major_version;
6552 info.minor_version = mddev->minor_version;
6553 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006554 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006556 info.size = mddev->dev_sectors / 2;
6557 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006558 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 info.nr_disks = nr;
6560 info.raid_disks = mddev->raid_disks;
6561 info.md_minor = mddev->md_minor;
6562 info.not_persistent= !mddev->persistent;
6563
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006564 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 info.state = 0;
6566 if (mddev->in_sync)
6567 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006568 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006569 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006570 if (mddev_is_clustered(mddev))
6571 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006572 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 info.working_disks = working;
6574 info.failed_disks = failed;
6575 info.spare_disks = spare;
6576
6577 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006578 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579
6580 if (copy_to_user(arg, &info, sizeof(info)))
6581 return -EFAULT;
6582
6583 return 0;
6584}
6585
NeilBrownf72ffdd2014-09-30 14:23:59 +10006586static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006587{
6588 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006589 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006590 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006591
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006592 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006593 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006594 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006595
NeilBrown32a76272005-06-21 17:17:14 -07006596 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006597 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006598 /* bitmap enabled */
6599 if (mddev->bitmap_info.file) {
6600 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6601 sizeof(file->pathname));
6602 if (IS_ERR(ptr))
6603 err = PTR_ERR(ptr);
6604 else
6605 memmove(file->pathname, ptr,
6606 sizeof(file->pathname)-(ptr-file->pathname));
6607 }
NeilBrown4af1a042014-12-15 12:57:00 +11006608 spin_unlock(&mddev->lock);
6609
6610 if (err == 0 &&
6611 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006612 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006613
NeilBrown32a76272005-06-21 17:17:14 -07006614 kfree(file);
6615 return err;
6616}
6617
NeilBrownf72ffdd2014-09-30 14:23:59 +10006618static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619{
6620 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006621 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622
6623 if (copy_from_user(&info, arg, sizeof(info)))
6624 return -EFAULT;
6625
NeilBrown1ca69c42012-10-11 13:37:33 +11006626 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006627 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628 if (rdev) {
6629 info.major = MAJOR(rdev->bdev->bd_dev);
6630 info.minor = MINOR(rdev->bdev->bd_dev);
6631 info.raid_disk = rdev->raid_disk;
6632 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006633 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006635 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 info.state |= (1<<MD_DISK_ACTIVE);
6637 info.state |= (1<<MD_DISK_SYNC);
6638 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006639 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006640 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006641 if (test_bit(WriteMostly, &rdev->flags))
6642 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006643 if (test_bit(FailFast, &rdev->flags))
6644 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645 } else {
6646 info.major = info.minor = 0;
6647 info.raid_disk = -1;
6648 info.state = (1<<MD_DISK_REMOVED);
6649 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006650 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651
6652 if (copy_to_user(arg, &info, sizeof(info)))
6653 return -EFAULT;
6654
6655 return 0;
6656}
6657
NeilBrownf72ffdd2014-09-30 14:23:59 +10006658static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659{
6660 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006661 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 dev_t dev = MKDEV(info->major,info->minor);
6663
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006664 if (mddev_is_clustered(mddev) &&
6665 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006666 pr_warn("%s: Cannot add to clustered mddev.\n",
6667 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006668 return -EINVAL;
6669 }
6670
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6672 return -EOVERFLOW;
6673
6674 if (!mddev->raid_disks) {
6675 int err;
6676 /* expecting a device which has a superblock */
6677 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6678 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006679 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680 PTR_ERR(rdev));
6681 return PTR_ERR(rdev);
6682 }
6683 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006684 struct md_rdev *rdev0
6685 = list_entry(mddev->disks.next,
6686 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006687 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 .load_super(rdev, rdev0, mddev->minor_version);
6689 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006690 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006691 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692 bdevname(rdev0->bdev,b2));
6693 export_rdev(rdev);
6694 return -EINVAL;
6695 }
6696 }
6697 err = bind_rdev_to_array(rdev, mddev);
6698 if (err)
6699 export_rdev(rdev);
6700 return err;
6701 }
6702
6703 /*
6704 * add_new_disk can be used once the array is assembled
6705 * to add "hot spares". They must already have a superblock
6706 * written
6707 */
6708 if (mddev->pers) {
6709 int err;
6710 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006711 pr_warn("%s: personality does not support diskops!\n",
6712 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713 return -EINVAL;
6714 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006715 if (mddev->persistent)
6716 rdev = md_import_device(dev, mddev->major_version,
6717 mddev->minor_version);
6718 else
6719 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006721 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 PTR_ERR(rdev));
6723 return PTR_ERR(rdev);
6724 }
NeilBrown1a855a02010-12-09 16:36:28 +11006725 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006726 if (!mddev->persistent) {
6727 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006728 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006729 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006730 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006731 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006732 } else
NeilBrown41158c72005-06-21 17:17:25 -07006733 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006734 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006735 } else
6736 super_types[mddev->major_version].
6737 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006738 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006739 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006740 /* This was a hot-add request, but events doesn't
6741 * match, so reject it.
6742 */
6743 export_rdev(rdev);
6744 return -EINVAL;
6745 }
6746
NeilBrownb2d444d2005-11-08 21:39:31 -08006747 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006748 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6749 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006750 else
6751 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006752 if (info->state & (1<<MD_DISK_FAILFAST))
6753 set_bit(FailFast, &rdev->flags);
6754 else
6755 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006756
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006757 if (info->state & (1<<MD_DISK_JOURNAL)) {
6758 struct md_rdev *rdev2;
6759 bool has_journal = false;
6760
6761 /* make sure no existing journal disk */
6762 rdev_for_each(rdev2, mddev) {
6763 if (test_bit(Journal, &rdev2->flags)) {
6764 has_journal = true;
6765 break;
6766 }
6767 }
NeilBrown230b55f2017-10-17 14:24:09 +11006768 if (has_journal || mddev->bitmap) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006769 export_rdev(rdev);
6770 return -EBUSY;
6771 }
Song Liubac624f2015-08-13 14:31:55 -07006772 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006773 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006774 /*
6775 * check whether the device shows up in other nodes
6776 */
6777 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006778 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006779 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006780 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006781 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006782 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006783 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006784 export_rdev(rdev);
6785 return err;
6786 }
6787 }
6788 }
6789
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 rdev->raid_disk = -1;
6791 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006792
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 if (err)
6794 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006795
6796 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006797 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6798 if (!err) {
6799 err = md_cluster_ops->new_disk_ack(mddev,
6800 err == 0);
6801 if (err)
6802 md_kick_rdev_from_array(rdev);
6803 }
6804 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006805 if (err)
6806 md_cluster_ops->add_new_disk_cancel(mddev);
6807 else
6808 err = add_bound_rdev(rdev);
6809 }
6810
6811 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006812 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006813
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814 return err;
6815 }
6816
6817 /* otherwise, add_new_disk is only allowed
6818 * for major_version==0 superblocks
6819 */
6820 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006821 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822 return -EINVAL;
6823 }
6824
6825 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6826 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006827 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006829 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830 PTR_ERR(rdev));
6831 return PTR_ERR(rdev);
6832 }
6833 rdev->desc_nr = info->number;
6834 if (info->raid_disk < mddev->raid_disks)
6835 rdev->raid_disk = info->raid_disk;
6836 else
6837 rdev->raid_disk = -1;
6838
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006840 if (info->state & (1<<MD_DISK_SYNC))
6841 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006843 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6844 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006845 if (info->state & (1<<MD_DISK_FAILFAST))
6846 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006847
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006849 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006850 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6851 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006852 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006853 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854
NeilBrown2bf071b2006-01-06 00:20:55 -08006855 err = bind_rdev_to_array(rdev, mddev);
6856 if (err) {
6857 export_rdev(rdev);
6858 return err;
6859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 }
6861
6862 return 0;
6863}
6864
NeilBrownf72ffdd2014-09-30 14:23:59 +10006865static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866{
6867 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006868 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869
Yufen Yuc42a0e22018-05-04 18:08:10 +08006870 if (!mddev->pers)
6871 return -ENODEV;
6872
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873 rdev = find_rdev(mddev, dev);
6874 if (!rdev)
6875 return -ENXIO;
6876
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006877 if (rdev->raid_disk < 0)
6878 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006879
NeilBrown3ea8929d2013-04-24 11:42:41 +10006880 clear_bit(Blocked, &rdev->flags);
6881 remove_and_add_spares(mddev, rdev);
6882
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883 if (rdev->raid_disk >= 0)
6884 goto busy;
6885
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006886kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006887 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006888 md_cluster_ops->remove_disk(mddev, rdev);
6889
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006890 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006891 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006892 if (mddev->thread)
6893 md_wakeup_thread(mddev->thread);
6894 else
6895 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006896 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897
6898 return 0;
6899busy:
NeilBrown9d487392016-11-02 14:16:49 +11006900 pr_debug("md: cannot remove active disk %s from %s ...\n",
6901 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 return -EBUSY;
6903}
6904
NeilBrownf72ffdd2014-09-30 14:23:59 +10006905static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906{
6907 char b[BDEVNAME_SIZE];
6908 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006909 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910
6911 if (!mddev->pers)
6912 return -ENODEV;
6913
6914 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006915 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 mdname(mddev));
6917 return -EINVAL;
6918 }
6919 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006920 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 mdname(mddev));
6922 return -EINVAL;
6923 }
6924
NeilBrownd710e132008-10-13 11:55:12 +11006925 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006927 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 PTR_ERR(rdev));
6929 return -EINVAL;
6930 }
6931
6932 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006933 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006935 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936
NeilBrown8190e752009-06-18 08:48:58 +10006937 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938
NeilBrownb2d444d2005-11-08 21:39:31 -08006939 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006940 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 bdevname(rdev->bdev,b), mdname(mddev));
6942 err = -EINVAL;
6943 goto abort_export;
6944 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006945
NeilBrownb2d444d2005-11-08 21:39:31 -08006946 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006948 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006949 err = bind_rdev_to_array(rdev, mddev);
6950 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006951 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952
6953 /*
6954 * The rest should better be atomic, we can have disk failures
6955 * noticed in interrupt contexts ...
6956 */
6957
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 rdev->raid_disk = -1;
6959
Shaohua Li29530792016-12-08 15:48:19 -08006960 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006961 if (!mddev->thread)
6962 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963 /*
6964 * Kick recovery, maybe this spare has to be added to the
6965 * array immediately.
6966 */
6967 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6968 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006969 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 return 0;
6971
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972abort_export:
6973 export_rdev(rdev);
6974 return err;
6975}
6976
NeilBrownfd01b882011-10-11 16:47:53 +11006977static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006978{
NeilBrown035328c2014-04-09 12:25:40 +10006979 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006980
NeilBrown36fa3062005-09-09 16:23:45 -07006981 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006982 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006983 return -EBUSY;
6984 if (mddev->recovery || mddev->sync_thread)
6985 return -EBUSY;
6986 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006987 }
6988
NeilBrown36fa3062005-09-09 16:23:45 -07006989 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006990 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006991 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006992
NeilBrown1e594bb2014-12-15 12:57:00 +11006993 if (mddev->bitmap || mddev->bitmap_info.file)
6994 return -EEXIST; /* cannot add when bitmap is present */
6995 f = fget(fd);
6996
6997 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006998 pr_warn("%s: error: failed to get bitmap file\n",
6999 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07007000 return -EBADF;
7001 }
7002
NeilBrown1e594bb2014-12-15 12:57:00 +11007003 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10007004 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11007005 pr_warn("%s: error: bitmap file must be a regular file\n",
7006 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10007007 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11007008 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11007009 pr_warn("%s: error: bitmap file must open for write\n",
7010 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10007011 err = -EBADF;
7012 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11007013 pr_warn("%s: error: bitmap file is already in use\n",
7014 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10007015 err = -EBUSY;
7016 }
7017 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11007018 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07007019 return err;
7020 }
NeilBrown1e594bb2014-12-15 12:57:00 +11007021 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11007022 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07007023 } else if (mddev->bitmap == NULL)
7024 return -ENOENT; /* cannot remove what isn't there */
7025 err = 0;
7026 if (mddev->pers) {
NeilBrown69e51b42010-06-01 19:37:35 +10007027 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007028 struct bitmap *bitmap;
7029
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007030 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007031 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007032 if (!IS_ERR(bitmap)) {
7033 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007034 err = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11007035 } else
7036 err = PTR_ERR(bitmap);
NeilBrown52a0d492017-10-17 13:46:43 +11007037 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007038 md_bitmap_destroy(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11007039 fd = -1;
7040 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007041 mddev_resume(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11007042 } else if (fd < 0) {
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007043 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007044 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007045 mddev_resume(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07007046 }
NeilBrownd7375ab2006-06-26 00:27:43 -07007047 }
7048 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11007049 struct file *f = mddev->bitmap_info.file;
7050 if (f) {
7051 spin_lock(&mddev->lock);
7052 mddev->bitmap_info.file = NULL;
7053 spin_unlock(&mddev->lock);
7054 fput(f);
7055 }
NeilBrown36fa3062005-09-09 16:23:45 -07007056 }
7057
NeilBrown32a76272005-06-21 17:17:14 -07007058 return err;
7059}
7060
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061/*
7062 * set_array_info is used two different ways
7063 * The original usage is when creating a new array.
7064 * In this usage, raid_disks is > 0 and it together with
7065 * level, size, not_persistent,layout,chunksize determine the
7066 * shape of the array.
7067 * This will always create an array with a type-0.90.0 superblock.
7068 * The newer usage is when assembling an array.
7069 * In this case raid_disks will be 0, and the major_version field is
7070 * use to determine which style super-blocks are to be found on the devices.
7071 * The minor and patch _version numbers are also kept incase the
7072 * super_block handler wishes to interpret them.
7073 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10007074static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075{
7076
7077 if (info->raid_disks == 0) {
7078 /* just setting version number for superblock loading */
7079 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07007080 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081 super_types[info->major_version].name == NULL) {
7082 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11007083 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084 info->major_version);
7085 return -EINVAL;
7086 }
7087 mddev->major_version = info->major_version;
7088 mddev->minor_version = info->minor_version;
7089 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08007090 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11007091 /* ensure mddev_put doesn't delete this now that there
7092 * is some minimal configuration.
7093 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11007094 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 return 0;
7096 }
7097 mddev->major_version = MD_MAJOR_VERSION;
7098 mddev->minor_version = MD_MINOR_VERSION;
7099 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11007100 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101
7102 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08007103 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11007104 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 mddev->raid_disks = info->raid_disks;
7106 /* don't set md_minor, it is determined by which /dev/md* was
7107 * openned
7108 */
7109 if (info->state & (1<<MD_SB_CLEAN))
7110 mddev->recovery_cp = MaxSector;
7111 else
7112 mddev->recovery_cp = 0;
7113 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08007114 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115
7116 mddev->layout = info->layout;
NeilBrown33f2c352019-09-09 16:52:29 +10007117 if (mddev->level == 0)
7118 /* Cannot trust RAID0 layout info here */
7119 mddev->layout = -1;
Andre Noll9d8f0362009-06-18 08:45:01 +10007120 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121
Shaohua Li29530792016-12-08 15:48:19 -08007122 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11007123 mddev->max_disks = MD_SB_DISKS;
7124 mddev->flags = 0;
7125 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08007126 }
7127 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128
NeilBrownc3d97142009-12-14 12:49:52 +11007129 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10007130 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11007131 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08007132
NeilBrownf6705572006-03-27 01:18:11 -08007133 mddev->reshape_position = MaxSector;
7134
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 /*
7136 * Generate a 128 bit UUID
7137 */
7138 get_random_bytes(mddev->uuid, 16);
7139
NeilBrownf6705572006-03-27 01:18:11 -08007140 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10007141 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08007142 mddev->new_layout = mddev->layout;
7143 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007144 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08007145
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 return 0;
7147}
7148
NeilBrownfd01b882011-10-11 16:47:53 +11007149void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11007150{
Shaohua Liefa4b772017-10-18 22:08:13 -07007151 lockdep_assert_held(&mddev->reconfig_mutex);
Dan Williamsb522adc2009-03-31 15:00:31 +11007152
7153 if (mddev->external_size)
7154 return;
7155
Dan Williams1f403622009-03-31 14:59:03 +11007156 mddev->array_sectors = array_sectors;
7157}
7158EXPORT_SYMBOL(md_set_array_sectors);
7159
NeilBrownfd01b882011-10-11 16:47:53 +11007160static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08007161{
NeilBrown3cb03002011-10-11 16:45:26 +11007162 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08007163 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10007164 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08007165 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04007166
NeilBrowna35b0d62006-01-06 00:20:49 -08007167 if (mddev->pers->resize == NULL)
7168 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10007169 /* The "num_sectors" is the number of sectors of each device that
7170 * is used. This can only make sense for arrays with redundancy.
7171 * linear and raid0 always use whatever space is available. We can only
7172 * consider changing this number if no resync or reconstruction is
7173 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10007174 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10007175 * of each device. If num_sectors is zero, we find the largest size
7176 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08007177 */
NeilBrownf851b602014-12-11 10:02:10 +11007178 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7179 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08007180 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10007181 if (mddev->ro)
7182 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10007183
NeilBrowndafb20f2012-03-19 12:46:39 +11007184 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11007185 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07007186
Andre Nolld71f9f82008-07-11 22:02:22 +10007187 if (fit && (num_sectors == 0 || num_sectors > avail))
7188 num_sectors = avail;
7189 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08007190 return -ENOSPC;
7191 }
Andre Nolld71f9f82008-07-11 22:02:22 +10007192 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08007193 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08007194 if (mddev_is_clustered(mddev))
7195 md_cluster_ops->update_size(mddev, old_dev_sectors);
7196 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08007197 set_capacity(mddev->gendisk, mddev->array_sectors);
7198 revalidate_disk(mddev->gendisk);
7199 }
7200 }
NeilBrowna35b0d62006-01-06 00:20:49 -08007201 return rv;
7202}
7203
NeilBrownfd01b882011-10-11 16:47:53 +11007204static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08007205{
7206 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10007207 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08007208 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08007209 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08007210 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10007211 if (mddev->ro)
7212 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08007213 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10007214 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08007215 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11007216 if (mddev->sync_thread ||
7217 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7218 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08007219 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10007220
7221 rdev_for_each(rdev, mddev) {
7222 if (mddev->raid_disks < raid_disks &&
7223 rdev->data_offset < rdev->new_data_offset)
7224 return -EINVAL;
7225 if (mddev->raid_disks > raid_disks &&
7226 rdev->data_offset > rdev->new_data_offset)
7227 return -EINVAL;
7228 }
7229
NeilBrown63c70c42006-03-27 01:18:13 -08007230 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10007231 if (mddev->delta_disks < 0)
7232 mddev->reshape_backwards = 1;
7233 else if (mddev->delta_disks > 0)
7234 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08007235
7236 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10007237 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11007238 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007239 mddev->reshape_backwards = 0;
7240 }
NeilBrownda943b992006-01-06 00:20:54 -08007241 return rv;
7242}
7243
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244/*
7245 * update_array_info is used to change the configuration of an
7246 * on-line array.
7247 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
7248 * fields in the info are checked against the array.
7249 * Any differences that cannot be handled will cause an error.
7250 * Normally, only one change can be managed at a time.
7251 */
NeilBrownfd01b882011-10-11 16:47:53 +11007252static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253{
7254 int rv = 0;
7255 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007256 int state = 0;
7257
7258 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11007259 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07007260 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261
7262 if (mddev->major_version != info->major_version ||
7263 mddev->minor_version != info->minor_version ||
7264/* mddev->patch_version != info->patch_version || */
7265 mddev->ctime != info->ctime ||
7266 mddev->level != info->level ||
7267/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08007268 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10007269 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07007270 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
7271 ((state^info->state) & 0xfffffe00)
7272 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273 return -EINVAL;
7274 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11007275 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
7276 cnt++;
7277 if (mddev->raid_disks != info->raid_disks)
7278 cnt++;
7279 if (mddev->layout != info->layout)
7280 cnt++;
7281 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
7282 cnt++;
7283 if (cnt == 0)
7284 return 0;
7285 if (cnt > 1)
7286 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287
7288 if (mddev->layout != info->layout) {
7289 /* Change layout
7290 * we don't need to do anything at the md level, the
7291 * personality will take care of it all.
7292 */
NeilBrown50ac1682009-06-18 08:47:55 +10007293 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007295 else {
7296 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10007297 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10007298 if (rv)
7299 mddev->new_layout = mddev->layout;
7300 return rv;
7301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007303 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10007304 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305
NeilBrownda943b992006-01-06 00:20:54 -08007306 if (mddev->raid_disks != info->raid_disks)
7307 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308
NeilBrown36fa3062005-09-09 16:23:45 -07007309 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007310 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
7311 rv = -EINVAL;
7312 goto err;
7313 }
7314 if (mddev->recovery || mddev->sync_thread) {
7315 rv = -EBUSY;
7316 goto err;
7317 }
NeilBrown36fa3062005-09-09 16:23:45 -07007318 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007319 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07007320 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007321 if (mddev->bitmap) {
7322 rv = -EEXIST;
7323 goto err;
7324 }
7325 if (mddev->bitmap_info.default_offset == 0) {
7326 rv = -EINVAL;
7327 goto err;
7328 }
NeilBrownc3d97142009-12-14 12:49:52 +11007329 mddev->bitmap_info.offset =
7330 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10007331 mddev->bitmap_info.space =
7332 mddev->bitmap_info.default_space;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007333 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007334 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007335 if (!IS_ERR(bitmap)) {
7336 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007337 rv = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11007338 } else
7339 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07007340 if (rv)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007341 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007342 mddev_resume(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07007343 } else {
7344 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007345 if (!mddev->bitmap) {
7346 rv = -ENOENT;
7347 goto err;
7348 }
7349 if (mddev->bitmap->storage.file) {
7350 rv = -EINVAL;
7351 goto err;
7352 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11007353 if (mddev->bitmap_info.nodes) {
7354 /* hold PW on all the bitmap lock */
7355 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11007356 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 +11007357 rv = -EPERM;
7358 md_cluster_ops->unlock_all_bitmaps(mddev);
7359 goto err;
7360 }
7361
7362 mddev->bitmap_info.nodes = 0;
7363 md_cluster_ops->leave(mddev);
7364 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007365 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007366 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007367 mddev_resume(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11007368 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007369 }
7370 }
NeilBrown850b2b422006-10-03 01:15:46 -07007371 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007372 return rv;
7373err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374 return rv;
7375}
7376
NeilBrownfd01b882011-10-11 16:47:53 +11007377static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378{
NeilBrown3cb03002011-10-11 16:45:26 +11007379 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11007380 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381
7382 if (mddev->pers == NULL)
7383 return -ENODEV;
7384
NeilBrown1ca69c42012-10-11 13:37:33 +11007385 rcu_read_lock();
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01007386 rdev = md_find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11007388 err = -ENODEV;
7389 else {
7390 md_error(mddev, rdev);
7391 if (!test_bit(Faulty, &rdev->flags))
7392 err = -EBUSY;
7393 }
7394 rcu_read_unlock();
7395 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396}
7397
Andre Noll2f9618c2008-04-25 18:57:58 +02007398/*
7399 * We have a problem here : there is no easy way to give a CHS
7400 * virtual geometry. We currently pretend that we have a 2 heads
7401 * 4 sectors (with a BIG number of cylinders...). This drives
7402 * dosfs just mad... ;-)
7403 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007404static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7405{
NeilBrownfd01b882011-10-11 16:47:53 +11007406 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007407
7408 geo->heads = 2;
7409 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11007410 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007411 return 0;
7412}
7413
Nicolas Schichancb335f82014-01-15 16:58:52 +01007414static inline bool md_ioctl_valid(unsigned int cmd)
7415{
7416 switch (cmd) {
7417 case ADD_NEW_DISK:
7418 case BLKROSET:
7419 case GET_ARRAY_INFO:
7420 case GET_BITMAP_FILE:
7421 case GET_DISK_INFO:
7422 case HOT_ADD_DISK:
7423 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007424 case RAID_AUTORUN:
7425 case RAID_VERSION:
7426 case RESTART_ARRAY_RW:
7427 case RUN_ARRAY:
7428 case SET_ARRAY_INFO:
7429 case SET_BITMAP_FILE:
7430 case SET_DISK_FAULTY:
7431 case STOP_ARRAY:
7432 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007433 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007434 return true;
7435 default:
7436 return false;
7437 }
7438}
7439
Al Viroa39907f2008-03-02 10:31:15 -05007440static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441 unsigned int cmd, unsigned long arg)
7442{
7443 int err = 0;
7444 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11007445 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10007446 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08007447 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448
Nicolas Schichancb335f82014-01-15 16:58:52 +01007449 if (!md_ioctl_valid(cmd))
7450 return -ENOTTY;
7451
NeilBrown506c9e42011-12-23 10:17:26 +11007452 switch (cmd) {
7453 case RAID_VERSION:
7454 case GET_ARRAY_INFO:
7455 case GET_DISK_INFO:
7456 break;
7457 default:
7458 if (!capable(CAP_SYS_ADMIN))
7459 return -EACCES;
7460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461
7462 /*
7463 * Commands dealing with the RAID driver but not any
7464 * particular array:
7465 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007466 switch (cmd) {
7467 case RAID_VERSION:
7468 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007469 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11007472 case RAID_AUTORUN:
7473 err = 0;
7474 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007475 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11007477 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478 }
7479
7480 /*
7481 * Commands creating/starting a new array:
7482 */
7483
Al Viroa39907f2008-03-02 10:31:15 -05007484 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485
7486 if (!mddev) {
7487 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007488 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 }
7490
NeilBrown1ca69c42012-10-11 13:37:33 +11007491 /* Some actions do not requires the mutex */
7492 switch (cmd) {
7493 case GET_ARRAY_INFO:
7494 if (!mddev->raid_disks && !mddev->external)
7495 err = -ENODEV;
7496 else
7497 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007498 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007499
7500 case GET_DISK_INFO:
7501 if (!mddev->raid_disks && !mddev->external)
7502 err = -ENODEV;
7503 else
7504 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007505 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007506
7507 case SET_DISK_FAULTY:
7508 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007509 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007510
7511 case GET_BITMAP_FILE:
7512 err = get_bitmap_file(mddev, argp);
7513 goto out;
7514
NeilBrown1ca69c42012-10-11 13:37:33 +11007515 }
7516
Guoqing Jiang78b990c2020-04-04 23:57:10 +02007517 if (cmd == ADD_NEW_DISK || cmd == HOT_ADD_DISK)
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02007518 flush_rdev_wq(mddev);
NeilBrowna7a3f082012-12-11 13:35:54 +11007519
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007520 if (cmd == HOT_REMOVE_DISK)
7521 /* need to ensure recovery thread has run */
7522 wait_event_interruptible_timeout(mddev->sb_wait,
7523 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007524 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007525 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007526 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7527 /* Need to flush page cache, and ensure no-one else opens
7528 * and writes
7529 */
7530 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007531 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007532 mutex_unlock(&mddev->open_mutex);
7533 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007534 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007535 }
NeilBrown065e5192017-04-06 11:16:33 +08007536 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007537 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007538 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007539 mutex_unlock(&mddev->open_mutex);
7540 sync_blockdev(bdev);
7541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542 err = mddev_lock(mddev);
7543 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007544 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7545 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007546 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 }
7548
NeilBrownc02c0ae2012-12-11 13:39:21 +11007549 if (cmd == SET_ARRAY_INFO) {
7550 mdu_array_info_t info;
7551 if (!arg)
7552 memset(&info, 0, sizeof(info));
7553 else if (copy_from_user(&info, argp, sizeof(info))) {
7554 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007555 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007556 }
7557 if (mddev->pers) {
7558 err = update_array_info(mddev, &info);
7559 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007560 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007561 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007563 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007564 }
7565 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007566 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007567 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007568 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007569 }
7570 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007571 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007572 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007573 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007574 }
7575 err = set_array_info(mddev, &info);
7576 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007577 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007578 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007579 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007580 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 }
7582
7583 /*
7584 * Commands querying/configuring an existing array:
7585 */
NeilBrown32a76272005-06-21 17:17:14 -07007586 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007587 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007588 if ((!mddev->raid_disks && !mddev->external)
7589 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7590 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7591 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007593 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 }
7595
7596 /*
7597 * Commands even a read-only array can execute:
7598 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007599 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007600 case RESTART_ARRAY_RW:
7601 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007602 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007603
7604 case STOP_ARRAY:
7605 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007606 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007607
7608 case STOP_ARRAY_RO:
7609 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007610 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007611
NeilBrown3ea8929d2013-04-24 11:42:41 +10007612 case HOT_REMOVE_DISK:
7613 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007614 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007615
NeilBrown7ceb17e2013-04-24 11:42:42 +10007616 case ADD_NEW_DISK:
7617 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007618 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007619 * So require mddev->pers and MD_DISK_SYNC.
7620 */
7621 if (mddev->pers) {
7622 mdu_disk_info_t info;
7623 if (copy_from_user(&info, argp, sizeof(info)))
7624 err = -EFAULT;
7625 else if (!(info.state & (1<<MD_DISK_SYNC)))
7626 /* Need to clear read-only for this */
7627 break;
7628 else
7629 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007630 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007631 }
7632 break;
7633
NeilBrownc02c0ae2012-12-11 13:39:21 +11007634 case BLKROSET:
7635 if (get_user(ro, (int __user *)(arg))) {
7636 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007637 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007638 }
7639 err = -EINVAL;
7640
7641 /* if the bdev is going readonly the value of mddev->ro
7642 * does not matter, no writes are coming
7643 */
7644 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007645 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007646
NeilBrownc02c0ae2012-12-11 13:39:21 +11007647 /* are we are already prepared for writes? */
7648 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007649 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007650
7651 /* transitioning to readauto need only happen for
7652 * arrays that call md_write_start
7653 */
7654 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007656 if (err == 0) {
7657 mddev->ro = 2;
7658 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007659 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007660 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007661 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007662 }
7663
7664 /*
7665 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007666 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667 */
NeilBrown326eb172014-09-30 15:36:28 +10007668 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007669 if (mddev->ro == 2) {
7670 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007671 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007672 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007673 /* mddev_unlock will wake thread */
7674 /* If a device failed while we were read-only, we
7675 * need to make sure the metadata is updated now.
7676 */
Shaohua Li29530792016-12-08 15:48:19 -08007677 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007678 mddev_unlock(mddev);
7679 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007680 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7681 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007682 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007683 }
NeilBrownf91de922005-11-08 21:39:36 -08007684 } else {
7685 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007686 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688 }
7689
NeilBrownc02c0ae2012-12-11 13:39:21 +11007690 switch (cmd) {
7691 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007692 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007693 mdu_disk_info_t info;
7694 if (copy_from_user(&info, argp, sizeof(info)))
7695 err = -EFAULT;
7696 else
7697 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007698 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007701 case CLUSTERED_DISK_NACK:
7702 if (mddev_is_clustered(mddev))
7703 md_cluster_ops->new_disk_ack(mddev, false);
7704 else
7705 err = -EINVAL;
7706 goto unlock;
7707
NeilBrownc02c0ae2012-12-11 13:39:21 +11007708 case HOT_ADD_DISK:
7709 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007710 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711
NeilBrownc02c0ae2012-12-11 13:39:21 +11007712 case RUN_ARRAY:
7713 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007714 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007715
NeilBrownc02c0ae2012-12-11 13:39:21 +11007716 case SET_BITMAP_FILE:
7717 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007718 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007719
NeilBrownc02c0ae2012-12-11 13:39:21 +11007720 default:
7721 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007722 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723 }
7724
NeilBrown3adc28d2014-09-30 15:46:41 +10007725unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007726 if (mddev->hold_active == UNTIL_IOCTL &&
7727 err != -EINVAL)
7728 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007730out:
NeilBrown065e5192017-04-06 11:16:33 +08007731 if(did_set_md_closing)
7732 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733 return err;
7734}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007735#ifdef CONFIG_COMPAT
7736static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7737 unsigned int cmd, unsigned long arg)
7738{
7739 switch (cmd) {
7740 case HOT_REMOVE_DISK:
7741 case HOT_ADD_DISK:
7742 case SET_DISK_FAULTY:
7743 case SET_BITMAP_FILE:
7744 /* These take in integer arg, do not convert */
7745 break;
7746 default:
7747 arg = (unsigned long)compat_ptr(arg);
7748 break;
7749 }
7750
7751 return md_ioctl(bdev, mode, cmd, arg);
7752}
7753#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007754
Al Viroa39907f2008-03-02 10:31:15 -05007755static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007756{
7757 /*
7758 * Succeed if we can lock the mddev, which confirms that
7759 * it isn't being stopped right now.
7760 */
NeilBrownfd01b882011-10-11 16:47:53 +11007761 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007762 int err;
7763
Yuanhan Liu0c098222012-05-22 13:55:32 +10007764 if (!mddev)
7765 return -ENODEV;
7766
NeilBrownd3374822009-01-09 08:31:10 +11007767 if (mddev->gendisk != bdev->bd_disk) {
7768 /* we are racing with mddev_put which is discarding this
7769 * bd_disk.
7770 */
7771 mddev_put(mddev);
7772 /* Wait until bdev->bd_disk is definitely gone */
Guoqing Jiangf6766ff2020-04-04 23:57:09 +02007773 if (work_pending(&mddev->del_work))
7774 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007775 /* Then retry the open from the top */
7776 return -ERESTARTSYS;
7777 }
7778 BUG_ON(mddev != bdev->bd_disk->private_data);
7779
NeilBrownc8c00a62009-08-10 12:50:52 +10007780 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781 goto out;
7782
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007783 if (test_bit(MD_CLOSING, &mddev->flags)) {
7784 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007785 err = -ENODEV;
7786 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007787 }
7788
Linus Torvalds1da177e2005-04-16 15:20:36 -07007789 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007790 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007791 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007792
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007793 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007794 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007795 if (err)
7796 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007797 return err;
7798}
7799
Al Virodb2a1442013-05-05 21:52:57 -04007800static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007801{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007802 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007804 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007805 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007806 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007808
7809static int md_media_changed(struct gendisk *disk)
7810{
NeilBrownfd01b882011-10-11 16:47:53 +11007811 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007812
7813 return mddev->changed;
7814}
7815
7816static int md_revalidate(struct gendisk *disk)
7817{
NeilBrownfd01b882011-10-11 16:47:53 +11007818 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007819
7820 mddev->changed = 0;
7821 return 0;
7822}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007823static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824{
7825 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007826 .open = md_open,
7827 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007828 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007829#ifdef CONFIG_COMPAT
7830 .compat_ioctl = md_compat_ioctl,
7831#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007832 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007833 .media_changed = md_media_changed,
7834 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835};
7836
NeilBrownf72ffdd2014-09-30 14:23:59 +10007837static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007838{
NeilBrown2b8bf342011-10-11 16:48:23 +11007839 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841 /*
7842 * md_thread is a 'system-thread', it's priority should be very
7843 * high. We avoid resource deadlocks individually in each
7844 * raid personality. (RAID5 does preallocation) We also use RR and
7845 * the very same RT priority as kswapd, thus we will never get
7846 * into a priority inversion deadlock.
7847 *
7848 * we definitely have to have equal or higher priority than
7849 * bdflush, otherwise bdflush will deadlock if there are too
7850 * many dirty RAID5 blocks.
7851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007852
NeilBrown6985c432005-10-19 21:23:47 -07007853 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007854 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007855
NeilBrown93588e22005-11-15 00:09:12 -08007856 /* We need to wait INTERRUPTIBLE so that
7857 * we don't add to the load-average.
7858 * That means we need to be sure no signals are
7859 * pending
7860 */
7861 if (signal_pending(current))
7862 flush_signals(current);
7863
7864 wait_event_interruptible_timeout
7865 (thread->wqueue,
7866 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007867 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007868 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869
NeilBrown6c987912011-01-14 09:13:53 +11007870 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007871 if (kthread_should_park())
7872 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007873 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007874 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007876
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877 return 0;
7878}
7879
NeilBrown2b8bf342011-10-11 16:48:23 +11007880void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881{
7882 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007883 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Guoqing Jiangd1d90142017-10-09 10:32:48 +08007884 set_bit(THREAD_WAKEUP, &thread->flags);
7885 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886 }
7887}
NeilBrown6c144d32014-09-30 16:15:38 +10007888EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889
Shaohua Li4ed87312012-10-11 13:34:00 +11007890struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7891 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892{
NeilBrown2b8bf342011-10-11 16:48:23 +11007893 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894
NeilBrown2b8bf342011-10-11 16:48:23 +11007895 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007896 if (!thread)
7897 return NULL;
7898
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899 init_waitqueue_head(&thread->wqueue);
7900
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901 thread->run = run;
7902 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007903 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007904 thread->tsk = kthread_run(md_thread, thread,
7905 "%s_%s",
7906 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007907 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007908 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909 kfree(thread);
7910 return NULL;
7911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007912 return thread;
7913}
NeilBrown6c144d32014-09-30 16:15:38 +10007914EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915
NeilBrown2b8bf342011-10-11 16:48:23 +11007916void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917{
NeilBrown2b8bf342011-10-11 16:48:23 +11007918 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007919 if (!thread)
7920 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007921 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007922 /* Locking ensures that mddev_unlock does not wake_up a
7923 * non-existent thread
7924 */
7925 spin_lock(&pers_lock);
7926 *threadp = NULL;
7927 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007928
7929 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930 kfree(thread);
7931}
NeilBrown6c144d32014-09-30 16:15:38 +10007932EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007933
NeilBrownfd01b882011-10-11 16:47:53 +11007934void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935{
NeilBrownb2d444d2005-11-08 21:39:31 -08007936 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007938
NeilBrownde393cd2011-07-28 11:31:48 +10007939 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940 return;
7941 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007942 if (mddev->degraded)
7943 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007944 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7946 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7947 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007948 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007949 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007950 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007951}
NeilBrown6c144d32014-09-30 16:15:38 +10007952EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953
7954/* seq_file implementation /proc/mdstat */
7955
7956static void status_unused(struct seq_file *seq)
7957{
7958 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007959 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007960
7961 seq_printf(seq, "unused devices: ");
7962
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007963 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964 char b[BDEVNAME_SIZE];
7965 i++;
7966 seq_printf(seq, "%s ",
7967 bdevname(rdev->bdev,b));
7968 }
7969 if (!i)
7970 seq_printf(seq, "<none>");
7971
7972 seq_printf(seq, "\n");
7973}
7974
NeilBrownf7851be2015-07-02 17:12:58 +10007975static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976{
NeilBrowndd71cf62009-05-07 12:49:35 +10007977 sector_t max_sectors, resync, res;
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02007978 unsigned long dt, db = 0;
7979 sector_t rt, curr_mark_cnt, resync_mark_cnt;
7980 int scale, recovery_active;
NeilBrown4588b422006-03-27 01:18:04 -08007981 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982
NeilBrownc804cde2012-05-21 09:28:33 +10007983 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7984 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007985 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007986 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007987 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007988
NeilBrownf7851be2015-07-02 17:12:58 +10007989 resync = mddev->curr_resync;
7990 if (resync <= 3) {
7991 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7992 /* Still cleaning up */
7993 resync = max_sectors;
Nate Daileyd2e2ec82017-11-30 11:33:30 -05007994 } else if (resync > max_sectors)
7995 resync = max_sectors;
7996 else
NeilBrownf7851be2015-07-02 17:12:58 +10007997 resync -= atomic_read(&mddev->recovery_active);
7998
7999 if (resync == 0) {
Guoqing Jiang0357ba22018-07-02 16:26:25 +08008000 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
8001 struct md_rdev *rdev;
8002
8003 rdev_for_each(rdev, mddev)
8004 if (rdev->raid_disk >= 0 &&
8005 !test_bit(Faulty, &rdev->flags) &&
8006 rdev->recovery_offset != MaxSector &&
8007 rdev->recovery_offset) {
8008 seq_printf(seq, "\trecover=REMOTE");
8009 return 1;
8010 }
8011 if (mddev->reshape_position != MaxSector)
8012 seq_printf(seq, "\treshape=REMOTE");
8013 else
8014 seq_printf(seq, "\tresync=REMOTE");
8015 return 1;
8016 }
NeilBrownf7851be2015-07-02 17:12:58 +10008017 if (mddev->recovery_cp < MaxSector) {
8018 seq_printf(seq, "\tresync=PENDING");
8019 return 1;
8020 }
8021 return 0;
8022 }
8023 if (resync < 3) {
8024 seq_printf(seq, "\tresync=DELAYED");
8025 return 1;
8026 }
8027
NeilBrown403df472014-09-30 15:52:29 +10008028 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08008029 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10008030 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08008031 * u32, as those are the requirements for sector_div.
8032 * Thus 'scale' must be at least 10
8033 */
8034 scale = 10;
8035 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10008036 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08008037 scale++;
8038 }
8039 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10008040 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08008041
8042 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008043 {
NeilBrown4588b422006-03-27 01:18:04 -08008044 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045 seq_printf(seq, "[");
8046 for (i = 0; i < x; i++)
8047 seq_printf(seq, "=");
8048 seq_printf(seq, ">");
8049 for (i = 0; i < y; i++)
8050 seq_printf(seq, ".");
8051 seq_printf(seq, "] ");
8052 }
NeilBrown4588b422006-03-27 01:18:04 -08008053 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08008054 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
8055 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07008056 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
8057 "check" :
8058 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
8059 "resync" : "recovery"))),
8060 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10008061 (unsigned long long) resync/2,
8062 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063
8064 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07008065 * dt: time from mark until now
8066 * db: blocks written from mark until now
8067 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10008068 *
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008069 * rt is a sector_t, which is always 64bit now. We are keeping
8070 * the original algorithm, but it is not really necessary.
8071 *
8072 * Original algorithm:
8073 * So we divide before multiply in case it is 32bit and close
8074 * to the limit.
8075 * We scale the divisor (db) by 32 to avoid losing precision
8076 * near the end of resync when the number of remaining sectors
8077 * is close to 'db'.
8078 * We then divide rt by 32 after multiplying by db to compensate.
8079 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008080 */
8081 dt = ((jiffies - mddev->resync_mark) / HZ);
8082 if (!dt) dt++;
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008083
8084 curr_mark_cnt = mddev->curr_mark_cnt;
8085 recovery_active = atomic_read(&mddev->recovery_active);
8086 resync_mark_cnt = mddev->resync_mark_cnt;
8087
8088 if (curr_mark_cnt >= (recovery_active + resync_mark_cnt))
8089 db = curr_mark_cnt - (recovery_active + resync_mark_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008090
NeilBrowndd71cf62009-05-07 12:49:35 +10008091 rt = max_sectors - resync; /* number of remaining sectors */
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008092 rt = div64_u64(rt, db/32+1);
NeilBrowndd71cf62009-05-07 12:49:35 +10008093 rt *= dt;
8094 rt >>= 5;
8095
8096 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
8097 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008098
NeilBrownff4e8d92006-07-10 04:44:16 -07008099 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10008100 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008101}
8102
8103static void *md_seq_start(struct seq_file *seq, loff_t *pos)
8104{
8105 struct list_head *tmp;
8106 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11008107 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008108
8109 if (l >= 0x10000)
8110 return NULL;
8111 if (!l--)
8112 /* header */
8113 return (void*)1;
8114
8115 spin_lock(&all_mddevs_lock);
8116 list_for_each(tmp,&all_mddevs)
8117 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11008118 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008119 mddev_get(mddev);
8120 spin_unlock(&all_mddevs_lock);
8121 return mddev;
8122 }
8123 spin_unlock(&all_mddevs_lock);
8124 if (!l--)
8125 return (void*)2;/* tail */
8126 return NULL;
8127}
8128
8129static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
8130{
8131 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008132 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10008133
Linus Torvalds1da177e2005-04-16 15:20:36 -07008134 ++*pos;
8135 if (v == (void*)2)
8136 return NULL;
8137
8138 spin_lock(&all_mddevs_lock);
8139 if (v == (void*)1)
8140 tmp = all_mddevs.next;
8141 else
8142 tmp = mddev->all_mddevs.next;
8143 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11008144 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008145 else {
8146 next_mddev = (void*)2;
8147 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10008148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008149 spin_unlock(&all_mddevs_lock);
8150
8151 if (v != (void*)1)
8152 mddev_put(mddev);
8153 return next_mddev;
8154
8155}
8156
8157static void md_seq_stop(struct seq_file *seq, void *v)
8158{
NeilBrownfd01b882011-10-11 16:47:53 +11008159 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008160
8161 if (mddev && v != (void*)1 && v != (void*)2)
8162 mddev_put(mddev);
8163}
8164
8165static int md_seq_show(struct seq_file *seq, void *v)
8166{
NeilBrownfd01b882011-10-11 16:47:53 +11008167 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11008168 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11008169 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008170
8171 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11008172 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008173 seq_printf(seq, "Personalities : ");
8174 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008175 list_for_each_entry(pers, &pers_list, list)
8176 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008177
8178 spin_unlock(&pers_lock);
8179 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02008180 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008181 return 0;
8182 }
8183 if (v == (void*)2) {
8184 status_unused(seq);
8185 return 0;
8186 }
8187
NeilBrown36d091f2014-12-15 12:56:58 +11008188 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
8190 seq_printf(seq, "%s : %sactive", mdname(mddev),
8191 mddev->pers ? "" : "in");
8192 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08008193 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008194 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08008195 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07008196 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197 seq_printf(seq, " %s", mddev->pers->name);
8198 }
8199
Andre Nolldd8ac332009-03-31 14:33:13 +11008200 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11008201 rcu_read_lock();
8202 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008203 char b[BDEVNAME_SIZE];
8204 seq_printf(seq, " %s[%d]",
8205 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07008206 if (test_bit(WriteMostly, &rdev->flags))
8207 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07008208 if (test_bit(Journal, &rdev->flags))
8209 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08008210 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008211 seq_printf(seq, "(F)");
8212 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11008213 }
8214 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07008215 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11008216 if (test_bit(Replacement, &rdev->flags))
8217 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11008218 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008219 }
NeilBrownf97fcad2014-12-15 12:56:59 +11008220 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008221
8222 if (!list_empty(&mddev->disks)) {
8223 if (mddev->pers)
8224 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10008225 (unsigned long long)
8226 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227 else
8228 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11008229 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008230 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07008231 if (mddev->persistent) {
8232 if (mddev->major_version != 0 ||
8233 mddev->minor_version != 90) {
8234 seq_printf(seq," super %d.%d",
8235 mddev->major_version,
8236 mddev->minor_version);
8237 }
NeilBrowne6910632008-02-06 01:39:51 -08008238 } else if (mddev->external)
8239 seq_printf(seq, " super external:%s",
8240 mddev->metadata_type);
8241 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07008242 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008243
8244 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11008245 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10008246 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08008247 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10008248 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08008249 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08008250 }
NeilBrown32a76272005-06-21 17:17:14 -07008251 } else
8252 seq_printf(seq, "\n ");
8253
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008254 md_bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008255
8256 seq_printf(seq, "\n");
8257 }
NeilBrown36d091f2014-12-15 12:56:58 +11008258 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10008259
Linus Torvalds1da177e2005-04-16 15:20:36 -07008260 return 0;
8261}
8262
Jan Engelhardt110518b2009-05-07 12:49:37 +10008263static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264 .start = md_seq_start,
8265 .next = md_seq_next,
8266 .stop = md_seq_stop,
8267 .show = md_seq_show,
8268};
8269
8270static int md_seq_open(struct inode *inode, struct file *file)
8271{
Kay Sieversf1514632011-07-12 20:48:39 +02008272 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008273 int error;
8274
8275 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08008276 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02008277 return error;
8278
8279 seq = file->private_data;
8280 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008281 return error;
8282}
8283
NeilBrowne2f23b62014-04-09 14:33:51 +10008284static int md_unloading;
Al Viroafc9a422017-07-03 06:39:46 -04008285static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
NeilBrownd7603b72006-01-06 00:20:30 -08008286{
Kay Sieversf1514632011-07-12 20:48:39 +02008287 struct seq_file *seq = filp->private_data;
Al Viroafc9a422017-07-03 06:39:46 -04008288 __poll_t mask;
NeilBrownd7603b72006-01-06 00:20:30 -08008289
NeilBrowne2f23b62014-04-09 14:33:51 +10008290 if (md_unloading)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008291 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08008292 poll_wait(filp, &md_event_waiters, wait);
8293
8294 /* always allow read */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008295 mask = EPOLLIN | EPOLLRDNORM;
NeilBrownd7603b72006-01-06 00:20:30 -08008296
Kay Sieversf1514632011-07-12 20:48:39 +02008297 if (seq->poll_event != atomic_read(&md_event_count))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008298 mask |= EPOLLERR | EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08008299 return mask;
8300}
8301
Alexey Dobriyan97a32532020-02-03 17:37:17 -08008302static const struct proc_ops mdstat_proc_ops = {
8303 .proc_open = md_seq_open,
8304 .proc_read = seq_read,
8305 .proc_lseek = seq_lseek,
8306 .proc_release = seq_release,
8307 .proc_poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008308};
8309
NeilBrown84fc4b52011-10-11 16:49:58 +11008310int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008311{
NeilBrown9d487392016-11-02 14:16:49 +11008312 pr_debug("md: %s personality registered for level %d\n",
8313 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008314 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008315 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008316 spin_unlock(&pers_lock);
8317 return 0;
8318}
NeilBrown6c144d32014-09-30 16:15:38 +10008319EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008320
NeilBrown84fc4b52011-10-11 16:49:58 +11008321int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008322{
NeilBrown9d487392016-11-02 14:16:49 +11008323 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008325 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008326 spin_unlock(&pers_lock);
8327 return 0;
8328}
NeilBrown6c144d32014-09-30 16:15:38 +10008329EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008330
NeilBrown6022e752015-08-13 12:32:55 +10008331int register_md_cluster_operations(struct md_cluster_operations *ops,
8332 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008333{
NeilBrown6022e752015-08-13 12:32:55 +10008334 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008335 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008336 if (md_cluster_ops != NULL)
8337 ret = -EALREADY;
8338 else {
8339 md_cluster_ops = ops;
8340 md_cluster_mod = module;
8341 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008342 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008343 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008344}
8345EXPORT_SYMBOL(register_md_cluster_operations);
8346
8347int unregister_md_cluster_operations(void)
8348{
8349 spin_lock(&pers_lock);
8350 md_cluster_ops = NULL;
8351 spin_unlock(&pers_lock);
8352 return 0;
8353}
8354EXPORT_SYMBOL(unregister_md_cluster_operations);
8355
8356int md_setup_cluster(struct mddev *mddev, int nodes)
8357{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008358 if (!md_cluster_ops)
8359 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008360 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008361 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008362 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11008363 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008364 spin_unlock(&pers_lock);
8365 return -ENOENT;
8366 }
8367 spin_unlock(&pers_lock);
8368
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05008369 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008370}
8371
8372void md_cluster_stop(struct mddev *mddev)
8373{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05008374 if (!md_cluster_ops)
8375 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008376 md_cluster_ops->leave(mddev);
8377 module_put(md_cluster_mod);
8378}
8379
NeilBrownfd01b882011-10-11 16:47:53 +11008380static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381{
NeilBrownf72ffdd2014-09-30 14:23:59 +10008382 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008383 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11008384 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008385
8386 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10008387 rcu_read_lock();
8388 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008389 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Michael Callahan59767fb2018-07-18 04:47:37 -07008390 curr_events = (int)part_stat_read_accum(&disk->part0, sectors) -
NeilBrowneea1bf32009-03-31 14:27:02 +11008391 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07008392 /* sync IO will cause sync_io to increase before the disk_stats
8393 * as sync_io is counted when a request starts, and
8394 * disk_stats is counted when it completes.
8395 * So resync activity will cause curr_events to be smaller than
8396 * when there was no such activity.
8397 * non-sync IO will cause disk_stat to increase without
8398 * increasing sync_io so curr_events will (eventually)
8399 * be larger than it was before. Once it becomes
8400 * substantially larger, the test below will cause
8401 * the array to appear non-idle, and resync will slow
8402 * down.
8403 * If there is a lot of outstanding resync activity when
8404 * we set last_event to curr_events, then all that activity
8405 * completing might cause the array to appear non-idle
8406 * and resync will be slowed down even though there might
8407 * not have been non-resync activity. This will only
8408 * happen once though. 'last_events' will soon reflect
8409 * the state where there is little or no outstanding
8410 * resync requests, and further resync activity will
8411 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08008412 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008413 */
NeilBrowneea1bf32009-03-31 14:27:02 +11008414 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415 rdev->last_events = curr_events;
8416 idle = 0;
8417 }
8418 }
NeilBrown4b809912008-07-21 17:05:25 +10008419 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008420 return idle;
8421}
8422
NeilBrownfd01b882011-10-11 16:47:53 +11008423void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008424{
8425 /* another "blocks" (512byte) blocks have been synced */
8426 atomic_sub(blocks, &mddev->recovery_active);
8427 wake_up(&mddev->recovery_wait);
8428 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07008429 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08008430 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008431 md_wakeup_thread(mddev->thread);
8432 // stop recovery, signal do_sync ....
8433 }
8434}
NeilBrown6c144d32014-09-30 16:15:38 +10008435EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008436
NeilBrown06d91a52005-06-21 17:17:12 -07008437/* md_write_start(mddev, bi)
8438 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07008439 * in superblock) before writing, schedule a superblock update
8440 * and wait for it to complete.
NeilBrowncc27b0c2017-06-05 16:49:39 +10008441 * A return value of 'false' means that the write wasn't recorded
8442 * and cannot proceed as the array is being suspend.
NeilBrown06d91a52005-06-21 17:17:12 -07008443 */
NeilBrowncc27b0c2017-06-05 16:49:39 +10008444bool md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445{
Neil Brown0fd62b82008-06-28 08:31:36 +10008446 int did_change = 0;
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008447
NeilBrown06d91a52005-06-21 17:17:12 -07008448 if (bio_data_dir(bi) != WRITE)
NeilBrowncc27b0c2017-06-05 16:49:39 +10008449 return true;
NeilBrown06d91a52005-06-21 17:17:12 -07008450
NeilBrownf91de922005-11-08 21:39:36 -08008451 BUG_ON(mddev->ro == 1);
8452 if (mddev->ro == 2) {
8453 /* need to switch to read/write */
8454 mddev->ro = 0;
8455 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8456 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08008457 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008458 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08008459 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008460 rcu_read_lock();
8461 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11008462 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07008463 if (mddev->safemode == 1)
8464 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11008465 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
NeilBrown81fe48e2017-08-08 16:56:36 +10008466 if (mddev->in_sync || mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11008467 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07008468 if (mddev->in_sync) {
8469 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008470 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8471 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07008472 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008473 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07008474 }
NeilBrown85572d72014-12-15 12:56:56 +11008475 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07008476 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008477 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10008478 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008479 sysfs_notify_dirent_safe(mddev->sysfs_state);
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008480 if (!mddev->has_superblocks)
8481 return true;
NeilBrown09a44cc2008-05-23 13:04:36 -07008482 wait_event(mddev->sb_wait,
NeilBrownd47c8ad2017-10-05 16:23:16 +11008483 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8484 mddev->suspended);
NeilBrowncc27b0c2017-06-05 16:49:39 +10008485 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8486 percpu_ref_put(&mddev->writes_pending);
8487 return false;
8488 }
8489 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008490}
NeilBrown6c144d32014-09-30 16:15:38 +10008491EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008492
NeilBrown49728052017-03-15 14:05:12 +11008493/* md_write_inc can only be called when md_write_start() has
8494 * already been called at least once of the current request.
8495 * It increments the counter and is useful when a single request
8496 * is split into several parts. Each part causes an increment and
8497 * so needs a matching md_write_end().
8498 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8499 * a spinlocked region.
8500 */
8501void md_write_inc(struct mddev *mddev, struct bio *bi)
8502{
8503 if (bio_data_dir(bi) != WRITE)
8504 return;
8505 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11008506 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008507}
8508EXPORT_SYMBOL(md_write_inc);
8509
NeilBrownfd01b882011-10-11 16:47:53 +11008510void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008511{
NeilBrown4ad23a972017-03-15 14:05:14 +11008512 percpu_ref_put(&mddev->writes_pending);
8513
8514 if (mddev->safemode == 2)
8515 md_wakeup_thread(mddev->thread);
8516 else if (mddev->safemode_delay)
8517 /* The roundup() ensures this only performs locking once
8518 * every ->safemode_delay jiffies
8519 */
8520 mod_timer(&mddev->safemode_timer,
8521 roundup(jiffies, mddev->safemode_delay) +
8522 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523}
NeilBrown4ad23a972017-03-15 14:05:14 +11008524
NeilBrown6c144d32014-09-30 16:15:38 +10008525EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008526
NeilBrown2a2275d2007-01-26 00:57:11 -08008527/* md_allow_write(mddev)
8528 * Calling this ensures that the array is marked 'active' so that writes
8529 * may proceed without blocking. It is important to call this before
8530 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8531 * Must be called with mddev_lock held.
8532 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008533void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008534{
8535 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008536 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008537 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008538 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008539 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008540 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008541
NeilBrown85572d72014-12-15 12:56:56 +11008542 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008543 if (mddev->in_sync) {
8544 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008545 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8546 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008547 if (mddev->safemode_delay &&
8548 mddev->safemode == 0)
8549 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008550 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008551 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008552 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008553 /* wait for the dirty state to be recorded in the metadata */
8554 wait_event(mddev->sb_wait,
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008555 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008556 } else
NeilBrown85572d72014-12-15 12:56:56 +11008557 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008558}
8559EXPORT_SYMBOL_GPL(md_allow_write);
8560
Linus Torvalds1da177e2005-04-16 15:20:36 -07008561#define SYNC_MARKS 10
8562#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008563#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008564void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008565{
Shaohua Li4ed87312012-10-11 13:34:00 +11008566 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008567 struct mddev *mddev2;
Yufen Yue5b521e2019-06-14 15:41:07 -07008568 unsigned int currspeed = 0, window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008569 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008570 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008571 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008572 sector_t mark_cnt[SYNC_MARKS];
8573 int last_mark,m;
8574 struct list_head *tmp;
8575 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008576 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008577 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008578 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008579 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008580 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008581
8582 /* just incase thread restarts... */
Song Liud5d885f2017-11-19 22:17:01 -08008583 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8584 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008585 return;
NeilBrown3991b312014-05-28 13:39:23 +10008586 if (mddev->ro) {/* never try to sync a read-only array */
8587 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008588 return;
NeilBrown3991b312014-05-28 13:39:23 +10008589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008591 if (mddev_is_clustered(mddev)) {
8592 ret = md_cluster_ops->resync_start(mddev);
8593 if (ret)
8594 goto skip;
8595
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008596 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008597 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8598 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8599 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8600 && ((unsigned long long)mddev->curr_resync_completed
8601 < (unsigned long long)mddev->resync_max_sectors))
8602 goto skip;
8603 }
8604
NeilBrown61df9d92006-10-03 01:15:57 -07008605 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008606 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008607 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008608 action = "check";
8609 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008610 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008611 action = "repair";
8612 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008613 desc = "resync";
8614 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8615 desc = "reshape";
8616 else
8617 desc = "recovery";
8618
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008619 mddev->last_sync_action = action ?: desc;
8620
Linus Torvalds1da177e2005-04-16 15:20:36 -07008621 /* we overload curr_resync somewhat here.
8622 * 0 == not engaged in resync at all
8623 * 2 == checking that there is no conflict with another sync
8624 * 1 == like 2, but have yielded to allow conflicting resync to
Yufen Yue5b521e2019-06-14 15:41:07 -07008625 * commence
Linus Torvalds1da177e2005-04-16 15:20:36 -07008626 * other == active in resync - this many blocks
8627 *
8628 * Before starting a resync we must have set curr_resync to
8629 * 2, and then checked that every "conflicting" array has curr_resync
8630 * less than ours. When we find one that is the same or higher
8631 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8632 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8633 * This will mean we have to start checking from the beginning again.
8634 *
8635 */
8636
8637 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008638 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639 mddev->curr_resync = 2;
8640
8641 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008642 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008643 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008644 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645 if (mddev2 == mddev)
8646 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008647 if (!mddev->parallel_resync
8648 && mddev2->curr_resync
8649 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008650 DEFINE_WAIT(wq);
8651 if (mddev < mddev2 && mddev->curr_resync == 2) {
8652 /* arbitrarily yield */
8653 mddev->curr_resync = 1;
8654 wake_up(&resync_wait);
8655 }
8656 if (mddev > mddev2 && mddev->curr_resync == 1)
8657 /* no need to wait here, we can wait the next
8658 * time 'round when curr_resync == 2
8659 */
8660 continue;
NeilBrown97441972008-09-19 11:49:54 +10008661 /* We need to wait 'interruptible' so as not to
8662 * contribute to the load average, and not to
8663 * be caught by 'softlockup'
8664 */
8665 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008666 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008667 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008668 if (mddev2_minor != mddev2->md_minor) {
8669 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008670 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8671 desc, mdname(mddev),
8672 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008674 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008675 if (signal_pending(current))
8676 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008677 schedule();
8678 finish_wait(&resync_wait, &wq);
8679 goto try_again;
8680 }
8681 finish_wait(&resync_wait, &wq);
8682 }
8683 }
8684 } while (mddev->curr_resync < 2);
8685
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008686 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008687 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008688 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008689 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008690 */
8691 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008692 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008693 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008694 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8695 j = mddev->resync_min;
8696 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008697 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008698
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008699 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
NeilBrownc804cde2012-05-21 09:28:33 +10008700 max_sectors = mddev->resync_max_sectors;
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008701 /*
8702 * If the original node aborts reshaping then we continue the
8703 * reshaping, so set j again to avoid restart reshape from the
8704 * first beginning
8705 */
8706 if (mddev_is_clustered(mddev) &&
8707 mddev->reshape_position != MaxSector)
8708 j = mddev->reshape_position;
8709 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008710 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008711 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008712 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008713 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008714 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008715 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008716 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008717 !test_bit(Faulty, &rdev->flags) &&
8718 !test_bit(In_sync, &rdev->flags) &&
8719 rdev->recovery_offset < j)
8720 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008721 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008722
8723 /* If there is a bitmap, we need to make sure all
8724 * writes that started before we added a spare
8725 * complete before we start doing a recovery.
8726 * Otherwise the write might complete and (via
8727 * bitmap_endwrite) set a bit in the bitmap after the
8728 * recovery has checked that bit and skipped that
8729 * region.
8730 */
8731 if (mddev->bitmap) {
8732 mddev->pers->quiesce(mddev, 1);
8733 mddev->pers->quiesce(mddev, 0);
8734 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008736
NeilBrown9d487392016-11-02 14:16:49 +11008737 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8738 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8739 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8740 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008741
NeilBrowneea1bf32009-03-31 14:27:02 +11008742 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008743
NeilBrown57afd892005-06-21 17:17:13 -07008744 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008745 for (m = 0; m < SYNC_MARKS; m++) {
8746 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008747 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008748 }
8749 last_mark = 0;
8750 mddev->resync_mark = mark[last_mark];
8751 mddev->resync_mark_cnt = mark_cnt[last_mark];
8752
8753 /*
8754 * Tune reconstruction:
8755 */
Yufen Yue5b521e2019-06-14 15:41:07 -07008756 window = 32 * (PAGE_SIZE / 512);
NeilBrown9d487392016-11-02 14:16:49 +11008757 pr_debug("md: using %dk window, over a total of %lluk.\n",
8758 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008759
8760 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008761 last_check = 0;
8762
8763 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008764 pr_debug("md: resuming %s of %s from checkpoint.\n",
8765 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008766 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008767 } else
8768 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008769 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008770 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8771 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008772 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773
majianpeng7c2c57c2012-07-03 12:12:26 +10008774 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008775 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008776 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008777
NeilBrown57afd892005-06-21 17:17:13 -07008778 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008779
NeilBrown7a91ee12009-05-26 12:57:21 +10008780 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8781 ((mddev->curr_resync > mddev->curr_resync_completed &&
8782 (mddev->curr_resync - mddev->curr_resync_completed)
8783 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008784 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008785 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008786 >= mddev->resync_max - mddev->curr_resync_completed ||
8787 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008788 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008789 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008790 wait_event(mddev->recovery_wait,
8791 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008792 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008793 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8794 j > mddev->recovery_cp)
8795 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008796 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008797 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008798 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008799 }
NeilBrownacb180b2009-04-14 16:28:34 +10008800
NeilBrownc91abf52013-11-19 12:02:01 +11008801 while (j >= mddev->resync_max &&
8802 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008803 /* As this condition is controlled by user-space,
8804 * we can block indefinitely, so use '_interruptible'
8805 * to avoid triggering warnings.
8806 */
8807 flush_signals(current); /* just in case */
8808 wait_event_interruptible(mddev->recovery_wait,
8809 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008810 || test_bit(MD_RECOVERY_INTR,
8811 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008812 }
NeilBrownacb180b2009-04-14 16:28:34 +10008813
NeilBrownc91abf52013-11-19 12:02:01 +11008814 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8815 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008816
NeilBrown09314792015-02-19 16:04:40 +11008817 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008818 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008819 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008820 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008821 }
NeilBrown57afd892005-06-21 17:17:13 -07008822
8823 if (!skipped) { /* actual IO requested */
8824 io_sectors += sectors;
8825 atomic_add(sectors, &mddev->recovery_active);
8826 }
8827
NeilBrowne875ece2011-07-28 11:39:24 +10008828 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8829 break;
8830
Linus Torvalds1da177e2005-04-16 15:20:36 -07008831 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008832 if (j > max_sectors)
8833 /* when skipping, extra large numbers can be returned. */
8834 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008835 if (j > 2)
8836 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008837 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008838 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008839 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008840 * visible in /proc/mdstat
8841 */
8842 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008843
8844 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008845 continue;
8846
NeilBrown57afd892005-06-21 17:17:13 -07008847 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008848 repeat:
8849 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8850 /* step marks */
8851 int next = (last_mark+1) % SYNC_MARKS;
8852
8853 mddev->resync_mark = mark[next];
8854 mddev->resync_mark_cnt = mark_cnt[next];
8855 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008856 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008857 last_mark = next;
8858 }
8859
NeilBrownc91abf52013-11-19 12:02:01 +11008860 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8861 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008862
8863 /*
8864 * this loop exits only if either when we are slower than
8865 * the 'hard' speed limit, or the system was IO-idle for
8866 * a jiffy.
8867 * the system might be non-idle CPU-wise, but we only care
8868 * about not overloading the IO subsystem. (things like an
8869 * e2fsck being done on the RAID array should execute fast)
8870 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008871 cond_resched();
8872
Xiao Niac7e50a2014-08-07 09:37:41 -04008873 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8874 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008875 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008876
NeilBrown88202a02006-01-06 00:21:36 -08008877 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008878 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008879 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008880 goto repeat;
8881 }
NeilBrownac8fa412015-02-19 16:55:00 +11008882 if (!is_mddev_idle(mddev, 0)) {
8883 /*
8884 * Give other IO more of a chance.
8885 * The faster the devices, the less we wait.
8886 */
8887 wait_event(mddev->recovery_wait,
8888 !atomic_read(&mddev->recovery_active));
8889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008890 }
8891 }
NeilBrown9d487392016-11-02 14:16:49 +11008892 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8893 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8894 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008895 /*
8896 * this also signals 'finished resyncing' to md_stop
8897 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008898 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008899 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8900
NeilBrown5ed1df22015-07-24 13:27:08 +10008901 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8902 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008903 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008904 mddev->curr_resync_completed = mddev->curr_resync;
8905 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8906 }
NeilBrown09314792015-02-19 16:04:40 +11008907 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008908
NeilBrowndfc70642008-05-23 13:04:39 -07008909 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008910 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008911 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8912 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8913 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008914 pr_debug("md: checkpointing %s of %s.\n",
8915 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008916 if (test_bit(MD_RECOVERY_ERROR,
8917 &mddev->recovery))
8918 mddev->recovery_cp =
8919 mddev->curr_resync_completed;
8920 else
8921 mddev->recovery_cp =
8922 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008923 }
8924 } else
8925 mddev->recovery_cp = MaxSector;
8926 } else {
8927 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8928 mddev->curr_resync = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11008929 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8930 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
8931 rcu_read_lock();
8932 rdev_for_each_rcu(rdev, mddev)
8933 if (rdev->raid_disk >= 0 &&
8934 mddev->delta_disks >= 0 &&
8935 !test_bit(Journal, &rdev->flags) &&
8936 !test_bit(Faulty, &rdev->flags) &&
8937 !test_bit(In_sync, &rdev->flags) &&
8938 rdev->recovery_offset < mddev->curr_resync)
8939 rdev->recovery_offset = mddev->curr_resync;
8940 rcu_read_unlock();
8941 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008943 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008944 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008945 /* set CHANGE_PENDING here since maybe another update is needed,
8946 * so other nodes are informed. It should be harmless for normal
8947 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008948 set_mask_bits(&mddev->sb_flags, 0,
8949 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008950
BingJing Chang88763912018-02-22 13:34:46 +08008951 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8952 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8953 mddev->delta_disks > 0 &&
8954 mddev->pers->finish_reshape &&
8955 mddev->pers->size &&
8956 mddev->queue) {
8957 mddev_lock_nointr(mddev);
8958 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
8959 mddev_unlock(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08008960 if (!mddev_is_clustered(mddev)) {
8961 set_capacity(mddev->gendisk, mddev->array_sectors);
8962 revalidate_disk(mddev->gendisk);
8963 }
BingJing Chang88763912018-02-22 13:34:46 +08008964 }
8965
NeilBrown23da4222014-12-15 12:57:01 +11008966 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008967 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8968 /* We completed so min/max setting can be forgotten if used. */
8969 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8970 mddev->resync_min = 0;
8971 mddev->resync_max = MaxSector;
8972 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8973 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008974 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008975 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008976 spin_unlock(&mddev->lock);
8977
Linus Torvalds1da177e2005-04-16 15:20:36 -07008978 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008979 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008980 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981}
NeilBrown29269552006-03-27 01:18:10 -08008982EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008983
NeilBrown746d3202013-04-24 11:42:41 +10008984static int remove_and_add_spares(struct mddev *mddev,
8985 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008986{
NeilBrown3cb03002011-10-11 16:45:26 +11008987 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008988 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008989 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008990 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008991
NeilBrown39772f02018-02-03 09:19:30 +11008992 if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
8993 /* Mustn't remove devices when resync thread is running */
8994 return 0;
8995
NeilBrownd787be42016-06-02 16:19:53 +10008996 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008997 if ((this == NULL || rdev == this) &&
8998 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008999 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10009000 test_bit(Faulty, &rdev->flags) &&
9001 atomic_read(&rdev->nr_pending)==0) {
9002 /* Faulty non-Blocked devices with nr_pending == 0
9003 * never get nr_pending incremented,
9004 * never get Faulty cleared, and never get Blocked set.
9005 * So we can synchronize_rcu now rather than once per device
9006 */
9007 remove_some = true;
9008 set_bit(RemoveSynchronized, &rdev->flags);
9009 }
9010 }
9011
9012 if (remove_some)
9013 synchronize_rcu();
9014 rdev_for_each(rdev, mddev) {
9015 if ((this == NULL || rdev == this) &&
9016 rdev->raid_disk >= 0 &&
9017 !test_bit(Blocked, &rdev->flags) &&
9018 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07009019 (!test_bit(In_sync, &rdev->flags) &&
9020 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10009021 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009022 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11009023 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10009024 sysfs_unlink_rdev(mddev, rdev);
NeilBrown011abdc2018-04-26 14:46:29 +10009025 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009026 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11009027 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009028 }
9029 }
NeilBrownd787be42016-06-02 16:19:53 +10009030 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
9031 clear_bit(RemoveSynchronized, &rdev->flags);
9032 }
9033
Jonathan Brassow90584fc2013-03-07 16:24:26 -06009034 if (removed && mddev->kobj.sd)
9035 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009036
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05009037 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10009038 goto no_add;
9039
NeilBrowndafb20f2012-03-19 12:46:39 +11009040 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05009041 if (this && this != rdev)
9042 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009043 if (test_bit(Candidate, &rdev->flags))
9044 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11009045 if (rdev->raid_disk >= 0 &&
9046 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07009047 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11009048 !test_bit(Faulty, &rdev->flags))
9049 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009050 if (rdev->raid_disk >= 0)
9051 continue;
9052 if (test_bit(Faulty, &rdev->flags))
9053 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009054 if (!test_bit(Journal, &rdev->flags)) {
9055 if (mddev->ro &&
9056 ! (rdev->saved_raid_disk >= 0 &&
9057 !test_bit(Bitmap_sync, &rdev->flags)))
9058 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009059
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009060 rdev->recovery_offset = 0;
9061 }
Guoqing Jiang3f79cc22020-04-04 23:57:11 +02009062 if (mddev->pers->hot_add_disk(mddev, rdev) == 0) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10009063 if (sysfs_link_rdev(mddev, rdev))
9064 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009065 if (!test_bit(Journal, &rdev->flags))
9066 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009067 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08009068 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07009069 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009070 }
NeilBrown746d3202013-04-24 11:42:41 +10009071no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10009072 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08009073 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009074 return spares;
9075}
NeilBrown7ebc0be2011-01-14 09:14:33 +11009076
NeilBrownac05f252014-09-30 08:10:42 +10009077static void md_start_sync(struct work_struct *ws)
9078{
9079 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05009080
NeilBrownac05f252014-09-30 08:10:42 +10009081 mddev->sync_thread = md_register_thread(md_do_sync,
9082 mddev,
9083 "resync");
9084 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11009085 pr_warn("%s: could not start resync thread...\n",
9086 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10009087 /* leave the spares where they are, it shouldn't hurt */
9088 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9089 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9090 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9091 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9092 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11009093 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10009094 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9095 &mddev->recovery))
9096 if (mddev->sysfs_action)
9097 sysfs_notify_dirent_safe(mddev->sysfs_action);
9098 } else
9099 md_wakeup_thread(mddev->sync_thread);
9100 sysfs_notify_dirent_safe(mddev->sysfs_action);
9101 md_new_event(mddev);
9102}
9103
Linus Torvalds1da177e2005-04-16 15:20:36 -07009104/*
9105 * This routine is regularly called by all per-raid-array threads to
9106 * deal with generic issues like resync and super-block update.
9107 * Raid personalities that don't have a thread (linear/raid0) do not
9108 * need this as they never do any recovery or update the superblock.
9109 *
9110 * It does not do any resync itself, but rather "forks" off other threads
9111 * to do that as needed.
9112 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
9113 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07009114 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07009115 * and wakeups up this thread which will reap the thread and finish up.
9116 * This thread also removes any faulty devices (with nr_pending == 0).
9117 *
9118 * The overall approach is:
9119 * 1/ if the superblock needs updating, update it.
9120 * 2/ If a recovery thread is running, don't do anything else.
9121 * 3/ If recovery has finished, clean up, possibly marking spares active.
9122 * 4/ If there are any faulty devices, remove them.
9123 * 5/ If array is degraded, try to add spares devices
9124 * 6/ If array has spares or is not in-sync, start a resync thread.
9125 */
NeilBrownfd01b882011-10-11 16:47:53 +11009126void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009127{
NeilBrown059421e2018-10-03 15:04:41 +10009128 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
9129 /* Write superblock - thread that called mddev_suspend()
9130 * holds reconfig_mutex for us.
9131 */
9132 set_bit(MD_UPDATING_SB, &mddev->flags);
9133 smp_mb__after_atomic();
9134 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
9135 md_update_sb(mddev, 0);
9136 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
9137 wake_up(&mddev->sb_wait);
9138 }
9139
Jonathan Brassow68866e42011-06-08 15:10:08 +10009140 if (mddev->suspended)
9141 return;
9142
NeilBrown5f404022005-06-21 17:17:16 -07009143 if (mddev->bitmap)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009144 md_bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009145
NeilBrownfca4d842005-06-21 17:17:11 -07009146 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07009147 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11009148 pr_debug("md: %s in immediate safe mode\n",
9149 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07009150 mddev->safemode = 2;
9151 }
9152 flush_signals(current);
9153 }
9154
NeilBrownc89a8ee2008-08-05 15:54:13 +10009155 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
9156 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009157 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08009158 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07009159 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07009160 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07009161 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11009162 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07009163 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009164 ))
9165 return;
NeilBrownfca4d842005-06-21 17:17:11 -07009166
NeilBrowndf5b89b2006-03-27 01:18:20 -08009167 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009168 int spares = 0;
NeilBrown480523f2019-08-20 10:21:09 +10009169 bool try_set_sync = mddev->safemode != 0;
NeilBrownfca4d842005-06-21 17:17:11 -07009170
Shaohua Liafc1f552017-08-11 20:34:45 -07009171 if (!mddev->external && mddev->safemode == 1)
NeilBrown33182d12017-08-08 16:56:36 +10009172 mddev->safemode = 0;
9173
NeilBrownc89a8ee2008-08-05 15:54:13 +10009174 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10009175 struct md_rdev *rdev;
9176 if (!mddev->external && mddev->in_sync)
9177 /* 'Blocked' flag not needed as failed devices
9178 * will be recorded if array switched to read/write.
9179 * Leaving it set will prevent the device
9180 * from being removed.
9181 */
9182 rdev_for_each(rdev, mddev)
9183 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10009184 /* On a read-only array we can:
9185 * - remove failed devices
9186 * - add already-in_sync devices if the array itself
9187 * is in-sync.
9188 * As we only add devices that are already in-sync,
9189 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10009190 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10009191 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11009192 /* There is no thread, but we need to call
9193 * ->spare_active and clear saved_raid_disk
9194 */
NeilBrown2ac295a2014-05-29 11:40:03 +10009195 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11009196 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10009197 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11009198 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08009199 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10009200 goto unlock;
9201 }
9202
Guoqing Jiang659b2542015-12-21 10:50:59 +11009203 if (mddev_is_clustered(mddev)) {
9204 struct md_rdev *rdev;
9205 /* kick the device if another node issued a
9206 * remove disk.
9207 */
9208 rdev_for_each(rdev, mddev) {
9209 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
9210 rdev->raid_disk < 0)
9211 md_kick_rdev_from_array(rdev);
9212 }
9213 }
9214
NeilBrown480523f2019-08-20 10:21:09 +10009215 if (try_set_sync && !mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11009216 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11009217 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11009218 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07009219 }
NeilBrownfca4d842005-06-21 17:17:11 -07009220
Shaohua Li29530792016-12-08 15:48:19 -08009221 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07009222 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07009223
Linus Torvalds1da177e2005-04-16 15:20:36 -07009224 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
9225 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
9226 /* resync/recovery still happening */
9227 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9228 goto unlock;
9229 }
9230 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009231 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009232 goto unlock;
9233 }
Neil Brown72a23c22008-06-28 08:31:41 +10009234 /* Set RUNNING before clearing NEEDED to avoid
9235 * any transients in the value of "sync_action".
9236 */
NeilBrown72f36d52012-10-11 14:25:57 +11009237 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11009238 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10009239 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11009240 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08009241 /* Clear some bits that don't mean anything, but
9242 * might be left set
9243 */
NeilBrown24dd4692005-11-08 21:39:26 -08009244 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
9245 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009246
NeilBrowned209582012-04-24 10:23:14 +10009247 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
9248 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10009249 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009250 /* no recovery is running.
9251 * remove any failed drives, then
9252 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11009253 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07009254 * the personality to fail the re-add.
9255 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009256
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009257 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10009258 if (mddev->pers->check_reshape == NULL ||
9259 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009260 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10009261 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009262 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009263 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10009264 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08009265 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9266 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07009267 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009268 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08009269 } else if (mddev->recovery_cp < MaxSector) {
9270 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009271 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08009272 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
9273 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10009274 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08009275
Linus Torvalds1da177e2005-04-16 15:20:36 -07009276 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10009277 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07009278 /* We are adding a device or devices to an array
9279 * which has the bitmap stored on all devices.
9280 * So make sure all bitmap pages get written
9281 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009282 md_bitmap_write_all(mddev->bitmap);
NeilBrowna654b9d82005-06-21 17:17:27 -07009283 }
NeilBrownac05f252014-09-30 08:10:42 +10009284 INIT_WORK(&mddev->del_work, md_start_sync);
9285 queue_work(md_misc_wq, &mddev->del_work);
9286 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009287 }
NeilBrownac05f252014-09-30 08:10:42 +10009288 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10009289 if (!mddev->sync_thread) {
9290 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11009291 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10009292 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9293 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11009294 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10009295 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10009296 }
NeilBrownac05f252014-09-30 08:10:42 +10009297 unlock:
9298 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009299 mddev_unlock(mddev);
9300 }
9301}
NeilBrown6c144d32014-09-30 16:15:38 +10009302EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009303
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009304void md_reap_sync_thread(struct mddev *mddev)
9305{
9306 struct md_rdev *rdev;
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009307 sector_t old_dev_sectors = mddev->dev_sectors;
9308 bool is_reshaped = false;
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009309
9310 /* resync has finished, collect result */
9311 md_unregister_thread(&mddev->sync_thread);
9312 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
Guoqing Jiang0d8ed0e92019-07-24 11:09:21 +02009313 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
9314 mddev->degraded != mddev->raid_disks) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009315 /* success...*/
9316 /* activate any spares */
9317 if (mddev->pers->spare_active(mddev)) {
9318 sysfs_notify(&mddev->kobj, NULL,
9319 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08009320 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009321 }
9322 }
9323 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009324 mddev->pers->finish_reshape) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009325 mddev->pers->finish_reshape(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009326 if (mddev_is_clustered(mddev))
9327 is_reshaped = true;
9328 }
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009329
9330 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11009331 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009332 */
NeilBrownf4667222013-12-09 12:04:56 +11009333 if (!mddev->degraded)
9334 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009335 rdev->saved_raid_disk = -1;
9336
9337 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08009338 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04009339 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9340 * clustered raid */
9341 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
9342 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009343 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10009344 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009345 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9346 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9347 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9348 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009349 /*
9350 * We call md_cluster_ops->update_size here because sync_size could
9351 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9352 * so it is time to update size across cluster.
9353 */
9354 if (mddev_is_clustered(mddev) && is_reshaped
9355 && !test_bit(MD_CLOSING, &mddev->flags))
9356 md_cluster_ops->update_size(mddev, old_dev_sectors);
NeilBrownf851b602014-12-11 10:02:10 +11009357 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009358 /* flag recovery needed just to double check */
9359 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9360 sysfs_notify_dirent_safe(mddev->sysfs_action);
9361 md_new_event(mddev);
9362 if (mddev->event_work.func)
9363 queue_work(md_misc_wq, &mddev->event_work);
9364}
NeilBrown6c144d32014-09-30 16:15:38 +10009365EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009366
NeilBrownfd01b882011-10-11 16:47:53 +11009367void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07009368{
NeilBrown00bcb4a2010-06-01 19:37:23 +10009369 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07009370 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10009371 !test_bit(Blocked, &rdev->flags) &&
9372 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07009373 msecs_to_jiffies(5000));
9374 rdev_dec_pending(rdev, mddev);
9375}
9376EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9377
NeilBrownc6563a82012-05-21 09:27:00 +10009378void md_finish_reshape(struct mddev *mddev)
9379{
9380 /* called be personality module when reshape completes. */
9381 struct md_rdev *rdev;
9382
9383 rdev_for_each(rdev, mddev) {
9384 if (rdev->data_offset > rdev->new_data_offset)
9385 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9386 else
9387 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9388 rdev->data_offset = rdev->new_data_offset;
9389 }
9390}
9391EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10009392
Vishal Vermafc974ee2015-12-24 19:20:34 -07009393/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10009394
Vishal Vermafc974ee2015-12-24 19:20:34 -07009395/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11009396int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10009397 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009398{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04009399 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10009400 int rv;
9401 if (is_new)
9402 s += rdev->new_data_offset;
9403 else
9404 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07009405 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9406 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10009407 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009408 if (test_bit(ExternalBbl, &rdev->flags))
9409 sysfs_notify(&rdev->kobj, NULL,
9410 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11009411 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08009412 set_mask_bits(&mddev->sb_flags, 0,
9413 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10009414 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07009415 return 1;
9416 } else
9417 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10009418}
9419EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9420
NeilBrownc6563a82012-05-21 09:27:00 +10009421int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9422 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009423{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009424 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10009425 if (is_new)
9426 s += rdev->new_data_offset;
9427 else
9428 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009429 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9430 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
9431 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
9432 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10009433}
9434EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9435
Adrian Bunk75c96f82005-05-05 16:16:09 -07009436static int md_notify_reboot(struct notifier_block *this,
9437 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009438{
9439 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11009440 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009441 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009442
NeilBrownc744a652012-03-19 12:46:37 +11009443 for_each_mddev(mddev, tmp) {
9444 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10009445 if (mddev->pers)
9446 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009447 if (mddev->persistent)
9448 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009449 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009450 }
NeilBrownc744a652012-03-19 12:46:37 +11009451 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452 }
NeilBrownc744a652012-03-19 12:46:37 +11009453 /*
9454 * certain more exotic SCSI devices are known to be
9455 * volatile wrt too early system reboots. While the
9456 * right place to handle this issue is the given
9457 * driver, we do want to have a safe RAID driver ...
9458 */
9459 if (need_delay)
9460 mdelay(1000*1);
9461
Linus Torvalds1da177e2005-04-16 15:20:36 -07009462 return NOTIFY_DONE;
9463}
9464
Adrian Bunk75c96f82005-05-05 16:16:09 -07009465static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009466 .notifier_call = md_notify_reboot,
9467 .next = NULL,
9468 .priority = INT_MAX, /* before any real devices */
9469};
9470
9471static void md_geninit(void)
9472{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009473 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009474
Alexey Dobriyan97a32532020-02-03 17:37:17 -08009475 proc_create("mdstat", S_IRUGO, NULL, &mdstat_proc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009476}
9477
Adrian Bunk75c96f82005-05-05 16:16:09 -07009478static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009479{
Tejun Heoe804ac72010-10-15 15:36:08 +02009480 int ret = -ENOMEM;
9481
Tejun Heoada609e2011-01-25 14:35:54 +01009482 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009483 if (!md_wq)
9484 goto err_wq;
9485
9486 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9487 if (!md_misc_wq)
9488 goto err_misc_wq;
9489
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009490 md_rdev_misc_wq = alloc_workqueue("md_rdev_misc", 0, 0);
9491 if (!md_misc_wq)
9492 goto err_rdev_misc_wq;
9493
Tejun Heoe804ac72010-10-15 15:36:08 +02009494 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9495 goto err_md;
9496
9497 if ((ret = register_blkdev(0, "mdp")) < 0)
9498 goto err_mdp;
9499 mdp_major = ret;
9500
NeilBrownaf5628f2014-07-31 13:54:54 +10009501 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009502 md_probe, NULL, NULL);
9503 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009504 md_probe, NULL, NULL);
9505
Linus Torvalds1da177e2005-04-16 15:20:36 -07009506 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009507 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009508
9509 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009511
Tejun Heoe804ac72010-10-15 15:36:08 +02009512err_mdp:
9513 unregister_blkdev(MD_MAJOR, "md");
9514err_md:
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009515 destroy_workqueue(md_rdev_misc_wq);
9516err_rdev_misc_wq:
Tejun Heoe804ac72010-10-15 15:36:08 +02009517 destroy_workqueue(md_misc_wq);
9518err_misc_wq:
9519 destroy_workqueue(md_wq);
9520err_wq:
9521 return ret;
9522}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009523
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009524static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009525{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009526 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9527 struct md_rdev *rdev2;
9528 int role, ret;
9529 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009530
Guoqing Jiang818da592017-03-01 16:42:40 +08009531 /*
9532 * If size is changed in another node then we need to
9533 * do resize as well.
9534 */
9535 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9536 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9537 if (ret)
9538 pr_info("md-cluster: resize failed\n");
9539 else
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009540 md_bitmap_update_sb(mddev->bitmap);
Guoqing Jiang818da592017-03-01 16:42:40 +08009541 }
9542
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009543 /* Check for change of roles in the active devices */
9544 rdev_for_each(rdev2, mddev) {
9545 if (test_bit(Faulty, &rdev2->flags))
9546 continue;
9547
9548 /* Check if the roles changed */
9549 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009550
9551 if (test_bit(Candidate, &rdev2->flags)) {
9552 if (role == 0xfffe) {
9553 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9554 md_kick_rdev_from_array(rdev2);
9555 continue;
9556 }
9557 else
9558 clear_bit(Candidate, &rdev2->flags);
9559 }
9560
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009561 if (role != rdev2->raid_disk) {
Guoqing Jiangca1e98e2018-10-18 16:37:45 +08009562 /*
9563 * got activated except reshape is happening.
9564 */
9565 if (rdev2->raid_disk == -1 && role != 0xffff &&
9566 !(le32_to_cpu(sb->feature_map) &
9567 MD_FEATURE_RESHAPE_ACTIVE)) {
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009568 rdev2->saved_raid_disk = role;
9569 ret = remove_and_add_spares(mddev, rdev2);
9570 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11009571 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04009572 /* wakeup mddev->thread here, so array could
9573 * perform resync with the new activated disk */
9574 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9575 md_wakeup_thread(mddev->thread);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009576 }
9577 /* device faulty
9578 * We just want to do the minimum to mark the disk
9579 * as faulty. The recovery is performed by the
9580 * one who initiated the error.
9581 */
9582 if ((role == 0xfffe) || (role == 0xfffd)) {
9583 md_error(mddev, rdev2);
9584 clear_bit(Blocked, &rdev2->flags);
9585 }
9586 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009587 }
9588
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009589 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9590 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009591
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009592 /*
9593 * Since mddev->delta_disks has already updated in update_raid_disks,
9594 * so it is time to check reshape.
9595 */
9596 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9597 (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9598 /*
9599 * reshape is happening in the remote node, we need to
9600 * update reshape_position and call start_reshape.
9601 */
Christoph Hellwiged4d0a4e2019-04-04 18:56:10 +02009602 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009603 if (mddev->pers->update_reshape_pos)
9604 mddev->pers->update_reshape_pos(mddev);
9605 if (mddev->pers->start_reshape)
9606 mddev->pers->start_reshape(mddev);
9607 } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9608 mddev->reshape_position != MaxSector &&
9609 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9610 /* reshape is just done in another node. */
9611 mddev->reshape_position = MaxSector;
9612 if (mddev->pers->update_reshape_pos)
9613 mddev->pers->update_reshape_pos(mddev);
9614 }
9615
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009616 /* Finally set the event to be up to date */
9617 mddev->events = le64_to_cpu(sb->events);
9618}
9619
9620static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9621{
9622 int err;
9623 struct page *swapout = rdev->sb_page;
9624 struct mdp_superblock_1 *sb;
9625
9626 /* Store the sb page of the rdev in the swapout temporary
9627 * variable in case we err in the future
9628 */
9629 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009630 err = alloc_disk_sb(rdev);
9631 if (err == 0) {
9632 ClearPageUptodate(rdev->sb_page);
9633 rdev->sb_loaded = 0;
9634 err = super_types[mddev->major_version].
9635 load_super(rdev, NULL, mddev->minor_version);
9636 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009637 if (err < 0) {
9638 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9639 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009640 if (rdev->sb_page)
9641 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009642 rdev->sb_page = swapout;
9643 rdev->sb_loaded = 1;
9644 return err;
9645 }
9646
9647 sb = page_address(rdev->sb_page);
9648 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9649 * is not set
9650 */
9651
9652 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9653 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9654
9655 /* The other node finished recovery, call spare_active to set
9656 * device In_sync and mddev->degraded
9657 */
9658 if (rdev->recovery_offset == MaxSector &&
9659 !test_bit(In_sync, &rdev->flags) &&
9660 mddev->pers->spare_active(mddev))
9661 sysfs_notify(&mddev->kobj, NULL, "degraded");
9662
9663 put_page(swapout);
9664 return 0;
9665}
9666
9667void md_reload_sb(struct mddev *mddev, int nr)
9668{
9669 struct md_rdev *rdev;
9670 int err;
9671
9672 /* Find the rdev */
9673 rdev_for_each_rcu(rdev, mddev) {
9674 if (rdev->desc_nr == nr)
9675 break;
9676 }
9677
9678 if (!rdev || rdev->desc_nr != nr) {
9679 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9680 return;
9681 }
9682
9683 err = read_rdev(mddev, rdev);
9684 if (err < 0)
9685 return;
9686
9687 check_sb_changes(mddev, rdev);
9688
9689 /* Read all rdev's to update recovery_offset */
Guoqing Jiang0ea99242018-04-09 17:01:21 +08009690 rdev_for_each_rcu(rdev, mddev) {
9691 if (!test_bit(Faulty, &rdev->flags))
9692 read_rdev(mddev, rdev);
9693 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009694}
9695EXPORT_SYMBOL(md_reload_sb);
9696
Linus Torvalds1da177e2005-04-16 15:20:36 -07009697#ifndef MODULE
9698
9699/*
9700 * Searches all registered partitions for autorun RAID arrays
9701 * at boot time.
9702 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009703
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009704static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009705static LIST_HEAD(all_detected_devices);
9706struct detected_devices_node {
9707 struct list_head list;
9708 dev_t dev;
9709};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009710
9711void md_autodetect_dev(dev_t dev)
9712{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009713 struct detected_devices_node *node_detected_dev;
9714
9715 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9716 if (node_detected_dev) {
9717 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009718 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009719 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009720 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009722}
9723
Linus Torvalds1da177e2005-04-16 15:20:36 -07009724static void autostart_arrays(int part)
9725{
NeilBrown3cb03002011-10-11 16:45:26 +11009726 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009727 struct detected_devices_node *node_detected_dev;
9728 dev_t dev;
9729 int i_scanned, i_passed;
9730
9731 i_scanned = 0;
9732 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009733
NeilBrown9d487392016-11-02 14:16:49 +11009734 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009736 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009737 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9738 i_scanned++;
9739 node_detected_dev = list_entry(all_detected_devices.next,
9740 struct detected_devices_node, list);
9741 list_del(&node_detected_dev->list);
9742 dev = node_detected_dev->dev;
9743 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009744 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009745 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009746 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009747 if (IS_ERR(rdev))
9748 continue;
9749
NeilBrown403df472014-09-30 15:52:29 +10009750 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009751 continue;
NeilBrown403df472014-09-30 15:52:29 +10009752
NeilBrownd0fae182008-03-04 14:29:31 -08009753 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009754 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009755 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009756 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009757 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009758
NeilBrown9d487392016-11-02 14:16:49 +11009759 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009760
9761 autorun_devices(part);
9762}
9763
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009764#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009765
9766static __exit void md_exit(void)
9767{
NeilBrownfd01b882011-10-11 16:47:53 +11009768 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009769 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009770 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009771
NeilBrownaf5628f2014-07-31 13:54:54 +10009772 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009773 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009774
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009775 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776 unregister_blkdev(mdp_major, "mdp");
9777 unregister_reboot_notifier(&md_notifier);
9778 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009779
9780 /* We cannot unload the modules while some process is
9781 * waiting for us in select() or poll() - wake them up
9782 */
9783 md_unloading = 1;
9784 while (waitqueue_active(&md_event_waiters)) {
9785 /* not safe to leave yet */
9786 wake_up(&md_event_waiters);
9787 msleep(delay);
9788 delay += delay;
9789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009790 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009791
NeilBrown29ac4aa2008-02-06 01:39:58 -08009792 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009793 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009794 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009795 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009796 /*
9797 * for_each_mddev() will call mddev_put() at the end of each
9798 * iteration. As the mddev is now fully clear, this will
9799 * schedule the mddev for destruction by a workqueue, and the
9800 * destroy_workqueue() below will wait for that to complete.
9801 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009802 }
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009803 destroy_workqueue(md_rdev_misc_wq);
Tejun Heoe804ac72010-10-15 15:36:08 +02009804 destroy_workqueue(md_misc_wq);
9805 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009806}
9807
Dan Williams685784a2007-07-09 11:56:42 -07009808subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009809module_exit(md_exit)
9810
Kees Cooke4dca7b2017-10-17 19:04:42 -07009811static int get_ro(char *buffer, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009812{
Xiongfeng Wang3f999802020-05-11 16:23:25 +08009813 return sprintf(buffer, "%d\n", start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009814}
Kees Cooke4dca7b2017-10-17 19:04:42 -07009815static int set_ro(const char *val, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009816{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009817 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009818}
9819
NeilBrown80ca3a42006-07-10 04:44:18 -07009820module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9821module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009822module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009823module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009824
Linus Torvalds1da177e2005-04-16 15:20:36 -07009825MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009826MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009827MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009828MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);