blob: 469f551863bead9ca419a2a11f20a5a18677f67d [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
NeilBrown4ad23a972017-03-15 14:05:14 +110062#include <linux/percpu-refcount.h>
63
Shaohua Li504634f2016-11-18 09:44:08 -080064#include <trace/events/block.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110065#include "md.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040066#include "md-bitmap.h"
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050067#include "md-cluster.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110070static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#endif
72
NeilBrown01f96c02011-09-21 15:30:20 +100073/* pers_list is a list of registered personalities protected
74 * by pers_lock.
75 * pers_lock does extra service to protect accesses to
76 * mddev->thread when the mutex cannot be held.
77 */
NeilBrown2604b702006-01-06 00:20:36 -080078static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static DEFINE_SPINLOCK(pers_lock);
80
Kent Overstreet28dec872018-06-07 20:52:54 -040081static struct kobj_type md_ktype;
82
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050083struct md_cluster_operations *md_cluster_ops;
Goldwyn Rodrigues589a1c42014-06-07 02:39:37 -050084EXPORT_SYMBOL(md_cluster_ops);
Christoph Hellwig2b598ee2019-04-04 18:56:14 +020085static struct module *md_cluster_mod;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -050086
Bernd Schubert90b08712008-05-23 13:04:38 -070087static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020088static struct workqueue_struct *md_wq;
89static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070090
NeilBrown746d3202013-04-24 11:42:41 +100091static int remove_and_add_spares(struct mddev *mddev,
92 struct md_rdev *this);
NeilBrown5aa61f42014-12-15 12:56:57 +110093static void mddev_detach(struct mddev *mddev);
NeilBrown746d3202013-04-24 11:42:41 +100094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
Robert Becker1e509152009-12-14 12:49:58 +110096 * Default number of read corrections we'll attempt on an rdev
97 * before ejecting it from the array. We divide the read error
98 * count by 2 for every hour elapsed between read errors.
99 */
100#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
101/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
103 * is 1000 KB/sec, so the extra system load does not show up that much.
104 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -0700105 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * subsystem is idle. There is also an 'absolute maximum' reconstruction
107 * speed limit - in case reconstruction slows down your system despite
108 * idle IO detection.
109 *
110 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -0800111 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
113
114static int sysctl_speed_limit_min = 1000;
115static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100116static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800117{
118 return mddev->sync_speed_min ?
119 mddev->sync_speed_min : sysctl_speed_limit_min;
120}
121
NeilBrownfd01b882011-10-11 16:47:53 +1100122static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800123{
124 return mddev->sync_speed_max ?
125 mddev->sync_speed_max : sysctl_speed_limit_max;
126}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100128static void rdev_uninit_serial(struct md_rdev *rdev)
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800129{
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100130 if (!test_and_clear_bit(CollisionCheck, &rdev->flags))
131 return;
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800132
Guoqing Jiang025471f2019-12-23 10:49:01 +0100133 kvfree(rdev->serial);
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100134 rdev->serial = NULL;
Guoqing Jiang3e148a32019-06-19 17:30:46 +0800135}
136
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100137static void rdevs_uninit_serial(struct mddev *mddev)
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100138{
139 struct md_rdev *rdev;
140
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100141 rdev_for_each(rdev, mddev)
142 rdev_uninit_serial(rdev);
143}
144
145static int rdev_init_serial(struct md_rdev *rdev)
146{
Guoqing Jiang025471f2019-12-23 10:49:01 +0100147 /* serial_nums equals with BARRIER_BUCKETS_NR */
148 int i, serial_nums = 1 << ((PAGE_SHIFT - ilog2(sizeof(atomic_t))));
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100149 struct serial_in_rdev *serial = NULL;
150
151 if (test_bit(CollisionCheck, &rdev->flags))
152 return 0;
153
Guoqing Jiang025471f2019-12-23 10:49:01 +0100154 serial = kvmalloc(sizeof(struct serial_in_rdev) * serial_nums,
155 GFP_KERNEL);
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100156 if (!serial)
157 return -ENOMEM;
158
Guoqing Jiang025471f2019-12-23 10:49:01 +0100159 for (i = 0; i < serial_nums; i++) {
160 struct serial_in_rdev *serial_tmp = &serial[i];
161
162 spin_lock_init(&serial_tmp->serial_lock);
163 serial_tmp->serial_rb = RB_ROOT_CACHED;
164 init_waitqueue_head(&serial_tmp->serial_io_wait);
165 }
166
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100167 rdev->serial = serial;
168 set_bit(CollisionCheck, &rdev->flags);
169
170 return 0;
171}
172
173static int rdevs_init_serial(struct mddev *mddev)
174{
175 struct md_rdev *rdev;
176 int ret = 0;
177
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100178 rdev_for_each(rdev, mddev) {
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100179 ret = rdev_init_serial(rdev);
180 if (ret)
181 break;
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100182 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100183
184 /* Free all resources if pool is not existed */
185 if (ret && !mddev->serial_info_pool)
186 rdevs_uninit_serial(mddev);
187
188 return ret;
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100189}
190
Guoqing Jiang963c5552019-06-14 17:10:36 +0800191/*
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100192 * rdev needs to enable serial stuffs if it meets the conditions:
193 * 1. it is multi-queue device flaged with writemostly.
194 * 2. the write-behind mode is enabled.
195 */
196static int rdev_need_serial(struct md_rdev *rdev)
197{
198 return (rdev && rdev->mddev->bitmap_info.max_write_behind > 0 &&
199 rdev->bdev->bd_queue->nr_hw_queues != 1 &&
200 test_bit(WriteMostly, &rdev->flags));
201}
202
203/*
204 * Init resource for rdev(s), then create serial_info_pool if:
205 * 1. rdev is the first device which return true from rdev_enable_serial.
206 * 2. rdev is NULL, means we want to enable serialization for all rdevs.
Guoqing Jiang963c5552019-06-14 17:10:36 +0800207 */
Guoqing Jiang404659c2019-12-23 10:48:53 +0100208void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev,
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100209 bool is_suspend)
Guoqing Jiang963c5552019-06-14 17:10:36 +0800210{
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100211 int ret = 0;
212
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100213 if (rdev && !rdev_need_serial(rdev) &&
214 !test_bit(CollisionCheck, &rdev->flags))
Guoqing Jiang963c5552019-06-14 17:10:36 +0800215 return;
216
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100217 if (!is_suspend)
218 mddev_suspend(mddev);
219
220 if (!rdev)
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100221 ret = rdevs_init_serial(mddev);
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100222 else
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100223 ret = rdev_init_serial(rdev);
224 if (ret)
225 goto abort;
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100226
Guoqing Jiang404659c2019-12-23 10:48:53 +0100227 if (mddev->serial_info_pool == NULL) {
Guoqing Jiang963c5552019-06-14 17:10:36 +0800228 unsigned int noio_flag;
229
Guoqing Jiang963c5552019-06-14 17:10:36 +0800230 noio_flag = memalloc_noio_save();
Guoqing Jiang404659c2019-12-23 10:48:53 +0100231 mddev->serial_info_pool =
232 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
233 sizeof(struct serial_info));
Guoqing Jiang963c5552019-06-14 17:10:36 +0800234 memalloc_noio_restore(noio_flag);
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100235 if (!mddev->serial_info_pool) {
236 rdevs_uninit_serial(mddev);
Guoqing Jiang404659c2019-12-23 10:48:53 +0100237 pr_err("can't alloc memory pool for serialization\n");
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100238 }
Guoqing Jiang963c5552019-06-14 17:10:36 +0800239 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100240
241abort:
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100242 if (!is_suspend)
243 mddev_resume(mddev);
Guoqing Jiang963c5552019-06-14 17:10:36 +0800244}
Guoqing Jiang963c5552019-06-14 17:10:36 +0800245
246/*
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100247 * Free resource from rdev(s), and destroy serial_info_pool under conditions:
248 * 1. rdev is the last device flaged with CollisionCheck.
249 * 2. when bitmap is destroyed while policy is not enabled.
250 * 3. for disable policy, the pool is destroyed only when no rdev needs it.
Guoqing Jiang963c5552019-06-14 17:10:36 +0800251 */
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100252void mddev_destroy_serial_pool(struct mddev *mddev, struct md_rdev *rdev,
253 bool is_suspend)
Guoqing Jiang963c5552019-06-14 17:10:36 +0800254{
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100255 if (rdev && !test_bit(CollisionCheck, &rdev->flags))
Guoqing Jiang963c5552019-06-14 17:10:36 +0800256 return;
257
Guoqing Jiang404659c2019-12-23 10:48:53 +0100258 if (mddev->serial_info_pool) {
Guoqing Jiang963c5552019-06-14 17:10:36 +0800259 struct md_rdev *temp;
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100260 int num = 0; /* used to track if other rdevs need the pool */
Guoqing Jiang963c5552019-06-14 17:10:36 +0800261
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100262 if (!is_suspend)
263 mddev_suspend(mddev);
264 rdev_for_each(temp, mddev) {
265 if (!rdev) {
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100266 if (!mddev->serialize_policy ||
267 !rdev_need_serial(temp))
268 rdev_uninit_serial(temp);
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100269 else
270 num++;
271 } else if (temp != rdev &&
272 test_bit(CollisionCheck, &temp->flags))
Guoqing Jiang963c5552019-06-14 17:10:36 +0800273 num++;
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100274 }
275
276 if (rdev)
Guoqing Jiang69b00b52019-12-23 10:49:00 +0100277 rdev_uninit_serial(rdev);
Guoqing Jiangde31ee92019-12-23 10:48:57 +0100278
279 if (num)
280 pr_info("The mempool could be used by other devices\n");
281 else {
Guoqing Jiang404659c2019-12-23 10:48:53 +0100282 mempool_destroy(mddev->serial_info_pool);
283 mddev->serial_info_pool = NULL;
Guoqing Jiang963c5552019-06-14 17:10:36 +0800284 }
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +0100285 if (!is_suspend)
286 mddev_resume(mddev);
Guoqing Jiang963c5552019-06-14 17:10:36 +0800287 }
288}
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290static struct ctl_table_header *raid_table_header;
291
Joe Perches82592c32013-11-14 15:16:18 +1100292static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 .procname = "speed_limit_min",
295 .data = &sysctl_speed_limit_min,
296 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700297 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800298 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 },
300 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 .procname = "speed_limit_max",
302 .data = &sysctl_speed_limit_max,
303 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700304 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800305 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800307 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308};
309
Joe Perches82592c32013-11-14 15:16:18 +1100310static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 .procname = "raid",
313 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700314 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 .child = raid_table,
316 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800317 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318};
319
Joe Perches82592c32013-11-14 15:16:18 +1100320static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 .procname = "dev",
323 .maxlen = 0,
324 .mode = 0555,
325 .child = raid_dir_table,
326 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800327 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328};
329
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700330static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
NeilBrownf91de922005-11-08 21:39:36 -0800332static int start_readonly;
333
NeilBrown78b63502017-04-12 16:26:13 +1000334/*
335 * The original mechanism for creating an md device is to create
336 * a device node in /dev and to open it. This causes races with device-close.
337 * The preferred method is to write to the "new_array" module parameter.
338 * This can avoid races.
339 * Setting create_on_open to false disables the original mechanism
340 * so all the races disappear.
341 */
342static bool create_on_open = true;
343
NeilBrowna167f662010-10-26 18:31:13 +1100344struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100345 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100346{
Kent Overstreetafeee512018-05-20 18:25:52 -0400347 if (!mddev || !bioset_initialized(&mddev->bio_set))
NeilBrowna167f662010-10-26 18:31:13 +1100348 return bio_alloc(gfp_mask, nr_iovecs);
349
Marcos Paulo de Souza62516912019-01-14 06:31:56 -0700350 return bio_alloc_bioset(gfp_mask, nr_iovecs, &mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100351}
352EXPORT_SYMBOL_GPL(bio_alloc_mddev);
353
NeilBrown5a850712017-06-21 09:12:21 +1000354static struct bio *md_bio_alloc_sync(struct mddev *mddev)
355{
Kent Overstreetafeee512018-05-20 18:25:52 -0400356 if (!mddev || !bioset_initialized(&mddev->sync_set))
NeilBrown5a850712017-06-21 09:12:21 +1000357 return bio_alloc(GFP_NOIO, 1);
358
Kent Overstreetafeee512018-05-20 18:25:52 -0400359 return bio_alloc_bioset(GFP_NOIO, 1, &mddev->sync_set);
NeilBrown5a850712017-06-21 09:12:21 +1000360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/*
NeilBrownd7603b72006-01-06 00:20:30 -0800363 * We have a system wide 'event count' that is incremented
364 * on any 'interesting' event, and readers of /proc/mdstat
365 * can use 'poll' or 'select' to find out when the event
366 * count increases.
367 *
368 * Events are:
369 * start array, stop array, error, add device, remove device,
370 * start build, activate spare
371 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800372static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800373static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100374void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800375{
376 atomic_inc(&md_event_count);
377 wake_up(&md_event_waiters);
378}
NeilBrown29269552006-03-27 01:18:10 -0800379EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800380
381/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 * Enables to iterate over all existing md arrays
383 * all_mddevs_lock protects this list.
384 */
385static LIST_HEAD(all_mddevs);
386static DEFINE_SPINLOCK(all_mddevs_lock);
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/*
389 * iterates through all used mddevs in the system.
390 * We take care to grab the all_mddevs_lock whenever navigating
391 * the list, and to always hold a refcount when unlocked.
392 * Any code which breaks out of this loop while own
393 * a reference to the current mddev and must mddev_put it.
394 */
NeilBrownfd01b882011-10-11 16:47:53 +1100395#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000397 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100398 _tmp = all_mddevs.next; \
399 _mddev = NULL;}); \
400 ({ if (_tmp != &all_mddevs) \
401 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100403 if (_mddev) mddev_put(_mddev); \
404 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
405 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100407 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 )
409
NeilBrown409c57f2009-03-31 14:39:39 +1100410/* Rather than calling directly into the personality make_request function,
411 * IO requests come here first so that we can check if the device is
412 * being suspended pending a reconfiguration.
413 * We hold a refcount over the call to ->make_request. By the time that
414 * call has finished, the bio has been linked into some internal structure
415 * and so is visible to ->quiesce(), so we don't need the refcount any more.
416 */
NeilBrownb3143b92017-10-17 13:46:43 +1100417static bool is_suspended(struct mddev *mddev, struct bio *bio)
418{
419 if (mddev->suspended)
420 return true;
421 if (bio_data_dir(bio) != WRITE)
422 return false;
423 if (mddev->suspend_lo >= mddev->suspend_hi)
424 return false;
425 if (bio->bi_iter.bi_sector >= mddev->suspend_hi)
426 return false;
427 if (bio_end_sector(bio) < mddev->suspend_lo)
428 return false;
429 return true;
430}
431
Shaohua Li393debc2017-09-21 10:23:35 -0700432void md_handle_request(struct mddev *mddev, struct bio *bio)
433{
434check_suspended:
435 rcu_read_lock();
NeilBrownb3143b92017-10-17 13:46:43 +1100436 if (is_suspended(mddev, bio)) {
Shaohua Li393debc2017-09-21 10:23:35 -0700437 DEFINE_WAIT(__wait);
438 for (;;) {
439 prepare_to_wait(&mddev->sb_wait, &__wait,
440 TASK_UNINTERRUPTIBLE);
NeilBrownb3143b92017-10-17 13:46:43 +1100441 if (!is_suspended(mddev, bio))
Shaohua Li393debc2017-09-21 10:23:35 -0700442 break;
443 rcu_read_unlock();
444 schedule();
445 rcu_read_lock();
446 }
447 finish_wait(&mddev->sb_wait, &__wait);
448 }
449 atomic_inc(&mddev->active_io);
450 rcu_read_unlock();
451
452 if (!mddev->pers->make_request(mddev, bio)) {
453 atomic_dec(&mddev->active_io);
454 wake_up(&mddev->sb_wait);
455 goto check_suspended;
456 }
457
458 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
459 wake_up(&mddev->sb_wait);
460}
461EXPORT_SYMBOL(md_handle_request);
462
Jens Axboedece1632015-11-05 10:41:16 -0700463static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
NeilBrown49077322010-03-25 16:20:56 +1100465 const int rw = bio_data_dir(bio);
Michael Callahanddcf35d2018-07-18 04:47:39 -0700466 const int sgrp = op_stat_group(bio_op(bio));
NeilBrownfd01b882011-10-11 16:47:53 +1100467 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500468 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100469
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -0300470 if (unlikely(test_bit(MD_BROKEN, &mddev->flags)) && (rw == WRITE)) {
471 bio_io_error(bio);
472 return BLK_QC_T_NONE;
473 }
474
NeilBrownaf67c312017-06-18 14:38:57 +1000475 blk_queue_split(q, &bio);
Kent Overstreet54efd502015-04-23 22:37:18 -0700476
NeilBrown274d8cb2016-01-04 16:16:58 +1100477 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100478 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700479 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100480 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100481 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200482 if (bio_sectors(bio) != 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200483 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200484 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700485 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100486 }
NeilBrown49077322010-03-25 16:20:56 +1100487
Chris Masone91ece52011-02-07 19:21:48 -0500488 /*
489 * save the sectors now since our bio can
490 * go away inside make_request
491 */
492 sectors = bio_sectors(bio);
Shaohua Li9c573de2016-04-25 16:52:38 -0700493 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600494 bio->bi_opf &= ~REQ_NOMERGE;
Shaohua Li393debc2017-09-21 10:23:35 -0700495
496 md_handle_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100497
Mike Snitzer112f1582018-12-06 11:41:18 -0500498 part_stat_lock();
499 part_stat_inc(&mddev->gendisk->part0, ios[sgrp]);
500 part_stat_add(&mddev->gendisk->part0, sectors[sgrp], sectors);
Gu Zheng74672d02015-04-03 08:44:47 +0800501 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100502
Jens Axboedece1632015-11-05 10:41:16 -0700503 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
NeilBrown9e35b992010-04-06 14:23:02 +1000506/* mddev_suspend makes sure no new requests are submitted
507 * to the device, and that any requests that have been submitted
508 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100509 * Once mddev_detach() is called and completes, the module will be
510 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000511 */
NeilBrownfd01b882011-10-11 16:47:53 +1100512void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100513{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200514 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
NeilBrown4d5324f2017-10-19 12:17:16 +1100515 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100516 if (mddev->suspended++)
517 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100518 synchronize_rcu();
NeilBrowncc27b0c2017-06-05 16:49:39 +1000519 wake_up(&mddev->sb_wait);
NeilBrown35bfc522017-10-17 13:46:43 +1100520 set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
521 smp_mb__after_atomic();
NeilBrown409c57f2009-03-31 14:39:39 +1100522 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
523 mddev->pers->quiesce(mddev, 1);
NeilBrown35bfc522017-10-17 13:46:43 +1100524 clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags);
525 wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags));
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500526
527 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100528}
NeilBrown390ee602010-06-01 19:37:27 +1000529EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100530
NeilBrownfd01b882011-10-11 16:47:53 +1100531void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100532{
NeilBrown4d5324f2017-10-19 12:17:16 +1100533 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100534 if (--mddev->suspended)
535 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100536 wake_up(&mddev->sb_wait);
537 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500538
Jonathan Brassow47525e52012-05-22 13:55:29 +1000539 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500540 md_wakeup_thread(mddev->thread);
541 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100542}
NeilBrown390ee602010-06-01 19:37:27 +1000543EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100544
NeilBrownfd01b882011-10-11 16:47:53 +1100545int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000546{
NeilBrown5c675f82014-12-15 12:56:56 +1100547 struct md_personality *pers = mddev->pers;
548 int ret = 0;
549
550 rcu_read_lock();
551 if (mddev->suspended)
552 ret = 1;
553 else if (pers && pers->congested)
554 ret = pers->congested(mddev, bits);
555 rcu_read_unlock();
556 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000557}
NeilBrown5c675f82014-12-15 12:56:56 +1100558EXPORT_SYMBOL_GPL(mddev_congested);
559static int md_congested(void *data, int bits)
560{
561 struct mddev *mddev = data;
562 return mddev_congested(mddev, bits);
563}
NeilBrown3fa841d2009-09-23 18:10:29 +1000564
NeilBrowna2826aa2009-12-14 12:49:49 +1100565/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200566 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100567 */
NeilBrown4bc034d2019-03-29 10:46:16 -0700568
569static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100570{
NeilBrown4bc034d2019-03-29 10:46:16 -0700571 struct md_rdev *rdev = bio->bi_private;
572 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100573
574 rdev_dec_pending(rdev, mddev);
575
NeilBrown4bc034d2019-03-29 10:46:16 -0700576 if (atomic_dec_and_test(&mddev->flush_pending)) {
577 /* The pre-request flush has finished */
578 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100579 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700580 bio_put(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100581}
582
NeilBrown4bc034d2019-03-29 10:46:16 -0700583static void md_submit_flush_data(struct work_struct *ws);
584
585static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100586{
NeilBrown4bc034d2019-03-29 10:46:16 -0700587 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100588 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100589
NeilBrown2bc13b82019-03-29 10:46:17 -0700590 mddev->start_flush = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700591 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
592 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100593 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100594 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100595 if (rdev->raid_disk >= 0 &&
596 !test_bit(Faulty, &rdev->flags)) {
597 /* Take two references, one is dropped
598 * when request finishes, one after
599 * we reclaim rcu_read_lock
600 */
601 struct bio *bi;
602 atomic_inc(&rdev->nr_pending);
603 atomic_inc(&rdev->nr_pending);
604 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000605 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Xiao Ni5a409b42018-05-21 11:49:54 +0800606 bi->bi_end_io = md_end_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700607 bi->bi_private = rdev;
608 bio_set_dev(bi, rdev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600609 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
NeilBrown4bc034d2019-03-29 10:46:16 -0700610 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500611 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100612 rcu_read_lock();
613 rdev_dec_pending(rdev, mddev);
614 }
615 rcu_read_unlock();
NeilBrown4bc034d2019-03-29 10:46:16 -0700616 if (atomic_dec_and_test(&mddev->flush_pending))
617 queue_work(md_wq, &mddev->flush_work);
618}
NeilBrowna2826aa2009-12-14 12:49:49 +1100619
NeilBrown4bc034d2019-03-29 10:46:16 -0700620static void md_submit_flush_data(struct work_struct *ws)
621{
622 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
623 struct bio *bio = mddev->flush_bio;
624
625 /*
626 * must reset flush_bio before calling into md_handle_request to avoid a
627 * deadlock, because other bios passed md_handle_request suspend check
628 * could wait for this and below md_handle_request could wait for those
629 * bios because of suspend check
630 */
NeilBrown2bc13b82019-03-29 10:46:17 -0700631 mddev->last_flush = mddev->start_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700632 mddev->flush_bio = NULL;
633 wake_up(&mddev->sb_wait);
634
635 if (bio->bi_iter.bi_size == 0) {
636 /* an empty barrier - all done */
637 bio_endio(bio);
638 } else {
639 bio->bi_opf &= ~REQ_PREFLUSH;
640 md_handle_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100641 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100642}
NeilBrown4bc034d2019-03-29 10:46:16 -0700643
David Jeffery775d7832019-09-16 13:15:14 -0400644/*
645 * Manages consolidation of flushes and submitting any flushes needed for
646 * a bio with REQ_PREFLUSH. Returns true if the bio is finished or is
647 * being finished in another context. Returns false if the flushing is
648 * complete but still needs the I/O portion of the bio to be processed.
649 */
650bool md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrown4bc034d2019-03-29 10:46:16 -0700651{
NeilBrown2bc13b82019-03-29 10:46:17 -0700652 ktime_t start = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700653 spin_lock_irq(&mddev->lock);
654 wait_event_lock_irq(mddev->sb_wait,
NeilBrown2bc13b82019-03-29 10:46:17 -0700655 !mddev->flush_bio ||
656 ktime_after(mddev->last_flush, start),
NeilBrown4bc034d2019-03-29 10:46:16 -0700657 mddev->lock);
NeilBrown2bc13b82019-03-29 10:46:17 -0700658 if (!ktime_after(mddev->last_flush, start)) {
659 WARN_ON(mddev->flush_bio);
660 mddev->flush_bio = bio;
661 bio = NULL;
662 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700663 spin_unlock_irq(&mddev->lock);
664
NeilBrown2bc13b82019-03-29 10:46:17 -0700665 if (!bio) {
666 INIT_WORK(&mddev->flush_work, submit_flushes);
667 queue_work(md_wq, &mddev->flush_work);
668 } else {
669 /* flush was performed for some other bio while we waited. */
670 if (bio->bi_iter.bi_size == 0)
671 /* an empty barrier - all done */
672 bio_endio(bio);
673 else {
674 bio->bi_opf &= ~REQ_PREFLUSH;
David Jeffery775d7832019-09-16 13:15:14 -0400675 return false;
NeilBrown2bc13b82019-03-29 10:46:17 -0700676 }
677 }
David Jeffery775d7832019-09-16 13:15:14 -0400678 return true;
NeilBrown4bc034d2019-03-29 10:46:16 -0700679}
Tejun Heoe9c74692010-09-03 11:56:18 +0200680EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100681
NeilBrownfd01b882011-10-11 16:47:53 +1100682static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 atomic_inc(&mddev->active);
685 return mddev;
686}
687
Dan Williams5fd3a172009-03-04 00:57:25 -0700688static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100689
NeilBrownfd01b882011-10-11 16:47:53 +1100690static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
693 return;
NeilBrownd3374822009-01-09 08:31:10 +1100694 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100695 mddev->ctime == 0 && !mddev->hold_active) {
696 /* Array is not configured at all, and not held active,
697 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100698 list_del_init(&mddev->all_mddevs);
Kent Overstreet28dec872018-06-07 20:52:54 -0400699
700 /*
701 * Call queue_work inside the spinlock so that
702 * flush_workqueue() after mddev_find will succeed in waiting
703 * for the work to be done.
704 */
705 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
706 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100707 }
708 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Kees Cook8376d3c2017-10-16 17:01:48 -0700711static void md_safemode_timeout(struct timer_list *t);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400712
NeilBrownfd01b882011-10-11 16:47:53 +1100713void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100714{
Kent Overstreet28dec872018-06-07 20:52:54 -0400715 kobject_init(&mddev->kobj, &md_ktype);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100716 mutex_init(&mddev->open_mutex);
717 mutex_init(&mddev->reconfig_mutex);
718 mutex_init(&mddev->bitmap_info.mutex);
719 INIT_LIST_HEAD(&mddev->disks);
720 INIT_LIST_HEAD(&mddev->all_mddevs);
Kees Cook8376d3c2017-10-16 17:01:48 -0700721 timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100722 atomic_set(&mddev->active, 1);
723 atomic_set(&mddev->openers, 0);
724 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100725 spin_lock_init(&mddev->lock);
NeilBrown4bc034d2019-03-29 10:46:16 -0700726 atomic_set(&mddev->flush_pending, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100727 init_waitqueue_head(&mddev->sb_wait);
728 init_waitqueue_head(&mddev->recovery_wait);
729 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000730 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500731 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100732 mddev->resync_min = 0;
733 mddev->resync_max = MaxSector;
734 mddev->level = LEVEL_NONE;
735}
NeilBrown390ee602010-06-01 19:37:27 +1000736EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100737
NeilBrownf72ffdd2014-09-30 14:23:59 +1000738static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
NeilBrownfd01b882011-10-11 16:47:53 +1100740 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
NeilBrown8f5f02c2011-02-16 13:58:51 +1100742 if (unit && MAJOR(unit) != MD_MAJOR)
743 unit &= ~((1<<MdpMinorShift)-1);
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 retry:
746 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
NeilBrownefeb53c2009-01-09 08:31:10 +1100748 if (unit) {
749 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
750 if (mddev->unit == unit) {
751 mddev_get(mddev);
752 spin_unlock(&all_mddevs_lock);
753 kfree(new);
754 return mddev;
755 }
756
757 if (new) {
758 list_add(&new->all_mddevs, &all_mddevs);
759 spin_unlock(&all_mddevs_lock);
760 new->hold_active = UNTIL_IOCTL;
761 return new;
762 }
763 } else if (new) {
764 /* find an unused unit number */
765 static int next_minor = 512;
766 int start = next_minor;
767 int is_free = 0;
768 int dev = 0;
769 while (!is_free) {
770 dev = MKDEV(MD_MAJOR, next_minor);
771 next_minor++;
772 if (next_minor > MINORMASK)
773 next_minor = 0;
774 if (next_minor == start) {
775 /* Oh dear, all in use. */
776 spin_unlock(&all_mddevs_lock);
777 kfree(new);
778 return NULL;
779 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000780
NeilBrownefeb53c2009-01-09 08:31:10 +1100781 is_free = 1;
782 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
783 if (mddev->unit == dev) {
784 is_free = 0;
785 break;
786 }
787 }
788 new->unit = dev;
789 new->md_minor = MINOR(dev);
790 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 list_add(&new->all_mddevs, &all_mddevs);
792 spin_unlock(&all_mddevs_lock);
793 return new;
794 }
795 spin_unlock(&all_mddevs_lock);
796
NeilBrown9ffae0c2006-01-06 00:20:32 -0800797 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!new)
799 return NULL;
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 new->unit = unit;
802 if (MAJOR(unit) == MD_MAJOR)
803 new->md_minor = MINOR(unit);
804 else
805 new->md_minor = MINOR(unit) >> MdpMinorShift;
806
NeilBrownfafd7fb2010-04-01 15:55:30 +1100807 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 goto retry;
810}
811
NeilBrownb6eb1272010-04-15 10:13:47 +1000812static struct attribute_group md_redundancy_group;
813
NeilBrown5c47daf2014-12-15 12:57:01 +1100814void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
NeilBrowna64c8762010-04-14 17:15:37 +1000816 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000817 /* These cannot be removed under reconfig_mutex as
818 * an access to the files will try to take reconfig_mutex
819 * while holding the file unremovable, which leads to
820 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000821 * So hold set sysfs_active while the remove in happeing,
822 * and anything else which might set ->to_remove or my
823 * otherwise change the sysfs namespace will fail with
824 * -EBUSY if sysfs_active is still set.
825 * We set sysfs_active under reconfig_mutex and elsewhere
826 * test it under the same mutex to ensure its correct value
827 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000828 */
NeilBrowna64c8762010-04-14 17:15:37 +1000829 struct attribute_group *to_remove = mddev->to_remove;
830 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000831 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000832 mutex_unlock(&mddev->reconfig_mutex);
833
NeilBrown00bcb4a2010-06-01 19:37:23 +1000834 if (mddev->kobj.sd) {
835 if (to_remove != &md_redundancy_group)
836 sysfs_remove_group(&mddev->kobj, to_remove);
837 if (mddev->pers == NULL ||
838 mddev->pers->sync_request == NULL) {
839 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
840 if (mddev->sysfs_action)
841 sysfs_put(mddev->sysfs_action);
842 mddev->sysfs_action = NULL;
843 }
NeilBrowna64c8762010-04-14 17:15:37 +1000844 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000845 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000846 } else
847 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Chris Dunlop751e67c2011-10-19 16:48:26 +1100849 /* As we've dropped the mutex we need a spinlock to
850 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000851 */
852 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700853 md_wakeup_thread(mddev->thread);
NeilBrown4d5324f2017-10-19 12:17:16 +1100854 wake_up(&mddev->sb_wait);
NeilBrown01f96c02011-09-21 15:30:20 +1000855 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
NeilBrown5c47daf2014-12-15 12:57:01 +1100857EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500859struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100860{
861 struct md_rdev *rdev;
862
863 rdev_for_each_rcu(rdev, mddev)
864 if (rdev->desc_nr == nr)
865 return rdev;
866
867 return NULL;
868}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500869EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100870
871static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
NeilBrown3cb03002011-10-11 16:45:26 +1100873 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
NeilBrowndafb20f2012-03-19 12:46:39 +1100875 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (rdev->bdev->bd_dev == dev)
877 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return NULL;
880}
881
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100882struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev)
NeilBrown1ca69c42012-10-11 13:37:33 +1100883{
884 struct md_rdev *rdev;
885
886 rdev_for_each_rcu(rdev, mddev)
887 if (rdev->bdev->bd_dev == dev)
888 return rdev;
889
890 return NULL;
891}
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100892EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100893
NeilBrown84fc4b52011-10-11 16:49:58 +1100894static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800895{
NeilBrown84fc4b52011-10-11 16:49:58 +1100896 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800897 list_for_each_entry(pers, &pers_list, list) {
898 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800899 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800900 if (strcmp(pers->name, clevel)==0)
901 return pers;
902 }
NeilBrown2604b702006-01-06 00:20:36 -0800903 return NULL;
904}
905
Andre Nollb73df2d2008-07-11 22:02:23 +1000906/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100907static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100909 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000910 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
NeilBrownf72ffdd2014-09-30 14:23:59 +1000913static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100916 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000917 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return 0;
919}
920
NeilBrown545c8792012-05-22 13:54:30 +1000921void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800924 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 rdev->sb_loaded = 0;
926 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000927 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100928 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
NeilBrown2699b672011-07-28 11:31:47 +1000930 if (rdev->bb_page) {
931 put_page(rdev->bb_page);
932 rdev->bb_page = NULL;
933 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800934 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
NeilBrown545c8792012-05-22 13:54:30 +1000936EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200938static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700939{
NeilBrown3cb03002011-10-11 16:45:26 +1100940 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100941 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700942
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200943 if (bio->bi_status) {
944 pr_err("md: super_written gets error=%d\n", bio->bi_status);
NeilBrowna9701a32005-11-08 21:39:34 -0800945 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100946 if (!test_bit(Faulty, &rdev->flags)
947 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800948 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100949 set_bit(LastDev, &rdev->flags);
950 }
951 } else
952 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700953
NeilBrowna9701a32005-11-08 21:39:34 -0800954 if (atomic_dec_and_test(&mddev->pending_writes))
955 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700956 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700957 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700958}
959
NeilBrownfd01b882011-10-11 16:47:53 +1100960void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700961 sector_t sector, int size, struct page *page)
962{
963 /* write first size bytes of page to sector of rdev
964 * Increment mddev->pending_writes before returning
965 * and decrement it on completion, waking up sb_wait
966 * if zero is reached.
967 * If an error occurred, call md_error
968 */
NeilBrown46533ff2016-11-18 16:16:11 +1100969 struct bio *bio;
970 int ff = 0;
971
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +0100972 if (!page)
973 return;
974
NeilBrown46533ff2016-11-18 16:16:11 +1100975 if (test_bit(Faulty, &rdev->flags))
976 return;
977
NeilBrown5a850712017-06-21 09:12:21 +1000978 bio = md_bio_alloc_sync(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700979
Shaohua Lied3b98c2016-03-29 14:00:19 -0700980 atomic_inc(&rdev->nr_pending);
981
Christoph Hellwig74d46992017-08-23 19:10:32 +0200982 bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700983 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700984 bio_add_page(bio, page, size, 0);
985 bio->bi_private = rdev;
986 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +1100987
988 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
989 test_bit(FailFast, &rdev->flags) &&
990 !test_bit(LastDev, &rdev->flags))
991 ff = MD_FAILFAST;
Jan Kara5a8948f2017-05-31 09:44:33 +0200992 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -0800993
NeilBrown7bfa19f2005-06-21 17:17:28 -0700994 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500995 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800996}
997
NeilBrown46533ff2016-11-18 16:16:11 +1100998int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800999{
Tejun Heoe9c74692010-09-03 11:56:18 +02001000 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +10001001 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -08001002 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +11001003 return -EAGAIN;
1004 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001005}
1006
NeilBrown3cb03002011-10-11 16:45:26 +11001007int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -05001008 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
NeilBrown5a850712017-06-21 09:12:21 +10001010 struct bio *bio = md_bio_alloc_sync(rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 int ret;
1012
Christoph Hellwig74d46992017-08-23 19:10:32 +02001013 if (metadata_op && rdev->meta_bdev)
1014 bio_set_dev(bio, rdev->meta_bdev);
1015 else
1016 bio_set_dev(bio, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -05001017 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001018 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -07001019 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +10001020 else if (rdev->mddev->reshape_position != MaxSector &&
1021 (rdev->mddev->reshape_backwards ==
1022 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001023 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001024 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001025 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -05001027
1028 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001030 ret = !bio->bi_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 bio_put(bio);
1032 return ret;
1033}
NeilBrowna8745db2006-01-06 00:20:34 -08001034EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
NeilBrownf72ffdd2014-09-30 14:23:59 +10001036static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (rdev->sb_loaded)
1041 return 0;
1042
Mike Christie796a5cf2016-06-05 14:32:07 -05001043 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 goto fail;
1045 rdev->sb_loaded = 1;
1046 return 0;
1047
1048fail:
NeilBrown9d487392016-11-02 14:16:49 +11001049 pr_err("md: disabled device %s, could not read superblock.\n",
1050 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return -EINVAL;
1052}
1053
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001054static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
NeilBrownf72ffdd2014-09-30 14:23:59 +10001056 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +10001057 sb1->set_uuid1 == sb2->set_uuid1 &&
1058 sb1->set_uuid2 == sb2->set_uuid2 &&
1059 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001062static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 int ret;
1065 mdp_super_t *tmp1, *tmp2;
1066
1067 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
1068 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
1069
1070 if (!tmp1 || !tmp2) {
1071 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 goto abort;
1073 }
1074
1075 *tmp1 = *sb1;
1076 *tmp2 = *sb2;
1077
1078 /*
1079 * nr_disks is not constant
1080 */
1081 tmp1->nr_disks = 0;
1082 tmp2->nr_disks = 0;
1083
Andre Nollce0c8e02008-07-11 22:02:20 +10001084 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -07001086 kfree(tmp1);
1087 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return ret;
1089}
1090
NeilBrown4d167f02007-05-09 02:35:37 -07001091static u32 md_csum_fold(u32 csum)
1092{
1093 csum = (csum & 0xffff) + (csum >> 16);
1094 return (csum & 0xffff) + (csum >> 16);
1095}
1096
NeilBrownf72ffdd2014-09-30 14:23:59 +10001097static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
NeilBrown4d167f02007-05-09 02:35:37 -07001099 u64 newcsum = 0;
1100 u32 *sb32 = (u32*)sb;
1101 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 unsigned int disk_csum, csum;
1103
1104 disk_csum = sb->sb_csum;
1105 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -07001106
1107 for (i = 0; i < MD_SB_BYTES/4 ; i++)
1108 newcsum += sb32[i];
1109 csum = (newcsum & 0xffffffff) + (newcsum>>32);
1110
NeilBrown4d167f02007-05-09 02:35:37 -07001111#ifdef CONFIG_ALPHA
1112 /* This used to use csum_partial, which was wrong for several
1113 * reasons including that different results are returned on
1114 * different architectures. It isn't critical that we get exactly
1115 * the same return value as before (we always csum_fold before
1116 * testing, and that removes any differences). However as we
1117 * know that csum_partial always returned a 16bit value on
1118 * alphas, do a fold to maximise conformity to previous behaviour.
1119 */
1120 sb->sb_csum = md_csum_fold(disk_csum);
1121#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -07001123#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return csum;
1125}
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127/*
1128 * Handle superblock details.
1129 * We want to be able to handle multiple superblock formats
1130 * so we have a common interface to them all, and an array of
1131 * different handlers.
1132 * We rely on user-space to write the initial superblock, and support
1133 * reading and updating of superblocks.
1134 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +11001135 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 * loads and validates a superblock on dev.
1137 * if refdev != NULL, compare superblocks on both devices
1138 * Return:
1139 * 0 - dev has a superblock that is compatible with refdev
1140 * 1 - dev has a superblock that is compatible and newer than refdev
1141 * so dev should be used as the refdev in future
1142 * -EINVAL superblock incompatible or invalid
1143 * -othererror e.g. -EIO
1144 *
NeilBrownfd01b882011-10-11 16:47:53 +11001145 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 * Verify that dev is acceptable into mddev.
1147 * The first time, mddev->raid_disks will be 0, and data from
1148 * dev should be merged in. Subsequent calls check that dev
1149 * is new enough. Return 0 or -EINVAL
1150 *
NeilBrownfd01b882011-10-11 16:47:53 +11001151 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 * Update the superblock for rdev with data in mddev
1153 * This does not write to disc.
1154 *
1155 */
1156
1157struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001158 char *name;
1159 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +10001160 int (*load_super)(struct md_rdev *rdev,
1161 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001162 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +10001163 int (*validate_super)(struct mddev *mddev,
1164 struct md_rdev *rdev);
1165 void (*sync_super)(struct mddev *mddev,
1166 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001167 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001168 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +10001169 int (*allow_new_offset)(struct md_rdev *rdev,
1170 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171};
1172
1173/*
Andre Noll0894cc32009-06-18 08:49:23 +10001174 * Check that the given mddev has no bitmap.
1175 *
1176 * This function is called from the run method of all personalities that do not
1177 * support bitmaps. It prints an error message and returns non-zero if mddev
1178 * has a bitmap. Otherwise, it returns 0.
1179 *
1180 */
NeilBrownfd01b882011-10-11 16:47:53 +11001181int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001182{
NeilBrownc3d97142009-12-14 12:49:52 +11001183 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001184 return 0;
NeilBrown9d487392016-11-02 14:16:49 +11001185 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +10001186 mdname(mddev), mddev->pers->name);
1187 return 1;
1188}
1189EXPORT_SYMBOL(md_check_no_bitmap);
1190
1191/*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001192 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 */
NeilBrown3cb03002011-10-11 16:45:26 +11001194static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1197 mdp_super_t *sb;
1198 int ret;
Yufen Yu228fc7d2019-10-30 18:47:02 +08001199 bool spare_disk = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 /*
Andre Noll0f420352008-07-11 22:02:23 +10001202 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 * it's at the end of the disk.
1204 *
1205 * It also happens to be a multiple of 4Kb.
1206 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001207 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
NeilBrown0002b272005-09-09 16:23:53 -07001209 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +11001210 if (ret)
1211 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 ret = -EINVAL;
1214
1215 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001216 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +11001219 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 goto abort;
1221 }
1222
1223 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001224 sb->minor_version < 90 ||
1225 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +11001226 pr_warn("Bad version number %d.%d on %s\n",
1227 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 goto abort;
1229 }
1230
1231 if (sb->raid_disks <= 0)
1232 goto abort;
1233
NeilBrown4d167f02007-05-09 02:35:37 -07001234 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001235 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 goto abort;
1237 }
1238
1239 rdev->preferred_minor = sb->md_minor;
1240 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001241 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001242 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001243 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 if (sb->level == LEVEL_MULTIPATH)
1246 rdev->desc_nr = -1;
1247 else
1248 rdev->desc_nr = sb->this_disk.number;
1249
Yufen Yu228fc7d2019-10-30 18:47:02 +08001250 /* not spare disk, or LEVEL_MULTIPATH */
1251 if (sb->level == LEVEL_MULTIPATH ||
1252 (rdev->desc_nr >= 0 &&
Yufen Yu3b7436c2019-12-10 15:01:29 +08001253 rdev->desc_nr < MD_SB_DISKS &&
Yufen Yu228fc7d2019-10-30 18:47:02 +08001254 sb->disks[rdev->desc_nr].state &
1255 ((1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))))
1256 spare_disk = false;
1257
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001258 if (!refdev) {
Yufen Yu228fc7d2019-10-30 18:47:02 +08001259 if (!spare_disk)
Yufen Yu6a5cb532019-10-16 16:00:03 +08001260 ret = 1;
1261 else
1262 ret = 0;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001263 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001265 mdp_super_t *refsb = page_address(refdev->sb_page);
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001266 if (!md_uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001267 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 b, bdevname(refdev->bdev,b2));
1269 goto abort;
1270 }
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001271 if (!md_sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001272 pr_warn("md: %s has same UUID but different superblock to %s\n",
1273 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 goto abort;
1275 }
1276 ev1 = md_event(sb);
1277 ev2 = md_event(refsb);
Yufen Yu6a5cb532019-10-16 16:00:03 +08001278
Yufen Yu228fc7d2019-10-30 18:47:02 +08001279 if (!spare_disk && ev1 > ev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001281 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 ret = 0;
1283 }
NeilBrown8190e752009-06-18 08:48:58 +10001284 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001285 /* Limit to 4TB as metadata cannot record more than that.
1286 * (not needed for Linear and RAID0 as metadata doesn't
1287 * record this size)
1288 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001289 if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001290 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
NeilBrown27a7b262011-09-10 17:21:28 +10001292 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001293 /* "this cannot possibly happen" ... */
1294 ret = -EINVAL;
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 abort:
1297 return ret;
1298}
1299
1300/*
1301 * validate_super for 0.90.0
1302 */
NeilBrownfd01b882011-10-11 16:47:53 +11001303static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001306 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001307 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
NeilBrown41158c72005-06-21 17:17:25 -07001309 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001310 clear_bit(Faulty, &rdev->flags);
1311 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001312 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001313 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (mddev->raid_disks == 0) {
1316 mddev->major_version = 0;
1317 mddev->minor_version = sb->minor_version;
1318 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001319 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001320 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 mddev->ctime = sb->ctime;
1322 mddev->utime = sb->utime;
1323 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001324 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 mddev->layout = sb->layout;
1326 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001327 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001328 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001329 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001330 mddev->bitmap_info.space = 0;
1331 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001332 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001333 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001334 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
NeilBrownf6705572006-03-27 01:18:11 -08001336 if (mddev->minor_version >= 91) {
1337 mddev->reshape_position = sb->reshape_position;
1338 mddev->delta_disks = sb->delta_disks;
1339 mddev->new_level = sb->new_level;
1340 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001341 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001342 if (mddev->delta_disks < 0)
1343 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001344 } else {
1345 mddev->reshape_position = MaxSector;
1346 mddev->delta_disks = 0;
1347 mddev->new_level = mddev->level;
1348 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001349 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001350 }
NeilBrown33f2c352019-09-09 16:52:29 +10001351 if (mddev->level == 0)
1352 mddev->layout = -1;
NeilBrownf6705572006-03-27 01:18:11 -08001353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (sb->state & (1<<MD_SB_CLEAN))
1355 mddev->recovery_cp = MaxSector;
1356 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001357 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 sb->events_lo == sb->cp_events_lo) {
1359 mddev->recovery_cp = sb->recovery_cp;
1360 } else
1361 mddev->recovery_cp = 0;
1362 }
1363
1364 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1365 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1366 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1367 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1368
1369 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001370
1371 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001372 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001373 mddev->bitmap_info.offset =
1374 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001375 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001376 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001377 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001378
NeilBrown41158c72005-06-21 17:17:25 -07001379 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001380 /* Insist on good event counter while assembling, except
1381 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001383 if (sb->disks[rdev->desc_nr].state & (
1384 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001385 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001386 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001387 } else if (mddev->bitmap) {
1388 /* if adding to array with a bitmap, then we can accept an
1389 * older device ... but not too old.
1390 */
NeilBrown41158c72005-06-21 17:17:25 -07001391 if (ev1 < mddev->bitmap->events_cleared)
1392 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001393 if (ev1 < mddev->events)
1394 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001395 } else {
1396 if (ev1 < mddev->events)
1397 /* just a hot-add of a new device, leave raid_disk at -1 */
1398 return 0;
1399 }
NeilBrown41158c72005-06-21 17:17:25 -07001400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 desc = sb->disks + rdev->desc_nr;
1403
1404 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001405 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001406 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1407 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001408 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001410 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001411 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1412 /* active but not in sync implies recovery up to
1413 * reshape position. We don't know exactly where
1414 * that is, so set to zero for now */
1415 if (mddev->minor_version >= 91) {
1416 rdev->recovery_offset = 0;
1417 rdev->raid_disk = desc->raid_disk;
1418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001420 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1421 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001422 if (desc->state & (1<<MD_DISK_FAILFAST))
1423 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001424 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001425 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return 0;
1427}
1428
1429/*
1430 * sync_super for 0.90.0
1431 */
NeilBrownfd01b882011-10-11 16:47:53 +11001432static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001435 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 /* make rdev->sb match mddev data..
1439 *
1440 * 1/ zero out disks
1441 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1442 * 3/ any empty disks < next_spare become removed
1443 *
1444 * disks[0] gets initialised to REMOVED because
1445 * we cannot be sure from other fields if it has
1446 * been initialised or not.
1447 */
1448 int i;
1449 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1450
NeilBrown61181562005-09-09 16:24:02 -07001451 rdev->sb_size = MD_SB_BYTES;
1452
Namhyung Kim65a06f062011-07-27 11:00:36 +10001453 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 memset(sb, 0, sizeof(*sb));
1456
1457 sb->md_magic = MD_SB_MAGIC;
1458 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 sb->patch_version = mddev->patch_version;
1460 sb->gvalid_words = 0; /* ignored */
1461 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1462 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1463 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1464 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1465
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001466 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001468 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 sb->raid_disks = mddev->raid_disks;
1470 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001471 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001472 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 sb->state = 0;
1474 sb->events_hi = (mddev->events>>32);
1475 sb->events_lo = (u32)mddev->events;
1476
NeilBrownf6705572006-03-27 01:18:11 -08001477 if (mddev->reshape_position == MaxSector)
1478 sb->minor_version = 90;
1479 else {
1480 sb->minor_version = 91;
1481 sb->reshape_position = mddev->reshape_position;
1482 sb->new_level = mddev->new_level;
1483 sb->delta_disks = mddev->delta_disks;
1484 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001485 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001486 }
1487 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (mddev->in_sync)
1489 {
1490 sb->recovery_cp = mddev->recovery_cp;
1491 sb->cp_events_hi = (mddev->events>>32);
1492 sb->cp_events_lo = (u32)mddev->events;
1493 if (mddev->recovery_cp == MaxSector)
1494 sb->state = (1<< MD_SB_CLEAN);
1495 } else
1496 sb->recovery_cp = 0;
1497
1498 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001499 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
NeilBrownc3d97142009-12-14 12:49:52 +11001501 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001502 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001505 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001507 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001508 int is_active = test_bit(In_sync, &rdev2->flags);
1509
1510 if (rdev2->raid_disk >= 0 &&
1511 sb->minor_version >= 91)
1512 /* we have nowhere to store the recovery_offset,
1513 * but if it is not below the reshape_position,
1514 * we can piggy-back on that.
1515 */
1516 is_active = 1;
1517 if (rdev2->raid_disk < 0 ||
1518 test_bit(Faulty, &rdev2->flags))
1519 is_active = 0;
1520 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001521 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001523 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001524 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 d = &sb->disks[rdev2->desc_nr];
1526 nr_disks++;
1527 d->number = rdev2->desc_nr;
1528 d->major = MAJOR(rdev2->bdev->bd_dev);
1529 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001530 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 d->raid_disk = rdev2->raid_disk;
1532 else
1533 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001534 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001536 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001538 if (test_bit(In_sync, &rdev2->flags))
1539 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 active++;
1541 working++;
1542 } else {
1543 d->state = 0;
1544 spare++;
1545 working++;
1546 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001547 if (test_bit(WriteMostly, &rdev2->flags))
1548 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001549 if (test_bit(FailFast, &rdev2->flags))
1550 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 /* now set the "removed" and "faulty" bits on any missing devices */
1553 for (i=0 ; i < mddev->raid_disks ; i++) {
1554 mdp_disk_t *d = &sb->disks[i];
1555 if (d->state == 0 && d->number == 0) {
1556 d->number = i;
1557 d->raid_disk = i;
1558 d->state = (1<<MD_DISK_REMOVED);
1559 d->state |= (1<<MD_DISK_FAULTY);
1560 failed++;
1561 }
1562 }
1563 sb->nr_disks = nr_disks;
1564 sb->active_disks = active;
1565 sb->working_disks = working;
1566 sb->failed_disks = failed;
1567 sb->spare_disks = spare;
1568
1569 sb->this_disk = sb->disks[rdev->desc_nr];
1570 sb->sb_csum = calc_sb_csum(sb);
1571}
1572
1573/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001574 * rdev_size_change for 0.90.0
1575 */
1576static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001577super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001578{
Andre Noll58c0fed2009-03-31 14:33:13 +11001579 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001580 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001581 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001582 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001583 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001584 if (!num_sectors || num_sectors > rdev->sb_start)
1585 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001586 /* Limit to 4TB as metadata cannot record more than that.
1587 * 4TB == 2^32 KB, or 2*2^32 sectors.
1588 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001589 if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001590 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001591 do {
1592 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001593 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001594 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001595 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001596}
1597
NeilBrownc6563a82012-05-21 09:27:00 +10001598static int
1599super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1600{
1601 /* non-zero offset changes not possible with v0.90 */
1602 return new_offset == 0;
1603}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001604
1605/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 * version 1 superblock
1607 */
1608
NeilBrownf72ffdd2014-09-30 14:23:59 +10001609static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001611 __le32 disk_csum;
1612 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 unsigned long long newcsum;
1614 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001615 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 disk_csum = sb->sb_csum;
1618 sb->sb_csum = 0;
1619 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001620 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 newcsum += le32_to_cpu(*isuper++);
1622
1623 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001624 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1627 sb->sb_csum = disk_csum;
1628 return cpu_to_le32(csum);
1629}
1630
NeilBrown3cb03002011-10-11 16:45:26 +11001631static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
1633 struct mdp_superblock_1 *sb;
1634 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001635 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001636 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001638 int bmask;
Yufen Yu228fc7d2019-10-30 18:47:02 +08001639 bool spare_disk = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 /*
Andre Noll0f420352008-07-11 22:02:23 +10001642 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 * It is always aligned to a 4K boundary and
1644 * depeding on minor_version, it can be:
1645 * 0: At least 8K, but less than 12K, from end of device
1646 * 1: At start of device
1647 * 2: 4K from start of device.
1648 */
1649 switch(minor_version) {
1650 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001651 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001652 sb_start -= 8*2;
1653 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 break;
1655 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001656 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 break;
1658 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001659 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 break;
1661 default:
1662 return -EINVAL;
1663 }
Andre Noll0f420352008-07-11 22:02:23 +10001664 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
NeilBrown0002b272005-09-09 16:23:53 -07001666 /* superblock is rarely larger than 1K, but it can be larger,
1667 * and it is safe to read 4k, so we do that
1668 */
1669 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (ret) return ret;
1671
Namhyung Kim65a06f062011-07-27 11:00:36 +10001672 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1675 sb->major_version != cpu_to_le32(1) ||
1676 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001677 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001678 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return -EINVAL;
1680
1681 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001682 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 bdevname(rdev->bdev,b));
1684 return -EINVAL;
1685 }
1686 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001687 pr_warn("md: data_size too small on %s\n",
1688 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return -EINVAL;
1690 }
NeilBrownc6563a82012-05-21 09:27:00 +10001691 if (sb->pad0 ||
1692 sb->pad3[0] ||
1693 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1694 /* Some padding is non-zero, might be a new feature */
1695 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 rdev->preferred_minor = 0xffff;
1698 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001699 rdev->new_data_offset = rdev->data_offset;
1700 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1701 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1702 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001703 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
NeilBrown0002b272005-09-09 16:23:53 -07001705 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001706 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001707 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001708 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1709
1710 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001711 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001712 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001713 if (minor_version
1714 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1715 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001716
NeilBrown31b65a02006-07-10 04:44:14 -07001717 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1718 rdev->desc_nr = -1;
1719 else
1720 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1721
NeilBrown2699b672011-07-28 11:31:47 +10001722 if (!rdev->bb_page) {
1723 rdev->bb_page = alloc_page(GFP_KERNEL);
1724 if (!rdev->bb_page)
1725 return -ENOMEM;
1726 }
1727 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1728 rdev->badblocks.count == 0) {
1729 /* need to load the bad block list.
1730 * Currently we limit it to one page.
1731 */
1732 s32 offset;
1733 sector_t bb_sector;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001734 __le64 *bbp;
NeilBrown2699b672011-07-28 11:31:47 +10001735 int i;
1736 int sectors = le16_to_cpu(sb->bblog_size);
1737 if (sectors > (PAGE_SIZE / 512))
1738 return -EINVAL;
1739 offset = le32_to_cpu(sb->bblog_offset);
1740 if (offset == 0)
1741 return -EINVAL;
1742 bb_sector = (long long)offset;
1743 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001744 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001745 return -EIO;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001746 bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10001747 rdev->badblocks.shift = sb->bblog_shift;
1748 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1749 u64 bb = le64_to_cpu(*bbp);
1750 int count = bb & (0x3ff);
1751 u64 sector = bb >> 10;
1752 sector <<= sb->bblog_shift;
1753 count <<= sb->bblog_shift;
1754 if (bb + 1 == 0)
1755 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001756 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001757 return -EINVAL;
1758 }
NeilBrown486adf72013-04-24 11:42:44 +10001759 } else if (sb->bblog_offset != 0)
1760 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001761
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001762 if ((le32_to_cpu(sb->feature_map) &
1763 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001764 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1765 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1766 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1767 }
1768
NeilBrown33f2c352019-09-09 16:52:29 +10001769 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT) &&
1770 sb->level != 0)
1771 return -EINVAL;
1772
Yufen Yu228fc7d2019-10-30 18:47:02 +08001773 /* not spare disk, or LEVEL_MULTIPATH */
1774 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH) ||
1775 (rdev->desc_nr >= 0 &&
1776 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1777 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1778 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)))
1779 spare_disk = false;
Yufen Yu6a5cb532019-10-16 16:00:03 +08001780
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001781 if (!refdev) {
Yufen Yu228fc7d2019-10-30 18:47:02 +08001782 if (!spare_disk)
Yufen Yu6a5cb532019-10-16 16:00:03 +08001783 ret = 1;
1784 else
1785 ret = 0;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001786 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001788 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1791 sb->level != refsb->level ||
1792 sb->layout != refsb->layout ||
1793 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001794 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 bdevname(rdev->bdev,b),
1796 bdevname(refdev->bdev,b2));
1797 return -EINVAL;
1798 }
1799 ev1 = le64_to_cpu(sb->events);
1800 ev2 = le64_to_cpu(refsb->events);
1801
Yufen Yu228fc7d2019-10-30 18:47:02 +08001802 if (!spare_disk && ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001803 ret = 1;
1804 else
1805 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
NeilBrownc6563a82012-05-21 09:27:00 +10001807 if (minor_version) {
1808 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1809 sectors -= rdev->data_offset;
1810 } else
1811 sectors = rdev->sb_start;
1812 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001814 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001815 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
NeilBrownfd01b882011-10-11 16:47:53 +11001818static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001820 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001821 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
NeilBrown41158c72005-06-21 17:17:25 -07001823 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001824 clear_bit(Faulty, &rdev->flags);
1825 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001826 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001827 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (mddev->raid_disks == 0) {
1830 mddev->major_version = 1;
1831 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001832 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001833 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001834 mddev->ctime = le64_to_cpu(sb->ctime);
1835 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001837 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 mddev->layout = le32_to_cpu(sb->layout);
1839 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001840 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001841 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001842 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001843 mddev->bitmap_info.space = 0;
1844 /* Default location for bitmap is 1K after superblock
1845 * using 3K - total of 4K
1846 */
NeilBrownc3d97142009-12-14 12:49:52 +11001847 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001848 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001849 mddev->reshape_backwards = 0;
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1852 memcpy(mddev->uuid, sb->set_uuid, 16);
1853
1854 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001855
NeilBrown71c08052005-09-09 16:23:51 -07001856 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001857 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001858 mddev->bitmap_info.offset =
1859 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001860 /* Metadata doesn't record how much space is available.
1861 * For 1.0, we assume we can use up to the superblock
1862 * if before, else to 4K beyond superblock.
1863 * For others, assume no change is possible.
1864 */
1865 if (mddev->minor_version > 0)
1866 mddev->bitmap_info.space = 0;
1867 else if (mddev->bitmap_info.offset > 0)
1868 mddev->bitmap_info.space =
1869 8 - mddev->bitmap_info.offset;
1870 else
1871 mddev->bitmap_info.space =
1872 -mddev->bitmap_info.offset;
1873 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001874
NeilBrownf6705572006-03-27 01:18:11 -08001875 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1876 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1877 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1878 mddev->new_level = le32_to_cpu(sb->new_level);
1879 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001880 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001881 if (mddev->delta_disks < 0 ||
1882 (mddev->delta_disks == 0 &&
1883 (le32_to_cpu(sb->feature_map)
1884 & MD_FEATURE_RESHAPE_BACKWARDS)))
1885 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001886 } else {
1887 mddev->reshape_position = MaxSector;
1888 mddev->delta_disks = 0;
1889 mddev->new_level = mddev->level;
1890 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001891 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001892 }
1893
NeilBrown33f2c352019-09-09 16:52:29 +10001894 if (mddev->level == 0 &&
1895 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT))
1896 mddev->layout = -1;
1897
Song Liu486b0f72016-08-19 15:34:01 -07001898 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001899 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001900
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001901 if (le32_to_cpu(sb->feature_map) &
1902 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001903 if (le32_to_cpu(sb->feature_map) &
1904 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1905 return -EINVAL;
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001906 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1907 (le32_to_cpu(sb->feature_map) &
1908 MD_FEATURE_MULTIPLE_PPLS))
1909 return -EINVAL;
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001910 set_bit(MD_HAS_PPL, &mddev->flags);
1911 }
NeilBrown41158c72005-06-21 17:17:25 -07001912 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001913 /* Insist of good event counter while assembling, except for
1914 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001916 if (rdev->desc_nr >= 0 &&
1917 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001918 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1919 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001920 if (ev1 < mddev->events)
1921 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001922 } else if (mddev->bitmap) {
1923 /* If adding to array with a bitmap, then we can accept an
1924 * older device, but not too old.
1925 */
NeilBrown41158c72005-06-21 17:17:25 -07001926 if (ev1 < mddev->bitmap->events_cleared)
1927 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001928 if (ev1 < mddev->events)
1929 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001930 } else {
1931 if (ev1 < mddev->events)
1932 /* just a hot-add of a new device, leave raid_disk at -1 */
1933 return 0;
1934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (mddev->level != LEVEL_MULTIPATH) {
1936 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001937 if (rdev->desc_nr < 0 ||
1938 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001939 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001940 rdev->desc_nr = -1;
1941 } else
1942 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001944 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001946 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001947 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 break;
Song Liubac624f2015-08-13 14:31:55 -07001949 case MD_DISK_ROLE_JOURNAL: /* journal device */
1950 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1951 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001952 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001953 return -EINVAL;
1954 }
1955 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001956 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001957 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001958 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 default:
NeilBrownf4667222013-12-09 12:04:56 +11001960 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001961 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001962 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001963 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001964 if (!(le32_to_cpu(sb->feature_map) &
1965 MD_FEATURE_RECOVERY_BITMAP))
1966 rdev->saved_raid_disk = -1;
Guoqing Jiang062f5b2a2019-07-24 11:09:20 +02001967 } else {
1968 /*
1969 * If the array is FROZEN, then the device can't
1970 * be in_sync with rest of array.
1971 */
1972 if (!test_bit(MD_RECOVERY_FROZEN,
1973 &mddev->recovery))
1974 set_bit(In_sync, &rdev->flags);
1975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 rdev->raid_disk = role;
1977 break;
1978 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001979 if (sb->devflags & WriteMostly1)
1980 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001981 if (sb->devflags & FailFast1)
1982 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001983 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1984 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001985 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001986 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return 0;
1989}
1990
NeilBrownfd01b882011-10-11 16:47:53 +11001991static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001994 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 int max_dev, i;
1996 /* make rdev->sb match mddev and rdev data. */
1997
Namhyung Kim65a06f062011-07-27 11:00:36 +10001998 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 sb->feature_map = 0;
2001 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002002 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 memset(sb->pad3, 0, sizeof(sb->pad3));
2004
2005 sb->utime = cpu_to_le64((__u64)mddev->utime);
2006 sb->events = cpu_to_le64(mddev->events);
2007 if (mddev->in_sync)
2008 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07002009 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
2010 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 else
2012 sb->resync_offset = cpu_to_le64(0);
2013
NeilBrown1c05b4b2006-10-21 10:24:08 -07002014 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08002015
NeilBrownf0ca3402006-02-02 14:28:04 -08002016 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11002017 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10002018 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10002019 sb->level = cpu_to_le32(mddev->level);
2020 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11002021 if (test_bit(FailFast, &rdev->flags))
2022 sb->devflags |= FailFast1;
2023 else
2024 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08002025
NeilBrownaeb9b2112011-08-25 14:43:08 +10002026 if (test_bit(WriteMostly, &rdev->flags))
2027 sb->devflags |= WriteMostly1;
2028 else
2029 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10002030 sb->data_offset = cpu_to_le64(rdev->data_offset);
2031 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10002032
NeilBrownc3d97142009-12-14 12:49:52 +11002033 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
2034 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07002035 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07002036 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002037
Shaohua Lif2076e72015-10-08 21:54:12 -07002038 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11002039 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11002040 sb->feature_map |=
2041 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
2042 sb->recovery_offset =
2043 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11002044 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
2045 sb->feature_map |=
2046 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002047 }
Shaohua Li3069aa82015-08-13 14:31:56 -07002048 /* Note: recovery_offset and journal_tail share space */
2049 if (test_bit(Journal, &rdev->flags))
2050 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002051 if (test_bit(Replacement, &rdev->flags))
2052 sb->feature_map |=
2053 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002054
NeilBrownf6705572006-03-27 01:18:11 -08002055 if (mddev->reshape_position != MaxSector) {
2056 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
2057 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
2058 sb->new_layout = cpu_to_le32(mddev->new_layout);
2059 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
2060 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10002061 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10002062 if (mddev->delta_disks == 0 &&
2063 mddev->reshape_backwards)
2064 sb->feature_map
2065 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10002066 if (rdev->new_data_offset != rdev->data_offset) {
2067 sb->feature_map
2068 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
2069 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
2070 - rdev->data_offset));
2071 }
NeilBrownf6705572006-03-27 01:18:11 -08002072 }
NeilBrowna654b9d82005-06-21 17:17:27 -07002073
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10002074 if (mddev_is_clustered(mddev))
2075 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
2076
NeilBrown2699b672011-07-28 11:31:47 +10002077 if (rdev->badblocks.count == 0)
2078 /* Nothing to do for bad blocks*/ ;
2079 else if (sb->bblog_offset == 0)
2080 /* Cannot record bad blocks on this device */
2081 md_error(mddev, rdev);
2082 else {
2083 struct badblocks *bb = &rdev->badblocks;
Christoph Hellwigae506402019-04-04 18:56:13 +02002084 __le64 *bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10002085 u64 *p = bb->page;
2086 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
2087 if (bb->changed) {
2088 unsigned seq;
2089
2090retry:
2091 seq = read_seqbegin(&bb->lock);
2092
2093 memset(bbp, 0xff, PAGE_SIZE);
2094
2095 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08002096 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10002097 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
2098 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08002099 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10002100 }
NeilBrownd0962932012-03-19 12:46:41 +11002101 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002102 if (read_seqretry(&bb->lock, seq))
2103 goto retry;
2104
2105 bb->sector = (rdev->sb_start +
2106 (int)le32_to_cpu(sb->bblog_offset));
2107 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10002108 }
2109 }
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11002112 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (rdev2->desc_nr+1 > max_dev)
2114 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07002115
NeilBrown70471da2009-08-03 10:59:57 +10002116 if (max_dev > le32_to_cpu(sb->max_dev)) {
2117 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07002118 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10002119 rdev->sb_size = max_dev * 2 + 256;
2120 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
2121 if (rdev->sb_size & bmask)
2122 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10002123 } else
2124 max_dev = le32_to_cpu(sb->max_dev);
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 for (i=0; i<max_dev;i++)
Lidong Zhong8df72022017-06-12 10:45:55 +08002127 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
NeilBrownf72ffdd2014-09-30 14:23:59 +10002128
Song Liua97b7892015-10-08 21:54:09 -07002129 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
2130 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01002132 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
Pawel Baldysiakddc08822017-08-16 17:13:45 +02002133 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
2134 sb->feature_map |=
2135 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
2136 else
2137 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01002138 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
2139 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
2140 }
2141
NeilBrowndafb20f2012-03-19 12:46:39 +11002142 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002144 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07002145 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08002146 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07002148 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07002149 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11002150 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002151 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 else
Song Liuc4d4c912015-08-13 14:31:54 -07002153 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 }
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 sb->sb_csum = calc_sb_1_csum(sb);
2157}
2158
Chris Webb0cd17fe2008-06-28 08:31:46 +10002159static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11002160super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002161{
2162 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002163 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11002164 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002165 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10002166 if (rdev->data_offset != rdev->new_data_offset)
2167 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10002168 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10002169 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01002170 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002171 max_sectors -= rdev->data_offset;
2172 if (!num_sectors || num_sectors > max_sectors)
2173 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11002174 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10002175 /* minor version 0 with bitmap we can't move */
2176 return 0;
2177 } else {
2178 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10002179 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01002180 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10002181 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11002182 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002183 if (!num_sectors || num_sectors > max_sectors)
2184 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10002185 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002186 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10002187 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10002188 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08002189 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10002190 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11002191 do {
2192 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
2193 rdev->sb_page);
2194 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11002195 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10002196
2197}
2198
2199static int
2200super_1_allow_new_offset(struct md_rdev *rdev,
2201 unsigned long long new_offset)
2202{
2203 /* All necessary checks on new >= old have been done */
2204 struct bitmap *bitmap;
2205 if (new_offset >= rdev->data_offset)
2206 return 1;
2207
2208 /* with 1.0 metadata, there is no metadata to tread on
2209 * so we can always move back */
2210 if (rdev->mddev->minor_version == 0)
2211 return 1;
2212
2213 /* otherwise we must be sure not to step on
2214 * any metadata, so stay:
2215 * 36K beyond start of superblock
2216 * beyond end of badblocks
2217 * beyond write-intent bitmap
2218 */
2219 if (rdev->sb_start + (32+4)*2 > new_offset)
2220 return 0;
2221 bitmap = rdev->mddev->bitmap;
2222 if (bitmap && !rdev->mddev->bitmap_info.file &&
2223 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10002224 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10002225 return 0;
2226 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2227 return 0;
2228
2229 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002230}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Adrian Bunk75c96f82005-05-05 16:16:09 -07002232static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 [0] = {
2234 .name = "0.90.0",
2235 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002236 .load_super = super_90_load,
2237 .validate_super = super_90_validate,
2238 .sync_super = super_90_sync,
2239 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002240 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 },
2242 [1] = {
2243 .name = "md-1",
2244 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002245 .load_super = super_1_load,
2246 .validate_super = super_1_validate,
2247 .sync_super = super_1_sync,
2248 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002249 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 },
2251};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
NeilBrownfd01b882011-10-11 16:47:53 +11002253static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05002254{
2255 if (mddev->sync_super) {
2256 mddev->sync_super(mddev, rdev);
2257 return;
2258 }
2259
2260 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2261
2262 super_types[mddev->major_version].sync_super(mddev, rdev);
2263}
2264
NeilBrownfd01b882011-10-11 16:47:53 +11002265static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
NeilBrown3cb03002011-10-11 16:45:26 +11002267 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
NeilBrown4b809912008-07-21 17:05:25 +10002269 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07002270 rdev_for_each_rcu(rdev, mddev1) {
2271 if (test_bit(Faulty, &rdev->flags) ||
2272 test_bit(Journal, &rdev->flags) ||
2273 rdev->raid_disk == -1)
2274 continue;
2275 rdev_for_each_rcu(rdev2, mddev2) {
2276 if (test_bit(Faulty, &rdev2->flags) ||
2277 test_bit(Journal, &rdev2->flags) ||
2278 rdev2->raid_disk == -1)
2279 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002280 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002281 rdev2->bdev->bd_contains) {
2282 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002283 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002284 }
Song Liu0b020e82015-09-03 23:00:35 -07002285 }
2286 }
NeilBrown4b809912008-07-21 17:05:25 +10002287 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 return 0;
2289}
2290
2291static LIST_HEAD(pending_raid_disks);
2292
Andre Nollac5e7112009-08-03 10:59:47 +10002293/*
2294 * Try to register data integrity profile for an mddev
2295 *
2296 * This is called when an array is started and after a disk has been kicked
2297 * from the array. It only succeeds if all working and active component devices
2298 * are integrity capable with matching profiles.
2299 */
NeilBrownfd01b882011-10-11 16:47:53 +11002300int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002301{
NeilBrown3cb03002011-10-11 16:45:26 +11002302 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002303
Andre Nollac5e7112009-08-03 10:59:47 +10002304 if (list_empty(&mddev->disks))
2305 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002306 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2307 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002308 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002309 /* skip spares and non-functional disks */
2310 if (test_bit(Faulty, &rdev->flags))
2311 continue;
2312 if (rdev->raid_disk < 0)
2313 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002314 if (!reference) {
2315 /* Use the first rdev as the reference */
2316 reference = rdev;
2317 continue;
2318 }
2319 /* does this rdev's profile match the reference profile? */
2320 if (blk_integrity_compare(reference->bdev->bd_disk,
2321 rdev->bdev->bd_disk) < 0)
2322 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002323 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002324 if (!reference || !bdev_get_integrity(reference->bdev))
2325 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002326 /*
2327 * All component devices are integrity capable and have matching
2328 * profiles, register the common profile for the md device.
2329 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002330 blk_integrity_register(mddev->gendisk,
2331 bdev_get_integrity(reference->bdev));
2332
NeilBrown9d487392016-11-02 14:16:49 +11002333 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Kent Overstreetafeee512018-05-20 18:25:52 -04002334 if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002335 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002336 mdname(mddev));
2337 return -EINVAL;
2338 }
Andre Nollac5e7112009-08-03 10:59:47 +10002339 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002340}
Andre Nollac5e7112009-08-03 10:59:47 +10002341EXPORT_SYMBOL(md_integrity_register);
2342
Dan Williams1501efa2016-01-13 16:00:07 -08002343/*
2344 * Attempt to add an rdev, but only if it is consistent with the current
2345 * integrity profile
2346 */
2347int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002348{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002349 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002350 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002351
2352 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002353 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002354
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002355 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002356
2357 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002358 return 0;
2359
2360 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002361 pr_err("%s: incompatible integrity profile for %s\n",
2362 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002363 return -ENXIO;
2364 }
2365
2366 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002367}
2368EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002369
NeilBrownf72ffdd2014-09-30 14:23:59 +10002370static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002372 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002373 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002374 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Dan Williams11e2ede2008-04-30 00:52:32 -07002376 /* prevent duplicates */
2377 if (find_rdev(mddev, rdev->bdev->bd_dev))
2378 return -EEXIST;
2379
NeilBrown97b20ef2017-04-13 08:53:48 +10002380 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2381 mddev->pers)
2382 return -EROFS;
2383
Andre Nolldd8ac332009-03-31 14:33:13 +11002384 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002385 if (!test_bit(Journal, &rdev->flags) &&
2386 rdev->sectors &&
2387 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002388 if (mddev->pers) {
2389 /* Cannot change size, so fail
2390 * If mddev->level <= 0, then we don't care
2391 * about aligning sizes (e.g. linear)
2392 */
2393 if (mddev->level > 0)
2394 return -ENOSPC;
2395 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002396 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
2399 /* Verify rdev->desc_nr is unique.
2400 * If it is -1, assign a free number, else
2401 * check number is not in use
2402 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002403 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (rdev->desc_nr < 0) {
2405 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002406 if (mddev->pers)
2407 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002408 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 choice++;
2410 rdev->desc_nr = choice;
2411 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002412 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002413 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002417 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002418 if (!test_bit(Journal, &rdev->flags) &&
2419 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002420 pr_warn("md: %s: array is limited to %d devices\n",
2421 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002422 return -EBUSY;
2423 }
NeilBrown19133a42005-11-08 21:39:35 -08002424 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002425 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002428 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002429
Guoqing Jiang963c5552019-06-14 17:10:36 +08002430 if (mddev->raid_disks)
Guoqing Jiang404659c2019-12-23 10:48:53 +01002431 mddev_create_serial_pool(mddev, rdev, false);
Guoqing Jiang963c5552019-06-14 17:10:36 +08002432
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002433 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002434 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002435
Tejun Heo0762b8b2008-08-25 19:56:12 +09002436 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002437 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2438 /* failure here is OK */;
2439 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002440
NeilBrown4b809912008-07-21 17:05:25 +10002441 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002442 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002443
2444 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002445 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002448
2449 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002450 pr_warn("md: failed to register dev-%s for %s\n",
2451 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002452 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
NeilBrown177a99b2008-02-06 01:39:56 -08002455static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002456{
NeilBrown3cb03002011-10-11 16:45:26 +11002457 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002458 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002459 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002460}
2461
NeilBrownf72ffdd2014-09-30 14:23:59 +10002462static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
2464 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002465
Tejun Heo49731ba2011-01-14 18:43:57 +01002466 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002467 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002468 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +01002469 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002471 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002472 sysfs_put(rdev->sysfs_state);
2473 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002474 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002475 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002476 * writing to 'remove' to "dev/state". We also need
2477 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002478 */
NeilBrown4b809912008-07-21 17:05:25 +10002479 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002480 INIT_WORK(&rdev->del_work, md_delayed_delete);
2481 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002482 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484
2485/*
2486 * prevent the device from being mounted, repartitioned or
2487 * otherwise reused by a RAID array (or any other kernel
2488 * subsystem), by bd_claiming the device.
2489 */
NeilBrown3cb03002011-10-11 16:45:26 +11002490static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
2492 int err = 0;
2493 struct block_device *bdev;
2494 char b[BDEVNAME_SIZE];
2495
Tejun Heod4d77622010-11-13 11:55:18 +01002496 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002497 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 if (IS_ERR(bdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11002499 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return PTR_ERR(bdev);
2501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 rdev->bdev = bdev;
2503 return err;
2504}
2505
NeilBrown3cb03002011-10-11 16:45:26 +11002506static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
2508 struct block_device *bdev = rdev->bdev;
2509 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002510 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511}
2512
2513void md_autodetect_dev(dev_t dev);
2514
NeilBrownf72ffdd2014-09-30 14:23:59 +10002515static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
2517 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002518
NeilBrown9d487392016-11-02 14:16:49 +11002519 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002520 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002522 if (test_bit(AutoDetected, &rdev->flags))
2523 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524#endif
2525 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002526 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
2528
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002529void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
2531 unbind_rdev_from_array(rdev);
2532 export_rdev(rdev);
2533}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002534EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
NeilBrownfd01b882011-10-11 16:47:53 +11002536static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
NeilBrown0638bb02014-09-25 17:43:47 +10002538 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
NeilBrown0638bb02014-09-25 17:43:47 +10002540 while (!list_empty(&mddev->disks)) {
2541 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2542 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002543 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 mddev->raid_disks = 0;
2546 mddev->major_version = 0;
2547}
2548
NeilBrown6497709b2017-03-15 14:05:14 +11002549static bool set_in_sync(struct mddev *mddev)
2550{
Shaohua Liefa4b772017-10-18 22:08:13 -07002551 lockdep_assert_held(&mddev->lock);
NeilBrown4ad23a972017-03-15 14:05:14 +11002552 if (!mddev->in_sync) {
2553 mddev->sync_checkers++;
2554 spin_unlock(&mddev->lock);
2555 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2556 spin_lock(&mddev->lock);
2557 if (!mddev->in_sync &&
2558 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002559 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002560 /*
2561 * Ensure ->in_sync is visible before we clear
2562 * ->sync_checkers.
2563 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002564 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002565 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2566 sysfs_notify_dirent_safe(mddev->sysfs_state);
2567 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002568 if (--mddev->sync_checkers == 0)
2569 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002570 }
2571 if (mddev->safemode == 1)
2572 mddev->safemode = 0;
2573 return mddev->in_sync;
2574}
2575
NeilBrownf72ffdd2014-09-30 14:23:59 +10002576static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
NeilBrown42543762006-06-26 00:27:57 -07002578 /* Update each superblock (in-memory image), but
2579 * if we are allowed to, skip spares which already
2580 * have the right event counter, or have one earlier
2581 * (which would mean they aren't being marked as dirty
2582 * with the rest of the array)
2583 */
NeilBrown3cb03002011-10-11 16:45:26 +11002584 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002585 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002586 if (rdev->sb_events == mddev->events ||
2587 (nospares &&
2588 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002589 rdev->sb_events+1 == mddev->events)) {
2590 /* Don't update this superblock */
2591 rdev->sb_loaded = 2;
2592 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002593 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002594 rdev->sb_loaded = 1;
2595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 }
2597}
2598
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002599static bool does_sb_need_changing(struct mddev *mddev)
2600{
2601 struct md_rdev *rdev;
2602 struct mdp_superblock_1 *sb;
2603 int role;
2604
2605 /* Find a good rdev */
2606 rdev_for_each(rdev, mddev)
2607 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2608 break;
2609
2610 /* No good device found. */
2611 if (!rdev)
2612 return false;
2613
2614 sb = page_address(rdev->sb_page);
2615 /* Check if a device has become faulty or a spare become active */
2616 rdev_for_each(rdev, mddev) {
2617 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2618 /* Device activated? */
2619 if (role == 0xffff && rdev->raid_disk >=0 &&
2620 !test_bit(Faulty, &rdev->flags))
2621 return true;
2622 /* Device turned faulty? */
2623 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2624 return true;
2625 }
2626
2627 /* Check if any mddev parameters have changed */
2628 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2629 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002630 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002631 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2632 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2633 return true;
2634
2635 return false;
2636}
2637
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002638void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
NeilBrown3cb03002011-10-11 16:45:26 +11002640 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002641 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002642 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002643 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002644 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
NeilBrownd87f0642013-04-24 11:42:40 +10002646 if (mddev->ro) {
2647 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002648 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002649 return;
2650 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002651
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002652repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002653 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002654 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002655 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002656 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002657 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002658 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002659 /* Has someone else has updated the sb */
2660 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002661 if (ret == 0)
2662 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002663 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2664 BIT(MD_SB_CHANGE_DEVS) |
2665 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002666 return;
2667 }
2668 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002669
NeilBrowndb0505d2017-10-17 16:18:36 +11002670 /*
2671 * First make sure individual recovery_offsets are correct
2672 * curr_resync_completed can only be used during recovery.
2673 * During reshape/resync it might use array-addresses rather
2674 * that device addresses.
2675 */
NeilBrowndafb20f2012-03-19 12:46:39 +11002676 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002677 if (rdev->raid_disk >= 0 &&
2678 mddev->delta_disks >= 0 &&
NeilBrowndb0505d2017-10-17 16:18:36 +11002679 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2680 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2681 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002682 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002683 !test_bit(In_sync, &rdev->flags) &&
2684 mddev->curr_resync_completed > rdev->recovery_offset)
2685 rdev->recovery_offset = mddev->curr_resync_completed;
2686
NeilBrownf72ffdd2014-09-30 14:23:59 +10002687 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002688 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002689 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2690 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002691 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002692 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002693 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002694 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002695 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002696 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002697 md_error(mddev, rdev);
2698 }
2699 clear_bit(Blocked, &rdev->flags);
2700 clear_bit(BlockedBadBlocks, &rdev->flags);
2701 wake_up(&rdev->blocked_wait);
2702 }
2703 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002704 wake_up(&mddev->sb_wait);
2705 return;
2706 }
2707
NeilBrown85572d72014-12-15 12:56:56 +11002708 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002709
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002710 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002711
Shaohua Li29530792016-12-08 15:48:19 -08002712 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002713 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002714 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002715 /* just a clean<-> dirty transition, possibly leave spares alone,
2716 * though if events isn't the right even/odd, we will have to do
2717 * spares after all
2718 */
2719 nospares = 1;
2720 if (force_change)
2721 nospares = 0;
2722 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002723 /* If the array is degraded, then skipping spares is both
2724 * dangerous and fairly pointless.
2725 * Dangerous because a device that was removed from the array
2726 * might have a event_count that still looks up-to-date,
2727 * so it can be re-added without a resync.
2728 * Pointless because if there are any spares to skip,
2729 * then a recovery will happen and soon that array won't
2730 * be degraded any more and the spare can go back to sleep then.
2731 */
NeilBrown850b2b422006-10-03 01:15:46 -07002732 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002733
NeilBrown06d91a52005-06-21 17:17:12 -07002734 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002735
2736 /* If this is just a dirty<->clean transition, and the array is clean
2737 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002738 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002739 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002740 && mddev->can_decrease_events
2741 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002742 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002743 mddev->can_decrease_events = 0;
2744 } else {
NeilBrown42543762006-06-26 00:27:57 -07002745 /* otherwise we have to go forward and ... */
2746 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002747 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
NeilBrown403df472014-09-30 15:52:29 +10002750 /*
2751 * This 64-bit counter should never wrap.
2752 * Either we are in around ~1 trillion A.C., assuming
2753 * 1 reboot per second, or we have a bug...
2754 */
2755 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002756
NeilBrowndafb20f2012-03-19 12:46:39 +11002757 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002758 if (rdev->badblocks.changed)
2759 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002760 if (test_bit(Faulty, &rdev->flags))
2761 set_bit(FaultRecorded, &rdev->flags);
2762 }
NeilBrown2699b672011-07-28 11:31:47 +10002763
NeilBrowne6910632008-02-06 01:39:51 -08002764 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002765 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
NeilBrown36a4e1f2011-10-07 14:23:17 +11002767 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2768 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
Shaohua Li504634f2016-11-18 09:44:08 -08002770 if (mddev->queue)
2771 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002772rewrite:
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002773 md_bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002774 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002776
NeilBrown42543762006-06-26 00:27:57 -07002777 if (rdev->sb_loaded != 1)
2778 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
NeilBrownf4667222013-12-09 12:04:56 +11002780 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002781 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002782 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002783 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002784 pr_debug("md: (write) %s's sb offset: %llu\n",
2785 bdevname(rdev->bdev, b),
2786 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002787 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002788 if (rdev->badblocks.size) {
2789 md_super_write(mddev, rdev,
2790 rdev->badblocks.sector,
2791 rdev->badblocks.size << 9,
2792 rdev->bb_page);
2793 rdev->badblocks.size = 0;
2794 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002795
NeilBrownf4667222013-12-09 12:04:56 +11002796 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002797 pr_debug("md: %s (skipping faulty)\n",
2798 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002799
NeilBrown7bfa19f2005-06-21 17:17:28 -07002800 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 /* only need to write one superblock... */
2802 break;
2803 }
NeilBrown46533ff2016-11-18 16:16:11 +11002804 if (md_super_wait(mddev) < 0)
2805 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002806 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002807
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002808 if (mddev_is_clustered(mddev) && ret == 0)
2809 md_cluster_ops->metadata_update_finish(mddev);
2810
NeilBrown850b2b422006-10-03 01:15:46 -07002811 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002812 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2813 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002814 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002815 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002816 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002817 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2818 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002819
NeilBrowndafb20f2012-03-19 12:46:39 +11002820 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002821 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2822 clear_bit(Blocked, &rdev->flags);
2823
2824 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002825 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002826 clear_bit(BlockedBadBlocks, &rdev->flags);
2827 wake_up(&rdev->blocked_wait);
2828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002830EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002832static int add_bound_rdev(struct md_rdev *rdev)
2833{
2834 struct mddev *mddev = rdev->mddev;
2835 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002836 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002837
Shaohua Li87d4d912016-01-06 14:37:14 -08002838 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002839 /* If there is hot_add_disk but no hot_remove_disk
2840 * then added disks for geometry changes,
2841 * and should be added immediately.
2842 */
2843 super_types[mddev->major_version].
2844 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002845 if (add_journal)
2846 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002847 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002848 if (add_journal)
2849 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002850 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002851 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002852 return err;
2853 }
2854 }
2855 sysfs_notify_dirent_safe(rdev->sysfs_state);
2856
Shaohua Li29530792016-12-08 15:48:19 -08002857 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002858 if (mddev->degraded)
2859 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2860 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2861 md_new_event(mddev);
2862 md_wakeup_thread(mddev->thread);
2863 return 0;
2864}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Andre Noll7f6ce762008-03-23 18:34:54 +01002866/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002867 * We want to accept with case. For this we use cmd_match.
2868 */
2869static int cmd_match(const char *cmd, const char *str)
2870{
2871 /* See if cmd, written into a sysfs file, matches
2872 * str. They must either be the same, or cmd can
2873 * have a trailing newline
2874 */
2875 while (*cmd && *str && *cmd == *str) {
2876 cmd++;
2877 str++;
2878 }
2879 if (*cmd == '\n')
2880 cmd++;
2881 if (*str || *cmd)
2882 return 0;
2883 return 1;
2884}
2885
NeilBrown86e6ffd2005-11-08 21:39:24 -08002886struct rdev_sysfs_entry {
2887 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002888 ssize_t (*show)(struct md_rdev *, char *);
2889 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002890};
2891
2892static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002893state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002894{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002895 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002896 size_t len = 0;
Mark Rutland6aa7de02017-10-23 14:07:29 -07002897 unsigned long flags = READ_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002898
NeilBrown758bfc82014-12-15 12:56:59 +11002899 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002900 (!test_bit(ExternalBbl, &flags) &&
2901 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002902 len += sprintf(page+len, "faulty%s", sep);
2903 if (test_bit(In_sync, &flags))
2904 len += sprintf(page+len, "in_sync%s", sep);
2905 if (test_bit(Journal, &flags))
2906 len += sprintf(page+len, "journal%s", sep);
2907 if (test_bit(WriteMostly, &flags))
2908 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002909 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002910 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002911 && !test_bit(Faulty, &flags)))
2912 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002913 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002914 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002915 !test_bit(In_sync, &flags))
2916 len += sprintf(page+len, "spare%s", sep);
2917 if (test_bit(WriteErrorSeen, &flags))
2918 len += sprintf(page+len, "write_error%s", sep);
2919 if (test_bit(WantReplacement, &flags))
2920 len += sprintf(page+len, "want_replacement%s", sep);
2921 if (test_bit(Replacement, &flags))
2922 len += sprintf(page+len, "replacement%s", sep);
2923 if (test_bit(ExternalBbl, &flags))
2924 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002925 if (test_bit(FailFast, &flags))
2926 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002927
2928 if (len)
2929 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002930
NeilBrown86e6ffd2005-11-08 21:39:24 -08002931 return len+sprintf(page+len, "\n");
2932}
2933
NeilBrown45dc2de2006-06-26 00:27:58 -07002934static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002935state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002936{
2937 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002938 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002939 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002940 * writemostly - sets write_mostly
2941 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002942 * blocked - sets the Blocked flags
2943 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002944 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002945 * -insync - clear Insync for a device with a slot assigned,
2946 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002947 * write_error - sets WriteErrorSeen
2948 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002949 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002950 */
2951 int err = -EINVAL;
2952 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2953 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002954 if (test_bit(Faulty, &rdev->flags))
2955 err = 0;
2956 else
2957 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002958 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002959 if (rdev->mddev->pers) {
2960 clear_bit(Blocked, &rdev->flags);
2961 remove_and_add_spares(rdev->mddev, rdev);
2962 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002963 if (rdev->raid_disk >= 0)
2964 err = -EBUSY;
2965 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002966 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002967 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002968 if (mddev_is_clustered(mddev))
2969 err = md_cluster_ops->remove_disk(mddev, rdev);
2970
2971 if (err == 0) {
2972 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002973 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08002974 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11002975 md_wakeup_thread(mddev->thread);
2976 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002977 md_new_event(mddev);
2978 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002979 }
NeilBrownf6556752006-06-26 00:28:01 -07002980 } else if (cmd_match(buf, "writemostly")) {
2981 set_bit(WriteMostly, &rdev->flags);
Guoqing Jiang404659c2019-12-23 10:48:53 +01002982 mddev_create_serial_pool(rdev->mddev, rdev, false);
NeilBrownf6556752006-06-26 00:28:01 -07002983 err = 0;
2984 } else if (cmd_match(buf, "-writemostly")) {
Guoqing Jiang11d3a9f2019-12-23 10:48:55 +01002985 mddev_destroy_serial_pool(rdev->mddev, rdev, false);
NeilBrownf6556752006-06-26 00:28:01 -07002986 clear_bit(WriteMostly, &rdev->flags);
2987 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002988 } else if (cmd_match(buf, "blocked")) {
2989 set_bit(Blocked, &rdev->flags);
2990 err = 0;
2991 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002992 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002993 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002994 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002995 /* metadata handler doesn't understand badblocks,
2996 * so we need to fail the device
2997 */
2998 md_error(rdev->mddev, rdev);
2999 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003000 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10003001 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003002 wake_up(&rdev->blocked_wait);
3003 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3004 md_wakeup_thread(rdev->mddev->thread);
3005
3006 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10003007 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
3008 set_bit(In_sync, &rdev->flags);
3009 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11003010 } else if (cmd_match(buf, "failfast")) {
3011 set_bit(FailFast, &rdev->flags);
3012 err = 0;
3013 } else if (cmd_match(buf, "-failfast")) {
3014 clear_bit(FailFast, &rdev->flags);
3015 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07003016 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
3017 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10003018 if (rdev->mddev->pers == NULL) {
3019 clear_bit(In_sync, &rdev->flags);
3020 rdev->saved_raid_disk = rdev->raid_disk;
3021 rdev->raid_disk = -1;
3022 err = 0;
3023 }
NeilBrownd7a9d442011-07-28 11:31:48 +10003024 } else if (cmd_match(buf, "write_error")) {
3025 set_bit(WriteErrorSeen, &rdev->flags);
3026 err = 0;
3027 } else if (cmd_match(buf, "-write_error")) {
3028 clear_bit(WriteErrorSeen, &rdev->flags);
3029 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11003030 } else if (cmd_match(buf, "want_replacement")) {
3031 /* Any non-spare device that is not a replacement can
3032 * become want_replacement at any time, but we then need to
3033 * check if recovery is needed.
3034 */
3035 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07003036 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11003037 !test_bit(Replacement, &rdev->flags))
3038 set_bit(WantReplacement, &rdev->flags);
3039 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3040 md_wakeup_thread(rdev->mddev->thread);
3041 err = 0;
3042 } else if (cmd_match(buf, "-want_replacement")) {
3043 /* Clearing 'want_replacement' is always allowed.
3044 * Once replacements starts it is too late though.
3045 */
3046 err = 0;
3047 clear_bit(WantReplacement, &rdev->flags);
3048 } else if (cmd_match(buf, "replacement")) {
3049 /* Can only set a device as a replacement when array has not
3050 * yet been started. Once running, replacement is automatic
3051 * from spares, or by assigning 'slot'.
3052 */
3053 if (rdev->mddev->pers)
3054 err = -EBUSY;
3055 else {
3056 set_bit(Replacement, &rdev->flags);
3057 err = 0;
3058 }
3059 } else if (cmd_match(buf, "-replacement")) {
3060 /* Similarly, can only clear Replacement before start */
3061 if (rdev->mddev->pers)
3062 err = -EBUSY;
3063 else {
3064 clear_bit(Replacement, &rdev->flags);
3065 err = 0;
3066 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05003067 } else if (cmd_match(buf, "re-add")) {
Yufen Yuee37e622019-04-02 14:22:14 +08003068 if (!rdev->mddev->pers)
3069 err = -EINVAL;
3070 else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) &&
3071 rdev->saved_raid_disk >= 0) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05003072 /* clear_bit is performed _after_ all the devices
3073 * have their local Faulty bit cleared. If any writes
3074 * happen in the meantime in the local node, they
3075 * will land in the local bitmap, which will be synced
3076 * by this node eventually
3077 */
3078 if (!mddev_is_clustered(rdev->mddev) ||
3079 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
3080 clear_bit(Faulty, &rdev->flags);
3081 err = add_bound_rdev(rdev);
3082 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05003083 } else
3084 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02003085 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
3086 set_bit(ExternalBbl, &rdev->flags);
3087 rdev->badblocks.shift = 0;
3088 err = 0;
3089 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
3090 clear_bit(ExternalBbl, &rdev->flags);
3091 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07003092 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10003093 if (!err)
3094 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07003095 return err ? err : len;
3096}
NeilBrown80ca3a42006-07-10 04:44:18 -07003097static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10003098__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003099
3100static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003101errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08003102{
3103 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
3104}
3105
3106static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003107errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08003108{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003109 unsigned int n;
3110 int rv;
3111
3112 rv = kstrtouint(buf, 10, &n);
3113 if (rv < 0)
3114 return rv;
3115 atomic_set(&rdev->corrected_errors, n);
3116 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08003117}
3118static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07003119__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08003120
NeilBrown014236d2006-01-06 00:20:55 -08003121static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003122slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08003123{
Shaohua Lif2076e72015-10-08 21:54:12 -07003124 if (test_bit(Journal, &rdev->flags))
3125 return sprintf(page, "journal\n");
3126 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08003127 return sprintf(page, "none\n");
3128 else
3129 return sprintf(page, "%d\n", rdev->raid_disk);
3130}
3131
3132static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003133slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08003134{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003135 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08003136 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003137
Shaohua Lif2076e72015-10-08 21:54:12 -07003138 if (test_bit(Journal, &rdev->flags))
3139 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08003140 if (strncmp(buf, "none", 4)==0)
3141 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003142 else {
3143 err = kstrtouint(buf, 10, (unsigned int *)&slot);
3144 if (err < 0)
3145 return err;
3146 }
Neil Brown6c2fce22008-06-28 08:31:31 +10003147 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08003148 /* Setting 'slot' on an active array requires also
3149 * updating the 'rd%d' link, and communicating
3150 * with the personality with ->hot_*_disk.
3151 * For now we only support removing
3152 * failed/spare devices. This normally happens automatically,
3153 * but not when the metadata is externally managed.
3154 */
NeilBrownc303da62008-02-06 01:39:51 -08003155 if (rdev->raid_disk == -1)
3156 return -EEXIST;
3157 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10003158 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08003159 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10003160 clear_bit(Blocked, &rdev->flags);
3161 remove_and_add_spares(rdev->mddev, rdev);
3162 if (rdev->raid_disk >= 0)
3163 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08003164 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3165 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10003166 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10003167 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10003168 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10003169 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11003170 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10003171
3172 if (rdev->raid_disk != -1)
3173 return -EBUSY;
3174
NeilBrownc6751b22011-02-02 11:57:13 +11003175 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
3176 return -EBUSY;
3177
Neil Brown6c2fce22008-06-28 08:31:31 +10003178 if (rdev->mddev->pers->hot_add_disk == NULL)
3179 return -EINVAL;
3180
NeilBrownba1b41b2011-01-14 09:14:34 +11003181 if (slot >= rdev->mddev->raid_disks &&
3182 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
3183 return -ENOSPC;
3184
Neil Brown6c2fce22008-06-28 08:31:31 +10003185 rdev->raid_disk = slot;
3186 if (test_bit(In_sync, &rdev->flags))
3187 rdev->saved_raid_disk = slot;
3188 else
3189 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11003190 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11003191 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11003192 err = rdev->mddev->pers->
3193 hot_add_disk(rdev->mddev, rdev);
3194 if (err) {
3195 rdev->raid_disk = -1;
3196 return err;
3197 } else
3198 sysfs_notify_dirent_safe(rdev->sysfs_state);
3199 if (sysfs_link_rdev(rdev->mddev, rdev))
3200 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10003201 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08003202 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11003203 if (slot >= rdev->mddev->raid_disks &&
3204 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08003205 return -ENOSPC;
3206 rdev->raid_disk = slot;
3207 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08003208 clear_bit(Faulty, &rdev->flags);
3209 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08003210 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003211 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08003212 }
NeilBrown014236d2006-01-06 00:20:55 -08003213 return len;
3214}
3215
NeilBrown014236d2006-01-06 00:20:55 -08003216static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07003217__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08003218
NeilBrown93c8cad2006-01-06 00:20:56 -08003219static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003220offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08003221{
Andrew Morton6961ece2006-01-06 00:20:59 -08003222 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08003223}
3224
3225static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003226offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08003227{
NeilBrownc6563a82012-05-21 09:27:00 +10003228 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003229 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003230 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10003231 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003232 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003233 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08003234 /* Must set offset before size, so overlap checks
3235 * can be sane */
3236 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08003237 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10003238 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08003239 return len;
3240}
3241
3242static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07003243__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08003244
NeilBrownc6563a82012-05-21 09:27:00 +10003245static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3246{
3247 return sprintf(page, "%llu\n",
3248 (unsigned long long)rdev->new_data_offset);
3249}
3250
3251static ssize_t new_offset_store(struct md_rdev *rdev,
3252 const char *buf, size_t len)
3253{
3254 unsigned long long new_offset;
3255 struct mddev *mddev = rdev->mddev;
3256
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003257 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10003258 return -EINVAL;
3259
NeilBrownf851b602014-12-11 10:02:10 +11003260 if (mddev->sync_thread ||
3261 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10003262 return -EBUSY;
3263 if (new_offset == rdev->data_offset)
3264 /* reset is always permitted */
3265 ;
3266 else if (new_offset > rdev->data_offset) {
3267 /* must not push array size beyond rdev_sectors */
3268 if (new_offset - rdev->data_offset
3269 + mddev->dev_sectors > rdev->sectors)
3270 return -E2BIG;
3271 }
3272 /* Metadata worries about other space details. */
3273
3274 /* decreasing the offset is inconsistent with a backwards
3275 * reshape.
3276 */
3277 if (new_offset < rdev->data_offset &&
3278 mddev->reshape_backwards)
3279 return -EINVAL;
3280 /* Increasing offset is inconsistent with forwards
3281 * reshape. reshape_direction should be set to
3282 * 'backwards' first.
3283 */
3284 if (new_offset > rdev->data_offset &&
3285 !mddev->reshape_backwards)
3286 return -EINVAL;
3287
3288 if (mddev->pers && mddev->persistent &&
3289 !super_types[mddev->major_version]
3290 .allow_new_offset(rdev, new_offset))
3291 return -E2BIG;
3292 rdev->new_data_offset = new_offset;
3293 if (new_offset > rdev->data_offset)
3294 mddev->reshape_backwards = 1;
3295 else if (new_offset < rdev->data_offset)
3296 mddev->reshape_backwards = 0;
3297
3298 return len;
3299}
3300static struct rdev_sysfs_entry rdev_new_offset =
3301__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3302
NeilBrown83303b62006-01-06 00:21:06 -08003303static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003304rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003305{
Andre Nolldd8ac332009-03-31 14:33:13 +11003306 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003307}
3308
NeilBrownc5d79ad2008-02-06 01:39:54 -08003309static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3310{
3311 /* check if two start/length pairs overlap */
3312 if (s1+l1 <= s2)
3313 return 0;
3314 if (s2+l2 <= s1)
3315 return 0;
3316 return 1;
3317}
3318
Dan Williamsb522adc2009-03-31 15:00:31 +11003319static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3320{
3321 unsigned long long blocks;
3322 sector_t new;
3323
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003324 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003325 return -EINVAL;
3326
3327 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3328 return -EINVAL; /* sector conversion overflow */
3329
3330 new = blocks * 2;
3331 if (new != blocks * 2)
3332 return -EINVAL; /* unsigned long long to sector_t overflow */
3333
3334 *sectors = new;
3335 return 0;
3336}
3337
NeilBrown83303b62006-01-06 00:21:06 -08003338static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003339rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003340{
NeilBrownfd01b882011-10-11 16:47:53 +11003341 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003342 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003343 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003344
Shaohua Lif2076e72015-10-08 21:54:12 -07003345 if (test_bit(Journal, &rdev->flags))
3346 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003347 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003348 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003349 if (rdev->data_offset != rdev->new_data_offset)
3350 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003351 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003352 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003353 sectors = super_types[my_mddev->major_version].
3354 rdev_size_change(rdev, sectors);
3355 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003356 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003357 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003358 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003359 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003360 if (!my_mddev->pers->resize)
3361 /* Cannot change size for RAID0 or Linear etc */
3362 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003363 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003364 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003365 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003366
Andre Nolldd8ac332009-03-31 14:33:13 +11003367 rdev->sectors = sectors;
3368 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003369 /* Need to check that all other rdevs with the same
3370 * ->bdev do not overlap. 'rcu' is sufficient to walk
3371 * the rdev lists safely.
3372 * This check does not provide a hard guarantee, it
3373 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003374 */
NeilBrownfd01b882011-10-11 16:47:53 +11003375 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003376 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003377 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003378
NeilBrown8b1afc32014-09-29 15:33:20 +10003379 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003380 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003381 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003382
NeilBrowndafb20f2012-03-19 12:46:39 +11003383 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003384 if (rdev->bdev == rdev2->bdev &&
3385 rdev != rdev2 &&
3386 overlaps(rdev->data_offset, rdev->sectors,
3387 rdev2->data_offset,
3388 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003389 overlap = 1;
3390 break;
3391 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003392 if (overlap) {
3393 mddev_put(mddev);
3394 break;
3395 }
3396 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003397 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003398 if (overlap) {
3399 /* Someone else could have slipped in a size
3400 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003401 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003402 * safe, and trust userspace not to race with
3403 * itself
3404 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003405 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003406 return -EBUSY;
3407 }
3408 }
NeilBrown83303b62006-01-06 00:21:06 -08003409 return len;
3410}
3411
3412static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003413__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003414
NeilBrown3cb03002011-10-11 16:45:26 +11003415static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003416{
3417 unsigned long long recovery_start = rdev->recovery_offset;
3418
3419 if (test_bit(In_sync, &rdev->flags) ||
3420 recovery_start == MaxSector)
3421 return sprintf(page, "none\n");
3422
3423 return sprintf(page, "%llu\n", recovery_start);
3424}
3425
NeilBrown3cb03002011-10-11 16:45:26 +11003426static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003427{
3428 unsigned long long recovery_start;
3429
3430 if (cmd_match(buf, "none"))
3431 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003432 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003433 return -EINVAL;
3434
3435 if (rdev->mddev->pers &&
3436 rdev->raid_disk >= 0)
3437 return -EBUSY;
3438
3439 rdev->recovery_offset = recovery_start;
3440 if (recovery_start == MaxSector)
3441 set_bit(In_sync, &rdev->flags);
3442 else
3443 clear_bit(In_sync, &rdev->flags);
3444 return len;
3445}
3446
3447static struct rdev_sysfs_entry rdev_recovery_start =
3448__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3449
Vishal Vermafc974ee2015-12-24 19:20:34 -07003450/* sysfs access to bad-blocks list.
3451 * We present two files.
3452 * 'bad-blocks' lists sector numbers and lengths of ranges that
3453 * are recorded as bad. The list is truncated to fit within
3454 * the one-page limit of sysfs.
3455 * Writing "sector length" to this file adds an acknowledged
3456 * bad block list.
3457 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3458 * been acknowledged. Writing to this file adds bad blocks
3459 * without acknowledging them. This is largely for testing.
3460 */
NeilBrown3cb03002011-10-11 16:45:26 +11003461static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003462{
3463 return badblocks_show(&rdev->badblocks, page, 0);
3464}
NeilBrown3cb03002011-10-11 16:45:26 +11003465static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003466{
NeilBrownde393cd2011-07-28 11:31:48 +10003467 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3468 /* Maybe that ack was all we needed */
3469 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3470 wake_up(&rdev->blocked_wait);
3471 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003472}
3473static struct rdev_sysfs_entry rdev_bad_blocks =
3474__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3475
NeilBrown3cb03002011-10-11 16:45:26 +11003476static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003477{
3478 return badblocks_show(&rdev->badblocks, page, 1);
3479}
NeilBrown3cb03002011-10-11 16:45:26 +11003480static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003481{
3482 return badblocks_store(&rdev->badblocks, page, len, 1);
3483}
3484static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3485__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3486
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003487static ssize_t
3488ppl_sector_show(struct md_rdev *rdev, char *page)
3489{
3490 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3491}
3492
3493static ssize_t
3494ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3495{
3496 unsigned long long sector;
3497
3498 if (kstrtoull(buf, 10, &sector) < 0)
3499 return -EINVAL;
3500 if (sector != (sector_t)sector)
3501 return -EINVAL;
3502
3503 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3504 rdev->raid_disk >= 0)
3505 return -EBUSY;
3506
3507 if (rdev->mddev->persistent) {
3508 if (rdev->mddev->major_version == 0)
3509 return -EINVAL;
3510 if ((sector > rdev->sb_start &&
3511 sector - rdev->sb_start > S16_MAX) ||
3512 (sector < rdev->sb_start &&
3513 rdev->sb_start - sector > -S16_MIN))
3514 return -EINVAL;
3515 rdev->ppl.offset = sector - rdev->sb_start;
3516 } else if (!rdev->mddev->external) {
3517 return -EBUSY;
3518 }
3519 rdev->ppl.sector = sector;
3520 return len;
3521}
3522
3523static struct rdev_sysfs_entry rdev_ppl_sector =
3524__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3525
3526static ssize_t
3527ppl_size_show(struct md_rdev *rdev, char *page)
3528{
3529 return sprintf(page, "%u\n", rdev->ppl.size);
3530}
3531
3532static ssize_t
3533ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3534{
3535 unsigned int size;
3536
3537 if (kstrtouint(buf, 10, &size) < 0)
3538 return -EINVAL;
3539
3540 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3541 rdev->raid_disk >= 0)
3542 return -EBUSY;
3543
3544 if (rdev->mddev->persistent) {
3545 if (rdev->mddev->major_version == 0)
3546 return -EINVAL;
3547 if (size > U16_MAX)
3548 return -EINVAL;
3549 } else if (!rdev->mddev->external) {
3550 return -EBUSY;
3551 }
3552 rdev->ppl.size = size;
3553 return len;
3554}
3555
3556static struct rdev_sysfs_entry rdev_ppl_size =
3557__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3558
NeilBrown86e6ffd2005-11-08 21:39:24 -08003559static struct attribute *rdev_default_attrs[] = {
3560 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003561 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003562 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003563 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003564 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003565 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003566 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003567 &rdev_bad_blocks.attr,
3568 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003569 &rdev_ppl_sector.attr,
3570 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003571 NULL,
3572};
3573static ssize_t
3574rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3575{
3576 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003577 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003578
3579 if (!entry->show)
3580 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003581 if (!rdev->mddev)
Marcos Paulo de Souza168b3052019-06-14 15:41:06 -07003582 return -ENODEV;
NeilBrown758bfc82014-12-15 12:56:59 +11003583 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003584}
3585
3586static ssize_t
3587rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3588 const char *page, size_t length)
3589{
3590 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003591 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003592 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003593 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003594
3595 if (!entry->store)
3596 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003597 if (!capable(CAP_SYS_ADMIN))
3598 return -EACCES;
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003599 rv = mddev ? mddev_lock(mddev) : -ENODEV;
NeilBrownca388052008-02-06 01:39:55 -08003600 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003601 if (rdev->mddev == NULL)
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003602 rv = -ENODEV;
NeilBrown27c529b2008-03-04 14:29:33 -08003603 else
3604 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003605 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003606 }
3607 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003608}
3609
3610static void rdev_free(struct kobject *ko)
3611{
NeilBrown3cb03002011-10-11 16:45:26 +11003612 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003613 kfree(rdev);
3614}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003615static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003616 .show = rdev_attr_show,
3617 .store = rdev_attr_store,
3618};
3619static struct kobj_type rdev_ktype = {
3620 .release = rdev_free,
3621 .sysfs_ops = &rdev_sysfs_ops,
3622 .default_attrs = rdev_default_attrs,
3623};
3624
NeilBrown3cb03002011-10-11 16:45:26 +11003625int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003626{
3627 rdev->desc_nr = -1;
3628 rdev->saved_raid_disk = -1;
3629 rdev->raid_disk = -1;
3630 rdev->flags = 0;
3631 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003632 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003633 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003634 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003635 rdev->sb_loaded = 0;
3636 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003637 atomic_set(&rdev->nr_pending, 0);
3638 atomic_set(&rdev->read_errors, 0);
3639 atomic_set(&rdev->corrected_errors, 0);
3640
3641 INIT_LIST_HEAD(&rdev->same_set);
3642 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003643
3644 /* Add space to store bad block list.
3645 * This reserves the space even on arrays where it cannot
3646 * be used - I wonder if that matters
3647 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003648 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003649}
3650EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651/*
3652 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3653 *
3654 * mark the device faulty if:
3655 *
3656 * - the device is nonexistent (zero size)
3657 * - the device has no valid superblock
3658 *
3659 * a faulty rdev _never_ has rdev->sb set.
3660 */
NeilBrown3cb03002011-10-11 16:45:26 +11003661static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
3663 char b[BDEVNAME_SIZE];
3664 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003665 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 sector_t size;
3667
NeilBrown9ffae0c2006-01-06 00:20:32 -08003668 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003669 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
NeilBrown2230dfe2011-07-28 11:31:46 +10003672 err = md_rdev_init(rdev);
3673 if (err)
3674 goto abort_free;
3675 err = alloc_disk_sb(rdev);
3676 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 goto abort_free;
3678
NeilBrownc5d79ad2008-02-06 01:39:54 -08003679 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 if (err)
3681 goto abort_free;
3682
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003683 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003684
Mike Snitzer77304d22010-11-08 14:39:12 +01003685 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003687 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 bdevname(rdev->bdev,b));
3689 err = -EINVAL;
3690 goto abort_free;
3691 }
3692
3693 if (super_format >= 0) {
3694 err = super_types[super_format].
3695 load_super(rdev, NULL, super_minor);
3696 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003697 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003698 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003699 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 goto abort_free;
3701 }
3702 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003703 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 bdevname(rdev->bdev,b));
3705 goto abort_free;
3706 }
3707 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 return rdev;
3710
3711abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003712 if (rdev->bdev)
3713 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003714 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 kfree(rdev);
3716 return ERR_PTR(err);
3717}
3718
3719/*
3720 * Check a full RAID array for plausibility
3721 */
3722
Yufen Yu6a5cb532019-10-16 16:00:03 +08003723static int analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724{
3725 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003726 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 char b[BDEVNAME_SIZE];
3728
3729 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003730 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 switch (super_types[mddev->major_version].
3732 load_super(rdev, freshest, mddev->minor_version)) {
3733 case 1:
3734 freshest = rdev;
3735 break;
3736 case 0:
3737 break;
3738 default:
NeilBrown9d487392016-11-02 14:16:49 +11003739 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003741 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 }
3743
Yufen Yu6a5cb532019-10-16 16:00:03 +08003744 /* Cannot find a valid fresh disk */
3745 if (!freshest) {
3746 pr_warn("md: cannot find a valid disk\n");
3747 return -EINVAL;
3748 }
3749
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 super_types[mddev->major_version].
3751 validate_super(mddev, freshest);
3752
3753 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003754 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003755 if (mddev->max_disks &&
3756 (rdev->desc_nr >= mddev->max_disks ||
3757 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003758 pr_warn("md: %s: %s: only %d devices permitted\n",
3759 mdname(mddev), bdevname(rdev->bdev, b),
3760 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003761 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003762 continue;
3763 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003764 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 if (super_types[mddev->major_version].
3766 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003767 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003769 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 continue;
3771 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 if (mddev->level == LEVEL_MULTIPATH) {
3774 rdev->desc_nr = i++;
3775 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003776 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003777 } else if (rdev->raid_disk >=
3778 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3779 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003780 rdev->raid_disk = -1;
3781 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 }
3783 }
Yufen Yu6a5cb532019-10-16 16:00:03 +08003784
3785 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786}
3787
NeilBrown72e02072009-12-14 12:49:55 +11003788/* Read a fixed-point number.
3789 * Numbers in sysfs attributes should be in "standard" units where
3790 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003791 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003792 * milliseconds or jiffies.
3793 * This function takes a decimal number with a possible fractional
3794 * component, and produces an integer which is the result of
3795 * multiplying that number by 10^'scale'.
3796 * all without any floating-point arithmetic.
3797 */
3798int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3799{
3800 unsigned long result = 0;
3801 long decimals = -1;
3802 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3803 if (*cp == '.')
3804 decimals = 0;
3805 else if (decimals < scale) {
3806 unsigned int value;
3807 value = *cp - '0';
3808 result = result * 10 + value;
3809 if (decimals >= 0)
3810 decimals++;
3811 }
3812 cp++;
3813 }
3814 if (*cp == '\n')
3815 cp++;
3816 if (*cp)
3817 return -EINVAL;
3818 if (decimals < 0)
3819 decimals = 0;
Andy Shevchenkocf891602019-07-23 23:41:55 +03003820 *res = result * int_pow(10, scale - decimals);
NeilBrown72e02072009-12-14 12:49:55 +11003821 return 0;
3822}
3823
NeilBrowneae17012005-11-08 21:39:23 -08003824static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003825safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003826{
3827 int msec = (mddev->safemode_delay*1000)/HZ;
3828 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3829}
3830static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003831safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003832{
NeilBrown16f17b32006-06-26 00:27:37 -07003833 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003834
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003835 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003836 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003837 return -EINVAL;
3838 }
3839
NeilBrown72e02072009-12-14 12:49:55 +11003840 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003841 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003842 if (msec == 0)
3843 mddev->safemode_delay = 0;
3844 else {
NeilBrown19052c02008-08-05 15:54:13 +10003845 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003846 unsigned long new_delay = (msec*HZ)/1000;
3847
3848 if (new_delay == 0)
3849 new_delay = 1;
3850 mddev->safemode_delay = new_delay;
3851 if (new_delay < old_delay || old_delay == 0)
3852 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003853 }
3854 return len;
3855}
3856static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003857__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003858
3859static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003860level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003861{
NeilBrown36d091f2014-12-15 12:56:58 +11003862 struct md_personality *p;
3863 int ret;
3864 spin_lock(&mddev->lock);
3865 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003866 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003867 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003868 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003869 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003870 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003871 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003872 else
NeilBrown36d091f2014-12-15 12:56:58 +11003873 ret = 0;
3874 spin_unlock(&mddev->lock);
3875 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003876}
3877
NeilBrownd9d166c2006-01-06 00:20:51 -08003878static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003879level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003880{
Dan Williamsf2859af2010-05-02 10:04:16 -07003881 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003882 ssize_t rv;
3883 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003884 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003885 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003886 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003887 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003888
NeilBrown67918752014-12-15 12:57:01 +11003889 if (slen == 0 || slen >= sizeof(clevel))
3890 return -EINVAL;
3891
3892 rv = mddev_lock(mddev);
3893 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003894 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003895
3896 if (mddev->pers == NULL) {
3897 strncpy(mddev->clevel, buf, slen);
3898 if (mddev->clevel[slen-1] == '\n')
3899 slen--;
3900 mddev->clevel[slen] = 0;
3901 mddev->level = LEVEL_NONE;
3902 rv = len;
3903 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003904 }
NeilBrown67918752014-12-15 12:57:01 +11003905 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003906 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003907 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003908
3909 /* request to change the personality. Need to ensure:
3910 * - array is not engaged in resync/recovery/reshape
3911 * - old personality can be suspended
3912 * - new personality will access other array.
3913 */
3914
NeilBrown67918752014-12-15 12:57:01 +11003915 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003916 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003917 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003918 mddev->reshape_position != MaxSector ||
3919 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003920 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003921
NeilBrown67918752014-12-15 12:57:01 +11003922 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003923 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003924 pr_warn("md: %s: %s does not support online personality change\n",
3925 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003926 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003927 }
3928
3929 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003930 strncpy(clevel, buf, slen);
3931 if (clevel[slen-1] == '\n')
3932 slen--;
3933 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003934 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003935 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003936
Dan Williamsf2859af2010-05-02 10:04:16 -07003937 if (request_module("md-%s", clevel) != 0)
3938 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003939 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003940 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003941 if (!pers || !try_module_get(pers->owner)) {
3942 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003943 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003944 rv = -EINVAL;
3945 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003946 }
3947 spin_unlock(&pers_lock);
3948
3949 if (pers == mddev->pers) {
3950 /* Nothing to do! */
3951 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003952 rv = len;
3953 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003954 }
3955 if (!pers->takeover) {
3956 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003957 pr_warn("md: %s: %s does not support personality takeover\n",
3958 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003959 rv = -EINVAL;
3960 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003961 }
3962
NeilBrowndafb20f2012-03-19 12:46:39 +11003963 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003964 rdev->new_raid_disk = rdev->raid_disk;
3965
NeilBrown245f46c2009-03-31 14:39:39 +11003966 /* ->takeover must set new_* and/or delta_disks
3967 * if it succeeds, and may set them when it fails.
3968 */
3969 priv = pers->takeover(mddev);
3970 if (IS_ERR(priv)) {
3971 mddev->new_level = mddev->level;
3972 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003973 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003974 mddev->raid_disks -= mddev->delta_disks;
3975 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003976 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003977 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003978 pr_warn("md: %s: %s would not accept array\n",
3979 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003980 rv = PTR_ERR(priv);
3981 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003982 }
3983
3984 /* Looks like we have a winner */
3985 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003986 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003987
3988 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003989 oldpers = mddev->pers;
3990 oldpriv = mddev->private;
3991 mddev->pers = pers;
3992 mddev->private = priv;
3993 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3994 mddev->level = mddev->new_level;
3995 mddev->layout = mddev->new_layout;
3996 mddev->chunk_sectors = mddev->new_chunk_sectors;
3997 mddev->delta_disks = 0;
3998 mddev->reshape_backwards = 0;
3999 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11004000 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10004001
NeilBrowndb721d32014-12-15 12:56:58 +11004002 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11004003 mddev->external) {
4004 /* We are converting from a no-redundancy array
4005 * to a redundancy array and metadata is managed
4006 * externally so we need to be sure that writes
4007 * won't block due to a need to transition
4008 * clean->dirty
4009 * until external management is started.
4010 */
4011 mddev->in_sync = 0;
4012 mddev->safemode_delay = 0;
4013 mddev->safemode = 0;
4014 }
4015
NeilBrowndb721d32014-12-15 12:56:58 +11004016 oldpers->free(mddev, oldpriv);
4017
4018 if (oldpers->sync_request == NULL &&
4019 pers->sync_request != NULL) {
4020 /* need to add the md_redundancy_group */
4021 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11004022 pr_warn("md: cannot register extra attributes for %s\n",
4023 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11004024 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
4025 }
4026 if (oldpers->sync_request != NULL &&
4027 pers->sync_request == NULL) {
4028 /* need to remove the md_redundancy_group */
4029 if (mddev->to_remove == NULL)
4030 mddev->to_remove = &md_redundancy_group;
4031 }
4032
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02004033 module_put(oldpers->owner);
4034
NeilBrowndafb20f2012-03-19 12:46:39 +11004035 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004036 if (rdev->raid_disk < 0)
4037 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11004038 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01004039 rdev->new_raid_disk = -1;
4040 if (rdev->new_raid_disk == rdev->raid_disk)
4041 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10004042 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01004043 }
NeilBrowndafb20f2012-03-19 12:46:39 +11004044 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01004045 if (rdev->raid_disk < 0)
4046 continue;
4047 if (rdev->new_raid_disk == rdev->raid_disk)
4048 continue;
4049 rdev->raid_disk = rdev->new_raid_disk;
4050 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10004051 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01004052 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10004053 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11004054 pr_warn("md: cannot register rd%d for %s after level change\n",
4055 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10004056 }
NeilBrowne93f68a2010-06-15 09:36:03 +01004057 }
4058
NeilBrowndb721d32014-12-15 12:56:58 +11004059 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11004060 /* this is now an array without redundancy, so
4061 * it must always be in_sync
4062 */
4063 mddev->in_sync = 1;
4064 del_timer_sync(&mddev->safemode_timer);
4065 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11004066 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11004067 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004068 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10004069 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11004070 if (!mddev->thread)
4071 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10004072 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07004073 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004074 rv = len;
4075out_unlock:
4076 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08004077 return rv;
4078}
4079
4080static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07004081__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08004082
NeilBrownd4dbd022006-06-26 00:27:59 -07004083static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004084layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07004085{
4086 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07004087 if (mddev->reshape_position != MaxSector &&
4088 mddev->layout != mddev->new_layout)
4089 return sprintf(page, "%d (%d)\n",
4090 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07004091 return sprintf(page, "%d\n", mddev->layout);
4092}
4093
4094static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004095layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07004096{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004097 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11004098 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07004099
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004100 err = kstrtouint(buf, 10, &n);
4101 if (err < 0)
4102 return err;
NeilBrown67918752014-12-15 12:57:01 +11004103 err = mddev_lock(mddev);
4104 if (err)
4105 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07004106
NeilBrownb3546032009-03-31 14:56:41 +11004107 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10004108 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004109 err = -EBUSY;
4110 else if (mddev->ro)
4111 err = -EROFS;
4112 else {
4113 mddev->new_layout = n;
4114 err = mddev->pers->check_reshape(mddev);
4115 if (err)
4116 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10004117 }
NeilBrownb3546032009-03-31 14:56:41 +11004118 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07004119 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11004120 if (mddev->reshape_position == MaxSector)
4121 mddev->layout = n;
4122 }
NeilBrown67918752014-12-15 12:57:01 +11004123 mddev_unlock(mddev);
4124 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07004125}
4126static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07004127__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07004128
NeilBrowneae17012005-11-08 21:39:23 -08004129static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004130raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08004131{
NeilBrownbb636542005-11-08 21:39:45 -08004132 if (mddev->raid_disks == 0)
4133 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004134 if (mddev->reshape_position != MaxSector &&
4135 mddev->delta_disks != 0)
4136 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
4137 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08004138 return sprintf(page, "%d\n", mddev->raid_disks);
4139}
4140
NeilBrownfd01b882011-10-11 16:47:53 +11004141static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08004142
4143static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004144raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08004145{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004146 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11004147 int err;
NeilBrownda943b992006-01-06 00:20:54 -08004148
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004149 err = kstrtouint(buf, 10, &n);
4150 if (err < 0)
4151 return err;
NeilBrownda943b992006-01-06 00:20:54 -08004152
NeilBrown67918752014-12-15 12:57:01 +11004153 err = mddev_lock(mddev);
4154 if (err)
4155 return err;
NeilBrownda943b992006-01-06 00:20:54 -08004156 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11004157 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07004158 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10004159 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004160 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10004161
NeilBrown67918752014-12-15 12:57:01 +11004162 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10004163 rdev_for_each(rdev, mddev) {
4164 if (olddisks < n &&
4165 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11004166 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10004167 if (olddisks > n &&
4168 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11004169 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10004170 }
NeilBrown67918752014-12-15 12:57:01 +11004171 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004172 mddev->delta_disks = n - olddisks;
4173 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10004174 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07004175 } else
NeilBrownda943b992006-01-06 00:20:54 -08004176 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11004177out_unlock:
4178 mddev_unlock(mddev);
4179 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08004180}
4181static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07004182__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08004183
NeilBrown24dd4692005-11-08 21:39:26 -08004184static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004185chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08004186{
NeilBrown08a02ec2007-05-09 02:35:38 -07004187 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10004188 mddev->chunk_sectors != mddev->new_chunk_sectors)
4189 return sprintf(page, "%d (%d)\n",
4190 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10004191 mddev->chunk_sectors << 9);
4192 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08004193}
4194
4195static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004196chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08004197{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004198 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11004199 int err;
NeilBrown3b343802006-01-06 00:20:47 -08004200
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004201 err = kstrtoul(buf, 10, &n);
4202 if (err < 0)
4203 return err;
NeilBrown3b343802006-01-06 00:20:47 -08004204
NeilBrown67918752014-12-15 12:57:01 +11004205 err = mddev_lock(mddev);
4206 if (err)
4207 return err;
NeilBrownb3546032009-03-31 14:56:41 +11004208 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10004209 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004210 err = -EBUSY;
4211 else if (mddev->ro)
4212 err = -EROFS;
4213 else {
4214 mddev->new_chunk_sectors = n >> 9;
4215 err = mddev->pers->check_reshape(mddev);
4216 if (err)
4217 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10004218 }
NeilBrownb3546032009-03-31 14:56:41 +11004219 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10004220 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004221 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10004222 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004223 }
NeilBrown67918752014-12-15 12:57:01 +11004224 mddev_unlock(mddev);
4225 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08004226}
4227static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004228__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08004229
NeilBrowna94213b2006-06-26 00:28:00 -07004230static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004231resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07004232{
NeilBrownd1a7c502009-03-31 15:24:32 +11004233 if (mddev->recovery_cp == MaxSector)
4234 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07004235 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4236}
4237
4238static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004239resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07004240{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004241 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11004242 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004243
4244 if (cmd_match(buf, "none"))
4245 n = MaxSector;
4246 else {
4247 err = kstrtoull(buf, 10, &n);
4248 if (err < 0)
4249 return err;
4250 if (n != (sector_t)n)
4251 return -EINVAL;
4252 }
NeilBrowna94213b2006-06-26 00:28:00 -07004253
NeilBrown67918752014-12-15 12:57:01 +11004254 err = mddev_lock(mddev);
4255 if (err)
4256 return err;
NeilBrownb0986362011-05-11 15:52:21 +10004257 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11004258 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07004259
NeilBrown67918752014-12-15 12:57:01 +11004260 if (!err) {
4261 mddev->recovery_cp = n;
4262 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08004263 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11004264 }
4265 mddev_unlock(mddev);
4266 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07004267}
4268static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10004269__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4270 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07004271
NeilBrown9e653b62006-06-26 00:27:58 -07004272/*
4273 * The array state can be:
4274 *
4275 * clear
4276 * No devices, no size, no level
4277 * Equivalent to STOP_ARRAY ioctl
4278 * inactive
4279 * May have some settings, but array is not active
4280 * all IO results in error
4281 * When written, doesn't tear down array, but just stops it
4282 * suspended (not supported yet)
4283 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01004284 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07004285 * readonly
4286 * no resync can happen. no superblocks get written.
4287 * write requests fail
4288 * read-auto
4289 * like readonly, but behaves like 'clean' on a write request.
4290 *
4291 * clean - no pending writes, but otherwise active.
4292 * When written to inactive array, starts without resync
4293 * If a write request arrives then
4294 * if metadata is known, mark 'dirty' and switch to 'active'.
4295 * if not known, block and switch to write-pending
4296 * If written to an active array that has pending writes, then fails.
4297 * active
4298 * fully active: IO and resync can be happening.
4299 * When written to inactive array, starts with resync
4300 *
4301 * write-pending
4302 * clean, but writes are blocked waiting for 'active' to be written.
4303 *
4304 * active-idle
4305 * like active, but no writes have been seen for a while (100msec).
4306 *
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004307 * broken
4308 * RAID0/LINEAR-only: same as clean, but array is missing a member.
4309 * It's useful because RAID0/LINEAR mounted-arrays aren't stopped
4310 * when a member is gone, so this state will at least alert the
4311 * user that something is wrong.
NeilBrown9e653b62006-06-26 00:27:58 -07004312 */
4313enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004314 write_pending, active_idle, broken, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004315static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004316 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004317 "write-pending", "active-idle", "broken", NULL };
NeilBrown9e653b62006-06-26 00:27:58 -07004318
4319static int match_word(const char *word, char **list)
4320{
4321 int n;
4322 for (n=0; list[n]; n++)
4323 if (cmd_match(word, list[n]))
4324 break;
4325 return n;
4326}
4327
4328static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004329array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004330{
4331 enum array_state st = inactive;
4332
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004333 if (mddev->pers && !test_bit(MD_NOT_READY, &mddev->flags)) {
NeilBrown9e653b62006-06-26 00:27:58 -07004334 switch(mddev->ro) {
4335 case 1:
4336 st = readonly;
4337 break;
4338 case 2:
4339 st = read_auto;
4340 break;
4341 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004342 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004343 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004344 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004345 else if (mddev->in_sync)
4346 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004347 else if (mddev->safemode)
4348 st = active_idle;
4349 else
4350 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004351 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004352 }
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004353
4354 if (test_bit(MD_BROKEN, &mddev->flags) && st == clean)
4355 st = broken;
4356 } else {
NeilBrown9e653b62006-06-26 00:27:58 -07004357 if (list_empty(&mddev->disks) &&
4358 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004359 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004360 st = clear;
4361 else
4362 st = inactive;
4363 }
4364 return sprintf(page, "%s\n", array_states[st]);
4365}
4366
NeilBrownf72ffdd2014-09-30 14:23:59 +10004367static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4368static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4369static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004370static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004371
4372static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004373array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004374{
NeilBrown6497709b2017-03-15 14:05:14 +11004375 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004376 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004377
4378 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4379 /* don't take reconfig_mutex when toggling between
4380 * clean and active
4381 */
4382 spin_lock(&mddev->lock);
4383 if (st == active) {
4384 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004385 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004386 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004387 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004388 } else /* st == clean */ {
4389 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004390 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004391 err = -EBUSY;
4392 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004393 if (!err)
4394 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004395 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004396 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004397 }
4398 err = mddev_lock(mddev);
4399 if (err)
4400 return err;
4401 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004402 switch(st) {
4403 case bad_word:
4404 break;
4405 case clear:
4406 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004407 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004408 break;
4409 case inactive:
4410 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004411 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004412 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004413 else
NeilBrowne6910632008-02-06 01:39:51 -08004414 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004415 break;
4416 case suspended:
4417 break; /* not supported yet */
4418 case readonly:
4419 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004420 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004421 else {
4422 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004423 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004424 err = do_md_run(mddev);
4425 }
4426 break;
4427 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004428 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004429 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004430 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004431 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004432 err = restart_array(mddev);
4433 if (err == 0) {
4434 mddev->ro = 2;
4435 set_disk_ro(mddev->gendisk, 0);
4436 }
NeilBrown9e653b62006-06-26 00:27:58 -07004437 } else {
4438 mddev->ro = 2;
4439 err = do_md_run(mddev);
4440 }
4441 break;
4442 case clean:
4443 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004444 err = restart_array(mddev);
4445 if (err)
4446 break;
NeilBrown85572d72014-12-15 12:56:56 +11004447 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004448 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004449 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004450 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004451 } else
4452 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004453 break;
4454 case active:
4455 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004456 err = restart_array(mddev);
4457 if (err)
4458 break;
Shaohua Li29530792016-12-08 15:48:19 -08004459 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004460 wake_up(&mddev->sb_wait);
4461 err = 0;
4462 } else {
4463 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004464 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004465 err = do_md_run(mddev);
4466 }
4467 break;
4468 case write_pending:
4469 case active_idle:
Guilherme G. Piccoli62f7b192019-09-03 16:49:00 -03004470 case broken:
NeilBrown9e653b62006-06-26 00:27:58 -07004471 /* these cannot be set */
4472 break;
4473 }
NeilBrown67918752014-12-15 12:57:01 +11004474
4475 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004476 if (mddev->hold_active == UNTIL_IOCTL)
4477 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004478 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004479 }
NeilBrown67918752014-12-15 12:57:01 +11004480 mddev_unlock(mddev);
4481 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004482}
NeilBrown80ca3a42006-07-10 04:44:18 -07004483static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004484__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004485
NeilBrown6d7ff7382006-01-06 00:21:16 -08004486static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004487max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004488 return sprintf(page, "%d\n",
4489 atomic_read(&mddev->max_corr_read_errors));
4490}
4491
4492static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004493max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004494{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004495 unsigned int n;
4496 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004497
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004498 rv = kstrtouint(buf, 10, &n);
4499 if (rv < 0)
4500 return rv;
4501 atomic_set(&mddev->max_corr_read_errors, n);
4502 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004503}
4504
4505static struct md_sysfs_entry max_corr_read_errors =
4506__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4507 max_corrected_read_errors_store);
4508
4509static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004510null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004511{
4512 return -EINVAL;
4513}
4514
4515static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004516new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004517{
4518 /* buf must be %d:%d\n? giving major and minor numbers */
4519 /* The new device is added to the array.
4520 * If the array has a persistent superblock, we read the
4521 * superblock to initialise info and check validity.
4522 * Otherwise, only checking done is that in bind_rdev_to_array,
4523 * which mainly checks size.
4524 */
4525 char *e;
4526 int major = simple_strtoul(buf, &e, 10);
4527 int minor;
4528 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004529 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004530 int err;
4531
4532 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4533 return -EINVAL;
4534 minor = simple_strtoul(e+1, &e, 10);
4535 if (*e && *e != '\n')
4536 return -EINVAL;
4537 dev = MKDEV(major, minor);
4538 if (major != MAJOR(dev) ||
4539 minor != MINOR(dev))
4540 return -EOVERFLOW;
4541
NeilBrown67918752014-12-15 12:57:01 +11004542 flush_workqueue(md_misc_wq);
4543
4544 err = mddev_lock(mddev);
4545 if (err)
4546 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004547 if (mddev->persistent) {
4548 rdev = md_import_device(dev, mddev->major_version,
4549 mddev->minor_version);
4550 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004551 struct md_rdev *rdev0
4552 = list_entry(mddev->disks.next,
4553 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004554 err = super_types[mddev->major_version]
4555 .load_super(rdev, rdev0, mddev->minor_version);
4556 if (err < 0)
4557 goto out;
4558 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004559 } else if (mddev->external)
4560 rdev = md_import_device(dev, -2, -1);
4561 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004562 rdev = md_import_device(dev, -1, -1);
4563
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004564 if (IS_ERR(rdev)) {
4565 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004566 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004567 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004568 err = bind_rdev_to_array(rdev, mddev);
4569 out:
4570 if (err)
4571 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004572 mddev_unlock(mddev);
Alexey Obitotskiy5492c462017-07-28 15:49:25 +02004573 if (!err)
4574 md_new_event(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004575 return err ? err : len;
4576}
4577
4578static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004579__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004580
4581static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004582bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004583{
4584 char *end;
4585 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004586 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004587
NeilBrown67918752014-12-15 12:57:01 +11004588 err = mddev_lock(mddev);
4589 if (err)
4590 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004591 if (!mddev->bitmap)
4592 goto out;
4593 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4594 while (*buf) {
4595 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4596 if (buf == end) break;
4597 if (*end == '-') { /* range */
4598 buf = end + 1;
4599 end_chunk = simple_strtoul(buf, &end, 0);
4600 if (buf == end) break;
4601 }
4602 if (*end && !isspace(*end)) break;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004603 md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004604 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004605 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004606 md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
Paul Clements9b1d1da2006-10-03 01:15:49 -07004607out:
NeilBrown67918752014-12-15 12:57:01 +11004608 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004609 return len;
4610}
4611
4612static struct md_sysfs_entry md_bitmap =
4613__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4614
4615static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004616size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004617{
Andre Noll58c0fed2009-03-31 14:33:13 +11004618 return sprintf(page, "%llu\n",
4619 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004620}
4621
NeilBrownfd01b882011-10-11 16:47:53 +11004622static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004623
4624static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004625size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004626{
4627 /* If array is inactive, we can reduce the component size, but
4628 * not increase it (except from 0).
4629 * If array is active, we can try an on-line resize
4630 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004631 sector_t sectors;
4632 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004633
Andre Noll58c0fed2009-03-31 14:33:13 +11004634 if (err < 0)
4635 return err;
NeilBrown67918752014-12-15 12:57:01 +11004636 err = mddev_lock(mddev);
4637 if (err)
4638 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004639 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004640 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004641 if (err == 0)
4642 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004643 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004644 if (mddev->dev_sectors == 0 ||
4645 mddev->dev_sectors > sectors)
4646 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004647 else
4648 err = -ENOSPC;
4649 }
NeilBrown67918752014-12-15 12:57:01 +11004650 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004651 return err ? err : len;
4652}
4653
4654static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004655__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004656
Masanari Iida83f0d772012-10-30 00:18:08 +09004657/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004658 * This is one of
4659 * 'none' for arrays with no metadata (good luck...)
4660 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004661 * or N.M for internally known formats
4662 */
4663static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004664metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004665{
4666 if (mddev->persistent)
4667 return sprintf(page, "%d.%d\n",
4668 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004669 else if (mddev->external)
4670 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004671 else
4672 return sprintf(page, "none\n");
4673}
4674
4675static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004676metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004677{
4678 int major, minor;
4679 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004680 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004681 /* Changing the details of 'external' metadata is
4682 * always permitted. Otherwise there must be
4683 * no devices attached to the array.
4684 */
NeilBrown67918752014-12-15 12:57:01 +11004685
4686 err = mddev_lock(mddev);
4687 if (err)
4688 return err;
4689 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004690 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4691 ;
4692 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004693 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004694
NeilBrown67918752014-12-15 12:57:01 +11004695 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004696 if (cmd_match(buf, "none")) {
4697 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004698 mddev->external = 0;
4699 mddev->major_version = 0;
4700 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004701 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004702 }
4703 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004704 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004705 if (namelen >= sizeof(mddev->metadata_type))
4706 namelen = sizeof(mddev->metadata_type)-1;
4707 strncpy(mddev->metadata_type, buf+9, namelen);
4708 mddev->metadata_type[namelen] = 0;
4709 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4710 mddev->metadata_type[--namelen] = 0;
4711 mddev->persistent = 0;
4712 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004713 mddev->major_version = 0;
4714 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004715 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004716 }
4717 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004718 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004719 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004720 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004721 buf = e+1;
4722 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004723 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004724 goto out_unlock;
4725 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004726 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004727 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004728 mddev->major_version = major;
4729 mddev->minor_version = minor;
4730 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004731 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004732 err = 0;
4733out_unlock:
4734 mddev_unlock(mddev);
4735 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004736}
4737
4738static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004739__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004740
NeilBrowna35b0d62006-01-06 00:20:49 -08004741static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004742action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004743{
NeilBrown7eec3142005-11-08 21:39:44 -08004744 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004745 unsigned long recovery = mddev->recovery;
4746 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004747 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004748 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4749 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4750 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004751 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004752 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4753 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004754 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004755 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004756 type = "check";
4757 else
4758 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004759 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004760 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004761 else if (mddev->reshape_position != MaxSector)
4762 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004763 }
4764 return sprintf(page, "%s\n", type);
4765}
4766
4767static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004768action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004769{
NeilBrown7eec3142005-11-08 21:39:44 -08004770 if (!mddev->pers || !mddev->pers->sync_request)
4771 return -EINVAL;
4772
NeilBrownb6a9ce62009-05-26 09:41:17 +10004773
4774 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004775 if (cmd_match(page, "frozen"))
4776 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4777 else
4778 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004779 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4780 mddev_lock(mddev) == 0) {
4781 flush_workqueue(md_misc_wq);
4782 if (mddev->sync_thread) {
4783 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004784 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004785 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004786 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004787 }
NeilBrown312045e2015-12-21 11:01:21 +11004788 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004789 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004790 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004791 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004792 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004793 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004794 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004795 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004796 int err;
4797 if (mddev->pers->start_reshape == NULL)
4798 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004799 err = mddev_lock(mddev);
4800 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004801 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4802 err = -EBUSY;
4803 else {
4804 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4805 err = mddev->pers->start_reshape(mddev);
4806 }
NeilBrown67918752014-12-15 12:57:01 +11004807 mddev_unlock(mddev);
4808 }
NeilBrown16484bf2006-03-27 01:18:13 -08004809 if (err)
4810 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004811 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004812 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004813 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004814 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004815 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004816 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004817 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004818 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4819 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004820 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004821 if (mddev->ro == 2) {
4822 /* A write to sync_action is enough to justify
4823 * canceling read-auto mode
4824 */
4825 mddev->ro = 0;
4826 md_wakeup_thread(mddev->sync_thread);
4827 }
NeilBrown03c902e2006-01-06 00:20:46 -08004828 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004829 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004830 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004831 return len;
4832}
4833
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004834static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004835__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004836
4837static ssize_t
4838last_sync_action_show(struct mddev *mddev, char *page)
4839{
4840 return sprintf(page, "%s\n", mddev->last_sync_action);
4841}
4842
4843static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4844
NeilBrown9d888832005-11-08 21:39:26 -08004845static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004846mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004847{
4848 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004849 (unsigned long long)
4850 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004851}
4852
NeilBrown80ca3a42006-07-10 04:44:18 -07004853static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004854
NeilBrown88202a02006-01-06 00:21:36 -08004855static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004856sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004857{
4858 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4859 mddev->sync_speed_min ? "local": "system");
4860}
4861
4862static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004863sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004864{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004865 unsigned int min;
4866 int rv;
4867
NeilBrown88202a02006-01-06 00:21:36 -08004868 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004869 min = 0;
4870 } else {
4871 rv = kstrtouint(buf, 10, &min);
4872 if (rv < 0)
4873 return rv;
4874 if (min == 0)
4875 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004876 }
NeilBrown88202a02006-01-06 00:21:36 -08004877 mddev->sync_speed_min = min;
4878 return len;
4879}
4880
4881static struct md_sysfs_entry md_sync_min =
4882__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4883
4884static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004885sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004886{
4887 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4888 mddev->sync_speed_max ? "local": "system");
4889}
4890
4891static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004892sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004893{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004894 unsigned int max;
4895 int rv;
4896
NeilBrown88202a02006-01-06 00:21:36 -08004897 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004898 max = 0;
4899 } else {
4900 rv = kstrtouint(buf, 10, &max);
4901 if (rv < 0)
4902 return rv;
4903 if (max == 0)
4904 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004905 }
NeilBrown88202a02006-01-06 00:21:36 -08004906 mddev->sync_speed_max = max;
4907 return len;
4908}
4909
4910static struct md_sysfs_entry md_sync_max =
4911__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4912
Iustin Popd7f3d292007-10-16 23:30:54 -07004913static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004914degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004915{
4916 return sprintf(page, "%d\n", mddev->degraded);
4917}
4918static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004919
4920static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004921sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004922{
4923 return sprintf(page, "%d\n", mddev->parallel_resync);
4924}
4925
4926static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004927sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004928{
4929 long n;
4930
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004931 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004932 return -EINVAL;
4933
4934 if (n != 0 && n != 1)
4935 return -EINVAL;
4936
4937 mddev->parallel_resync = n;
4938
4939 if (mddev->sync_thread)
4940 wake_up(&resync_wait);
4941
4942 return len;
4943}
4944
4945/* force parallel resync, even with shared block devices */
4946static struct md_sysfs_entry md_sync_force_parallel =
4947__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4948 sync_force_parallel_show, sync_force_parallel_store);
4949
4950static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004951sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004952{
4953 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004954 if (mddev->curr_resync == 0)
4955 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004956 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4957 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004958 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004959 db = resync - mddev->resync_mark_cnt;
4960 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004961}
4962
NeilBrown80ca3a42006-07-10 04:44:18 -07004963static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004964
4965static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004966sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004967{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004968 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004969
NeilBrownacb180b2009-04-14 16:28:34 +10004970 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4971 return sprintf(page, "none\n");
4972
NeilBrown72f36d52012-10-11 14:25:57 +11004973 if (mddev->curr_resync == 1 ||
4974 mddev->curr_resync == 2)
4975 return sprintf(page, "delayed\n");
4976
NeilBrownc804cde2012-05-21 09:28:33 +10004977 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4978 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004979 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004980 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004981 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004982
NeilBrownacb180b2009-04-14 16:28:34 +10004983 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004984 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004985}
4986
NeilBrown750f1992014-09-30 08:53:05 +10004987static struct md_sysfs_entry md_sync_completed =
4988 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004989
NeilBrowne464eaf2006-03-27 01:18:14 -08004990static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004991min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004992{
4993 return sprintf(page, "%llu\n",
4994 (unsigned long long)mddev->resync_min);
4995}
4996static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004997min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004998{
4999 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11005000 int err;
NeilBrown23da4222014-12-15 12:57:01 +11005001
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005002 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10005003 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11005004
5005 spin_lock(&mddev->lock);
5006 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10005007 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11005008 goto out_unlock;
5009
5010 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10005011 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11005012 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10005013
NeilBrown50c37b12015-03-23 17:36:38 +11005014 /* Round down to multiple of 4K for safety */
5015 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11005016 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10005017
NeilBrown23da4222014-12-15 12:57:01 +11005018out_unlock:
5019 spin_unlock(&mddev->lock);
5020 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10005021}
5022
5023static struct md_sysfs_entry md_min_sync =
5024__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
5025
5026static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005027max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08005028{
5029 if (mddev->resync_max == MaxSector)
5030 return sprintf(page, "max\n");
5031 else
5032 return sprintf(page, "%llu\n",
5033 (unsigned long long)mddev->resync_max);
5034}
5035static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005036max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08005037{
NeilBrown23da4222014-12-15 12:57:01 +11005038 int err;
5039 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08005040 if (strncmp(buf, "max", 3) == 0)
5041 mddev->resync_max = MaxSector;
5042 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10005043 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11005044 int chunk;
5045
5046 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005047 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11005048 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10005049 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11005050 goto out_unlock;
5051
5052 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08005053 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10005054 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08005055 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11005056 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08005057
5058 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11005059 chunk = mddev->chunk_sectors;
5060 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10005061 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11005062
5063 err = -EINVAL;
5064 if (sector_div(temp, chunk))
5065 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08005066 }
5067 mddev->resync_max = max;
5068 }
5069 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11005070 err = 0;
5071out_unlock:
5072 spin_unlock(&mddev->lock);
5073 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08005074}
5075
5076static struct md_sysfs_entry md_max_sync =
5077__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
5078
5079static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005080suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08005081{
5082 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
5083}
5084
5085static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005086suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08005087{
NeilBrownb03e0cc2017-10-19 12:49:15 +11005088 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005089 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08005090
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005091 err = kstrtoull(buf, 10, &new);
5092 if (err < 0)
5093 return err;
5094 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08005095 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11005096
NeilBrown67918752014-12-15 12:57:01 +11005097 err = mddev_lock(mddev);
5098 if (err)
5099 return err;
5100 err = -EINVAL;
5101 if (mddev->pers == NULL ||
5102 mddev->pers->quiesce == NULL)
5103 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005104 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11005105 mddev->suspend_lo = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005106 mddev_resume(mddev);
5107
NeilBrown67918752014-12-15 12:57:01 +11005108 err = 0;
5109unlock:
5110 mddev_unlock(mddev);
5111 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08005112}
5113static struct md_sysfs_entry md_suspend_lo =
5114__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
5115
NeilBrowne464eaf2006-03-27 01:18:14 -08005116static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005117suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08005118{
5119 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
5120}
5121
5122static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005123suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08005124{
NeilBrownb03e0cc2017-10-19 12:49:15 +11005125 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005126 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08005127
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005128 err = kstrtoull(buf, 10, &new);
5129 if (err < 0)
5130 return err;
5131 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08005132 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11005133
NeilBrown67918752014-12-15 12:57:01 +11005134 err = mddev_lock(mddev);
5135 if (err)
5136 return err;
5137 err = -EINVAL;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005138 if (mddev->pers == NULL)
NeilBrown67918752014-12-15 12:57:01 +11005139 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005140
5141 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11005142 mddev->suspend_hi = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11005143 mddev_resume(mddev);
5144
NeilBrown67918752014-12-15 12:57:01 +11005145 err = 0;
5146unlock:
5147 mddev_unlock(mddev);
5148 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08005149}
5150static struct md_sysfs_entry md_suspend_hi =
5151__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
5152
NeilBrown08a02ec2007-05-09 02:35:38 -07005153static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005154reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07005155{
5156 if (mddev->reshape_position != MaxSector)
5157 return sprintf(page, "%llu\n",
5158 (unsigned long long)mddev->reshape_position);
5159 strcpy(page, "none\n");
5160 return 5;
5161}
5162
5163static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005164reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07005165{
NeilBrownc6563a82012-05-21 09:27:00 +10005166 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005167 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005168 int err;
NeilBrown67918752014-12-15 12:57:01 +11005169
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005170 err = kstrtoull(buf, 10, &new);
5171 if (err < 0)
5172 return err;
5173 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07005174 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11005175 err = mddev_lock(mddev);
5176 if (err)
5177 return err;
5178 err = -EBUSY;
5179 if (mddev->pers)
5180 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07005181 mddev->reshape_position = new;
5182 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005183 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07005184 mddev->new_level = mddev->level;
5185 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10005186 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10005187 rdev_for_each(rdev, mddev)
5188 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11005189 err = 0;
5190unlock:
5191 mddev_unlock(mddev);
5192 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07005193}
5194
5195static struct md_sysfs_entry md_reshape_position =
5196__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
5197 reshape_position_store);
5198
Dan Williamsb522adc2009-03-31 15:00:31 +11005199static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10005200reshape_direction_show(struct mddev *mddev, char *page)
5201{
5202 return sprintf(page, "%s\n",
5203 mddev->reshape_backwards ? "backwards" : "forwards");
5204}
5205
5206static ssize_t
5207reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
5208{
5209 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11005210 int err;
5211
NeilBrown2c810cd2012-05-21 09:27:00 +10005212 if (cmd_match(buf, "forwards"))
5213 backwards = 0;
5214 else if (cmd_match(buf, "backwards"))
5215 backwards = 1;
5216 else
5217 return -EINVAL;
5218 if (mddev->reshape_backwards == backwards)
5219 return len;
5220
NeilBrown67918752014-12-15 12:57:01 +11005221 err = mddev_lock(mddev);
5222 if (err)
5223 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10005224 /* check if we are allowed to change */
5225 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11005226 err = -EBUSY;
5227 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10005228 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11005229 err = -EINVAL;
5230 else
5231 mddev->reshape_backwards = backwards;
5232 mddev_unlock(mddev);
5233 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10005234}
5235
5236static struct md_sysfs_entry md_reshape_direction =
5237__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5238 reshape_direction_store);
5239
5240static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005241array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11005242{
5243 if (mddev->external_size)
5244 return sprintf(page, "%llu\n",
5245 (unsigned long long)mddev->array_sectors/2);
5246 else
5247 return sprintf(page, "default\n");
5248}
5249
5250static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005251array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11005252{
5253 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11005254 int err;
5255
5256 err = mddev_lock(mddev);
5257 if (err)
5258 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11005259
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005260 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08005261 if (mddev_is_clustered(mddev)) {
5262 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005263 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08005264 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005265
Dan Williamsb522adc2009-03-31 15:00:31 +11005266 if (strncmp(buf, "default", 7) == 0) {
5267 if (mddev->pers)
5268 sectors = mddev->pers->size(mddev, 0, 0);
5269 else
5270 sectors = mddev->array_sectors;
5271
5272 mddev->external_size = 0;
5273 } else {
5274 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11005275 err = -EINVAL;
5276 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5277 err = -E2BIG;
5278 else
5279 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11005280 }
5281
NeilBrown67918752014-12-15 12:57:01 +11005282 if (!err) {
5283 mddev->array_sectors = sectors;
5284 if (mddev->pers) {
5285 set_capacity(mddev->gendisk, mddev->array_sectors);
5286 revalidate_disk(mddev->gendisk);
5287 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11005288 }
NeilBrown67918752014-12-15 12:57:01 +11005289 mddev_unlock(mddev);
5290 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11005291}
5292
5293static struct md_sysfs_entry md_array_size =
5294__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5295 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005296
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005297static ssize_t
5298consistency_policy_show(struct mddev *mddev, char *page)
5299{
5300 int ret;
5301
5302 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5303 ret = sprintf(page, "journal\n");
5304 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5305 ret = sprintf(page, "ppl\n");
5306 } else if (mddev->bitmap) {
5307 ret = sprintf(page, "bitmap\n");
5308 } else if (mddev->pers) {
5309 if (mddev->pers->sync_request)
5310 ret = sprintf(page, "resync\n");
5311 else
5312 ret = sprintf(page, "none\n");
5313 } else {
5314 ret = sprintf(page, "unknown\n");
5315 }
5316
5317 return ret;
5318}
5319
5320static ssize_t
5321consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5322{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005323 int err = 0;
5324
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005325 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005326 if (mddev->pers->change_consistency_policy)
5327 err = mddev->pers->change_consistency_policy(mddev, buf);
5328 else
5329 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005330 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5331 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005332 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005333 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005334 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005335
5336 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005337}
5338
5339static struct md_sysfs_entry md_consistency_policy =
5340__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5341 consistency_policy_store);
5342
Guoqing Jiang9a567842019-07-24 11:09:19 +02005343static ssize_t fail_last_dev_show(struct mddev *mddev, char *page)
5344{
5345 return sprintf(page, "%d\n", mddev->fail_last_dev);
5346}
5347
5348/*
5349 * Setting fail_last_dev to true to allow last device to be forcibly removed
5350 * from RAID1/RAID10.
5351 */
5352static ssize_t
5353fail_last_dev_store(struct mddev *mddev, const char *buf, size_t len)
5354{
5355 int ret;
5356 bool value;
5357
5358 ret = kstrtobool(buf, &value);
5359 if (ret)
5360 return ret;
5361
5362 if (value != mddev->fail_last_dev)
5363 mddev->fail_last_dev = value;
5364
5365 return len;
5366}
5367static struct md_sysfs_entry md_fail_last_dev =
5368__ATTR(fail_last_dev, S_IRUGO | S_IWUSR, fail_last_dev_show,
5369 fail_last_dev_store);
5370
Guoqing Jiang3938f5f2019-12-23 10:48:56 +01005371static ssize_t serialize_policy_show(struct mddev *mddev, char *page)
5372{
5373 if (mddev->pers == NULL || (mddev->pers->level != 1))
5374 return sprintf(page, "n/a\n");
5375 else
5376 return sprintf(page, "%d\n", mddev->serialize_policy);
5377}
5378
5379/*
5380 * Setting serialize_policy to true to enforce write IO is not reordered
5381 * for raid1.
5382 */
5383static ssize_t
5384serialize_policy_store(struct mddev *mddev, const char *buf, size_t len)
5385{
5386 int err;
5387 bool value;
5388
5389 err = kstrtobool(buf, &value);
5390 if (err)
5391 return err;
5392
5393 if (value == mddev->serialize_policy)
5394 return len;
5395
5396 err = mddev_lock(mddev);
5397 if (err)
5398 return err;
5399 if (mddev->pers == NULL || (mddev->pers->level != 1)) {
5400 pr_err("md: serialize_policy is only effective for raid1\n");
5401 err = -EINVAL;
5402 goto unlock;
5403 }
5404
5405 mddev_suspend(mddev);
5406 if (value)
5407 mddev_create_serial_pool(mddev, NULL, true);
5408 else
5409 mddev_destroy_serial_pool(mddev, NULL, true);
5410 mddev->serialize_policy = value;
5411 mddev_resume(mddev);
5412unlock:
5413 mddev_unlock(mddev);
5414 return err ?: len;
5415}
5416
5417static struct md_sysfs_entry md_serialize_policy =
5418__ATTR(serialize_policy, S_IRUGO | S_IWUSR, serialize_policy_show,
5419 serialize_policy_store);
5420
5421
NeilBrowneae17012005-11-08 21:39:23 -08005422static struct attribute *md_default_attrs[] = {
5423 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005424 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005425 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005426 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005427 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005428 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005429 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005430 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005431 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005432 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005433 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005434 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005435 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005436 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005437 &md_consistency_policy.attr,
Guoqing Jiang9a567842019-07-24 11:09:19 +02005438 &md_fail_last_dev.attr,
Guoqing Jiang3938f5f2019-12-23 10:48:56 +01005439 &md_serialize_policy.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005440 NULL,
5441};
5442
5443static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005444 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005445 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005446 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005447 &md_sync_min.attr,
5448 &md_sync_max.attr,
5449 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005450 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005451 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005452 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005453 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005454 &md_suspend_lo.attr,
5455 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005456 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005457 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005458 NULL,
5459};
NeilBrown411036f2005-11-08 21:39:40 -08005460static struct attribute_group md_redundancy_group = {
5461 .name = NULL,
5462 .attrs = md_redundancy_attrs,
5463};
5464
NeilBrowneae17012005-11-08 21:39:23 -08005465static ssize_t
5466md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5467{
5468 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005469 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005470 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005471
5472 if (!entry->show)
5473 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005474 spin_lock(&all_mddevs_lock);
5475 if (list_empty(&mddev->all_mddevs)) {
5476 spin_unlock(&all_mddevs_lock);
5477 return -EBUSY;
5478 }
5479 mddev_get(mddev);
5480 spin_unlock(&all_mddevs_lock);
5481
NeilBrownb7b17c92014-12-15 12:56:59 +11005482 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005483 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005484 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005485}
5486
5487static ssize_t
5488md_attr_store(struct kobject *kobj, struct attribute *attr,
5489 const char *page, size_t length)
5490{
5491 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005492 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005493 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005494
5495 if (!entry->store)
5496 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005497 if (!capable(CAP_SYS_ADMIN))
5498 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005499 spin_lock(&all_mddevs_lock);
5500 if (list_empty(&mddev->all_mddevs)) {
5501 spin_unlock(&all_mddevs_lock);
5502 return -EBUSY;
5503 }
5504 mddev_get(mddev);
5505 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005506 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005507 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005508 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005509}
5510
5511static void md_free(struct kobject *ko)
5512{
NeilBrownfd01b882011-10-11 16:47:53 +11005513 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005514
5515 if (mddev->sysfs_state)
5516 sysfs_put(mddev->sysfs_state);
5517
Bart Van Assched8115c352018-02-28 10:15:29 -08005518 if (mddev->gendisk)
5519 del_gendisk(mddev->gendisk);
NeilBrown6cd18e72015-04-27 14:12:22 +10005520 if (mddev->queue)
5521 blk_cleanup_queue(mddev->queue);
Bart Van Assched8115c352018-02-28 10:15:29 -08005522 if (mddev->gendisk)
NeilBrowna21d1502009-01-09 08:31:09 +11005523 put_disk(mddev->gendisk);
NeilBrown4ad23a972017-03-15 14:05:14 +11005524 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005525
Kent Overstreet28dec872018-06-07 20:52:54 -04005526 bioset_exit(&mddev->bio_set);
5527 bioset_exit(&mddev->sync_set);
NeilBrowneae17012005-11-08 21:39:23 -08005528 kfree(mddev);
5529}
5530
Emese Revfy52cf25d2010-01-19 02:58:23 +01005531static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005532 .show = md_attr_show,
5533 .store = md_attr_store,
5534};
5535static struct kobj_type md_ktype = {
5536 .release = md_free,
5537 .sysfs_ops = &md_sysfs_ops,
5538 .default_attrs = md_default_attrs,
5539};
5540
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541int mdp_major = 0;
5542
Dan Williams5fd3a172009-03-04 00:57:25 -07005543static void mddev_delayed_delete(struct work_struct *ws)
5544{
NeilBrownfd01b882011-10-11 16:47:53 +11005545 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005546
NeilBrown43a70502009-12-14 12:49:55 +11005547 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005548 kobject_del(&mddev->kobj);
5549 kobject_put(&mddev->kobj);
5550}
5551
NeilBrown4ad23a972017-03-15 14:05:14 +11005552static void no_op(struct percpu_ref *r) {}
5553
NeilBrowna415c0f2017-06-05 16:05:13 +10005554int mddev_init_writes_pending(struct mddev *mddev)
5555{
5556 if (mddev->writes_pending.percpu_count_ptr)
5557 return 0;
Roman Gushchinddde2af2019-05-07 10:01:49 -07005558 if (percpu_ref_init(&mddev->writes_pending, no_op,
5559 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL) < 0)
NeilBrowna415c0f2017-06-05 16:05:13 +10005560 return -ENOMEM;
5561 /* We want to start with the refcount at zero */
5562 percpu_ref_put(&mddev->writes_pending);
5563 return 0;
5564}
5565EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5566
NeilBrownefeb53c2009-01-09 08:31:10 +11005567static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568{
NeilBrown039b7222017-04-12 16:26:13 +10005569 /*
5570 * If dev is zero, name is the name of a device to allocate with
5571 * an arbitrary minor number. It will be "md_???"
5572 * If dev is non-zero it must be a device number with a MAJOR of
5573 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5574 * the device is being created by opening a node in /dev.
5575 * If "name" is not NULL, the device is being created by
5576 * writing to /sys/module/md_mod/parameters/new_array.
5577 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005578 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005579 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005581 int partitioned;
5582 int shift;
5583 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005584 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585
5586 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005587 return -ENODEV;
5588
5589 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5590 shift = partitioned ? MdpMinorShift : 0;
5591 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
Tejun Heoe804ac72010-10-15 15:36:08 +02005593 /* wait for any previous instance of this device to be
5594 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005595 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005596 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005597
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005598 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005599 error = -EEXIST;
5600 if (mddev->gendisk)
5601 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005602
NeilBrown039b7222017-04-12 16:26:13 +10005603 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005604 /* Need to ensure that 'name' is not a duplicate.
5605 */
NeilBrownfd01b882011-10-11 16:47:53 +11005606 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005607 spin_lock(&all_mddevs_lock);
5608
5609 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5610 if (mddev2->gendisk &&
5611 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5612 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005613 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005614 }
5615 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 }
NeilBrown039b7222017-04-12 16:26:13 +10005617 if (name && dev)
5618 /*
5619 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5620 */
5621 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005622
NeilBrown0909dc42009-07-01 12:27:21 +10005623 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005624 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005625 if (!mddev->queue)
5626 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005627 mddev->queue->queuedata = mddev;
5628
NeilBrown409c57f2009-03-31 14:39:39 +11005629 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005630 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005631
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 disk = alloc_disk(1 << shift);
5633 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005634 blk_cleanup_queue(mddev->queue);
5635 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005636 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005638 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005640 if (name)
5641 strcpy(disk->disk_name, name);
5642 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005644 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 disk->fops = &md_fops;
5647 disk->private_data = mddev;
5648 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005649 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005650 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005651 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005652 * remove it now.
5653 */
5654 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005656 /* As soon as we call add_disk(), another thread could get
5657 * through to md_open, so make sure it doesn't get too far
5658 */
5659 mutex_lock(&mddev->open_mutex);
5660 add_disk(disk);
5661
Kent Overstreet28dec872018-06-07 20:52:54 -04005662 error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005663 if (error) {
5664 /* This isn't possible, but as kobject_init_and_add is marked
5665 * __must_check, we must do something with the result
5666 */
NeilBrown9d487392016-11-02 14:16:49 +11005667 pr_debug("md: cannot register %s/md - name in use\n",
5668 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005669 error = 0;
5670 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005671 if (mddev->kobj.sd &&
5672 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005673 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005674 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005675 abort:
5676 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005677 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005678 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005679 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005680 }
NeilBrownd3374822009-01-09 08:31:10 +11005681 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005682 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005683}
5684
5685static struct kobject *md_probe(dev_t dev, int *part, void *data)
5686{
NeilBrown78b63502017-04-12 16:26:13 +10005687 if (create_on_open)
5688 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 return NULL;
5690}
5691
Kees Cooke4dca7b2017-10-17 19:04:42 -07005692static int add_named_array(const char *val, const struct kernel_param *kp)
NeilBrownefeb53c2009-01-09 08:31:10 +11005693{
NeilBrown039b7222017-04-12 16:26:13 +10005694 /*
5695 * val must be "md_*" or "mdNNN".
5696 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005697 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005698 * For "mdNNN" we allocate an array with the minor number NNN
5699 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005700 */
5701 int len = strlen(val);
5702 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005703 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005704
5705 while (len && val[len-1] == '\n')
5706 len--;
5707 if (len >= DISK_NAME_LEN)
5708 return -E2BIG;
5709 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005710 if (strncmp(buf, "md_", 3) == 0)
5711 return md_alloc(0, buf);
5712 if (strncmp(buf, "md", 2) == 0 &&
5713 isdigit(buf[2]) &&
5714 kstrtoul(buf+2, 10, &devnum) == 0 &&
5715 devnum <= MINORMASK)
5716 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5717
5718 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005719}
5720
Kees Cook8376d3c2017-10-16 17:01:48 -07005721static void md_safemode_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722{
Kees Cook8376d3c2017-10-16 17:01:48 -07005723 struct mddev *mddev = from_timer(mddev, t, safemode_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724
NeilBrown4ad23a972017-03-15 14:05:14 +11005725 mddev->safemode = 1;
5726 if (mddev->external)
5727 sysfs_notify_dirent_safe(mddev->sysfs_state);
5728
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 md_wakeup_thread(mddev->thread);
5730}
5731
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005732static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733
NeilBrownfd01b882011-10-11 16:47:53 +11005734int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735{
NeilBrown2604b702006-01-06 00:20:36 -08005736 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005737 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005738 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739
NeilBrowna757e642005-04-16 15:26:42 -07005740 if (list_empty(&mddev->disks))
5741 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743
5744 if (mddev->pers)
5745 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005746 /* Cannot run until previous stop completes properly */
5747 if (mddev->sysfs_active)
5748 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005749
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 /*
5751 * Analyze all RAID superblock(s)
5752 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005753 if (!mddev->raid_disks) {
5754 if (!mddev->persistent)
5755 return -EINVAL;
Yufen Yu6a5cb532019-10-16 16:00:03 +08005756 err = analyze_sbs(mddev);
5757 if (err)
5758 return -EINVAL;
NeilBrown1ec4a932008-02-06 01:39:53 -08005759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760
NeilBrownd9d166c2006-01-06 00:20:51 -08005761 if (mddev->level != LEVEL_NONE)
5762 request_module("md-level-%d", mddev->level);
5763 else if (mddev->clevel[0])
5764 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765
5766 /*
5767 * Drop all container device buffers, from now on
5768 * the only valid external interface is through the md
5769 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 */
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005771 mddev->has_superblocks = false;
NeilBrowndafb20f2012-03-19 12:46:39 +11005772 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005773 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 continue;
5775 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005776 invalidate_bdev(rdev->bdev);
NeilBrown97b20ef2017-04-13 08:53:48 +10005777 if (mddev->ro != 1 &&
5778 (bdev_read_only(rdev->bdev) ||
5779 bdev_read_only(rdev->meta_bdev))) {
5780 mddev->ro = 1;
5781 if (mddev->gendisk)
5782 set_disk_ro(mddev->gendisk, 1);
5783 }
NeilBrownf0d76d72007-07-17 04:06:12 -07005784
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005785 if (rdev->sb_page)
5786 mddev->has_superblocks = true;
5787
NeilBrownf0d76d72007-07-17 04:06:12 -07005788 /* perform some consistency tests on the device.
5789 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005790 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005791 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005792 if (rdev->meta_bdev) {
5793 /* Nothing to check */;
5794 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005795 if (mddev->dev_sectors &&
5796 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005797 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005798 pr_warn("md: %s: data overlaps metadata\n",
5799 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005800 return -EINVAL;
5801 }
5802 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005803 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005804 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005805 pr_warn("md: %s: metadata overlaps data\n",
5806 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005807 return -EINVAL;
5808 }
5809 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005810 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 }
5812
Kent Overstreetafeee512018-05-20 18:25:52 -04005813 if (!bioset_initialized(&mddev->bio_set)) {
5814 err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5815 if (err)
5816 return err;
Ming Lei10273172017-02-14 23:29:00 +08005817 }
Kent Overstreetafeee512018-05-20 18:25:52 -04005818 if (!bioset_initialized(&mddev->sync_set)) {
5819 err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5820 if (err)
Kent Overstreet28dec872018-06-07 20:52:54 -04005821 return err;
NeilBrown5a850712017-06-21 09:12:21 +10005822 }
NeilBrowna167f662010-10-26 18:31:13 +11005823
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005825 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005826 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005828 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005829 pr_warn("md: personality for level %d is not loaded!\n",
5830 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005831 else
NeilBrown9d487392016-11-02 14:16:49 +11005832 pr_warn("md: personality for level %s is not loaded!\n",
5833 mddev->clevel);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005834 err = -EINVAL;
5835 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005838 if (mddev->level != pers->level) {
5839 mddev->level = pers->level;
5840 mddev->new_level = pers->level;
5841 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005842 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
NeilBrownf6705572006-03-27 01:18:11 -08005844 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005845 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005846 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005847 module_put(pers->owner);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005848 err = -EINVAL;
5849 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08005850 }
5851
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005852 if (pers->sync_request) {
5853 /* Warn if this is a potentially silly
5854 * configuration.
5855 */
5856 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005857 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005858 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005859
NeilBrowndafb20f2012-03-19 12:46:39 +11005860 rdev_for_each(rdev, mddev)
5861 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005862 if (rdev < rdev2 &&
5863 rdev->bdev->bd_contains ==
5864 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005865 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5866 mdname(mddev),
5867 bdevname(rdev->bdev,b),
5868 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005869 warned = 1;
5870 }
5871 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005872
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005873 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005874 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005875 }
5876
NeilBrown657390d2005-08-26 18:34:16 -07005877 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005878 /* may be over-ridden by personality */
5879 mddev->resync_max_sectors = mddev->dev_sectors;
5880
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005881 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
NeilBrown0f9552b52009-12-30 12:08:50 +11005883 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005884 mddev->ro = 2; /* read-only, but switch on first write */
5885
NeilBrown36d091f2014-12-15 12:56:58 +11005886 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005887 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005888 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005889 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005890 WARN_ONCE(!mddev->external_size,
5891 "%s: default size too small, but 'external_size' not in effect?\n",
5892 __func__);
5893 pr_warn("md: invalid array_size %llu > default size %llu\n",
5894 (unsigned long long)mddev->array_sectors / 2,
5895 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005896 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005897 }
NeilBrown36d091f2014-12-15 12:56:58 +11005898 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005899 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005900 struct bitmap *bitmap;
5901
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005902 bitmap = md_bitmap_create(mddev, -1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005903 if (IS_ERR(bitmap)) {
5904 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005905 pr_warn("%s: failed to create bitmap (%d)\n",
5906 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005907 } else
5908 mddev->bitmap = bitmap;
5909
NeilBrownb15c2e52006-01-06 00:20:16 -08005910 }
Guoqing Jiangd4945492019-06-14 17:10:39 +08005911 if (err)
5912 goto bitmap_abort;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005913
5914 if (mddev->bitmap_info.max_write_behind > 0) {
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01005915 bool create_pool = false;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005916
5917 rdev_for_each(rdev, mddev) {
5918 if (test_bit(WriteMostly, &rdev->flags) &&
Guoqing Jiang404659c2019-12-23 10:48:53 +01005919 rdev_init_serial(rdev))
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01005920 create_pool = true;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005921 }
Guoqing Jiang3e173ab2019-12-23 10:48:54 +01005922 if (create_pool && mddev->serial_info_pool == NULL) {
Guoqing Jiang404659c2019-12-23 10:48:53 +01005923 mddev->serial_info_pool =
5924 mempool_create_kmalloc_pool(NR_SERIAL_INFOS,
5925 sizeof(struct serial_info));
5926 if (!mddev->serial_info_pool) {
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005927 err = -ENOMEM;
Guoqing Jiangd4945492019-06-14 17:10:39 +08005928 goto bitmap_abort;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005929 }
5930 }
5931 }
5932
NeilBrown5c675f82014-12-15 12:56:56 +11005933 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005934 bool nonrot = true;
5935
5936 rdev_for_each(rdev, mddev) {
5937 if (rdev->raid_disk >= 0 &&
5938 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5939 nonrot = false;
5940 break;
5941 }
5942 }
5943 if (mddev->degraded)
5944 nonrot = false;
5945 if (nonrot)
Bart Van Assche8b904b52018-03-07 17:10:10 -08005946 blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue);
Shaohua Libb086a82016-09-30 09:45:40 -07005947 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08005948 blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01005949 mddev->queue->backing_dev_info->congested_data = mddev;
5950 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11005951 }
NeilBrown36d091f2014-12-15 12:56:58 +11005952 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005953 if (mddev->kobj.sd &&
5954 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005955 pr_warn("md: cannot register extra attributes for %s\n",
5956 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005957 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005958 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005959 mddev->ro = 0;
5960
Robert Becker1e509152009-12-14 12:49:58 +11005961 atomic_set(&mddev->max_corr_read_errors,
5962 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005964 if (mddev_is_clustered(mddev))
5965 mddev->safemode_delay = 0;
5966 else
5967 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005969 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005970 spin_lock(&mddev->lock);
5971 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005972 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005973 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005974 if (rdev->raid_disk >= 0)
Yufen Yue5b521e2019-06-14 15:41:07 -07005975 sysfs_link_rdev(mddev, rdev); /* failure here is OK */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005976
NeilBrowna4a3d262015-07-17 11:57:30 +10005977 if (mddev->degraded && !mddev->ro)
5978 /* This ensures that recovering status is reported immediately
5979 * via sysfs - until a lack of spares is confirmed.
5980 */
5981 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005983
Shaohua Li29530792016-12-08 15:48:19 -08005984 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07005985 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986
NeilBrownd7603b72006-01-06 00:20:30 -08005987 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 return 0;
Xiao Nib1261942018-01-24 12:17:38 +08005989
Guoqing Jiangd4945492019-06-14 17:10:39 +08005990bitmap_abort:
5991 mddev_detach(mddev);
5992 if (mddev->private)
5993 pers->free(mddev, mddev->private);
5994 mddev->private = NULL;
5995 module_put(pers->owner);
5996 md_bitmap_destroy(mddev);
Xiao Nib1261942018-01-24 12:17:38 +08005997abort:
NeilBrown4bc034d2019-03-29 10:46:16 -07005998 bioset_exit(&mddev->bio_set);
5999 bioset_exit(&mddev->sync_set);
Xiao Nib1261942018-01-24 12:17:38 +08006000 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001}
NeilBrown390ee602010-06-01 19:37:27 +10006002EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003
NeilBrownfd01b882011-10-11 16:47:53 +11006004static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11006005{
6006 int err;
6007
NeilBrown9d4b45d2019-08-20 10:21:09 +10006008 set_bit(MD_NOT_READY, &mddev->flags);
NeilBrownfe60b012010-03-29 11:10:42 +11006009 err = md_run(mddev);
6010 if (err)
6011 goto out;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006012 err = md_bitmap_load(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006013 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006014 md_bitmap_destroy(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006015 goto out;
6016 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05006017
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006018 if (mddev_is_clustered(mddev))
6019 md_allow_write(mddev);
6020
Song Liud5d885f2017-11-19 22:17:01 -08006021 /* run start up tasks that require md_thread */
6022 md_start(mddev);
6023
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05006024 md_wakeup_thread(mddev->thread);
6025 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
6026
NeilBrownfe60b012010-03-29 11:10:42 +11006027 set_capacity(mddev->gendisk, mddev->array_sectors);
6028 revalidate_disk(mddev->gendisk);
NeilBrown9d4b45d2019-08-20 10:21:09 +10006029 clear_bit(MD_NOT_READY, &mddev->flags);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006030 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11006031 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrown9d4b45d2019-08-20 10:21:09 +10006032 sysfs_notify_dirent_safe(mddev->sysfs_state);
6033 sysfs_notify_dirent_safe(mddev->sysfs_action);
6034 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownfe60b012010-03-29 11:10:42 +11006035out:
NeilBrown9d4b45d2019-08-20 10:21:09 +10006036 clear_bit(MD_NOT_READY, &mddev->flags);
NeilBrownfe60b012010-03-29 11:10:42 +11006037 return err;
6038}
6039
Song Liud5d885f2017-11-19 22:17:01 -08006040int md_start(struct mddev *mddev)
6041{
6042 int ret = 0;
6043
6044 if (mddev->pers->start) {
6045 set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6046 md_wakeup_thread(mddev->thread);
6047 ret = mddev->pers->start(mddev);
6048 clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
6049 md_wakeup_thread(mddev->sync_thread);
6050 }
6051 return ret;
6052}
6053EXPORT_SYMBOL_GPL(md_start);
6054
NeilBrownfd01b882011-10-11 16:47:53 +11006055static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056{
6057 struct gendisk *disk = mddev->gendisk;
NeilBrown97b20ef2017-04-13 08:53:48 +10006058 struct md_rdev *rdev;
6059 bool has_journal = false;
6060 bool has_readonly = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
Andre Noll80fab1d2008-07-11 22:02:21 +10006062 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10006064 return -ENXIO;
6065 if (!mddev->pers)
6066 return -EINVAL;
6067 if (!mddev->ro)
6068 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07006069
NeilBrown97b20ef2017-04-13 08:53:48 +10006070 rcu_read_lock();
6071 rdev_for_each_rcu(rdev, mddev) {
6072 if (test_bit(Journal, &rdev->flags) &&
6073 !test_bit(Faulty, &rdev->flags))
6074 has_journal = true;
6075 if (bdev_read_only(rdev->bdev))
6076 has_readonly = true;
Song Liu339421d2015-10-08 21:54:13 -07006077 }
NeilBrown97b20ef2017-04-13 08:53:48 +10006078 rcu_read_unlock();
6079 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
6080 /* Don't restart rw with journal missing/faulty */
6081 return -EINVAL;
6082 if (has_readonly)
6083 return -EROFS;
Song Liu339421d2015-10-08 21:54:13 -07006084
Andre Noll80fab1d2008-07-11 22:02:21 +10006085 mddev->safemode = 0;
6086 mddev->ro = 0;
6087 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11006088 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10006089 /* Kick recovery or resync if necessary */
6090 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6091 md_wakeup_thread(mddev->thread);
6092 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006093 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10006094 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095}
6096
NeilBrownfd01b882011-10-11 16:47:53 +11006097static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11006098{
6099 mddev->array_sectors = 0;
6100 mddev->external_size = 0;
6101 mddev->dev_sectors = 0;
6102 mddev->raid_disks = 0;
6103 mddev->recovery_cp = 0;
6104 mddev->resync_min = 0;
6105 mddev->resync_max = MaxSector;
6106 mddev->reshape_position = MaxSector;
6107 mddev->external = 0;
6108 mddev->persistent = 0;
6109 mddev->level = LEVEL_NONE;
6110 mddev->clevel[0] = 0;
6111 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006112 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006113 mddev->ro = 0;
6114 mddev->metadata_type[0] = 0;
6115 mddev->chunk_sectors = 0;
6116 mddev->ctime = mddev->utime = 0;
6117 mddev->layout = 0;
6118 mddev->max_disks = 0;
6119 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10006120 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006121 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006122 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006123 mddev->new_level = LEVEL_NONE;
6124 mddev->new_layout = 0;
6125 mddev->new_chunk_sectors = 0;
6126 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11006127 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11006128 mddev->suspend_lo = mddev->suspend_hi = 0;
6129 mddev->sync_speed_min = mddev->sync_speed_max = 0;
6130 mddev->recovery = 0;
6131 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006132 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006133 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006134 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10006135 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08006136 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11006137 mddev->bitmap_info.offset = 0;
6138 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10006139 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006140 mddev->bitmap_info.chunksize = 0;
6141 mddev->bitmap_info.daemon_sleep = 0;
6142 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08006143 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11006144}
6145
NeilBrownfd01b882011-10-11 16:47:53 +11006146static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11006147{
NeilBrown6b6204e2013-05-09 09:48:30 +10006148 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006149 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11006150 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11006151 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10006152 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11006153 }
6154
6155 del_timer_sync(&mddev->safemode_timer);
6156
Shaohua Li034e33f2016-11-21 10:29:19 -08006157 if (mddev->pers && mddev->pers->quiesce) {
6158 mddev->pers->quiesce(mddev, 1);
6159 mddev->pers->quiesce(mddev, 0);
6160 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006161 md_bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11006162
NeilBrownb6d428c2013-04-24 11:42:42 +10006163 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006164 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08006165 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11006166 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11006167 if (!mddev_is_clustered(mddev))
6168 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11006169 md_update_sb(mddev, 1);
6170 }
Guoqing Jiang69b00b52019-12-23 10:49:00 +01006171 /* disable policy to guarantee rdevs free resources for serialization */
6172 mddev->serialize_policy = 0;
6173 mddev_destroy_serial_pool(mddev, NULL, true);
NeilBrowna047e122010-03-29 12:07:53 +11006174}
NeilBrowndefad612011-01-14 09:14:33 +11006175
NeilBrownfd01b882011-10-11 16:47:53 +11006176void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11006177{
NeilBrown29f097c2013-11-14 17:54:51 +11006178 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11006179 __md_stop_writes(mddev);
6180 mddev_unlock(mddev);
6181}
NeilBrown390ee602010-06-01 19:37:27 +10006182EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11006183
NeilBrown5aa61f42014-12-15 12:56:57 +11006184static void mddev_detach(struct mddev *mddev)
6185{
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006186 md_bitmap_wait_behind_writes(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11006187 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11006188 mddev->pers->quiesce(mddev, 1);
6189 mddev->pers->quiesce(mddev, 0);
6190 }
6191 md_unregister_thread(&mddev->thread);
6192 if (mddev->queue)
6193 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
6194}
6195
NeilBrown5eff3c42012-11-19 10:47:48 +11006196static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11006197{
NeilBrown36d091f2014-12-15 12:56:58 +11006198 struct md_personality *pers = mddev->pers;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006199 md_bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11006200 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10006201 /* Ensure ->event_work is done */
6202 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11006203 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11006204 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11006205 spin_unlock(&mddev->lock);
6206 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10006207 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11006208 if (pers->sync_request && mddev->to_remove == NULL)
6209 mddev->to_remove = &md_redundancy_group;
6210 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11006211 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Jack Wang6aaa58c2018-10-19 16:21:31 +02006212}
6213
6214void md_stop(struct mddev *mddev)
6215{
6216 /* stop the array and free an attached data structures.
6217 * This is called from dm-raid
6218 */
6219 __md_stop(mddev);
Kent Overstreetafeee512018-05-20 18:25:52 -04006220 bioset_exit(&mddev->bio_set);
6221 bioset_exit(&mddev->sync_set);
NeilBrown5eff3c42012-11-19 10:47:48 +11006222}
6223
NeilBrown390ee602010-06-01 19:37:27 +10006224EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11006225
NeilBrowna05b7ea2012-07-19 15:59:18 +10006226static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11006227{
6228 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11006229 int did_freeze = 0;
6230
6231 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6232 did_freeze = 1;
6233 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6234 md_wakeup_thread(mddev->thread);
6235 }
NeilBrownf851b602014-12-11 10:02:10 +11006236 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11006237 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006238 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11006239 /* Thread might be blocked waiting for metadata update
6240 * which will now never happen */
6241 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11006242
Shaohua Li29530792016-12-08 15:48:19 -08006243 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10006244 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11006245 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006246 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
6247 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10006248 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08006249 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11006250 mddev_lock_nointr(mddev);
6251
NeilBrowna4bd82d2010-03-29 13:23:10 +11006252 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006253 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006254 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006255 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006256 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11006257 if (did_freeze) {
6258 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006259 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006260 md_wakeup_thread(mddev->thread);
6261 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11006262 err = -EBUSY;
6263 goto out;
6264 }
6265 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11006266 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11006267
6268 err = -ENXIO;
6269 if (mddev->ro==1)
6270 goto out;
6271 mddev->ro = 1;
6272 set_disk_ro(mddev->gendisk, 1);
6273 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006274 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6275 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006276 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11006277 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006278 }
6279out:
6280 mutex_unlock(&mddev->open_mutex);
6281 return err;
6282}
6283
NeilBrown9e653b62006-06-26 00:27:58 -07006284/* mode:
6285 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07006286 * 2 - stop but do not disassemble array
6287 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006288static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10006289 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11006292 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11006293 int did_freeze = 0;
6294
6295 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6296 did_freeze = 1;
6297 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6298 md_wakeup_thread(mddev->thread);
6299 }
NeilBrownf851b602014-12-11 10:02:10 +11006300 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11006301 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006302 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11006303 /* Thread might be blocked waiting for metadata update
6304 * which will now never happen */
6305 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11006306
NeilBrown30b8feb2013-11-14 15:16:17 +11006307 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006308 wait_event(resync_wait, (mddev->sync_thread == NULL &&
6309 !test_bit(MD_RECOVERY_RUNNING,
6310 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11006311 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312
NeilBrownc8c00a62009-08-10 12:50:52 +10006313 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006314 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006315 mddev->sysfs_active ||
6316 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006317 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006318 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10006319 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11006320 if (did_freeze) {
6321 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006322 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006323 md_wakeup_thread(mddev->thread);
6324 }
NeilBrown260fa032013-08-27 16:44:13 +10006325 return -EBUSY;
6326 }
NeilBrown6e17b022010-08-07 21:41:19 +10006327 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11006328 if (mddev->ro)
6329 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11006330
NeilBrowndefad612011-01-14 09:14:33 +11006331 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11006332 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01006333 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11006334
NeilBrowna4bd82d2010-03-29 13:23:10 +11006335 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10006336 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08006337
NeilBrowndafb20f2012-03-19 12:46:39 +11006338 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10006339 if (rdev->raid_disk >= 0)
6340 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10006341
NeilBrowna4bd82d2010-03-29 13:23:10 +11006342 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10006343 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006344 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006345 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08006346
NeilBrowna4bd82d2010-03-29 13:23:10 +11006347 if (mddev->ro)
6348 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10006349 } else
6350 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 /*
6352 * Free resources if final stop
6353 */
NeilBrown9e653b62006-06-26 00:27:58 -07006354 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006355 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
NeilBrownc3d97142009-12-14 12:49:52 +11006357 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11006358 struct file *f = mddev->bitmap_info.file;
6359 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11006360 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11006361 spin_unlock(&mddev->lock);
6362 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08006363 }
NeilBrownc3d97142009-12-14 12:49:52 +11006364 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08006365
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 export_array(mddev);
6367
NeilBrown6177b472010-03-29 11:37:13 +11006368 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11006369 if (mddev->hold_active == UNTIL_STOP)
6370 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006371 }
NeilBrownd7603b72006-01-06 00:20:30 -08006372 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006373 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10006374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375}
6376
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006377#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11006378static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379{
NeilBrown3cb03002011-10-11 16:45:26 +11006380 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 int err;
6382
NeilBrowna757e642005-04-16 15:26:42 -07006383 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385
NeilBrown9d487392016-11-02 14:16:49 +11006386 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387
NeilBrowndafb20f2012-03-19 12:46:39 +11006388 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11006390 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 }
NeilBrown9d487392016-11-02 14:16:49 +11006392 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393
NeilBrownd710e132008-10-13 11:55:12 +11006394 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006396 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10006397 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 }
6399}
6400
6401/*
6402 * lets try to run arrays based on all disks that have arrived
6403 * until now. (those are in pending_raid_disks)
6404 *
6405 * the method: pick the first pending disk, collect all disks with
6406 * the same UUID, remove all from the pending list and put them into
6407 * the 'same_array' list. Then order this list based on superblock
6408 * update time (freshest comes first), kick out 'old' disks and
6409 * compare superblocks. If everything's fine then run it.
6410 *
6411 * If "unit" is allocated, then bump its reference count
6412 */
6413static void autorun_devices(int part)
6414{
NeilBrown3cb03002011-10-11 16:45:26 +11006415 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006416 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 char b[BDEVNAME_SIZE];
6418
NeilBrown9d487392016-11-02 14:16:49 +11006419 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07006421 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08006423 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11006425 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426
NeilBrown9d487392016-11-02 14:16:49 +11006427 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006429 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006431 pr_debug("md: adding %s ...\n",
6432 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 list_move(&rdev->same_set, &candidates);
6434 }
6435 /*
6436 * now we have a set of devices, with all of them having
6437 * mostly sane superblocks. It's time to allocate the
6438 * mddev.
6439 */
NeilBrowne8703fe2006-10-03 01:15:59 -07006440 if (part) {
6441 dev = MKDEV(mdp_major,
6442 rdev0->preferred_minor << MdpMinorShift);
6443 unit = MINOR(dev) >> MdpMinorShift;
6444 } else {
6445 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6446 unit = MINOR(dev);
6447 }
6448 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11006449 pr_warn("md: unit number in %s is bad: %d\n",
6450 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 break;
6452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453
6454 md_probe(dev, NULL, NULL);
6455 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006456 if (!mddev || !mddev->gendisk) {
6457 if (mddev)
6458 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 break;
6460 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006461 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006462 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 else if (mddev->raid_disks || mddev->major_version
6464 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006465 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 mdname(mddev), bdevname(rdev0->bdev,b));
6467 mddev_unlock(mddev);
6468 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006469 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006470 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006471 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 list_del_init(&rdev->same_set);
6473 if (bind_rdev_to_array(rdev, mddev))
6474 export_rdev(rdev);
6475 }
6476 autorun_array(mddev);
6477 mddev_unlock(mddev);
6478 }
6479 /* on success, candidates will be empty, on error
6480 * it won't...
6481 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006482 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006483 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 mddev_put(mddev);
6487 }
NeilBrown9d487392016-11-02 14:16:49 +11006488 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006490#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491
NeilBrownf72ffdd2014-09-30 14:23:59 +10006492static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493{
6494 mdu_version_t ver;
6495
6496 ver.major = MD_MAJOR_VERSION;
6497 ver.minor = MD_MINOR_VERSION;
6498 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6499
6500 if (copy_to_user(arg, &ver, sizeof(ver)))
6501 return -EFAULT;
6502
6503 return 0;
6504}
6505
NeilBrownf72ffdd2014-09-30 14:23:59 +10006506static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507{
6508 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006509 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006510 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511
NeilBrown1ca69c42012-10-11 13:37:33 +11006512 nr = working = insync = failed = spare = 0;
6513 rcu_read_lock();
6514 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006516 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 failed++;
6518 else {
6519 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006520 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006521 insync++;
Song Liub347af82016-08-11 17:14:45 -07006522 else if (test_bit(Journal, &rdev->flags))
6523 /* TODO: add journal count to md_u.h */
6524 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525 else
6526 spare++;
6527 }
6528 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006529 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530
6531 info.major_version = mddev->major_version;
6532 info.minor_version = mddev->minor_version;
6533 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006534 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006536 info.size = mddev->dev_sectors / 2;
6537 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006538 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 info.nr_disks = nr;
6540 info.raid_disks = mddev->raid_disks;
6541 info.md_minor = mddev->md_minor;
6542 info.not_persistent= !mddev->persistent;
6543
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006544 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545 info.state = 0;
6546 if (mddev->in_sync)
6547 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006548 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006549 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006550 if (mddev_is_clustered(mddev))
6551 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006552 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553 info.working_disks = working;
6554 info.failed_disks = failed;
6555 info.spare_disks = spare;
6556
6557 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006558 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559
6560 if (copy_to_user(arg, &info, sizeof(info)))
6561 return -EFAULT;
6562
6563 return 0;
6564}
6565
NeilBrownf72ffdd2014-09-30 14:23:59 +10006566static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006567{
6568 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006569 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006570 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006571
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006572 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006573 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006574 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006575
NeilBrown32a76272005-06-21 17:17:14 -07006576 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006577 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006578 /* bitmap enabled */
6579 if (mddev->bitmap_info.file) {
6580 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6581 sizeof(file->pathname));
6582 if (IS_ERR(ptr))
6583 err = PTR_ERR(ptr);
6584 else
6585 memmove(file->pathname, ptr,
6586 sizeof(file->pathname)-(ptr-file->pathname));
6587 }
NeilBrown4af1a042014-12-15 12:57:00 +11006588 spin_unlock(&mddev->lock);
6589
6590 if (err == 0 &&
6591 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006592 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006593
NeilBrown32a76272005-06-21 17:17:14 -07006594 kfree(file);
6595 return err;
6596}
6597
NeilBrownf72ffdd2014-09-30 14:23:59 +10006598static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599{
6600 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006601 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602
6603 if (copy_from_user(&info, arg, sizeof(info)))
6604 return -EFAULT;
6605
NeilBrown1ca69c42012-10-11 13:37:33 +11006606 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006607 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 if (rdev) {
6609 info.major = MAJOR(rdev->bdev->bd_dev);
6610 info.minor = MINOR(rdev->bdev->bd_dev);
6611 info.raid_disk = rdev->raid_disk;
6612 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006613 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006615 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 info.state |= (1<<MD_DISK_ACTIVE);
6617 info.state |= (1<<MD_DISK_SYNC);
6618 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006619 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006620 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006621 if (test_bit(WriteMostly, &rdev->flags))
6622 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006623 if (test_bit(FailFast, &rdev->flags))
6624 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 } else {
6626 info.major = info.minor = 0;
6627 info.raid_disk = -1;
6628 info.state = (1<<MD_DISK_REMOVED);
6629 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006630 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631
6632 if (copy_to_user(arg, &info, sizeof(info)))
6633 return -EFAULT;
6634
6635 return 0;
6636}
6637
NeilBrownf72ffdd2014-09-30 14:23:59 +10006638static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639{
6640 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006641 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642 dev_t dev = MKDEV(info->major,info->minor);
6643
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006644 if (mddev_is_clustered(mddev) &&
6645 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006646 pr_warn("%s: Cannot add to clustered mddev.\n",
6647 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006648 return -EINVAL;
6649 }
6650
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6652 return -EOVERFLOW;
6653
6654 if (!mddev->raid_disks) {
6655 int err;
6656 /* expecting a device which has a superblock */
6657 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6658 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006659 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 PTR_ERR(rdev));
6661 return PTR_ERR(rdev);
6662 }
6663 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006664 struct md_rdev *rdev0
6665 = list_entry(mddev->disks.next,
6666 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006667 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 .load_super(rdev, rdev0, mddev->minor_version);
6669 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006670 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006671 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 bdevname(rdev0->bdev,b2));
6673 export_rdev(rdev);
6674 return -EINVAL;
6675 }
6676 }
6677 err = bind_rdev_to_array(rdev, mddev);
6678 if (err)
6679 export_rdev(rdev);
6680 return err;
6681 }
6682
6683 /*
6684 * add_new_disk can be used once the array is assembled
6685 * to add "hot spares". They must already have a superblock
6686 * written
6687 */
6688 if (mddev->pers) {
6689 int err;
6690 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006691 pr_warn("%s: personality does not support diskops!\n",
6692 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 return -EINVAL;
6694 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006695 if (mddev->persistent)
6696 rdev = md_import_device(dev, mddev->major_version,
6697 mddev->minor_version);
6698 else
6699 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006701 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702 PTR_ERR(rdev));
6703 return PTR_ERR(rdev);
6704 }
NeilBrown1a855a02010-12-09 16:36:28 +11006705 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006706 if (!mddev->persistent) {
6707 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006708 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006709 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006710 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006711 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006712 } else
NeilBrown41158c72005-06-21 17:17:25 -07006713 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006714 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006715 } else
6716 super_types[mddev->major_version].
6717 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006718 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006719 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006720 /* This was a hot-add request, but events doesn't
6721 * match, so reject it.
6722 */
6723 export_rdev(rdev);
6724 return -EINVAL;
6725 }
6726
NeilBrownb2d444d2005-11-08 21:39:31 -08006727 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006728 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6729 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006730 else
6731 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006732 if (info->state & (1<<MD_DISK_FAILFAST))
6733 set_bit(FailFast, &rdev->flags);
6734 else
6735 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006736
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006737 if (info->state & (1<<MD_DISK_JOURNAL)) {
6738 struct md_rdev *rdev2;
6739 bool has_journal = false;
6740
6741 /* make sure no existing journal disk */
6742 rdev_for_each(rdev2, mddev) {
6743 if (test_bit(Journal, &rdev2->flags)) {
6744 has_journal = true;
6745 break;
6746 }
6747 }
NeilBrown230b55f2017-10-17 14:24:09 +11006748 if (has_journal || mddev->bitmap) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006749 export_rdev(rdev);
6750 return -EBUSY;
6751 }
Song Liubac624f2015-08-13 14:31:55 -07006752 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006753 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006754 /*
6755 * check whether the device shows up in other nodes
6756 */
6757 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006758 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006759 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006760 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006761 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006762 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006763 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006764 export_rdev(rdev);
6765 return err;
6766 }
6767 }
6768 }
6769
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 rdev->raid_disk = -1;
6771 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006772
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 if (err)
6774 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006775
6776 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006777 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6778 if (!err) {
6779 err = md_cluster_ops->new_disk_ack(mddev,
6780 err == 0);
6781 if (err)
6782 md_kick_rdev_from_array(rdev);
6783 }
6784 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006785 if (err)
6786 md_cluster_ops->add_new_disk_cancel(mddev);
6787 else
6788 err = add_bound_rdev(rdev);
6789 }
6790
6791 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006792 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006793
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794 return err;
6795 }
6796
6797 /* otherwise, add_new_disk is only allowed
6798 * for major_version==0 superblocks
6799 */
6800 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006801 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802 return -EINVAL;
6803 }
6804
6805 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6806 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006807 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006809 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 PTR_ERR(rdev));
6811 return PTR_ERR(rdev);
6812 }
6813 rdev->desc_nr = info->number;
6814 if (info->raid_disk < mddev->raid_disks)
6815 rdev->raid_disk = info->raid_disk;
6816 else
6817 rdev->raid_disk = -1;
6818
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006820 if (info->state & (1<<MD_DISK_SYNC))
6821 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006823 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6824 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006825 if (info->state & (1<<MD_DISK_FAILFAST))
6826 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006827
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006829 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006830 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6831 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006832 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006833 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834
NeilBrown2bf071b2006-01-06 00:20:55 -08006835 err = bind_rdev_to_array(rdev, mddev);
6836 if (err) {
6837 export_rdev(rdev);
6838 return err;
6839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 }
6841
6842 return 0;
6843}
6844
NeilBrownf72ffdd2014-09-30 14:23:59 +10006845static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846{
6847 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006848 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849
Yufen Yuc42a0e22018-05-04 18:08:10 +08006850 if (!mddev->pers)
6851 return -ENODEV;
6852
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 rdev = find_rdev(mddev, dev);
6854 if (!rdev)
6855 return -ENXIO;
6856
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006857 if (rdev->raid_disk < 0)
6858 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006859
NeilBrown3ea8929d2013-04-24 11:42:41 +10006860 clear_bit(Blocked, &rdev->flags);
6861 remove_and_add_spares(mddev, rdev);
6862
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863 if (rdev->raid_disk >= 0)
6864 goto busy;
6865
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006866kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006867 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006868 md_cluster_ops->remove_disk(mddev, rdev);
6869
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006870 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006871 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006872 if (mddev->thread)
6873 md_wakeup_thread(mddev->thread);
6874 else
6875 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006876 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877
6878 return 0;
6879busy:
NeilBrown9d487392016-11-02 14:16:49 +11006880 pr_debug("md: cannot remove active disk %s from %s ...\n",
6881 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 return -EBUSY;
6883}
6884
NeilBrownf72ffdd2014-09-30 14:23:59 +10006885static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886{
6887 char b[BDEVNAME_SIZE];
6888 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006889 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890
6891 if (!mddev->pers)
6892 return -ENODEV;
6893
6894 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006895 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 mdname(mddev));
6897 return -EINVAL;
6898 }
6899 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006900 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 mdname(mddev));
6902 return -EINVAL;
6903 }
6904
NeilBrownd710e132008-10-13 11:55:12 +11006905 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006907 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 PTR_ERR(rdev));
6909 return -EINVAL;
6910 }
6911
6912 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006913 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006915 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916
NeilBrown8190e752009-06-18 08:48:58 +10006917 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918
NeilBrownb2d444d2005-11-08 21:39:31 -08006919 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006920 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 bdevname(rdev->bdev,b), mdname(mddev));
6922 err = -EINVAL;
6923 goto abort_export;
6924 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006925
NeilBrownb2d444d2005-11-08 21:39:31 -08006926 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006928 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006929 err = bind_rdev_to_array(rdev, mddev);
6930 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006931 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932
6933 /*
6934 * The rest should better be atomic, we can have disk failures
6935 * noticed in interrupt contexts ...
6936 */
6937
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 rdev->raid_disk = -1;
6939
Shaohua Li29530792016-12-08 15:48:19 -08006940 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006941 if (!mddev->thread)
6942 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 /*
6944 * Kick recovery, maybe this spare has to be added to the
6945 * array immediately.
6946 */
6947 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6948 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006949 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950 return 0;
6951
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952abort_export:
6953 export_rdev(rdev);
6954 return err;
6955}
6956
NeilBrownfd01b882011-10-11 16:47:53 +11006957static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006958{
NeilBrown035328c2014-04-09 12:25:40 +10006959 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006960
NeilBrown36fa3062005-09-09 16:23:45 -07006961 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006962 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006963 return -EBUSY;
6964 if (mddev->recovery || mddev->sync_thread)
6965 return -EBUSY;
6966 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006967 }
6968
NeilBrown36fa3062005-09-09 16:23:45 -07006969 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006970 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006971 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006972
NeilBrown1e594bb2014-12-15 12:57:00 +11006973 if (mddev->bitmap || mddev->bitmap_info.file)
6974 return -EEXIST; /* cannot add when bitmap is present */
6975 f = fget(fd);
6976
6977 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006978 pr_warn("%s: error: failed to get bitmap file\n",
6979 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07006980 return -EBADF;
6981 }
6982
NeilBrown1e594bb2014-12-15 12:57:00 +11006983 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006984 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11006985 pr_warn("%s: error: bitmap file must be a regular file\n",
6986 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006987 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006988 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11006989 pr_warn("%s: error: bitmap file must open for write\n",
6990 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006991 err = -EBADF;
6992 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11006993 pr_warn("%s: error: bitmap file is already in use\n",
6994 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006995 err = -EBUSY;
6996 }
6997 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006998 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006999 return err;
7000 }
NeilBrown1e594bb2014-12-15 12:57:00 +11007001 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11007002 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07007003 } else if (mddev->bitmap == NULL)
7004 return -ENOENT; /* cannot remove what isn't there */
7005 err = 0;
7006 if (mddev->pers) {
NeilBrown69e51b42010-06-01 19:37:35 +10007007 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007008 struct bitmap *bitmap;
7009
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007010 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007011 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007012 if (!IS_ERR(bitmap)) {
7013 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007014 err = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11007015 } else
7016 err = PTR_ERR(bitmap);
NeilBrown52a0d492017-10-17 13:46:43 +11007017 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007018 md_bitmap_destroy(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11007019 fd = -1;
7020 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007021 mddev_resume(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11007022 } else if (fd < 0) {
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007023 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007024 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007025 mddev_resume(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07007026 }
NeilBrownd7375ab2006-06-26 00:27:43 -07007027 }
7028 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11007029 struct file *f = mddev->bitmap_info.file;
7030 if (f) {
7031 spin_lock(&mddev->lock);
7032 mddev->bitmap_info.file = NULL;
7033 spin_unlock(&mddev->lock);
7034 fput(f);
7035 }
NeilBrown36fa3062005-09-09 16:23:45 -07007036 }
7037
NeilBrown32a76272005-06-21 17:17:14 -07007038 return err;
7039}
7040
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041/*
7042 * set_array_info is used two different ways
7043 * The original usage is when creating a new array.
7044 * In this usage, raid_disks is > 0 and it together with
7045 * level, size, not_persistent,layout,chunksize determine the
7046 * shape of the array.
7047 * This will always create an array with a type-0.90.0 superblock.
7048 * The newer usage is when assembling an array.
7049 * In this case raid_disks will be 0, and the major_version field is
7050 * use to determine which style super-blocks are to be found on the devices.
7051 * The minor and patch _version numbers are also kept incase the
7052 * super_block handler wishes to interpret them.
7053 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10007054static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055{
7056
7057 if (info->raid_disks == 0) {
7058 /* just setting version number for superblock loading */
7059 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07007060 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061 super_types[info->major_version].name == NULL) {
7062 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11007063 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 info->major_version);
7065 return -EINVAL;
7066 }
7067 mddev->major_version = info->major_version;
7068 mddev->minor_version = info->minor_version;
7069 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08007070 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11007071 /* ensure mddev_put doesn't delete this now that there
7072 * is some minimal configuration.
7073 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11007074 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 return 0;
7076 }
7077 mddev->major_version = MD_MAJOR_VERSION;
7078 mddev->minor_version = MD_MINOR_VERSION;
7079 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11007080 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081
7082 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08007083 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11007084 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 mddev->raid_disks = info->raid_disks;
7086 /* don't set md_minor, it is determined by which /dev/md* was
7087 * openned
7088 */
7089 if (info->state & (1<<MD_SB_CLEAN))
7090 mddev->recovery_cp = MaxSector;
7091 else
7092 mddev->recovery_cp = 0;
7093 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08007094 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095
7096 mddev->layout = info->layout;
NeilBrown33f2c352019-09-09 16:52:29 +10007097 if (mddev->level == 0)
7098 /* Cannot trust RAID0 layout info here */
7099 mddev->layout = -1;
Andre Noll9d8f0362009-06-18 08:45:01 +10007100 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101
Shaohua Li29530792016-12-08 15:48:19 -08007102 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11007103 mddev->max_disks = MD_SB_DISKS;
7104 mddev->flags = 0;
7105 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08007106 }
7107 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108
NeilBrownc3d97142009-12-14 12:49:52 +11007109 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10007110 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11007111 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08007112
NeilBrownf6705572006-03-27 01:18:11 -08007113 mddev->reshape_position = MaxSector;
7114
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115 /*
7116 * Generate a 128 bit UUID
7117 */
7118 get_random_bytes(mddev->uuid, 16);
7119
NeilBrownf6705572006-03-27 01:18:11 -08007120 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10007121 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08007122 mddev->new_layout = mddev->layout;
7123 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007124 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08007125
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126 return 0;
7127}
7128
NeilBrownfd01b882011-10-11 16:47:53 +11007129void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11007130{
Shaohua Liefa4b772017-10-18 22:08:13 -07007131 lockdep_assert_held(&mddev->reconfig_mutex);
Dan Williamsb522adc2009-03-31 15:00:31 +11007132
7133 if (mddev->external_size)
7134 return;
7135
Dan Williams1f403622009-03-31 14:59:03 +11007136 mddev->array_sectors = array_sectors;
7137}
7138EXPORT_SYMBOL(md_set_array_sectors);
7139
NeilBrownfd01b882011-10-11 16:47:53 +11007140static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08007141{
NeilBrown3cb03002011-10-11 16:45:26 +11007142 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08007143 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10007144 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08007145 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04007146
NeilBrowna35b0d62006-01-06 00:20:49 -08007147 if (mddev->pers->resize == NULL)
7148 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10007149 /* The "num_sectors" is the number of sectors of each device that
7150 * is used. This can only make sense for arrays with redundancy.
7151 * linear and raid0 always use whatever space is available. We can only
7152 * consider changing this number if no resync or reconstruction is
7153 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10007154 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10007155 * of each device. If num_sectors is zero, we find the largest size
7156 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08007157 */
NeilBrownf851b602014-12-11 10:02:10 +11007158 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7159 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08007160 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10007161 if (mddev->ro)
7162 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10007163
NeilBrowndafb20f2012-03-19 12:46:39 +11007164 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11007165 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07007166
Andre Nolld71f9f82008-07-11 22:02:22 +10007167 if (fit && (num_sectors == 0 || num_sectors > avail))
7168 num_sectors = avail;
7169 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08007170 return -ENOSPC;
7171 }
Andre Nolld71f9f82008-07-11 22:02:22 +10007172 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08007173 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08007174 if (mddev_is_clustered(mddev))
7175 md_cluster_ops->update_size(mddev, old_dev_sectors);
7176 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08007177 set_capacity(mddev->gendisk, mddev->array_sectors);
7178 revalidate_disk(mddev->gendisk);
7179 }
7180 }
NeilBrowna35b0d62006-01-06 00:20:49 -08007181 return rv;
7182}
7183
NeilBrownfd01b882011-10-11 16:47:53 +11007184static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08007185{
7186 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10007187 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08007188 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08007189 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08007190 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10007191 if (mddev->ro)
7192 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08007193 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10007194 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08007195 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11007196 if (mddev->sync_thread ||
7197 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
7198 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08007199 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10007200
7201 rdev_for_each(rdev, mddev) {
7202 if (mddev->raid_disks < raid_disks &&
7203 rdev->data_offset < rdev->new_data_offset)
7204 return -EINVAL;
7205 if (mddev->raid_disks > raid_disks &&
7206 rdev->data_offset > rdev->new_data_offset)
7207 return -EINVAL;
7208 }
7209
NeilBrown63c70c42006-03-27 01:18:13 -08007210 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10007211 if (mddev->delta_disks < 0)
7212 mddev->reshape_backwards = 1;
7213 else if (mddev->delta_disks > 0)
7214 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08007215
7216 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10007217 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11007218 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007219 mddev->reshape_backwards = 0;
7220 }
NeilBrownda943b992006-01-06 00:20:54 -08007221 return rv;
7222}
7223
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224/*
7225 * update_array_info is used to change the configuration of an
7226 * on-line array.
7227 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
7228 * fields in the info are checked against the array.
7229 * Any differences that cannot be handled will cause an error.
7230 * Normally, only one change can be managed at a time.
7231 */
NeilBrownfd01b882011-10-11 16:47:53 +11007232static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233{
7234 int rv = 0;
7235 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007236 int state = 0;
7237
7238 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11007239 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07007240 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241
7242 if (mddev->major_version != info->major_version ||
7243 mddev->minor_version != info->minor_version ||
7244/* mddev->patch_version != info->patch_version || */
7245 mddev->ctime != info->ctime ||
7246 mddev->level != info->level ||
7247/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08007248 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10007249 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07007250 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
7251 ((state^info->state) & 0xfffffe00)
7252 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253 return -EINVAL;
7254 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11007255 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
7256 cnt++;
7257 if (mddev->raid_disks != info->raid_disks)
7258 cnt++;
7259 if (mddev->layout != info->layout)
7260 cnt++;
7261 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
7262 cnt++;
7263 if (cnt == 0)
7264 return 0;
7265 if (cnt > 1)
7266 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267
7268 if (mddev->layout != info->layout) {
7269 /* Change layout
7270 * we don't need to do anything at the md level, the
7271 * personality will take care of it all.
7272 */
NeilBrown50ac1682009-06-18 08:47:55 +10007273 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007275 else {
7276 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10007277 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10007278 if (rv)
7279 mddev->new_layout = mddev->layout;
7280 return rv;
7281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007283 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10007284 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285
NeilBrownda943b992006-01-06 00:20:54 -08007286 if (mddev->raid_disks != info->raid_disks)
7287 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288
NeilBrown36fa3062005-09-09 16:23:45 -07007289 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007290 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
7291 rv = -EINVAL;
7292 goto err;
7293 }
7294 if (mddev->recovery || mddev->sync_thread) {
7295 rv = -EBUSY;
7296 goto err;
7297 }
NeilBrown36fa3062005-09-09 16:23:45 -07007298 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007299 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07007300 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007301 if (mddev->bitmap) {
7302 rv = -EEXIST;
7303 goto err;
7304 }
7305 if (mddev->bitmap_info.default_offset == 0) {
7306 rv = -EINVAL;
7307 goto err;
7308 }
NeilBrownc3d97142009-12-14 12:49:52 +11007309 mddev->bitmap_info.offset =
7310 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10007311 mddev->bitmap_info.space =
7312 mddev->bitmap_info.default_space;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007313 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007314 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007315 if (!IS_ERR(bitmap)) {
7316 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007317 rv = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11007318 } else
7319 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07007320 if (rv)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007321 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007322 mddev_resume(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07007323 } else {
7324 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007325 if (!mddev->bitmap) {
7326 rv = -ENOENT;
7327 goto err;
7328 }
7329 if (mddev->bitmap->storage.file) {
7330 rv = -EINVAL;
7331 goto err;
7332 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11007333 if (mddev->bitmap_info.nodes) {
7334 /* hold PW on all the bitmap lock */
7335 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11007336 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 +11007337 rv = -EPERM;
7338 md_cluster_ops->unlock_all_bitmaps(mddev);
7339 goto err;
7340 }
7341
7342 mddev->bitmap_info.nodes = 0;
7343 md_cluster_ops->leave(mddev);
7344 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007345 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007346 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007347 mddev_resume(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11007348 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007349 }
7350 }
NeilBrown850b2b422006-10-03 01:15:46 -07007351 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007352 return rv;
7353err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354 return rv;
7355}
7356
NeilBrownfd01b882011-10-11 16:47:53 +11007357static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358{
NeilBrown3cb03002011-10-11 16:45:26 +11007359 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11007360 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361
7362 if (mddev->pers == NULL)
7363 return -ENODEV;
7364
NeilBrown1ca69c42012-10-11 13:37:33 +11007365 rcu_read_lock();
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01007366 rdev = md_find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11007368 err = -ENODEV;
7369 else {
7370 md_error(mddev, rdev);
7371 if (!test_bit(Faulty, &rdev->flags))
7372 err = -EBUSY;
7373 }
7374 rcu_read_unlock();
7375 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376}
7377
Andre Noll2f9618c2008-04-25 18:57:58 +02007378/*
7379 * We have a problem here : there is no easy way to give a CHS
7380 * virtual geometry. We currently pretend that we have a 2 heads
7381 * 4 sectors (with a BIG number of cylinders...). This drives
7382 * dosfs just mad... ;-)
7383 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007384static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7385{
NeilBrownfd01b882011-10-11 16:47:53 +11007386 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007387
7388 geo->heads = 2;
7389 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11007390 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007391 return 0;
7392}
7393
Nicolas Schichancb335f82014-01-15 16:58:52 +01007394static inline bool md_ioctl_valid(unsigned int cmd)
7395{
7396 switch (cmd) {
7397 case ADD_NEW_DISK:
7398 case BLKROSET:
7399 case GET_ARRAY_INFO:
7400 case GET_BITMAP_FILE:
7401 case GET_DISK_INFO:
7402 case HOT_ADD_DISK:
7403 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007404 case RAID_AUTORUN:
7405 case RAID_VERSION:
7406 case RESTART_ARRAY_RW:
7407 case RUN_ARRAY:
7408 case SET_ARRAY_INFO:
7409 case SET_BITMAP_FILE:
7410 case SET_DISK_FAULTY:
7411 case STOP_ARRAY:
7412 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007413 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007414 return true;
7415 default:
7416 return false;
7417 }
7418}
7419
Al Viroa39907f2008-03-02 10:31:15 -05007420static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 unsigned int cmd, unsigned long arg)
7422{
7423 int err = 0;
7424 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11007425 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10007426 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08007427 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007428
Nicolas Schichancb335f82014-01-15 16:58:52 +01007429 if (!md_ioctl_valid(cmd))
7430 return -ENOTTY;
7431
NeilBrown506c9e42011-12-23 10:17:26 +11007432 switch (cmd) {
7433 case RAID_VERSION:
7434 case GET_ARRAY_INFO:
7435 case GET_DISK_INFO:
7436 break;
7437 default:
7438 if (!capable(CAP_SYS_ADMIN))
7439 return -EACCES;
7440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441
7442 /*
7443 * Commands dealing with the RAID driver but not any
7444 * particular array:
7445 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007446 switch (cmd) {
7447 case RAID_VERSION:
7448 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007449 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11007452 case RAID_AUTORUN:
7453 err = 0;
7454 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007455 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11007457 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458 }
7459
7460 /*
7461 * Commands creating/starting a new array:
7462 */
7463
Al Viroa39907f2008-03-02 10:31:15 -05007464 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465
7466 if (!mddev) {
7467 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007468 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 }
7470
NeilBrown1ca69c42012-10-11 13:37:33 +11007471 /* Some actions do not requires the mutex */
7472 switch (cmd) {
7473 case GET_ARRAY_INFO:
7474 if (!mddev->raid_disks && !mddev->external)
7475 err = -ENODEV;
7476 else
7477 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007478 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007479
7480 case GET_DISK_INFO:
7481 if (!mddev->raid_disks && !mddev->external)
7482 err = -ENODEV;
7483 else
7484 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007485 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007486
7487 case SET_DISK_FAULTY:
7488 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007489 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007490
7491 case GET_BITMAP_FILE:
7492 err = get_bitmap_file(mddev, argp);
7493 goto out;
7494
NeilBrown1ca69c42012-10-11 13:37:33 +11007495 }
7496
NeilBrowna7a3f082012-12-11 13:35:54 +11007497 if (cmd == ADD_NEW_DISK)
7498 /* need to ensure md_delayed_delete() has completed */
7499 flush_workqueue(md_misc_wq);
7500
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007501 if (cmd == HOT_REMOVE_DISK)
7502 /* need to ensure recovery thread has run */
7503 wait_event_interruptible_timeout(mddev->sb_wait,
7504 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007505 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007506 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007507 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7508 /* Need to flush page cache, and ensure no-one else opens
7509 * and writes
7510 */
7511 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007512 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007513 mutex_unlock(&mddev->open_mutex);
7514 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007515 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007516 }
NeilBrown065e5192017-04-06 11:16:33 +08007517 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007518 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007519 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007520 mutex_unlock(&mddev->open_mutex);
7521 sync_blockdev(bdev);
7522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523 err = mddev_lock(mddev);
7524 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007525 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7526 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007527 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528 }
7529
NeilBrownc02c0ae2012-12-11 13:39:21 +11007530 if (cmd == SET_ARRAY_INFO) {
7531 mdu_array_info_t info;
7532 if (!arg)
7533 memset(&info, 0, sizeof(info));
7534 else if (copy_from_user(&info, argp, sizeof(info))) {
7535 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007536 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007537 }
7538 if (mddev->pers) {
7539 err = update_array_info(mddev, &info);
7540 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007541 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007542 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007543 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007544 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007545 }
7546 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007547 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007548 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007549 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007550 }
7551 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007552 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007553 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007554 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007555 }
7556 err = set_array_info(mddev, &info);
7557 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007558 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007559 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007560 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007561 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562 }
7563
7564 /*
7565 * Commands querying/configuring an existing array:
7566 */
NeilBrown32a76272005-06-21 17:17:14 -07007567 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007568 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007569 if ((!mddev->raid_disks && !mddev->external)
7570 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7571 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7572 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007573 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007574 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575 }
7576
7577 /*
7578 * Commands even a read-only array can execute:
7579 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007580 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007581 case RESTART_ARRAY_RW:
7582 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007583 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007584
7585 case STOP_ARRAY:
7586 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007587 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007588
7589 case STOP_ARRAY_RO:
7590 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007591 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007592
NeilBrown3ea8929d2013-04-24 11:42:41 +10007593 case HOT_REMOVE_DISK:
7594 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007595 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007596
NeilBrown7ceb17e2013-04-24 11:42:42 +10007597 case ADD_NEW_DISK:
7598 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007599 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007600 * So require mddev->pers and MD_DISK_SYNC.
7601 */
7602 if (mddev->pers) {
7603 mdu_disk_info_t info;
7604 if (copy_from_user(&info, argp, sizeof(info)))
7605 err = -EFAULT;
7606 else if (!(info.state & (1<<MD_DISK_SYNC)))
7607 /* Need to clear read-only for this */
7608 break;
7609 else
7610 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007611 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007612 }
7613 break;
7614
NeilBrownc02c0ae2012-12-11 13:39:21 +11007615 case BLKROSET:
7616 if (get_user(ro, (int __user *)(arg))) {
7617 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007618 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007619 }
7620 err = -EINVAL;
7621
7622 /* if the bdev is going readonly the value of mddev->ro
7623 * does not matter, no writes are coming
7624 */
7625 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007626 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007627
NeilBrownc02c0ae2012-12-11 13:39:21 +11007628 /* are we are already prepared for writes? */
7629 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007630 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007631
7632 /* transitioning to readauto need only happen for
7633 * arrays that call md_write_start
7634 */
7635 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007637 if (err == 0) {
7638 mddev->ro = 2;
7639 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007640 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007641 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007642 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643 }
7644
7645 /*
7646 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007647 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648 */
NeilBrown326eb172014-09-30 15:36:28 +10007649 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007650 if (mddev->ro == 2) {
7651 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007652 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007653 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007654 /* mddev_unlock will wake thread */
7655 /* If a device failed while we were read-only, we
7656 * need to make sure the metadata is updated now.
7657 */
Shaohua Li29530792016-12-08 15:48:19 -08007658 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007659 mddev_unlock(mddev);
7660 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007661 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7662 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007663 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007664 }
NeilBrownf91de922005-11-08 21:39:36 -08007665 } else {
7666 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007667 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669 }
7670
NeilBrownc02c0ae2012-12-11 13:39:21 +11007671 switch (cmd) {
7672 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007674 mdu_disk_info_t info;
7675 if (copy_from_user(&info, argp, sizeof(info)))
7676 err = -EFAULT;
7677 else
7678 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007679 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007681
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007682 case CLUSTERED_DISK_NACK:
7683 if (mddev_is_clustered(mddev))
7684 md_cluster_ops->new_disk_ack(mddev, false);
7685 else
7686 err = -EINVAL;
7687 goto unlock;
7688
NeilBrownc02c0ae2012-12-11 13:39:21 +11007689 case HOT_ADD_DISK:
7690 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007691 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007692
NeilBrownc02c0ae2012-12-11 13:39:21 +11007693 case RUN_ARRAY:
7694 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007695 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696
NeilBrownc02c0ae2012-12-11 13:39:21 +11007697 case SET_BITMAP_FILE:
7698 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007699 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007700
NeilBrownc02c0ae2012-12-11 13:39:21 +11007701 default:
7702 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007703 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007704 }
7705
NeilBrown3adc28d2014-09-30 15:46:41 +10007706unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007707 if (mddev->hold_active == UNTIL_IOCTL &&
7708 err != -EINVAL)
7709 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007710 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007711out:
NeilBrown065e5192017-04-06 11:16:33 +08007712 if(did_set_md_closing)
7713 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714 return err;
7715}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007716#ifdef CONFIG_COMPAT
7717static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7718 unsigned int cmd, unsigned long arg)
7719{
7720 switch (cmd) {
7721 case HOT_REMOVE_DISK:
7722 case HOT_ADD_DISK:
7723 case SET_DISK_FAULTY:
7724 case SET_BITMAP_FILE:
7725 /* These take in integer arg, do not convert */
7726 break;
7727 default:
7728 arg = (unsigned long)compat_ptr(arg);
7729 break;
7730 }
7731
7732 return md_ioctl(bdev, mode, cmd, arg);
7733}
7734#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007735
Al Viroa39907f2008-03-02 10:31:15 -05007736static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007737{
7738 /*
7739 * Succeed if we can lock the mddev, which confirms that
7740 * it isn't being stopped right now.
7741 */
NeilBrownfd01b882011-10-11 16:47:53 +11007742 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743 int err;
7744
Yuanhan Liu0c098222012-05-22 13:55:32 +10007745 if (!mddev)
7746 return -ENODEV;
7747
NeilBrownd3374822009-01-09 08:31:10 +11007748 if (mddev->gendisk != bdev->bd_disk) {
7749 /* we are racing with mddev_put which is discarding this
7750 * bd_disk.
7751 */
7752 mddev_put(mddev);
7753 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007754 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007755 /* Then retry the open from the top */
7756 return -ERESTARTSYS;
7757 }
7758 BUG_ON(mddev != bdev->bd_disk->private_data);
7759
NeilBrownc8c00a62009-08-10 12:50:52 +10007760 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007761 goto out;
7762
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007763 if (test_bit(MD_CLOSING, &mddev->flags)) {
7764 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007765 err = -ENODEV;
7766 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007767 }
7768
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007770 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007771 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007773 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007775 if (err)
7776 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007777 return err;
7778}
7779
Al Virodb2a1442013-05-05 21:52:57 -04007780static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007782 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007783
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007784 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007785 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007788
7789static int md_media_changed(struct gendisk *disk)
7790{
NeilBrownfd01b882011-10-11 16:47:53 +11007791 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007792
7793 return mddev->changed;
7794}
7795
7796static int md_revalidate(struct gendisk *disk)
7797{
NeilBrownfd01b882011-10-11 16:47:53 +11007798 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007799
7800 mddev->changed = 0;
7801 return 0;
7802}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007803static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804{
7805 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007806 .open = md_open,
7807 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007808 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007809#ifdef CONFIG_COMPAT
7810 .compat_ioctl = md_compat_ioctl,
7811#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007812 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007813 .media_changed = md_media_changed,
7814 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007815};
7816
NeilBrownf72ffdd2014-09-30 14:23:59 +10007817static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007818{
NeilBrown2b8bf342011-10-11 16:48:23 +11007819 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821 /*
7822 * md_thread is a 'system-thread', it's priority should be very
7823 * high. We avoid resource deadlocks individually in each
7824 * raid personality. (RAID5 does preallocation) We also use RR and
7825 * the very same RT priority as kswapd, thus we will never get
7826 * into a priority inversion deadlock.
7827 *
7828 * we definitely have to have equal or higher priority than
7829 * bdflush, otherwise bdflush will deadlock if there are too
7830 * many dirty RAID5 blocks.
7831 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832
NeilBrown6985c432005-10-19 21:23:47 -07007833 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007834 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835
NeilBrown93588e22005-11-15 00:09:12 -08007836 /* We need to wait INTERRUPTIBLE so that
7837 * we don't add to the load-average.
7838 * That means we need to be sure no signals are
7839 * pending
7840 */
7841 if (signal_pending(current))
7842 flush_signals(current);
7843
7844 wait_event_interruptible_timeout
7845 (thread->wqueue,
7846 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007847 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007848 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007849
NeilBrown6c987912011-01-14 09:13:53 +11007850 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007851 if (kthread_should_park())
7852 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007853 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007854 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007855 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007856
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857 return 0;
7858}
7859
NeilBrown2b8bf342011-10-11 16:48:23 +11007860void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007861{
7862 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007863 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Guoqing Jiangd1d90142017-10-09 10:32:48 +08007864 set_bit(THREAD_WAKEUP, &thread->flags);
7865 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007866 }
7867}
NeilBrown6c144d32014-09-30 16:15:38 +10007868EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869
Shaohua Li4ed87312012-10-11 13:34:00 +11007870struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7871 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007872{
NeilBrown2b8bf342011-10-11 16:48:23 +11007873 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874
NeilBrown2b8bf342011-10-11 16:48:23 +11007875 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876 if (!thread)
7877 return NULL;
7878
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879 init_waitqueue_head(&thread->wqueue);
7880
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881 thread->run = run;
7882 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007883 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007884 thread->tsk = kthread_run(md_thread, thread,
7885 "%s_%s",
7886 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007887 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007888 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 kfree(thread);
7890 return NULL;
7891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892 return thread;
7893}
NeilBrown6c144d32014-09-30 16:15:38 +10007894EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007895
NeilBrown2b8bf342011-10-11 16:48:23 +11007896void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007897{
NeilBrown2b8bf342011-10-11 16:48:23 +11007898 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007899 if (!thread)
7900 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007901 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007902 /* Locking ensures that mddev_unlock does not wake_up a
7903 * non-existent thread
7904 */
7905 spin_lock(&pers_lock);
7906 *threadp = NULL;
7907 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007908
7909 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910 kfree(thread);
7911}
NeilBrown6c144d32014-09-30 16:15:38 +10007912EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913
NeilBrownfd01b882011-10-11 16:47:53 +11007914void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915{
NeilBrownb2d444d2005-11-08 21:39:31 -08007916 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007918
NeilBrownde393cd2011-07-28 11:31:48 +10007919 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007920 return;
7921 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007922 if (mddev->degraded)
7923 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007924 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7926 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7927 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007928 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007929 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007930 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931}
NeilBrown6c144d32014-09-30 16:15:38 +10007932EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007933
7934/* seq_file implementation /proc/mdstat */
7935
7936static void status_unused(struct seq_file *seq)
7937{
7938 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007939 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940
7941 seq_printf(seq, "unused devices: ");
7942
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007943 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007944 char b[BDEVNAME_SIZE];
7945 i++;
7946 seq_printf(seq, "%s ",
7947 bdevname(rdev->bdev,b));
7948 }
7949 if (!i)
7950 seq_printf(seq, "<none>");
7951
7952 seq_printf(seq, "\n");
7953}
7954
NeilBrownf7851be2015-07-02 17:12:58 +10007955static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956{
NeilBrowndd71cf62009-05-07 12:49:35 +10007957 sector_t max_sectors, resync, res;
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02007958 unsigned long dt, db = 0;
7959 sector_t rt, curr_mark_cnt, resync_mark_cnt;
7960 int scale, recovery_active;
NeilBrown4588b422006-03-27 01:18:04 -08007961 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007962
NeilBrownc804cde2012-05-21 09:28:33 +10007963 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7964 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007965 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007967 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007968
NeilBrownf7851be2015-07-02 17:12:58 +10007969 resync = mddev->curr_resync;
7970 if (resync <= 3) {
7971 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7972 /* Still cleaning up */
7973 resync = max_sectors;
Nate Daileyd2e2ec82017-11-30 11:33:30 -05007974 } else if (resync > max_sectors)
7975 resync = max_sectors;
7976 else
NeilBrownf7851be2015-07-02 17:12:58 +10007977 resync -= atomic_read(&mddev->recovery_active);
7978
7979 if (resync == 0) {
Guoqing Jiang0357ba22018-07-02 16:26:25 +08007980 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
7981 struct md_rdev *rdev;
7982
7983 rdev_for_each(rdev, mddev)
7984 if (rdev->raid_disk >= 0 &&
7985 !test_bit(Faulty, &rdev->flags) &&
7986 rdev->recovery_offset != MaxSector &&
7987 rdev->recovery_offset) {
7988 seq_printf(seq, "\trecover=REMOTE");
7989 return 1;
7990 }
7991 if (mddev->reshape_position != MaxSector)
7992 seq_printf(seq, "\treshape=REMOTE");
7993 else
7994 seq_printf(seq, "\tresync=REMOTE");
7995 return 1;
7996 }
NeilBrownf7851be2015-07-02 17:12:58 +10007997 if (mddev->recovery_cp < MaxSector) {
7998 seq_printf(seq, "\tresync=PENDING");
7999 return 1;
8000 }
8001 return 0;
8002 }
8003 if (resync < 3) {
8004 seq_printf(seq, "\tresync=DELAYED");
8005 return 1;
8006 }
8007
NeilBrown403df472014-09-30 15:52:29 +10008008 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08008009 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10008010 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08008011 * u32, as those are the requirements for sector_div.
8012 * Thus 'scale' must be at least 10
8013 */
8014 scale = 10;
8015 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10008016 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08008017 scale++;
8018 }
8019 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10008020 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08008021
8022 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008023 {
NeilBrown4588b422006-03-27 01:18:04 -08008024 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008025 seq_printf(seq, "[");
8026 for (i = 0; i < x; i++)
8027 seq_printf(seq, "=");
8028 seq_printf(seq, ">");
8029 for (i = 0; i < y; i++)
8030 seq_printf(seq, ".");
8031 seq_printf(seq, "] ");
8032 }
NeilBrown4588b422006-03-27 01:18:04 -08008033 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08008034 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
8035 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07008036 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
8037 "check" :
8038 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
8039 "resync" : "recovery"))),
8040 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10008041 (unsigned long long) resync/2,
8042 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008043
8044 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045 * dt: time from mark until now
8046 * db: blocks written from mark until now
8047 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10008048 *
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008049 * rt is a sector_t, which is always 64bit now. We are keeping
8050 * the original algorithm, but it is not really necessary.
8051 *
8052 * Original algorithm:
8053 * So we divide before multiply in case it is 32bit and close
8054 * to the limit.
8055 * We scale the divisor (db) by 32 to avoid losing precision
8056 * near the end of resync when the number of remaining sectors
8057 * is close to 'db'.
8058 * We then divide rt by 32 after multiplying by db to compensate.
8059 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008060 */
8061 dt = ((jiffies - mddev->resync_mark) / HZ);
8062 if (!dt) dt++;
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008063
8064 curr_mark_cnt = mddev->curr_mark_cnt;
8065 recovery_active = atomic_read(&mddev->recovery_active);
8066 resync_mark_cnt = mddev->resync_mark_cnt;
8067
8068 if (curr_mark_cnt >= (recovery_active + resync_mark_cnt))
8069 db = curr_mark_cnt - (recovery_active + resync_mark_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008070
NeilBrowndd71cf62009-05-07 12:49:35 +10008071 rt = max_sectors - resync; /* number of remaining sectors */
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02008072 rt = div64_u64(rt, db/32+1);
NeilBrowndd71cf62009-05-07 12:49:35 +10008073 rt *= dt;
8074 rt >>= 5;
8075
8076 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
8077 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008078
NeilBrownff4e8d92006-07-10 04:44:16 -07008079 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10008080 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081}
8082
8083static void *md_seq_start(struct seq_file *seq, loff_t *pos)
8084{
8085 struct list_head *tmp;
8086 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11008087 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008088
8089 if (l >= 0x10000)
8090 return NULL;
8091 if (!l--)
8092 /* header */
8093 return (void*)1;
8094
8095 spin_lock(&all_mddevs_lock);
8096 list_for_each(tmp,&all_mddevs)
8097 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11008098 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008099 mddev_get(mddev);
8100 spin_unlock(&all_mddevs_lock);
8101 return mddev;
8102 }
8103 spin_unlock(&all_mddevs_lock);
8104 if (!l--)
8105 return (void*)2;/* tail */
8106 return NULL;
8107}
8108
8109static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
8110{
8111 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008112 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10008113
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114 ++*pos;
8115 if (v == (void*)2)
8116 return NULL;
8117
8118 spin_lock(&all_mddevs_lock);
8119 if (v == (void*)1)
8120 tmp = all_mddevs.next;
8121 else
8122 tmp = mddev->all_mddevs.next;
8123 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11008124 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008125 else {
8126 next_mddev = (void*)2;
8127 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10008128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008129 spin_unlock(&all_mddevs_lock);
8130
8131 if (v != (void*)1)
8132 mddev_put(mddev);
8133 return next_mddev;
8134
8135}
8136
8137static void md_seq_stop(struct seq_file *seq, void *v)
8138{
NeilBrownfd01b882011-10-11 16:47:53 +11008139 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008140
8141 if (mddev && v != (void*)1 && v != (void*)2)
8142 mddev_put(mddev);
8143}
8144
8145static int md_seq_show(struct seq_file *seq, void *v)
8146{
NeilBrownfd01b882011-10-11 16:47:53 +11008147 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11008148 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11008149 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008150
8151 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11008152 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008153 seq_printf(seq, "Personalities : ");
8154 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008155 list_for_each_entry(pers, &pers_list, list)
8156 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008157
8158 spin_unlock(&pers_lock);
8159 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02008160 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161 return 0;
8162 }
8163 if (v == (void*)2) {
8164 status_unused(seq);
8165 return 0;
8166 }
8167
NeilBrown36d091f2014-12-15 12:56:58 +11008168 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008169 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
8170 seq_printf(seq, "%s : %sactive", mdname(mddev),
8171 mddev->pers ? "" : "in");
8172 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08008173 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008174 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08008175 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07008176 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008177 seq_printf(seq, " %s", mddev->pers->name);
8178 }
8179
Andre Nolldd8ac332009-03-31 14:33:13 +11008180 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11008181 rcu_read_lock();
8182 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183 char b[BDEVNAME_SIZE];
8184 seq_printf(seq, " %s[%d]",
8185 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07008186 if (test_bit(WriteMostly, &rdev->flags))
8187 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07008188 if (test_bit(Journal, &rdev->flags))
8189 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08008190 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008191 seq_printf(seq, "(F)");
8192 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11008193 }
8194 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07008195 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11008196 if (test_bit(Replacement, &rdev->flags))
8197 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11008198 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008199 }
NeilBrownf97fcad2014-12-15 12:56:59 +11008200 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201
8202 if (!list_empty(&mddev->disks)) {
8203 if (mddev->pers)
8204 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10008205 (unsigned long long)
8206 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008207 else
8208 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11008209 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008210 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07008211 if (mddev->persistent) {
8212 if (mddev->major_version != 0 ||
8213 mddev->minor_version != 90) {
8214 seq_printf(seq," super %d.%d",
8215 mddev->major_version,
8216 mddev->minor_version);
8217 }
NeilBrowne6910632008-02-06 01:39:51 -08008218 } else if (mddev->external)
8219 seq_printf(seq, " super external:%s",
8220 mddev->metadata_type);
8221 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07008222 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008223
8224 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11008225 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10008226 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08008227 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10008228 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08008229 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08008230 }
NeilBrown32a76272005-06-21 17:17:14 -07008231 } else
8232 seq_printf(seq, "\n ");
8233
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008234 md_bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008235
8236 seq_printf(seq, "\n");
8237 }
NeilBrown36d091f2014-12-15 12:56:58 +11008238 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10008239
Linus Torvalds1da177e2005-04-16 15:20:36 -07008240 return 0;
8241}
8242
Jan Engelhardt110518b2009-05-07 12:49:37 +10008243static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008244 .start = md_seq_start,
8245 .next = md_seq_next,
8246 .stop = md_seq_stop,
8247 .show = md_seq_show,
8248};
8249
8250static int md_seq_open(struct inode *inode, struct file *file)
8251{
Kay Sieversf1514632011-07-12 20:48:39 +02008252 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008253 int error;
8254
8255 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08008256 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02008257 return error;
8258
8259 seq = file->private_data;
8260 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008261 return error;
8262}
8263
NeilBrowne2f23b62014-04-09 14:33:51 +10008264static int md_unloading;
Al Viroafc9a422017-07-03 06:39:46 -04008265static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
NeilBrownd7603b72006-01-06 00:20:30 -08008266{
Kay Sieversf1514632011-07-12 20:48:39 +02008267 struct seq_file *seq = filp->private_data;
Al Viroafc9a422017-07-03 06:39:46 -04008268 __poll_t mask;
NeilBrownd7603b72006-01-06 00:20:30 -08008269
NeilBrowne2f23b62014-04-09 14:33:51 +10008270 if (md_unloading)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008271 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08008272 poll_wait(filp, &md_event_waiters, wait);
8273
8274 /* always allow read */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008275 mask = EPOLLIN | EPOLLRDNORM;
NeilBrownd7603b72006-01-06 00:20:30 -08008276
Kay Sieversf1514632011-07-12 20:48:39 +02008277 if (seq->poll_event != atomic_read(&md_event_count))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008278 mask |= EPOLLERR | EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08008279 return mask;
8280}
8281
Alexey Dobriyan97a32532020-02-03 17:37:17 -08008282static const struct proc_ops mdstat_proc_ops = {
8283 .proc_open = md_seq_open,
8284 .proc_read = seq_read,
8285 .proc_lseek = seq_lseek,
8286 .proc_release = seq_release,
8287 .proc_poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008288};
8289
NeilBrown84fc4b52011-10-11 16:49:58 +11008290int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008291{
NeilBrown9d487392016-11-02 14:16:49 +11008292 pr_debug("md: %s personality registered for level %d\n",
8293 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008295 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008296 spin_unlock(&pers_lock);
8297 return 0;
8298}
NeilBrown6c144d32014-09-30 16:15:38 +10008299EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008300
NeilBrown84fc4b52011-10-11 16:49:58 +11008301int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008302{
NeilBrown9d487392016-11-02 14:16:49 +11008303 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008304 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008305 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008306 spin_unlock(&pers_lock);
8307 return 0;
8308}
NeilBrown6c144d32014-09-30 16:15:38 +10008309EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008310
NeilBrown6022e752015-08-13 12:32:55 +10008311int register_md_cluster_operations(struct md_cluster_operations *ops,
8312 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008313{
NeilBrown6022e752015-08-13 12:32:55 +10008314 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008315 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008316 if (md_cluster_ops != NULL)
8317 ret = -EALREADY;
8318 else {
8319 md_cluster_ops = ops;
8320 md_cluster_mod = module;
8321 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008322 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008323 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008324}
8325EXPORT_SYMBOL(register_md_cluster_operations);
8326
8327int unregister_md_cluster_operations(void)
8328{
8329 spin_lock(&pers_lock);
8330 md_cluster_ops = NULL;
8331 spin_unlock(&pers_lock);
8332 return 0;
8333}
8334EXPORT_SYMBOL(unregister_md_cluster_operations);
8335
8336int md_setup_cluster(struct mddev *mddev, int nodes)
8337{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008338 if (!md_cluster_ops)
8339 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008340 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008341 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008342 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11008343 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008344 spin_unlock(&pers_lock);
8345 return -ENOENT;
8346 }
8347 spin_unlock(&pers_lock);
8348
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05008349 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008350}
8351
8352void md_cluster_stop(struct mddev *mddev)
8353{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05008354 if (!md_cluster_ops)
8355 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008356 md_cluster_ops->leave(mddev);
8357 module_put(md_cluster_mod);
8358}
8359
NeilBrownfd01b882011-10-11 16:47:53 +11008360static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008361{
NeilBrownf72ffdd2014-09-30 14:23:59 +10008362 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008363 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11008364 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008365
8366 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10008367 rcu_read_lock();
8368 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008369 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Michael Callahan59767fb2018-07-18 04:47:37 -07008370 curr_events = (int)part_stat_read_accum(&disk->part0, sectors) -
NeilBrowneea1bf32009-03-31 14:27:02 +11008371 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07008372 /* sync IO will cause sync_io to increase before the disk_stats
8373 * as sync_io is counted when a request starts, and
8374 * disk_stats is counted when it completes.
8375 * So resync activity will cause curr_events to be smaller than
8376 * when there was no such activity.
8377 * non-sync IO will cause disk_stat to increase without
8378 * increasing sync_io so curr_events will (eventually)
8379 * be larger than it was before. Once it becomes
8380 * substantially larger, the test below will cause
8381 * the array to appear non-idle, and resync will slow
8382 * down.
8383 * If there is a lot of outstanding resync activity when
8384 * we set last_event to curr_events, then all that activity
8385 * completing might cause the array to appear non-idle
8386 * and resync will be slowed down even though there might
8387 * not have been non-resync activity. This will only
8388 * happen once though. 'last_events' will soon reflect
8389 * the state where there is little or no outstanding
8390 * resync requests, and further resync activity will
8391 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08008392 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008393 */
NeilBrowneea1bf32009-03-31 14:27:02 +11008394 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008395 rdev->last_events = curr_events;
8396 idle = 0;
8397 }
8398 }
NeilBrown4b809912008-07-21 17:05:25 +10008399 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008400 return idle;
8401}
8402
NeilBrownfd01b882011-10-11 16:47:53 +11008403void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404{
8405 /* another "blocks" (512byte) blocks have been synced */
8406 atomic_sub(blocks, &mddev->recovery_active);
8407 wake_up(&mddev->recovery_wait);
8408 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07008409 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08008410 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008411 md_wakeup_thread(mddev->thread);
8412 // stop recovery, signal do_sync ....
8413 }
8414}
NeilBrown6c144d32014-09-30 16:15:38 +10008415EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008416
NeilBrown06d91a52005-06-21 17:17:12 -07008417/* md_write_start(mddev, bi)
8418 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07008419 * in superblock) before writing, schedule a superblock update
8420 * and wait for it to complete.
NeilBrowncc27b0c2017-06-05 16:49:39 +10008421 * A return value of 'false' means that the write wasn't recorded
8422 * and cannot proceed as the array is being suspend.
NeilBrown06d91a52005-06-21 17:17:12 -07008423 */
NeilBrowncc27b0c2017-06-05 16:49:39 +10008424bool md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008425{
Neil Brown0fd62b82008-06-28 08:31:36 +10008426 int did_change = 0;
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008427
NeilBrown06d91a52005-06-21 17:17:12 -07008428 if (bio_data_dir(bi) != WRITE)
NeilBrowncc27b0c2017-06-05 16:49:39 +10008429 return true;
NeilBrown06d91a52005-06-21 17:17:12 -07008430
NeilBrownf91de922005-11-08 21:39:36 -08008431 BUG_ON(mddev->ro == 1);
8432 if (mddev->ro == 2) {
8433 /* need to switch to read/write */
8434 mddev->ro = 0;
8435 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8436 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08008437 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008438 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08008439 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008440 rcu_read_lock();
8441 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11008442 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07008443 if (mddev->safemode == 1)
8444 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11008445 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
NeilBrown81fe48e2017-08-08 16:56:36 +10008446 if (mddev->in_sync || mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11008447 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07008448 if (mddev->in_sync) {
8449 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008450 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8451 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07008452 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008453 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07008454 }
NeilBrown85572d72014-12-15 12:56:56 +11008455 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07008456 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008457 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10008458 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008459 sysfs_notify_dirent_safe(mddev->sysfs_state);
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008460 if (!mddev->has_superblocks)
8461 return true;
NeilBrown09a44cc2008-05-23 13:04:36 -07008462 wait_event(mddev->sb_wait,
NeilBrownd47c8ad2017-10-05 16:23:16 +11008463 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8464 mddev->suspended);
NeilBrowncc27b0c2017-06-05 16:49:39 +10008465 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8466 percpu_ref_put(&mddev->writes_pending);
8467 return false;
8468 }
8469 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008470}
NeilBrown6c144d32014-09-30 16:15:38 +10008471EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008472
NeilBrown49728052017-03-15 14:05:12 +11008473/* md_write_inc can only be called when md_write_start() has
8474 * already been called at least once of the current request.
8475 * It increments the counter and is useful when a single request
8476 * is split into several parts. Each part causes an increment and
8477 * so needs a matching md_write_end().
8478 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8479 * a spinlocked region.
8480 */
8481void md_write_inc(struct mddev *mddev, struct bio *bi)
8482{
8483 if (bio_data_dir(bi) != WRITE)
8484 return;
8485 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11008486 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008487}
8488EXPORT_SYMBOL(md_write_inc);
8489
NeilBrownfd01b882011-10-11 16:47:53 +11008490void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008491{
NeilBrown4ad23a972017-03-15 14:05:14 +11008492 percpu_ref_put(&mddev->writes_pending);
8493
8494 if (mddev->safemode == 2)
8495 md_wakeup_thread(mddev->thread);
8496 else if (mddev->safemode_delay)
8497 /* The roundup() ensures this only performs locking once
8498 * every ->safemode_delay jiffies
8499 */
8500 mod_timer(&mddev->safemode_timer,
8501 roundup(jiffies, mddev->safemode_delay) +
8502 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008503}
NeilBrown4ad23a972017-03-15 14:05:14 +11008504
NeilBrown6c144d32014-09-30 16:15:38 +10008505EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008506
NeilBrown2a2275d2007-01-26 00:57:11 -08008507/* md_allow_write(mddev)
8508 * Calling this ensures that the array is marked 'active' so that writes
8509 * may proceed without blocking. It is important to call this before
8510 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8511 * Must be called with mddev_lock held.
8512 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008513void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008514{
8515 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008516 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008517 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008518 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008519 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008520 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008521
NeilBrown85572d72014-12-15 12:56:56 +11008522 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008523 if (mddev->in_sync) {
8524 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008525 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8526 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008527 if (mddev->safemode_delay &&
8528 mddev->safemode == 0)
8529 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008530 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008531 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008532 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008533 /* wait for the dirty state to be recorded in the metadata */
8534 wait_event(mddev->sb_wait,
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008535 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008536 } else
NeilBrown85572d72014-12-15 12:56:56 +11008537 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008538}
8539EXPORT_SYMBOL_GPL(md_allow_write);
8540
Linus Torvalds1da177e2005-04-16 15:20:36 -07008541#define SYNC_MARKS 10
8542#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008543#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008544void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008545{
Shaohua Li4ed87312012-10-11 13:34:00 +11008546 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008547 struct mddev *mddev2;
Yufen Yue5b521e2019-06-14 15:41:07 -07008548 unsigned int currspeed = 0, window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008549 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008550 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008551 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008552 sector_t mark_cnt[SYNC_MARKS];
8553 int last_mark,m;
8554 struct list_head *tmp;
8555 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008556 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008557 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008558 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008559 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008560 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008561
8562 /* just incase thread restarts... */
Song Liud5d885f2017-11-19 22:17:01 -08008563 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8564 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008565 return;
NeilBrown3991b312014-05-28 13:39:23 +10008566 if (mddev->ro) {/* never try to sync a read-only array */
8567 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008568 return;
NeilBrown3991b312014-05-28 13:39:23 +10008569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008570
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008571 if (mddev_is_clustered(mddev)) {
8572 ret = md_cluster_ops->resync_start(mddev);
8573 if (ret)
8574 goto skip;
8575
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008576 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008577 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8578 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8579 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8580 && ((unsigned long long)mddev->curr_resync_completed
8581 < (unsigned long long)mddev->resync_max_sectors))
8582 goto skip;
8583 }
8584
NeilBrown61df9d92006-10-03 01:15:57 -07008585 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008586 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008587 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008588 action = "check";
8589 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008590 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008591 action = "repair";
8592 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008593 desc = "resync";
8594 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8595 desc = "reshape";
8596 else
8597 desc = "recovery";
8598
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008599 mddev->last_sync_action = action ?: desc;
8600
Linus Torvalds1da177e2005-04-16 15:20:36 -07008601 /* we overload curr_resync somewhat here.
8602 * 0 == not engaged in resync at all
8603 * 2 == checking that there is no conflict with another sync
8604 * 1 == like 2, but have yielded to allow conflicting resync to
Yufen Yue5b521e2019-06-14 15:41:07 -07008605 * commence
Linus Torvalds1da177e2005-04-16 15:20:36 -07008606 * other == active in resync - this many blocks
8607 *
8608 * Before starting a resync we must have set curr_resync to
8609 * 2, and then checked that every "conflicting" array has curr_resync
8610 * less than ours. When we find one that is the same or higher
8611 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8612 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8613 * This will mean we have to start checking from the beginning again.
8614 *
8615 */
8616
8617 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008618 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008619 mddev->curr_resync = 2;
8620
8621 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008622 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008623 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008624 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008625 if (mddev2 == mddev)
8626 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008627 if (!mddev->parallel_resync
8628 && mddev2->curr_resync
8629 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008630 DEFINE_WAIT(wq);
8631 if (mddev < mddev2 && mddev->curr_resync == 2) {
8632 /* arbitrarily yield */
8633 mddev->curr_resync = 1;
8634 wake_up(&resync_wait);
8635 }
8636 if (mddev > mddev2 && mddev->curr_resync == 1)
8637 /* no need to wait here, we can wait the next
8638 * time 'round when curr_resync == 2
8639 */
8640 continue;
NeilBrown97441972008-09-19 11:49:54 +10008641 /* We need to wait 'interruptible' so as not to
8642 * contribute to the load average, and not to
8643 * be caught by 'softlockup'
8644 */
8645 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008646 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008647 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008648 if (mddev2_minor != mddev2->md_minor) {
8649 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008650 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8651 desc, mdname(mddev),
8652 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008654 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008655 if (signal_pending(current))
8656 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008657 schedule();
8658 finish_wait(&resync_wait, &wq);
8659 goto try_again;
8660 }
8661 finish_wait(&resync_wait, &wq);
8662 }
8663 }
8664 } while (mddev->curr_resync < 2);
8665
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008666 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008667 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008668 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008669 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008670 */
8671 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008672 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008673 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008674 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8675 j = mddev->resync_min;
8676 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008677 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008678
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008679 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
NeilBrownc804cde2012-05-21 09:28:33 +10008680 max_sectors = mddev->resync_max_sectors;
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008681 /*
8682 * If the original node aborts reshaping then we continue the
8683 * reshaping, so set j again to avoid restart reshape from the
8684 * first beginning
8685 */
8686 if (mddev_is_clustered(mddev) &&
8687 mddev->reshape_position != MaxSector)
8688 j = mddev->reshape_position;
8689 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008690 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008691 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008692 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008693 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008694 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008695 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008696 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008697 !test_bit(Faulty, &rdev->flags) &&
8698 !test_bit(In_sync, &rdev->flags) &&
8699 rdev->recovery_offset < j)
8700 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008701 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008702
8703 /* If there is a bitmap, we need to make sure all
8704 * writes that started before we added a spare
8705 * complete before we start doing a recovery.
8706 * Otherwise the write might complete and (via
8707 * bitmap_endwrite) set a bit in the bitmap after the
8708 * recovery has checked that bit and skipped that
8709 * region.
8710 */
8711 if (mddev->bitmap) {
8712 mddev->pers->quiesce(mddev, 1);
8713 mddev->pers->quiesce(mddev, 0);
8714 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008716
NeilBrown9d487392016-11-02 14:16:49 +11008717 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8718 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8719 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8720 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008721
NeilBrowneea1bf32009-03-31 14:27:02 +11008722 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008723
NeilBrown57afd892005-06-21 17:17:13 -07008724 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008725 for (m = 0; m < SYNC_MARKS; m++) {
8726 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008727 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008728 }
8729 last_mark = 0;
8730 mddev->resync_mark = mark[last_mark];
8731 mddev->resync_mark_cnt = mark_cnt[last_mark];
8732
8733 /*
8734 * Tune reconstruction:
8735 */
Yufen Yue5b521e2019-06-14 15:41:07 -07008736 window = 32 * (PAGE_SIZE / 512);
NeilBrown9d487392016-11-02 14:16:49 +11008737 pr_debug("md: using %dk window, over a total of %lluk.\n",
8738 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008739
8740 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008741 last_check = 0;
8742
8743 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008744 pr_debug("md: resuming %s of %s from checkpoint.\n",
8745 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008746 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008747 } else
8748 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008749 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008750 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8751 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008752 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008753
majianpeng7c2c57c2012-07-03 12:12:26 +10008754 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008755 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008756 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008757
NeilBrown57afd892005-06-21 17:17:13 -07008758 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008759
NeilBrown7a91ee12009-05-26 12:57:21 +10008760 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8761 ((mddev->curr_resync > mddev->curr_resync_completed &&
8762 (mddev->curr_resync - mddev->curr_resync_completed)
8763 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008764 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008765 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008766 >= mddev->resync_max - mddev->curr_resync_completed ||
8767 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008768 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008769 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008770 wait_event(mddev->recovery_wait,
8771 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008772 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008773 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8774 j > mddev->recovery_cp)
8775 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008776 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008777 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008778 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008779 }
NeilBrownacb180b2009-04-14 16:28:34 +10008780
NeilBrownc91abf52013-11-19 12:02:01 +11008781 while (j >= mddev->resync_max &&
8782 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008783 /* As this condition is controlled by user-space,
8784 * we can block indefinitely, so use '_interruptible'
8785 * to avoid triggering warnings.
8786 */
8787 flush_signals(current); /* just in case */
8788 wait_event_interruptible(mddev->recovery_wait,
8789 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008790 || test_bit(MD_RECOVERY_INTR,
8791 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008792 }
NeilBrownacb180b2009-04-14 16:28:34 +10008793
NeilBrownc91abf52013-11-19 12:02:01 +11008794 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8795 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008796
NeilBrown09314792015-02-19 16:04:40 +11008797 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008798 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008799 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008800 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008801 }
NeilBrown57afd892005-06-21 17:17:13 -07008802
8803 if (!skipped) { /* actual IO requested */
8804 io_sectors += sectors;
8805 atomic_add(sectors, &mddev->recovery_active);
8806 }
8807
NeilBrowne875ece2011-07-28 11:39:24 +10008808 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8809 break;
8810
Linus Torvalds1da177e2005-04-16 15:20:36 -07008811 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008812 if (j > max_sectors)
8813 /* when skipping, extra large numbers can be returned. */
8814 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008815 if (j > 2)
8816 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008817 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008818 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008819 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008820 * visible in /proc/mdstat
8821 */
8822 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008823
8824 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008825 continue;
8826
NeilBrown57afd892005-06-21 17:17:13 -07008827 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008828 repeat:
8829 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8830 /* step marks */
8831 int next = (last_mark+1) % SYNC_MARKS;
8832
8833 mddev->resync_mark = mark[next];
8834 mddev->resync_mark_cnt = mark_cnt[next];
8835 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008836 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008837 last_mark = next;
8838 }
8839
NeilBrownc91abf52013-11-19 12:02:01 +11008840 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8841 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008842
8843 /*
8844 * this loop exits only if either when we are slower than
8845 * the 'hard' speed limit, or the system was IO-idle for
8846 * a jiffy.
8847 * the system might be non-idle CPU-wise, but we only care
8848 * about not overloading the IO subsystem. (things like an
8849 * e2fsck being done on the RAID array should execute fast)
8850 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008851 cond_resched();
8852
Xiao Niac7e50a2014-08-07 09:37:41 -04008853 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8854 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008855 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008856
NeilBrown88202a02006-01-06 00:21:36 -08008857 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008858 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008859 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008860 goto repeat;
8861 }
NeilBrownac8fa412015-02-19 16:55:00 +11008862 if (!is_mddev_idle(mddev, 0)) {
8863 /*
8864 * Give other IO more of a chance.
8865 * The faster the devices, the less we wait.
8866 */
8867 wait_event(mddev->recovery_wait,
8868 !atomic_read(&mddev->recovery_active));
8869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008870 }
8871 }
NeilBrown9d487392016-11-02 14:16:49 +11008872 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8873 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8874 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008875 /*
8876 * this also signals 'finished resyncing' to md_stop
8877 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008878 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008879 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8880
NeilBrown5ed1df22015-07-24 13:27:08 +10008881 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8882 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008883 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008884 mddev->curr_resync_completed = mddev->curr_resync;
8885 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8886 }
NeilBrown09314792015-02-19 16:04:40 +11008887 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888
NeilBrowndfc70642008-05-23 13:04:39 -07008889 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008890 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008891 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8892 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8893 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008894 pr_debug("md: checkpointing %s of %s.\n",
8895 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008896 if (test_bit(MD_RECOVERY_ERROR,
8897 &mddev->recovery))
8898 mddev->recovery_cp =
8899 mddev->curr_resync_completed;
8900 else
8901 mddev->recovery_cp =
8902 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008903 }
8904 } else
8905 mddev->recovery_cp = MaxSector;
8906 } else {
8907 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8908 mddev->curr_resync = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11008909 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8910 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
8911 rcu_read_lock();
8912 rdev_for_each_rcu(rdev, mddev)
8913 if (rdev->raid_disk >= 0 &&
8914 mddev->delta_disks >= 0 &&
8915 !test_bit(Journal, &rdev->flags) &&
8916 !test_bit(Faulty, &rdev->flags) &&
8917 !test_bit(In_sync, &rdev->flags) &&
8918 rdev->recovery_offset < mddev->curr_resync)
8919 rdev->recovery_offset = mddev->curr_resync;
8920 rcu_read_unlock();
8921 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008923 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008924 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008925 /* set CHANGE_PENDING here since maybe another update is needed,
8926 * so other nodes are informed. It should be harmless for normal
8927 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008928 set_mask_bits(&mddev->sb_flags, 0,
8929 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008930
BingJing Chang88763912018-02-22 13:34:46 +08008931 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8932 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8933 mddev->delta_disks > 0 &&
8934 mddev->pers->finish_reshape &&
8935 mddev->pers->size &&
8936 mddev->queue) {
8937 mddev_lock_nointr(mddev);
8938 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
8939 mddev_unlock(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08008940 if (!mddev_is_clustered(mddev)) {
8941 set_capacity(mddev->gendisk, mddev->array_sectors);
8942 revalidate_disk(mddev->gendisk);
8943 }
BingJing Chang88763912018-02-22 13:34:46 +08008944 }
8945
NeilBrown23da4222014-12-15 12:57:01 +11008946 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008947 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8948 /* We completed so min/max setting can be forgotten if used. */
8949 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8950 mddev->resync_min = 0;
8951 mddev->resync_max = MaxSector;
8952 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8953 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008954 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008955 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008956 spin_unlock(&mddev->lock);
8957
Linus Torvalds1da177e2005-04-16 15:20:36 -07008958 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008959 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008960 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008961}
NeilBrown29269552006-03-27 01:18:10 -08008962EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008963
NeilBrown746d3202013-04-24 11:42:41 +10008964static int remove_and_add_spares(struct mddev *mddev,
8965 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008966{
NeilBrown3cb03002011-10-11 16:45:26 +11008967 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008968 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008969 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008970 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008971
NeilBrown39772f02018-02-03 09:19:30 +11008972 if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
8973 /* Mustn't remove devices when resync thread is running */
8974 return 0;
8975
NeilBrownd787be42016-06-02 16:19:53 +10008976 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008977 if ((this == NULL || rdev == this) &&
8978 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008979 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008980 test_bit(Faulty, &rdev->flags) &&
8981 atomic_read(&rdev->nr_pending)==0) {
8982 /* Faulty non-Blocked devices with nr_pending == 0
8983 * never get nr_pending incremented,
8984 * never get Faulty cleared, and never get Blocked set.
8985 * So we can synchronize_rcu now rather than once per device
8986 */
8987 remove_some = true;
8988 set_bit(RemoveSynchronized, &rdev->flags);
8989 }
8990 }
8991
8992 if (remove_some)
8993 synchronize_rcu();
8994 rdev_for_each(rdev, mddev) {
8995 if ((this == NULL || rdev == this) &&
8996 rdev->raid_disk >= 0 &&
8997 !test_bit(Blocked, &rdev->flags) &&
8998 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008999 (!test_bit(In_sync, &rdev->flags) &&
9000 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10009001 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009002 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11009003 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10009004 sysfs_unlink_rdev(mddev, rdev);
NeilBrown011abdc2018-04-26 14:46:29 +10009005 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009006 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11009007 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009008 }
9009 }
NeilBrownd787be42016-06-02 16:19:53 +10009010 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
9011 clear_bit(RemoveSynchronized, &rdev->flags);
9012 }
9013
Jonathan Brassow90584fc2013-03-07 16:24:26 -06009014 if (removed && mddev->kobj.sd)
9015 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009016
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05009017 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10009018 goto no_add;
9019
NeilBrowndafb20f2012-03-19 12:46:39 +11009020 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05009021 if (this && this != rdev)
9022 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009023 if (test_bit(Candidate, &rdev->flags))
9024 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11009025 if (rdev->raid_disk >= 0 &&
9026 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07009027 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11009028 !test_bit(Faulty, &rdev->flags))
9029 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009030 if (rdev->raid_disk >= 0)
9031 continue;
9032 if (test_bit(Faulty, &rdev->flags))
9033 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009034 if (!test_bit(Journal, &rdev->flags)) {
9035 if (mddev->ro &&
9036 ! (rdev->saved_raid_disk >= 0 &&
9037 !test_bit(Bitmap_sync, &rdev->flags)))
9038 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009039
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009040 rdev->recovery_offset = 0;
9041 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10009042 if (mddev->pers->
9043 hot_add_disk(mddev, rdev) == 0) {
9044 if (sysfs_link_rdev(mddev, rdev))
9045 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11009046 if (!test_bit(Journal, &rdev->flags))
9047 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10009048 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08009049 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07009050 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009051 }
NeilBrown746d3202013-04-24 11:42:41 +10009052no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10009053 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08009054 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009055 return spares;
9056}
NeilBrown7ebc0be2011-01-14 09:14:33 +11009057
NeilBrownac05f252014-09-30 08:10:42 +10009058static void md_start_sync(struct work_struct *ws)
9059{
9060 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05009061
NeilBrownac05f252014-09-30 08:10:42 +10009062 mddev->sync_thread = md_register_thread(md_do_sync,
9063 mddev,
9064 "resync");
9065 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11009066 pr_warn("%s: could not start resync thread...\n",
9067 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10009068 /* leave the spares where they are, it shouldn't hurt */
9069 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9070 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9071 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9072 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
9073 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11009074 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10009075 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9076 &mddev->recovery))
9077 if (mddev->sysfs_action)
9078 sysfs_notify_dirent_safe(mddev->sysfs_action);
9079 } else
9080 md_wakeup_thread(mddev->sync_thread);
9081 sysfs_notify_dirent_safe(mddev->sysfs_action);
9082 md_new_event(mddev);
9083}
9084
Linus Torvalds1da177e2005-04-16 15:20:36 -07009085/*
9086 * This routine is regularly called by all per-raid-array threads to
9087 * deal with generic issues like resync and super-block update.
9088 * Raid personalities that don't have a thread (linear/raid0) do not
9089 * need this as they never do any recovery or update the superblock.
9090 *
9091 * It does not do any resync itself, but rather "forks" off other threads
9092 * to do that as needed.
9093 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
9094 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07009095 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07009096 * and wakeups up this thread which will reap the thread and finish up.
9097 * This thread also removes any faulty devices (with nr_pending == 0).
9098 *
9099 * The overall approach is:
9100 * 1/ if the superblock needs updating, update it.
9101 * 2/ If a recovery thread is running, don't do anything else.
9102 * 3/ If recovery has finished, clean up, possibly marking spares active.
9103 * 4/ If there are any faulty devices, remove them.
9104 * 5/ If array is degraded, try to add spares devices
9105 * 6/ If array has spares or is not in-sync, start a resync thread.
9106 */
NeilBrownfd01b882011-10-11 16:47:53 +11009107void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009108{
NeilBrown059421e2018-10-03 15:04:41 +10009109 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
9110 /* Write superblock - thread that called mddev_suspend()
9111 * holds reconfig_mutex for us.
9112 */
9113 set_bit(MD_UPDATING_SB, &mddev->flags);
9114 smp_mb__after_atomic();
9115 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
9116 md_update_sb(mddev, 0);
9117 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
9118 wake_up(&mddev->sb_wait);
9119 }
9120
Jonathan Brassow68866e42011-06-08 15:10:08 +10009121 if (mddev->suspended)
9122 return;
9123
NeilBrown5f404022005-06-21 17:17:16 -07009124 if (mddev->bitmap)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009125 md_bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009126
NeilBrownfca4d842005-06-21 17:17:11 -07009127 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07009128 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11009129 pr_debug("md: %s in immediate safe mode\n",
9130 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07009131 mddev->safemode = 2;
9132 }
9133 flush_signals(current);
9134 }
9135
NeilBrownc89a8ee2008-08-05 15:54:13 +10009136 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
9137 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009138 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08009139 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07009140 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07009141 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07009142 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11009143 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07009144 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009145 ))
9146 return;
NeilBrownfca4d842005-06-21 17:17:11 -07009147
NeilBrowndf5b89b2006-03-27 01:18:20 -08009148 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009149 int spares = 0;
NeilBrown480523f2019-08-20 10:21:09 +10009150 bool try_set_sync = mddev->safemode != 0;
NeilBrownfca4d842005-06-21 17:17:11 -07009151
Shaohua Liafc1f552017-08-11 20:34:45 -07009152 if (!mddev->external && mddev->safemode == 1)
NeilBrown33182d12017-08-08 16:56:36 +10009153 mddev->safemode = 0;
9154
NeilBrownc89a8ee2008-08-05 15:54:13 +10009155 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10009156 struct md_rdev *rdev;
9157 if (!mddev->external && mddev->in_sync)
9158 /* 'Blocked' flag not needed as failed devices
9159 * will be recorded if array switched to read/write.
9160 * Leaving it set will prevent the device
9161 * from being removed.
9162 */
9163 rdev_for_each(rdev, mddev)
9164 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10009165 /* On a read-only array we can:
9166 * - remove failed devices
9167 * - add already-in_sync devices if the array itself
9168 * is in-sync.
9169 * As we only add devices that are already in-sync,
9170 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10009171 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10009172 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11009173 /* There is no thread, but we need to call
9174 * ->spare_active and clear saved_raid_disk
9175 */
NeilBrown2ac295a2014-05-29 11:40:03 +10009176 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11009177 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10009178 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11009179 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08009180 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10009181 goto unlock;
9182 }
9183
Guoqing Jiang659b2542015-12-21 10:50:59 +11009184 if (mddev_is_clustered(mddev)) {
9185 struct md_rdev *rdev;
9186 /* kick the device if another node issued a
9187 * remove disk.
9188 */
9189 rdev_for_each(rdev, mddev) {
9190 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
9191 rdev->raid_disk < 0)
9192 md_kick_rdev_from_array(rdev);
9193 }
9194 }
9195
NeilBrown480523f2019-08-20 10:21:09 +10009196 if (try_set_sync && !mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11009197 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11009198 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11009199 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07009200 }
NeilBrownfca4d842005-06-21 17:17:11 -07009201
Shaohua Li29530792016-12-08 15:48:19 -08009202 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07009203 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07009204
Linus Torvalds1da177e2005-04-16 15:20:36 -07009205 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
9206 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
9207 /* resync/recovery still happening */
9208 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9209 goto unlock;
9210 }
9211 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009212 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009213 goto unlock;
9214 }
Neil Brown72a23c22008-06-28 08:31:41 +10009215 /* Set RUNNING before clearing NEEDED to avoid
9216 * any transients in the value of "sync_action".
9217 */
NeilBrown72f36d52012-10-11 14:25:57 +11009218 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11009219 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10009220 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11009221 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08009222 /* Clear some bits that don't mean anything, but
9223 * might be left set
9224 */
NeilBrown24dd4692005-11-08 21:39:26 -08009225 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
9226 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009227
NeilBrowned209582012-04-24 10:23:14 +10009228 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
9229 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10009230 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231 /* no recovery is running.
9232 * remove any failed drives, then
9233 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11009234 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07009235 * the personality to fail the re-add.
9236 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009237
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009238 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10009239 if (mddev->pers->check_reshape == NULL ||
9240 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009241 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10009242 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009243 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009244 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10009245 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08009246 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9247 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07009248 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009249 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08009250 } else if (mddev->recovery_cp < MaxSector) {
9251 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009252 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08009253 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
9254 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10009255 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08009256
Linus Torvalds1da177e2005-04-16 15:20:36 -07009257 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10009258 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07009259 /* We are adding a device or devices to an array
9260 * which has the bitmap stored on all devices.
9261 * So make sure all bitmap pages get written
9262 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009263 md_bitmap_write_all(mddev->bitmap);
NeilBrowna654b9d82005-06-21 17:17:27 -07009264 }
NeilBrownac05f252014-09-30 08:10:42 +10009265 INIT_WORK(&mddev->del_work, md_start_sync);
9266 queue_work(md_misc_wq, &mddev->del_work);
9267 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009268 }
NeilBrownac05f252014-09-30 08:10:42 +10009269 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10009270 if (!mddev->sync_thread) {
9271 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11009272 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10009273 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9274 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11009275 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10009276 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10009277 }
NeilBrownac05f252014-09-30 08:10:42 +10009278 unlock:
9279 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009280 mddev_unlock(mddev);
9281 }
9282}
NeilBrown6c144d32014-09-30 16:15:38 +10009283EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009284
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009285void md_reap_sync_thread(struct mddev *mddev)
9286{
9287 struct md_rdev *rdev;
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009288 sector_t old_dev_sectors = mddev->dev_sectors;
9289 bool is_reshaped = false;
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009290
9291 /* resync has finished, collect result */
9292 md_unregister_thread(&mddev->sync_thread);
9293 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
Guoqing Jiang0d8ed0e92019-07-24 11:09:21 +02009294 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
9295 mddev->degraded != mddev->raid_disks) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009296 /* success...*/
9297 /* activate any spares */
9298 if (mddev->pers->spare_active(mddev)) {
9299 sysfs_notify(&mddev->kobj, NULL,
9300 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08009301 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009302 }
9303 }
9304 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009305 mddev->pers->finish_reshape) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009306 mddev->pers->finish_reshape(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009307 if (mddev_is_clustered(mddev))
9308 is_reshaped = true;
9309 }
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009310
9311 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11009312 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009313 */
NeilBrownf4667222013-12-09 12:04:56 +11009314 if (!mddev->degraded)
9315 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009316 rdev->saved_raid_disk = -1;
9317
9318 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08009319 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04009320 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9321 * clustered raid */
9322 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
9323 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009324 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10009325 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009326 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9327 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9328 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9329 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009330 /*
9331 * We call md_cluster_ops->update_size here because sync_size could
9332 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9333 * so it is time to update size across cluster.
9334 */
9335 if (mddev_is_clustered(mddev) && is_reshaped
9336 && !test_bit(MD_CLOSING, &mddev->flags))
9337 md_cluster_ops->update_size(mddev, old_dev_sectors);
NeilBrownf851b602014-12-11 10:02:10 +11009338 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009339 /* flag recovery needed just to double check */
9340 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9341 sysfs_notify_dirent_safe(mddev->sysfs_action);
9342 md_new_event(mddev);
9343 if (mddev->event_work.func)
9344 queue_work(md_misc_wq, &mddev->event_work);
9345}
NeilBrown6c144d32014-09-30 16:15:38 +10009346EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009347
NeilBrownfd01b882011-10-11 16:47:53 +11009348void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07009349{
NeilBrown00bcb4a2010-06-01 19:37:23 +10009350 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07009351 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10009352 !test_bit(Blocked, &rdev->flags) &&
9353 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07009354 msecs_to_jiffies(5000));
9355 rdev_dec_pending(rdev, mddev);
9356}
9357EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9358
NeilBrownc6563a82012-05-21 09:27:00 +10009359void md_finish_reshape(struct mddev *mddev)
9360{
9361 /* called be personality module when reshape completes. */
9362 struct md_rdev *rdev;
9363
9364 rdev_for_each(rdev, mddev) {
9365 if (rdev->data_offset > rdev->new_data_offset)
9366 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9367 else
9368 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9369 rdev->data_offset = rdev->new_data_offset;
9370 }
9371}
9372EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10009373
Vishal Vermafc974ee2015-12-24 19:20:34 -07009374/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10009375
Vishal Vermafc974ee2015-12-24 19:20:34 -07009376/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11009377int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10009378 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009379{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04009380 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10009381 int rv;
9382 if (is_new)
9383 s += rdev->new_data_offset;
9384 else
9385 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07009386 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9387 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10009388 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009389 if (test_bit(ExternalBbl, &rdev->flags))
9390 sysfs_notify(&rdev->kobj, NULL,
9391 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11009392 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08009393 set_mask_bits(&mddev->sb_flags, 0,
9394 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10009395 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07009396 return 1;
9397 } else
9398 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10009399}
9400EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9401
NeilBrownc6563a82012-05-21 09:27:00 +10009402int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9403 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009404{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009405 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10009406 if (is_new)
9407 s += rdev->new_data_offset;
9408 else
9409 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009410 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9411 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
9412 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
9413 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10009414}
9415EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9416
Adrian Bunk75c96f82005-05-05 16:16:09 -07009417static int md_notify_reboot(struct notifier_block *this,
9418 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009419{
9420 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11009421 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009422 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009423
NeilBrownc744a652012-03-19 12:46:37 +11009424 for_each_mddev(mddev, tmp) {
9425 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10009426 if (mddev->pers)
9427 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009428 if (mddev->persistent)
9429 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009430 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009431 }
NeilBrownc744a652012-03-19 12:46:37 +11009432 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009433 }
NeilBrownc744a652012-03-19 12:46:37 +11009434 /*
9435 * certain more exotic SCSI devices are known to be
9436 * volatile wrt too early system reboots. While the
9437 * right place to handle this issue is the given
9438 * driver, we do want to have a safe RAID driver ...
9439 */
9440 if (need_delay)
9441 mdelay(1000*1);
9442
Linus Torvalds1da177e2005-04-16 15:20:36 -07009443 return NOTIFY_DONE;
9444}
9445
Adrian Bunk75c96f82005-05-05 16:16:09 -07009446static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009447 .notifier_call = md_notify_reboot,
9448 .next = NULL,
9449 .priority = INT_MAX, /* before any real devices */
9450};
9451
9452static void md_geninit(void)
9453{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009454 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009455
Alexey Dobriyan97a32532020-02-03 17:37:17 -08009456 proc_create("mdstat", S_IRUGO, NULL, &mdstat_proc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009457}
9458
Adrian Bunk75c96f82005-05-05 16:16:09 -07009459static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009460{
Tejun Heoe804ac72010-10-15 15:36:08 +02009461 int ret = -ENOMEM;
9462
Tejun Heoada609e2011-01-25 14:35:54 +01009463 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009464 if (!md_wq)
9465 goto err_wq;
9466
9467 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9468 if (!md_misc_wq)
9469 goto err_misc_wq;
9470
9471 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9472 goto err_md;
9473
9474 if ((ret = register_blkdev(0, "mdp")) < 0)
9475 goto err_mdp;
9476 mdp_major = ret;
9477
NeilBrownaf5628f2014-07-31 13:54:54 +10009478 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009479 md_probe, NULL, NULL);
9480 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009481 md_probe, NULL, NULL);
9482
Linus Torvalds1da177e2005-04-16 15:20:36 -07009483 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009484 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009485
9486 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009488
Tejun Heoe804ac72010-10-15 15:36:08 +02009489err_mdp:
9490 unregister_blkdev(MD_MAJOR, "md");
9491err_md:
9492 destroy_workqueue(md_misc_wq);
9493err_misc_wq:
9494 destroy_workqueue(md_wq);
9495err_wq:
9496 return ret;
9497}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009498
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009499static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009500{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009501 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9502 struct md_rdev *rdev2;
9503 int role, ret;
9504 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009505
Guoqing Jiang818da592017-03-01 16:42:40 +08009506 /*
9507 * If size is changed in another node then we need to
9508 * do resize as well.
9509 */
9510 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9511 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9512 if (ret)
9513 pr_info("md-cluster: resize failed\n");
9514 else
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009515 md_bitmap_update_sb(mddev->bitmap);
Guoqing Jiang818da592017-03-01 16:42:40 +08009516 }
9517
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009518 /* Check for change of roles in the active devices */
9519 rdev_for_each(rdev2, mddev) {
9520 if (test_bit(Faulty, &rdev2->flags))
9521 continue;
9522
9523 /* Check if the roles changed */
9524 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009525
9526 if (test_bit(Candidate, &rdev2->flags)) {
9527 if (role == 0xfffe) {
9528 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9529 md_kick_rdev_from_array(rdev2);
9530 continue;
9531 }
9532 else
9533 clear_bit(Candidate, &rdev2->flags);
9534 }
9535
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009536 if (role != rdev2->raid_disk) {
Guoqing Jiangca1e98e2018-10-18 16:37:45 +08009537 /*
9538 * got activated except reshape is happening.
9539 */
9540 if (rdev2->raid_disk == -1 && role != 0xffff &&
9541 !(le32_to_cpu(sb->feature_map) &
9542 MD_FEATURE_RESHAPE_ACTIVE)) {
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009543 rdev2->saved_raid_disk = role;
9544 ret = remove_and_add_spares(mddev, rdev2);
9545 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11009546 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04009547 /* wakeup mddev->thread here, so array could
9548 * perform resync with the new activated disk */
9549 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9550 md_wakeup_thread(mddev->thread);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009551 }
9552 /* device faulty
9553 * We just want to do the minimum to mark the disk
9554 * as faulty. The recovery is performed by the
9555 * one who initiated the error.
9556 */
9557 if ((role == 0xfffe) || (role == 0xfffd)) {
9558 md_error(mddev, rdev2);
9559 clear_bit(Blocked, &rdev2->flags);
9560 }
9561 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009562 }
9563
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009564 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9565 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009566
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009567 /*
9568 * Since mddev->delta_disks has already updated in update_raid_disks,
9569 * so it is time to check reshape.
9570 */
9571 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9572 (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9573 /*
9574 * reshape is happening in the remote node, we need to
9575 * update reshape_position and call start_reshape.
9576 */
Christoph Hellwiged4d0a4e2019-04-04 18:56:10 +02009577 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009578 if (mddev->pers->update_reshape_pos)
9579 mddev->pers->update_reshape_pos(mddev);
9580 if (mddev->pers->start_reshape)
9581 mddev->pers->start_reshape(mddev);
9582 } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9583 mddev->reshape_position != MaxSector &&
9584 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9585 /* reshape is just done in another node. */
9586 mddev->reshape_position = MaxSector;
9587 if (mddev->pers->update_reshape_pos)
9588 mddev->pers->update_reshape_pos(mddev);
9589 }
9590
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009591 /* Finally set the event to be up to date */
9592 mddev->events = le64_to_cpu(sb->events);
9593}
9594
9595static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9596{
9597 int err;
9598 struct page *swapout = rdev->sb_page;
9599 struct mdp_superblock_1 *sb;
9600
9601 /* Store the sb page of the rdev in the swapout temporary
9602 * variable in case we err in the future
9603 */
9604 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009605 err = alloc_disk_sb(rdev);
9606 if (err == 0) {
9607 ClearPageUptodate(rdev->sb_page);
9608 rdev->sb_loaded = 0;
9609 err = super_types[mddev->major_version].
9610 load_super(rdev, NULL, mddev->minor_version);
9611 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009612 if (err < 0) {
9613 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9614 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009615 if (rdev->sb_page)
9616 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009617 rdev->sb_page = swapout;
9618 rdev->sb_loaded = 1;
9619 return err;
9620 }
9621
9622 sb = page_address(rdev->sb_page);
9623 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9624 * is not set
9625 */
9626
9627 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9628 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9629
9630 /* The other node finished recovery, call spare_active to set
9631 * device In_sync and mddev->degraded
9632 */
9633 if (rdev->recovery_offset == MaxSector &&
9634 !test_bit(In_sync, &rdev->flags) &&
9635 mddev->pers->spare_active(mddev))
9636 sysfs_notify(&mddev->kobj, NULL, "degraded");
9637
9638 put_page(swapout);
9639 return 0;
9640}
9641
9642void md_reload_sb(struct mddev *mddev, int nr)
9643{
9644 struct md_rdev *rdev;
9645 int err;
9646
9647 /* Find the rdev */
9648 rdev_for_each_rcu(rdev, mddev) {
9649 if (rdev->desc_nr == nr)
9650 break;
9651 }
9652
9653 if (!rdev || rdev->desc_nr != nr) {
9654 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9655 return;
9656 }
9657
9658 err = read_rdev(mddev, rdev);
9659 if (err < 0)
9660 return;
9661
9662 check_sb_changes(mddev, rdev);
9663
9664 /* Read all rdev's to update recovery_offset */
Guoqing Jiang0ea99242018-04-09 17:01:21 +08009665 rdev_for_each_rcu(rdev, mddev) {
9666 if (!test_bit(Faulty, &rdev->flags))
9667 read_rdev(mddev, rdev);
9668 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009669}
9670EXPORT_SYMBOL(md_reload_sb);
9671
Linus Torvalds1da177e2005-04-16 15:20:36 -07009672#ifndef MODULE
9673
9674/*
9675 * Searches all registered partitions for autorun RAID arrays
9676 * at boot time.
9677 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009678
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009679static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009680static LIST_HEAD(all_detected_devices);
9681struct detected_devices_node {
9682 struct list_head list;
9683 dev_t dev;
9684};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009685
9686void md_autodetect_dev(dev_t dev)
9687{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009688 struct detected_devices_node *node_detected_dev;
9689
9690 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9691 if (node_detected_dev) {
9692 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009693 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009694 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009695 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009697}
9698
Linus Torvalds1da177e2005-04-16 15:20:36 -07009699static void autostart_arrays(int part)
9700{
NeilBrown3cb03002011-10-11 16:45:26 +11009701 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009702 struct detected_devices_node *node_detected_dev;
9703 dev_t dev;
9704 int i_scanned, i_passed;
9705
9706 i_scanned = 0;
9707 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009708
NeilBrown9d487392016-11-02 14:16:49 +11009709 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009710
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009711 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009712 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9713 i_scanned++;
9714 node_detected_dev = list_entry(all_detected_devices.next,
9715 struct detected_devices_node, list);
9716 list_del(&node_detected_dev->list);
9717 dev = node_detected_dev->dev;
9718 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009719 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009720 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009721 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009722 if (IS_ERR(rdev))
9723 continue;
9724
NeilBrown403df472014-09-30 15:52:29 +10009725 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009726 continue;
NeilBrown403df472014-09-30 15:52:29 +10009727
NeilBrownd0fae182008-03-04 14:29:31 -08009728 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009729 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009730 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009731 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009732 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009733
NeilBrown9d487392016-11-02 14:16:49 +11009734 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735
9736 autorun_devices(part);
9737}
9738
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009739#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009740
9741static __exit void md_exit(void)
9742{
NeilBrownfd01b882011-10-11 16:47:53 +11009743 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009744 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009745 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009746
NeilBrownaf5628f2014-07-31 13:54:54 +10009747 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009748 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009749
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009750 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009751 unregister_blkdev(mdp_major, "mdp");
9752 unregister_reboot_notifier(&md_notifier);
9753 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009754
9755 /* We cannot unload the modules while some process is
9756 * waiting for us in select() or poll() - wake them up
9757 */
9758 md_unloading = 1;
9759 while (waitqueue_active(&md_event_waiters)) {
9760 /* not safe to leave yet */
9761 wake_up(&md_event_waiters);
9762 msleep(delay);
9763 delay += delay;
9764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009765 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009766
NeilBrown29ac4aa2008-02-06 01:39:58 -08009767 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009768 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009769 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009770 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009771 /*
9772 * for_each_mddev() will call mddev_put() at the end of each
9773 * iteration. As the mddev is now fully clear, this will
9774 * schedule the mddev for destruction by a workqueue, and the
9775 * destroy_workqueue() below will wait for that to complete.
9776 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009777 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009778 destroy_workqueue(md_misc_wq);
9779 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009780}
9781
Dan Williams685784a2007-07-09 11:56:42 -07009782subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009783module_exit(md_exit)
9784
Kees Cooke4dca7b2017-10-17 19:04:42 -07009785static int get_ro(char *buffer, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009786{
9787 return sprintf(buffer, "%d", start_readonly);
9788}
Kees Cooke4dca7b2017-10-17 19:04:42 -07009789static int set_ro(const char *val, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009790{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009791 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009792}
9793
NeilBrown80ca3a42006-07-10 04:44:18 -07009794module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9795module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009796module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009797module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009798
Linus Torvalds1da177e2005-04-16 15:20:36 -07009799MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009800MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009801MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009802MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);