blob: 9c69084cae7388172930bb61cc3ce23be1c04bf7 [file] [log] [blame]
Thomas Gleixneraf1a8892019-05-20 19:08:12 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 md.c : Multiple Devices driver for Linux
NeilBrownf72ffdd2014-09-30 14:23:59 +10004 Copyright (C) 1998, 1999, 2000 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
6 completely rewritten, based on the MD driver code from Marc Zyngier
7
8 Changes:
9
10 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
11 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
12 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
13 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
14 - kmod support by: Cyrus Durgin
15 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
16 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
17
18 - lots of fixes and improvements to the RAID1/RAID5 and generic
19 RAID code (such as request based resynchronization):
20
21 Neil Brown <neilb@cse.unsw.edu.au>.
22
NeilBrown32a76272005-06-21 17:17:14 -070023 - persistent bitmap code
24 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
25
NeilBrown9d487392016-11-02 14:16:49 +110026
27 Errors, Warnings, etc.
28 Please use:
29 pr_crit() for error conditions that risk data loss
30 pr_err() for error conditions that are unexpected, like an IO error
31 or internal inconsistency
32 pr_warn() for error conditions that could have been predicated, like
33 adding a device to an array when it has incompatible metadata
34 pr_info() for every interesting, very rare events, like an array starting
35 or stopping, or resync starting or stopping
36 pr_debug() for everything else.
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038*/
39
Guoqing Jiang963c5552019-06-14 17:10:36 +080040#include <linux/sched/mm.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010041#include <linux/sched/signal.h>
NeilBrowna6fb0932005-09-09 16:23:56 -070042#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110043#include <linux/blkdev.h>
Vishal Vermafc974ee2015-12-24 19:20:34 -070044#include <linux/badblocks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040047#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080048#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070049#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080050#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110051#include <linux/hdreg.h>
52#include <linux/proc_fs.h>
53#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040054#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110055#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070056#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110057#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110058#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110059#include <linux/raid/md_p.h>
60#include <linux/raid/md_u.h>
Christoph Hellwig74cc979c2020-03-24 08:25:19 +010061#include <linux/raid/detect.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090062#include <linux/slab.h>
NeilBrown4ad23a972017-03-15 14:05:14 +110063#include <linux/percpu-refcount.h>
Christoph Hellwigc6a564ff2020-03-25 16:48:42 +010064#include <linux/part_stat.h>
NeilBrown4ad23a972017-03-15 14:05:14 +110065
Shaohua Li504634f2016-11-18 09:44:08 -080066#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110067#include "md.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040068#include "md-bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050069#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110072static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#endif
74
NeilBrown01f96c02011-09-21 15:30:20 +100075/* pers_list is a list of registered personalities protected
76 * by pers_lock.
77 * pers_lock does extra service to protect accesses to
78 * mddev->thread when the mutex cannot be held.
79 */
NeilBrown2604b702006-01-06 00:20:36 -080080static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static DEFINE_SPINLOCK(pers_lock);
82
Kent Overstreet28dec872018-06-07 20:52:54 -040083static struct kobj_type md_ktype;
84
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050085struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050086EXPORT_SYMBOL(md_cluster_ops);
Christoph Hellwig2b598ee2019-04-04 18:56:14 +020087static struct module *md_cluster_mod;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050088
Bernd Schubert90b08712008-05-23 13:04:38 -070089static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020090static struct workqueue_struct *md_wq;
91static struct workqueue_struct *md_misc_wq;
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +020092static struct workqueue_struct *md_rdev_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070093
NeilBrown746d3202013-04-24 11:42:41 +100094static int remove_and_add_spares(struct mddev *mddev,
95 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110096static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
Robert Becker1e509152009-12-14 12:49:58 +110099 * Default number of read corrections we'll attempt on an rdev
100 * before ejecting it from the array. We divide the read error
101 * count by 2 for every hour elapsed between read errors.
102 */
103#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
Zhao Heming7c9d5c52020-07-21 02:08:52 +0800104/* Default safemode delay: 200 msec */
105#define DEFAULT_SAFEMODE_DELAY ((200 * HZ)/1000 +1)
Robert Becker1e509152009-12-14 12:49:58 +1100106/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
108 * is 1000 KB/sec, so the extra system load does not show up that much.
109 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -0700110 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * subsystem is idle. There is also an 'absolute maximum' reconstruction
112 * speed limit - in case reconstruction slows down your system despite
113 * idle IO detection.
114 *
115 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -0800116 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
118
119static int sysctl_speed_limit_min = 1000;
120static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100121static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800122{
123 return mddev->sync_speed_min ?
124 mddev->sync_speed_min : sysctl_speed_limit_min;
125}
126
NeilBrownfd01b882011-10-11 16:47:53 +1100127static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800128{
129 return mddev->sync_speed_max ?
130 mddev->sync_speed_max : sysctl_speed_limit_max;
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100133static void rdev_uninit_serial(struct md_rdev *rdev)
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800134{
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100135 if (!test_and_clear_bit(CollisionCheck, &rdev->flags))
136 return;
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800137
Guoqing Jiang025471f2019-12-23 10:49:01 +0100138 kvfree(rdev->serial);
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100139 rdev->serial = NULL;
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800140}
141
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100142static void rdevs_uninit_serial(struct mddev *mddev)
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100143{
144 struct md_rdev *rdev;
145
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100146 rdev_for_each(rdev, mddev)
147 rdev_uninit_serial(rdev);
148}
149
150static int rdev_init_serial(struct md_rdev *rdev)
151{
Guoqing Jiang025471f2019-12-23 10:49:01 +0100152 /* serial_nums equals with BARRIER_BUCKETS_NR */
153 int i, serial_nums = 1 << ((PAGE_SHIFT - ilog2(sizeof(atomic_t))));
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100154 struct serial_in_rdev *serial = NULL;
155
156 if (test_bit(CollisionCheck, &rdev->flags))
157 return 0;
158
Guoqing Jiang025471f2019-12-23 10:49:01 +0100159 serial = kvmalloc(sizeof(struct serial_in_rdev) * serial_nums,
160 GFP_KERNEL);
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100161 if (!serial)
162 return -ENOMEM;
163
Guoqing Jiang025471f2019-12-23 10:49:01 +0100164 for (i = 0; i < serial_nums; i++) {
165 struct serial_in_rdev *serial_tmp = &serial[i];
166
167 spin_lock_init(&serial_tmp->serial_lock);
168 serial_tmp->serial_rb = RB_ROOT_CACHED;
169 init_waitqueue_head(&serial_tmp->serial_io_wait);
170 }
171
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100172 rdev->serial = serial;
173 set_bit(CollisionCheck, &rdev->flags);
174
175 return 0;
176}
177
178static int rdevs_init_serial(struct mddev *mddev)
179{
180 struct md_rdev *rdev;
181 int ret = 0;
182
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100183 rdev_for_each(rdev, mddev) {
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100184 ret = rdev_init_serial(rdev);
185 if (ret)
186 break;
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100187 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100188
189 /* Free all resources if pool is not existed */
190 if (ret && !mddev->serial_info_pool)
191 rdevs_uninit_serial(mddev);
192
193 return ret;
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100194}
195
Guoqing Jiang963c5552019-06-14 17:10:36 +0800196/*
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100197 * rdev needs to enable serial stuffs if it meets the conditions:
198 * 1. it is multi-queue device flaged with writemostly.
199 * 2. the write-behind mode is enabled.
200 */
201static int rdev_need_serial(struct md_rdev *rdev)
202{
203 return (rdev && rdev->mddev->bitmap_info.max_write_behind > 0 &&
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200204 rdev->bdev->bd_disk->queue->nr_hw_queues != 1 &&
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100205 test_bit(WriteMostly, &rdev->flags));
206}
207
208/*
209 * Init resource for rdev(s), then create serial_info_pool if:
210 * 1. rdev is the first device which return true from rdev_enable_serial.
211 * 2. rdev is NULL, means we want to enable serialization for all rdevs.
Guoqing Jiang963c5552019-06-14 17:10:36 +0800212 */
Guoqing Jiang404659c2019-12-23 10:48:53 +0100213void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev,
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100214 bool is_suspend)
Guoqing Jiang963c5552019-06-14 17:10:36 +0800215{
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100216 int ret = 0;
217
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100218 if (rdev && !rdev_need_serial(rdev) &&
219 !test_bit(CollisionCheck, &rdev->flags))
Guoqing Jiang963c5552019-06-14 17:10:36 +0800220 return;
221
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100222 if (!is_suspend)
223 mddev_suspend(mddev);
224
225 if (!rdev)
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100226 ret = rdevs_init_serial(mddev);
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100227 else
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100228 ret = rdev_init_serial(rdev);
229 if (ret)
230 goto abort;
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100231
Guoqing Jiang404659c2019-12-23 10:48:53 +0100232 if (mddev->serial_info_pool == NULL) {
Coly Li3024ba22020-04-09 22:17:23 +0800233 /*
234 * already in memalloc noio context by
235 * mddev_suspend()
236 */
Guoqing Jiang404659c2019-12-23 10:48:53 +0100237 mddev->serial_info_pool =
238 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
239 sizeof(struct serial_info));
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100240 if (!mddev->serial_info_pool) {
241 rdevs_uninit_serial(mddev);
Guoqing Jiang404659c2019-12-23 10:48:53 +0100242 pr_err("can't alloc memory pool for serialization\n");
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100243 }
Guoqing Jiang963c5552019-06-14 17:10:36 +0800244 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100245
246abort:
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100247 if (!is_suspend)
248 mddev_resume(mddev);
Guoqing Jiang963c5552019-06-14 17:10:36 +0800249}
Guoqing Jiang963c5552019-06-14 17:10:36 +0800250
251/*
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100252 * Free resource from rdev(s), and destroy serial_info_pool under conditions:
253 * 1. rdev is the last device flaged with CollisionCheck.
254 * 2. when bitmap is destroyed while policy is not enabled.
255 * 3. for disable policy, the pool is destroyed only when no rdev needs it.
Guoqing Jiang963c5552019-06-14 17:10:36 +0800256 */
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100257void mddev_destroy_serial_pool(struct mddev *mddev, struct md_rdev *rdev,
258 bool is_suspend)
Guoqing Jiang963c5552019-06-14 17:10:36 +0800259{
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100260 if (rdev && !test_bit(CollisionCheck, &rdev->flags))
Guoqing Jiang963c5552019-06-14 17:10:36 +0800261 return;
262
Guoqing Jiang404659c2019-12-23 10:48:53 +0100263 if (mddev->serial_info_pool) {
Guoqing Jiang963c5552019-06-14 17:10:36 +0800264 struct md_rdev *temp;
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100265 int num = 0; /* used to track if other rdevs need the pool */
Guoqing Jiang963c5552019-06-14 17:10:36 +0800266
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100267 if (!is_suspend)
268 mddev_suspend(mddev);
269 rdev_for_each(temp, mddev) {
270 if (!rdev) {
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100271 if (!mddev->serialize_policy ||
272 !rdev_need_serial(temp))
273 rdev_uninit_serial(temp);
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100274 else
275 num++;
276 } else if (temp != rdev &&
277 test_bit(CollisionCheck, &temp->flags))
Guoqing Jiang963c5552019-06-14 17:10:36 +0800278 num++;
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100279 }
280
281 if (rdev)
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100282 rdev_uninit_serial(rdev);
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100283
284 if (num)
285 pr_info("The mempool could be used by other devices\n");
286 else {
Guoqing Jiang404659c2019-12-23 10:48:53 +0100287 mempool_destroy(mddev->serial_info_pool);
288 mddev->serial_info_pool = NULL;
Guoqing Jiang963c5552019-06-14 17:10:36 +0800289 }
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100290 if (!is_suspend)
291 mddev_resume(mddev);
Guoqing Jiang963c5552019-06-14 17:10:36 +0800292 }
293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295static struct ctl_table_header *raid_table_header;
296
Joe Perches82592c32013-11-14 15:16:18 +1100297static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 .procname = "speed_limit_min",
300 .data = &sysctl_speed_limit_min,
301 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700302 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800303 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 },
305 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 .procname = "speed_limit_max",
307 .data = &sysctl_speed_limit_max,
308 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700309 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800310 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800312 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313};
314
Joe Perches82592c32013-11-14 15:16:18 +1100315static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 .procname = "raid",
318 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700319 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 .child = raid_table,
321 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800322 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323};
324
Joe Perches82592c32013-11-14 15:16:18 +1100325static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 .procname = "dev",
328 .maxlen = 0,
329 .mode = 0555,
330 .child = raid_dir_table,
331 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800332 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333};
334
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700335static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
NeilBrownf91de922005-11-08 21:39:36 -0800337static int start_readonly;
338
NeilBrown78b63502017-04-12 16:26:13 +1000339/*
340 * The original mechanism for creating an md device is to create
341 * a device node in /dev and to open it. This causes races with device-close.
342 * The preferred method is to write to the "new_array" module parameter.
343 * This can avoid races.
344 * Setting create_on_open to false disables the original mechanism
345 * so all the races disappear.
346 */
347static bool create_on_open = true;
348
NeilBrowna167f662010-10-26 18:31:13 +1100349struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100350 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100351{
Kent Overstreetafeee512018-05-20 18:25:52 -0400352 if (!mddev || !bioset_initialized(&mddev->bio_set))
NeilBrowna167f662010-10-26 18:31:13 +1100353 return bio_alloc(gfp_mask, nr_iovecs);
354
Marcos Paulo de Souza62516912019-01-14 06:31:56 -0700355 return bio_alloc_bioset(gfp_mask, nr_iovecs, &mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100356}
357EXPORT_SYMBOL_GPL(bio_alloc_mddev);
358
NeilBrown5a850712017-06-21 09:12:21 +1000359static struct bio *md_bio_alloc_sync(struct mddev *mddev)
360{
Kent Overstreetafeee512018-05-20 18:25:52 -0400361 if (!mddev || !bioset_initialized(&mddev->sync_set))
NeilBrown5a850712017-06-21 09:12:21 +1000362 return bio_alloc(GFP_NOIO, 1);
363
Kent Overstreetafeee512018-05-20 18:25:52 -0400364 return bio_alloc_bioset(GFP_NOIO, 1, &mddev->sync_set);
NeilBrown5a850712017-06-21 09:12:21 +1000365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/*
NeilBrownd7603b72006-01-06 00:20:30 -0800368 * We have a system wide 'event count' that is incremented
369 * on any 'interesting' event, and readers of /proc/mdstat
370 * can use 'poll' or 'select' to find out when the event
371 * count increases.
372 *
373 * Events are:
374 * start array, stop array, error, add device, remove device,
375 * start build, activate spare
376 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800377static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800378static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100379void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800380{
381 atomic_inc(&md_event_count);
382 wake_up(&md_event_waiters);
383}
NeilBrown29269552006-03-27 01:18:10 -0800384EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800385
386/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 * Enables to iterate over all existing md arrays
388 * all_mddevs_lock protects this list.
389 */
390static LIST_HEAD(all_mddevs);
391static DEFINE_SPINLOCK(all_mddevs_lock);
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/*
394 * iterates through all used mddevs in the system.
395 * We take care to grab the all_mddevs_lock whenever navigating
396 * the list, and to always hold a refcount when unlocked.
397 * Any code which breaks out of this loop while own
398 * a reference to the current mddev and must mddev_put it.
399 */
NeilBrownfd01b882011-10-11 16:47:53 +1100400#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000402 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100403 _tmp = all_mddevs.next; \
404 _mddev = NULL;}); \
405 ({ if (_tmp != &all_mddevs) \
406 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100408 if (_mddev) mddev_put(_mddev); \
409 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
410 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100412 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 )
414
NeilBrown409c57f2009-03-31 14:39:39 +1100415/* Rather than calling directly into the personality make_request function,
416 * IO requests come here first so that we can check if the device is
417 * being suspended pending a reconfiguration.
418 * We hold a refcount over the call to ->make_request. By the time that
419 * call has finished, the bio has been linked into some internal structure
420 * and so is visible to ->quiesce(), so we don't need the refcount any more.
421 */
NeilBrownb3143b92017-10-17 13:46:43 +1100422static bool is_suspended(struct mddev *mddev, struct bio *bio)
423{
424 if (mddev->suspended)
425 return true;
426 if (bio_data_dir(bio) != WRITE)
427 return false;
428 if (mddev->suspend_lo >= mddev->suspend_hi)
429 return false;
430 if (bio->bi_iter.bi_sector >= mddev->suspend_hi)
431 return false;
432 if (bio_end_sector(bio) < mddev->suspend_lo)
433 return false;
434 return true;
435}
436
Shaohua Li393debc2017-09-21 10:23:35 -0700437void md_handle_request(struct mddev *mddev, struct bio *bio)
438{
439check_suspended:
440 rcu_read_lock();
NeilBrownb3143b92017-10-17 13:46:43 +1100441 if (is_suspended(mddev, bio)) {
Shaohua Li393debc2017-09-21 10:23:35 -0700442 DEFINE_WAIT(__wait);
443 for (;;) {
444 prepare_to_wait(&mddev->sb_wait, &__wait,
445 TASK_UNINTERRUPTIBLE);
NeilBrownb3143b92017-10-17 13:46:43 +1100446 if (!is_suspended(mddev, bio))
Shaohua Li393debc2017-09-21 10:23:35 -0700447 break;
448 rcu_read_unlock();
449 schedule();
450 rcu_read_lock();
451 }
452 finish_wait(&mddev->sb_wait, &__wait);
453 }
454 atomic_inc(&mddev->active_io);
455 rcu_read_unlock();
456
457 if (!mddev->pers->make_request(mddev, bio)) {
458 atomic_dec(&mddev->active_io);
459 wake_up(&mddev->sb_wait);
460 goto check_suspended;
461 }
462
463 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
464 wake_up(&mddev->sb_wait);
465}
466EXPORT_SYMBOL(md_handle_request);
467
Artur Paszkiewicz41d2d842020-07-03 11:13:09 +0200468struct md_io {
469 struct mddev *mddev;
470 bio_end_io_t *orig_bi_end_io;
471 void *orig_bi_private;
472 unsigned long start_time;
473};
474
475static void md_end_io(struct bio *bio)
476{
477 struct md_io *md_io = bio->bi_private;
478 struct mddev *mddev = md_io->mddev;
479
480 disk_end_io_acct(mddev->gendisk, bio_op(bio), md_io->start_time);
481
482 bio->bi_end_io = md_io->orig_bi_end_io;
483 bio->bi_private = md_io->orig_bi_private;
484
485 mempool_free(md_io, &mddev->md_io_pool);
486
487 if (bio->bi_end_io)
488 bio->bi_end_io(bio);
489}
490
Christoph Hellwigc62b37d2020-07-01 10:59:43 +0200491static blk_qc_t md_submit_bio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
NeilBrown49077322010-03-25 16:20:56 +1100493 const int rw = bio_data_dir(bio);
Christoph Hellwige4fc5a72020-05-08 18:15:14 +0200494 struct mddev *mddev = bio->bi_disk->private_data;
NeilBrown49077322010-03-25 16:20:56 +1100495
Colin Ian King9a5a8592020-07-02 12:35:02 +0100496 if (mddev == NULL || mddev->pers == NULL) {
497 bio_io_error(bio);
498 return BLK_QC_T_NONE;
499 }
500
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -0300501 if (unlikely(test_bit(MD_BROKEN, &mddev->flags)) && (rw == WRITE)) {
502 bio_io_error(bio);
503 return BLK_QC_T_NONE;
504 }
505
Christoph Hellwigf695ca32020-07-01 10:59:39 +0200506 blk_queue_split(&bio);
Kent Overstreet54efd502015-04-23 22:37:18 -0700507
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100508 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200509 if (bio_sectors(bio) != 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200510 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200511 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700512 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100513 }
NeilBrown49077322010-03-25 16:20:56 +1100514
Artur Paszkiewicz41d2d842020-07-03 11:13:09 +0200515 if (bio->bi_end_io != md_end_io) {
516 struct md_io *md_io;
517
518 md_io = mempool_alloc(&mddev->md_io_pool, GFP_NOIO);
519 md_io->mddev = mddev;
520 md_io->orig_bi_end_io = bio->bi_end_io;
521 md_io->orig_bi_private = bio->bi_private;
522
523 bio->bi_end_io = md_end_io;
524 bio->bi_private = md_io;
525
526 md_io->start_time = disk_start_io_acct(mddev->gendisk,
527 bio_sectors(bio),
528 bio_op(bio));
529 }
530
Shaohua Li9c573de2016-04-25 16:52:38 -0700531 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600532 bio->bi_opf &= ~REQ_NOMERGE;
Shaohua Li393debc2017-09-21 10:23:35 -0700533
534 md_handle_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100535
Jens Axboedece1632015-11-05 10:41:16 -0700536 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
NeilBrown9e35b992010-04-06 14:23:02 +1000539/* mddev_suspend makes sure no new requests are submitted
540 * to the device, and that any requests that have been submitted
541 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100542 * Once mddev_detach() is called and completes, the module will be
543 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000544 */
NeilBrownfd01b882011-10-11 16:47:53 +1100545void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100546{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200547 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
NeilBrown4d5324f2017-10-19 12:17:16 +1100548 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100549 if (mddev->suspended++)
550 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100551 synchronize_rcu();
NeilBrowncc27b0c2017-06-05 16:49:39 +1000552 wake_up(&mddev->sb_wait);
NeilBrown35bfc522017-10-17 13:46:43 +1100553 set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
554 smp_mb__after_atomic();
NeilBrown409c57f2009-03-31 14:39:39 +1100555 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
556 mddev->pers->quiesce(mddev, 1);
NeilBrown35bfc522017-10-17 13:46:43 +1100557 clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags);
558 wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags));
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500559
560 del_timer_sync(&mddev->safemode_timer);
Coly Li78f57ef2020-04-09 22:17:20 +0800561 /* restrict memory reclaim I/O during raid array is suspend */
562 mddev->noio_flag = memalloc_noio_save();
NeilBrown409c57f2009-03-31 14:39:39 +1100563}
NeilBrown390ee602010-06-01 19:37:27 +1000564EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100565
NeilBrownfd01b882011-10-11 16:47:53 +1100566void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100567{
Coly Li78f57ef2020-04-09 22:17:20 +0800568 /* entred the memalloc scope from mddev_suspend() */
569 memalloc_noio_restore(mddev->noio_flag);
NeilBrown4d5324f2017-10-19 12:17:16 +1100570 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100571 if (--mddev->suspended)
572 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100573 wake_up(&mddev->sb_wait);
574 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500575
Jonathan Brassow47525e52012-05-22 13:55:29 +1000576 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500577 md_wakeup_thread(mddev->thread);
578 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100579}
NeilBrown390ee602010-06-01 19:37:27 +1000580EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100581
NeilBrownfd01b882011-10-11 16:47:53 +1100582int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000583{
NeilBrown5c675f82014-12-15 12:56:56 +1100584 struct md_personality *pers = mddev->pers;
585 int ret = 0;
586
587 rcu_read_lock();
588 if (mddev->suspended)
589 ret = 1;
590 else if (pers && pers->congested)
591 ret = pers->congested(mddev, bits);
592 rcu_read_unlock();
593 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000594}
NeilBrown5c675f82014-12-15 12:56:56 +1100595EXPORT_SYMBOL_GPL(mddev_congested);
596static int md_congested(void *data, int bits)
597{
598 struct mddev *mddev = data;
599 return mddev_congested(mddev, bits);
600}
NeilBrown3fa841d2009-09-23 18:10:29 +1000601
NeilBrowna2826aa2009-12-14 12:49:49 +1100602/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200603 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100604 */
NeilBrown4bc034d2019-03-29 10:46:16 -0700605
606static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100607{
NeilBrown4bc034d2019-03-29 10:46:16 -0700608 struct md_rdev *rdev = bio->bi_private;
609 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100610
611 rdev_dec_pending(rdev, mddev);
612
NeilBrown4bc034d2019-03-29 10:46:16 -0700613 if (atomic_dec_and_test(&mddev->flush_pending)) {
614 /* The pre-request flush has finished */
615 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100616 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700617 bio_put(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100618}
619
NeilBrown4bc034d2019-03-29 10:46:16 -0700620static void md_submit_flush_data(struct work_struct *ws);
621
622static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100623{
NeilBrown4bc034d2019-03-29 10:46:16 -0700624 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100625 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100626
NeilBrown2bc13b82019-03-29 10:46:17 -0700627 mddev->start_flush = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700628 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
629 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100630 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100631 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100632 if (rdev->raid_disk >= 0 &&
633 !test_bit(Faulty, &rdev->flags)) {
634 /* Take two references, one is dropped
635 * when request finishes, one after
636 * we reclaim rcu_read_lock
637 */
638 struct bio *bi;
639 atomic_inc(&rdev->nr_pending);
640 atomic_inc(&rdev->nr_pending);
641 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000642 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Xiao Ni5a409b42018-05-21 11:49:54 +0800643 bi->bi_end_io = md_end_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700644 bi->bi_private = rdev;
645 bio_set_dev(bi, rdev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600646 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
NeilBrown4bc034d2019-03-29 10:46:16 -0700647 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500648 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100649 rcu_read_lock();
650 rdev_dec_pending(rdev, mddev);
651 }
652 rcu_read_unlock();
NeilBrown4bc034d2019-03-29 10:46:16 -0700653 if (atomic_dec_and_test(&mddev->flush_pending))
654 queue_work(md_wq, &mddev->flush_work);
655}
NeilBrowna2826aa2009-12-14 12:49:49 +1100656
NeilBrown4bc034d2019-03-29 10:46:16 -0700657static void md_submit_flush_data(struct work_struct *ws)
658{
659 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
660 struct bio *bio = mddev->flush_bio;
661
662 /*
663 * must reset flush_bio before calling into md_handle_request to avoid a
664 * deadlock, because other bios passed md_handle_request suspend check
665 * could wait for this and below md_handle_request could wait for those
666 * bios because of suspend check
667 */
NeilBrown2bc13b82019-03-29 10:46:17 -0700668 mddev->last_flush = mddev->start_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700669 mddev->flush_bio = NULL;
670 wake_up(&mddev->sb_wait);
671
672 if (bio->bi_iter.bi_size == 0) {
673 /* an empty barrier - all done */
674 bio_endio(bio);
675 } else {
676 bio->bi_opf &= ~REQ_PREFLUSH;
677 md_handle_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100678 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100679}
NeilBrown4bc034d2019-03-29 10:46:16 -0700680
David Jeffery775d7832019-09-16 13:15:14 -0400681/*
682 * Manages consolidation of flushes and submitting any flushes needed for
683 * a bio with REQ_PREFLUSH. Returns true if the bio is finished or is
684 * being finished in another context. Returns false if the flushing is
685 * complete but still needs the I/O portion of the bio to be processed.
686 */
687bool md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrown4bc034d2019-03-29 10:46:16 -0700688{
NeilBrown2bc13b82019-03-29 10:46:17 -0700689 ktime_t start = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700690 spin_lock_irq(&mddev->lock);
691 wait_event_lock_irq(mddev->sb_wait,
NeilBrown2bc13b82019-03-29 10:46:17 -0700692 !mddev->flush_bio ||
693 ktime_after(mddev->last_flush, start),
NeilBrown4bc034d2019-03-29 10:46:16 -0700694 mddev->lock);
NeilBrown2bc13b82019-03-29 10:46:17 -0700695 if (!ktime_after(mddev->last_flush, start)) {
696 WARN_ON(mddev->flush_bio);
697 mddev->flush_bio = bio;
698 bio = NULL;
699 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700700 spin_unlock_irq(&mddev->lock);
701
NeilBrown2bc13b82019-03-29 10:46:17 -0700702 if (!bio) {
703 INIT_WORK(&mddev->flush_work, submit_flushes);
704 queue_work(md_wq, &mddev->flush_work);
705 } else {
706 /* flush was performed for some other bio while we waited. */
707 if (bio->bi_iter.bi_size == 0)
708 /* an empty barrier - all done */
709 bio_endio(bio);
710 else {
711 bio->bi_opf &= ~REQ_PREFLUSH;
David Jeffery775d7832019-09-16 13:15:14 -0400712 return false;
NeilBrown2bc13b82019-03-29 10:46:17 -0700713 }
714 }
David Jeffery775d7832019-09-16 13:15:14 -0400715 return true;
NeilBrown4bc034d2019-03-29 10:46:16 -0700716}
Tejun Heoe9c74692010-09-03 11:56:18 +0200717EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100718
NeilBrownfd01b882011-10-11 16:47:53 +1100719static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 atomic_inc(&mddev->active);
722 return mddev;
723}
724
Dan Williams5fd3a172009-03-04 00:57:25 -0700725static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100726
NeilBrownfd01b882011-10-11 16:47:53 +1100727static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
730 return;
NeilBrownd3374822009-01-09 08:31:10 +1100731 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100732 mddev->ctime == 0 && !mddev->hold_active) {
733 /* Array is not configured at all, and not held active,
734 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100735 list_del_init(&mddev->all_mddevs);
Kent Overstreet28dec872018-06-07 20:52:54 -0400736
737 /*
738 * Call queue_work inside the spinlock so that
739 * flush_workqueue() after mddev_find will succeed in waiting
740 * for the work to be done.
741 */
742 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
743 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100744 }
745 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
Kees Cook8376d3c2017-10-16 17:01:48 -0700748static void md_safemode_timeout(struct timer_list *t);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400749
NeilBrownfd01b882011-10-11 16:47:53 +1100750void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100751{
Kent Overstreet28dec872018-06-07 20:52:54 -0400752 kobject_init(&mddev->kobj, &md_ktype);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100753 mutex_init(&mddev->open_mutex);
754 mutex_init(&mddev->reconfig_mutex);
755 mutex_init(&mddev->bitmap_info.mutex);
756 INIT_LIST_HEAD(&mddev->disks);
757 INIT_LIST_HEAD(&mddev->all_mddevs);
Kees Cook8376d3c2017-10-16 17:01:48 -0700758 timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100759 atomic_set(&mddev->active, 1);
760 atomic_set(&mddev->openers, 0);
761 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100762 spin_lock_init(&mddev->lock);
NeilBrown4bc034d2019-03-29 10:46:16 -0700763 atomic_set(&mddev->flush_pending, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100764 init_waitqueue_head(&mddev->sb_wait);
765 init_waitqueue_head(&mddev->recovery_wait);
766 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000767 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500768 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100769 mddev->resync_min = 0;
770 mddev->resync_max = MaxSector;
771 mddev->level = LEVEL_NONE;
772}
NeilBrown390ee602010-06-01 19:37:27 +1000773EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100774
NeilBrownf72ffdd2014-09-30 14:23:59 +1000775static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
NeilBrownfd01b882011-10-11 16:47:53 +1100777 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
NeilBrown8f5f02c2011-02-16 13:58:51 +1100779 if (unit && MAJOR(unit) != MD_MAJOR)
780 unit &= ~((1<<MdpMinorShift)-1);
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 retry:
783 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
NeilBrownefeb53c2009-01-09 08:31:10 +1100785 if (unit) {
786 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
787 if (mddev->unit == unit) {
788 mddev_get(mddev);
789 spin_unlock(&all_mddevs_lock);
790 kfree(new);
791 return mddev;
792 }
793
794 if (new) {
795 list_add(&new->all_mddevs, &all_mddevs);
796 spin_unlock(&all_mddevs_lock);
797 new->hold_active = UNTIL_IOCTL;
798 return new;
799 }
800 } else if (new) {
801 /* find an unused unit number */
802 static int next_minor = 512;
803 int start = next_minor;
804 int is_free = 0;
805 int dev = 0;
806 while (!is_free) {
807 dev = MKDEV(MD_MAJOR, next_minor);
808 next_minor++;
809 if (next_minor > MINORMASK)
810 next_minor = 0;
811 if (next_minor == start) {
812 /* Oh dear, all in use. */
813 spin_unlock(&all_mddevs_lock);
814 kfree(new);
815 return NULL;
816 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000817
NeilBrownefeb53c2009-01-09 08:31:10 +1100818 is_free = 1;
819 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
820 if (mddev->unit == dev) {
821 is_free = 0;
822 break;
823 }
824 }
825 new->unit = dev;
826 new->md_minor = MINOR(dev);
827 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 list_add(&new->all_mddevs, &all_mddevs);
829 spin_unlock(&all_mddevs_lock);
830 return new;
831 }
832 spin_unlock(&all_mddevs_lock);
833
NeilBrown9ffae0c2006-01-06 00:20:32 -0800834 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (!new)
836 return NULL;
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 new->unit = unit;
839 if (MAJOR(unit) == MD_MAJOR)
840 new->md_minor = MINOR(unit);
841 else
842 new->md_minor = MINOR(unit) >> MdpMinorShift;
843
NeilBrownfafd7fb2010-04-01 15:55:30 +1100844 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 goto retry;
847}
848
NeilBrownb6eb1272010-04-15 10:13:47 +1000849static struct attribute_group md_redundancy_group;
850
NeilBrown5c47daf2014-12-15 12:57:01 +1100851void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
NeilBrowna64c8762010-04-14 17:15:37 +1000853 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000854 /* These cannot be removed under reconfig_mutex as
855 * an access to the files will try to take reconfig_mutex
856 * while holding the file unremovable, which leads to
857 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000858 * So hold set sysfs_active while the remove in happeing,
859 * and anything else which might set ->to_remove or my
860 * otherwise change the sysfs namespace will fail with
861 * -EBUSY if sysfs_active is still set.
862 * We set sysfs_active under reconfig_mutex and elsewhere
863 * test it under the same mutex to ensure its correct value
864 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000865 */
NeilBrowna64c8762010-04-14 17:15:37 +1000866 struct attribute_group *to_remove = mddev->to_remove;
867 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000868 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000869 mutex_unlock(&mddev->reconfig_mutex);
870
NeilBrown00bcb4a2010-06-01 19:37:23 +1000871 if (mddev->kobj.sd) {
872 if (to_remove != &md_redundancy_group)
873 sysfs_remove_group(&mddev->kobj, to_remove);
874 if (mddev->pers == NULL ||
875 mddev->pers->sync_request == NULL) {
876 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
877 if (mddev->sysfs_action)
878 sysfs_put(mddev->sysfs_action);
879 mddev->sysfs_action = NULL;
880 }
NeilBrowna64c8762010-04-14 17:15:37 +1000881 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000882 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000883 } else
884 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Chris Dunlop751e67c2011-10-19 16:48:26 +1100886 /* As we've dropped the mutex we need a spinlock to
887 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000888 */
889 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700890 md_wakeup_thread(mddev->thread);
NeilBrown4d5324f2017-10-19 12:17:16 +1100891 wake_up(&mddev->sb_wait);
NeilBrown01f96c02011-09-21 15:30:20 +1000892 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
NeilBrown5c47daf2014-12-15 12:57:01 +1100894EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500896struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100897{
898 struct md_rdev *rdev;
899
900 rdev_for_each_rcu(rdev, mddev)
901 if (rdev->desc_nr == nr)
902 return rdev;
903
904 return NULL;
905}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500906EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100907
908static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
NeilBrown3cb03002011-10-11 16:45:26 +1100910 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
NeilBrowndafb20f2012-03-19 12:46:39 +1100912 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (rdev->bdev->bd_dev == dev)
914 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return NULL;
917}
918
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100919struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev)
NeilBrown1ca69c42012-10-11 13:37:33 +1100920{
921 struct md_rdev *rdev;
922
923 rdev_for_each_rcu(rdev, mddev)
924 if (rdev->bdev->bd_dev == dev)
925 return rdev;
926
927 return NULL;
928}
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100929EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100930
NeilBrown84fc4b52011-10-11 16:49:58 +1100931static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800932{
NeilBrown84fc4b52011-10-11 16:49:58 +1100933 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800934 list_for_each_entry(pers, &pers_list, list) {
935 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800936 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800937 if (strcmp(pers->name, clevel)==0)
938 return pers;
939 }
NeilBrown2604b702006-01-06 00:20:36 -0800940 return NULL;
941}
942
Andre Nollb73df2d2008-07-11 22:02:23 +1000943/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100944static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100946 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000947 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
NeilBrownf72ffdd2014-09-30 14:23:59 +1000950static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100953 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000954 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return 0;
956}
957
NeilBrown545c8792012-05-22 13:54:30 +1000958void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800961 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 rdev->sb_loaded = 0;
963 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000964 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100965 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
NeilBrown2699b672011-07-28 11:31:47 +1000967 if (rdev->bb_page) {
968 put_page(rdev->bb_page);
969 rdev->bb_page = NULL;
970 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800971 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
NeilBrown545c8792012-05-22 13:54:30 +1000973EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200975static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700976{
NeilBrown3cb03002011-10-11 16:45:26 +1100977 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100978 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700979
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200980 if (bio->bi_status) {
Guoqing Jiangb3db8a22020-07-28 12:01:41 +0200981 pr_err("md: %s gets error=%d\n", __func__,
982 blk_status_to_errno(bio->bi_status));
NeilBrowna9701a32005-11-08 21:39:34 -0800983 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100984 if (!test_bit(Faulty, &rdev->flags)
985 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800986 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100987 set_bit(LastDev, &rdev->flags);
988 }
989 } else
990 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700991
NeilBrowna9701a32005-11-08 21:39:34 -0800992 if (atomic_dec_and_test(&mddev->pending_writes))
993 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700994 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700995 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700996}
997
NeilBrownfd01b882011-10-11 16:47:53 +1100998void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700999 sector_t sector, int size, struct page *page)
1000{
1001 /* write first size bytes of page to sector of rdev
1002 * Increment mddev->pending_writes before returning
1003 * and decrement it on completion, waking up sb_wait
1004 * if zero is reached.
1005 * If an error occurred, call md_error
1006 */
NeilBrown46533ff2016-11-18 16:16:11 +11001007 struct bio *bio;
1008 int ff = 0;
1009
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01001010 if (!page)
1011 return;
1012
NeilBrown46533ff2016-11-18 16:16:11 +11001013 if (test_bit(Faulty, &rdev->flags))
1014 return;
1015
NeilBrown5a850712017-06-21 09:12:21 +10001016 bio = md_bio_alloc_sync(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001017
Shaohua Lied3b98c2016-03-29 14:00:19 -07001018 atomic_inc(&rdev->nr_pending);
1019
Christoph Hellwig74d46992017-08-23 19:10:32 +02001020 bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001021 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001022 bio_add_page(bio, page, size, 0);
1023 bio->bi_private = rdev;
1024 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +11001025
1026 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
1027 test_bit(FailFast, &rdev->flags) &&
1028 !test_bit(LastDev, &rdev->flags))
1029 ff = MD_FAILFAST;
Jan Kara5a8948f2017-05-31 09:44:33 +02001030 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -08001031
NeilBrown7bfa19f2005-06-21 17:17:28 -07001032 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -05001033 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -08001034}
1035
NeilBrown46533ff2016-11-18 16:16:11 +11001036int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -08001037{
Tejun Heoe9c74692010-09-03 11:56:18 +02001038 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +10001039 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -08001040 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +11001041 return -EAGAIN;
1042 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001043}
1044
NeilBrown3cb03002011-10-11 16:45:26 +11001045int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -05001046 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
NeilBrown5a850712017-06-21 09:12:21 +10001048 struct bio *bio = md_bio_alloc_sync(rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 int ret;
1050
Christoph Hellwig74d46992017-08-23 19:10:32 +02001051 if (metadata_op && rdev->meta_bdev)
1052 bio_set_dev(bio, rdev->meta_bdev);
1053 else
1054 bio_set_dev(bio, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -05001055 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001056 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -07001057 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +10001058 else if (rdev->mddev->reshape_position != MaxSector &&
1059 (rdev->mddev->reshape_backwards ==
1060 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001061 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001062 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001063 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -05001065
1066 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001068 ret = !bio->bi_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 bio_put(bio);
1070 return ret;
1071}
NeilBrowna8745db2006-01-06 00:20:34 -08001072EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
NeilBrownf72ffdd2014-09-30 14:23:59 +10001074static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (rdev->sb_loaded)
1079 return 0;
1080
Mike Christie796a5cf2016-06-05 14:32:07 -05001081 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 goto fail;
1083 rdev->sb_loaded = 1;
1084 return 0;
1085
1086fail:
NeilBrown9d487392016-11-02 14:16:49 +11001087 pr_err("md: disabled device %s, could not read superblock.\n",
1088 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return -EINVAL;
1090}
1091
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001092static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
NeilBrownf72ffdd2014-09-30 14:23:59 +10001094 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +10001095 sb1->set_uuid1 == sb2->set_uuid1 &&
1096 sb1->set_uuid2 == sb2->set_uuid2 &&
1097 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001100static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 int ret;
1103 mdp_super_t *tmp1, *tmp2;
1104
1105 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
1106 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
1107
1108 if (!tmp1 || !tmp2) {
1109 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 goto abort;
1111 }
1112
1113 *tmp1 = *sb1;
1114 *tmp2 = *sb2;
1115
1116 /*
1117 * nr_disks is not constant
1118 */
1119 tmp1->nr_disks = 0;
1120 tmp2->nr_disks = 0;
1121
Andre Nollce0c8e02008-07-11 22:02:20 +10001122 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001124 kfree(tmp1);
1125 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return ret;
1127}
1128
NeilBrown4d167f02007-05-09 02:35:37 -07001129static u32 md_csum_fold(u32 csum)
1130{
1131 csum = (csum & 0xffff) + (csum >> 16);
1132 return (csum & 0xffff) + (csum >> 16);
1133}
1134
NeilBrownf72ffdd2014-09-30 14:23:59 +10001135static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
NeilBrown4d167f02007-05-09 02:35:37 -07001137 u64 newcsum = 0;
1138 u32 *sb32 = (u32*)sb;
1139 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 unsigned int disk_csum, csum;
1141
1142 disk_csum = sb->sb_csum;
1143 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -07001144
1145 for (i = 0; i < MD_SB_BYTES/4 ; i++)
1146 newcsum += sb32[i];
1147 csum = (newcsum & 0xffffffff) + (newcsum>>32);
1148
NeilBrown4d167f02007-05-09 02:35:37 -07001149#ifdef CONFIG_ALPHA
1150 /* This used to use csum_partial, which was wrong for several
1151 * reasons including that different results are returned on
1152 * different architectures. It isn't critical that we get exactly
1153 * the same return value as before (we always csum_fold before
1154 * testing, and that removes any differences). However as we
1155 * know that csum_partial always returned a 16bit value on
1156 * alphas, do a fold to maximise conformity to previous behaviour.
1157 */
1158 sb->sb_csum = md_csum_fold(disk_csum);
1159#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -07001161#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return csum;
1163}
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165/*
1166 * Handle superblock details.
1167 * We want to be able to handle multiple superblock formats
1168 * so we have a common interface to them all, and an array of
1169 * different handlers.
1170 * We rely on user-space to write the initial superblock, and support
1171 * reading and updating of superblocks.
1172 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +11001173 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 * loads and validates a superblock on dev.
1175 * if refdev != NULL, compare superblocks on both devices
1176 * Return:
1177 * 0 - dev has a superblock that is compatible with refdev
1178 * 1 - dev has a superblock that is compatible and newer than refdev
1179 * so dev should be used as the refdev in future
1180 * -EINVAL superblock incompatible or invalid
1181 * -othererror e.g. -EIO
1182 *
NeilBrownfd01b882011-10-11 16:47:53 +11001183 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 * Verify that dev is acceptable into mddev.
1185 * The first time, mddev->raid_disks will be 0, and data from
1186 * dev should be merged in. Subsequent calls check that dev
1187 * is new enough. Return 0 or -EINVAL
1188 *
NeilBrownfd01b882011-10-11 16:47:53 +11001189 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 * Update the superblock for rdev with data in mddev
1191 * This does not write to disc.
1192 *
1193 */
1194
1195struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001196 char *name;
1197 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +10001198 int (*load_super)(struct md_rdev *rdev,
1199 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001200 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +10001201 int (*validate_super)(struct mddev *mddev,
1202 struct md_rdev *rdev);
1203 void (*sync_super)(struct mddev *mddev,
1204 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001205 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001206 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +10001207 int (*allow_new_offset)(struct md_rdev *rdev,
1208 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209};
1210
1211/*
Andre Noll0894cc32009-06-18 08:49:23 +10001212 * Check that the given mddev has no bitmap.
1213 *
1214 * This function is called from the run method of all personalities that do not
1215 * support bitmaps. It prints an error message and returns non-zero if mddev
1216 * has a bitmap. Otherwise, it returns 0.
1217 *
1218 */
NeilBrownfd01b882011-10-11 16:47:53 +11001219int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001220{
NeilBrownc3d97142009-12-14 12:49:52 +11001221 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001222 return 0;
NeilBrown9d487392016-11-02 14:16:49 +11001223 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +10001224 mdname(mddev), mddev->pers->name);
1225 return 1;
1226}
1227EXPORT_SYMBOL(md_check_no_bitmap);
1228
1229/*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001230 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 */
NeilBrown3cb03002011-10-11 16:45:26 +11001232static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1235 mdp_super_t *sb;
1236 int ret;
Yufen Yu228fc7d2019-10-30 18:47:02 +08001237 bool spare_disk = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 /*
Andre Noll0f420352008-07-11 22:02:23 +10001240 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 * it's at the end of the disk.
1242 *
1243 * It also happens to be a multiple of 4Kb.
1244 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001245 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
NeilBrown0002b272005-09-09 16:23:53 -07001247 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +11001248 if (ret)
1249 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 ret = -EINVAL;
1252
1253 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001254 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +11001257 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 goto abort;
1259 }
1260
1261 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001262 sb->minor_version < 90 ||
1263 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +11001264 pr_warn("Bad version number %d.%d on %s\n",
1265 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 goto abort;
1267 }
1268
1269 if (sb->raid_disks <= 0)
1270 goto abort;
1271
NeilBrown4d167f02007-05-09 02:35:37 -07001272 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001273 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 goto abort;
1275 }
1276
1277 rdev->preferred_minor = sb->md_minor;
1278 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001279 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001280 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001281 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 if (sb->level == LEVEL_MULTIPATH)
1284 rdev->desc_nr = -1;
1285 else
1286 rdev->desc_nr = sb->this_disk.number;
1287
Yufen Yu228fc7d2019-10-30 18:47:02 +08001288 /* not spare disk, or LEVEL_MULTIPATH */
1289 if (sb->level == LEVEL_MULTIPATH ||
1290 (rdev->desc_nr >= 0 &&
Yufen Yu3b7436c2019-12-10 15:01:29 +08001291 rdev->desc_nr < MD_SB_DISKS &&
Yufen Yu228fc7d2019-10-30 18:47:02 +08001292 sb->disks[rdev->desc_nr].state &
1293 ((1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))))
1294 spare_disk = false;
1295
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001296 if (!refdev) {
Yufen Yu228fc7d2019-10-30 18:47:02 +08001297 if (!spare_disk)
Yufen Yu6a5cb532019-10-16 16:00:03 +08001298 ret = 1;
1299 else
1300 ret = 0;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001301 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001303 mdp_super_t *refsb = page_address(refdev->sb_page);
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001304 if (!md_uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001305 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 b, bdevname(refdev->bdev,b2));
1307 goto abort;
1308 }
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001309 if (!md_sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001310 pr_warn("md: %s has same UUID but different superblock to %s\n",
1311 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 goto abort;
1313 }
1314 ev1 = md_event(sb);
1315 ev2 = md_event(refsb);
Yufen Yu6a5cb532019-10-16 16:00:03 +08001316
Yufen Yu228fc7d2019-10-30 18:47:02 +08001317 if (!spare_disk && ev1 > ev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001319 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 ret = 0;
1321 }
NeilBrown8190e752009-06-18 08:48:58 +10001322 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001323 /* Limit to 4TB as metadata cannot record more than that.
1324 * (not needed for Linear and RAID0 as metadata doesn't
1325 * record this size)
1326 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001327 if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001328 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
NeilBrown27a7b262011-09-10 17:21:28 +10001330 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001331 /* "this cannot possibly happen" ... */
1332 ret = -EINVAL;
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 abort:
1335 return ret;
1336}
1337
1338/*
1339 * validate_super for 0.90.0
1340 */
NeilBrownfd01b882011-10-11 16:47:53 +11001341static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001344 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001345 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
NeilBrown41158c72005-06-21 17:17:25 -07001347 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001348 clear_bit(Faulty, &rdev->flags);
1349 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001350 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001351 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (mddev->raid_disks == 0) {
1354 mddev->major_version = 0;
1355 mddev->minor_version = sb->minor_version;
1356 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001357 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001358 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 mddev->ctime = sb->ctime;
1360 mddev->utime = sb->utime;
1361 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001362 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 mddev->layout = sb->layout;
1364 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001365 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001366 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001367 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001368 mddev->bitmap_info.space = 0;
1369 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001370 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001371 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001372 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
NeilBrownf6705572006-03-27 01:18:11 -08001374 if (mddev->minor_version >= 91) {
1375 mddev->reshape_position = sb->reshape_position;
1376 mddev->delta_disks = sb->delta_disks;
1377 mddev->new_level = sb->new_level;
1378 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001379 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001380 if (mddev->delta_disks < 0)
1381 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001382 } else {
1383 mddev->reshape_position = MaxSector;
1384 mddev->delta_disks = 0;
1385 mddev->new_level = mddev->level;
1386 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001387 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001388 }
NeilBrown33f2c352019-09-09 16:52:29 +10001389 if (mddev->level == 0)
1390 mddev->layout = -1;
NeilBrownf6705572006-03-27 01:18:11 -08001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (sb->state & (1<<MD_SB_CLEAN))
1393 mddev->recovery_cp = MaxSector;
1394 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001395 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 sb->events_lo == sb->cp_events_lo) {
1397 mddev->recovery_cp = sb->recovery_cp;
1398 } else
1399 mddev->recovery_cp = 0;
1400 }
1401
1402 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1403 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1404 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1405 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1406
1407 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001408
1409 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001410 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001411 mddev->bitmap_info.offset =
1412 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001413 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001414 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001415 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001416
NeilBrown41158c72005-06-21 17:17:25 -07001417 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001418 /* Insist on good event counter while assembling, except
1419 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001421 if (sb->disks[rdev->desc_nr].state & (
1422 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001423 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001424 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001425 } else if (mddev->bitmap) {
1426 /* if adding to array with a bitmap, then we can accept an
1427 * older device ... but not too old.
1428 */
NeilBrown41158c72005-06-21 17:17:25 -07001429 if (ev1 < mddev->bitmap->events_cleared)
1430 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001431 if (ev1 < mddev->events)
1432 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001433 } else {
1434 if (ev1 < mddev->events)
1435 /* just a hot-add of a new device, leave raid_disk at -1 */
1436 return 0;
1437 }
NeilBrown41158c72005-06-21 17:17:25 -07001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 desc = sb->disks + rdev->desc_nr;
1441
1442 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001443 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001444 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1445 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001446 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001448 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001449 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1450 /* active but not in sync implies recovery up to
1451 * reshape position. We don't know exactly where
1452 * that is, so set to zero for now */
1453 if (mddev->minor_version >= 91) {
1454 rdev->recovery_offset = 0;
1455 rdev->raid_disk = desc->raid_disk;
1456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001458 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1459 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001460 if (desc->state & (1<<MD_DISK_FAILFAST))
1461 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001462 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001463 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return 0;
1465}
1466
1467/*
1468 * sync_super for 0.90.0
1469 */
NeilBrownfd01b882011-10-11 16:47:53 +11001470static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001473 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 /* make rdev->sb match mddev data..
1477 *
1478 * 1/ zero out disks
1479 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1480 * 3/ any empty disks < next_spare become removed
1481 *
1482 * disks[0] gets initialised to REMOVED because
1483 * we cannot be sure from other fields if it has
1484 * been initialised or not.
1485 */
1486 int i;
1487 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1488
NeilBrown61181562005-09-09 16:24:02 -07001489 rdev->sb_size = MD_SB_BYTES;
1490
Namhyung Kim65a06f062011-07-27 11:00:36 +10001491 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 memset(sb, 0, sizeof(*sb));
1494
1495 sb->md_magic = MD_SB_MAGIC;
1496 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 sb->patch_version = mddev->patch_version;
1498 sb->gvalid_words = 0; /* ignored */
1499 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1500 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1501 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1502 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1503
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001504 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001506 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 sb->raid_disks = mddev->raid_disks;
1508 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001509 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001510 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 sb->state = 0;
1512 sb->events_hi = (mddev->events>>32);
1513 sb->events_lo = (u32)mddev->events;
1514
NeilBrownf6705572006-03-27 01:18:11 -08001515 if (mddev->reshape_position == MaxSector)
1516 sb->minor_version = 90;
1517 else {
1518 sb->minor_version = 91;
1519 sb->reshape_position = mddev->reshape_position;
1520 sb->new_level = mddev->new_level;
1521 sb->delta_disks = mddev->delta_disks;
1522 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001523 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001524 }
1525 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (mddev->in_sync)
1527 {
1528 sb->recovery_cp = mddev->recovery_cp;
1529 sb->cp_events_hi = (mddev->events>>32);
1530 sb->cp_events_lo = (u32)mddev->events;
1531 if (mddev->recovery_cp == MaxSector)
1532 sb->state = (1<< MD_SB_CLEAN);
1533 } else
1534 sb->recovery_cp = 0;
1535
1536 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001537 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
NeilBrownc3d97142009-12-14 12:49:52 +11001539 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001540 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001543 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001545 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001546 int is_active = test_bit(In_sync, &rdev2->flags);
1547
1548 if (rdev2->raid_disk >= 0 &&
1549 sb->minor_version >= 91)
1550 /* we have nowhere to store the recovery_offset,
1551 * but if it is not below the reshape_position,
1552 * we can piggy-back on that.
1553 */
1554 is_active = 1;
1555 if (rdev2->raid_disk < 0 ||
1556 test_bit(Faulty, &rdev2->flags))
1557 is_active = 0;
1558 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001559 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001561 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001562 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 d = &sb->disks[rdev2->desc_nr];
1564 nr_disks++;
1565 d->number = rdev2->desc_nr;
1566 d->major = MAJOR(rdev2->bdev->bd_dev);
1567 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001568 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 d->raid_disk = rdev2->raid_disk;
1570 else
1571 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001572 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001574 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001576 if (test_bit(In_sync, &rdev2->flags))
1577 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 active++;
1579 working++;
1580 } else {
1581 d->state = 0;
1582 spare++;
1583 working++;
1584 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001585 if (test_bit(WriteMostly, &rdev2->flags))
1586 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001587 if (test_bit(FailFast, &rdev2->flags))
1588 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 /* now set the "removed" and "faulty" bits on any missing devices */
1591 for (i=0 ; i < mddev->raid_disks ; i++) {
1592 mdp_disk_t *d = &sb->disks[i];
1593 if (d->state == 0 && d->number == 0) {
1594 d->number = i;
1595 d->raid_disk = i;
1596 d->state = (1<<MD_DISK_REMOVED);
1597 d->state |= (1<<MD_DISK_FAULTY);
1598 failed++;
1599 }
1600 }
1601 sb->nr_disks = nr_disks;
1602 sb->active_disks = active;
1603 sb->working_disks = working;
1604 sb->failed_disks = failed;
1605 sb->spare_disks = spare;
1606
1607 sb->this_disk = sb->disks[rdev->desc_nr];
1608 sb->sb_csum = calc_sb_csum(sb);
1609}
1610
1611/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001612 * rdev_size_change for 0.90.0
1613 */
1614static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001615super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001616{
Andre Noll58c0fed2009-03-31 14:33:13 +11001617 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001618 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001619 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001620 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001621 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001622 if (!num_sectors || num_sectors > rdev->sb_start)
1623 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001624 /* Limit to 4TB as metadata cannot record more than that.
1625 * 4TB == 2^32 KB, or 2*2^32 sectors.
1626 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001627 if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001628 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001629 do {
1630 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001631 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001632 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001633 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001634}
1635
NeilBrownc6563a82012-05-21 09:27:00 +10001636static int
1637super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1638{
1639 /* non-zero offset changes not possible with v0.90 */
1640 return new_offset == 0;
1641}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001642
1643/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 * version 1 superblock
1645 */
1646
NeilBrownf72ffdd2014-09-30 14:23:59 +10001647static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001649 __le32 disk_csum;
1650 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 unsigned long long newcsum;
1652 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001653 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 disk_csum = sb->sb_csum;
1656 sb->sb_csum = 0;
1657 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001658 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 newcsum += le32_to_cpu(*isuper++);
1660
1661 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001662 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1665 sb->sb_csum = disk_csum;
1666 return cpu_to_le32(csum);
1667}
1668
NeilBrown3cb03002011-10-11 16:45:26 +11001669static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 struct mdp_superblock_1 *sb;
1672 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001673 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001674 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001676 int bmask;
Yufen Yu228fc7d2019-10-30 18:47:02 +08001677 bool spare_disk = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 /*
Andre Noll0f420352008-07-11 22:02:23 +10001680 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 * It is always aligned to a 4K boundary and
1682 * depeding on minor_version, it can be:
1683 * 0: At least 8K, but less than 12K, from end of device
1684 * 1: At start of device
1685 * 2: 4K from start of device.
1686 */
1687 switch(minor_version) {
1688 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001689 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001690 sb_start -= 8*2;
1691 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 break;
1693 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001694 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 break;
1696 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001697 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 break;
1699 default:
1700 return -EINVAL;
1701 }
Andre Noll0f420352008-07-11 22:02:23 +10001702 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
NeilBrown0002b272005-09-09 16:23:53 -07001704 /* superblock is rarely larger than 1K, but it can be larger,
1705 * and it is safe to read 4k, so we do that
1706 */
1707 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (ret) return ret;
1709
Namhyung Kim65a06f062011-07-27 11:00:36 +10001710 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1713 sb->major_version != cpu_to_le32(1) ||
1714 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001715 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001716 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return -EINVAL;
1718
1719 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001720 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 bdevname(rdev->bdev,b));
1722 return -EINVAL;
1723 }
1724 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001725 pr_warn("md: data_size too small on %s\n",
1726 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return -EINVAL;
1728 }
NeilBrownc6563a82012-05-21 09:27:00 +10001729 if (sb->pad0 ||
1730 sb->pad3[0] ||
1731 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1732 /* Some padding is non-zero, might be a new feature */
1733 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 rdev->preferred_minor = 0xffff;
1736 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001737 rdev->new_data_offset = rdev->data_offset;
1738 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1739 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1740 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001741 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
NeilBrown0002b272005-09-09 16:23:53 -07001743 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001744 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001745 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001746 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1747
1748 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001749 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001750 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001751 if (minor_version
1752 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1753 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001754
NeilBrown31b65a02006-07-10 04:44:14 -07001755 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1756 rdev->desc_nr = -1;
1757 else
1758 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1759
NeilBrown2699b672011-07-28 11:31:47 +10001760 if (!rdev->bb_page) {
1761 rdev->bb_page = alloc_page(GFP_KERNEL);
1762 if (!rdev->bb_page)
1763 return -ENOMEM;
1764 }
1765 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1766 rdev->badblocks.count == 0) {
1767 /* need to load the bad block list.
1768 * Currently we limit it to one page.
1769 */
1770 s32 offset;
1771 sector_t bb_sector;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001772 __le64 *bbp;
NeilBrown2699b672011-07-28 11:31:47 +10001773 int i;
1774 int sectors = le16_to_cpu(sb->bblog_size);
1775 if (sectors > (PAGE_SIZE / 512))
1776 return -EINVAL;
1777 offset = le32_to_cpu(sb->bblog_offset);
1778 if (offset == 0)
1779 return -EINVAL;
1780 bb_sector = (long long)offset;
1781 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001782 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001783 return -EIO;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001784 bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10001785 rdev->badblocks.shift = sb->bblog_shift;
1786 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1787 u64 bb = le64_to_cpu(*bbp);
1788 int count = bb & (0x3ff);
1789 u64 sector = bb >> 10;
1790 sector <<= sb->bblog_shift;
1791 count <<= sb->bblog_shift;
1792 if (bb + 1 == 0)
1793 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001794 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001795 return -EINVAL;
1796 }
NeilBrown486adf72013-04-24 11:42:44 +10001797 } else if (sb->bblog_offset != 0)
1798 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001799
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001800 if ((le32_to_cpu(sb->feature_map) &
1801 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001802 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1803 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1804 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1805 }
1806
NeilBrown33f2c352019-09-09 16:52:29 +10001807 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT) &&
1808 sb->level != 0)
1809 return -EINVAL;
1810
Yufen Yu228fc7d2019-10-30 18:47:02 +08001811 /* not spare disk, or LEVEL_MULTIPATH */
1812 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH) ||
1813 (rdev->desc_nr >= 0 &&
1814 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1815 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1816 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)))
1817 spare_disk = false;
Yufen Yu6a5cb532019-10-16 16:00:03 +08001818
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001819 if (!refdev) {
Yufen Yu228fc7d2019-10-30 18:47:02 +08001820 if (!spare_disk)
Yufen Yu6a5cb532019-10-16 16:00:03 +08001821 ret = 1;
1822 else
1823 ret = 0;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001824 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001826 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1829 sb->level != refsb->level ||
1830 sb->layout != refsb->layout ||
1831 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001832 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 bdevname(rdev->bdev,b),
1834 bdevname(refdev->bdev,b2));
1835 return -EINVAL;
1836 }
1837 ev1 = le64_to_cpu(sb->events);
1838 ev2 = le64_to_cpu(refsb->events);
1839
Yufen Yu228fc7d2019-10-30 18:47:02 +08001840 if (!spare_disk && ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001841 ret = 1;
1842 else
1843 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 }
NeilBrownc6563a82012-05-21 09:27:00 +10001845 if (minor_version) {
1846 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1847 sectors -= rdev->data_offset;
1848 } else
1849 sectors = rdev->sb_start;
1850 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001852 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001853 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
NeilBrownfd01b882011-10-11 16:47:53 +11001856static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001858 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001859 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
NeilBrown41158c72005-06-21 17:17:25 -07001861 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001862 clear_bit(Faulty, &rdev->flags);
1863 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001864 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001865 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (mddev->raid_disks == 0) {
1868 mddev->major_version = 1;
1869 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001870 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001871 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001872 mddev->ctime = le64_to_cpu(sb->ctime);
1873 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001875 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 mddev->layout = le32_to_cpu(sb->layout);
1877 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001878 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001879 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001880 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001881 mddev->bitmap_info.space = 0;
1882 /* Default location for bitmap is 1K after superblock
1883 * using 3K - total of 4K
1884 */
NeilBrownc3d97142009-12-14 12:49:52 +11001885 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001886 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001887 mddev->reshape_backwards = 0;
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1890 memcpy(mddev->uuid, sb->set_uuid, 16);
1891
1892 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001893
NeilBrown71c08052005-09-09 16:23:51 -07001894 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001895 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001896 mddev->bitmap_info.offset =
1897 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001898 /* Metadata doesn't record how much space is available.
1899 * For 1.0, we assume we can use up to the superblock
1900 * if before, else to 4K beyond superblock.
1901 * For others, assume no change is possible.
1902 */
1903 if (mddev->minor_version > 0)
1904 mddev->bitmap_info.space = 0;
1905 else if (mddev->bitmap_info.offset > 0)
1906 mddev->bitmap_info.space =
1907 8 - mddev->bitmap_info.offset;
1908 else
1909 mddev->bitmap_info.space =
1910 -mddev->bitmap_info.offset;
1911 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001912
NeilBrownf6705572006-03-27 01:18:11 -08001913 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1914 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1915 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1916 mddev->new_level = le32_to_cpu(sb->new_level);
1917 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001918 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001919 if (mddev->delta_disks < 0 ||
1920 (mddev->delta_disks == 0 &&
1921 (le32_to_cpu(sb->feature_map)
1922 & MD_FEATURE_RESHAPE_BACKWARDS)))
1923 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001924 } else {
1925 mddev->reshape_position = MaxSector;
1926 mddev->delta_disks = 0;
1927 mddev->new_level = mddev->level;
1928 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001929 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001930 }
1931
NeilBrown33f2c352019-09-09 16:52:29 +10001932 if (mddev->level == 0 &&
1933 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT))
1934 mddev->layout = -1;
1935
Song Liu486b0f72016-08-19 15:34:01 -07001936 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001937 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001938
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001939 if (le32_to_cpu(sb->feature_map) &
1940 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001941 if (le32_to_cpu(sb->feature_map) &
1942 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1943 return -EINVAL;
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001944 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1945 (le32_to_cpu(sb->feature_map) &
1946 MD_FEATURE_MULTIPLE_PPLS))
1947 return -EINVAL;
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001948 set_bit(MD_HAS_PPL, &mddev->flags);
1949 }
NeilBrown41158c72005-06-21 17:17:25 -07001950 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001951 /* Insist of good event counter while assembling, except for
1952 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001954 if (rdev->desc_nr >= 0 &&
1955 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001956 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1957 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001958 if (ev1 < mddev->events)
1959 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001960 } else if (mddev->bitmap) {
1961 /* If adding to array with a bitmap, then we can accept an
1962 * older device, but not too old.
1963 */
NeilBrown41158c72005-06-21 17:17:25 -07001964 if (ev1 < mddev->bitmap->events_cleared)
1965 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001966 if (ev1 < mddev->events)
1967 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001968 } else {
1969 if (ev1 < mddev->events)
1970 /* just a hot-add of a new device, leave raid_disk at -1 */
1971 return 0;
1972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 if (mddev->level != LEVEL_MULTIPATH) {
1974 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001975 if (rdev->desc_nr < 0 ||
1976 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001977 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001978 rdev->desc_nr = -1;
1979 } else
1980 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001982 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001984 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001985 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 break;
Song Liubac624f2015-08-13 14:31:55 -07001987 case MD_DISK_ROLE_JOURNAL: /* journal device */
1988 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1989 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001990 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001991 return -EINVAL;
1992 }
1993 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001994 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001995 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001996 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 default:
NeilBrownf4667222013-12-09 12:04:56 +11001998 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001999 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11002000 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002001 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11002002 if (!(le32_to_cpu(sb->feature_map) &
2003 MD_FEATURE_RECOVERY_BITMAP))
2004 rdev->saved_raid_disk = -1;
Guoqing Jiang062f5b2a2019-07-24 11:09:20 +02002005 } else {
2006 /*
2007 * If the array is FROZEN, then the device can't
2008 * be in_sync with rest of array.
2009 */
2010 if (!test_bit(MD_RECOVERY_FROZEN,
2011 &mddev->recovery))
2012 set_bit(In_sync, &rdev->flags);
2013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 rdev->raid_disk = role;
2015 break;
2016 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07002017 if (sb->devflags & WriteMostly1)
2018 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11002019 if (sb->devflags & FailFast1)
2020 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002021 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
2022 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07002023 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08002024 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 return 0;
2027}
2028
NeilBrownfd01b882011-10-11 16:47:53 +11002029static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
2031 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11002032 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 int max_dev, i;
2034 /* make rdev->sb match mddev and rdev data. */
2035
Namhyung Kim65a06f062011-07-27 11:00:36 +10002036 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 sb->feature_map = 0;
2039 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002040 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 memset(sb->pad3, 0, sizeof(sb->pad3));
2042
2043 sb->utime = cpu_to_le64((__u64)mddev->utime);
2044 sb->events = cpu_to_le64(mddev->events);
2045 if (mddev->in_sync)
2046 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07002047 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
2048 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 else
2050 sb->resync_offset = cpu_to_le64(0);
2051
NeilBrown1c05b4b2006-10-21 10:24:08 -07002052 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08002053
NeilBrownf0ca3402006-02-02 14:28:04 -08002054 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11002055 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10002056 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10002057 sb->level = cpu_to_le32(mddev->level);
2058 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11002059 if (test_bit(FailFast, &rdev->flags))
2060 sb->devflags |= FailFast1;
2061 else
2062 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08002063
NeilBrownaeb9b2112011-08-25 14:43:08 +10002064 if (test_bit(WriteMostly, &rdev->flags))
2065 sb->devflags |= WriteMostly1;
2066 else
2067 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10002068 sb->data_offset = cpu_to_le64(rdev->data_offset);
2069 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10002070
NeilBrownc3d97142009-12-14 12:49:52 +11002071 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
2072 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07002073 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07002074 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002075
Shaohua Lif2076e72015-10-08 21:54:12 -07002076 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11002077 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11002078 sb->feature_map |=
2079 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
2080 sb->recovery_offset =
2081 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11002082 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
2083 sb->feature_map |=
2084 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002085 }
Shaohua Li3069aa82015-08-13 14:31:56 -07002086 /* Note: recovery_offset and journal_tail share space */
2087 if (test_bit(Journal, &rdev->flags))
2088 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002089 if (test_bit(Replacement, &rdev->flags))
2090 sb->feature_map |=
2091 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002092
NeilBrownf6705572006-03-27 01:18:11 -08002093 if (mddev->reshape_position != MaxSector) {
2094 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
2095 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
2096 sb->new_layout = cpu_to_le32(mddev->new_layout);
2097 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
2098 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10002099 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10002100 if (mddev->delta_disks == 0 &&
2101 mddev->reshape_backwards)
2102 sb->feature_map
2103 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10002104 if (rdev->new_data_offset != rdev->data_offset) {
2105 sb->feature_map
2106 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
2107 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
2108 - rdev->data_offset));
2109 }
NeilBrownf6705572006-03-27 01:18:11 -08002110 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002111
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10002112 if (mddev_is_clustered(mddev))
2113 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
2114
NeilBrown2699b672011-07-28 11:31:47 +10002115 if (rdev->badblocks.count == 0)
2116 /* Nothing to do for bad blocks*/ ;
2117 else if (sb->bblog_offset == 0)
2118 /* Cannot record bad blocks on this device */
2119 md_error(mddev, rdev);
2120 else {
2121 struct badblocks *bb = &rdev->badblocks;
Christoph Hellwigae506402019-04-04 18:56:13 +02002122 __le64 *bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10002123 u64 *p = bb->page;
2124 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
2125 if (bb->changed) {
2126 unsigned seq;
2127
2128retry:
2129 seq = read_seqbegin(&bb->lock);
2130
2131 memset(bbp, 0xff, PAGE_SIZE);
2132
2133 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08002134 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10002135 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
2136 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08002137 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10002138 }
NeilBrownd0962932012-03-19 12:46:41 +11002139 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002140 if (read_seqretry(&bb->lock, seq))
2141 goto retry;
2142
2143 bb->sector = (rdev->sb_start +
2144 (int)le32_to_cpu(sb->bblog_offset));
2145 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10002146 }
2147 }
2148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11002150 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if (rdev2->desc_nr+1 > max_dev)
2152 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07002153
NeilBrown70471da2009-08-03 10:59:57 +10002154 if (max_dev > le32_to_cpu(sb->max_dev)) {
2155 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07002156 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10002157 rdev->sb_size = max_dev * 2 + 256;
2158 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
2159 if (rdev->sb_size & bmask)
2160 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10002161 } else
2162 max_dev = le32_to_cpu(sb->max_dev);
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 for (i=0; i<max_dev;i++)
Lidong Zhong8df72022017-06-12 10:45:55 +08002165 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
NeilBrownf72ffdd2014-09-30 14:23:59 +10002166
Song Liua97b7892015-10-08 21:54:09 -07002167 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
2168 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01002170 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
Pawel Baldysiakddc08822017-08-16 17:13:45 +02002171 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
2172 sb->feature_map |=
2173 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
2174 else
2175 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01002176 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
2177 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
2178 }
2179
NeilBrowndafb20f2012-03-19 12:46:39 +11002180 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002182 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07002183 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08002184 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07002186 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07002187 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11002188 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002189 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 else
Song Liuc4d4c912015-08-13 14:31:54 -07002191 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 sb->sb_csum = calc_sb_1_csum(sb);
2195}
2196
Xiao Nid9c0fa52020-06-30 15:55:36 +08002197static sector_t super_1_choose_bm_space(sector_t dev_size)
2198{
2199 sector_t bm_space;
2200
2201 /* if the device is bigger than 8Gig, save 64k for bitmap
2202 * usage, if bigger than 200Gig, save 128k
2203 */
2204 if (dev_size < 64*2)
2205 bm_space = 0;
2206 else if (dev_size - 64*2 >= 200*1024*1024*2)
2207 bm_space = 128*2;
2208 else if (dev_size - 4*2 > 8*1024*1024*2)
2209 bm_space = 64*2;
2210 else
2211 bm_space = 4*2;
2212 return bm_space;
2213}
2214
Chris Webb0cd17fe2008-06-28 08:31:46 +10002215static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11002216super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002217{
2218 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002219 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11002220 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002221 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10002222 if (rdev->data_offset != rdev->new_data_offset)
2223 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10002224 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10002225 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01002226 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002227 max_sectors -= rdev->data_offset;
2228 if (!num_sectors || num_sectors > max_sectors)
2229 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11002230 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10002231 /* minor version 0 with bitmap we can't move */
2232 return 0;
2233 } else {
2234 /* minor version 0; superblock after data */
Xiao Nid9c0fa52020-06-30 15:55:36 +08002235 sector_t sb_start, bm_space;
2236 sector_t dev_size = i_size_read(rdev->bdev->bd_inode) >> 9;
2237
2238 /* 8K is for superblock */
2239 sb_start = dev_size - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10002240 sb_start &= ~(sector_t)(4*2 - 1);
Xiao Nid9c0fa52020-06-30 15:55:36 +08002241
2242 bm_space = super_1_choose_bm_space(dev_size);
2243
2244 /* Space that can be used to store date needs to decrease
2245 * superblock bitmap space and bad block space(4K)
2246 */
2247 max_sectors = sb_start - bm_space - 4*2;
2248
Andre Noll15f4a5f2008-07-21 14:42:12 +10002249 if (!num_sectors || num_sectors > max_sectors)
2250 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002251 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10002252 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10002253 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08002254 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10002255 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11002256 do {
2257 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
2258 rdev->sb_page);
2259 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11002260 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10002261
2262}
2263
2264static int
2265super_1_allow_new_offset(struct md_rdev *rdev,
2266 unsigned long long new_offset)
2267{
2268 /* All necessary checks on new >= old have been done */
2269 struct bitmap *bitmap;
2270 if (new_offset >= rdev->data_offset)
2271 return 1;
2272
2273 /* with 1.0 metadata, there is no metadata to tread on
2274 * so we can always move back */
2275 if (rdev->mddev->minor_version == 0)
2276 return 1;
2277
2278 /* otherwise we must be sure not to step on
2279 * any metadata, so stay:
2280 * 36K beyond start of superblock
2281 * beyond end of badblocks
2282 * beyond write-intent bitmap
2283 */
2284 if (rdev->sb_start + (32+4)*2 > new_offset)
2285 return 0;
2286 bitmap = rdev->mddev->bitmap;
2287 if (bitmap && !rdev->mddev->bitmap_info.file &&
2288 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10002289 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10002290 return 0;
2291 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2292 return 0;
2293
2294 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002295}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Adrian Bunk75c96f82005-05-05 16:16:09 -07002297static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 [0] = {
2299 .name = "0.90.0",
2300 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002301 .load_super = super_90_load,
2302 .validate_super = super_90_validate,
2303 .sync_super = super_90_sync,
2304 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002305 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 },
2307 [1] = {
2308 .name = "md-1",
2309 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002310 .load_super = super_1_load,
2311 .validate_super = super_1_validate,
2312 .sync_super = super_1_sync,
2313 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002314 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 },
2316};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
NeilBrownfd01b882011-10-11 16:47:53 +11002318static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05002319{
2320 if (mddev->sync_super) {
2321 mddev->sync_super(mddev, rdev);
2322 return;
2323 }
2324
2325 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2326
2327 super_types[mddev->major_version].sync_super(mddev, rdev);
2328}
2329
NeilBrownfd01b882011-10-11 16:47:53 +11002330static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
NeilBrown3cb03002011-10-11 16:45:26 +11002332 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
NeilBrown4b809912008-07-21 17:05:25 +10002334 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07002335 rdev_for_each_rcu(rdev, mddev1) {
2336 if (test_bit(Faulty, &rdev->flags) ||
2337 test_bit(Journal, &rdev->flags) ||
2338 rdev->raid_disk == -1)
2339 continue;
2340 rdev_for_each_rcu(rdev2, mddev2) {
2341 if (test_bit(Faulty, &rdev2->flags) ||
2342 test_bit(Journal, &rdev2->flags) ||
2343 rdev2->raid_disk == -1)
2344 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002345 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002346 rdev2->bdev->bd_contains) {
2347 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002348 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002349 }
Song Liu0b020e82015-09-03 23:00:35 -07002350 }
2351 }
NeilBrown4b809912008-07-21 17:05:25 +10002352 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return 0;
2354}
2355
2356static LIST_HEAD(pending_raid_disks);
2357
Andre Nollac5e7112009-08-03 10:59:47 +10002358/*
2359 * Try to register data integrity profile for an mddev
2360 *
2361 * This is called when an array is started and after a disk has been kicked
2362 * from the array. It only succeeds if all working and active component devices
2363 * are integrity capable with matching profiles.
2364 */
NeilBrownfd01b882011-10-11 16:47:53 +11002365int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002366{
NeilBrown3cb03002011-10-11 16:45:26 +11002367 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002368
Andre Nollac5e7112009-08-03 10:59:47 +10002369 if (list_empty(&mddev->disks))
2370 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002371 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2372 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002373 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002374 /* skip spares and non-functional disks */
2375 if (test_bit(Faulty, &rdev->flags))
2376 continue;
2377 if (rdev->raid_disk < 0)
2378 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002379 if (!reference) {
2380 /* Use the first rdev as the reference */
2381 reference = rdev;
2382 continue;
2383 }
2384 /* does this rdev's profile match the reference profile? */
2385 if (blk_integrity_compare(reference->bdev->bd_disk,
2386 rdev->bdev->bd_disk) < 0)
2387 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002388 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002389 if (!reference || !bdev_get_integrity(reference->bdev))
2390 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002391 /*
2392 * All component devices are integrity capable and have matching
2393 * profiles, register the common profile for the md device.
2394 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002395 blk_integrity_register(mddev->gendisk,
2396 bdev_get_integrity(reference->bdev));
2397
NeilBrown9d487392016-11-02 14:16:49 +11002398 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Kent Overstreetafeee512018-05-20 18:25:52 -04002399 if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002400 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002401 mdname(mddev));
2402 return -EINVAL;
2403 }
Andre Nollac5e7112009-08-03 10:59:47 +10002404 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002405}
Andre Nollac5e7112009-08-03 10:59:47 +10002406EXPORT_SYMBOL(md_integrity_register);
2407
Dan Williams1501efa2016-01-13 16:00:07 -08002408/*
2409 * Attempt to add an rdev, but only if it is consistent with the current
2410 * integrity profile
2411 */
2412int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002413{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002414 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002415 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002416
2417 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002418 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002419
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002420 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002421
2422 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002423 return 0;
2424
2425 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002426 pr_err("%s: incompatible integrity profile for %s\n",
2427 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002428 return -ENXIO;
2429 }
2430
2431 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002432}
2433EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002434
NeilBrownf72ffdd2014-09-30 14:23:59 +10002435static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002437 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002438 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002439 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Dan Williams11e2ede2008-04-30 00:52:32 -07002441 /* prevent duplicates */
2442 if (find_rdev(mddev, rdev->bdev->bd_dev))
2443 return -EEXIST;
2444
NeilBrown97b20ef2017-04-13 08:53:48 +10002445 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2446 mddev->pers)
2447 return -EROFS;
2448
Andre Nolldd8ac332009-03-31 14:33:13 +11002449 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002450 if (!test_bit(Journal, &rdev->flags) &&
2451 rdev->sectors &&
2452 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002453 if (mddev->pers) {
2454 /* Cannot change size, so fail
2455 * If mddev->level <= 0, then we don't care
2456 * about aligning sizes (e.g. linear)
2457 */
2458 if (mddev->level > 0)
2459 return -ENOSPC;
2460 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002461 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 /* Verify rdev->desc_nr is unique.
2465 * If it is -1, assign a free number, else
2466 * check number is not in use
2467 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002468 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (rdev->desc_nr < 0) {
2470 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002471 if (mddev->pers)
2472 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002473 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 choice++;
2475 rdev->desc_nr = choice;
2476 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002477 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002478 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002482 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002483 if (!test_bit(Journal, &rdev->flags) &&
2484 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002485 pr_warn("md: %s: array is limited to %d devices\n",
2486 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002487 return -EBUSY;
2488 }
NeilBrown19133a42005-11-08 21:39:35 -08002489 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002490 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002493 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002494
Guoqing Jiang963c5552019-06-14 17:10:36 +08002495 if (mddev->raid_disks)
Guoqing Jiang404659c2019-12-23 10:48:53 +01002496 mddev_create_serial_pool(mddev, rdev, false);
Guoqing Jiang963c5552019-06-14 17:10:36 +08002497
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002498 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002499 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002500
Tejun Heo0762b8b2008-08-25 19:56:12 +09002501 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
Damien Le Moal5e3b8a82020-07-16 13:54:40 +09002502 /* failure here is OK */
2503 err = sysfs_create_link(&rdev->kobj, ko, "block");
NeilBrown00bcb4a2010-06-01 19:37:23 +10002504 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
Junxiao Bie1a86db2020-07-14 16:10:26 -07002505 rdev->sysfs_unack_badblocks =
2506 sysfs_get_dirent_safe(rdev->kobj.sd, "unacknowledged_bad_blocks");
2507 rdev->sysfs_badblocks =
2508 sysfs_get_dirent_safe(rdev->kobj.sd, "bad_blocks");
NeilBrown3c0ee632008-10-21 13:25:28 +11002509
NeilBrown4b809912008-07-21 17:05:25 +10002510 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002511 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002512
2513 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002514 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002517
2518 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002519 pr_warn("md: failed to register dev-%s for %s\n",
2520 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002521 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
2523
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002524static void rdev_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002525{
NeilBrown3cb03002011-10-11 16:45:26 +11002526 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002527 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002528 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002529}
2530
NeilBrownf72ffdd2014-09-30 14:23:59 +10002531static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
2533 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002534
Tejun Heo49731ba2011-01-14 18:43:57 +01002535 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002536 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002537 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +01002538 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002540 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002541 sysfs_put(rdev->sysfs_state);
Junxiao Bie1a86db2020-07-14 16:10:26 -07002542 sysfs_put(rdev->sysfs_unack_badblocks);
2543 sysfs_put(rdev->sysfs_badblocks);
NeilBrown3c0ee632008-10-21 13:25:28 +11002544 rdev->sysfs_state = NULL;
Junxiao Bie1a86db2020-07-14 16:10:26 -07002545 rdev->sysfs_unack_badblocks = NULL;
2546 rdev->sysfs_badblocks = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002547 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002548 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002549 * writing to 'remove' to "dev/state". We also need
2550 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002551 */
NeilBrown4b809912008-07-21 17:05:25 +10002552 synchronize_rcu();
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002553 INIT_WORK(&rdev->del_work, rdev_delayed_delete);
NeilBrown177a99b2008-02-06 01:39:56 -08002554 kobject_get(&rdev->kobj);
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02002555 queue_work(md_rdev_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556}
2557
2558/*
2559 * prevent the device from being mounted, repartitioned or
2560 * otherwise reused by a RAID array (or any other kernel
2561 * subsystem), by bd_claiming the device.
2562 */
NeilBrown3cb03002011-10-11 16:45:26 +11002563static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
2565 int err = 0;
2566 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Tejun Heod4d77622010-11-13 11:55:18 +01002568 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002569 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 if (IS_ERR(bdev)) {
Christoph Hellwigea3edd42020-03-24 08:25:11 +01002571 pr_warn("md: could not open device unknown-block(%u,%u).\n",
2572 MAJOR(dev), MINOR(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 return PTR_ERR(bdev);
2574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 rdev->bdev = bdev;
2576 return err;
2577}
2578
NeilBrown3cb03002011-10-11 16:45:26 +11002579static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
2581 struct block_device *bdev = rdev->bdev;
2582 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002583 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
2586void md_autodetect_dev(dev_t dev);
2587
NeilBrownf72ffdd2014-09-30 14:23:59 +10002588static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
2590 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002591
NeilBrown9d487392016-11-02 14:16:49 +11002592 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002593 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002595 if (test_bit(AutoDetected, &rdev->flags))
2596 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597#endif
2598 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002599 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600}
2601
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002602void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
2604 unbind_rdev_from_array(rdev);
2605 export_rdev(rdev);
2606}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002607EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
NeilBrownfd01b882011-10-11 16:47:53 +11002609static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
NeilBrown0638bb02014-09-25 17:43:47 +10002611 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
NeilBrown0638bb02014-09-25 17:43:47 +10002613 while (!list_empty(&mddev->disks)) {
2614 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2615 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002616 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 mddev->raid_disks = 0;
2619 mddev->major_version = 0;
2620}
2621
NeilBrown6497709b2017-03-15 14:05:14 +11002622static bool set_in_sync(struct mddev *mddev)
2623{
Shaohua Liefa4b772017-10-18 22:08:13 -07002624 lockdep_assert_held(&mddev->lock);
NeilBrown4ad23a972017-03-15 14:05:14 +11002625 if (!mddev->in_sync) {
2626 mddev->sync_checkers++;
2627 spin_unlock(&mddev->lock);
2628 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2629 spin_lock(&mddev->lock);
2630 if (!mddev->in_sync &&
2631 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002632 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002633 /*
2634 * Ensure ->in_sync is visible before we clear
2635 * ->sync_checkers.
2636 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002637 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002638 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2639 sysfs_notify_dirent_safe(mddev->sysfs_state);
2640 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002641 if (--mddev->sync_checkers == 0)
2642 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002643 }
2644 if (mddev->safemode == 1)
2645 mddev->safemode = 0;
2646 return mddev->in_sync;
2647}
2648
NeilBrownf72ffdd2014-09-30 14:23:59 +10002649static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
NeilBrown42543762006-06-26 00:27:57 -07002651 /* Update each superblock (in-memory image), but
2652 * if we are allowed to, skip spares which already
2653 * have the right event counter, or have one earlier
2654 * (which would mean they aren't being marked as dirty
2655 * with the rest of the array)
2656 */
NeilBrown3cb03002011-10-11 16:45:26 +11002657 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002658 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002659 if (rdev->sb_events == mddev->events ||
2660 (nospares &&
2661 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002662 rdev->sb_events+1 == mddev->events)) {
2663 /* Don't update this superblock */
2664 rdev->sb_loaded = 2;
2665 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002666 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002667 rdev->sb_loaded = 1;
2668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
2670}
2671
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002672static bool does_sb_need_changing(struct mddev *mddev)
2673{
2674 struct md_rdev *rdev;
2675 struct mdp_superblock_1 *sb;
2676 int role;
2677
2678 /* Find a good rdev */
2679 rdev_for_each(rdev, mddev)
2680 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2681 break;
2682
2683 /* No good device found. */
2684 if (!rdev)
2685 return false;
2686
2687 sb = page_address(rdev->sb_page);
2688 /* Check if a device has become faulty or a spare become active */
2689 rdev_for_each(rdev, mddev) {
2690 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2691 /* Device activated? */
2692 if (role == 0xffff && rdev->raid_disk >=0 &&
2693 !test_bit(Faulty, &rdev->flags))
2694 return true;
2695 /* Device turned faulty? */
2696 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2697 return true;
2698 }
2699
2700 /* Check if any mddev parameters have changed */
2701 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2702 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002703 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002704 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2705 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2706 return true;
2707
2708 return false;
2709}
2710
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002711void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
NeilBrown3cb03002011-10-11 16:45:26 +11002713 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002714 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002715 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002716 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002717 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
NeilBrownd87f0642013-04-24 11:42:40 +10002719 if (mddev->ro) {
2720 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002721 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002722 return;
2723 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002724
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002725repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002726 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002727 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002728 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002729 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002730 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002731 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002732 /* Has someone else has updated the sb */
2733 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002734 if (ret == 0)
2735 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002736 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2737 BIT(MD_SB_CHANGE_DEVS) |
2738 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002739 return;
2740 }
2741 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002742
NeilBrowndb0505d2017-10-17 16:18:36 +11002743 /*
2744 * First make sure individual recovery_offsets are correct
2745 * curr_resync_completed can only be used during recovery.
2746 * During reshape/resync it might use array-addresses rather
2747 * that device addresses.
2748 */
NeilBrowndafb20f2012-03-19 12:46:39 +11002749 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002750 if (rdev->raid_disk >= 0 &&
2751 mddev->delta_disks >= 0 &&
NeilBrowndb0505d2017-10-17 16:18:36 +11002752 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2753 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2754 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002755 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002756 !test_bit(In_sync, &rdev->flags) &&
2757 mddev->curr_resync_completed > rdev->recovery_offset)
2758 rdev->recovery_offset = mddev->curr_resync_completed;
2759
NeilBrownf72ffdd2014-09-30 14:23:59 +10002760 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002761 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002762 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2763 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002764 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002765 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002766 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002767 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002768 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002769 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002770 md_error(mddev, rdev);
2771 }
2772 clear_bit(Blocked, &rdev->flags);
2773 clear_bit(BlockedBadBlocks, &rdev->flags);
2774 wake_up(&rdev->blocked_wait);
2775 }
2776 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002777 wake_up(&mddev->sb_wait);
2778 return;
2779 }
2780
NeilBrown85572d72014-12-15 12:56:56 +11002781 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002782
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002783 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002784
Shaohua Li29530792016-12-08 15:48:19 -08002785 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002786 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002787 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002788 /* just a clean<-> dirty transition, possibly leave spares alone,
2789 * though if events isn't the right even/odd, we will have to do
2790 * spares after all
2791 */
2792 nospares = 1;
2793 if (force_change)
2794 nospares = 0;
2795 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002796 /* If the array is degraded, then skipping spares is both
2797 * dangerous and fairly pointless.
2798 * Dangerous because a device that was removed from the array
2799 * might have a event_count that still looks up-to-date,
2800 * so it can be re-added without a resync.
2801 * Pointless because if there are any spares to skip,
2802 * then a recovery will happen and soon that array won't
2803 * be degraded any more and the spare can go back to sleep then.
2804 */
NeilBrown850b2b422006-10-03 01:15:46 -07002805 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002806
NeilBrown06d91a52005-06-21 17:17:12 -07002807 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002808
2809 /* If this is just a dirty<->clean transition, and the array is clean
2810 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002811 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002812 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002813 && mddev->can_decrease_events
2814 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002815 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002816 mddev->can_decrease_events = 0;
2817 } else {
NeilBrown42543762006-06-26 00:27:57 -07002818 /* otherwise we have to go forward and ... */
2819 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002820 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
NeilBrown403df472014-09-30 15:52:29 +10002823 /*
2824 * This 64-bit counter should never wrap.
2825 * Either we are in around ~1 trillion A.C., assuming
2826 * 1 reboot per second, or we have a bug...
2827 */
2828 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002829
NeilBrowndafb20f2012-03-19 12:46:39 +11002830 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002831 if (rdev->badblocks.changed)
2832 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002833 if (test_bit(Faulty, &rdev->flags))
2834 set_bit(FaultRecorded, &rdev->flags);
2835 }
NeilBrown2699b672011-07-28 11:31:47 +10002836
NeilBrowne6910632008-02-06 01:39:51 -08002837 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002838 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
NeilBrown36a4e1f2011-10-07 14:23:17 +11002840 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2841 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Shaohua Li504634f2016-11-18 09:44:08 -08002843 if (mddev->queue)
2844 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002845rewrite:
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002846 md_bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002847 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002849
NeilBrown42543762006-06-26 00:27:57 -07002850 if (rdev->sb_loaded != 1)
2851 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
NeilBrownf4667222013-12-09 12:04:56 +11002853 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002854 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002855 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002856 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002857 pr_debug("md: (write) %s's sb offset: %llu\n",
2858 bdevname(rdev->bdev, b),
2859 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002860 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002861 if (rdev->badblocks.size) {
2862 md_super_write(mddev, rdev,
2863 rdev->badblocks.sector,
2864 rdev->badblocks.size << 9,
2865 rdev->bb_page);
2866 rdev->badblocks.size = 0;
2867 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002868
NeilBrownf4667222013-12-09 12:04:56 +11002869 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002870 pr_debug("md: %s (skipping faulty)\n",
2871 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002872
NeilBrown7bfa19f2005-06-21 17:17:28 -07002873 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 /* only need to write one superblock... */
2875 break;
2876 }
NeilBrown46533ff2016-11-18 16:16:11 +11002877 if (md_super_wait(mddev) < 0)
2878 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002879 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002880
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002881 if (mddev_is_clustered(mddev) && ret == 0)
2882 md_cluster_ops->metadata_update_finish(mddev);
2883
NeilBrown850b2b422006-10-03 01:15:46 -07002884 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002885 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2886 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002887 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002888 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002889 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002890 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
Junxiao Bie1a86db2020-07-14 16:10:26 -07002891 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown06d91a52005-06-21 17:17:12 -07002892
NeilBrowndafb20f2012-03-19 12:46:39 +11002893 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002894 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2895 clear_bit(Blocked, &rdev->flags);
2896
2897 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002898 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002899 clear_bit(BlockedBadBlocks, &rdev->flags);
2900 wake_up(&rdev->blocked_wait);
2901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002903EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002905static int add_bound_rdev(struct md_rdev *rdev)
2906{
2907 struct mddev *mddev = rdev->mddev;
2908 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002909 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002910
Shaohua Li87d4d912016-01-06 14:37:14 -08002911 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002912 /* If there is hot_add_disk but no hot_remove_disk
2913 * then added disks for geometry changes,
2914 * and should be added immediately.
2915 */
2916 super_types[mddev->major_version].
2917 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002918 if (add_journal)
2919 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002920 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002921 if (add_journal)
2922 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002923 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002924 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002925 return err;
2926 }
2927 }
2928 sysfs_notify_dirent_safe(rdev->sysfs_state);
2929
Shaohua Li29530792016-12-08 15:48:19 -08002930 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002931 if (mddev->degraded)
2932 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2933 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2934 md_new_event(mddev);
2935 md_wakeup_thread(mddev->thread);
2936 return 0;
2937}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Andre Noll7f6ce762008-03-23 18:34:54 +01002939/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002940 * We want to accept with case. For this we use cmd_match.
2941 */
2942static int cmd_match(const char *cmd, const char *str)
2943{
2944 /* See if cmd, written into a sysfs file, matches
2945 * str. They must either be the same, or cmd can
2946 * have a trailing newline
2947 */
2948 while (*cmd && *str && *cmd == *str) {
2949 cmd++;
2950 str++;
2951 }
2952 if (*cmd == '\n')
2953 cmd++;
2954 if (*str || *cmd)
2955 return 0;
2956 return 1;
2957}
2958
NeilBrown86e6ffd2005-11-08 21:39:24 -08002959struct rdev_sysfs_entry {
2960 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002961 ssize_t (*show)(struct md_rdev *, char *);
2962 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002963};
2964
2965static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002966state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002967{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002968 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002969 size_t len = 0;
Mark Rutland6aa7de02017-10-23 14:07:29 -07002970 unsigned long flags = READ_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002971
NeilBrown758bfc82014-12-15 12:56:59 +11002972 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002973 (!test_bit(ExternalBbl, &flags) &&
2974 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002975 len += sprintf(page+len, "faulty%s", sep);
2976 if (test_bit(In_sync, &flags))
2977 len += sprintf(page+len, "in_sync%s", sep);
2978 if (test_bit(Journal, &flags))
2979 len += sprintf(page+len, "journal%s", sep);
2980 if (test_bit(WriteMostly, &flags))
2981 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002982 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002983 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002984 && !test_bit(Faulty, &flags)))
2985 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002986 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002987 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002988 !test_bit(In_sync, &flags))
2989 len += sprintf(page+len, "spare%s", sep);
2990 if (test_bit(WriteErrorSeen, &flags))
2991 len += sprintf(page+len, "write_error%s", sep);
2992 if (test_bit(WantReplacement, &flags))
2993 len += sprintf(page+len, "want_replacement%s", sep);
2994 if (test_bit(Replacement, &flags))
2995 len += sprintf(page+len, "replacement%s", sep);
2996 if (test_bit(ExternalBbl, &flags))
2997 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002998 if (test_bit(FailFast, &flags))
2999 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02003000
3001 if (len)
3002 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11003003
NeilBrown86e6ffd2005-11-08 21:39:24 -08003004 return len+sprintf(page+len, "\n");
3005}
3006
NeilBrown45dc2de2006-06-26 00:27:58 -07003007static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003008state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07003009{
3010 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10003011 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07003012 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07003013 * writemostly - sets write_mostly
3014 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10003015 * blocked - sets the Blocked flags
3016 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10003017 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11003018 * -insync - clear Insync for a device with a slot assigned,
3019 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10003020 * write_error - sets WriteErrorSeen
3021 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11003022 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07003023 */
3024 int err = -EINVAL;
3025 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
3026 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10003027 if (test_bit(Faulty, &rdev->flags))
3028 err = 0;
3029 else
3030 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07003031 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07003032 if (rdev->mddev->pers) {
3033 clear_bit(Blocked, &rdev->flags);
3034 remove_and_add_spares(rdev->mddev, rdev);
3035 }
NeilBrown45dc2de2006-06-26 00:27:58 -07003036 if (rdev->raid_disk >= 0)
3037 err = -EBUSY;
3038 else {
NeilBrownfd01b882011-10-11 16:47:53 +11003039 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07003040 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08003041 if (mddev_is_clustered(mddev))
3042 err = md_cluster_ops->remove_disk(mddev, rdev);
3043
3044 if (err == 0) {
3045 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11003046 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08003047 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11003048 md_wakeup_thread(mddev->thread);
3049 }
Guoqing Jianga9720902015-10-12 17:21:27 +08003050 md_new_event(mddev);
3051 }
NeilBrown45dc2de2006-06-26 00:27:58 -07003052 }
NeilBrownf6556752006-06-26 00:28:01 -07003053 } else if (cmd_match(buf, "writemostly")) {
3054 set_bit(WriteMostly, &rdev->flags);
Guoqing Jiang404659c2019-12-23 10:48:53 +01003055 mddev_create_serial_pool(rdev->mddev, rdev, false);
NeilBrownf6556752006-06-26 00:28:01 -07003056 err = 0;
3057 } else if (cmd_match(buf, "-writemostly")) {
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +01003058 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
NeilBrownf6556752006-06-26 00:28:01 -07003059 clear_bit(WriteMostly, &rdev->flags);
3060 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003061 } else if (cmd_match(buf, "blocked")) {
3062 set_bit(Blocked, &rdev->flags);
3063 err = 0;
3064 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10003065 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02003066 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10003067 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10003068 /* metadata handler doesn't understand badblocks,
3069 * so we need to fail the device
3070 */
3071 md_error(rdev->mddev, rdev);
3072 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003073 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10003074 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003075 wake_up(&rdev->blocked_wait);
3076 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3077 md_wakeup_thread(rdev->mddev->thread);
3078
3079 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10003080 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
3081 set_bit(In_sync, &rdev->flags);
3082 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11003083 } else if (cmd_match(buf, "failfast")) {
3084 set_bit(FailFast, &rdev->flags);
3085 err = 0;
3086 } else if (cmd_match(buf, "-failfast")) {
3087 clear_bit(FailFast, &rdev->flags);
3088 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07003089 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
3090 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10003091 if (rdev->mddev->pers == NULL) {
3092 clear_bit(In_sync, &rdev->flags);
3093 rdev->saved_raid_disk = rdev->raid_disk;
3094 rdev->raid_disk = -1;
3095 err = 0;
3096 }
NeilBrownd7a9d442011-07-28 11:31:48 +10003097 } else if (cmd_match(buf, "write_error")) {
3098 set_bit(WriteErrorSeen, &rdev->flags);
3099 err = 0;
3100 } else if (cmd_match(buf, "-write_error")) {
3101 clear_bit(WriteErrorSeen, &rdev->flags);
3102 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11003103 } else if (cmd_match(buf, "want_replacement")) {
3104 /* Any non-spare device that is not a replacement can
3105 * become want_replacement at any time, but we then need to
3106 * check if recovery is needed.
3107 */
3108 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07003109 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11003110 !test_bit(Replacement, &rdev->flags))
3111 set_bit(WantReplacement, &rdev->flags);
3112 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3113 md_wakeup_thread(rdev->mddev->thread);
3114 err = 0;
3115 } else if (cmd_match(buf, "-want_replacement")) {
3116 /* Clearing 'want_replacement' is always allowed.
3117 * Once replacements starts it is too late though.
3118 */
3119 err = 0;
3120 clear_bit(WantReplacement, &rdev->flags);
3121 } else if (cmd_match(buf, "replacement")) {
3122 /* Can only set a device as a replacement when array has not
3123 * yet been started. Once running, replacement is automatic
3124 * from spares, or by assigning 'slot'.
3125 */
3126 if (rdev->mddev->pers)
3127 err = -EBUSY;
3128 else {
3129 set_bit(Replacement, &rdev->flags);
3130 err = 0;
3131 }
3132 } else if (cmd_match(buf, "-replacement")) {
3133 /* Similarly, can only clear Replacement before start */
3134 if (rdev->mddev->pers)
3135 err = -EBUSY;
3136 else {
3137 clear_bit(Replacement, &rdev->flags);
3138 err = 0;
3139 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05003140 } else if (cmd_match(buf, "re-add")) {
Yufen Yuee37e622019-04-02 14:22:14 +08003141 if (!rdev->mddev->pers)
3142 err = -EINVAL;
3143 else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) &&
3144 rdev->saved_raid_disk >= 0) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05003145 /* clear_bit is performed _after_ all the devices
3146 * have their local Faulty bit cleared. If any writes
3147 * happen in the meantime in the local node, they
3148 * will land in the local bitmap, which will be synced
3149 * by this node eventually
3150 */
3151 if (!mddev_is_clustered(rdev->mddev) ||
3152 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
3153 clear_bit(Faulty, &rdev->flags);
3154 err = add_bound_rdev(rdev);
3155 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05003156 } else
3157 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02003158 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
3159 set_bit(ExternalBbl, &rdev->flags);
3160 rdev->badblocks.shift = 0;
3161 err = 0;
3162 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
3163 clear_bit(ExternalBbl, &rdev->flags);
3164 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07003165 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10003166 if (!err)
3167 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07003168 return err ? err : len;
3169}
NeilBrown80ca3a42006-07-10 04:44:18 -07003170static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10003171__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003172
3173static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003174errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08003175{
3176 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
3177}
3178
3179static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003180errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08003181{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003182 unsigned int n;
3183 int rv;
3184
3185 rv = kstrtouint(buf, 10, &n);
3186 if (rv < 0)
3187 return rv;
3188 atomic_set(&rdev->corrected_errors, n);
3189 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08003190}
3191static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07003192__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08003193
NeilBrown014236d2006-01-06 00:20:55 -08003194static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003195slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08003196{
Shaohua Lif2076e72015-10-08 21:54:12 -07003197 if (test_bit(Journal, &rdev->flags))
3198 return sprintf(page, "journal\n");
3199 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08003200 return sprintf(page, "none\n");
3201 else
3202 return sprintf(page, "%d\n", rdev->raid_disk);
3203}
3204
3205static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003206slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08003207{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003208 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08003209 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003210
Shaohua Lif2076e72015-10-08 21:54:12 -07003211 if (test_bit(Journal, &rdev->flags))
3212 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08003213 if (strncmp(buf, "none", 4)==0)
3214 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003215 else {
3216 err = kstrtouint(buf, 10, (unsigned int *)&slot);
3217 if (err < 0)
3218 return err;
3219 }
Neil Brown6c2fce22008-06-28 08:31:31 +10003220 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08003221 /* Setting 'slot' on an active array requires also
3222 * updating the 'rd%d' link, and communicating
3223 * with the personality with ->hot_*_disk.
3224 * For now we only support removing
3225 * failed/spare devices. This normally happens automatically,
3226 * but not when the metadata is externally managed.
3227 */
NeilBrownc303da62008-02-06 01:39:51 -08003228 if (rdev->raid_disk == -1)
3229 return -EEXIST;
3230 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10003231 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08003232 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10003233 clear_bit(Blocked, &rdev->flags);
3234 remove_and_add_spares(rdev->mddev, rdev);
3235 if (rdev->raid_disk >= 0)
3236 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08003237 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3238 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10003239 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10003240 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10003241 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10003242 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11003243 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10003244
3245 if (rdev->raid_disk != -1)
3246 return -EBUSY;
3247
NeilBrownc6751b22011-02-02 11:57:13 +11003248 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
3249 return -EBUSY;
3250
Neil Brown6c2fce22008-06-28 08:31:31 +10003251 if (rdev->mddev->pers->hot_add_disk == NULL)
3252 return -EINVAL;
3253
NeilBrownba1b41b2011-01-14 09:14:34 +11003254 if (slot >= rdev->mddev->raid_disks &&
3255 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
3256 return -ENOSPC;
3257
Neil Brown6c2fce22008-06-28 08:31:31 +10003258 rdev->raid_disk = slot;
3259 if (test_bit(In_sync, &rdev->flags))
3260 rdev->saved_raid_disk = slot;
3261 else
3262 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11003263 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11003264 clear_bit(Bitmap_sync, &rdev->flags);
Guoqing Jiang3f79cc22020-04-04 23:57:11 +02003265 err = rdev->mddev->pers->hot_add_disk(rdev->mddev, rdev);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11003266 if (err) {
3267 rdev->raid_disk = -1;
3268 return err;
3269 } else
3270 sysfs_notify_dirent_safe(rdev->sysfs_state);
Damien Le Moal5e3b8a82020-07-16 13:54:40 +09003271 /* failure here is OK */;
3272 sysfs_link_rdev(rdev->mddev, rdev);
Neil Brown6c2fce22008-06-28 08:31:31 +10003273 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08003274 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11003275 if (slot >= rdev->mddev->raid_disks &&
3276 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08003277 return -ENOSPC;
3278 rdev->raid_disk = slot;
3279 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08003280 clear_bit(Faulty, &rdev->flags);
3281 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08003282 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003283 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08003284 }
NeilBrown014236d2006-01-06 00:20:55 -08003285 return len;
3286}
3287
NeilBrown014236d2006-01-06 00:20:55 -08003288static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07003289__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08003290
NeilBrown93c8cad2006-01-06 00:20:56 -08003291static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003292offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08003293{
Andrew Morton6961ece2006-01-06 00:20:59 -08003294 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08003295}
3296
3297static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003298offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08003299{
NeilBrownc6563a82012-05-21 09:27:00 +10003300 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003301 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003302 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10003303 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003304 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003305 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08003306 /* Must set offset before size, so overlap checks
3307 * can be sane */
3308 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08003309 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10003310 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08003311 return len;
3312}
3313
3314static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07003315__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08003316
NeilBrownc6563a82012-05-21 09:27:00 +10003317static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3318{
3319 return sprintf(page, "%llu\n",
3320 (unsigned long long)rdev->new_data_offset);
3321}
3322
3323static ssize_t new_offset_store(struct md_rdev *rdev,
3324 const char *buf, size_t len)
3325{
3326 unsigned long long new_offset;
3327 struct mddev *mddev = rdev->mddev;
3328
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003329 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10003330 return -EINVAL;
3331
NeilBrownf851b602014-12-11 10:02:10 +11003332 if (mddev->sync_thread ||
3333 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10003334 return -EBUSY;
3335 if (new_offset == rdev->data_offset)
3336 /* reset is always permitted */
3337 ;
3338 else if (new_offset > rdev->data_offset) {
3339 /* must not push array size beyond rdev_sectors */
3340 if (new_offset - rdev->data_offset
3341 + mddev->dev_sectors > rdev->sectors)
3342 return -E2BIG;
3343 }
3344 /* Metadata worries about other space details. */
3345
3346 /* decreasing the offset is inconsistent with a backwards
3347 * reshape.
3348 */
3349 if (new_offset < rdev->data_offset &&
3350 mddev->reshape_backwards)
3351 return -EINVAL;
3352 /* Increasing offset is inconsistent with forwards
3353 * reshape. reshape_direction should be set to
3354 * 'backwards' first.
3355 */
3356 if (new_offset > rdev->data_offset &&
3357 !mddev->reshape_backwards)
3358 return -EINVAL;
3359
3360 if (mddev->pers && mddev->persistent &&
3361 !super_types[mddev->major_version]
3362 .allow_new_offset(rdev, new_offset))
3363 return -E2BIG;
3364 rdev->new_data_offset = new_offset;
3365 if (new_offset > rdev->data_offset)
3366 mddev->reshape_backwards = 1;
3367 else if (new_offset < rdev->data_offset)
3368 mddev->reshape_backwards = 0;
3369
3370 return len;
3371}
3372static struct rdev_sysfs_entry rdev_new_offset =
3373__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3374
NeilBrown83303b62006-01-06 00:21:06 -08003375static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003376rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003377{
Andre Nolldd8ac332009-03-31 14:33:13 +11003378 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003379}
3380
NeilBrownc5d79ad2008-02-06 01:39:54 -08003381static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3382{
3383 /* check if two start/length pairs overlap */
3384 if (s1+l1 <= s2)
3385 return 0;
3386 if (s2+l2 <= s1)
3387 return 0;
3388 return 1;
3389}
3390
Dan Williamsb522adc2009-03-31 15:00:31 +11003391static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3392{
3393 unsigned long long blocks;
3394 sector_t new;
3395
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003396 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003397 return -EINVAL;
3398
3399 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3400 return -EINVAL; /* sector conversion overflow */
3401
3402 new = blocks * 2;
3403 if (new != blocks * 2)
3404 return -EINVAL; /* unsigned long long to sector_t overflow */
3405
3406 *sectors = new;
3407 return 0;
3408}
3409
NeilBrown83303b62006-01-06 00:21:06 -08003410static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003411rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003412{
NeilBrownfd01b882011-10-11 16:47:53 +11003413 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003414 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003415 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003416
Shaohua Lif2076e72015-10-08 21:54:12 -07003417 if (test_bit(Journal, &rdev->flags))
3418 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003419 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003420 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003421 if (rdev->data_offset != rdev->new_data_offset)
3422 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003423 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003424 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003425 sectors = super_types[my_mddev->major_version].
3426 rdev_size_change(rdev, sectors);
3427 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003428 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003429 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003430 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003431 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003432 if (!my_mddev->pers->resize)
3433 /* Cannot change size for RAID0 or Linear etc */
3434 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003435 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003436 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003437 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003438
Andre Nolldd8ac332009-03-31 14:33:13 +11003439 rdev->sectors = sectors;
3440 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003441 /* Need to check that all other rdevs with the same
3442 * ->bdev do not overlap. 'rcu' is sufficient to walk
3443 * the rdev lists safely.
3444 * This check does not provide a hard guarantee, it
3445 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003446 */
NeilBrownfd01b882011-10-11 16:47:53 +11003447 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003448 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003449 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003450
NeilBrown8b1afc32014-09-29 15:33:20 +10003451 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003452 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003453 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003454
NeilBrowndafb20f2012-03-19 12:46:39 +11003455 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003456 if (rdev->bdev == rdev2->bdev &&
3457 rdev != rdev2 &&
3458 overlaps(rdev->data_offset, rdev->sectors,
3459 rdev2->data_offset,
3460 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003461 overlap = 1;
3462 break;
3463 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003464 if (overlap) {
3465 mddev_put(mddev);
3466 break;
3467 }
3468 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003469 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003470 if (overlap) {
3471 /* Someone else could have slipped in a size
3472 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003473 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003474 * safe, and trust userspace not to race with
3475 * itself
3476 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003477 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003478 return -EBUSY;
3479 }
3480 }
NeilBrown83303b62006-01-06 00:21:06 -08003481 return len;
3482}
3483
3484static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003485__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003486
NeilBrown3cb03002011-10-11 16:45:26 +11003487static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003488{
3489 unsigned long long recovery_start = rdev->recovery_offset;
3490
3491 if (test_bit(In_sync, &rdev->flags) ||
3492 recovery_start == MaxSector)
3493 return sprintf(page, "none\n");
3494
3495 return sprintf(page, "%llu\n", recovery_start);
3496}
3497
NeilBrown3cb03002011-10-11 16:45:26 +11003498static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003499{
3500 unsigned long long recovery_start;
3501
3502 if (cmd_match(buf, "none"))
3503 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003504 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003505 return -EINVAL;
3506
3507 if (rdev->mddev->pers &&
3508 rdev->raid_disk >= 0)
3509 return -EBUSY;
3510
3511 rdev->recovery_offset = recovery_start;
3512 if (recovery_start == MaxSector)
3513 set_bit(In_sync, &rdev->flags);
3514 else
3515 clear_bit(In_sync, &rdev->flags);
3516 return len;
3517}
3518
3519static struct rdev_sysfs_entry rdev_recovery_start =
3520__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3521
Vishal Vermafc974ee2015-12-24 19:20:34 -07003522/* sysfs access to bad-blocks list.
3523 * We present two files.
3524 * 'bad-blocks' lists sector numbers and lengths of ranges that
3525 * are recorded as bad. The list is truncated to fit within
3526 * the one-page limit of sysfs.
3527 * Writing "sector length" to this file adds an acknowledged
3528 * bad block list.
3529 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3530 * been acknowledged. Writing to this file adds bad blocks
3531 * without acknowledging them. This is largely for testing.
3532 */
NeilBrown3cb03002011-10-11 16:45:26 +11003533static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003534{
3535 return badblocks_show(&rdev->badblocks, page, 0);
3536}
NeilBrown3cb03002011-10-11 16:45:26 +11003537static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003538{
NeilBrownde393cd2011-07-28 11:31:48 +10003539 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3540 /* Maybe that ack was all we needed */
3541 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3542 wake_up(&rdev->blocked_wait);
3543 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003544}
3545static struct rdev_sysfs_entry rdev_bad_blocks =
3546__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3547
NeilBrown3cb03002011-10-11 16:45:26 +11003548static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003549{
3550 return badblocks_show(&rdev->badblocks, page, 1);
3551}
NeilBrown3cb03002011-10-11 16:45:26 +11003552static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003553{
3554 return badblocks_store(&rdev->badblocks, page, len, 1);
3555}
3556static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3557__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3558
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003559static ssize_t
3560ppl_sector_show(struct md_rdev *rdev, char *page)
3561{
3562 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3563}
3564
3565static ssize_t
3566ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3567{
3568 unsigned long long sector;
3569
3570 if (kstrtoull(buf, 10, &sector) < 0)
3571 return -EINVAL;
3572 if (sector != (sector_t)sector)
3573 return -EINVAL;
3574
3575 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3576 rdev->raid_disk >= 0)
3577 return -EBUSY;
3578
3579 if (rdev->mddev->persistent) {
3580 if (rdev->mddev->major_version == 0)
3581 return -EINVAL;
3582 if ((sector > rdev->sb_start &&
3583 sector - rdev->sb_start > S16_MAX) ||
3584 (sector < rdev->sb_start &&
3585 rdev->sb_start - sector > -S16_MIN))
3586 return -EINVAL;
3587 rdev->ppl.offset = sector - rdev->sb_start;
3588 } else if (!rdev->mddev->external) {
3589 return -EBUSY;
3590 }
3591 rdev->ppl.sector = sector;
3592 return len;
3593}
3594
3595static struct rdev_sysfs_entry rdev_ppl_sector =
3596__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3597
3598static ssize_t
3599ppl_size_show(struct md_rdev *rdev, char *page)
3600{
3601 return sprintf(page, "%u\n", rdev->ppl.size);
3602}
3603
3604static ssize_t
3605ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3606{
3607 unsigned int size;
3608
3609 if (kstrtouint(buf, 10, &size) < 0)
3610 return -EINVAL;
3611
3612 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3613 rdev->raid_disk >= 0)
3614 return -EBUSY;
3615
3616 if (rdev->mddev->persistent) {
3617 if (rdev->mddev->major_version == 0)
3618 return -EINVAL;
3619 if (size > U16_MAX)
3620 return -EINVAL;
3621 } else if (!rdev->mddev->external) {
3622 return -EBUSY;
3623 }
3624 rdev->ppl.size = size;
3625 return len;
3626}
3627
3628static struct rdev_sysfs_entry rdev_ppl_size =
3629__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3630
NeilBrown86e6ffd2005-11-08 21:39:24 -08003631static struct attribute *rdev_default_attrs[] = {
3632 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003633 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003634 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003635 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003636 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003637 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003638 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003639 &rdev_bad_blocks.attr,
3640 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003641 &rdev_ppl_sector.attr,
3642 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003643 NULL,
3644};
3645static ssize_t
3646rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3647{
3648 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003649 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003650
3651 if (!entry->show)
3652 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003653 if (!rdev->mddev)
Marcos Paulo de Souza168b3052019-06-14 15:41:06 -07003654 return -ENODEV;
NeilBrown758bfc82014-12-15 12:56:59 +11003655 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003656}
3657
3658static ssize_t
3659rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3660 const char *page, size_t length)
3661{
3662 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003663 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003664 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003665 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003666
3667 if (!entry->store)
3668 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003669 if (!capable(CAP_SYS_ADMIN))
3670 return -EACCES;
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003671 rv = mddev ? mddev_lock(mddev) : -ENODEV;
NeilBrownca388052008-02-06 01:39:55 -08003672 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003673 if (rdev->mddev == NULL)
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003674 rv = -ENODEV;
NeilBrown27c529b2008-03-04 14:29:33 -08003675 else
3676 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003677 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003678 }
3679 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003680}
3681
3682static void rdev_free(struct kobject *ko)
3683{
NeilBrown3cb03002011-10-11 16:45:26 +11003684 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003685 kfree(rdev);
3686}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003687static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003688 .show = rdev_attr_show,
3689 .store = rdev_attr_store,
3690};
3691static struct kobj_type rdev_ktype = {
3692 .release = rdev_free,
3693 .sysfs_ops = &rdev_sysfs_ops,
3694 .default_attrs = rdev_default_attrs,
3695};
3696
NeilBrown3cb03002011-10-11 16:45:26 +11003697int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003698{
3699 rdev->desc_nr = -1;
3700 rdev->saved_raid_disk = -1;
3701 rdev->raid_disk = -1;
3702 rdev->flags = 0;
3703 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003704 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003705 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003706 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003707 rdev->sb_loaded = 0;
3708 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003709 atomic_set(&rdev->nr_pending, 0);
3710 atomic_set(&rdev->read_errors, 0);
3711 atomic_set(&rdev->corrected_errors, 0);
3712
3713 INIT_LIST_HEAD(&rdev->same_set);
3714 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003715
3716 /* Add space to store bad block list.
3717 * This reserves the space even on arrays where it cannot
3718 * be used - I wonder if that matters
3719 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003720 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003721}
3722EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723/*
3724 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3725 *
3726 * mark the device faulty if:
3727 *
3728 * - the device is nonexistent (zero size)
3729 * - the device has no valid superblock
3730 *
3731 * a faulty rdev _never_ has rdev->sb set.
3732 */
NeilBrown3cb03002011-10-11 16:45:26 +11003733static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734{
3735 char b[BDEVNAME_SIZE];
3736 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003737 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 sector_t size;
3739
NeilBrown9ffae0c2006-01-06 00:20:32 -08003740 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003741 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
NeilBrown2230dfe2011-07-28 11:31:46 +10003744 err = md_rdev_init(rdev);
3745 if (err)
3746 goto abort_free;
3747 err = alloc_disk_sb(rdev);
3748 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 goto abort_free;
3750
NeilBrownc5d79ad2008-02-06 01:39:54 -08003751 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 if (err)
3753 goto abort_free;
3754
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003755 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003756
Mike Snitzer77304d22010-11-08 14:39:12 +01003757 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003759 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 bdevname(rdev->bdev,b));
3761 err = -EINVAL;
3762 goto abort_free;
3763 }
3764
3765 if (super_format >= 0) {
3766 err = super_types[super_format].
3767 load_super(rdev, NULL, super_minor);
3768 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003769 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003770 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003771 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 goto abort_free;
3773 }
3774 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003775 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 bdevname(rdev->bdev,b));
3777 goto abort_free;
3778 }
3779 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003780
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 return rdev;
3782
3783abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003784 if (rdev->bdev)
3785 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003786 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 kfree(rdev);
3788 return ERR_PTR(err);
3789}
3790
3791/*
3792 * Check a full RAID array for plausibility
3793 */
3794
Yufen Yu6a5cb532019-10-16 16:00:03 +08003795static int analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796{
3797 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003798 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 char b[BDEVNAME_SIZE];
3800
3801 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003802 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 switch (super_types[mddev->major_version].
3804 load_super(rdev, freshest, mddev->minor_version)) {
3805 case 1:
3806 freshest = rdev;
3807 break;
3808 case 0:
3809 break;
3810 default:
NeilBrown9d487392016-11-02 14:16:49 +11003811 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003813 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 }
3815
Yufen Yu6a5cb532019-10-16 16:00:03 +08003816 /* Cannot find a valid fresh disk */
3817 if (!freshest) {
3818 pr_warn("md: cannot find a valid disk\n");
3819 return -EINVAL;
3820 }
3821
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 super_types[mddev->major_version].
3823 validate_super(mddev, freshest);
3824
3825 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003826 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003827 if (mddev->max_disks &&
3828 (rdev->desc_nr >= mddev->max_disks ||
3829 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003830 pr_warn("md: %s: %s: only %d devices permitted\n",
3831 mdname(mddev), bdevname(rdev->bdev, b),
3832 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003833 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003834 continue;
3835 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003836 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 if (super_types[mddev->major_version].
3838 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003839 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003841 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 continue;
3843 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 if (mddev->level == LEVEL_MULTIPATH) {
3846 rdev->desc_nr = i++;
3847 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003848 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003849 } else if (rdev->raid_disk >=
3850 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3851 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003852 rdev->raid_disk = -1;
3853 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 }
3855 }
Yufen Yu6a5cb532019-10-16 16:00:03 +08003856
3857 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858}
3859
NeilBrown72e02072009-12-14 12:49:55 +11003860/* Read a fixed-point number.
3861 * Numbers in sysfs attributes should be in "standard" units where
3862 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003863 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003864 * milliseconds or jiffies.
3865 * This function takes a decimal number with a possible fractional
3866 * component, and produces an integer which is the result of
3867 * multiplying that number by 10^'scale'.
3868 * all without any floating-point arithmetic.
3869 */
3870int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3871{
3872 unsigned long result = 0;
3873 long decimals = -1;
3874 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3875 if (*cp == '.')
3876 decimals = 0;
3877 else if (decimals < scale) {
3878 unsigned int value;
3879 value = *cp - '0';
3880 result = result * 10 + value;
3881 if (decimals >= 0)
3882 decimals++;
3883 }
3884 cp++;
3885 }
3886 if (*cp == '\n')
3887 cp++;
3888 if (*cp)
3889 return -EINVAL;
3890 if (decimals < 0)
3891 decimals = 0;
Andy Shevchenkocf891602019-07-23 23:41:55 +03003892 *res = result * int_pow(10, scale - decimals);
NeilBrown72e02072009-12-14 12:49:55 +11003893 return 0;
3894}
3895
NeilBrowneae17012005-11-08 21:39:23 -08003896static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003897safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003898{
3899 int msec = (mddev->safemode_delay*1000)/HZ;
3900 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3901}
3902static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003903safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003904{
NeilBrown16f17b32006-06-26 00:27:37 -07003905 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003906
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003907 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003908 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003909 return -EINVAL;
3910 }
3911
NeilBrown72e02072009-12-14 12:49:55 +11003912 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003913 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003914 if (msec == 0)
3915 mddev->safemode_delay = 0;
3916 else {
NeilBrown19052c02008-08-05 15:54:13 +10003917 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003918 unsigned long new_delay = (msec*HZ)/1000;
3919
3920 if (new_delay == 0)
3921 new_delay = 1;
3922 mddev->safemode_delay = new_delay;
3923 if (new_delay < old_delay || old_delay == 0)
3924 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003925 }
3926 return len;
3927}
3928static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003929__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003930
3931static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003932level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003933{
NeilBrown36d091f2014-12-15 12:56:58 +11003934 struct md_personality *p;
3935 int ret;
3936 spin_lock(&mddev->lock);
3937 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003938 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003939 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003940 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003941 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003942 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003943 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003944 else
NeilBrown36d091f2014-12-15 12:56:58 +11003945 ret = 0;
3946 spin_unlock(&mddev->lock);
3947 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003948}
3949
NeilBrownd9d166c2006-01-06 00:20:51 -08003950static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003951level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003952{
Dan Williamsf2859af2010-05-02 10:04:16 -07003953 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003954 ssize_t rv;
3955 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003956 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003957 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003958 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003959 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003960
NeilBrown67918752014-12-15 12:57:01 +11003961 if (slen == 0 || slen >= sizeof(clevel))
3962 return -EINVAL;
3963
3964 rv = mddev_lock(mddev);
3965 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003966 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003967
3968 if (mddev->pers == NULL) {
3969 strncpy(mddev->clevel, buf, slen);
3970 if (mddev->clevel[slen-1] == '\n')
3971 slen--;
3972 mddev->clevel[slen] = 0;
3973 mddev->level = LEVEL_NONE;
3974 rv = len;
3975 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003976 }
NeilBrown67918752014-12-15 12:57:01 +11003977 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003978 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003979 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003980
3981 /* request to change the personality. Need to ensure:
3982 * - array is not engaged in resync/recovery/reshape
3983 * - old personality can be suspended
3984 * - new personality will access other array.
3985 */
3986
NeilBrown67918752014-12-15 12:57:01 +11003987 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003988 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003989 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003990 mddev->reshape_position != MaxSector ||
3991 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003992 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003993
NeilBrown67918752014-12-15 12:57:01 +11003994 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003995 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003996 pr_warn("md: %s: %s does not support online personality change\n",
3997 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003998 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003999 }
4000
4001 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11004002 strncpy(clevel, buf, slen);
4003 if (clevel[slen-1] == '\n')
4004 slen--;
4005 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004006 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07004007 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11004008
Dan Williamsf2859af2010-05-02 10:04:16 -07004009 if (request_module("md-%s", clevel) != 0)
4010 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11004011 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07004012 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11004013 if (!pers || !try_module_get(pers->owner)) {
4014 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11004015 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11004016 rv = -EINVAL;
4017 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11004018 }
4019 spin_unlock(&pers_lock);
4020
4021 if (pers == mddev->pers) {
4022 /* Nothing to do! */
4023 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11004024 rv = len;
4025 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11004026 }
4027 if (!pers->takeover) {
4028 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11004029 pr_warn("md: %s: %s does not support personality takeover\n",
4030 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11004031 rv = -EINVAL;
4032 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11004033 }
4034
NeilBrowndafb20f2012-03-19 12:46:39 +11004035 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01004036 rdev->new_raid_disk = rdev->raid_disk;
4037
NeilBrown245f46c2009-03-31 14:39:39 +11004038 /* ->takeover must set new_* and/or delta_disks
4039 * if it succeeds, and may set them when it fails.
4040 */
4041 priv = pers->takeover(mddev);
4042 if (IS_ERR(priv)) {
4043 mddev->new_level = mddev->level;
4044 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004045 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11004046 mddev->raid_disks -= mddev->delta_disks;
4047 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004048 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11004049 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11004050 pr_warn("md: %s: %s would not accept array\n",
4051 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11004052 rv = PTR_ERR(priv);
4053 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11004054 }
4055
4056 /* Looks like we have a winner */
4057 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11004058 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11004059
4060 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11004061 oldpers = mddev->pers;
4062 oldpriv = mddev->private;
4063 mddev->pers = pers;
4064 mddev->private = priv;
4065 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
4066 mddev->level = mddev->new_level;
4067 mddev->layout = mddev->new_layout;
4068 mddev->chunk_sectors = mddev->new_chunk_sectors;
4069 mddev->delta_disks = 0;
4070 mddev->reshape_backwards = 0;
4071 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11004072 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10004073
NeilBrowndb721d32014-12-15 12:56:58 +11004074 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11004075 mddev->external) {
4076 /* We are converting from a no-redundancy array
4077 * to a redundancy array and metadata is managed
4078 * externally so we need to be sure that writes
4079 * won't block due to a need to transition
4080 * clean->dirty
4081 * until external management is started.
4082 */
4083 mddev->in_sync = 0;
4084 mddev->safemode_delay = 0;
4085 mddev->safemode = 0;
4086 }
4087
NeilBrowndb721d32014-12-15 12:56:58 +11004088 oldpers->free(mddev, oldpriv);
4089
4090 if (oldpers->sync_request == NULL &&
4091 pers->sync_request != NULL) {
4092 /* need to add the md_redundancy_group */
4093 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11004094 pr_warn("md: cannot register extra attributes for %s\n",
4095 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11004096 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
4097 }
4098 if (oldpers->sync_request != NULL &&
4099 pers->sync_request == NULL) {
4100 /* need to remove the md_redundancy_group */
4101 if (mddev->to_remove == NULL)
4102 mddev->to_remove = &md_redundancy_group;
4103 }
4104
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02004105 module_put(oldpers->owner);
4106
NeilBrowndafb20f2012-03-19 12:46:39 +11004107 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004108 if (rdev->raid_disk < 0)
4109 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11004110 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01004111 rdev->new_raid_disk = -1;
4112 if (rdev->new_raid_disk == rdev->raid_disk)
4113 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10004114 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01004115 }
NeilBrowndafb20f2012-03-19 12:46:39 +11004116 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004117 if (rdev->raid_disk < 0)
4118 continue;
4119 if (rdev->new_raid_disk == rdev->raid_disk)
4120 continue;
4121 rdev->raid_disk = rdev->new_raid_disk;
4122 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10004123 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01004124 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10004125 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11004126 pr_warn("md: cannot register rd%d for %s after level change\n",
4127 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10004128 }
NeilBrowne93f68a2010-06-15 09:36:03 +01004129 }
4130
NeilBrowndb721d32014-12-15 12:56:58 +11004131 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11004132 /* this is now an array without redundancy, so
4133 * it must always be in_sync
4134 */
4135 mddev->in_sync = 1;
4136 del_timer_sync(&mddev->safemode_timer);
4137 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11004138 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11004139 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004140 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10004141 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11004142 if (!mddev->thread)
4143 md_update_sb(mddev, 1);
Junxiao Bie1a86db2020-07-14 16:10:26 -07004144 sysfs_notify_dirent_safe(mddev->sysfs_level);
Dan Williamsbb7f8d22010-05-01 18:14:57 -07004145 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004146 rv = len;
4147out_unlock:
4148 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08004149 return rv;
4150}
4151
4152static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07004153__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08004154
NeilBrownd4dbd022006-06-26 00:27:59 -07004155static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004156layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07004157{
4158 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07004159 if (mddev->reshape_position != MaxSector &&
4160 mddev->layout != mddev->new_layout)
4161 return sprintf(page, "%d (%d)\n",
4162 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07004163 return sprintf(page, "%d\n", mddev->layout);
4164}
4165
4166static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004167layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07004168{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004169 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11004170 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07004171
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004172 err = kstrtouint(buf, 10, &n);
4173 if (err < 0)
4174 return err;
NeilBrown67918752014-12-15 12:57:01 +11004175 err = mddev_lock(mddev);
4176 if (err)
4177 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07004178
NeilBrownb3546032009-03-31 14:56:41 +11004179 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10004180 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004181 err = -EBUSY;
4182 else if (mddev->ro)
4183 err = -EROFS;
4184 else {
4185 mddev->new_layout = n;
4186 err = mddev->pers->check_reshape(mddev);
4187 if (err)
4188 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10004189 }
NeilBrownb3546032009-03-31 14:56:41 +11004190 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07004191 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11004192 if (mddev->reshape_position == MaxSector)
4193 mddev->layout = n;
4194 }
NeilBrown67918752014-12-15 12:57:01 +11004195 mddev_unlock(mddev);
4196 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07004197}
4198static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07004199__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07004200
NeilBrowneae17012005-11-08 21:39:23 -08004201static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004202raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08004203{
NeilBrownbb636542005-11-08 21:39:45 -08004204 if (mddev->raid_disks == 0)
4205 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004206 if (mddev->reshape_position != MaxSector &&
4207 mddev->delta_disks != 0)
4208 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
4209 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08004210 return sprintf(page, "%d\n", mddev->raid_disks);
4211}
4212
NeilBrownfd01b882011-10-11 16:47:53 +11004213static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08004214
4215static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004216raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08004217{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004218 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11004219 int err;
NeilBrownda943b992006-01-06 00:20:54 -08004220
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004221 err = kstrtouint(buf, 10, &n);
4222 if (err < 0)
4223 return err;
NeilBrownda943b992006-01-06 00:20:54 -08004224
NeilBrown67918752014-12-15 12:57:01 +11004225 err = mddev_lock(mddev);
4226 if (err)
4227 return err;
NeilBrownda943b992006-01-06 00:20:54 -08004228 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11004229 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07004230 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10004231 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004232 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10004233
NeilBrown67918752014-12-15 12:57:01 +11004234 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10004235 rdev_for_each(rdev, mddev) {
4236 if (olddisks < n &&
4237 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11004238 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10004239 if (olddisks > n &&
4240 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11004241 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10004242 }
NeilBrown67918752014-12-15 12:57:01 +11004243 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004244 mddev->delta_disks = n - olddisks;
4245 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10004246 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07004247 } else
NeilBrownda943b992006-01-06 00:20:54 -08004248 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11004249out_unlock:
4250 mddev_unlock(mddev);
4251 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08004252}
4253static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07004254__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08004255
NeilBrown24dd4692005-11-08 21:39:26 -08004256static ssize_t
Sebastian Parschauerec164d072020-07-28 12:01:39 +02004257uuid_show(struct mddev *mddev, char *page)
4258{
4259 return sprintf(page, "%pU\n", mddev->uuid);
4260}
4261static struct md_sysfs_entry md_uuid =
4262__ATTR(uuid, S_IRUGO, uuid_show, NULL);
4263
4264static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004265chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08004266{
NeilBrown08a02ec2007-05-09 02:35:38 -07004267 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10004268 mddev->chunk_sectors != mddev->new_chunk_sectors)
4269 return sprintf(page, "%d (%d)\n",
4270 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10004271 mddev->chunk_sectors << 9);
4272 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08004273}
4274
4275static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004276chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08004277{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004278 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11004279 int err;
NeilBrown3b343802006-01-06 00:20:47 -08004280
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004281 err = kstrtoul(buf, 10, &n);
4282 if (err < 0)
4283 return err;
NeilBrown3b343802006-01-06 00:20:47 -08004284
NeilBrown67918752014-12-15 12:57:01 +11004285 err = mddev_lock(mddev);
4286 if (err)
4287 return err;
NeilBrownb3546032009-03-31 14:56:41 +11004288 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10004289 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004290 err = -EBUSY;
4291 else if (mddev->ro)
4292 err = -EROFS;
4293 else {
4294 mddev->new_chunk_sectors = n >> 9;
4295 err = mddev->pers->check_reshape(mddev);
4296 if (err)
4297 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10004298 }
NeilBrownb3546032009-03-31 14:56:41 +11004299 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10004300 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004301 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10004302 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004303 }
NeilBrown67918752014-12-15 12:57:01 +11004304 mddev_unlock(mddev);
4305 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08004306}
4307static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004308__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08004309
NeilBrowna94213b2006-06-26 00:28:00 -07004310static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004311resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07004312{
NeilBrownd1a7c502009-03-31 15:24:32 +11004313 if (mddev->recovery_cp == MaxSector)
4314 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07004315 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4316}
4317
4318static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004319resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07004320{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004321 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11004322 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004323
4324 if (cmd_match(buf, "none"))
4325 n = MaxSector;
4326 else {
4327 err = kstrtoull(buf, 10, &n);
4328 if (err < 0)
4329 return err;
4330 if (n != (sector_t)n)
4331 return -EINVAL;
4332 }
NeilBrowna94213b2006-06-26 00:28:00 -07004333
NeilBrown67918752014-12-15 12:57:01 +11004334 err = mddev_lock(mddev);
4335 if (err)
4336 return err;
NeilBrownb0986362011-05-11 15:52:21 +10004337 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11004338 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07004339
NeilBrown67918752014-12-15 12:57:01 +11004340 if (!err) {
4341 mddev->recovery_cp = n;
4342 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08004343 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11004344 }
4345 mddev_unlock(mddev);
4346 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07004347}
4348static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10004349__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4350 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07004351
NeilBrown9e653b62006-06-26 00:27:58 -07004352/*
4353 * The array state can be:
4354 *
4355 * clear
4356 * No devices, no size, no level
4357 * Equivalent to STOP_ARRAY ioctl
4358 * inactive
4359 * May have some settings, but array is not active
4360 * all IO results in error
4361 * When written, doesn't tear down array, but just stops it
4362 * suspended (not supported yet)
4363 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01004364 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07004365 * readonly
4366 * no resync can happen. no superblocks get written.
4367 * write requests fail
4368 * read-auto
4369 * like readonly, but behaves like 'clean' on a write request.
4370 *
4371 * clean - no pending writes, but otherwise active.
4372 * When written to inactive array, starts without resync
4373 * If a write request arrives then
4374 * if metadata is known, mark 'dirty' and switch to 'active'.
4375 * if not known, block and switch to write-pending
4376 * If written to an active array that has pending writes, then fails.
4377 * active
4378 * fully active: IO and resync can be happening.
4379 * When written to inactive array, starts with resync
4380 *
4381 * write-pending
4382 * clean, but writes are blocked waiting for 'active' to be written.
4383 *
4384 * active-idle
4385 * like active, but no writes have been seen for a while (100msec).
4386 *
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004387 * broken
4388 * RAID0/LINEAR-only: same as clean, but array is missing a member.
4389 * It's useful because RAID0/LINEAR mounted-arrays aren't stopped
4390 * when a member is gone, so this state will at least alert the
4391 * user that something is wrong.
NeilBrown9e653b62006-06-26 00:27:58 -07004392 */
4393enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004394 write_pending, active_idle, broken, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004395static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004396 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004397 "write-pending", "active-idle", "broken", NULL };
NeilBrown9e653b62006-06-26 00:27:58 -07004398
4399static int match_word(const char *word, char **list)
4400{
4401 int n;
4402 for (n=0; list[n]; n++)
4403 if (cmd_match(word, list[n]))
4404 break;
4405 return n;
4406}
4407
4408static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004409array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004410{
4411 enum array_state st = inactive;
4412
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004413 if (mddev->pers && !test_bit(MD_NOT_READY, &mddev->flags)) {
NeilBrown9e653b62006-06-26 00:27:58 -07004414 switch(mddev->ro) {
4415 case 1:
4416 st = readonly;
4417 break;
4418 case 2:
4419 st = read_auto;
4420 break;
4421 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004422 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004423 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004424 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004425 else if (mddev->in_sync)
4426 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004427 else if (mddev->safemode)
4428 st = active_idle;
4429 else
4430 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004431 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004432 }
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004433
4434 if (test_bit(MD_BROKEN, &mddev->flags) && st == clean)
4435 st = broken;
4436 } else {
NeilBrown9e653b62006-06-26 00:27:58 -07004437 if (list_empty(&mddev->disks) &&
4438 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004439 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004440 st = clear;
4441 else
4442 st = inactive;
4443 }
4444 return sprintf(page, "%s\n", array_states[st]);
4445}
4446
NeilBrownf72ffdd2014-09-30 14:23:59 +10004447static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4448static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4449static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004450static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004451
4452static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004453array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004454{
NeilBrown6497709b2017-03-15 14:05:14 +11004455 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004456 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004457
4458 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4459 /* don't take reconfig_mutex when toggling between
4460 * clean and active
4461 */
4462 spin_lock(&mddev->lock);
4463 if (st == active) {
4464 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004465 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004466 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004467 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004468 } else /* st == clean */ {
4469 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004470 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004471 err = -EBUSY;
4472 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004473 if (!err)
4474 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004475 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004476 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004477 }
4478 err = mddev_lock(mddev);
4479 if (err)
4480 return err;
4481 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004482 switch(st) {
4483 case bad_word:
4484 break;
4485 case clear:
4486 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004487 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004488 break;
4489 case inactive:
4490 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004491 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004492 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004493 else
NeilBrowne6910632008-02-06 01:39:51 -08004494 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004495 break;
4496 case suspended:
4497 break; /* not supported yet */
4498 case readonly:
4499 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004500 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004501 else {
4502 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004503 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004504 err = do_md_run(mddev);
4505 }
4506 break;
4507 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004508 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004509 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004510 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004511 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004512 err = restart_array(mddev);
4513 if (err == 0) {
4514 mddev->ro = 2;
4515 set_disk_ro(mddev->gendisk, 0);
4516 }
NeilBrown9e653b62006-06-26 00:27:58 -07004517 } else {
4518 mddev->ro = 2;
4519 err = do_md_run(mddev);
4520 }
4521 break;
4522 case clean:
4523 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004524 err = restart_array(mddev);
4525 if (err)
4526 break;
NeilBrown85572d72014-12-15 12:56:56 +11004527 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004528 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004529 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004530 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004531 } else
4532 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004533 break;
4534 case active:
4535 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004536 err = restart_array(mddev);
4537 if (err)
4538 break;
Shaohua Li29530792016-12-08 15:48:19 -08004539 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004540 wake_up(&mddev->sb_wait);
4541 err = 0;
4542 } else {
4543 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004544 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004545 err = do_md_run(mddev);
4546 }
4547 break;
4548 case write_pending:
4549 case active_idle:
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004550 case broken:
NeilBrown9e653b62006-06-26 00:27:58 -07004551 /* these cannot be set */
4552 break;
4553 }
NeilBrown67918752014-12-15 12:57:01 +11004554
4555 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004556 if (mddev->hold_active == UNTIL_IOCTL)
4557 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004558 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004559 }
NeilBrown67918752014-12-15 12:57:01 +11004560 mddev_unlock(mddev);
4561 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004562}
NeilBrown80ca3a42006-07-10 04:44:18 -07004563static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004564__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004565
NeilBrown6d7ff7382006-01-06 00:21:16 -08004566static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004567max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004568 return sprintf(page, "%d\n",
4569 atomic_read(&mddev->max_corr_read_errors));
4570}
4571
4572static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004573max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004574{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004575 unsigned int n;
4576 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004577
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004578 rv = kstrtouint(buf, 10, &n);
4579 if (rv < 0)
4580 return rv;
4581 atomic_set(&mddev->max_corr_read_errors, n);
4582 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004583}
4584
4585static struct md_sysfs_entry max_corr_read_errors =
4586__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4587 max_corrected_read_errors_store);
4588
4589static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004590null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004591{
4592 return -EINVAL;
4593}
4594
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004595/* need to ensure rdev_delayed_delete() has completed */
4596static void flush_rdev_wq(struct mddev *mddev)
4597{
4598 struct md_rdev *rdev;
4599
4600 rcu_read_lock();
4601 rdev_for_each_rcu(rdev, mddev)
4602 if (work_pending(&rdev->del_work)) {
4603 flush_workqueue(md_rdev_misc_wq);
4604 break;
4605 }
4606 rcu_read_unlock();
4607}
4608
NeilBrown6d7ff7382006-01-06 00:21:16 -08004609static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004610new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004611{
4612 /* buf must be %d:%d\n? giving major and minor numbers */
4613 /* The new device is added to the array.
4614 * If the array has a persistent superblock, we read the
4615 * superblock to initialise info and check validity.
4616 * Otherwise, only checking done is that in bind_rdev_to_array,
4617 * which mainly checks size.
4618 */
4619 char *e;
4620 int major = simple_strtoul(buf, &e, 10);
4621 int minor;
4622 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004623 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004624 int err;
4625
4626 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4627 return -EINVAL;
4628 minor = simple_strtoul(e+1, &e, 10);
4629 if (*e && *e != '\n')
4630 return -EINVAL;
4631 dev = MKDEV(major, minor);
4632 if (major != MAJOR(dev) ||
4633 minor != MINOR(dev))
4634 return -EOVERFLOW;
4635
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004636 flush_rdev_wq(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004637 err = mddev_lock(mddev);
4638 if (err)
4639 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004640 if (mddev->persistent) {
4641 rdev = md_import_device(dev, mddev->major_version,
4642 mddev->minor_version);
4643 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004644 struct md_rdev *rdev0
4645 = list_entry(mddev->disks.next,
4646 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004647 err = super_types[mddev->major_version]
4648 .load_super(rdev, rdev0, mddev->minor_version);
4649 if (err < 0)
4650 goto out;
4651 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004652 } else if (mddev->external)
4653 rdev = md_import_device(dev, -2, -1);
4654 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004655 rdev = md_import_device(dev, -1, -1);
4656
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004657 if (IS_ERR(rdev)) {
4658 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004659 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004660 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004661 err = bind_rdev_to_array(rdev, mddev);
4662 out:
4663 if (err)
4664 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004665 mddev_unlock(mddev);
Alexey Obitotskiy5492c462017-07-28 15:49:25 +02004666 if (!err)
4667 md_new_event(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004668 return err ? err : len;
4669}
4670
4671static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004672__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004673
4674static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004675bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004676{
4677 char *end;
4678 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004679 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004680
NeilBrown67918752014-12-15 12:57:01 +11004681 err = mddev_lock(mddev);
4682 if (err)
4683 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004684 if (!mddev->bitmap)
4685 goto out;
4686 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4687 while (*buf) {
4688 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4689 if (buf == end) break;
4690 if (*end == '-') { /* range */
4691 buf = end + 1;
4692 end_chunk = simple_strtoul(buf, &end, 0);
4693 if (buf == end) break;
4694 }
4695 if (*end && !isspace(*end)) break;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004696 md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004697 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004698 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004699 md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
Paul Clements9b1d1da2006-10-03 01:15:49 -07004700out:
NeilBrown67918752014-12-15 12:57:01 +11004701 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004702 return len;
4703}
4704
4705static struct md_sysfs_entry md_bitmap =
4706__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4707
4708static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004709size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004710{
Andre Noll58c0fed2009-03-31 14:33:13 +11004711 return sprintf(page, "%llu\n",
4712 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004713}
4714
NeilBrownfd01b882011-10-11 16:47:53 +11004715static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004716
4717static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004718size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004719{
4720 /* If array is inactive, we can reduce the component size, but
4721 * not increase it (except from 0).
4722 * If array is active, we can try an on-line resize
4723 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004724 sector_t sectors;
4725 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004726
Andre Noll58c0fed2009-03-31 14:33:13 +11004727 if (err < 0)
4728 return err;
NeilBrown67918752014-12-15 12:57:01 +11004729 err = mddev_lock(mddev);
4730 if (err)
4731 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004732 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004733 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004734 if (err == 0)
4735 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004736 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004737 if (mddev->dev_sectors == 0 ||
4738 mddev->dev_sectors > sectors)
4739 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004740 else
4741 err = -ENOSPC;
4742 }
NeilBrown67918752014-12-15 12:57:01 +11004743 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004744 return err ? err : len;
4745}
4746
4747static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004748__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004749
Masanari Iida83f0d772012-10-30 00:18:08 +09004750/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004751 * This is one of
4752 * 'none' for arrays with no metadata (good luck...)
4753 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004754 * or N.M for internally known formats
4755 */
4756static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004757metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004758{
4759 if (mddev->persistent)
4760 return sprintf(page, "%d.%d\n",
4761 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004762 else if (mddev->external)
4763 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004764 else
4765 return sprintf(page, "none\n");
4766}
4767
4768static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004769metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004770{
4771 int major, minor;
4772 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004773 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004774 /* Changing the details of 'external' metadata is
4775 * always permitted. Otherwise there must be
4776 * no devices attached to the array.
4777 */
NeilBrown67918752014-12-15 12:57:01 +11004778
4779 err = mddev_lock(mddev);
4780 if (err)
4781 return err;
4782 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004783 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4784 ;
4785 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004786 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004787
NeilBrown67918752014-12-15 12:57:01 +11004788 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004789 if (cmd_match(buf, "none")) {
4790 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004791 mddev->external = 0;
4792 mddev->major_version = 0;
4793 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004794 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004795 }
4796 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004797 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004798 if (namelen >= sizeof(mddev->metadata_type))
4799 namelen = sizeof(mddev->metadata_type)-1;
4800 strncpy(mddev->metadata_type, buf+9, namelen);
4801 mddev->metadata_type[namelen] = 0;
4802 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4803 mddev->metadata_type[--namelen] = 0;
4804 mddev->persistent = 0;
4805 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004806 mddev->major_version = 0;
4807 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004808 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004809 }
4810 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004811 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004812 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004813 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004814 buf = e+1;
4815 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004816 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004817 goto out_unlock;
4818 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004819 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004820 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004821 mddev->major_version = major;
4822 mddev->minor_version = minor;
4823 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004824 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004825 err = 0;
4826out_unlock:
4827 mddev_unlock(mddev);
4828 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004829}
4830
4831static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004832__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004833
NeilBrowna35b0d62006-01-06 00:20:49 -08004834static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004835action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004836{
NeilBrown7eec3142005-11-08 21:39:44 -08004837 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004838 unsigned long recovery = mddev->recovery;
4839 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004840 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004841 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4842 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4843 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004844 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004845 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4846 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004847 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004848 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004849 type = "check";
4850 else
4851 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004852 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004853 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004854 else if (mddev->reshape_position != MaxSector)
4855 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004856 }
4857 return sprintf(page, "%s\n", type);
4858}
4859
4860static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004861action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004862{
NeilBrown7eec3142005-11-08 21:39:44 -08004863 if (!mddev->pers || !mddev->pers->sync_request)
4864 return -EINVAL;
4865
NeilBrownb6a9ce62009-05-26 09:41:17 +10004866
4867 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004868 if (cmd_match(page, "frozen"))
4869 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4870 else
4871 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004872 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4873 mddev_lock(mddev) == 0) {
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02004874 if (work_pending(&mddev->del_work))
4875 flush_workqueue(md_misc_wq);
NeilBrown8e8e2512015-06-12 19:51:27 +10004876 if (mddev->sync_thread) {
4877 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004878 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004879 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004880 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004881 }
NeilBrown312045e2015-12-21 11:01:21 +11004882 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004883 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004884 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004885 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004886 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004887 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004888 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004889 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004890 int err;
4891 if (mddev->pers->start_reshape == NULL)
4892 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004893 err = mddev_lock(mddev);
4894 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004895 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4896 err = -EBUSY;
4897 else {
4898 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4899 err = mddev->pers->start_reshape(mddev);
4900 }
NeilBrown67918752014-12-15 12:57:01 +11004901 mddev_unlock(mddev);
4902 }
NeilBrown16484bf2006-03-27 01:18:13 -08004903 if (err)
4904 return err;
Junxiao Bie1a86db2020-07-14 16:10:26 -07004905 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
NeilBrown16484bf2006-03-27 01:18:13 -08004906 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004907 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004908 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004909 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004910 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004911 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004912 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4913 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004914 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004915 if (mddev->ro == 2) {
4916 /* A write to sync_action is enough to justify
4917 * canceling read-auto mode
4918 */
4919 mddev->ro = 0;
4920 md_wakeup_thread(mddev->sync_thread);
4921 }
NeilBrown03c902e2006-01-06 00:20:46 -08004922 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004923 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004924 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004925 return len;
4926}
4927
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004928static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004929__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004930
4931static ssize_t
4932last_sync_action_show(struct mddev *mddev, char *page)
4933{
4934 return sprintf(page, "%s\n", mddev->last_sync_action);
4935}
4936
4937static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4938
NeilBrown9d888832005-11-08 21:39:26 -08004939static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004940mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004941{
4942 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004943 (unsigned long long)
4944 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004945}
4946
NeilBrown80ca3a42006-07-10 04:44:18 -07004947static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004948
NeilBrown88202a02006-01-06 00:21:36 -08004949static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004950sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004951{
4952 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4953 mddev->sync_speed_min ? "local": "system");
4954}
4955
4956static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004957sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004958{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004959 unsigned int min;
4960 int rv;
4961
NeilBrown88202a02006-01-06 00:21:36 -08004962 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004963 min = 0;
4964 } else {
4965 rv = kstrtouint(buf, 10, &min);
4966 if (rv < 0)
4967 return rv;
4968 if (min == 0)
4969 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004970 }
NeilBrown88202a02006-01-06 00:21:36 -08004971 mddev->sync_speed_min = min;
4972 return len;
4973}
4974
4975static struct md_sysfs_entry md_sync_min =
4976__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4977
4978static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004979sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004980{
4981 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4982 mddev->sync_speed_max ? "local": "system");
4983}
4984
4985static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004986sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004987{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004988 unsigned int max;
4989 int rv;
4990
NeilBrown88202a02006-01-06 00:21:36 -08004991 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004992 max = 0;
4993 } else {
4994 rv = kstrtouint(buf, 10, &max);
4995 if (rv < 0)
4996 return rv;
4997 if (max == 0)
4998 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004999 }
NeilBrown88202a02006-01-06 00:21:36 -08005000 mddev->sync_speed_max = max;
5001 return len;
5002}
5003
5004static struct md_sysfs_entry md_sync_max =
5005__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
5006
Iustin Popd7f3d292007-10-16 23:30:54 -07005007static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005008degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07005009{
5010 return sprintf(page, "%d\n", mddev->degraded);
5011}
5012static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08005013
5014static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005015sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07005016{
5017 return sprintf(page, "%d\n", mddev->parallel_resync);
5018}
5019
5020static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005021sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07005022{
5023 long n;
5024
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005025 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07005026 return -EINVAL;
5027
5028 if (n != 0 && n != 1)
5029 return -EINVAL;
5030
5031 mddev->parallel_resync = n;
5032
5033 if (mddev->sync_thread)
5034 wake_up(&resync_wait);
5035
5036 return len;
5037}
5038
5039/* force parallel resync, even with shared block devices */
5040static struct md_sysfs_entry md_sync_force_parallel =
5041__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
5042 sync_force_parallel_show, sync_force_parallel_store);
5043
5044static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005045sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08005046{
5047 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11005048 if (mddev->curr_resync == 0)
5049 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01005050 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
5051 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08005052 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01005053 db = resync - mddev->resync_mark_cnt;
5054 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08005055}
5056
NeilBrown80ca3a42006-07-10 04:44:18 -07005057static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08005058
5059static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005060sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08005061{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11005062 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08005063
NeilBrownacb180b2009-04-14 16:28:34 +10005064 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5065 return sprintf(page, "none\n");
5066
NeilBrown72f36d52012-10-11 14:25:57 +11005067 if (mddev->curr_resync == 1 ||
5068 mddev->curr_resync == 2)
5069 return sprintf(page, "delayed\n");
5070
NeilBrownc804cde2012-05-21 09:28:33 +10005071 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
5072 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11005073 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08005074 else
Andre Noll58c0fed2009-03-31 14:33:13 +11005075 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08005076
NeilBrownacb180b2009-04-14 16:28:34 +10005077 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11005078 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08005079}
5080
NeilBrown750f1992014-09-30 08:53:05 +10005081static struct md_sysfs_entry md_sync_completed =
5082 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08005083
NeilBrowne464eaf2006-03-27 01:18:14 -08005084static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005085min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10005086{
5087 return sprintf(page, "%llu\n",
5088 (unsigned long long)mddev->resync_min);
5089}
5090static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005091min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10005092{
5093 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11005094 int err;
NeilBrown23da4222014-12-15 12:57:01 +11005095
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005096 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10005097 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11005098
5099 spin_lock(&mddev->lock);
5100 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10005101 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11005102 goto out_unlock;
5103
5104 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10005105 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11005106 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10005107
NeilBrown50c37b12015-03-23 17:36:38 +11005108 /* Round down to multiple of 4K for safety */
5109 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11005110 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10005111
NeilBrown23da4222014-12-15 12:57:01 +11005112out_unlock:
5113 spin_unlock(&mddev->lock);
5114 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10005115}
5116
5117static struct md_sysfs_entry md_min_sync =
5118__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
5119
5120static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005121max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08005122{
5123 if (mddev->resync_max == MaxSector)
5124 return sprintf(page, "max\n");
5125 else
5126 return sprintf(page, "%llu\n",
5127 (unsigned long long)mddev->resync_max);
5128}
5129static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005130max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08005131{
NeilBrown23da4222014-12-15 12:57:01 +11005132 int err;
5133 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08005134 if (strncmp(buf, "max", 3) == 0)
5135 mddev->resync_max = MaxSector;
5136 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10005137 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11005138 int chunk;
5139
5140 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005141 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11005142 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10005143 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11005144 goto out_unlock;
5145
5146 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08005147 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10005148 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08005149 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11005150 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08005151
5152 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11005153 chunk = mddev->chunk_sectors;
5154 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10005155 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11005156
5157 err = -EINVAL;
5158 if (sector_div(temp, chunk))
5159 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08005160 }
5161 mddev->resync_max = max;
5162 }
5163 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11005164 err = 0;
5165out_unlock:
5166 spin_unlock(&mddev->lock);
5167 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08005168}
5169
5170static struct md_sysfs_entry md_max_sync =
5171__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
5172
5173static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005174suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08005175{
5176 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
5177}
5178
5179static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005180suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08005181{
NeilBrownb03e0cc2017-10-19 12:49:15 +11005182 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005183 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08005184
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005185 err = kstrtoull(buf, 10, &new);
5186 if (err < 0)
5187 return err;
5188 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08005189 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11005190
NeilBrown67918752014-12-15 12:57:01 +11005191 err = mddev_lock(mddev);
5192 if (err)
5193 return err;
5194 err = -EINVAL;
5195 if (mddev->pers == NULL ||
5196 mddev->pers->quiesce == NULL)
5197 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005198 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11005199 mddev->suspend_lo = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005200 mddev_resume(mddev);
5201
NeilBrown67918752014-12-15 12:57:01 +11005202 err = 0;
5203unlock:
5204 mddev_unlock(mddev);
5205 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08005206}
5207static struct md_sysfs_entry md_suspend_lo =
5208__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
5209
NeilBrowne464eaf2006-03-27 01:18:14 -08005210static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005211suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08005212{
5213 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
5214}
5215
5216static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005217suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08005218{
NeilBrownb03e0cc2017-10-19 12:49:15 +11005219 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005220 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08005221
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005222 err = kstrtoull(buf, 10, &new);
5223 if (err < 0)
5224 return err;
5225 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08005226 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11005227
NeilBrown67918752014-12-15 12:57:01 +11005228 err = mddev_lock(mddev);
5229 if (err)
5230 return err;
5231 err = -EINVAL;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005232 if (mddev->pers == NULL)
NeilBrown67918752014-12-15 12:57:01 +11005233 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005234
5235 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11005236 mddev->suspend_hi = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005237 mddev_resume(mddev);
5238
NeilBrown67918752014-12-15 12:57:01 +11005239 err = 0;
5240unlock:
5241 mddev_unlock(mddev);
5242 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08005243}
5244static struct md_sysfs_entry md_suspend_hi =
5245__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
5246
NeilBrown08a02ec2007-05-09 02:35:38 -07005247static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005248reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07005249{
5250 if (mddev->reshape_position != MaxSector)
5251 return sprintf(page, "%llu\n",
5252 (unsigned long long)mddev->reshape_position);
5253 strcpy(page, "none\n");
5254 return 5;
5255}
5256
5257static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005258reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07005259{
NeilBrownc6563a82012-05-21 09:27:00 +10005260 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005261 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005262 int err;
NeilBrown67918752014-12-15 12:57:01 +11005263
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005264 err = kstrtoull(buf, 10, &new);
5265 if (err < 0)
5266 return err;
5267 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07005268 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11005269 err = mddev_lock(mddev);
5270 if (err)
5271 return err;
5272 err = -EBUSY;
5273 if (mddev->pers)
5274 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07005275 mddev->reshape_position = new;
5276 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005277 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07005278 mddev->new_level = mddev->level;
5279 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10005280 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10005281 rdev_for_each(rdev, mddev)
5282 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11005283 err = 0;
5284unlock:
5285 mddev_unlock(mddev);
5286 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07005287}
5288
5289static struct md_sysfs_entry md_reshape_position =
5290__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
5291 reshape_position_store);
5292
Dan Williamsb522adc2009-03-31 15:00:31 +11005293static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10005294reshape_direction_show(struct mddev *mddev, char *page)
5295{
5296 return sprintf(page, "%s\n",
5297 mddev->reshape_backwards ? "backwards" : "forwards");
5298}
5299
5300static ssize_t
5301reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
5302{
5303 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11005304 int err;
5305
NeilBrown2c810cd2012-05-21 09:27:00 +10005306 if (cmd_match(buf, "forwards"))
5307 backwards = 0;
5308 else if (cmd_match(buf, "backwards"))
5309 backwards = 1;
5310 else
5311 return -EINVAL;
5312 if (mddev->reshape_backwards == backwards)
5313 return len;
5314
NeilBrown67918752014-12-15 12:57:01 +11005315 err = mddev_lock(mddev);
5316 if (err)
5317 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10005318 /* check if we are allowed to change */
5319 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11005320 err = -EBUSY;
5321 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10005322 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11005323 err = -EINVAL;
5324 else
5325 mddev->reshape_backwards = backwards;
5326 mddev_unlock(mddev);
5327 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10005328}
5329
5330static struct md_sysfs_entry md_reshape_direction =
5331__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5332 reshape_direction_store);
5333
5334static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005335array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11005336{
5337 if (mddev->external_size)
5338 return sprintf(page, "%llu\n",
5339 (unsigned long long)mddev->array_sectors/2);
5340 else
5341 return sprintf(page, "default\n");
5342}
5343
5344static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005345array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11005346{
5347 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11005348 int err;
5349
5350 err = mddev_lock(mddev);
5351 if (err)
5352 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11005353
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005354 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08005355 if (mddev_is_clustered(mddev)) {
5356 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005357 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08005358 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005359
Dan Williamsb522adc2009-03-31 15:00:31 +11005360 if (strncmp(buf, "default", 7) == 0) {
5361 if (mddev->pers)
5362 sectors = mddev->pers->size(mddev, 0, 0);
5363 else
5364 sectors = mddev->array_sectors;
5365
5366 mddev->external_size = 0;
5367 } else {
5368 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11005369 err = -EINVAL;
5370 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5371 err = -E2BIG;
5372 else
5373 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11005374 }
5375
NeilBrown67918752014-12-15 12:57:01 +11005376 if (!err) {
5377 mddev->array_sectors = sectors;
5378 if (mddev->pers) {
5379 set_capacity(mddev->gendisk, mddev->array_sectors);
5380 revalidate_disk(mddev->gendisk);
5381 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11005382 }
NeilBrown67918752014-12-15 12:57:01 +11005383 mddev_unlock(mddev);
5384 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11005385}
5386
5387static struct md_sysfs_entry md_array_size =
5388__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5389 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005390
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005391static ssize_t
5392consistency_policy_show(struct mddev *mddev, char *page)
5393{
5394 int ret;
5395
5396 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5397 ret = sprintf(page, "journal\n");
5398 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5399 ret = sprintf(page, "ppl\n");
5400 } else if (mddev->bitmap) {
5401 ret = sprintf(page, "bitmap\n");
5402 } else if (mddev->pers) {
5403 if (mddev->pers->sync_request)
5404 ret = sprintf(page, "resync\n");
5405 else
5406 ret = sprintf(page, "none\n");
5407 } else {
5408 ret = sprintf(page, "unknown\n");
5409 }
5410
5411 return ret;
5412}
5413
5414static ssize_t
5415consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5416{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005417 int err = 0;
5418
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005419 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005420 if (mddev->pers->change_consistency_policy)
5421 err = mddev->pers->change_consistency_policy(mddev, buf);
5422 else
5423 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005424 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5425 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005426 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005427 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005428 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005429
5430 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005431}
5432
5433static struct md_sysfs_entry md_consistency_policy =
5434__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5435 consistency_policy_store);
5436
Guoqing Jiang9a567842019-07-24 11:09:19 +02005437static ssize_t fail_last_dev_show(struct mddev *mddev, char *page)
5438{
5439 return sprintf(page, "%d\n", mddev->fail_last_dev);
5440}
5441
5442/*
5443 * Setting fail_last_dev to true to allow last device to be forcibly removed
5444 * from RAID1/RAID10.
5445 */
5446static ssize_t
5447fail_last_dev_store(struct mddev *mddev, const char *buf, size_t len)
5448{
5449 int ret;
5450 bool value;
5451
5452 ret = kstrtobool(buf, &value);
5453 if (ret)
5454 return ret;
5455
5456 if (value != mddev->fail_last_dev)
5457 mddev->fail_last_dev = value;
5458
5459 return len;
5460}
5461static struct md_sysfs_entry md_fail_last_dev =
5462__ATTR(fail_last_dev, S_IRUGO | S_IWUSR, fail_last_dev_show,
5463 fail_last_dev_store);
5464
Guoqing Jiang3938f5f2019-12-23 10:48:56 +01005465static ssize_t serialize_policy_show(struct mddev *mddev, char *page)
5466{
5467 if (mddev->pers == NULL || (mddev->pers->level != 1))
5468 return sprintf(page, "n/a\n");
5469 else
5470 return sprintf(page, "%d\n", mddev->serialize_policy);
5471}
5472
5473/*
5474 * Setting serialize_policy to true to enforce write IO is not reordered
5475 * for raid1.
5476 */
5477static ssize_t
5478serialize_policy_store(struct mddev *mddev, const char *buf, size_t len)
5479{
5480 int err;
5481 bool value;
5482
5483 err = kstrtobool(buf, &value);
5484 if (err)
5485 return err;
5486
5487 if (value == mddev->serialize_policy)
5488 return len;
5489
5490 err = mddev_lock(mddev);
5491 if (err)
5492 return err;
5493 if (mddev->pers == NULL || (mddev->pers->level != 1)) {
5494 pr_err("md: serialize_policy is only effective for raid1\n");
5495 err = -EINVAL;
5496 goto unlock;
5497 }
5498
5499 mddev_suspend(mddev);
5500 if (value)
5501 mddev_create_serial_pool(mddev, NULL, true);
5502 else
5503 mddev_destroy_serial_pool(mddev, NULL, true);
5504 mddev->serialize_policy = value;
5505 mddev_resume(mddev);
5506unlock:
5507 mddev_unlock(mddev);
5508 return err ?: len;
5509}
5510
5511static struct md_sysfs_entry md_serialize_policy =
5512__ATTR(serialize_policy, S_IRUGO | S_IWUSR, serialize_policy_show,
5513 serialize_policy_store);
5514
5515
NeilBrowneae17012005-11-08 21:39:23 -08005516static struct attribute *md_default_attrs[] = {
5517 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005518 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005519 &md_raid_disks.attr,
Sebastian Parschauerec164d072020-07-28 12:01:39 +02005520 &md_uuid.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005521 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005522 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005523 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005524 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005525 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005526 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005527 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005528 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005529 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005530 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005531 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005532 &md_consistency_policy.attr,
Guoqing Jiang9a567842019-07-24 11:09:19 +02005533 &md_fail_last_dev.attr,
Guoqing Jiang3938f5f2019-12-23 10:48:56 +01005534 &md_serialize_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005535 NULL,
5536};
5537
5538static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005539 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005540 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005541 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005542 &md_sync_min.attr,
5543 &md_sync_max.attr,
5544 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005545 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005546 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005547 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005548 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005549 &md_suspend_lo.attr,
5550 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005551 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005552 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005553 NULL,
5554};
NeilBrown411036f2005-11-08 21:39:40 -08005555static struct attribute_group md_redundancy_group = {
5556 .name = NULL,
5557 .attrs = md_redundancy_attrs,
5558};
5559
NeilBrowneae17012005-11-08 21:39:23 -08005560static ssize_t
5561md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5562{
5563 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005564 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005565 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005566
5567 if (!entry->show)
5568 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005569 spin_lock(&all_mddevs_lock);
5570 if (list_empty(&mddev->all_mddevs)) {
5571 spin_unlock(&all_mddevs_lock);
5572 return -EBUSY;
5573 }
5574 mddev_get(mddev);
5575 spin_unlock(&all_mddevs_lock);
5576
NeilBrownb7b17c92014-12-15 12:56:59 +11005577 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005578 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005579 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005580}
5581
5582static ssize_t
5583md_attr_store(struct kobject *kobj, struct attribute *attr,
5584 const char *page, size_t length)
5585{
5586 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005587 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005588 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005589
5590 if (!entry->store)
5591 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005592 if (!capable(CAP_SYS_ADMIN))
5593 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005594 spin_lock(&all_mddevs_lock);
5595 if (list_empty(&mddev->all_mddevs)) {
5596 spin_unlock(&all_mddevs_lock);
5597 return -EBUSY;
5598 }
5599 mddev_get(mddev);
5600 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005601 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005602 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005603 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005604}
5605
5606static void md_free(struct kobject *ko)
5607{
NeilBrownfd01b882011-10-11 16:47:53 +11005608 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005609
5610 if (mddev->sysfs_state)
5611 sysfs_put(mddev->sysfs_state);
Junxiao Bie1a86db2020-07-14 16:10:26 -07005612 if (mddev->sysfs_completed)
5613 sysfs_put(mddev->sysfs_completed);
5614 if (mddev->sysfs_degraded)
5615 sysfs_put(mddev->sysfs_degraded);
5616 if (mddev->sysfs_level)
5617 sysfs_put(mddev->sysfs_level);
5618
NeilBrowna21d1502009-01-09 08:31:09 +11005619
Bart Van Assched8115c352018-02-28 10:15:29 -08005620 if (mddev->gendisk)
5621 del_gendisk(mddev->gendisk);
NeilBrown6cd18e72015-04-27 14:12:22 +10005622 if (mddev->queue)
5623 blk_cleanup_queue(mddev->queue);
Bart Van Assched8115c352018-02-28 10:15:29 -08005624 if (mddev->gendisk)
NeilBrowna21d1502009-01-09 08:31:09 +11005625 put_disk(mddev->gendisk);
NeilBrown4ad23a972017-03-15 14:05:14 +11005626 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005627
Kent Overstreet28dec872018-06-07 20:52:54 -04005628 bioset_exit(&mddev->bio_set);
5629 bioset_exit(&mddev->sync_set);
Artur Paszkiewicz41d2d842020-07-03 11:13:09 +02005630 mempool_exit(&mddev->md_io_pool);
NeilBrowneae17012005-11-08 21:39:23 -08005631 kfree(mddev);
5632}
5633
Emese Revfy52cf25d2010-01-19 02:58:23 +01005634static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005635 .show = md_attr_show,
5636 .store = md_attr_store,
5637};
5638static struct kobj_type md_ktype = {
5639 .release = md_free,
5640 .sysfs_ops = &md_sysfs_ops,
5641 .default_attrs = md_default_attrs,
5642};
5643
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644int mdp_major = 0;
5645
Dan Williams5fd3a172009-03-04 00:57:25 -07005646static void mddev_delayed_delete(struct work_struct *ws)
5647{
NeilBrownfd01b882011-10-11 16:47:53 +11005648 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005649
NeilBrown43a70502009-12-14 12:49:55 +11005650 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005651 kobject_del(&mddev->kobj);
5652 kobject_put(&mddev->kobj);
5653}
5654
NeilBrown4ad23a972017-03-15 14:05:14 +11005655static void no_op(struct percpu_ref *r) {}
5656
NeilBrowna415c0f2017-06-05 16:05:13 +10005657int mddev_init_writes_pending(struct mddev *mddev)
5658{
5659 if (mddev->writes_pending.percpu_count_ptr)
5660 return 0;
Roman Gushchinddde2af2019-05-07 10:01:49 -07005661 if (percpu_ref_init(&mddev->writes_pending, no_op,
5662 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL) < 0)
NeilBrowna415c0f2017-06-05 16:05:13 +10005663 return -ENOMEM;
5664 /* We want to start with the refcount at zero */
5665 percpu_ref_put(&mddev->writes_pending);
5666 return 0;
5667}
5668EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5669
NeilBrownefeb53c2009-01-09 08:31:10 +11005670static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671{
NeilBrown039b7222017-04-12 16:26:13 +10005672 /*
5673 * If dev is zero, name is the name of a device to allocate with
5674 * an arbitrary minor number. It will be "md_???"
5675 * If dev is non-zero it must be a device number with a MAJOR of
5676 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5677 * the device is being created by opening a node in /dev.
5678 * If "name" is not NULL, the device is being created by
5679 * writing to /sys/module/md_mod/parameters/new_array.
5680 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005681 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005682 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005684 int partitioned;
5685 int shift;
5686 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005687 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
5689 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005690 return -ENODEV;
5691
5692 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5693 shift = partitioned ? MdpMinorShift : 0;
5694 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
Tejun Heoe804ac72010-10-15 15:36:08 +02005696 /* wait for any previous instance of this device to be
5697 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005698 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005699 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005700
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005701 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005702 error = -EEXIST;
5703 if (mddev->gendisk)
5704 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005705
NeilBrown039b7222017-04-12 16:26:13 +10005706 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005707 /* Need to ensure that 'name' is not a duplicate.
5708 */
NeilBrownfd01b882011-10-11 16:47:53 +11005709 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005710 spin_lock(&all_mddevs_lock);
5711
5712 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5713 if (mddev2->gendisk &&
5714 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5715 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005716 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005717 }
5718 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 }
NeilBrown039b7222017-04-12 16:26:13 +10005720 if (name && dev)
5721 /*
5722 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5723 */
5724 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005725
Artur Paszkiewicz41d2d842020-07-03 11:13:09 +02005726 error = mempool_init_kmalloc_pool(&mddev->md_io_pool, BIO_POOL_SIZE,
5727 sizeof(struct md_io));
5728 if (error)
5729 goto abort;
5730
NeilBrown0909dc42009-07-01 12:27:21 +10005731 error = -ENOMEM;
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02005732 mddev->queue = blk_alloc_queue(NUMA_NO_NODE);
NeilBrown0909dc42009-07-01 12:27:21 +10005733 if (!mddev->queue)
5734 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005735
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005736 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005737
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 disk = alloc_disk(1 << shift);
5739 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005740 blk_cleanup_queue(mddev->queue);
5741 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005742 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005744 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005746 if (name)
5747 strcpy(disk->disk_name, name);
5748 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005750 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 disk->fops = &md_fops;
5753 disk->private_data = mddev;
5754 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005755 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005756 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005757 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005758 * remove it now.
5759 */
5760 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005762 /* As soon as we call add_disk(), another thread could get
5763 * through to md_open, so make sure it doesn't get too far
5764 */
5765 mutex_lock(&mddev->open_mutex);
5766 add_disk(disk);
5767
Kent Overstreet28dec872018-06-07 20:52:54 -04005768 error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005769 if (error) {
5770 /* This isn't possible, but as kobject_init_and_add is marked
5771 * __must_check, we must do something with the result
5772 */
NeilBrown9d487392016-11-02 14:16:49 +11005773 pr_debug("md: cannot register %s/md - name in use\n",
5774 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005775 error = 0;
5776 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005777 if (mddev->kobj.sd &&
5778 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005779 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005780 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005781 abort:
5782 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005783 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005784 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005785 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
Junxiao Bie1a86db2020-07-14 16:10:26 -07005786 mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed");
5787 mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded");
5788 mddev->sysfs_level = sysfs_get_dirent_safe(mddev->kobj.sd, "level");
NeilBrownb62b7592008-10-21 13:25:21 +11005789 }
NeilBrownd3374822009-01-09 08:31:10 +11005790 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005791 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005792}
5793
5794static struct kobject *md_probe(dev_t dev, int *part, void *data)
5795{
NeilBrown78b63502017-04-12 16:26:13 +10005796 if (create_on_open)
5797 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 return NULL;
5799}
5800
Kees Cooke4dca7b2017-10-17 19:04:42 -07005801static int add_named_array(const char *val, const struct kernel_param *kp)
NeilBrownefeb53c2009-01-09 08:31:10 +11005802{
NeilBrown039b7222017-04-12 16:26:13 +10005803 /*
5804 * val must be "md_*" or "mdNNN".
5805 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005806 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005807 * For "mdNNN" we allocate an array with the minor number NNN
5808 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005809 */
5810 int len = strlen(val);
5811 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005812 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005813
5814 while (len && val[len-1] == '\n')
5815 len--;
5816 if (len >= DISK_NAME_LEN)
5817 return -E2BIG;
5818 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005819 if (strncmp(buf, "md_", 3) == 0)
5820 return md_alloc(0, buf);
5821 if (strncmp(buf, "md", 2) == 0 &&
5822 isdigit(buf[2]) &&
5823 kstrtoul(buf+2, 10, &devnum) == 0 &&
5824 devnum <= MINORMASK)
5825 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5826
5827 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005828}
5829
Kees Cook8376d3c2017-10-16 17:01:48 -07005830static void md_safemode_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831{
Kees Cook8376d3c2017-10-16 17:01:48 -07005832 struct mddev *mddev = from_timer(mddev, t, safemode_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
NeilBrown4ad23a972017-03-15 14:05:14 +11005834 mddev->safemode = 1;
5835 if (mddev->external)
5836 sysfs_notify_dirent_safe(mddev->sysfs_state);
5837
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 md_wakeup_thread(mddev->thread);
5839}
5840
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005841static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842
NeilBrownfd01b882011-10-11 16:47:53 +11005843int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844{
NeilBrown2604b702006-01-06 00:20:36 -08005845 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005846 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005847 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848
NeilBrowna757e642005-04-16 15:26:42 -07005849 if (list_empty(&mddev->disks))
5850 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852
5853 if (mddev->pers)
5854 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005855 /* Cannot run until previous stop completes properly */
5856 if (mddev->sysfs_active)
5857 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005858
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 /*
5860 * Analyze all RAID superblock(s)
5861 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005862 if (!mddev->raid_disks) {
5863 if (!mddev->persistent)
5864 return -EINVAL;
Yufen Yu6a5cb532019-10-16 16:00:03 +08005865 err = analyze_sbs(mddev);
5866 if (err)
5867 return -EINVAL;
NeilBrown1ec4a932008-02-06 01:39:53 -08005868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869
NeilBrownd9d166c2006-01-06 00:20:51 -08005870 if (mddev->level != LEVEL_NONE)
5871 request_module("md-level-%d", mddev->level);
5872 else if (mddev->clevel[0])
5873 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874
5875 /*
5876 * Drop all container device buffers, from now on
5877 * the only valid external interface is through the md
5878 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 */
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005880 mddev->has_superblocks = false;
NeilBrowndafb20f2012-03-19 12:46:39 +11005881 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005882 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 continue;
5884 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005885 invalidate_bdev(rdev->bdev);
NeilBrown97b20ef2017-04-13 08:53:48 +10005886 if (mddev->ro != 1 &&
5887 (bdev_read_only(rdev->bdev) ||
5888 bdev_read_only(rdev->meta_bdev))) {
5889 mddev->ro = 1;
5890 if (mddev->gendisk)
5891 set_disk_ro(mddev->gendisk, 1);
5892 }
NeilBrownf0d76d72007-07-17 04:06:12 -07005893
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005894 if (rdev->sb_page)
5895 mddev->has_superblocks = true;
5896
NeilBrownf0d76d72007-07-17 04:06:12 -07005897 /* perform some consistency tests on the device.
5898 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005899 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005900 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005901 if (rdev->meta_bdev) {
5902 /* Nothing to check */;
5903 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005904 if (mddev->dev_sectors &&
5905 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005906 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005907 pr_warn("md: %s: data overlaps metadata\n",
5908 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005909 return -EINVAL;
5910 }
5911 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005912 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005913 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005914 pr_warn("md: %s: metadata overlaps data\n",
5915 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005916 return -EINVAL;
5917 }
5918 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005919 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 }
5921
Kent Overstreetafeee512018-05-20 18:25:52 -04005922 if (!bioset_initialized(&mddev->bio_set)) {
5923 err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5924 if (err)
5925 return err;
Ming Lei10273172017-02-14 23:29:00 +08005926 }
Kent Overstreetafeee512018-05-20 18:25:52 -04005927 if (!bioset_initialized(&mddev->sync_set)) {
5928 err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5929 if (err)
Kent Overstreet28dec872018-06-07 20:52:54 -04005930 return err;
NeilBrown5a850712017-06-21 09:12:21 +10005931 }
NeilBrowna167f662010-10-26 18:31:13 +11005932
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005934 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005935 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005937 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005938 pr_warn("md: personality for level %d is not loaded!\n",
5939 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005940 else
NeilBrown9d487392016-11-02 14:16:49 +11005941 pr_warn("md: personality for level %s is not loaded!\n",
5942 mddev->clevel);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005943 err = -EINVAL;
5944 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005947 if (mddev->level != pers->level) {
5948 mddev->level = pers->level;
5949 mddev->new_level = pers->level;
5950 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005951 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
NeilBrownf6705572006-03-27 01:18:11 -08005953 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005954 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005955 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005956 module_put(pers->owner);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005957 err = -EINVAL;
5958 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08005959 }
5960
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005961 if (pers->sync_request) {
5962 /* Warn if this is a potentially silly
5963 * configuration.
5964 */
5965 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005966 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005967 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005968
NeilBrowndafb20f2012-03-19 12:46:39 +11005969 rdev_for_each(rdev, mddev)
5970 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005971 if (rdev < rdev2 &&
5972 rdev->bdev->bd_contains ==
5973 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005974 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5975 mdname(mddev),
5976 bdevname(rdev->bdev,b),
5977 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005978 warned = 1;
5979 }
5980 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005981
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005982 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005983 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005984 }
5985
NeilBrown657390d2005-08-26 18:34:16 -07005986 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005987 /* may be over-ridden by personality */
5988 mddev->resync_max_sectors = mddev->dev_sectors;
5989
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005990 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991
NeilBrown0f9552b52009-12-30 12:08:50 +11005992 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005993 mddev->ro = 2; /* read-only, but switch on first write */
5994
NeilBrown36d091f2014-12-15 12:56:58 +11005995 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005996 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005997 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005998 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005999 WARN_ONCE(!mddev->external_size,
6000 "%s: default size too small, but 'external_size' not in effect?\n",
6001 __func__);
6002 pr_warn("md: invalid array_size %llu > default size %llu\n",
6003 (unsigned long long)mddev->array_sectors / 2,
6004 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11006005 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11006006 }
NeilBrown36d091f2014-12-15 12:56:58 +11006007 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10006008 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006009 struct bitmap *bitmap;
6010
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006011 bitmap = md_bitmap_create(mddev, -1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006012 if (IS_ERR(bitmap)) {
6013 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11006014 pr_warn("%s: failed to create bitmap (%d)\n",
6015 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006016 } else
6017 mddev->bitmap = bitmap;
6018
NeilBrownb15c2e52006-01-06 00:20:16 -08006019 }
Guoqing Jiangd4945492019-06-14 17:10:39 +08006020 if (err)
6021 goto bitmap_abort;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08006022
6023 if (mddev->bitmap_info.max_write_behind > 0) {
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01006024 bool create_pool = false;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08006025
6026 rdev_for_each(rdev, mddev) {
6027 if (test_bit(WriteMostly, &rdev->flags) &&
Guoqing Jiang404659c2019-12-23 10:48:53 +01006028 rdev_init_serial(rdev))
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01006029 create_pool = true;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08006030 }
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01006031 if (create_pool && mddev->serial_info_pool == NULL) {
Guoqing Jiang404659c2019-12-23 10:48:53 +01006032 mddev->serial_info_pool =
6033 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
6034 sizeof(struct serial_info));
6035 if (!mddev->serial_info_pool) {
Guoqing Jiang3e148a32019-06-19 17:30:46 +08006036 err = -ENOMEM;
Guoqing Jiangd4945492019-06-14 17:10:39 +08006037 goto bitmap_abort;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08006038 }
6039 }
6040 }
6041
NeilBrown5c675f82014-12-15 12:56:56 +11006042 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07006043 bool nonrot = true;
6044
6045 rdev_for_each(rdev, mddev) {
6046 if (rdev->raid_disk >= 0 &&
6047 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
6048 nonrot = false;
6049 break;
6050 }
6051 }
6052 if (mddev->degraded)
6053 nonrot = false;
6054 if (nonrot)
Bart Van Assche8b904b52018-03-07 17:10:10 -08006055 blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue);
Shaohua Libb086a82016-09-30 09:45:40 -07006056 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08006057 blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01006058 mddev->queue->backing_dev_info->congested_data = mddev;
6059 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11006060 }
NeilBrown36d091f2014-12-15 12:56:58 +11006061 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10006062 if (mddev->kobj.sd &&
6063 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11006064 pr_warn("md: cannot register extra attributes for %s\n",
6065 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10006066 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08006067 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08006068 mddev->ro = 0;
6069
Robert Becker1e509152009-12-14 12:49:58 +11006070 atomic_set(&mddev->max_corr_read_errors,
6071 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006073 if (mddev_is_clustered(mddev))
6074 mddev->safemode_delay = 0;
6075 else
Zhao Heming7c9d5c52020-07-21 02:08:52 +08006076 mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11006078 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11006079 spin_lock(&mddev->lock);
6080 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11006081 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11006082 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10006083 if (rdev->raid_disk >= 0)
Yufen Yue5b521e2019-06-14 15:41:07 -07006084 sysfs_link_rdev(mddev, rdev); /* failure here is OK */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006085
NeilBrowna4a3d262015-07-17 11:57:30 +10006086 if (mddev->degraded && !mddev->ro)
6087 /* This ensures that recovering status is reported immediately
6088 * via sysfs - until a lack of spares is confirmed.
6089 */
6090 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10006092
Shaohua Li29530792016-12-08 15:48:19 -08006093 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07006094 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095
NeilBrownd7603b72006-01-06 00:20:30 -08006096 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 return 0;
Xiao Nib1261942018-01-24 12:17:38 +08006098
Guoqing Jiangd4945492019-06-14 17:10:39 +08006099bitmap_abort:
6100 mddev_detach(mddev);
6101 if (mddev->private)
6102 pers->free(mddev, mddev->private);
6103 mddev->private = NULL;
6104 module_put(pers->owner);
6105 md_bitmap_destroy(mddev);
Xiao Nib1261942018-01-24 12:17:38 +08006106abort:
NeilBrown4bc034d2019-03-29 10:46:16 -07006107 bioset_exit(&mddev->bio_set);
6108 bioset_exit(&mddev->sync_set);
Xiao Nib1261942018-01-24 12:17:38 +08006109 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110}
NeilBrown390ee602010-06-01 19:37:27 +10006111EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112
NeilBrownfd01b882011-10-11 16:47:53 +11006113static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11006114{
6115 int err;
6116
NeilBrown9d4b45d2019-08-20 10:21:09 +10006117 set_bit(MD_NOT_READY, &mddev->flags);
NeilBrownfe60b012010-03-29 11:10:42 +11006118 err = md_run(mddev);
6119 if (err)
6120 goto out;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006121 err = md_bitmap_load(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006122 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006123 md_bitmap_destroy(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006124 goto out;
6125 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05006126
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006127 if (mddev_is_clustered(mddev))
6128 md_allow_write(mddev);
6129
Song Liud5d885f2017-11-19 22:17:01 -08006130 /* run start up tasks that require md_thread */
6131 md_start(mddev);
6132
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05006133 md_wakeup_thread(mddev->thread);
6134 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
6135
NeilBrownfe60b012010-03-29 11:10:42 +11006136 set_capacity(mddev->gendisk, mddev->array_sectors);
6137 revalidate_disk(mddev->gendisk);
NeilBrown9d4b45d2019-08-20 10:21:09 +10006138 clear_bit(MD_NOT_READY, &mddev->flags);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006139 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11006140 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrown9d4b45d2019-08-20 10:21:09 +10006141 sysfs_notify_dirent_safe(mddev->sysfs_state);
6142 sysfs_notify_dirent_safe(mddev->sysfs_action);
Junxiao Bie1a86db2020-07-14 16:10:26 -07006143 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
NeilBrownfe60b012010-03-29 11:10:42 +11006144out:
NeilBrown9d4b45d2019-08-20 10:21:09 +10006145 clear_bit(MD_NOT_READY, &mddev->flags);
NeilBrownfe60b012010-03-29 11:10:42 +11006146 return err;
6147}
6148
Song Liud5d885f2017-11-19 22:17:01 -08006149int md_start(struct mddev *mddev)
6150{
6151 int ret = 0;
6152
6153 if (mddev->pers->start) {
6154 set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6155 md_wakeup_thread(mddev->thread);
6156 ret = mddev->pers->start(mddev);
6157 clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6158 md_wakeup_thread(mddev->sync_thread);
6159 }
6160 return ret;
6161}
6162EXPORT_SYMBOL_GPL(md_start);
6163
NeilBrownfd01b882011-10-11 16:47:53 +11006164static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165{
6166 struct gendisk *disk = mddev->gendisk;
NeilBrown97b20ef2017-04-13 08:53:48 +10006167 struct md_rdev *rdev;
6168 bool has_journal = false;
6169 bool has_readonly = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170
Andre Noll80fab1d2008-07-11 22:02:21 +10006171 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10006173 return -ENXIO;
6174 if (!mddev->pers)
6175 return -EINVAL;
6176 if (!mddev->ro)
6177 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07006178
NeilBrown97b20ef2017-04-13 08:53:48 +10006179 rcu_read_lock();
6180 rdev_for_each_rcu(rdev, mddev) {
6181 if (test_bit(Journal, &rdev->flags) &&
6182 !test_bit(Faulty, &rdev->flags))
6183 has_journal = true;
6184 if (bdev_read_only(rdev->bdev))
6185 has_readonly = true;
Song Liu339421d2015-10-08 21:54:13 -07006186 }
NeilBrown97b20ef2017-04-13 08:53:48 +10006187 rcu_read_unlock();
6188 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
6189 /* Don't restart rw with journal missing/faulty */
6190 return -EINVAL;
6191 if (has_readonly)
6192 return -EROFS;
Song Liu339421d2015-10-08 21:54:13 -07006193
Andre Noll80fab1d2008-07-11 22:02:21 +10006194 mddev->safemode = 0;
6195 mddev->ro = 0;
6196 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11006197 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10006198 /* Kick recovery or resync if necessary */
6199 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6200 md_wakeup_thread(mddev->thread);
6201 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006202 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10006203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204}
6205
NeilBrownfd01b882011-10-11 16:47:53 +11006206static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11006207{
6208 mddev->array_sectors = 0;
6209 mddev->external_size = 0;
6210 mddev->dev_sectors = 0;
6211 mddev->raid_disks = 0;
6212 mddev->recovery_cp = 0;
6213 mddev->resync_min = 0;
6214 mddev->resync_max = MaxSector;
6215 mddev->reshape_position = MaxSector;
6216 mddev->external = 0;
6217 mddev->persistent = 0;
6218 mddev->level = LEVEL_NONE;
6219 mddev->clevel[0] = 0;
6220 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006221 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006222 mddev->ro = 0;
6223 mddev->metadata_type[0] = 0;
6224 mddev->chunk_sectors = 0;
6225 mddev->ctime = mddev->utime = 0;
6226 mddev->layout = 0;
6227 mddev->max_disks = 0;
6228 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10006229 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006230 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006231 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006232 mddev->new_level = LEVEL_NONE;
6233 mddev->new_layout = 0;
6234 mddev->new_chunk_sectors = 0;
6235 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11006236 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11006237 mddev->suspend_lo = mddev->suspend_hi = 0;
6238 mddev->sync_speed_min = mddev->sync_speed_max = 0;
6239 mddev->recovery = 0;
6240 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006241 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006242 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006243 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10006244 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08006245 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11006246 mddev->bitmap_info.offset = 0;
6247 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10006248 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006249 mddev->bitmap_info.chunksize = 0;
6250 mddev->bitmap_info.daemon_sleep = 0;
6251 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08006252 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006253}
6254
NeilBrownfd01b882011-10-11 16:47:53 +11006255static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11006256{
NeilBrown6b6204e2013-05-09 09:48:30 +10006257 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Guoqing Jiang21e09582020-04-04 23:57:07 +02006258 if (work_pending(&mddev->del_work))
6259 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11006260 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11006261 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10006262 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11006263 }
6264
6265 del_timer_sync(&mddev->safemode_timer);
6266
Shaohua Li034e33f2016-11-21 10:29:19 -08006267 if (mddev->pers && mddev->pers->quiesce) {
6268 mddev->pers->quiesce(mddev, 1);
6269 mddev->pers->quiesce(mddev, 0);
6270 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006271 md_bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11006272
NeilBrownb6d428c2013-04-24 11:42:42 +10006273 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006274 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08006275 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11006276 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006277 if (!mddev_is_clustered(mddev))
6278 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11006279 md_update_sb(mddev, 1);
6280 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +01006281 /* disable policy to guarantee rdevs free resources for serialization */
6282 mddev->serialize_policy = 0;
6283 mddev_destroy_serial_pool(mddev, NULL, true);
NeilBrowna047e122010-03-29 12:07:53 +11006284}
NeilBrowndefad612011-01-14 09:14:33 +11006285
NeilBrownfd01b882011-10-11 16:47:53 +11006286void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11006287{
NeilBrown29f097c2013-11-14 17:54:51 +11006288 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11006289 __md_stop_writes(mddev);
6290 mddev_unlock(mddev);
6291}
NeilBrown390ee602010-06-01 19:37:27 +10006292EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11006293
NeilBrown5aa61f42014-12-15 12:56:57 +11006294static void mddev_detach(struct mddev *mddev)
6295{
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006296 md_bitmap_wait_behind_writes(mddev);
Guoqing Jiang6b40bec2020-02-11 11:10:04 +01006297 if (mddev->pers && mddev->pers->quiesce && !mddev->suspended) {
NeilBrown5aa61f42014-12-15 12:56:57 +11006298 mddev->pers->quiesce(mddev, 1);
6299 mddev->pers->quiesce(mddev, 0);
6300 }
6301 md_unregister_thread(&mddev->thread);
6302 if (mddev->queue)
6303 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
6304}
6305
NeilBrown5eff3c42012-11-19 10:47:48 +11006306static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11006307{
NeilBrown36d091f2014-12-15 12:56:58 +11006308 struct md_personality *pers = mddev->pers;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006309 md_bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11006310 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10006311 /* Ensure ->event_work is done */
Guoqing Jiang21e09582020-04-04 23:57:07 +02006312 if (mddev->event_work.func)
6313 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11006314 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11006315 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11006316 spin_unlock(&mddev->lock);
6317 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10006318 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11006319 if (pers->sync_request && mddev->to_remove == NULL)
6320 mddev->to_remove = &md_redundancy_group;
6321 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11006322 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Jack Wang6aaa58c2018-10-19 16:21:31 +02006323}
6324
6325void md_stop(struct mddev *mddev)
6326{
6327 /* stop the array and free an attached data structures.
6328 * This is called from dm-raid
6329 */
6330 __md_stop(mddev);
Kent Overstreetafeee512018-05-20 18:25:52 -04006331 bioset_exit(&mddev->bio_set);
6332 bioset_exit(&mddev->sync_set);
NeilBrown5eff3c42012-11-19 10:47:48 +11006333}
6334
NeilBrown390ee602010-06-01 19:37:27 +10006335EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11006336
NeilBrowna05b7ea2012-07-19 15:59:18 +10006337static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11006338{
6339 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11006340 int did_freeze = 0;
6341
6342 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6343 did_freeze = 1;
6344 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6345 md_wakeup_thread(mddev->thread);
6346 }
NeilBrownf851b602014-12-11 10:02:10 +11006347 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11006348 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006349 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11006350 /* Thread might be blocked waiting for metadata update
6351 * which will now never happen */
6352 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11006353
Shaohua Li29530792016-12-08 15:48:19 -08006354 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10006355 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11006356 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006357 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
6358 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10006359 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08006360 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11006361 mddev_lock_nointr(mddev);
6362
NeilBrowna4bd82d2010-03-29 13:23:10 +11006363 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006364 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006365 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006366 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006367 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11006368 if (did_freeze) {
6369 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006370 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006371 md_wakeup_thread(mddev->thread);
6372 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11006373 err = -EBUSY;
6374 goto out;
6375 }
6376 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11006377 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11006378
6379 err = -ENXIO;
6380 if (mddev->ro==1)
6381 goto out;
6382 mddev->ro = 1;
6383 set_disk_ro(mddev->gendisk, 1);
6384 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006385 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6386 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006387 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11006388 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006389 }
6390out:
6391 mutex_unlock(&mddev->open_mutex);
6392 return err;
6393}
6394
NeilBrown9e653b62006-06-26 00:27:58 -07006395/* mode:
6396 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07006397 * 2 - stop but do not disassemble array
6398 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006399static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10006400 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11006403 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11006404 int did_freeze = 0;
6405
6406 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6407 did_freeze = 1;
6408 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6409 md_wakeup_thread(mddev->thread);
6410 }
NeilBrownf851b602014-12-11 10:02:10 +11006411 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11006412 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006413 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11006414 /* Thread might be blocked waiting for metadata update
6415 * which will now never happen */
6416 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11006417
NeilBrown30b8feb2013-11-14 15:16:17 +11006418 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006419 wait_event(resync_wait, (mddev->sync_thread == NULL &&
6420 !test_bit(MD_RECOVERY_RUNNING,
6421 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11006422 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423
NeilBrownc8c00a62009-08-10 12:50:52 +10006424 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006425 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006426 mddev->sysfs_active ||
6427 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006428 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006429 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10006430 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11006431 if (did_freeze) {
6432 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006433 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006434 md_wakeup_thread(mddev->thread);
6435 }
NeilBrown260fa032013-08-27 16:44:13 +10006436 return -EBUSY;
6437 }
NeilBrown6e17b022010-08-07 21:41:19 +10006438 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11006439 if (mddev->ro)
6440 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11006441
NeilBrowndefad612011-01-14 09:14:33 +11006442 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11006443 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01006444 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11006445
NeilBrowna4bd82d2010-03-29 13:23:10 +11006446 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10006447 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08006448
NeilBrowndafb20f2012-03-19 12:46:39 +11006449 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10006450 if (rdev->raid_disk >= 0)
6451 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10006452
NeilBrowna4bd82d2010-03-29 13:23:10 +11006453 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10006454 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006455 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006456 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08006457
NeilBrowna4bd82d2010-03-29 13:23:10 +11006458 if (mddev->ro)
6459 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10006460 } else
6461 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462 /*
6463 * Free resources if final stop
6464 */
NeilBrown9e653b62006-06-26 00:27:58 -07006465 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006466 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467
NeilBrownc3d97142009-12-14 12:49:52 +11006468 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11006469 struct file *f = mddev->bitmap_info.file;
6470 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11006471 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11006472 spin_unlock(&mddev->lock);
6473 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08006474 }
NeilBrownc3d97142009-12-14 12:49:52 +11006475 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08006476
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477 export_array(mddev);
6478
NeilBrown6177b472010-03-29 11:37:13 +11006479 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11006480 if (mddev->hold_active == UNTIL_STOP)
6481 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006482 }
NeilBrownd7603b72006-01-06 00:20:30 -08006483 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006484 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10006485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486}
6487
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006488#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11006489static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490{
NeilBrown3cb03002011-10-11 16:45:26 +11006491 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 int err;
6493
NeilBrowna757e642005-04-16 15:26:42 -07006494 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496
NeilBrown9d487392016-11-02 14:16:49 +11006497 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498
NeilBrowndafb20f2012-03-19 12:46:39 +11006499 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11006501 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 }
NeilBrown9d487392016-11-02 14:16:49 +11006503 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504
NeilBrownd710e132008-10-13 11:55:12 +11006505 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006507 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10006508 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509 }
6510}
6511
6512/*
6513 * lets try to run arrays based on all disks that have arrived
6514 * until now. (those are in pending_raid_disks)
6515 *
6516 * the method: pick the first pending disk, collect all disks with
6517 * the same UUID, remove all from the pending list and put them into
6518 * the 'same_array' list. Then order this list based on superblock
6519 * update time (freshest comes first), kick out 'old' disks and
6520 * compare superblocks. If everything's fine then run it.
6521 *
6522 * If "unit" is allocated, then bump its reference count
6523 */
6524static void autorun_devices(int part)
6525{
NeilBrown3cb03002011-10-11 16:45:26 +11006526 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006527 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528 char b[BDEVNAME_SIZE];
6529
NeilBrown9d487392016-11-02 14:16:49 +11006530 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07006532 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08006534 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11006536 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537
NeilBrown9d487392016-11-02 14:16:49 +11006538 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006540 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006542 pr_debug("md: adding %s ...\n",
6543 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544 list_move(&rdev->same_set, &candidates);
6545 }
6546 /*
6547 * now we have a set of devices, with all of them having
6548 * mostly sane superblocks. It's time to allocate the
6549 * mddev.
6550 */
NeilBrowne8703fe2006-10-03 01:15:59 -07006551 if (part) {
6552 dev = MKDEV(mdp_major,
6553 rdev0->preferred_minor << MdpMinorShift);
6554 unit = MINOR(dev) >> MdpMinorShift;
6555 } else {
6556 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6557 unit = MINOR(dev);
6558 }
6559 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11006560 pr_warn("md: unit number in %s is bad: %d\n",
6561 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 break;
6563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564
6565 md_probe(dev, NULL, NULL);
6566 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006567 if (!mddev || !mddev->gendisk) {
6568 if (mddev)
6569 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 break;
6571 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006572 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006573 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574 else if (mddev->raid_disks || mddev->major_version
6575 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006576 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 mdname(mddev), bdevname(rdev0->bdev,b));
6578 mddev_unlock(mddev);
6579 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006580 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006581 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006582 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583 list_del_init(&rdev->same_set);
6584 if (bind_rdev_to_array(rdev, mddev))
6585 export_rdev(rdev);
6586 }
6587 autorun_array(mddev);
6588 mddev_unlock(mddev);
6589 }
6590 /* on success, candidates will be empty, on error
6591 * it won't...
6592 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006593 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006594 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 mddev_put(mddev);
6598 }
NeilBrown9d487392016-11-02 14:16:49 +11006599 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006601#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602
NeilBrownf72ffdd2014-09-30 14:23:59 +10006603static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604{
6605 mdu_version_t ver;
6606
6607 ver.major = MD_MAJOR_VERSION;
6608 ver.minor = MD_MINOR_VERSION;
6609 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6610
6611 if (copy_to_user(arg, &ver, sizeof(ver)))
6612 return -EFAULT;
6613
6614 return 0;
6615}
6616
NeilBrownf72ffdd2014-09-30 14:23:59 +10006617static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618{
6619 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006620 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006621 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622
NeilBrown1ca69c42012-10-11 13:37:33 +11006623 nr = working = insync = failed = spare = 0;
6624 rcu_read_lock();
6625 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006627 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628 failed++;
6629 else {
6630 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006631 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006632 insync++;
Song Liub347af82016-08-11 17:14:45 -07006633 else if (test_bit(Journal, &rdev->flags))
6634 /* TODO: add journal count to md_u.h */
6635 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 else
6637 spare++;
6638 }
6639 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006640 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641
6642 info.major_version = mddev->major_version;
6643 info.minor_version = mddev->minor_version;
6644 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006645 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006647 info.size = mddev->dev_sectors / 2;
6648 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006649 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 info.nr_disks = nr;
6651 info.raid_disks = mddev->raid_disks;
6652 info.md_minor = mddev->md_minor;
6653 info.not_persistent= !mddev->persistent;
6654
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006655 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 info.state = 0;
6657 if (mddev->in_sync)
6658 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006659 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006660 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006661 if (mddev_is_clustered(mddev))
6662 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006663 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 info.working_disks = working;
6665 info.failed_disks = failed;
6666 info.spare_disks = spare;
6667
6668 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006669 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670
6671 if (copy_to_user(arg, &info, sizeof(info)))
6672 return -EFAULT;
6673
6674 return 0;
6675}
6676
NeilBrownf72ffdd2014-09-30 14:23:59 +10006677static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006678{
6679 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006680 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006681 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006682
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006683 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006684 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006685 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006686
NeilBrown32a76272005-06-21 17:17:14 -07006687 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006688 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006689 /* bitmap enabled */
6690 if (mddev->bitmap_info.file) {
6691 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6692 sizeof(file->pathname));
6693 if (IS_ERR(ptr))
6694 err = PTR_ERR(ptr);
6695 else
6696 memmove(file->pathname, ptr,
6697 sizeof(file->pathname)-(ptr-file->pathname));
6698 }
NeilBrown4af1a042014-12-15 12:57:00 +11006699 spin_unlock(&mddev->lock);
6700
6701 if (err == 0 &&
6702 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006703 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006704
NeilBrown32a76272005-06-21 17:17:14 -07006705 kfree(file);
6706 return err;
6707}
6708
NeilBrownf72ffdd2014-09-30 14:23:59 +10006709static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710{
6711 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006712 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713
6714 if (copy_from_user(&info, arg, sizeof(info)))
6715 return -EFAULT;
6716
NeilBrown1ca69c42012-10-11 13:37:33 +11006717 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006718 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719 if (rdev) {
6720 info.major = MAJOR(rdev->bdev->bd_dev);
6721 info.minor = MINOR(rdev->bdev->bd_dev);
6722 info.raid_disk = rdev->raid_disk;
6723 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006724 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006726 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 info.state |= (1<<MD_DISK_ACTIVE);
6728 info.state |= (1<<MD_DISK_SYNC);
6729 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006730 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006731 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006732 if (test_bit(WriteMostly, &rdev->flags))
6733 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006734 if (test_bit(FailFast, &rdev->flags))
6735 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 } else {
6737 info.major = info.minor = 0;
6738 info.raid_disk = -1;
6739 info.state = (1<<MD_DISK_REMOVED);
6740 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006741 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742
6743 if (copy_to_user(arg, &info, sizeof(info)))
6744 return -EFAULT;
6745
6746 return 0;
6747}
6748
NeilBrownf72ffdd2014-09-30 14:23:59 +10006749static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750{
6751 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006752 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 dev_t dev = MKDEV(info->major,info->minor);
6754
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006755 if (mddev_is_clustered(mddev) &&
6756 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006757 pr_warn("%s: Cannot add to clustered mddev.\n",
6758 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006759 return -EINVAL;
6760 }
6761
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6763 return -EOVERFLOW;
6764
6765 if (!mddev->raid_disks) {
6766 int err;
6767 /* expecting a device which has a superblock */
6768 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6769 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006770 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 PTR_ERR(rdev));
6772 return PTR_ERR(rdev);
6773 }
6774 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006775 struct md_rdev *rdev0
6776 = list_entry(mddev->disks.next,
6777 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006778 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 .load_super(rdev, rdev0, mddev->minor_version);
6780 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006781 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006782 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 bdevname(rdev0->bdev,b2));
6784 export_rdev(rdev);
6785 return -EINVAL;
6786 }
6787 }
6788 err = bind_rdev_to_array(rdev, mddev);
6789 if (err)
6790 export_rdev(rdev);
6791 return err;
6792 }
6793
6794 /*
6795 * add_new_disk can be used once the array is assembled
6796 * to add "hot spares". They must already have a superblock
6797 * written
6798 */
6799 if (mddev->pers) {
6800 int err;
6801 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006802 pr_warn("%s: personality does not support diskops!\n",
6803 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 return -EINVAL;
6805 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006806 if (mddev->persistent)
6807 rdev = md_import_device(dev, mddev->major_version,
6808 mddev->minor_version);
6809 else
6810 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006812 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 PTR_ERR(rdev));
6814 return PTR_ERR(rdev);
6815 }
NeilBrown1a855a02010-12-09 16:36:28 +11006816 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006817 if (!mddev->persistent) {
6818 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006819 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006820 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006821 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006822 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006823 } else
NeilBrown41158c72005-06-21 17:17:25 -07006824 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006825 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006826 } else
6827 super_types[mddev->major_version].
6828 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006829 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006830 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006831 /* This was a hot-add request, but events doesn't
6832 * match, so reject it.
6833 */
6834 export_rdev(rdev);
6835 return -EINVAL;
6836 }
6837
NeilBrownb2d444d2005-11-08 21:39:31 -08006838 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006839 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6840 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006841 else
6842 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006843 if (info->state & (1<<MD_DISK_FAILFAST))
6844 set_bit(FailFast, &rdev->flags);
6845 else
6846 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006847
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006848 if (info->state & (1<<MD_DISK_JOURNAL)) {
6849 struct md_rdev *rdev2;
6850 bool has_journal = false;
6851
6852 /* make sure no existing journal disk */
6853 rdev_for_each(rdev2, mddev) {
6854 if (test_bit(Journal, &rdev2->flags)) {
6855 has_journal = true;
6856 break;
6857 }
6858 }
NeilBrown230b55f2017-10-17 14:24:09 +11006859 if (has_journal || mddev->bitmap) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006860 export_rdev(rdev);
6861 return -EBUSY;
6862 }
Song Liubac624f2015-08-13 14:31:55 -07006863 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006864 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006865 /*
6866 * check whether the device shows up in other nodes
6867 */
6868 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006869 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006870 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006871 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006872 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006873 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006874 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006875 export_rdev(rdev);
6876 return err;
6877 }
6878 }
6879 }
6880
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881 rdev->raid_disk = -1;
6882 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006883
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 if (err)
6885 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006886
6887 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006888 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6889 if (!err) {
6890 err = md_cluster_ops->new_disk_ack(mddev,
6891 err == 0);
6892 if (err)
6893 md_kick_rdev_from_array(rdev);
6894 }
6895 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006896 if (err)
6897 md_cluster_ops->add_new_disk_cancel(mddev);
6898 else
6899 err = add_bound_rdev(rdev);
6900 }
6901
6902 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006903 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006904
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 return err;
6906 }
6907
6908 /* otherwise, add_new_disk is only allowed
6909 * for major_version==0 superblocks
6910 */
6911 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006912 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913 return -EINVAL;
6914 }
6915
6916 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6917 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006918 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006920 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 PTR_ERR(rdev));
6922 return PTR_ERR(rdev);
6923 }
6924 rdev->desc_nr = info->number;
6925 if (info->raid_disk < mddev->raid_disks)
6926 rdev->raid_disk = info->raid_disk;
6927 else
6928 rdev->raid_disk = -1;
6929
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006931 if (info->state & (1<<MD_DISK_SYNC))
6932 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006934 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6935 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006936 if (info->state & (1<<MD_DISK_FAILFAST))
6937 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006938
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006940 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006941 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6942 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006943 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006944 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945
NeilBrown2bf071b2006-01-06 00:20:55 -08006946 err = bind_rdev_to_array(rdev, mddev);
6947 if (err) {
6948 export_rdev(rdev);
6949 return err;
6950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951 }
6952
6953 return 0;
6954}
6955
NeilBrownf72ffdd2014-09-30 14:23:59 +10006956static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957{
6958 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006959 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960
Yufen Yuc42a0e22018-05-04 18:08:10 +08006961 if (!mddev->pers)
6962 return -ENODEV;
6963
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964 rdev = find_rdev(mddev, dev);
6965 if (!rdev)
6966 return -ENXIO;
6967
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006968 if (rdev->raid_disk < 0)
6969 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006970
NeilBrown3ea8929d2013-04-24 11:42:41 +10006971 clear_bit(Blocked, &rdev->flags);
6972 remove_and_add_spares(mddev, rdev);
6973
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974 if (rdev->raid_disk >= 0)
6975 goto busy;
6976
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006977kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006978 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006979 md_cluster_ops->remove_disk(mddev, rdev);
6980
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006981 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006982 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006983 if (mddev->thread)
6984 md_wakeup_thread(mddev->thread);
6985 else
6986 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006987 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988
6989 return 0;
6990busy:
NeilBrown9d487392016-11-02 14:16:49 +11006991 pr_debug("md: cannot remove active disk %s from %s ...\n",
6992 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993 return -EBUSY;
6994}
6995
NeilBrownf72ffdd2014-09-30 14:23:59 +10006996static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997{
6998 char b[BDEVNAME_SIZE];
6999 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11007000 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001
7002 if (!mddev->pers)
7003 return -ENODEV;
7004
7005 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11007006 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 mdname(mddev));
7008 return -EINVAL;
7009 }
7010 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11007011 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012 mdname(mddev));
7013 return -EINVAL;
7014 }
7015
NeilBrownd710e132008-10-13 11:55:12 +11007016 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11007018 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019 PTR_ERR(rdev));
7020 return -EINVAL;
7021 }
7022
7023 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11007024 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025 else
Mike Snitzer77304d22010-11-08 14:39:12 +01007026 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027
NeilBrown8190e752009-06-18 08:48:58 +10007028 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029
NeilBrownb2d444d2005-11-08 21:39:31 -08007030 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11007031 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 bdevname(rdev->bdev,b), mdname(mddev));
7033 err = -EINVAL;
7034 goto abort_export;
7035 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007036
NeilBrownb2d444d2005-11-08 21:39:31 -08007037 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07007039 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08007040 err = bind_rdev_to_array(rdev, mddev);
7041 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05007042 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043
7044 /*
7045 * The rest should better be atomic, we can have disk failures
7046 * noticed in interrupt contexts ...
7047 */
7048
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 rdev->raid_disk = -1;
7050
Shaohua Li29530792016-12-08 15:48:19 -08007051 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11007052 if (!mddev->thread)
7053 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 /*
7055 * Kick recovery, maybe this spare has to be added to the
7056 * array immediately.
7057 */
7058 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7059 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08007060 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061 return 0;
7062
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063abort_export:
7064 export_rdev(rdev);
7065 return err;
7066}
7067
NeilBrownfd01b882011-10-11 16:47:53 +11007068static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07007069{
NeilBrown035328c2014-04-09 12:25:40 +10007070 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07007071
NeilBrown36fa3062005-09-09 16:23:45 -07007072 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10007073 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07007074 return -EBUSY;
7075 if (mddev->recovery || mddev->sync_thread)
7076 return -EBUSY;
7077 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07007078 }
7079
NeilBrown36fa3062005-09-09 16:23:45 -07007080 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10007081 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11007082 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07007083
NeilBrown1e594bb2014-12-15 12:57:00 +11007084 if (mddev->bitmap || mddev->bitmap_info.file)
7085 return -EEXIST; /* cannot add when bitmap is present */
7086 f = fget(fd);
7087
7088 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11007089 pr_warn("%s: error: failed to get bitmap file\n",
7090 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07007091 return -EBADF;
7092 }
7093
NeilBrown1e594bb2014-12-15 12:57:00 +11007094 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10007095 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11007096 pr_warn("%s: error: bitmap file must be a regular file\n",
7097 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10007098 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11007099 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11007100 pr_warn("%s: error: bitmap file must open for write\n",
7101 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10007102 err = -EBADF;
7103 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11007104 pr_warn("%s: error: bitmap file is already in use\n",
7105 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10007106 err = -EBUSY;
7107 }
7108 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11007109 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07007110 return err;
7111 }
NeilBrown1e594bb2014-12-15 12:57:00 +11007112 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11007113 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07007114 } else if (mddev->bitmap == NULL)
7115 return -ENOENT; /* cannot remove what isn't there */
7116 err = 0;
7117 if (mddev->pers) {
NeilBrown69e51b42010-06-01 19:37:35 +10007118 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007119 struct bitmap *bitmap;
7120
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007121 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007122 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007123 if (!IS_ERR(bitmap)) {
7124 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007125 err = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11007126 } else
7127 err = PTR_ERR(bitmap);
NeilBrown52a0d492017-10-17 13:46:43 +11007128 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007129 md_bitmap_destroy(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11007130 fd = -1;
7131 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007132 mddev_resume(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11007133 } else if (fd < 0) {
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007134 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007135 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007136 mddev_resume(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07007137 }
NeilBrownd7375ab2006-06-26 00:27:43 -07007138 }
7139 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11007140 struct file *f = mddev->bitmap_info.file;
7141 if (f) {
7142 spin_lock(&mddev->lock);
7143 mddev->bitmap_info.file = NULL;
7144 spin_unlock(&mddev->lock);
7145 fput(f);
7146 }
NeilBrown36fa3062005-09-09 16:23:45 -07007147 }
7148
NeilBrown32a76272005-06-21 17:17:14 -07007149 return err;
7150}
7151
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152/*
7153 * set_array_info is used two different ways
7154 * The original usage is when creating a new array.
7155 * In this usage, raid_disks is > 0 and it together with
7156 * level, size, not_persistent,layout,chunksize determine the
7157 * shape of the array.
7158 * This will always create an array with a type-0.90.0 superblock.
7159 * The newer usage is when assembling an array.
7160 * In this case raid_disks will be 0, and the major_version field is
7161 * use to determine which style super-blocks are to be found on the devices.
7162 * The minor and patch _version numbers are also kept incase the
7163 * super_block handler wishes to interpret them.
7164 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10007165static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166{
7167
7168 if (info->raid_disks == 0) {
7169 /* just setting version number for superblock loading */
7170 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07007171 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172 super_types[info->major_version].name == NULL) {
7173 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11007174 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175 info->major_version);
7176 return -EINVAL;
7177 }
7178 mddev->major_version = info->major_version;
7179 mddev->minor_version = info->minor_version;
7180 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08007181 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11007182 /* ensure mddev_put doesn't delete this now that there
7183 * is some minimal configuration.
7184 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11007185 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186 return 0;
7187 }
7188 mddev->major_version = MD_MAJOR_VERSION;
7189 mddev->minor_version = MD_MINOR_VERSION;
7190 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11007191 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192
7193 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08007194 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11007195 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196 mddev->raid_disks = info->raid_disks;
7197 /* don't set md_minor, it is determined by which /dev/md* was
7198 * openned
7199 */
7200 if (info->state & (1<<MD_SB_CLEAN))
7201 mddev->recovery_cp = MaxSector;
7202 else
7203 mddev->recovery_cp = 0;
7204 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08007205 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206
7207 mddev->layout = info->layout;
NeilBrown33f2c352019-09-09 16:52:29 +10007208 if (mddev->level == 0)
7209 /* Cannot trust RAID0 layout info here */
7210 mddev->layout = -1;
Andre Noll9d8f0362009-06-18 08:45:01 +10007211 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212
Shaohua Li29530792016-12-08 15:48:19 -08007213 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11007214 mddev->max_disks = MD_SB_DISKS;
7215 mddev->flags = 0;
7216 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08007217 }
7218 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219
NeilBrownc3d97142009-12-14 12:49:52 +11007220 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10007221 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11007222 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08007223
NeilBrownf6705572006-03-27 01:18:11 -08007224 mddev->reshape_position = MaxSector;
7225
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226 /*
7227 * Generate a 128 bit UUID
7228 */
7229 get_random_bytes(mddev->uuid, 16);
7230
NeilBrownf6705572006-03-27 01:18:11 -08007231 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10007232 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08007233 mddev->new_layout = mddev->layout;
7234 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007235 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08007236
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 return 0;
7238}
7239
NeilBrownfd01b882011-10-11 16:47:53 +11007240void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11007241{
Shaohua Liefa4b772017-10-18 22:08:13 -07007242 lockdep_assert_held(&mddev->reconfig_mutex);
Dan Williamsb522adc2009-03-31 15:00:31 +11007243
7244 if (mddev->external_size)
7245 return;
7246
Dan Williams1f403622009-03-31 14:59:03 +11007247 mddev->array_sectors = array_sectors;
7248}
7249EXPORT_SYMBOL(md_set_array_sectors);
7250
NeilBrownfd01b882011-10-11 16:47:53 +11007251static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08007252{
NeilBrown3cb03002011-10-11 16:45:26 +11007253 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08007254 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10007255 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08007256 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04007257
NeilBrowna35b0d62006-01-06 00:20:49 -08007258 if (mddev->pers->resize == NULL)
7259 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10007260 /* The "num_sectors" is the number of sectors of each device that
7261 * is used. This can only make sense for arrays with redundancy.
7262 * linear and raid0 always use whatever space is available. We can only
7263 * consider changing this number if no resync or reconstruction is
7264 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10007265 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10007266 * of each device. If num_sectors is zero, we find the largest size
7267 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08007268 */
NeilBrownf851b602014-12-11 10:02:10 +11007269 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7270 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08007271 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10007272 if (mddev->ro)
7273 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10007274
NeilBrowndafb20f2012-03-19 12:46:39 +11007275 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11007276 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07007277
Andre Nolld71f9f82008-07-11 22:02:22 +10007278 if (fit && (num_sectors == 0 || num_sectors > avail))
7279 num_sectors = avail;
7280 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08007281 return -ENOSPC;
7282 }
Andre Nolld71f9f82008-07-11 22:02:22 +10007283 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08007284 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08007285 if (mddev_is_clustered(mddev))
7286 md_cluster_ops->update_size(mddev, old_dev_sectors);
7287 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08007288 set_capacity(mddev->gendisk, mddev->array_sectors);
7289 revalidate_disk(mddev->gendisk);
7290 }
7291 }
NeilBrowna35b0d62006-01-06 00:20:49 -08007292 return rv;
7293}
7294
NeilBrownfd01b882011-10-11 16:47:53 +11007295static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08007296{
7297 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10007298 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08007299 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08007300 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08007301 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10007302 if (mddev->ro)
7303 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08007304 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10007305 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08007306 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11007307 if (mddev->sync_thread ||
7308 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7309 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08007310 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10007311
7312 rdev_for_each(rdev, mddev) {
7313 if (mddev->raid_disks < raid_disks &&
7314 rdev->data_offset < rdev->new_data_offset)
7315 return -EINVAL;
7316 if (mddev->raid_disks > raid_disks &&
7317 rdev->data_offset > rdev->new_data_offset)
7318 return -EINVAL;
7319 }
7320
NeilBrown63c70c42006-03-27 01:18:13 -08007321 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10007322 if (mddev->delta_disks < 0)
7323 mddev->reshape_backwards = 1;
7324 else if (mddev->delta_disks > 0)
7325 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08007326
7327 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10007328 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11007329 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007330 mddev->reshape_backwards = 0;
7331 }
NeilBrownda943b992006-01-06 00:20:54 -08007332 return rv;
7333}
7334
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335/*
7336 * update_array_info is used to change the configuration of an
7337 * on-line array.
7338 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
7339 * fields in the info are checked against the array.
7340 * Any differences that cannot be handled will cause an error.
7341 * Normally, only one change can be managed at a time.
7342 */
NeilBrownfd01b882011-10-11 16:47:53 +11007343static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344{
7345 int rv = 0;
7346 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007347 int state = 0;
7348
7349 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11007350 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07007351 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352
7353 if (mddev->major_version != info->major_version ||
7354 mddev->minor_version != info->minor_version ||
7355/* mddev->patch_version != info->patch_version || */
7356 mddev->ctime != info->ctime ||
7357 mddev->level != info->level ||
7358/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08007359 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10007360 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07007361 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
7362 ((state^info->state) & 0xfffffe00)
7363 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 return -EINVAL;
7365 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11007366 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
7367 cnt++;
7368 if (mddev->raid_disks != info->raid_disks)
7369 cnt++;
7370 if (mddev->layout != info->layout)
7371 cnt++;
7372 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
7373 cnt++;
7374 if (cnt == 0)
7375 return 0;
7376 if (cnt > 1)
7377 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378
7379 if (mddev->layout != info->layout) {
7380 /* Change layout
7381 * we don't need to do anything at the md level, the
7382 * personality will take care of it all.
7383 */
NeilBrown50ac1682009-06-18 08:47:55 +10007384 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007386 else {
7387 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10007388 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10007389 if (rv)
7390 mddev->new_layout = mddev->layout;
7391 return rv;
7392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007394 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10007395 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396
NeilBrownda943b992006-01-06 00:20:54 -08007397 if (mddev->raid_disks != info->raid_disks)
7398 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399
NeilBrown36fa3062005-09-09 16:23:45 -07007400 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007401 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
7402 rv = -EINVAL;
7403 goto err;
7404 }
7405 if (mddev->recovery || mddev->sync_thread) {
7406 rv = -EBUSY;
7407 goto err;
7408 }
NeilBrown36fa3062005-09-09 16:23:45 -07007409 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007410 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07007411 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007412 if (mddev->bitmap) {
7413 rv = -EEXIST;
7414 goto err;
7415 }
7416 if (mddev->bitmap_info.default_offset == 0) {
7417 rv = -EINVAL;
7418 goto err;
7419 }
NeilBrownc3d97142009-12-14 12:49:52 +11007420 mddev->bitmap_info.offset =
7421 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10007422 mddev->bitmap_info.space =
7423 mddev->bitmap_info.default_space;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007424 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007425 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007426 if (!IS_ERR(bitmap)) {
7427 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007428 rv = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11007429 } else
7430 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07007431 if (rv)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007432 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007433 mddev_resume(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07007434 } else {
7435 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007436 if (!mddev->bitmap) {
7437 rv = -ENOENT;
7438 goto err;
7439 }
7440 if (mddev->bitmap->storage.file) {
7441 rv = -EINVAL;
7442 goto err;
7443 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11007444 if (mddev->bitmap_info.nodes) {
7445 /* hold PW on all the bitmap lock */
7446 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11007447 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 +11007448 rv = -EPERM;
7449 md_cluster_ops->unlock_all_bitmaps(mddev);
7450 goto err;
7451 }
7452
7453 mddev->bitmap_info.nodes = 0;
7454 md_cluster_ops->leave(mddev);
Zhao Hemingedee9df2020-07-21 02:08:53 +08007455 module_put(md_cluster_mod);
Zhao Heming7c9d5c52020-07-21 02:08:52 +08007456 mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY;
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11007457 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007458 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007459 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007460 mddev_resume(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11007461 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007462 }
7463 }
NeilBrown850b2b422006-10-03 01:15:46 -07007464 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007465 return rv;
7466err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467 return rv;
7468}
7469
NeilBrownfd01b882011-10-11 16:47:53 +11007470static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471{
NeilBrown3cb03002011-10-11 16:45:26 +11007472 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11007473 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474
7475 if (mddev->pers == NULL)
7476 return -ENODEV;
7477
NeilBrown1ca69c42012-10-11 13:37:33 +11007478 rcu_read_lock();
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01007479 rdev = md_find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11007481 err = -ENODEV;
7482 else {
7483 md_error(mddev, rdev);
7484 if (!test_bit(Faulty, &rdev->flags))
7485 err = -EBUSY;
7486 }
7487 rcu_read_unlock();
7488 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489}
7490
Andre Noll2f9618c2008-04-25 18:57:58 +02007491/*
7492 * We have a problem here : there is no easy way to give a CHS
7493 * virtual geometry. We currently pretend that we have a 2 heads
7494 * 4 sectors (with a BIG number of cylinders...). This drives
7495 * dosfs just mad... ;-)
7496 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007497static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7498{
NeilBrownfd01b882011-10-11 16:47:53 +11007499 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007500
7501 geo->heads = 2;
7502 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11007503 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007504 return 0;
7505}
7506
Nicolas Schichancb335f82014-01-15 16:58:52 +01007507static inline bool md_ioctl_valid(unsigned int cmd)
7508{
7509 switch (cmd) {
7510 case ADD_NEW_DISK:
7511 case BLKROSET:
7512 case GET_ARRAY_INFO:
7513 case GET_BITMAP_FILE:
7514 case GET_DISK_INFO:
7515 case HOT_ADD_DISK:
7516 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007517 case RAID_AUTORUN:
7518 case RAID_VERSION:
7519 case RESTART_ARRAY_RW:
7520 case RUN_ARRAY:
7521 case SET_ARRAY_INFO:
7522 case SET_BITMAP_FILE:
7523 case SET_DISK_FAULTY:
7524 case STOP_ARRAY:
7525 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007526 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007527 return true;
7528 default:
7529 return false;
7530 }
7531}
7532
Al Viroa39907f2008-03-02 10:31:15 -05007533static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534 unsigned int cmd, unsigned long arg)
7535{
7536 int err = 0;
7537 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11007538 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10007539 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08007540 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541
Nicolas Schichancb335f82014-01-15 16:58:52 +01007542 if (!md_ioctl_valid(cmd))
7543 return -ENOTTY;
7544
NeilBrown506c9e42011-12-23 10:17:26 +11007545 switch (cmd) {
7546 case RAID_VERSION:
7547 case GET_ARRAY_INFO:
7548 case GET_DISK_INFO:
7549 break;
7550 default:
7551 if (!capable(CAP_SYS_ADMIN))
7552 return -EACCES;
7553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007554
7555 /*
7556 * Commands dealing with the RAID driver but not any
7557 * particular array:
7558 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007559 switch (cmd) {
7560 case RAID_VERSION:
7561 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007562 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11007565 case RAID_AUTORUN:
7566 err = 0;
7567 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007568 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11007570 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571 }
7572
7573 /*
7574 * Commands creating/starting a new array:
7575 */
7576
Al Viroa39907f2008-03-02 10:31:15 -05007577 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578
7579 if (!mddev) {
7580 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007581 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582 }
7583
NeilBrown1ca69c42012-10-11 13:37:33 +11007584 /* Some actions do not requires the mutex */
7585 switch (cmd) {
7586 case GET_ARRAY_INFO:
7587 if (!mddev->raid_disks && !mddev->external)
7588 err = -ENODEV;
7589 else
7590 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007591 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007592
7593 case GET_DISK_INFO:
7594 if (!mddev->raid_disks && !mddev->external)
7595 err = -ENODEV;
7596 else
7597 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007598 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007599
7600 case SET_DISK_FAULTY:
7601 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007602 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007603
7604 case GET_BITMAP_FILE:
7605 err = get_bitmap_file(mddev, argp);
7606 goto out;
7607
NeilBrown1ca69c42012-10-11 13:37:33 +11007608 }
7609
Guoqing Jiang78b990c2020-04-04 23:57:10 +02007610 if (cmd == ADD_NEW_DISK || cmd == HOT_ADD_DISK)
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02007611 flush_rdev_wq(mddev);
NeilBrowna7a3f082012-12-11 13:35:54 +11007612
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007613 if (cmd == HOT_REMOVE_DISK)
7614 /* need to ensure recovery thread has run */
7615 wait_event_interruptible_timeout(mddev->sb_wait,
7616 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007617 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007618 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007619 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7620 /* Need to flush page cache, and ensure no-one else opens
7621 * and writes
7622 */
7623 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007624 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007625 mutex_unlock(&mddev->open_mutex);
7626 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007627 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007628 }
NeilBrown065e5192017-04-06 11:16:33 +08007629 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007630 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007631 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007632 mutex_unlock(&mddev->open_mutex);
7633 sync_blockdev(bdev);
7634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635 err = mddev_lock(mddev);
7636 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007637 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7638 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007639 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640 }
7641
NeilBrownc02c0ae2012-12-11 13:39:21 +11007642 if (cmd == SET_ARRAY_INFO) {
7643 mdu_array_info_t info;
7644 if (!arg)
7645 memset(&info, 0, sizeof(info));
7646 else if (copy_from_user(&info, argp, sizeof(info))) {
7647 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007648 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007649 }
7650 if (mddev->pers) {
7651 err = update_array_info(mddev, &info);
7652 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007653 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007654 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007656 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007657 }
7658 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007659 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007660 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007661 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007662 }
7663 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007664 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007665 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007666 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007667 }
7668 err = set_array_info(mddev, &info);
7669 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007670 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007671 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007672 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007673 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674 }
7675
7676 /*
7677 * Commands querying/configuring an existing array:
7678 */
NeilBrown32a76272005-06-21 17:17:14 -07007679 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007680 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007681 if ((!mddev->raid_disks && !mddev->external)
7682 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7683 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7684 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007686 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007687 }
7688
7689 /*
7690 * Commands even a read-only array can execute:
7691 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007692 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007693 case RESTART_ARRAY_RW:
7694 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007695 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007696
7697 case STOP_ARRAY:
7698 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007699 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007700
7701 case STOP_ARRAY_RO:
7702 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007703 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007704
NeilBrown3ea8929d2013-04-24 11:42:41 +10007705 case HOT_REMOVE_DISK:
7706 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007707 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007708
NeilBrown7ceb17e2013-04-24 11:42:42 +10007709 case ADD_NEW_DISK:
7710 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007711 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007712 * So require mddev->pers and MD_DISK_SYNC.
7713 */
7714 if (mddev->pers) {
7715 mdu_disk_info_t info;
7716 if (copy_from_user(&info, argp, sizeof(info)))
7717 err = -EFAULT;
7718 else if (!(info.state & (1<<MD_DISK_SYNC)))
7719 /* Need to clear read-only for this */
7720 break;
7721 else
7722 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007723 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007724 }
7725 break;
7726
NeilBrownc02c0ae2012-12-11 13:39:21 +11007727 case BLKROSET:
7728 if (get_user(ro, (int __user *)(arg))) {
7729 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007730 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007731 }
7732 err = -EINVAL;
7733
7734 /* if the bdev is going readonly the value of mddev->ro
7735 * does not matter, no writes are coming
7736 */
7737 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007738 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007739
NeilBrownc02c0ae2012-12-11 13:39:21 +11007740 /* are we are already prepared for writes? */
7741 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007742 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007743
7744 /* transitioning to readauto need only happen for
7745 * arrays that call md_write_start
7746 */
7747 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007748 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007749 if (err == 0) {
7750 mddev->ro = 2;
7751 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007752 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007753 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007754 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755 }
7756
7757 /*
7758 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007759 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760 */
NeilBrown326eb172014-09-30 15:36:28 +10007761 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007762 if (mddev->ro == 2) {
7763 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007764 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007765 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007766 /* mddev_unlock will wake thread */
7767 /* If a device failed while we were read-only, we
7768 * need to make sure the metadata is updated now.
7769 */
Shaohua Li29530792016-12-08 15:48:19 -08007770 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007771 mddev_unlock(mddev);
7772 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007773 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7774 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007775 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007776 }
NeilBrownf91de922005-11-08 21:39:36 -08007777 } else {
7778 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007779 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781 }
7782
NeilBrownc02c0ae2012-12-11 13:39:21 +11007783 switch (cmd) {
7784 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007786 mdu_disk_info_t info;
7787 if (copy_from_user(&info, argp, sizeof(info)))
7788 err = -EFAULT;
7789 else
7790 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007791 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007794 case CLUSTERED_DISK_NACK:
7795 if (mddev_is_clustered(mddev))
7796 md_cluster_ops->new_disk_ack(mddev, false);
7797 else
7798 err = -EINVAL;
7799 goto unlock;
7800
NeilBrownc02c0ae2012-12-11 13:39:21 +11007801 case HOT_ADD_DISK:
7802 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007803 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804
NeilBrownc02c0ae2012-12-11 13:39:21 +11007805 case RUN_ARRAY:
7806 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007807 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808
NeilBrownc02c0ae2012-12-11 13:39:21 +11007809 case SET_BITMAP_FILE:
7810 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007811 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007812
NeilBrownc02c0ae2012-12-11 13:39:21 +11007813 default:
7814 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007815 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816 }
7817
NeilBrown3adc28d2014-09-30 15:46:41 +10007818unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007819 if (mddev->hold_active == UNTIL_IOCTL &&
7820 err != -EINVAL)
7821 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007823out:
NeilBrown065e5192017-04-06 11:16:33 +08007824 if(did_set_md_closing)
7825 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007826 return err;
7827}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007828#ifdef CONFIG_COMPAT
7829static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7830 unsigned int cmd, unsigned long arg)
7831{
7832 switch (cmd) {
7833 case HOT_REMOVE_DISK:
7834 case HOT_ADD_DISK:
7835 case SET_DISK_FAULTY:
7836 case SET_BITMAP_FILE:
7837 /* These take in integer arg, do not convert */
7838 break;
7839 default:
7840 arg = (unsigned long)compat_ptr(arg);
7841 break;
7842 }
7843
7844 return md_ioctl(bdev, mode, cmd, arg);
7845}
7846#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847
Al Viroa39907f2008-03-02 10:31:15 -05007848static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007849{
7850 /*
7851 * Succeed if we can lock the mddev, which confirms that
7852 * it isn't being stopped right now.
7853 */
NeilBrownfd01b882011-10-11 16:47:53 +11007854 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007855 int err;
7856
Yuanhan Liu0c098222012-05-22 13:55:32 +10007857 if (!mddev)
7858 return -ENODEV;
7859
NeilBrownd3374822009-01-09 08:31:10 +11007860 if (mddev->gendisk != bdev->bd_disk) {
7861 /* we are racing with mddev_put which is discarding this
7862 * bd_disk.
7863 */
7864 mddev_put(mddev);
7865 /* Wait until bdev->bd_disk is definitely gone */
Guoqing Jiangf6766ff2020-04-04 23:57:09 +02007866 if (work_pending(&mddev->del_work))
7867 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007868 /* Then retry the open from the top */
7869 return -ERESTARTSYS;
7870 }
7871 BUG_ON(mddev != bdev->bd_disk->private_data);
7872
NeilBrownc8c00a62009-08-10 12:50:52 +10007873 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874 goto out;
7875
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007876 if (test_bit(MD_CLOSING, &mddev->flags)) {
7877 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007878 err = -ENODEV;
7879 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007880 }
7881
Linus Torvalds1da177e2005-04-16 15:20:36 -07007882 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007883 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007884 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007885
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007886 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007888 if (err)
7889 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890 return err;
7891}
7892
Al Virodb2a1442013-05-05 21:52:57 -04007893static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007895 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007896
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007897 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007898 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007900}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007901
7902static int md_media_changed(struct gendisk *disk)
7903{
NeilBrownfd01b882011-10-11 16:47:53 +11007904 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007905
7906 return mddev->changed;
7907}
7908
7909static int md_revalidate(struct gendisk *disk)
7910{
NeilBrownfd01b882011-10-11 16:47:53 +11007911 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007912
7913 mddev->changed = 0;
7914 return 0;
7915}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007916static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917{
7918 .owner = THIS_MODULE,
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02007919 .submit_bio = md_submit_bio,
Al Viroa39907f2008-03-02 10:31:15 -05007920 .open = md_open,
7921 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007922 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007923#ifdef CONFIG_COMPAT
7924 .compat_ioctl = md_compat_ioctl,
7925#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007926 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007927 .media_changed = md_media_changed,
7928 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929};
7930
NeilBrownf72ffdd2014-09-30 14:23:59 +10007931static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932{
NeilBrown2b8bf342011-10-11 16:48:23 +11007933 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007934
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935 /*
7936 * md_thread is a 'system-thread', it's priority should be very
7937 * high. We avoid resource deadlocks individually in each
7938 * raid personality. (RAID5 does preallocation) We also use RR and
7939 * the very same RT priority as kswapd, thus we will never get
7940 * into a priority inversion deadlock.
7941 *
7942 * we definitely have to have equal or higher priority than
7943 * bdflush, otherwise bdflush will deadlock if there are too
7944 * many dirty RAID5 blocks.
7945 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007946
NeilBrown6985c432005-10-19 21:23:47 -07007947 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007948 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949
NeilBrown93588e22005-11-15 00:09:12 -08007950 /* We need to wait INTERRUPTIBLE so that
7951 * we don't add to the load-average.
7952 * That means we need to be sure no signals are
7953 * pending
7954 */
7955 if (signal_pending(current))
7956 flush_signals(current);
7957
7958 wait_event_interruptible_timeout
7959 (thread->wqueue,
7960 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007961 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007962 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963
NeilBrown6c987912011-01-14 09:13:53 +11007964 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007965 if (kthread_should_park())
7966 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007967 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007968 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007969 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007970
Linus Torvalds1da177e2005-04-16 15:20:36 -07007971 return 0;
7972}
7973
NeilBrown2b8bf342011-10-11 16:48:23 +11007974void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007975{
7976 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007977 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Guoqing Jiangd1d90142017-10-09 10:32:48 +08007978 set_bit(THREAD_WAKEUP, &thread->flags);
7979 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007980 }
7981}
NeilBrown6c144d32014-09-30 16:15:38 +10007982EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007983
Shaohua Li4ed87312012-10-11 13:34:00 +11007984struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7985 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007986{
NeilBrown2b8bf342011-10-11 16:48:23 +11007987 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007988
NeilBrown2b8bf342011-10-11 16:48:23 +11007989 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007990 if (!thread)
7991 return NULL;
7992
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993 init_waitqueue_head(&thread->wqueue);
7994
Linus Torvalds1da177e2005-04-16 15:20:36 -07007995 thread->run = run;
7996 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007997 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007998 thread->tsk = kthread_run(md_thread, thread,
7999 "%s_%s",
8000 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10008001 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07008002 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003 kfree(thread);
8004 return NULL;
8005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006 return thread;
8007}
NeilBrown6c144d32014-09-30 16:15:38 +10008008EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008009
NeilBrown2b8bf342011-10-11 16:48:23 +11008010void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008011{
NeilBrown2b8bf342011-10-11 16:48:23 +11008012 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11008013 if (!thread)
8014 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11008015 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10008016 /* Locking ensures that mddev_unlock does not wake_up a
8017 * non-existent thread
8018 */
8019 spin_lock(&pers_lock);
8020 *threadp = NULL;
8021 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07008022
8023 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024 kfree(thread);
8025}
NeilBrown6c144d32014-09-30 16:15:38 +10008026EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008027
NeilBrownfd01b882011-10-11 16:47:53 +11008028void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029{
NeilBrownb2d444d2005-11-08 21:39:31 -08008030 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008031 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07008032
NeilBrownde393cd2011-07-28 11:31:48 +10008033 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008034 return;
8035 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10008036 if (mddev->degraded)
8037 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008038 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008039 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
8040 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8041 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10008042 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02008043 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08008044 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045}
NeilBrown6c144d32014-09-30 16:15:38 +10008046EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008047
8048/* seq_file implementation /proc/mdstat */
8049
8050static void status_unused(struct seq_file *seq)
8051{
8052 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008053 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008054
8055 seq_printf(seq, "unused devices: ");
8056
Cheng Renquan159ec1f2009-01-09 08:31:08 +11008057 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008058 char b[BDEVNAME_SIZE];
8059 i++;
8060 seq_printf(seq, "%s ",
8061 bdevname(rdev->bdev,b));
8062 }
8063 if (!i)
8064 seq_printf(seq, "<none>");
8065
8066 seq_printf(seq, "\n");
8067}
8068
NeilBrownf7851be2015-07-02 17:12:58 +10008069static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008070{
NeilBrowndd71cf62009-05-07 12:49:35 +10008071 sector_t max_sectors, resync, res;
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008072 unsigned long dt, db = 0;
8073 sector_t rt, curr_mark_cnt, resync_mark_cnt;
8074 int scale, recovery_active;
NeilBrown4588b422006-03-27 01:18:04 -08008075 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008076
NeilBrownc804cde2012-05-21 09:28:33 +10008077 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8078 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10008079 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008080 else
NeilBrowndd71cf62009-05-07 12:49:35 +10008081 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008082
NeilBrownf7851be2015-07-02 17:12:58 +10008083 resync = mddev->curr_resync;
8084 if (resync <= 3) {
8085 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
8086 /* Still cleaning up */
8087 resync = max_sectors;
Nate Daileyd2e2ec82017-11-30 11:33:30 -05008088 } else if (resync > max_sectors)
8089 resync = max_sectors;
8090 else
NeilBrownf7851be2015-07-02 17:12:58 +10008091 resync -= atomic_read(&mddev->recovery_active);
8092
8093 if (resync == 0) {
Guoqing Jiang0357ba22018-07-02 16:26:25 +08008094 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
8095 struct md_rdev *rdev;
8096
8097 rdev_for_each(rdev, mddev)
8098 if (rdev->raid_disk >= 0 &&
8099 !test_bit(Faulty, &rdev->flags) &&
8100 rdev->recovery_offset != MaxSector &&
8101 rdev->recovery_offset) {
8102 seq_printf(seq, "\trecover=REMOTE");
8103 return 1;
8104 }
8105 if (mddev->reshape_position != MaxSector)
8106 seq_printf(seq, "\treshape=REMOTE");
8107 else
8108 seq_printf(seq, "\tresync=REMOTE");
8109 return 1;
8110 }
NeilBrownf7851be2015-07-02 17:12:58 +10008111 if (mddev->recovery_cp < MaxSector) {
8112 seq_printf(seq, "\tresync=PENDING");
8113 return 1;
8114 }
8115 return 0;
8116 }
8117 if (resync < 3) {
8118 seq_printf(seq, "\tresync=DELAYED");
8119 return 1;
8120 }
8121
NeilBrown403df472014-09-30 15:52:29 +10008122 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08008123 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10008124 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08008125 * u32, as those are the requirements for sector_div.
8126 * Thus 'scale' must be at least 10
8127 */
8128 scale = 10;
8129 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10008130 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08008131 scale++;
8132 }
8133 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10008134 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08008135
8136 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008137 {
NeilBrown4588b422006-03-27 01:18:04 -08008138 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008139 seq_printf(seq, "[");
8140 for (i = 0; i < x; i++)
8141 seq_printf(seq, "=");
8142 seq_printf(seq, ">");
8143 for (i = 0; i < y; i++)
8144 seq_printf(seq, ".");
8145 seq_printf(seq, "] ");
8146 }
NeilBrown4588b422006-03-27 01:18:04 -08008147 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08008148 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
8149 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07008150 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
8151 "check" :
8152 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
8153 "resync" : "recovery"))),
8154 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10008155 (unsigned long long) resync/2,
8156 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008157
8158 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07008159 * dt: time from mark until now
8160 * db: blocks written from mark until now
8161 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10008162 *
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008163 * rt is a sector_t, which is always 64bit now. We are keeping
8164 * the original algorithm, but it is not really necessary.
8165 *
8166 * Original algorithm:
8167 * So we divide before multiply in case it is 32bit and close
8168 * to the limit.
8169 * We scale the divisor (db) by 32 to avoid losing precision
8170 * near the end of resync when the number of remaining sectors
8171 * is close to 'db'.
8172 * We then divide rt by 32 after multiplying by db to compensate.
8173 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008174 */
8175 dt = ((jiffies - mddev->resync_mark) / HZ);
8176 if (!dt) dt++;
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008177
8178 curr_mark_cnt = mddev->curr_mark_cnt;
8179 recovery_active = atomic_read(&mddev->recovery_active);
8180 resync_mark_cnt = mddev->resync_mark_cnt;
8181
8182 if (curr_mark_cnt >= (recovery_active + resync_mark_cnt))
8183 db = curr_mark_cnt - (recovery_active + resync_mark_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008184
NeilBrowndd71cf62009-05-07 12:49:35 +10008185 rt = max_sectors - resync; /* number of remaining sectors */
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008186 rt = div64_u64(rt, db/32+1);
NeilBrowndd71cf62009-05-07 12:49:35 +10008187 rt *= dt;
8188 rt >>= 5;
8189
8190 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
8191 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192
NeilBrownff4e8d92006-07-10 04:44:16 -07008193 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10008194 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195}
8196
8197static void *md_seq_start(struct seq_file *seq, loff_t *pos)
8198{
8199 struct list_head *tmp;
8200 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11008201 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008202
8203 if (l >= 0x10000)
8204 return NULL;
8205 if (!l--)
8206 /* header */
8207 return (void*)1;
8208
8209 spin_lock(&all_mddevs_lock);
8210 list_for_each(tmp,&all_mddevs)
8211 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11008212 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008213 mddev_get(mddev);
8214 spin_unlock(&all_mddevs_lock);
8215 return mddev;
8216 }
8217 spin_unlock(&all_mddevs_lock);
8218 if (!l--)
8219 return (void*)2;/* tail */
8220 return NULL;
8221}
8222
8223static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
8224{
8225 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008226 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10008227
Linus Torvalds1da177e2005-04-16 15:20:36 -07008228 ++*pos;
8229 if (v == (void*)2)
8230 return NULL;
8231
8232 spin_lock(&all_mddevs_lock);
8233 if (v == (void*)1)
8234 tmp = all_mddevs.next;
8235 else
8236 tmp = mddev->all_mddevs.next;
8237 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11008238 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008239 else {
8240 next_mddev = (void*)2;
8241 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10008242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008243 spin_unlock(&all_mddevs_lock);
8244
8245 if (v != (void*)1)
8246 mddev_put(mddev);
8247 return next_mddev;
8248
8249}
8250
8251static void md_seq_stop(struct seq_file *seq, void *v)
8252{
NeilBrownfd01b882011-10-11 16:47:53 +11008253 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008254
8255 if (mddev && v != (void*)1 && v != (void*)2)
8256 mddev_put(mddev);
8257}
8258
8259static int md_seq_show(struct seq_file *seq, void *v)
8260{
NeilBrownfd01b882011-10-11 16:47:53 +11008261 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11008262 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11008263 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264
8265 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11008266 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008267 seq_printf(seq, "Personalities : ");
8268 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008269 list_for_each_entry(pers, &pers_list, list)
8270 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008271
8272 spin_unlock(&pers_lock);
8273 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02008274 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008275 return 0;
8276 }
8277 if (v == (void*)2) {
8278 status_unused(seq);
8279 return 0;
8280 }
8281
NeilBrown36d091f2014-12-15 12:56:58 +11008282 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008283 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
8284 seq_printf(seq, "%s : %sactive", mdname(mddev),
8285 mddev->pers ? "" : "in");
8286 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08008287 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008288 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08008289 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07008290 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008291 seq_printf(seq, " %s", mddev->pers->name);
8292 }
8293
Andre Nolldd8ac332009-03-31 14:33:13 +11008294 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11008295 rcu_read_lock();
8296 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008297 char b[BDEVNAME_SIZE];
8298 seq_printf(seq, " %s[%d]",
8299 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07008300 if (test_bit(WriteMostly, &rdev->flags))
8301 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07008302 if (test_bit(Journal, &rdev->flags))
8303 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08008304 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008305 seq_printf(seq, "(F)");
8306 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11008307 }
8308 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07008309 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11008310 if (test_bit(Replacement, &rdev->flags))
8311 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11008312 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313 }
NeilBrownf97fcad2014-12-15 12:56:59 +11008314 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008315
8316 if (!list_empty(&mddev->disks)) {
8317 if (mddev->pers)
8318 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10008319 (unsigned long long)
8320 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008321 else
8322 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11008323 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07008325 if (mddev->persistent) {
8326 if (mddev->major_version != 0 ||
8327 mddev->minor_version != 90) {
8328 seq_printf(seq," super %d.%d",
8329 mddev->major_version,
8330 mddev->minor_version);
8331 }
NeilBrowne6910632008-02-06 01:39:51 -08008332 } else if (mddev->external)
8333 seq_printf(seq, " super external:%s",
8334 mddev->metadata_type);
8335 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07008336 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008337
8338 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11008339 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10008340 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08008341 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10008342 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08008343 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08008344 }
NeilBrown32a76272005-06-21 17:17:14 -07008345 } else
8346 seq_printf(seq, "\n ");
8347
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008348 md_bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008349
8350 seq_printf(seq, "\n");
8351 }
NeilBrown36d091f2014-12-15 12:56:58 +11008352 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10008353
Linus Torvalds1da177e2005-04-16 15:20:36 -07008354 return 0;
8355}
8356
Jan Engelhardt110518b2009-05-07 12:49:37 +10008357static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008358 .start = md_seq_start,
8359 .next = md_seq_next,
8360 .stop = md_seq_stop,
8361 .show = md_seq_show,
8362};
8363
8364static int md_seq_open(struct inode *inode, struct file *file)
8365{
Kay Sieversf1514632011-07-12 20:48:39 +02008366 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008367 int error;
8368
8369 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08008370 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02008371 return error;
8372
8373 seq = file->private_data;
8374 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008375 return error;
8376}
8377
NeilBrowne2f23b62014-04-09 14:33:51 +10008378static int md_unloading;
Al Viroafc9a422017-07-03 06:39:46 -04008379static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
NeilBrownd7603b72006-01-06 00:20:30 -08008380{
Kay Sieversf1514632011-07-12 20:48:39 +02008381 struct seq_file *seq = filp->private_data;
Al Viroafc9a422017-07-03 06:39:46 -04008382 __poll_t mask;
NeilBrownd7603b72006-01-06 00:20:30 -08008383
NeilBrowne2f23b62014-04-09 14:33:51 +10008384 if (md_unloading)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008385 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08008386 poll_wait(filp, &md_event_waiters, wait);
8387
8388 /* always allow read */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008389 mask = EPOLLIN | EPOLLRDNORM;
NeilBrownd7603b72006-01-06 00:20:30 -08008390
Kay Sieversf1514632011-07-12 20:48:39 +02008391 if (seq->poll_event != atomic_read(&md_event_count))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008392 mask |= EPOLLERR | EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08008393 return mask;
8394}
8395
Alexey Dobriyan97a32532020-02-03 17:37:17 -08008396static const struct proc_ops mdstat_proc_ops = {
8397 .proc_open = md_seq_open,
8398 .proc_read = seq_read,
8399 .proc_lseek = seq_lseek,
8400 .proc_release = seq_release,
8401 .proc_poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402};
8403
NeilBrown84fc4b52011-10-11 16:49:58 +11008404int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008405{
NeilBrown9d487392016-11-02 14:16:49 +11008406 pr_debug("md: %s personality registered for level %d\n",
8407 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008409 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008410 spin_unlock(&pers_lock);
8411 return 0;
8412}
NeilBrown6c144d32014-09-30 16:15:38 +10008413EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414
NeilBrown84fc4b52011-10-11 16:49:58 +11008415int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008416{
NeilBrown9d487392016-11-02 14:16:49 +11008417 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008418 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008419 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008420 spin_unlock(&pers_lock);
8421 return 0;
8422}
NeilBrown6c144d32014-09-30 16:15:38 +10008423EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008424
NeilBrown6022e752015-08-13 12:32:55 +10008425int register_md_cluster_operations(struct md_cluster_operations *ops,
8426 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008427{
NeilBrown6022e752015-08-13 12:32:55 +10008428 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008429 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008430 if (md_cluster_ops != NULL)
8431 ret = -EALREADY;
8432 else {
8433 md_cluster_ops = ops;
8434 md_cluster_mod = module;
8435 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008436 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008437 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008438}
8439EXPORT_SYMBOL(register_md_cluster_operations);
8440
8441int unregister_md_cluster_operations(void)
8442{
8443 spin_lock(&pers_lock);
8444 md_cluster_ops = NULL;
8445 spin_unlock(&pers_lock);
8446 return 0;
8447}
8448EXPORT_SYMBOL(unregister_md_cluster_operations);
8449
8450int md_setup_cluster(struct mddev *mddev, int nodes)
8451{
Zhao Heming7c9d5c52020-07-21 02:08:52 +08008452 int ret;
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008453 if (!md_cluster_ops)
8454 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008455 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008456 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008457 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11008458 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008459 spin_unlock(&pers_lock);
8460 return -ENOENT;
8461 }
8462 spin_unlock(&pers_lock);
8463
Zhao Heming7c9d5c52020-07-21 02:08:52 +08008464 ret = md_cluster_ops->join(mddev, nodes);
8465 if (!ret)
8466 mddev->safemode_delay = 0;
8467 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008468}
8469
8470void md_cluster_stop(struct mddev *mddev)
8471{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05008472 if (!md_cluster_ops)
8473 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008474 md_cluster_ops->leave(mddev);
8475 module_put(md_cluster_mod);
8476}
8477
NeilBrownfd01b882011-10-11 16:47:53 +11008478static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008479{
NeilBrownf72ffdd2014-09-30 14:23:59 +10008480 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11008482 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008483
8484 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10008485 rcu_read_lock();
8486 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008487 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Michael Callahan59767fb2018-07-18 04:47:37 -07008488 curr_events = (int)part_stat_read_accum(&disk->part0, sectors) -
NeilBrowneea1bf32009-03-31 14:27:02 +11008489 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07008490 /* sync IO will cause sync_io to increase before the disk_stats
8491 * as sync_io is counted when a request starts, and
8492 * disk_stats is counted when it completes.
8493 * So resync activity will cause curr_events to be smaller than
8494 * when there was no such activity.
8495 * non-sync IO will cause disk_stat to increase without
8496 * increasing sync_io so curr_events will (eventually)
8497 * be larger than it was before. Once it becomes
8498 * substantially larger, the test below will cause
8499 * the array to appear non-idle, and resync will slow
8500 * down.
8501 * If there is a lot of outstanding resync activity when
8502 * we set last_event to curr_events, then all that activity
8503 * completing might cause the array to appear non-idle
8504 * and resync will be slowed down even though there might
8505 * not have been non-resync activity. This will only
8506 * happen once though. 'last_events' will soon reflect
8507 * the state where there is little or no outstanding
8508 * resync requests, and further resync activity will
8509 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08008510 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008511 */
NeilBrowneea1bf32009-03-31 14:27:02 +11008512 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513 rdev->last_events = curr_events;
8514 idle = 0;
8515 }
8516 }
NeilBrown4b809912008-07-21 17:05:25 +10008517 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008518 return idle;
8519}
8520
NeilBrownfd01b882011-10-11 16:47:53 +11008521void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008522{
8523 /* another "blocks" (512byte) blocks have been synced */
8524 atomic_sub(blocks, &mddev->recovery_active);
8525 wake_up(&mddev->recovery_wait);
8526 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07008527 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08008528 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008529 md_wakeup_thread(mddev->thread);
8530 // stop recovery, signal do_sync ....
8531 }
8532}
NeilBrown6c144d32014-09-30 16:15:38 +10008533EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534
NeilBrown06d91a52005-06-21 17:17:12 -07008535/* md_write_start(mddev, bi)
8536 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07008537 * in superblock) before writing, schedule a superblock update
8538 * and wait for it to complete.
NeilBrowncc27b0c2017-06-05 16:49:39 +10008539 * A return value of 'false' means that the write wasn't recorded
8540 * and cannot proceed as the array is being suspend.
NeilBrown06d91a52005-06-21 17:17:12 -07008541 */
NeilBrowncc27b0c2017-06-05 16:49:39 +10008542bool md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008543{
Neil Brown0fd62b82008-06-28 08:31:36 +10008544 int did_change = 0;
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008545
NeilBrown06d91a52005-06-21 17:17:12 -07008546 if (bio_data_dir(bi) != WRITE)
NeilBrowncc27b0c2017-06-05 16:49:39 +10008547 return true;
NeilBrown06d91a52005-06-21 17:17:12 -07008548
NeilBrownf91de922005-11-08 21:39:36 -08008549 BUG_ON(mddev->ro == 1);
8550 if (mddev->ro == 2) {
8551 /* need to switch to read/write */
8552 mddev->ro = 0;
8553 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8554 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08008555 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008556 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08008557 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008558 rcu_read_lock();
8559 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11008560 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07008561 if (mddev->safemode == 1)
8562 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11008563 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
NeilBrown81fe48e2017-08-08 16:56:36 +10008564 if (mddev->in_sync || mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11008565 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07008566 if (mddev->in_sync) {
8567 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008568 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8569 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07008570 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008571 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07008572 }
NeilBrown85572d72014-12-15 12:56:56 +11008573 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07008574 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008575 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10008576 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008577 sysfs_notify_dirent_safe(mddev->sysfs_state);
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008578 if (!mddev->has_superblocks)
8579 return true;
NeilBrown09a44cc2008-05-23 13:04:36 -07008580 wait_event(mddev->sb_wait,
NeilBrownd47c8ad2017-10-05 16:23:16 +11008581 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8582 mddev->suspended);
NeilBrowncc27b0c2017-06-05 16:49:39 +10008583 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8584 percpu_ref_put(&mddev->writes_pending);
8585 return false;
8586 }
8587 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008588}
NeilBrown6c144d32014-09-30 16:15:38 +10008589EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590
NeilBrown49728052017-03-15 14:05:12 +11008591/* md_write_inc can only be called when md_write_start() has
8592 * already been called at least once of the current request.
8593 * It increments the counter and is useful when a single request
8594 * is split into several parts. Each part causes an increment and
8595 * so needs a matching md_write_end().
8596 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8597 * a spinlocked region.
8598 */
8599void md_write_inc(struct mddev *mddev, struct bio *bi)
8600{
8601 if (bio_data_dir(bi) != WRITE)
8602 return;
8603 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11008604 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008605}
8606EXPORT_SYMBOL(md_write_inc);
8607
NeilBrownfd01b882011-10-11 16:47:53 +11008608void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008609{
NeilBrown4ad23a972017-03-15 14:05:14 +11008610 percpu_ref_put(&mddev->writes_pending);
8611
8612 if (mddev->safemode == 2)
8613 md_wakeup_thread(mddev->thread);
8614 else if (mddev->safemode_delay)
8615 /* The roundup() ensures this only performs locking once
8616 * every ->safemode_delay jiffies
8617 */
8618 mod_timer(&mddev->safemode_timer,
8619 roundup(jiffies, mddev->safemode_delay) +
8620 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008621}
NeilBrown4ad23a972017-03-15 14:05:14 +11008622
NeilBrown6c144d32014-09-30 16:15:38 +10008623EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008624
NeilBrown2a2275d2007-01-26 00:57:11 -08008625/* md_allow_write(mddev)
8626 * Calling this ensures that the array is marked 'active' so that writes
8627 * may proceed without blocking. It is important to call this before
8628 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8629 * Must be called with mddev_lock held.
8630 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008631void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008632{
8633 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008634 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008635 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008636 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008637 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008638 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008639
NeilBrown85572d72014-12-15 12:56:56 +11008640 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008641 if (mddev->in_sync) {
8642 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008643 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8644 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008645 if (mddev->safemode_delay &&
8646 mddev->safemode == 0)
8647 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008648 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008649 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008650 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008651 /* wait for the dirty state to be recorded in the metadata */
8652 wait_event(mddev->sb_wait,
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008653 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008654 } else
NeilBrown85572d72014-12-15 12:56:56 +11008655 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008656}
8657EXPORT_SYMBOL_GPL(md_allow_write);
8658
Linus Torvalds1da177e2005-04-16 15:20:36 -07008659#define SYNC_MARKS 10
8660#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008661#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008662void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008663{
Shaohua Li4ed87312012-10-11 13:34:00 +11008664 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008665 struct mddev *mddev2;
Yufen Yue5b521e2019-06-14 15:41:07 -07008666 unsigned int currspeed = 0, window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008667 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008668 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008669 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008670 sector_t mark_cnt[SYNC_MARKS];
8671 int last_mark,m;
8672 struct list_head *tmp;
8673 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008674 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008675 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008676 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008677 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008678 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008679
8680 /* just incase thread restarts... */
Song Liud5d885f2017-11-19 22:17:01 -08008681 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8682 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008683 return;
NeilBrown3991b312014-05-28 13:39:23 +10008684 if (mddev->ro) {/* never try to sync a read-only array */
8685 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008686 return;
NeilBrown3991b312014-05-28 13:39:23 +10008687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008688
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008689 if (mddev_is_clustered(mddev)) {
8690 ret = md_cluster_ops->resync_start(mddev);
8691 if (ret)
8692 goto skip;
8693
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008694 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008695 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8696 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8697 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8698 && ((unsigned long long)mddev->curr_resync_completed
8699 < (unsigned long long)mddev->resync_max_sectors))
8700 goto skip;
8701 }
8702
NeilBrown61df9d92006-10-03 01:15:57 -07008703 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008704 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008705 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008706 action = "check";
8707 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008708 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008709 action = "repair";
8710 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008711 desc = "resync";
8712 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8713 desc = "reshape";
8714 else
8715 desc = "recovery";
8716
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008717 mddev->last_sync_action = action ?: desc;
8718
Linus Torvalds1da177e2005-04-16 15:20:36 -07008719 /* we overload curr_resync somewhat here.
8720 * 0 == not engaged in resync at all
8721 * 2 == checking that there is no conflict with another sync
8722 * 1 == like 2, but have yielded to allow conflicting resync to
Yufen Yue5b521e2019-06-14 15:41:07 -07008723 * commence
Linus Torvalds1da177e2005-04-16 15:20:36 -07008724 * other == active in resync - this many blocks
8725 *
8726 * Before starting a resync we must have set curr_resync to
8727 * 2, and then checked that every "conflicting" array has curr_resync
8728 * less than ours. When we find one that is the same or higher
8729 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8730 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8731 * This will mean we have to start checking from the beginning again.
8732 *
8733 */
8734
8735 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008736 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008737 mddev->curr_resync = 2;
8738
8739 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008740 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008741 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008742 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008743 if (mddev2 == mddev)
8744 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008745 if (!mddev->parallel_resync
8746 && mddev2->curr_resync
8747 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008748 DEFINE_WAIT(wq);
8749 if (mddev < mddev2 && mddev->curr_resync == 2) {
8750 /* arbitrarily yield */
8751 mddev->curr_resync = 1;
8752 wake_up(&resync_wait);
8753 }
8754 if (mddev > mddev2 && mddev->curr_resync == 1)
8755 /* no need to wait here, we can wait the next
8756 * time 'round when curr_resync == 2
8757 */
8758 continue;
NeilBrown97441972008-09-19 11:49:54 +10008759 /* We need to wait 'interruptible' so as not to
8760 * contribute to the load average, and not to
8761 * be caught by 'softlockup'
8762 */
8763 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008764 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008765 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008766 if (mddev2_minor != mddev2->md_minor) {
8767 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008768 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8769 desc, mdname(mddev),
8770 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008772 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008773 if (signal_pending(current))
8774 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008775 schedule();
8776 finish_wait(&resync_wait, &wq);
8777 goto try_again;
8778 }
8779 finish_wait(&resync_wait, &wq);
8780 }
8781 }
8782 } while (mddev->curr_resync < 2);
8783
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008784 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008785 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008786 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008787 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008788 */
8789 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008790 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008791 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008792 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8793 j = mddev->resync_min;
8794 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008795 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008796
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008797 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
NeilBrownc804cde2012-05-21 09:28:33 +10008798 max_sectors = mddev->resync_max_sectors;
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008799 /*
8800 * If the original node aborts reshaping then we continue the
8801 * reshaping, so set j again to avoid restart reshape from the
8802 * first beginning
8803 */
8804 if (mddev_is_clustered(mddev) &&
8805 mddev->reshape_position != MaxSector)
8806 j = mddev->reshape_position;
8807 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008808 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008809 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008810 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008811 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008812 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008813 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008814 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008815 !test_bit(Faulty, &rdev->flags) &&
8816 !test_bit(In_sync, &rdev->flags) &&
8817 rdev->recovery_offset < j)
8818 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008819 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008820
8821 /* If there is a bitmap, we need to make sure all
8822 * writes that started before we added a spare
8823 * complete before we start doing a recovery.
8824 * Otherwise the write might complete and (via
8825 * bitmap_endwrite) set a bit in the bitmap after the
8826 * recovery has checked that bit and skipped that
8827 * region.
8828 */
8829 if (mddev->bitmap) {
8830 mddev->pers->quiesce(mddev, 1);
8831 mddev->pers->quiesce(mddev, 0);
8832 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008834
NeilBrown9d487392016-11-02 14:16:49 +11008835 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8836 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8837 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8838 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008839
NeilBrowneea1bf32009-03-31 14:27:02 +11008840 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008841
NeilBrown57afd892005-06-21 17:17:13 -07008842 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008843 for (m = 0; m < SYNC_MARKS; m++) {
8844 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008845 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008846 }
8847 last_mark = 0;
8848 mddev->resync_mark = mark[last_mark];
8849 mddev->resync_mark_cnt = mark_cnt[last_mark];
8850
8851 /*
8852 * Tune reconstruction:
8853 */
Yufen Yue5b521e2019-06-14 15:41:07 -07008854 window = 32 * (PAGE_SIZE / 512);
NeilBrown9d487392016-11-02 14:16:49 +11008855 pr_debug("md: using %dk window, over a total of %lluk.\n",
8856 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008857
8858 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008859 last_check = 0;
8860
8861 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008862 pr_debug("md: resuming %s of %s from checkpoint.\n",
8863 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008864 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008865 } else
8866 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008867 mddev->curr_resync_completed = j;
Junxiao Bie1a86db2020-07-14 16:10:26 -07008868 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown72f36d52012-10-11 14:25:57 +11008869 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008870 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008871
majianpeng7c2c57c2012-07-03 12:12:26 +10008872 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008873 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008874 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008875
NeilBrown57afd892005-06-21 17:17:13 -07008876 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008877
NeilBrown7a91ee12009-05-26 12:57:21 +10008878 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8879 ((mddev->curr_resync > mddev->curr_resync_completed &&
8880 (mddev->curr_resync - mddev->curr_resync_completed)
8881 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008882 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008883 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008884 >= mddev->resync_max - mddev->curr_resync_completed ||
8885 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008886 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008887 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008888 wait_event(mddev->recovery_wait,
8889 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008890 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008891 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8892 j > mddev->recovery_cp)
8893 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008894 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008895 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
Junxiao Bie1a86db2020-07-14 16:10:26 -07008896 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown97e4f422009-03-31 14:33:13 +11008897 }
NeilBrownacb180b2009-04-14 16:28:34 +10008898
NeilBrownc91abf52013-11-19 12:02:01 +11008899 while (j >= mddev->resync_max &&
8900 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008901 /* As this condition is controlled by user-space,
8902 * we can block indefinitely, so use '_interruptible'
8903 * to avoid triggering warnings.
8904 */
8905 flush_signals(current); /* just in case */
8906 wait_event_interruptible(mddev->recovery_wait,
8907 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008908 || test_bit(MD_RECOVERY_INTR,
8909 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008910 }
NeilBrownacb180b2009-04-14 16:28:34 +10008911
NeilBrownc91abf52013-11-19 12:02:01 +11008912 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8913 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008914
NeilBrown09314792015-02-19 16:04:40 +11008915 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008916 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008917 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008918 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008919 }
NeilBrown57afd892005-06-21 17:17:13 -07008920
8921 if (!skipped) { /* actual IO requested */
8922 io_sectors += sectors;
8923 atomic_add(sectors, &mddev->recovery_active);
8924 }
8925
NeilBrowne875ece2011-07-28 11:39:24 +10008926 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8927 break;
8928
Linus Torvalds1da177e2005-04-16 15:20:36 -07008929 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008930 if (j > max_sectors)
8931 /* when skipping, extra large numbers can be returned. */
8932 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008933 if (j > 2)
8934 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008935 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008936 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008937 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008938 * visible in /proc/mdstat
8939 */
8940 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008941
8942 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008943 continue;
8944
NeilBrown57afd892005-06-21 17:17:13 -07008945 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008946 repeat:
8947 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8948 /* step marks */
8949 int next = (last_mark+1) % SYNC_MARKS;
8950
8951 mddev->resync_mark = mark[next];
8952 mddev->resync_mark_cnt = mark_cnt[next];
8953 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008954 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008955 last_mark = next;
8956 }
8957
NeilBrownc91abf52013-11-19 12:02:01 +11008958 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8959 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008960
8961 /*
8962 * this loop exits only if either when we are slower than
8963 * the 'hard' speed limit, or the system was IO-idle for
8964 * a jiffy.
8965 * the system might be non-idle CPU-wise, but we only care
8966 * about not overloading the IO subsystem. (things like an
8967 * e2fsck being done on the RAID array should execute fast)
8968 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008969 cond_resched();
8970
Xiao Niac7e50a2014-08-07 09:37:41 -04008971 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8972 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008973 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008974
NeilBrown88202a02006-01-06 00:21:36 -08008975 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008976 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008977 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008978 goto repeat;
8979 }
NeilBrownac8fa412015-02-19 16:55:00 +11008980 if (!is_mddev_idle(mddev, 0)) {
8981 /*
8982 * Give other IO more of a chance.
8983 * The faster the devices, the less we wait.
8984 */
8985 wait_event(mddev->recovery_wait,
8986 !atomic_read(&mddev->recovery_active));
8987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008988 }
8989 }
NeilBrown9d487392016-11-02 14:16:49 +11008990 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8991 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8992 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008993 /*
8994 * this also signals 'finished resyncing' to md_stop
8995 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008996 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008997 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8998
NeilBrown5ed1df22015-07-24 13:27:08 +10008999 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9000 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11009001 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10009002 mddev->curr_resync_completed = mddev->curr_resync;
Junxiao Bie1a86db2020-07-14 16:10:26 -07009003 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown5ed1df22015-07-24 13:27:08 +10009004 }
NeilBrown09314792015-02-19 16:04:40 +11009005 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009006
NeilBrowndfc70642008-05-23 13:04:39 -07009007 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11009008 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07009009 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
9010 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
9011 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11009012 pr_debug("md: checkpointing %s of %s.\n",
9013 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08009014 if (test_bit(MD_RECOVERY_ERROR,
9015 &mddev->recovery))
9016 mddev->recovery_cp =
9017 mddev->curr_resync_completed;
9018 else
9019 mddev->recovery_cp =
9020 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07009021 }
9022 } else
9023 mddev->recovery_cp = MaxSector;
9024 } else {
9025 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
9026 mddev->curr_resync = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11009027 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9028 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
9029 rcu_read_lock();
9030 rdev_for_each_rcu(rdev, mddev)
9031 if (rdev->raid_disk >= 0 &&
9032 mddev->delta_disks >= 0 &&
9033 !test_bit(Journal, &rdev->flags) &&
9034 !test_bit(Faulty, &rdev->flags) &&
9035 !test_bit(In_sync, &rdev->flags) &&
9036 rdev->recovery_offset < mddev->curr_resync)
9037 rdev->recovery_offset = mddev->curr_resync;
9038 rcu_read_unlock();
9039 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07009040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009041 }
NeilBrowndb91ff52012-02-07 12:01:51 +11009042 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04009043 /* set CHANGE_PENDING here since maybe another update is needed,
9044 * so other nodes are informed. It should be harmless for normal
9045 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08009046 set_mask_bits(&mddev->sb_flags, 0,
9047 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05009048
BingJing Chang88763912018-02-22 13:34:46 +08009049 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
9050 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
9051 mddev->delta_disks > 0 &&
9052 mddev->pers->finish_reshape &&
9053 mddev->pers->size &&
9054 mddev->queue) {
9055 mddev_lock_nointr(mddev);
9056 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
9057 mddev_unlock(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009058 if (!mddev_is_clustered(mddev)) {
9059 set_capacity(mddev->gendisk, mddev->array_sectors);
9060 revalidate_disk(mddev->gendisk);
9061 }
BingJing Chang88763912018-02-22 13:34:46 +08009062 }
9063
NeilBrown23da4222014-12-15 12:57:01 +11009064 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11009065 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
9066 /* We completed so min/max setting can be forgotten if used. */
9067 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
9068 mddev->resync_min = 0;
9069 mddev->resync_max = MaxSector;
9070 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
9071 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10009072 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009073 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11009074 spin_unlock(&mddev->lock);
9075
Linus Torvalds1da177e2005-04-16 15:20:36 -07009076 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009077 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08009078 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009079}
NeilBrown29269552006-03-27 01:18:10 -08009080EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009081
NeilBrown746d3202013-04-24 11:42:41 +10009082static int remove_and_add_spares(struct mddev *mddev,
9083 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009084{
NeilBrown3cb03002011-10-11 16:45:26 +11009085 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009086 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11009087 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10009088 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009089
NeilBrown39772f02018-02-03 09:19:30 +11009090 if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
9091 /* Mustn't remove devices when resync thread is running */
9092 return 0;
9093
NeilBrownd787be42016-06-02 16:19:53 +10009094 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10009095 if ((this == NULL || rdev == this) &&
9096 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07009097 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10009098 test_bit(Faulty, &rdev->flags) &&
9099 atomic_read(&rdev->nr_pending)==0) {
9100 /* Faulty non-Blocked devices with nr_pending == 0
9101 * never get nr_pending incremented,
9102 * never get Faulty cleared, and never get Blocked set.
9103 * So we can synchronize_rcu now rather than once per device
9104 */
9105 remove_some = true;
9106 set_bit(RemoveSynchronized, &rdev->flags);
9107 }
9108 }
9109
9110 if (remove_some)
9111 synchronize_rcu();
9112 rdev_for_each(rdev, mddev) {
9113 if ((this == NULL || rdev == this) &&
9114 rdev->raid_disk >= 0 &&
9115 !test_bit(Blocked, &rdev->flags) &&
9116 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07009117 (!test_bit(In_sync, &rdev->flags) &&
9118 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10009119 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009120 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11009121 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10009122 sysfs_unlink_rdev(mddev, rdev);
NeilBrown011abdc2018-04-26 14:46:29 +10009123 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009124 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11009125 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009126 }
9127 }
NeilBrownd787be42016-06-02 16:19:53 +10009128 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
9129 clear_bit(RemoveSynchronized, &rdev->flags);
9130 }
9131
Jonathan Brassow90584fc2013-03-07 16:24:26 -06009132 if (removed && mddev->kobj.sd)
Junxiao Bie1a86db2020-07-14 16:10:26 -07009133 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009134
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05009135 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10009136 goto no_add;
9137
NeilBrowndafb20f2012-03-19 12:46:39 +11009138 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05009139 if (this && this != rdev)
9140 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009141 if (test_bit(Candidate, &rdev->flags))
9142 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11009143 if (rdev->raid_disk >= 0 &&
9144 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07009145 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11009146 !test_bit(Faulty, &rdev->flags))
9147 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009148 if (rdev->raid_disk >= 0)
9149 continue;
9150 if (test_bit(Faulty, &rdev->flags))
9151 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009152 if (!test_bit(Journal, &rdev->flags)) {
9153 if (mddev->ro &&
9154 ! (rdev->saved_raid_disk >= 0 &&
9155 !test_bit(Bitmap_sync, &rdev->flags)))
9156 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009157
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009158 rdev->recovery_offset = 0;
9159 }
Guoqing Jiang3f79cc22020-04-04 23:57:11 +02009160 if (mddev->pers->hot_add_disk(mddev, rdev) == 0) {
Damien Le Moal5e3b8a82020-07-16 13:54:40 +09009161 /* failure here is OK */
9162 sysfs_link_rdev(mddev, rdev);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009163 if (!test_bit(Journal, &rdev->flags))
9164 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009165 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08009166 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07009167 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009168 }
NeilBrown746d3202013-04-24 11:42:41 +10009169no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10009170 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08009171 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009172 return spares;
9173}
NeilBrown7ebc0be2011-01-14 09:14:33 +11009174
NeilBrownac05f252014-09-30 08:10:42 +10009175static void md_start_sync(struct work_struct *ws)
9176{
9177 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05009178
NeilBrownac05f252014-09-30 08:10:42 +10009179 mddev->sync_thread = md_register_thread(md_do_sync,
9180 mddev,
9181 "resync");
9182 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11009183 pr_warn("%s: could not start resync thread...\n",
9184 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10009185 /* leave the spares where they are, it shouldn't hurt */
9186 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9187 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9188 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9189 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9190 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11009191 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10009192 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9193 &mddev->recovery))
9194 if (mddev->sysfs_action)
9195 sysfs_notify_dirent_safe(mddev->sysfs_action);
9196 } else
9197 md_wakeup_thread(mddev->sync_thread);
9198 sysfs_notify_dirent_safe(mddev->sysfs_action);
9199 md_new_event(mddev);
9200}
9201
Linus Torvalds1da177e2005-04-16 15:20:36 -07009202/*
9203 * This routine is regularly called by all per-raid-array threads to
9204 * deal with generic issues like resync and super-block update.
9205 * Raid personalities that don't have a thread (linear/raid0) do not
9206 * need this as they never do any recovery or update the superblock.
9207 *
9208 * It does not do any resync itself, but rather "forks" off other threads
9209 * to do that as needed.
9210 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
9211 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07009212 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07009213 * and wakeups up this thread which will reap the thread and finish up.
9214 * This thread also removes any faulty devices (with nr_pending == 0).
9215 *
9216 * The overall approach is:
9217 * 1/ if the superblock needs updating, update it.
9218 * 2/ If a recovery thread is running, don't do anything else.
9219 * 3/ If recovery has finished, clean up, possibly marking spares active.
9220 * 4/ If there are any faulty devices, remove them.
9221 * 5/ If array is degraded, try to add spares devices
9222 * 6/ If array has spares or is not in-sync, start a resync thread.
9223 */
NeilBrownfd01b882011-10-11 16:47:53 +11009224void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009225{
NeilBrown059421e2018-10-03 15:04:41 +10009226 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
9227 /* Write superblock - thread that called mddev_suspend()
9228 * holds reconfig_mutex for us.
9229 */
9230 set_bit(MD_UPDATING_SB, &mddev->flags);
9231 smp_mb__after_atomic();
9232 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
9233 md_update_sb(mddev, 0);
9234 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
9235 wake_up(&mddev->sb_wait);
9236 }
9237
Jonathan Brassow68866e42011-06-08 15:10:08 +10009238 if (mddev->suspended)
9239 return;
9240
NeilBrown5f404022005-06-21 17:17:16 -07009241 if (mddev->bitmap)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009242 md_bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009243
NeilBrownfca4d842005-06-21 17:17:11 -07009244 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07009245 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11009246 pr_debug("md: %s in immediate safe mode\n",
9247 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07009248 mddev->safemode = 2;
9249 }
9250 flush_signals(current);
9251 }
9252
NeilBrownc89a8ee2008-08-05 15:54:13 +10009253 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
9254 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009255 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08009256 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07009257 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07009258 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07009259 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11009260 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07009261 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009262 ))
9263 return;
NeilBrownfca4d842005-06-21 17:17:11 -07009264
NeilBrowndf5b89b2006-03-27 01:18:20 -08009265 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009266 int spares = 0;
NeilBrown480523f2019-08-20 10:21:09 +10009267 bool try_set_sync = mddev->safemode != 0;
NeilBrownfca4d842005-06-21 17:17:11 -07009268
Shaohua Liafc1f552017-08-11 20:34:45 -07009269 if (!mddev->external && mddev->safemode == 1)
NeilBrown33182d12017-08-08 16:56:36 +10009270 mddev->safemode = 0;
9271
NeilBrownc89a8ee2008-08-05 15:54:13 +10009272 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10009273 struct md_rdev *rdev;
9274 if (!mddev->external && mddev->in_sync)
9275 /* 'Blocked' flag not needed as failed devices
9276 * will be recorded if array switched to read/write.
9277 * Leaving it set will prevent the device
9278 * from being removed.
9279 */
9280 rdev_for_each(rdev, mddev)
9281 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10009282 /* On a read-only array we can:
9283 * - remove failed devices
9284 * - add already-in_sync devices if the array itself
9285 * is in-sync.
9286 * As we only add devices that are already in-sync,
9287 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10009288 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10009289 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11009290 /* There is no thread, but we need to call
9291 * ->spare_active and clear saved_raid_disk
9292 */
NeilBrown2ac295a2014-05-29 11:40:03 +10009293 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11009294 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10009295 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11009296 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08009297 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10009298 goto unlock;
9299 }
9300
Guoqing Jiang659b2542015-12-21 10:50:59 +11009301 if (mddev_is_clustered(mddev)) {
9302 struct md_rdev *rdev;
9303 /* kick the device if another node issued a
9304 * remove disk.
9305 */
9306 rdev_for_each(rdev, mddev) {
9307 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
9308 rdev->raid_disk < 0)
9309 md_kick_rdev_from_array(rdev);
9310 }
9311 }
9312
NeilBrown480523f2019-08-20 10:21:09 +10009313 if (try_set_sync && !mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11009314 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11009315 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11009316 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07009317 }
NeilBrownfca4d842005-06-21 17:17:11 -07009318
Shaohua Li29530792016-12-08 15:48:19 -08009319 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07009320 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07009321
Linus Torvalds1da177e2005-04-16 15:20:36 -07009322 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
9323 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
9324 /* resync/recovery still happening */
9325 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9326 goto unlock;
9327 }
9328 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009329 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330 goto unlock;
9331 }
Neil Brown72a23c22008-06-28 08:31:41 +10009332 /* Set RUNNING before clearing NEEDED to avoid
9333 * any transients in the value of "sync_action".
9334 */
NeilBrown72f36d52012-10-11 14:25:57 +11009335 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11009336 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10009337 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11009338 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08009339 /* Clear some bits that don't mean anything, but
9340 * might be left set
9341 */
NeilBrown24dd4692005-11-08 21:39:26 -08009342 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
9343 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344
NeilBrowned209582012-04-24 10:23:14 +10009345 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
9346 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10009347 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009348 /* no recovery is running.
9349 * remove any failed drives, then
9350 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11009351 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07009352 * the personality to fail the re-add.
9353 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009354
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009355 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10009356 if (mddev->pers->check_reshape == NULL ||
9357 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009358 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10009359 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009360 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009361 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10009362 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08009363 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9364 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07009365 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009366 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08009367 } else if (mddev->recovery_cp < MaxSector) {
9368 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009369 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08009370 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
9371 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10009372 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08009373
Linus Torvalds1da177e2005-04-16 15:20:36 -07009374 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10009375 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07009376 /* We are adding a device or devices to an array
9377 * which has the bitmap stored on all devices.
9378 * So make sure all bitmap pages get written
9379 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009380 md_bitmap_write_all(mddev->bitmap);
NeilBrowna654b9d82005-06-21 17:17:27 -07009381 }
NeilBrownac05f252014-09-30 08:10:42 +10009382 INIT_WORK(&mddev->del_work, md_start_sync);
9383 queue_work(md_misc_wq, &mddev->del_work);
9384 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009385 }
NeilBrownac05f252014-09-30 08:10:42 +10009386 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10009387 if (!mddev->sync_thread) {
9388 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11009389 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10009390 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9391 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11009392 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10009393 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10009394 }
NeilBrownac05f252014-09-30 08:10:42 +10009395 unlock:
9396 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009397 mddev_unlock(mddev);
9398 }
9399}
NeilBrown6c144d32014-09-30 16:15:38 +10009400EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009401
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009402void md_reap_sync_thread(struct mddev *mddev)
9403{
9404 struct md_rdev *rdev;
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009405 sector_t old_dev_sectors = mddev->dev_sectors;
9406 bool is_reshaped = false;
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009407
9408 /* resync has finished, collect result */
9409 md_unregister_thread(&mddev->sync_thread);
9410 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
Guoqing Jiang0d8ed0e92019-07-24 11:09:21 +02009411 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
9412 mddev->degraded != mddev->raid_disks) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009413 /* success...*/
9414 /* activate any spares */
9415 if (mddev->pers->spare_active(mddev)) {
Junxiao Bie1a86db2020-07-14 16:10:26 -07009416 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
Shaohua Li29530792016-12-08 15:48:19 -08009417 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009418 }
9419 }
9420 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009421 mddev->pers->finish_reshape) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009422 mddev->pers->finish_reshape(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009423 if (mddev_is_clustered(mddev))
9424 is_reshaped = true;
9425 }
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009426
9427 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11009428 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009429 */
NeilBrownf4667222013-12-09 12:04:56 +11009430 if (!mddev->degraded)
9431 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009432 rdev->saved_raid_disk = -1;
9433
9434 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08009435 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04009436 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9437 * clustered raid */
9438 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
9439 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009440 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10009441 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009442 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9443 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9444 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9445 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009446 /*
9447 * We call md_cluster_ops->update_size here because sync_size could
9448 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9449 * so it is time to update size across cluster.
9450 */
9451 if (mddev_is_clustered(mddev) && is_reshaped
9452 && !test_bit(MD_CLOSING, &mddev->flags))
9453 md_cluster_ops->update_size(mddev, old_dev_sectors);
NeilBrownf851b602014-12-11 10:02:10 +11009454 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009455 /* flag recovery needed just to double check */
9456 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9457 sysfs_notify_dirent_safe(mddev->sysfs_action);
9458 md_new_event(mddev);
9459 if (mddev->event_work.func)
9460 queue_work(md_misc_wq, &mddev->event_work);
9461}
NeilBrown6c144d32014-09-30 16:15:38 +10009462EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009463
NeilBrownfd01b882011-10-11 16:47:53 +11009464void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07009465{
NeilBrown00bcb4a2010-06-01 19:37:23 +10009466 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07009467 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10009468 !test_bit(Blocked, &rdev->flags) &&
9469 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07009470 msecs_to_jiffies(5000));
9471 rdev_dec_pending(rdev, mddev);
9472}
9473EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9474
NeilBrownc6563a82012-05-21 09:27:00 +10009475void md_finish_reshape(struct mddev *mddev)
9476{
9477 /* called be personality module when reshape completes. */
9478 struct md_rdev *rdev;
9479
9480 rdev_for_each(rdev, mddev) {
9481 if (rdev->data_offset > rdev->new_data_offset)
9482 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9483 else
9484 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9485 rdev->data_offset = rdev->new_data_offset;
9486 }
9487}
9488EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10009489
Vishal Vermafc974ee2015-12-24 19:20:34 -07009490/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10009491
Vishal Vermafc974ee2015-12-24 19:20:34 -07009492/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11009493int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10009494 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009495{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04009496 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10009497 int rv;
9498 if (is_new)
9499 s += rdev->new_data_offset;
9500 else
9501 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07009502 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9503 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10009504 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009505 if (test_bit(ExternalBbl, &rdev->flags))
Junxiao Bie1a86db2020-07-14 16:10:26 -07009506 sysfs_notify_dirent_safe(rdev->sysfs_unack_badblocks);
NeilBrown8bd2f0a2011-12-08 16:26:08 +11009507 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08009508 set_mask_bits(&mddev->sb_flags, 0,
9509 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10009510 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07009511 return 1;
9512 } else
9513 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10009514}
9515EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9516
NeilBrownc6563a82012-05-21 09:27:00 +10009517int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9518 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009519{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009520 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10009521 if (is_new)
9522 s += rdev->new_data_offset;
9523 else
9524 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009525 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9526 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
Junxiao Bie1a86db2020-07-14 16:10:26 -07009527 sysfs_notify_dirent_safe(rdev->sysfs_badblocks);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009528 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10009529}
9530EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9531
Adrian Bunk75c96f82005-05-05 16:16:09 -07009532static int md_notify_reboot(struct notifier_block *this,
9533 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009534{
9535 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11009536 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009537 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009538
NeilBrownc744a652012-03-19 12:46:37 +11009539 for_each_mddev(mddev, tmp) {
9540 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10009541 if (mddev->pers)
9542 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009543 if (mddev->persistent)
9544 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009545 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009546 }
NeilBrownc744a652012-03-19 12:46:37 +11009547 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009548 }
NeilBrownc744a652012-03-19 12:46:37 +11009549 /*
9550 * certain more exotic SCSI devices are known to be
9551 * volatile wrt too early system reboots. While the
9552 * right place to handle this issue is the given
9553 * driver, we do want to have a safe RAID driver ...
9554 */
9555 if (need_delay)
9556 mdelay(1000*1);
9557
Linus Torvalds1da177e2005-04-16 15:20:36 -07009558 return NOTIFY_DONE;
9559}
9560
Adrian Bunk75c96f82005-05-05 16:16:09 -07009561static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009562 .notifier_call = md_notify_reboot,
9563 .next = NULL,
9564 .priority = INT_MAX, /* before any real devices */
9565};
9566
9567static void md_geninit(void)
9568{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009569 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009570
Alexey Dobriyan97a32532020-02-03 17:37:17 -08009571 proc_create("mdstat", S_IRUGO, NULL, &mdstat_proc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009572}
9573
Adrian Bunk75c96f82005-05-05 16:16:09 -07009574static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009575{
Tejun Heoe804ac72010-10-15 15:36:08 +02009576 int ret = -ENOMEM;
9577
Tejun Heoada609e2011-01-25 14:35:54 +01009578 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009579 if (!md_wq)
9580 goto err_wq;
9581
9582 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9583 if (!md_misc_wq)
9584 goto err_misc_wq;
9585
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009586 md_rdev_misc_wq = alloc_workqueue("md_rdev_misc", 0, 0);
9587 if (!md_misc_wq)
9588 goto err_rdev_misc_wq;
9589
Tejun Heoe804ac72010-10-15 15:36:08 +02009590 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9591 goto err_md;
9592
9593 if ((ret = register_blkdev(0, "mdp")) < 0)
9594 goto err_mdp;
9595 mdp_major = ret;
9596
NeilBrownaf5628f2014-07-31 13:54:54 +10009597 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009598 md_probe, NULL, NULL);
9599 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009600 md_probe, NULL, NULL);
9601
Linus Torvalds1da177e2005-04-16 15:20:36 -07009602 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009603 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009604
9605 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009607
Tejun Heoe804ac72010-10-15 15:36:08 +02009608err_mdp:
9609 unregister_blkdev(MD_MAJOR, "md");
9610err_md:
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009611 destroy_workqueue(md_rdev_misc_wq);
9612err_rdev_misc_wq:
Tejun Heoe804ac72010-10-15 15:36:08 +02009613 destroy_workqueue(md_misc_wq);
9614err_misc_wq:
9615 destroy_workqueue(md_wq);
9616err_wq:
9617 return ret;
9618}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009619
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009620static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009621{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009622 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9623 struct md_rdev *rdev2;
9624 int role, ret;
9625 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009626
Guoqing Jiang818da592017-03-01 16:42:40 +08009627 /*
9628 * If size is changed in another node then we need to
9629 * do resize as well.
9630 */
9631 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9632 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9633 if (ret)
9634 pr_info("md-cluster: resize failed\n");
9635 else
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009636 md_bitmap_update_sb(mddev->bitmap);
Guoqing Jiang818da592017-03-01 16:42:40 +08009637 }
9638
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009639 /* Check for change of roles in the active devices */
9640 rdev_for_each(rdev2, mddev) {
9641 if (test_bit(Faulty, &rdev2->flags))
9642 continue;
9643
9644 /* Check if the roles changed */
9645 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009646
9647 if (test_bit(Candidate, &rdev2->flags)) {
9648 if (role == 0xfffe) {
9649 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9650 md_kick_rdev_from_array(rdev2);
9651 continue;
9652 }
9653 else
9654 clear_bit(Candidate, &rdev2->flags);
9655 }
9656
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009657 if (role != rdev2->raid_disk) {
Guoqing Jiangca1e98e2018-10-18 16:37:45 +08009658 /*
9659 * got activated except reshape is happening.
9660 */
9661 if (rdev2->raid_disk == -1 && role != 0xffff &&
9662 !(le32_to_cpu(sb->feature_map) &
9663 MD_FEATURE_RESHAPE_ACTIVE)) {
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009664 rdev2->saved_raid_disk = role;
9665 ret = remove_and_add_spares(mddev, rdev2);
9666 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11009667 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04009668 /* wakeup mddev->thread here, so array could
9669 * perform resync with the new activated disk */
9670 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9671 md_wakeup_thread(mddev->thread);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009672 }
9673 /* device faulty
9674 * We just want to do the minimum to mark the disk
9675 * as faulty. The recovery is performed by the
9676 * one who initiated the error.
9677 */
9678 if ((role == 0xfffe) || (role == 0xfffd)) {
9679 md_error(mddev, rdev2);
9680 clear_bit(Blocked, &rdev2->flags);
9681 }
9682 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009683 }
9684
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009685 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9686 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009687
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009688 /*
9689 * Since mddev->delta_disks has already updated in update_raid_disks,
9690 * so it is time to check reshape.
9691 */
9692 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9693 (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9694 /*
9695 * reshape is happening in the remote node, we need to
9696 * update reshape_position and call start_reshape.
9697 */
Christoph Hellwiged4d0a4e2019-04-04 18:56:10 +02009698 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009699 if (mddev->pers->update_reshape_pos)
9700 mddev->pers->update_reshape_pos(mddev);
9701 if (mddev->pers->start_reshape)
9702 mddev->pers->start_reshape(mddev);
9703 } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9704 mddev->reshape_position != MaxSector &&
9705 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9706 /* reshape is just done in another node. */
9707 mddev->reshape_position = MaxSector;
9708 if (mddev->pers->update_reshape_pos)
9709 mddev->pers->update_reshape_pos(mddev);
9710 }
9711
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009712 /* Finally set the event to be up to date */
9713 mddev->events = le64_to_cpu(sb->events);
9714}
9715
9716static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9717{
9718 int err;
9719 struct page *swapout = rdev->sb_page;
9720 struct mdp_superblock_1 *sb;
9721
9722 /* Store the sb page of the rdev in the swapout temporary
9723 * variable in case we err in the future
9724 */
9725 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009726 err = alloc_disk_sb(rdev);
9727 if (err == 0) {
9728 ClearPageUptodate(rdev->sb_page);
9729 rdev->sb_loaded = 0;
9730 err = super_types[mddev->major_version].
9731 load_super(rdev, NULL, mddev->minor_version);
9732 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009733 if (err < 0) {
9734 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9735 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009736 if (rdev->sb_page)
9737 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009738 rdev->sb_page = swapout;
9739 rdev->sb_loaded = 1;
9740 return err;
9741 }
9742
9743 sb = page_address(rdev->sb_page);
9744 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9745 * is not set
9746 */
9747
9748 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9749 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9750
9751 /* The other node finished recovery, call spare_active to set
9752 * device In_sync and mddev->degraded
9753 */
9754 if (rdev->recovery_offset == MaxSector &&
9755 !test_bit(In_sync, &rdev->flags) &&
9756 mddev->pers->spare_active(mddev))
Junxiao Bie1a86db2020-07-14 16:10:26 -07009757 sysfs_notify_dirent_safe(mddev->sysfs_degraded);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009758
9759 put_page(swapout);
9760 return 0;
9761}
9762
9763void md_reload_sb(struct mddev *mddev, int nr)
9764{
9765 struct md_rdev *rdev;
9766 int err;
9767
9768 /* Find the rdev */
9769 rdev_for_each_rcu(rdev, mddev) {
9770 if (rdev->desc_nr == nr)
9771 break;
9772 }
9773
9774 if (!rdev || rdev->desc_nr != nr) {
9775 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9776 return;
9777 }
9778
9779 err = read_rdev(mddev, rdev);
9780 if (err < 0)
9781 return;
9782
9783 check_sb_changes(mddev, rdev);
9784
9785 /* Read all rdev's to update recovery_offset */
Guoqing Jiang0ea99242018-04-09 17:01:21 +08009786 rdev_for_each_rcu(rdev, mddev) {
9787 if (!test_bit(Faulty, &rdev->flags))
9788 read_rdev(mddev, rdev);
9789 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009790}
9791EXPORT_SYMBOL(md_reload_sb);
9792
Linus Torvalds1da177e2005-04-16 15:20:36 -07009793#ifndef MODULE
9794
9795/*
9796 * Searches all registered partitions for autorun RAID arrays
9797 * at boot time.
9798 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009799
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009800static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009801static LIST_HEAD(all_detected_devices);
9802struct detected_devices_node {
9803 struct list_head list;
9804 dev_t dev;
9805};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009806
9807void md_autodetect_dev(dev_t dev)
9808{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009809 struct detected_devices_node *node_detected_dev;
9810
9811 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9812 if (node_detected_dev) {
9813 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009814 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009815 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009816 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009818}
9819
Linus Torvalds1da177e2005-04-16 15:20:36 -07009820static void autostart_arrays(int part)
9821{
NeilBrown3cb03002011-10-11 16:45:26 +11009822 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009823 struct detected_devices_node *node_detected_dev;
9824 dev_t dev;
9825 int i_scanned, i_passed;
9826
9827 i_scanned = 0;
9828 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009829
NeilBrown9d487392016-11-02 14:16:49 +11009830 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009831
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009832 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009833 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9834 i_scanned++;
9835 node_detected_dev = list_entry(all_detected_devices.next,
9836 struct detected_devices_node, list);
9837 list_del(&node_detected_dev->list);
9838 dev = node_detected_dev->dev;
9839 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009840 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009841 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009842 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843 if (IS_ERR(rdev))
9844 continue;
9845
NeilBrown403df472014-09-30 15:52:29 +10009846 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009847 continue;
NeilBrown403df472014-09-30 15:52:29 +10009848
NeilBrownd0fae182008-03-04 14:29:31 -08009849 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009851 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009852 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009853 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009854
NeilBrown9d487392016-11-02 14:16:49 +11009855 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009856
9857 autorun_devices(part);
9858}
9859
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009860#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009861
9862static __exit void md_exit(void)
9863{
NeilBrownfd01b882011-10-11 16:47:53 +11009864 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009865 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009866 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009867
NeilBrownaf5628f2014-07-31 13:54:54 +10009868 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009869 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009870
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009871 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009872 unregister_blkdev(mdp_major, "mdp");
9873 unregister_reboot_notifier(&md_notifier);
9874 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009875
9876 /* We cannot unload the modules while some process is
9877 * waiting for us in select() or poll() - wake them up
9878 */
9879 md_unloading = 1;
9880 while (waitqueue_active(&md_event_waiters)) {
9881 /* not safe to leave yet */
9882 wake_up(&md_event_waiters);
9883 msleep(delay);
9884 delay += delay;
9885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009886 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009887
NeilBrown29ac4aa2008-02-06 01:39:58 -08009888 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009889 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009890 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009891 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009892 /*
9893 * for_each_mddev() will call mddev_put() at the end of each
9894 * iteration. As the mddev is now fully clear, this will
9895 * schedule the mddev for destruction by a workqueue, and the
9896 * destroy_workqueue() below will wait for that to complete.
9897 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009898 }
Guoqing Jiangcc1ffe62020-04-04 23:57:08 +02009899 destroy_workqueue(md_rdev_misc_wq);
Tejun Heoe804ac72010-10-15 15:36:08 +02009900 destroy_workqueue(md_misc_wq);
9901 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009902}
9903
Dan Williams685784a2007-07-09 11:56:42 -07009904subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009905module_exit(md_exit)
9906
Kees Cooke4dca7b2017-10-17 19:04:42 -07009907static int get_ro(char *buffer, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009908{
Xiongfeng Wang3f999802020-05-11 16:23:25 +08009909 return sprintf(buffer, "%d\n", start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009910}
Kees Cooke4dca7b2017-10-17 19:04:42 -07009911static int set_ro(const char *val, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009912{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009913 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009914}
9915
NeilBrown80ca3a42006-07-10 04:44:18 -07009916module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9917module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009918module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009919module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009920
Linus Torvalds1da177e2005-04-16 15:20:36 -07009921MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009922MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009923MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009924MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);